Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bin/
build/
develop-eggs/
dist/
results/
eggs/
lib/
lib64/
Expand Down
5 changes: 5 additions & 0 deletions apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ endmacro()

add_sim_executable(vss_generation_timetable_mip_testing)
add_sim_executable(vss_generation_timetable_mip_iterative_vss_testing)
add_sim_executable(vss_generation_timetable_simulator)
add_sim_executable(vss_generation_timetable_simulator_genetic_parameter_testing)
add_sim_executable(vss_generation_timetable_simulator_greedy_parameter_testing)
add_sim_executable(vss_generation_timetable_simulator_local_parameter_testing)
add_sim_executable(vss_generation_timetable_simulator_search_methods_testing)
add_sim_executable(vss_generation_timetable_iterative_parameter_testing)
add_sim_executable(vss_generation_timetable_using_mb_information_testing)
add_sim_executable(gen_po_moving_block_lazy_vss_gen_testing)
Expand Down
53 changes: 53 additions & 0 deletions apps/vss_generation_timetable_simulator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "simulation/RoutingSolver.hpp"

#include <filesystem>
#include <future>

Check warning on line 4 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:4:1 [misc-include-cleaner]

included header future is not used directly
#include <gsl/span>
#include <plog/Appenders/ColorConsoleAppender.h>
#include <plog/Formatters/TxtFormatter.h>
#include <plog/Initializers/ConsoleInitializer.h>

Check warning on line 8 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:8:1 [misc-include-cleaner]

included header ConsoleInitializer.h is not used directly
#include <plog/Log.h>
#include <string>
#include <thread>

Check warning on line 11 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:11:1 [misc-include-cleaner]

included header thread is not used directly

// 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) {

Check warning on line 18 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:18:13 [misc-include-cleaner]

no header providing "plog::get" is directly included
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

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:20:22 [misc-include-cleaner]

no header providing "plog::debug" is directly included

Check warning on line 20 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:20:11 [misc-include-cleaner]

no header providing "plog::init" is directly included
}

if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||

Check warning on line 23 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:23:48 [cppcoreguidelines-pro-bounds-pointer-arithmetic]

do not use pointer arithmetic

Check warning on line 23 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:23:14 [cppcoreguidelines-pro-bounds-pointer-arithmetic]

do not use pointer arithmetic

Check warning on line 23 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:23:7 [misc-include-cleaner]

