Skip to content

Commit

Permalink
refactor(Verbose): Remove verbose level logic
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukiMoriarty committed Apr 10, 2024
1 parent 9bc6ed0 commit 7e49291
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion calmapf/include/instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct Instance {
// Assign agent group
void assign_agent_group();
// Simple feasibility check of instance
void _is_valid(int verbose = 0);
void _is_valid();
// Check if reached port
bool is_port(Vertex* port) const;

Expand Down
9 changes: 7 additions & 2 deletions calmapf/include/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,28 @@ struct Log {
std::ofstream step_output_handler;
std::ofstream visual_output_handler;

// Solution log
Solution step_solution;
Solution life_long_solution;
std::vector<std::vector<uint>> bit_status_log;

// Logger
std::shared_ptr<spdlog::logger> log_console;

Log(Parser* parser);
~Log();

bool update_solution(Solution& solution, std::vector<uint> bit_status);
bool is_feasible_solution(const Instance& ins, const int verbose = 0);
bool is_feasible_solution(const Instance& ins);
int get_makespan();
int get_path_cost(int i); // single-agent path cost
int get_sum_of_costs();
int get_sum_of_loss();
int get_makespan_lower_bound(const Instance& ins, DistTable& D);
int get_sum_of_costs_lower_bound(const Instance& ins, DistTable& D);
void print_stats(const int verbose, const Instance& ins, const double comp_time_ms);
void print_stats(const Instance& ins, const double comp_time_ms);

// File output function
void make_step_log(const Instance& ins, const std::string& output_name, const double comp_time_ms, const std::string& map_name, const int seed, const bool log_short = false);
void make_life_long_log(const Instance& ins, std::string visual_name);
void make_throughput_log(uint index, uint start_cnt, uint make_span);
Expand Down
1 change: 0 additions & 1 deletion calmapf/include/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ struct Parser {
std::string output_visual_file;

// Log settings
int verbose_level;
bool short_log_format;
bool debug_log;

Expand Down
7 changes: 2 additions & 5 deletions calmapf/include/planner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct Planner {
const Instance* ins;
const Deadline* deadline;
std::mt19937* MT;
const int verbose;

// solver utils
const int N; // number of agents
Expand All @@ -61,13 +60,11 @@ struct Planner {
Agents occupied_now; // for quick collision checking
Agents occupied_next; // for quick collision checking

Planner(const Instance* _ins, const Deadline* _deadline, std::mt19937* _MT,
int _verbose = 0);
Planner(const Instance* _ins, const Deadline* _deadline, std::mt19937* _MT);
Solution solve();
bool get_new_config(Node* S, Constraint* M);
bool funcPIBT(Agent* ai);
};

// main function
Solution solve(const Instance& ins, const int verbose = 0,
const Deadline* deadline = nullptr, std::mt19937* MT = nullptr);
Solution solve(const Instance& ins, const Deadline* deadline = nullptr, std::mt19937* MT = nullptr);
4 changes: 2 additions & 2 deletions calmapf/src/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ Instance::Instance(Parser* _parser) : graph(Graph(_parser)), parser(_parser)
}

// check instance
_is_valid(parser->verbose_level);
_is_valid();
}

void Instance::_is_valid(int verbose)
void Instance::_is_valid()
{
if (parser->num_agents != starts.size() || parser->num_agents != goals.size()) {
instance_console->error("invalid N, check instance nagents {} starts {} goals {}", parser->num_agents, starts.size(), goals.size());
Expand Down
9 changes: 4 additions & 5 deletions calmapf/src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ Log::Log(Parser* parser)
log_console->error("Failed to open file: {}", parser->output_throughput_file);
exit(1);
}
throughput_output_handler << parser->map_file << "," << parser->cache_type_input << "," << parser->goals_gen_strategy_input << "," << parser->num_goals << "," << parser->num_agents << "," << parser->random_seed << "," << parser->verbose_level << "," << parser->time_limit_sec << "," << parser->goals_max_m << "," << parser->goals_max_k << ",";
throughput_output_handler << parser->map_file << "," << parser->cache_type_input << "," << parser->goals_gen_strategy_input << "," << parser->num_goals << "," << parser->num_agents << "," << parser->random_seed << "," << "," << parser->time_limit_sec << "," << parser->goals_max_m << "," << parser->goals_max_k << ",";

// Open csv file
csv_output_handler.open(parser->output_csv_file, std::ios::app);
if (!csv_output_handler.is_open()) {
log_console->error("Failed to open file: {}", parser->output_csv_file);
exit(1);
}
csv_output_handler << parser->map_file << "," << parser->cache_type_input << "," << parser->look_ahead_num << "," << parser->delay_deadline_limit << "," << parser->goals_gen_strategy_input << "," << parser->num_goals << "," << parser->num_agents << "," << parser->random_seed << "," << parser->verbose_level << "," << parser->time_limit_sec << "," << parser->goals_max_m << "," << parser->goals_max_k << ",";
csv_output_handler << parser->map_file << "," << parser->cache_type_input << "," << parser->look_ahead_num << "," << parser->delay_deadline_limit << "," << parser->goals_gen_strategy_input << "," << parser->num_goals << "," << parser->num_agents << "," << parser->random_seed << "," << "," << parser->time_limit_sec << "," << parser->goals_max_m << "," << parser->goals_max_k << ",";

// Open step file
step_output_handler.open(parser->output_step_file, std::ios::app);
Expand Down Expand Up @@ -76,7 +76,7 @@ bool Log::update_solution(Solution& solution, std::vector<uint> bit_status)
return true;
}

bool Log::is_feasible_solution(const Instance& ins, const int verbose)
bool Log::is_feasible_solution(const Instance& ins)
{
if (step_solution.empty()) return true;

Expand Down Expand Up @@ -182,8 +182,7 @@ int Log::get_sum_of_costs_lower_bound(const Instance& ins,
return c;
}

void Log::print_stats(const int verbose, const Instance& ins,
const double comp_time_ms)
void Log::print_stats(const Instance& ins, const double comp_time_ms)
{
auto ceil = [](float x) { return std::ceil(x * 100) / 100; };

Expand Down
3 changes: 0 additions & 3 deletions calmapf/src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Parser::Parser(int argc, char* argv[]) {
program.add_argument("-na", "--num-agents").help("Number of agents to use.").required();
program.add_argument("-ac", "--agents-capacity").help("Capacity of agents.").default_value(std::string("100"));
program.add_argument("-rs", "--random-seed").help("Seed for random number generation. Defaults to 0.").default_value(std::string("0"));
program.add_argument("-vl", "--verbose-level").help("Set the verbose logging level. Defaults to 0.").default_value(std::string("0"));
program.add_argument("-tls", "--time-limit-sec").help("Time limit in seconds. Defaults to 10.").default_value(std::string("10"));
program.add_argument("-osrf", "--output-step-file").help("Path to the step result output file. Defaults to './result/step_result.txt'.").default_value(std::string("./result/step_result.txt"));
program.add_argument("-ocf", "--output-csv-file").help("Path to the throughput output file. Defaults to './result/throughput.csv'.").default_value(std::string("./result/result.csv"));
Expand Down Expand Up @@ -61,7 +60,6 @@ Parser::Parser(int argc, char* argv[]) {
output_throughput_file = program.get<std::string>("output-throughput-file");
output_visual_file = program.get<std::string>("visual-output-file");

verbose_level = std::stoi(program.get<std::string>("verbose-level"));
short_log_format = program.get<bool>("short-log-format");
debug_log = program.get<bool>("debug-log");

Expand Down Expand Up @@ -148,7 +146,6 @@ void Parser::_print() {
parser_console->info("Real disy file: {}", real_dist_file_path);
}
parser_console->info("Seed: {}", random_seed);
parser_console->info("Verbose: {}", verbose_level);
parser_console->info("Time limit (sec): {}", time_limit_sec);
parser_console->info("Step file: {}", output_step_file);
parser_console->info("CSV file: {}", output_csv_file);
Expand Down
8 changes: 3 additions & 5 deletions calmapf/src/planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ Node::~Node()
}
}

Planner::Planner(const Instance* _ins, const Deadline* _deadline,
std::mt19937* _MT, int _verbose)
Planner::Planner(const Instance* _ins, const Deadline* _deadline, std::mt19937* _MT)
: ins(_ins),
deadline(_deadline),
MT(_MT),
verbose(_verbose),
N(ins->parser->num_agents),
V_size(ins->graph.size()),
D(DistTable(ins)),
Expand Down Expand Up @@ -255,10 +253,10 @@ bool Planner::funcPIBT(Agent* ai)
return false;
}

Solution solve(const Instance& ins, const int verbose, const Deadline* deadline,
Solution solve(const Instance& ins, const Deadline* deadline,
std::mt19937* MT)
{
// info(1, verbose, "elapsed:", elapsed_ms(deadline), "ms\tpre-processing");
auto planner = Planner(&ins, deadline, MT, verbose);
auto planner = Planner(&ins, deadline, MT);
return planner.solve();
}
30 changes: 16 additions & 14 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,47 +54,46 @@ int main(int argc, char* argv[])

log.make_throughput_log(i, throughput_index_cnt, makespan);

// ternimal log
console->debug(
"----------------------------------------------------------------------"
"----------------------------------------");
// Ternimal log
console->debug("--------------------------------------------------------------------------------------------------------------");
console->debug("STEP: {}", makespan);
console->debug("STARTS: {}", ins.starts);
console->debug("GOALS: {}", ins.goals);

// reset time clock
// Reset time clock
assert(deadline.reset());

auto solution = solve(ins, parser.verbose_level - 1, &deadline, &parser.MT);
// Get solution
auto solution = solve(ins, &deadline, &parser.MT);
const auto comp_time_ms = deadline.elapsed_ms();

// failure
// Failure
if (solution.empty()) {
log.make_csv_log(.0, 0, nullptr, true);
console->error("failed to solve");
return 1;
}

// update step solution
// Update step solution
if (!log.update_solution(solution, ins.bit_status)) {
console->error("Update step solution fails!");
return 1;
}

// check feasibility
if (!log.is_feasible_solution(ins, parser.verbose_level)) {
// Check feasibility
if (!log.is_feasible_solution(ins)) {
console->error("invalid solution");
return 1;
}

// statistics
// Statistics
makespan += (solution.size() - 1);

// post processing
log.print_stats(parser.verbose_level, ins, comp_time_ms);
// Post processing
log.print_stats(ins, comp_time_ms);
log.make_step_log(ins, parser.output_step_file, comp_time_ms, parser.map_file, parser.random_seed, parser.short_log_format);

// assign new goals
// Assign new goals
if (is_cache(parser.cache_type)) {
nagents_with_new_goals = ins.update_on_reaching_goals_with_cache(solution, parser.num_goals - i, cache_access, cache_hit);
}
Expand All @@ -103,6 +102,7 @@ int main(int argc, char* argv[])
}
console->debug("Reached Goals: {}", nagents_with_new_goals);
}

// Get percentiles
std::vector<uint> step_percentiles = ins.compute_percentiles();

Expand All @@ -113,7 +113,9 @@ int main(int argc, char* argv[])
else {
console->info("Total Goals Reached: {:5} | Makespan: {:5} | P0 Steps: {:5} | P50 Steps: {:5} | P99 Steps: {:5}", parser.num_goals, makespan, step_percentiles[0], step_percentiles[2], step_percentiles[6]);
}

log.make_life_long_log(ins, parser.output_visual_file);
log.make_csv_log(total_cache_rate, makespan, &step_percentiles, false);

return 0;
}

0 comments on commit 7e49291

Please sign in to comment.