Skip to content

Commit

Permalink
Normalize and test summing of objective scores
Browse files Browse the repository at this point in the history
  • Loading branch information
ottoblep committed Mar 11, 2025
1 parent 61ebd76 commit 18b8723
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions include/simulation/SolverResult.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ struct ScoreSet {

double get_score() const;
double get_collision_score() const;
double get_stop_score() const;
double get_destination_score() const;
double get_norm_stop_score() const;
double get_norm_destination_score() const;
};

class SolverResult {
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/Objectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ double cda_rail::sim::collision_penalty(const TrainTrajectorySet& traj_set) {
* Train position is assumed to be the center of the train
*
* @param traj_set Set of train trajectories
* @return Normalized penalty score from 0 to 1, lower is better
* @return Penalty score, lower is better
*/

constexpr double safety_distance = 100;
Expand Down
10 changes: 6 additions & 4 deletions src/simulation/RoutingSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ void cda_rail::sim::ScoreHistory::export_csv(
for (auto it = begin(); it != end(); it++) {
const ScoreSet& set = std::get<1>(*it);
csvfile << std::get<0>(*it).count() << "," << set.get_score() << ","
<< set.get_collision_score() << "," << set.get_destination_score()
<< "," << set.get_stop_score() << "," << std::endl;
<< set.get_collision_score() << ","
<< set.get_norm_destination_score() << ","
<< set.get_norm_stop_score() << "," << std::endl;
}

csvfile.close();
Expand All @@ -26,8 +27,9 @@ void cda_rail::sim::ScoreHistoryCollection::export_csv(
for (auto it = (*hist_it).begin(); it != (*hist_it).end(); it++) {
const ScoreSet& set = std::get<1>(*it);
csvfile << std::get<0>(*it).count() << "," << set.get_score() << ","
<< set.get_collision_score() << "," << set.get_destination_score()
<< "," << set.get_stop_score() << "," << std::endl;
<< set.get_collision_score() << ","
<< set.get_norm_destination_score() << ","
<< set.get_norm_stop_score() << "," << std::endl;
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/simulation/SolverResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ cda_rail::sim::SolverResult::SolverResult(const SimulationInstance& instance,
"Solutions and Trajectories are not the same size");

for (auto const& [train_name, traj] : trajectories.get_map()) {
double pen = stop_penalty(traj);
scores.stop_scores.insert_or_assign(train_name, pen);
scores.stop_scores.insert_or_assign(train_name, stop_penalty(traj));
}

for (auto const& [train_name, traj] : trajectories.get_map()) {
Expand Down Expand Up @@ -54,8 +53,8 @@ double cda_rail::sim::ScoreSet::get_score() const {
double score_sum = 0;

score_sum += get_collision_score();
score_sum += get_destination_score();
score_sum += get_stop_score();
score_sum += get_norm_destination_score();
score_sum += get_norm_stop_score();

return score_sum;
}
Expand All @@ -64,22 +63,22 @@ double cda_rail::sim::ScoreSet::get_collision_score() const {
return collision_score;
}

double cda_rail::sim::ScoreSet::get_stop_score() const {
double cda_rail::sim::ScoreSet::get_norm_stop_score() const {
double score_sum = 0;
for (auto const& [train_name, score] : stop_scores) {
score_sum = score_sum + score;
}

return score_sum;
return score_sum / (double)stop_scores.size();
}

double cda_rail::sim::ScoreSet::get_destination_score() const {
double cda_rail::sim::ScoreSet::get_norm_destination_score() const {
double score_sum = 0;
for (auto const& [train_name, score] : destination_scores) {
score_sum = score_sum + score;
}

return score_sum;
return score_sum / (double)stop_scores.size();
}

const cda_rail::sim::ScoreSet&
Expand Down
13 changes: 9 additions & 4 deletions test/test_simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,15 @@ TEST(Simulation, Penalties) {
for (int i = 0; i < 100; i++) {
sim::RoutingSolutionSet solution_set{instance, rng_engine};
sim::TrainTrajectorySet traj{instance, solution_set};
sim::collision_penalty(traj);
sim::destination_penalty(traj);
sim::stop_penalty(traj);
sim::SolverResult res{instance, solution_set};
sim::ScoreSet scores = res.get_score_set();
ASSERT_DOUBLE_EQ(sim::collision_penalty(traj),
scores.get_collision_score());
ASSERT_DOUBLE_EQ(sim::destination_penalty(traj),
scores.get_norm_destination_score());
ASSERT_DOUBLE_EQ(sim::stop_penalty(traj), scores.get_norm_stop_score());
ASSERT_DOUBLE_EQ(sim::combined_objective(traj), scores.get_score());
traj.export_csv("tmp/test_traj_penalty.csv");
}
}

Expand Down Expand Up @@ -263,7 +269,6 @@ TEST(Simulation, SolverResult) {
sim::RoutingSolution round_sol{instance, train, rng_engine};
sim::TrainTrajectory round_traj{instance, train, round_sol};
result.insert_or_assign(round_sol, round_traj);
ASSERT_GE(result.get_score_set().get_score(), previous_score);
}
}
}
Expand Down

0 comments on commit 18b8723

Please sign in to comment.