no header providing "strcmp" is directly included
argc != 3) {
PLOGI << "Usage: vss_generation_timetable_simulator [NETWORK PATH] [OUTPUT "
"PATH]"
<< std::endl;

Check warning on line 27 in apps/vss_generation_timetable_simulator.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

apps/vss_generation_timetable_simulator.cpp:27:14 [performance-avoid-endl]

do not use 'std::endl' with streams; use '\n' instead
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)
224 changes: 224 additions & 0 deletions apps/vss_generation_timetable_simulator_genetic_parameter_testing.cpp
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) {

Check warning

Code scanning / CodeQL

Poorly documented large function Warning

Poorly documented function: fewer than 2% comments for a function of 208 lines.

Copilot Autofix

AI about 1 year ago

To address the issue, we will add comments to document the purpose and functionality of the main function and its key sections. This will include:

  1. A high-level comment at the start of the main function explaining its overall purpose.
  2. Inline comments for each major block of code to describe what it does.
  3. Comments for any non-obvious operations or parameters to clarify their intent.

These changes will improve the readability and maintainability of the function without altering its functionality.


Suggested changeset 1
apps/vss_generation_timetable_simulator_genetic_parameter_testing.cpp

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/vss_generation_timetable_simulator_genetic_parameter_testing.cpp b/apps/vss_generation_timetable_simulator_genetic_parameter_testing.cpp
--- a/apps/vss_generation_timetable_simulator_genetic_parameter_testing.cpp
+++ b/apps/vss_generation_timetable_simulator_genetic_parameter_testing.cpp
@@ -15,4 +15,8 @@
 int main(int argc, char** argv) {
-  auto args = gsl::span<char*>(argv, argc);
+  // Main function for running genetic parameter testing on a timetable simulation.
+  // This function imports the network and timetable, sets up simulation parameters,
+  // and performs multithreaded genetic algorithm optimization with varying crossover fractions.
 
+  // Parse command-line arguments for model and output paths.
+  auto args = gsl::span<char*>(argv, argc);
   const std::string model_path  = args[1];
@@ -22,2 +26,3 @@
 
+  // Import the network and timetable data.
   cda_rail::Network network =
@@ -27,4 +32,6 @@
 
+  // Initialize the simulation instance with the imported data.
   cda_rail::sim::SimulationInstance instance{network, timetable, false};
 
+  // Determine the number of available hardware threads for multithreading.
   size_t processor_count = std::thread::hardware_concurrency();
@@ -33,2 +40,3 @@
 
+  // Set up default genetic algorithm parameters.
   cda_rail::sim::GeneticParams ga_params{
@@ -44,2 +52,3 @@
   {
+    // Test different crossover fractions for the genetic algorithm.
     std::vector<double> xover_fractions = {0.01, 0.025, 0.1, 0.5, 0.7, 0.99};
@@ -47,2 +56,3 @@
     for (double xover_fraction : xover_fractions) {
+      // Initialize a collection to store score histories and a mutex for thread safety.
       cda_rail::sim::ScoreHistoryCollection score_coll;
@@ -50,4 +60,6 @@
 
+      // Update the crossover fraction in the genetic algorithm parameters.
       ga_params.xover_frac = xover_fraction;
 
+      // Launch worker threads to perform genetic algorithm optimization.
       std::vector<std::thread> workers;
@@ -57,2 +69,3 @@
 
+          // Perform multiple runs of the genetic algorithm and collect results.
           for (size_t sample_runs = 0; sample_runs < 3; sample_runs++) {
@@ -68,2 +81,3 @@
 
+      // Wait for all worker threads to finish.
       while (workers.size() > 0) {
@@ -73,2 +87,3 @@
 
+      // Save the score history to a CSV file for the current crossover fraction.
       auto save_path =
EOF
@@ -15,4 +15,8 @@
int main(int argc, char** argv) {
auto args = gsl::span<char*>(argv, argc);
// Main function for running genetic parameter testing on a timetable simulation.
// This function imports the network and timetable, sets up simulation parameters,
// and performs multithreaded genetic algorithm optimization with varying crossover fractions.

// Parse command-line arguments for model and output paths.
auto args = gsl::span<char*>(argv, argc);
const std::string model_path = args[1];
@@ -22,2 +26,3 @@

// Import the network and timetable data.
cda_rail::Network network =
@@ -27,4 +32,6 @@

// Initialize the simulation instance with the imported data.
cda_rail::sim::SimulationInstance instance{network, timetable, false};

// Determine the number of available hardware threads for multithreading.
size_t processor_count = std::thread::hardware_concurrency();
@@ -33,2 +40,3 @@

// Set up default genetic algorithm parameters.
cda_rail::sim::GeneticParams ga_params{
@@ -44,2 +52,3 @@
{
// Test different crossover fractions for the genetic algorithm.
std::vector<double> xover_fractions = {0.01, 0.025, 0.1, 0.5, 0.7, 0.99};
@@ -47,2 +56,3 @@
for (double xover_fraction : xover_fractions) {
// Initialize a collection to store score histories and a mutex for thread safety.
cda_rail::sim::ScoreHistoryCollection score_coll;
@@ -50,4 +60,6 @@

// Update the crossover fraction in the genetic algorithm parameters.
ga_params.xover_frac = xover_fraction;

// Launch worker threads to perform genetic algorithm optimization.
std::vector<std::thread> workers;
@@ -57,2 +69,3 @@

// Perform multiple runs of the genetic algorithm and collect results.
for (size_t sample_runs = 0; sample_runs < 3; sample_runs++) {
@@ -68,2 +81,3 @@

// Wait for all worker threads to finish.
while (workers.size() > 0) {
@@ -73,2 +87,3 @@

// Save the score history to a CSV file for the current crossover fraction.
auto save_path =
Copilot is powered by AI and may make mistakes. Always verify output.
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)
Loading
Loading