Skip to content

Commit d0bbab9

Browse files
committed
Normalize and test summing of objective scores
1 parent 61ebd76 commit d0bbab9

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

include/simulation/SolverResult.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ struct ScoreSet {
1313

1414
double get_score() const;
1515
double get_collision_score() const;
16-
double get_stop_score() const;
17-
double get_destination_score() const;
16+
double get_norm_stop_score() const;
17+
double get_norm_destination_score() const;
1818
};
1919

2020
class SolverResult {

src/simulation/Objectives.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ double cda_rail::sim::collision_penalty(const TrainTrajectorySet& traj_set) {
1414
* Train position is assumed to be the center of the train
1515
*
1616
* @param traj_set Set of train trajectories
17-
* @return Normalized penalty score from 0 to 1, lower is better
17+
* @return Penalty score, lower is better
1818
*/
1919

2020
constexpr double safety_distance = 100;

src/simulation/RoutingSolver.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ void cda_rail::sim::ScoreHistory::export_csv(
99
for (auto it = begin(); it != end(); it++) {
1010
const ScoreSet& set = std::get<1>(*it);
1111
csvfile << std::get<0>(*it).count() << "," << set.get_score() << ","
12-
<< set.get_collision_score() << "," << set.get_destination_score()
13-
<< "," << set.get_stop_score() << "," << std::endl;
12+
<< set.get_collision_score() << ","
13+
<< set.get_norm_destination_score() << ","
14+
<< set.get_norm_stop_score() << "," << std::endl;
1415
}
1516

1617
csvfile.close();
@@ -26,8 +27,9 @@ void cda_rail::sim::ScoreHistoryCollection::export_csv(
2627
for (auto it = (*hist_it).begin(); it != (*hist_it).end(); it++) {
2728
const ScoreSet& set = std::get<1>(*it);
2829
csvfile << std::get<0>(*it).count() << "," << set.get_score() << ","
29-
<< set.get_collision_score() << "," << set.get_destination_score()
30-
<< "," << set.get_stop_score() << "," << std::endl;
30+
<< set.get_collision_score() << ","
31+
<< set.get_norm_destination_score() << ","
32+
<< set.get_norm_stop_score() << "," << std::endl;
3133
}
3234
}
3335

src/simulation/SolverResult.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ cda_rail::sim::SolverResult::SolverResult(const SimulationInstance& instance,
1414
"Solutions and Trajectories are not the same size");
1515

1616
for (auto const& [train_name, traj] : trajectories.get_map()) {
17-
double pen = stop_penalty(traj);
18-
scores.stop_scores.insert_or_assign(train_name, pen);
17+
scores.stop_scores.insert_or_assign(train_name, stop_penalty(traj));
1918
}
2019

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

5655
score_sum += get_collision_score();
57-
score_sum += get_destination_score();
58-
score_sum += get_stop_score();
56+
score_sum += get_norm_destination_score();
57+
score_sum += get_norm_stop_score();
5958

6059
return score_sum;
6160
}
@@ -64,22 +63,22 @@ double cda_rail::sim::ScoreSet::get_collision_score() const {
6463
return collision_score;
6564
}
6665

67-
double cda_rail::sim::ScoreSet::get_stop_score() const {
66+
double cda_rail::sim::ScoreSet::get_norm_stop_score() const {
6867
double score_sum = 0;
6968
for (auto const& [train_name, score] : stop_scores) {
7069
score_sum = score_sum + score;
7170
}
7271

73-
return score_sum;
72+
return score_sum / (double)stop_scores.size();
7473
}
7574

76-
double cda_rail::sim::ScoreSet::get_destination_score() const {
75+
double cda_rail::sim::ScoreSet::get_norm_destination_score() const {
7776
double score_sum = 0;
7877
for (auto const& [train_name, score] : destination_scores) {
7978
score_sum = score_sum + score;
8079
}
8180

82-
return score_sum;
81+
return score_sum / (double)stop_scores.size();
8382
}
8483

8584
const cda_rail::sim::ScoreSet&

test/test_simulation.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,14 @@ TEST(Simulation, Penalties) {
230230
for (int i = 0; i < 100; i++) {
231231
sim::RoutingSolutionSet solution_set{instance, rng_engine};
232232
sim::TrainTrajectorySet traj{instance, solution_set};
233-
sim::collision_penalty(traj);
234-
sim::destination_penalty(traj);
235-
sim::stop_penalty(traj);
233+
sim::SolverResult res{instance, solution_set};
234+
sim::ScoreSet scores = res.get_score_set();
235+
ASSERT_NEAR(sim::collision_penalty(traj), scores.get_collision_score(),
236+
EPS);
237+
ASSERT_NEAR(sim::destination_penalty(traj),
238+
scores.get_norm_destination_score(), EPS);
239+
ASSERT_NEAR(sim::stop_penalty(traj), scores.get_norm_stop_score(), EPS);
240+
traj.export_csv("tmp/test_traj_penalty.csv");
236241
}
237242
}
238243

@@ -263,7 +268,6 @@ TEST(Simulation, SolverResult) {
263268
sim::RoutingSolution round_sol{instance, train, rng_engine};
264269
sim::TrainTrajectory round_traj{instance, train, round_sol};
265270
result.insert_or_assign(round_sol, round_traj);
266-
ASSERT_GE(result.get_score_set().get_score(), previous_score);
267271
}
268272
}
269273
}

0 commit comments

Comments
 (0)