-
Notifications
You must be signed in to change notification settings - Fork 7
Moving Block Routing using Simulation #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
31f197b
d800eac
04210fa
63e4c04
e10bef4
9cbd9c7
7db83bb
69363af
8b7c619
6530858
5332f2a
b49bb21
5d6e6f8
11edff5
7655d90
da995bd
605dbab
68c0d39
086de94
76f4bd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ bin/ | |
| build/ | ||
| develop-eggs/ | ||
| dist/ | ||
| results/ | ||
| eggs/ | ||
| lib/ | ||
| lib64/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include "simulation/RoutingSolver.hpp" | ||
|
|
||
| #include <filesystem> | ||
| #include <future> | ||
| #include <gsl/span> | ||
| #include <plog/Appenders/ColorConsoleAppender.h> | ||
| #include <plog/Formatters/TxtFormatter.h> | ||
| #include <plog/Initializers/ConsoleInitializer.h> | ||
| #include <plog/Log.h> | ||
| #include <string> | ||
| #include <thread> | ||
|
|
||
| // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-array-to-pointer-decay,bugprone-exception-escape) | ||
|
|
||
| int main(int argc, char** argv) { | ||
| // Only log to console using std::cerr and std::cout respectively unless | ||
| // initialized differently | ||
| if (plog::get() == nullptr) { | ||
| static plog::ColorConsoleAppender<plog::TxtFormatter> console_appender; | ||
| plog::init(plog::debug, &console_appender); | ||
|
Check warning on line 20 in apps/vss_generation_timetable_simulator.cpp
|
||
| } | ||
|
|
||
| if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 || | ||
|
Check warning on line 23 in apps/vss_generation_timetable_simulator.cpp
|
||
| argc != 3) { | ||
| PLOGI << "Usage: vss_generation_timetable_simulator [NETWORK PATH] [OUTPUT " | ||
| "PATH]" | ||
| << std::endl; | ||
| std::exit(0); | ||
| } | ||
|
|
||
| auto args = gsl::span<char*>(argv, argc); | ||
|
|
||
| const std::string model_path = args[1]; | ||
| const std::string output_path = args[2]; | ||
|
|
||
| cda_rail::Network network = | ||
| cda_rail::Network::import_network(model_path + "/network"); | ||
| cda_rail::Timetable timetable = | ||
| cda_rail::Timetable::import_timetable(model_path + "/timetable", network); | ||
|
|
||
| cda_rail::sim::SimulationInstance instance{network, timetable, false}; | ||
|
|
||
| cda_rail::sim::RoutingSolver solver{instance}; | ||
|
|
||
| auto res = solver.greedy_search(std::chrono::seconds{6}, {}, | ||
| {std::chrono::milliseconds{50}}); | ||
|
|
||
| if (std::get<0>(res)) | ||
| std::get<0>(res).value().get_trajectories().export_csv(output_path + | ||
| "/result.csv"); | ||
| } | ||
|
|
||
| // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-array-to-pointer-decay,bugprone-exception-escape) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| #include "simulation/RoutingSolver.hpp" | ||
|
|
||
| #include <filesystem> | ||
| #include <future> | ||
| #include <gsl/span> | ||
| #include <plog/Appenders/ColorConsoleAppender.h> | ||
| #include <plog/Formatters/TxtFormatter.h> | ||
| #include <plog/Initializers/ConsoleInitializer.h> | ||
| #include <plog/Log.h> | ||
| #include <string> | ||
| #include <thread> | ||
|
|
||
| // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-array-to-pointer-decay,bugprone-exception-escape) | ||
|
|
||
| int main(int argc, char** argv) { | ||
| auto args = gsl::span<char*>(argv, argc); | ||
|
|
||
| const std::string model_path = args[1]; | ||
| const std::string output_path = args[2]; | ||
| const std::string model_name = | ||
| model_path.substr(model_path.find_last_of("/"), model_path.length()); | ||
|
|
||
| cda_rail::Network network = | ||
| cda_rail::Network::import_network(model_path + "/network"); | ||
| cda_rail::Timetable timetable = | ||
| cda_rail::Timetable::import_timetable(model_path + "/timetable", network); | ||
|
|
||
| cda_rail::sim::SimulationInstance instance{network, timetable, false}; | ||
|
|
||
| size_t processor_count = std::thread::hardware_concurrency(); | ||
| if (processor_count == 0) | ||
| processor_count = 1; | ||
|
|
||
| cda_rail::sim::GeneticParams ga_params{ | ||
| .is_multithread = false, | ||
| .population = 1000, | ||
| .gen_max = 20, | ||
| .stall_max = 5, | ||
| .n_elite = 10, | ||
| .xover_frac = 0.7, | ||
| .mut_rate = 0.1, | ||
| }; | ||
|
|
||
| { | ||
| std::vector<double> xover_fractions = {0.01, 0.025, 0.1, 0.5, 0.7, 0.99}; | ||
|
|
||
| for (double xover_fraction : xover_fractions) { | ||
| cda_rail::sim::ScoreHistoryCollection score_coll; | ||
| std::mutex hist_mutex; | ||
|
|
||
| ga_params.xover_frac = xover_fraction; | ||
|
|
||
| std::vector<std::thread> workers; | ||
| for (size_t process = 0; process < processor_count; process++) { | ||
| workers.push_back(std::thread{[&]() { | ||
| cda_rail::sim::RoutingSolver solver{instance}; | ||
|
|
||
| for (size_t sample_runs = 0; sample_runs < 3; sample_runs++) { | ||
| auto res = solver.genetic_search(ga_params); | ||
|
|
||
| if (std::get<0>(res)) { | ||
| const std::lock_guard<std::mutex> lock(hist_mutex); | ||
| score_coll.add(std::get<1>(res)); | ||
| } | ||
| } | ||
| }}); | ||
| } | ||
|
|
||
| while (workers.size() > 0) { | ||
| workers.back().join(); | ||
| workers.pop_back(); | ||
| } | ||
|
|
||
| auto save_path = | ||
| output_path + "/results/genetic_params/crossover/" + model_name; | ||
| cda_rail::is_directory_and_create(save_path); | ||
| score_coll.export_csv(save_path + "/score_hist_" + | ||
| std::to_string(xover_fraction).substr(0, 5) + | ||
| ".csv"); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| std::vector<double> mut_rates = {0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99}; | ||
|
|
||
| for (double mut_rate : mut_rates) { | ||
| cda_rail::sim::ScoreHistoryCollection score_coll; | ||
| std::mutex hist_mutex; | ||
|
|
||
| ga_params.mut_rate = mut_rate; | ||
|
|
||
| std::vector<std::thread> workers; | ||
| for (size_t process = 0; process < processor_count; process++) { | ||
| workers.push_back(std::thread{[&]() { | ||
| cda_rail::sim::RoutingSolver solver{instance}; | ||
|
|
||
| for (size_t sample_runs = 0; sample_runs < 3; sample_runs++) { | ||
| auto res = solver.genetic_search(ga_params); | ||
|
|
||
| if (std::get<0>(res)) { | ||
| const std::lock_guard<std::mutex> lock(hist_mutex); | ||
| score_coll.add(std::get<1>(res)); | ||
| } | ||
| } | ||
| }}); | ||
| } | ||
|
|
||
| while (workers.size() > 0) { | ||
| workers.back().join(); | ||
| workers.pop_back(); | ||
| } | ||
|
|
||
| auto save_path = | ||
| output_path + "/results/genetic_params/mut_rate/" + model_name; | ||
| cda_rail::is_directory_and_create(save_path); | ||
| score_coll.export_csv(save_path + "/score_hist_" + | ||
| std::to_string(mut_rate).substr(0, 5) + ".csv"); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| std::vector<size_t> pops = {10, 100, 1000}; | ||
|
|
||
| for (size_t pop : pops) { | ||
| cda_rail::sim::ScoreHistoryCollection score_coll; | ||
| std::mutex hist_mutex; | ||
|
|
||
| ga_params.population = pop; | ||
| ga_params.n_elite = (size_t)std::round(0.1 * pop); | ||
|
|
||
| std::vector<std::thread> workers; | ||
| for (size_t process = 0; process < processor_count; process++) { | ||
| workers.push_back(std::thread{[&]() { | ||
| cda_rail::sim::RoutingSolver solver{instance}; | ||
|
|
||
| for (size_t sample_runs = 0; sample_runs < 3; sample_runs++) { | ||
| auto res = solver.genetic_search(ga_params); | ||
|
|
||
| if (std::get<0>(res)) { | ||
| const std::lock_guard<std::mutex> lock(hist_mutex); | ||
| score_coll.add(std::get<1>(res)); | ||
| } | ||
| } | ||
| }}); | ||
| } | ||
|
|
||
| while (workers.size() > 0) { | ||
| workers.back().join(); | ||
| workers.pop_back(); | ||
| } | ||
|
|
||
| auto save_path = | ||
| output_path + "/results/genetic_params/pop/" + model_name; | ||
| cda_rail::is_directory_and_create(save_path); | ||
| score_coll.export_csv(save_path + "/score_hist_" + | ||
| std::to_string(pop).substr(0, 5) + ".csv"); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| std::vector<double> elites = {0.01, 0.05, 0.1, 0.25, 0.5}; | ||
|
|
||
| for (double elite : elites) { | ||
| cda_rail::sim::ScoreHistoryCollection score_coll; | ||
| std::mutex hist_mutex; | ||
|
|
||
| ga_params.n_elite = | ||
| (size_t)std::round(elite * (double)ga_params.population); | ||
|
|
||
| std::vector<std::thread> workers; | ||
| for (size_t process = 0; process < processor_count; process++) { | ||
| workers.push_back(std::thread{[&]() { | ||
| cda_rail::sim::RoutingSolver solver{instance}; | ||
|
|
||
| for (size_t sample_runs = 0; sample_runs < 3; sample_runs++) { | ||
| auto res = solver.genetic_search(ga_params); | ||
|
|
||
| if (std::get<0>(res)) { | ||
| const std::lock_guard<std::mutex> lock(hist_mutex); | ||
| score_coll.add(std::get<1>(res)); | ||
| } | ||
| } | ||
| }}); | ||
| } | ||
|
|
||
| while (workers.size() > 0) { | ||
| workers.back().join(); | ||
| workers.pop_back(); | ||
| } | ||
|
|
||
| auto save_path = | ||
| output_path + "/results/genetic_params/elite/" + model_name; | ||
| cda_rail::is_directory_and_create(save_path); | ||
| score_coll.export_csv(save_path + "/score_hist_" + | ||
| std::to_string(elite).substr(0, 5) + ".csv"); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| std::vector<bool> multithreads = {false, true}; | ||
|
|
||
| for (bool multithread : multithreads) { | ||
| cda_rail::sim::ScoreHistoryCollection score_coll; | ||
| std::mutex hist_mutex; | ||
|
|
||
| ga_params.is_multithread = multithread; | ||
|
|
||
| cda_rail::sim::RoutingSolver solver{instance}; | ||
|
|
||
| for (size_t sample_runs = 0; sample_runs < 5; sample_runs++) { | ||
| auto res = solver.genetic_search(ga_params); | ||
| score_coll.add(std::get<1>(res)); | ||
| } | ||
|
|
||
| auto save_path = | ||
| output_path + "/results/genetic_params/multithread/" + model_name; | ||
| cda_rail::is_directory_and_create(save_path); | ||
| score_coll.export_csv(save_path + "/score_hist_" + | ||
| std::to_string(multithread) + ".csv"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-array-to-pointer-decay,bugprone-exception-escape) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "simulation/RoutingSolver.hpp" | ||
|
|
||
| #include <filesystem> | ||
| #include <future> | ||
| #include <gsl/span> | ||
| #include <plog/Appenders/ColorConsoleAppender.h> | ||
| #include <plog/Formatters/TxtFormatter.h> | ||
| #include <plog/Initializers/ConsoleInitializer.h> | ||
| #include <plog/Log.h> | ||
| #include <string> | ||
| #include <thread> | ||
|
|
||
| // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-array-to-pointer-decay,bugprone-exception-escape) | ||
|
|
||
| int main(int argc, char** argv) { | ||
| auto args = gsl::span<char*>(argv, argc); | ||
|
|
||
| const std::string model_path = args[1]; | ||
| const std::string output_path = args[2]; | ||
| const std::string model_name = | ||
| model_path.substr(model_path.find_last_of("/"), model_path.length()); | ||
|
|
||
| cda_rail::Network network = | ||
| cda_rail::Network::import_network(model_path + "/network"); | ||
| cda_rail::Timetable timetable = | ||
| cda_rail::Timetable::import_timetable(model_path + "/timetable", network); | ||
|
|
||
| cda_rail::sim::SimulationInstance instance{network, timetable, false}; | ||
|
|
||
| size_t processor_count = std::thread::hardware_concurrency(); | ||
| if (processor_count == 0) | ||
| processor_count = 1; | ||
|
|
||
| std::vector<size_t> test_timeouts = {1, 2, 5, 10, 50, 100, 250}; | ||
|
|
||
| for (size_t train_to : test_timeouts) { | ||
| cda_rail::sim::ScoreHistoryCollection score_coll; | ||
| std::mutex hist_mutex; | ||
|
|
||
| std::vector<std::thread> workers; | ||
| for (size_t process = 0; process < processor_count; process++) { | ||
| workers.push_back(std::thread{[&]() { | ||
| cda_rail::sim::RoutingSolver solver{instance}; | ||
|
|
||
| for (size_t sample = 0; sample < std::floor(100 / processor_count); | ||
| sample++) { | ||
| auto res = | ||
| solver.greedy_search(std::chrono::seconds{10}, {}, | ||
| {std::chrono::milliseconds{train_to}}); | ||
|
|
||
| if (std::get<0>(res)) { | ||
| const std::lock_guard<std::mutex> lock(hist_mutex); | ||
| score_coll.add(std::get<1>(res)); | ||
| } | ||
| } | ||
| }}); | ||
| } | ||
|
|
||
| while (workers.size() > 0) { | ||
| workers.back().join(); | ||
| workers.pop_back(); | ||
| } | ||
|
|
||
| auto save_path = | ||
| output_path + "/results/greedy_params/stall_time/" + model_name; | ||
| cda_rail::is_directory_and_create(save_path); | ||
| score_coll.export_csv(save_path + "/score_hist_" + | ||
| std::to_string(train_to).substr(0, 5) + ".csv"); | ||
| } | ||
| } | ||
|
|
||
| // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-array-to-pointer-decay,bugprone-exception-escape) |
Check warning
Code scanning / CodeQL
Poorly documented large function Warning
Copilot Autofix
AI about 1 year ago
To address the issue, we will add comments to document the purpose and functionality of the
mainfunction and its key sections. This will include:mainfunction explaining its overall purpose.These changes will improve the readability and maintainability of the function without altering its functionality.