Skip to content

Commit

Permalink
refactor(Log): clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukiMoriarty committed Apr 12, 2024
1 parent 7e49291 commit 4bd8081
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
4 changes: 2 additions & 2 deletions calmapf/include/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct Log {
Log(Parser* parser);
~Log();

bool update_solution(Solution& solution, std::vector<uint> bit_status);
void update_solution(Solution& solution, std::vector<uint> bit_status);
bool is_feasible_solution(const Instance& ins);
int get_makespan();
int get_path_cost(int i); // single-agent path cost
Expand All @@ -38,6 +38,6 @@ struct Log {
// 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);
void make_throughput_log(uint index, uint* start_cnt, uint make_span);
void make_csv_log(double cache_hit_rate, uint make_span, std::vector<uint>* step_percentiles, bool failure);
};
8 changes: 4 additions & 4 deletions calmapf/src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Log::~Log() {
visual_output_handler.close();
}

bool Log::update_solution(Solution& solution, std::vector<uint> bit_status)
void Log::update_solution(Solution& solution, std::vector<uint> bit_status)
{
// Update step solution
step_solution = solution;
Expand All @@ -73,7 +73,7 @@ bool Log::update_solution(Solution& solution, std::vector<uint> bit_status)
}
}

return true;
return;
}

bool Log::is_feasible_solution(const Instance& ins)
Expand Down Expand Up @@ -296,10 +296,10 @@ void Log::make_life_long_log(const Instance& ins, std::string visual_name)
}
}

void Log::make_throughput_log(uint index, uint start_cnt, uint make_span)
void Log::make_throughput_log(uint index, uint* start_cnt, uint make_span)
{
double throughput = double(index) / double(make_span);
for (; start_cnt < make_span; start_cnt += 200) {
for (; *start_cnt < make_span; *start_cnt += 200) {
throughput_output_handler << throughput << ",";
}
}
Expand Down
7 changes: 2 additions & 5 deletions calmapf/src/planner.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "../include/planner.hpp"

Constraint::Constraint() : who(std::vector<int>()), where(Vertices()), depth(0)
{
}
Constraint::Constraint() : who(std::vector<int>()), where(Vertices()), depth(0) {}

Constraint::Constraint(Constraint* parent, int i, Vertex* v)
: who(parent->who), where(parent->where), depth(parent->depth + 1)
Expand Down Expand Up @@ -66,8 +64,7 @@ Planner::Planner(const Instance* _ins, const Deadline* _deadline, std::mt19937*
A(Agents(N, nullptr)),
occupied_now(Agents(V_size, nullptr)),
occupied_next(Agents(V_size, nullptr))
{
}
{}

Solution Planner::solve()
{
Expand Down
20 changes: 5 additions & 15 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,21 @@ int main(int argc, char* argv[])
batch_idx++;
// info output
auto current_time = std::chrono::steady_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(
current_time - timer)
.count();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - timer).count();

if (!parser.debug_log && batch_idx % 100 == 0 && cache_access > 0 && is_cache(parser.cache_type)) {
double cacheRate = static_cast<double>(cache_hit) / cache_access * 100.0;
console->info(
"Elapsed Time: {:5}ms | Goals Reached: {:5} | Cache Hit Rate: "
"{:2.2f}% | Steps Used: {:5}",
elapsed_time, i, cacheRate, makespan);
console->info("Elapsed Time: {:5}ms | Goals Reached: {:5} | Cache Hit Rate: {:2.2f}% | Steps Used: {:5}", elapsed_time, i, cacheRate, makespan);
// Reset the timer
timer = std::chrono::steady_clock::now();
}
else if (!parser.debug_log && batch_idx % 100 == 0 && !is_cache(parser.cache_type)) {
console->info(
"Elapsed Time: {:5}ms | Goals Reached: {:5} | Steps Used: {:5}",
elapsed_time, i, makespan);
console->info("Elapsed Time: {:5}ms | Goals Reached: {:5} | Steps Used: {:5}", elapsed_time, i, makespan);
// Reset the timer
timer = std::chrono::steady_clock::now();
}

log.make_throughput_log(i, throughput_index_cnt, makespan);
log.make_throughput_log(i, &throughput_index_cnt, makespan);

// Ternimal log
console->debug("--------------------------------------------------------------------------------------------------------------");
Expand All @@ -75,10 +68,7 @@ int main(int argc, char* argv[])
}

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

// Check feasibility
if (!log.is_feasible_solution(ins)) {
Expand Down

0 comments on commit 4bd8081

Please sign in to comment.