Skip to content

Commit

Permalink
add read distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukiMoriarty committed Mar 3, 2024
1 parent 3a96bea commit 2bd45c7
Show file tree
Hide file tree
Showing 9 changed files with 931,122 additions and 13 deletions.
8 changes: 5 additions & 3 deletions calmapf/include/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once
#include "utils.hpp"
#include "cache.hpp"
#include <map>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/discrete_distribution.hpp>

Expand All @@ -16,7 +17,8 @@ enum class GraphType {
// Goals generation type
enum class GoalGenerationType {
MK,
Zhang
Zhang,
Real,
};

struct Graph {
Expand All @@ -37,14 +39,14 @@ struct Graph {
std::shared_ptr<spdlog::logger> logger;

// Instructor with cache
Graph(const std::string& filename, std::shared_ptr<spdlog::logger> _logger, GoalGenerationType goal_generation_type, uint goals_m, uint goals_k, uint ngoals, CacheType cache_type, std::mt19937* _randomSeed = 0);
Graph(const std::string& filename, std::shared_ptr<spdlog::logger> _logger, GoalGenerationType goal_generation_type, std::string goal_real_file, uint goals_m, uint goals_k, uint ngoals, CacheType cache_type, std::mt19937* _randomSeed = 0);
// Destructor
~Graph();

GraphType get_graph_type(std::string type);
int size() const; // the number of vertices, |V|
Vertex* random_target_vertex(int group);
void fill_goals_list(GoalGenerationType goal_generation_type, int group, uint goals_m, uint goals_k, uint ngoals);
void fill_goals_list(GoalGenerationType goal_generation_type, std::string goal_real_file, int group, uint goals_m, uint goals_k, uint ngoals);
Vertex* get_next_goal(int group);
};

Expand Down
3 changes: 2 additions & 1 deletion calmapf/include/instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct Instance {
std::vector<uint> bit_status;

const uint nagents; // number of agents
std::vector<int> agent_group; // agents group
std::vector<int> agent_group; // agents group
const uint ngoals; // number of goals

std::shared_ptr<spdlog::logger> logger;
Expand All @@ -36,6 +36,7 @@ struct Instance {
std::mt19937* MT,
std::shared_ptr<spdlog::logger> _logger,
GoalGenerationType goals_generation_type,
std::string goal_real_file,
CacheType cache_type,
const uint _nagents = 1,
const uint _ngoals = 1,
Expand Down
57 changes: 55 additions & 2 deletions calmapf/src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,51 @@ std::vector<double> calculate_probabilities(int n) {

return probabilities;
}

// This is a general function that loads teh data file and compute the frequency of each product.
// The frequencies will be used to generate a sequence of products.
// The probability vector: prob_v[product_id] = frequency (0 if product_id never appears in the data).
std::vector<float> compute_frequency_from_file(std::string file_path) {
int total = 0;
int n_line = 0;
int largest_idx = 0;
std::string line;
std::map<int, float> product_frequency;
std::vector<float> prob_v;

std::ifstream file(file_path);
if (!file.is_open()) {
std::cerr << "Failed to open data file." << std::endl;
return prob_v;
}

while (getline(file, line)) {
if (n_line == 0) {
n_line++;
continue;
}
std::stringstream ss(line);
std::string field;
getline(ss, field, ','); // get the first element in each row (the product id).
int product_id = stoi(field);
product_frequency[product_id]++;
if (product_id > largest_idx) {
largest_idx = product_id;
}
total++;
n_line++;
}

for (auto it = product_frequency.begin(); it != product_frequency.end(); it++) {
it->second = it->second / total;
}

for (int i = 0;i <= largest_idx;i++) {
prob_v.push_back(product_frequency[i]);
}

return prob_v;
}
/* Help function end*/


Expand Down Expand Up @@ -74,6 +119,7 @@ Graph::Graph(
const std::string& filename,
std::shared_ptr<spdlog::logger> _logger,
GoalGenerationType goal_generation_type,
std::string goal_real_file,
uint goals_m,
uint goals_k,
uint ngoals,
Expand Down Expand Up @@ -348,7 +394,7 @@ Graph::Graph(
logger->info("Generating goals...");

for (int i = 0; i < group; i++) {
fill_goals_list(goal_generation_type, i, goals_m, goals_k, ngoals);
fill_goals_list(goal_generation_type, goal_real_file, i, goals_m, goals_k, ngoals);
}
}

Expand All @@ -364,7 +410,7 @@ Vertex* Graph::random_target_vertex(int group) {
return *it;
}

void Graph::fill_goals_list(GoalGenerationType goal_generation_type, int group, uint goals_m, uint goals_k, uint ngoals) {
void Graph::fill_goals_list(GoalGenerationType goal_generation_type, std::string goal_real_file, int group, uint goals_m, uint goals_k, uint ngoals) {
if (goal_generation_type == GoalGenerationType::MK) {
std::deque<Vertex*> sliding_window;
std::unordered_map<Vertex*, int> goal_count;
Expand Down Expand Up @@ -418,6 +464,13 @@ void Graph::fill_goals_list(GoalGenerationType goal_generation_type, int group,
goals_list[group].push_back(cargo_vertices[group][dist(*randomSeed)]);
}
}
else if (goal_generation_type == GoalGenerationType::Real) {
std::vector<float> prob_v = compute_frequency_from_file(goal_real_file);
boost::random::discrete_distribution<> dist(prob_v);
for (uint i = 0; i < ngoals; i++) {
goals_list[group].push_back(cargo_vertices[group][dist(*randomSeed)]);
}
}

logger->debug("Group {} goals {}", group, goals_list[group]);
}
Expand Down
3 changes: 2 additions & 1 deletion calmapf/src/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ Instance::Instance(
std::mt19937* MT,
std::shared_ptr<spdlog::logger> _logger,
GoalGenerationType goal_generation_type,
std::string goal_real_file,
CacheType cache_type,
const uint _nagents,
const uint _ngoals,
const uint goals_m,
const uint goals_k)
: graph(Graph(map_filename, _logger, goal_generation_type, goals_m, goals_k, _ngoals, cache_type, MT)),
: graph(Graph(map_filename, _logger, goal_generation_type, goal_real_file, goals_m, goals_k, _ngoals, cache_type, MT)),
starts(Config()),
goals(Config()),
nagents(_nagents),
Expand Down
Loading

0 comments on commit 2bd45c7

Please sign in to comment.