Skip to content

Commit

Permalink
refactor(Parser): Add parser struct, make main.cpp more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukiMoriarty committed Apr 8, 2024
1 parent 9b414b5 commit b2bfdf3
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 196 deletions.
2 changes: 2 additions & 0 deletions calmapf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ add_library(${PROJECT_NAME} STATIC ${SRCS})
target_compile_options(${PROJECT_NAME} PUBLIC -O3 -Wall -mtune=native -march=native)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
target_include_directories(${PROJECT_NAME} INTERFACE ./include)
target_link_libraries(${PROJECT_NAME} PUBLIC argparse)

1 change: 1 addition & 0 deletions calmapf/include/calmapf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#include "planner.hpp"
#include "log.hpp"
#include "utils.hpp"
#include "parser.hpp"
13 changes: 0 additions & 13 deletions calmapf/include/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/discrete_distribution.hpp>

// Graph Type
enum class GraphType {
SINGLE_PORT,
MULTI_PORT
};

// Goals generation type
enum class GoalGenerationType {
MK,
Zhang,
Real,
};

struct Graph {
Vertices V; // without nullptr
Vertices U; // with nullptr, i.e., |U| = width * height
Expand Down
55 changes: 55 additions & 0 deletions calmapf/include/parser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Parser definition
// Zhenghong Yu
#pragma once

#include <argparse/argparse.hpp>
#include "utils.hpp"

struct Parser {
// Map settings
std::string map_file;

// Cache settings
std::string cache_type_input;
CacheType cache_type;

int look_ahead_num;
int delay_deadline_limit;

// Goal settings
int num_goals;

std::string goals_gen_strategy_input;
GoalGenerationType goals_gen_strategy;

uint goals_max_k;
uint goals_max_m;
std::string real_dist_file_path;

// Instance settings
int num_agents;

int random_seed;
std::mt19937 MT;

int time_limit_sec;

// Output settings
std::string output_step_file;
std::string output_csv_file;
std::string output_throughput_file;
std::string output_visual_file;

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

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

Parser(int argc, char* argv[]);
void _post_parse();
void _check();
void _print();
};
6 changes: 3 additions & 3 deletions calmapf/include/planner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "instance.hpp"
#include "utils.hpp"

// low-level search node
// low-level search node
struct Constraint {
std::vector<int> who;
Vertices where;
Expand Down Expand Up @@ -62,12 +62,12 @@ struct Planner {
Agents occupied_next; // for quick collision checking

Planner(const Instance* _ins, const Deadline* _deadline, std::mt19937* _MT,
int _verbose = 0);
int _verbose = 0);
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);
const Deadline* deadline = nullptr, std::mt19937* MT = nullptr);
13 changes: 13 additions & 0 deletions calmapf/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,17 @@ struct fmt::formatter<std::vector<unsigned int>> {
}
return format_to(ctx.out(), "]");
}
};

// Graph Type
enum class GraphType {
SINGLE_PORT,
MULTI_PORT
};

// Goals generation type
enum class GoalGenerationType {
MK,
Zhang,
Real,
};
157 changes: 157 additions & 0 deletions calmapf/src/parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Parser implementation
// Zhenghong Yu

#include "../include/parser.hpp"

Parser::Parser(int argc, char* argv[]) {
parser_console = spdlog::stderr_color_mt("parser");
parser_console->set_level(spdlog::level::info);

// arguments definition
argparse::ArgumentParser program("CAL-MAPF", "0.1.0");
program.add_argument("-mf", "--map-file").help("Path to the map file.").required();
program.add_argument("-ct", "--cache-type").help("Type of cache to use: NONE, LRU, FIFO, RANDOM. Defaults to NONE.").default_value(std::string("NONE"));
program.add_argument("-lan", "--look-ahead-num").help("Number for look-ahead logic. Defaults to 1.").default_value(std::string("1"));
program.add_argument("-ddl", "--delay-deadline-limit").help("Delay deadline limit for task assignment. Defaults to 1.").default_value(std::string("1"));
program.add_argument("-ng", "--num-goals").help("Number of goals to achieve.").required();
program.add_argument("-ggs", "--goals-gen-strategy").help("Strategy for goals generation: MK, Zhang, Real.").required();
program.add_argument("-gmk", "--goals-max-k").help("Maximum 'k' different goals in 'm' segments of all goals. Defaults to 0.").default_value(std::string("0"));
program.add_argument("-gmm", "--goals-max-m").help("Maximum 'k' different goals in 'm' segments of all goals. Defaults to 100.").default_value(std::string("100"));
program.add_argument("-rdfp", "--real-dist-file-path").help("Path to the real distribution data file. Defaults to './data/order_data.csv'.").default_value(std::string("./data/order_data.csv"));
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"));
program.add_argument("-otf", "--output-throughput-file").help("Path to the throughput output file. Defaults to './result/throughput.csv'.").default_value(std::string("./result/throughput.csv"));
program.add_argument("-vof", "--visual-output-file").help("Path to the visual output file. Defaults to './result/vis.yaml'.").default_value(std::string("./result/vis.yaml"));
program.add_argument("-slf", "--short-log-format").help("Enable short log format. Implicitly true when set.").default_value(false).implicit_value(true);
program.add_argument("-dl", "--debug-log").help("Enable debug logging. Implicitly true when set.").default_value(false).implicit_value(true);

try {
program.parse_known_args(argc, argv);
}
catch (const std::runtime_error& err) {
parser_console->error("{}", err.what());
std::exit(1);
}

map_file = program.get<std::string>("map-file");

cache_type_input = program.get<std::string>("cache-type");
look_ahead_num = std::stoi(program.get<std::string>("look-ahead-num"));
delay_deadline_limit = std::stoi(program.get<std::string>("delay-deadline-limit"));

num_goals = std::stoi(program.get<std::string>("num-goals"));
goals_gen_strategy_input = program.get<std::string>("goals-gen-strategy");
goals_max_k = std::stoi(program.get<std::string>("goals-max-k"));
goals_max_m = std::stoi(program.get<std::string>("goals-max-m"));
real_dist_file_path = program.get<std::string>("real-dist-file-path");

num_agents = std::stoi(program.get<std::string>("num-agents"));
random_seed = std::stoi(program.get<std::string>("random-seed"));
time_limit_sec = std::stoi(program.get<std::string>("time-limit-sec"));

output_step_file = program.get<std::string>("output-step-file");
output_csv_file = program.get<std::string>("output-csv-file");
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");

// Post parse
_post_parse();

// Check
_check();

// Print
_print();
}

void Parser::_post_parse() {
// Set log level
if (debug_log) parser_console->set_level(spdlog::level::debug);

// Set cache type
cache_type = CacheType::NONE;
if (cache_type_input == "NONE") {
cache_type = CacheType::NONE;
}
else if (cache_type_input == "LRU") {
cache_type = CacheType::LRU;
}
else if (cache_type_input == "FIFO") {
cache_type = CacheType::FIFO;
}
else if (cache_type_input == "RANDOM") {
cache_type = CacheType::RANDOM;
}
else {
parser_console->error("Invalid cache type!");
exit(1);
}

// Set goal generation strategy
if (goals_gen_strategy_input == "MK") {
if (goals_max_k == 0 || goals_max_m == 0) {
parser_console->error("For MK generation, both --goals-m and --goals-k must have non-zero values.");
exit(1);
}
goals_gen_strategy = GoalGenerationType::MK;
}
else if (goals_gen_strategy_input == "Zhang") {
goals_gen_strategy = GoalGenerationType::Zhang;
}
else if (goals_gen_strategy_input == "Real") {
goals_gen_strategy = GoalGenerationType::Real;
}
else {
parser_console->error("Invalid goals_generation strategy specified.");
exit(1);
}

// Set MT
MT = std::mt19937(random_seed);
}

void Parser::_check() {
// Check paras
if (num_agents > num_goals) {
parser_console->error("number of goals must larger or equal to number of agents");
exit(1);
}
if (look_ahead_num < 1) {
parser_console->error("look ahead should be greater than 1");
exit(1);
}
}

void Parser::_print() {
parser_console->info("Map file: {}", map_file);
parser_console->info("Cache type: {}", cache_type_input);
parser_console->info("Look ahead: {}", look_ahead_num);
parser_console->info("Number of goals: {}", num_goals);
parser_console->info("Number of agents: {}", num_agents);
parser_console->info("Goal Generation: {}", goals_gen_strategy_input);
if (goals_gen_strategy == GoalGenerationType::MK) {
parser_console->info("Goals m: {}", goals_max_m);
parser_console->info("Goals k: {}", goals_max_k);
}
else if (goals_gen_strategy == GoalGenerationType::Real) {
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);
parser_console->info("Throughput file: {}", output_throughput_file);
parser_console->info("Visual file: {}", output_visual_file);
parser_console->info("Log short: {}", short_log_format);
parser_console->info("Debug: {}", debug_log);
}
Loading

0 comments on commit b2bfdf3

Please sign in to comment.