3
3
void cda_rail::sim::ScoreHistory::export_csv (
4
4
const std::filesystem::path& p) const {
5
5
std::ofstream csvfile (p);
6
- csvfile << " time,score" << std::endl;
6
+ csvfile << " time,overall_score,collision_score,destination_score,stop_score"
7
+ << std::endl;
7
8
8
9
for (auto it = begin (); it != end (); it++) {
9
- csvfile << std::get<0 >(*it).count () << " ," << std::get<1 >(*it) << std::endl;
10
+ const ScoreSet& set = std::get<1 >(*it);
11
+ 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;
10
14
}
11
15
12
16
csvfile.close ();
@@ -15,12 +19,15 @@ void cda_rail::sim::ScoreHistory::export_csv(
15
19
void cda_rail::sim::ScoreHistoryCollection::export_csv (
16
20
const std::filesystem::path& p) const {
17
21
std::ofstream csvfile (p);
18
- csvfile << " time,score" << std::endl;
22
+ csvfile << " time,overall_score,collision_score,destination_score,stop_score"
23
+ << std::endl;
19
24
20
25
for (auto hist_it = begin (); hist_it != end (); hist_it++) {
21
26
for (auto it = (*hist_it).begin (); it != (*hist_it).end (); it++) {
22
- csvfile << std::get<0 >(*it).count () << " ," << std::get<1 >(*it)
23
- << std::endl;
27
+ const ScoreSet& set = std::get<1 >(*it);
28
+ 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;
24
31
}
25
32
}
26
33
@@ -46,8 +53,9 @@ cda_rail::sim::RoutingSolver::random_local_search(
46
53
std::chrono::steady_clock::now ();
47
54
48
55
for (;;) {
49
- auto res = local_search (RoutingSolutionSet{instance, rng_engine}, params);
50
- double round_score = std::get<0 >(res).get_score ();
56
+ auto res = local_search (RoutingSolutionSet{instance, rng_engine}, params);
57
+ ScoreSet round_score_set = std::get<0 >(res).get_score_set ();
58
+ double round_score = round_score_set.get_score ();
51
59
52
60
std::chrono::steady_clock::time_point round_time =
53
61
std::chrono::steady_clock::now ();
@@ -58,7 +66,7 @@ cda_rail::sim::RoutingSolver::random_local_search(
58
66
best_result.emplace (std::get<0 >(res));
59
67
hist.push_back ({std::chrono::duration_cast<std::chrono::milliseconds>(
60
68
round_time - initial_time),
61
- best_score });
69
+ round_score_set });
62
70
}
63
71
64
72
if (std::chrono::duration_cast<std::chrono::seconds>(
@@ -89,7 +97,8 @@ cda_rail::sim::RoutingSolver::grasp_search(std::chrono::seconds max_search_time,
89
97
90
98
auto res =
91
99
local_search (RoutingSolutionSet{instance, rng_engine}, loc_params);
92
- double round_score = std::get<0 >(res).get_score ();
100
+ ScoreSet round_score_set = std::get<0 >(res).get_score_set ();
101
+ double round_score = round_score_set.get_score ();
93
102
94
103
std::chrono::steady_clock::time_point round_time =
95
104
std::chrono::steady_clock::now ();
@@ -100,7 +109,7 @@ cda_rail::sim::RoutingSolver::grasp_search(std::chrono::seconds max_search_time,
100
109
best_result.emplace (std::get<0 >(res));
101
110
hist.push_back ({std::chrono::duration_cast<std::chrono::milliseconds>(
102
111
round_time - initial_time),
103
- best_score });
112
+ round_score_set });
104
113
}
105
114
106
115
if (std::chrono::duration_cast<std::chrono::seconds>(
@@ -131,7 +140,8 @@ cda_rail::sim::RoutingSolver::random_search(
131
140
for (;;) {
132
141
SolverResult round_result{instance,
133
142
RoutingSolutionSet{instance, rng_engine}};
134
- double round_score = round_result.get_score ();
143
+ ScoreSet round_score_set = round_result.get_score_set ();
144
+ double round_score = round_score_set.get_score ();
135
145
136
146
std::chrono::steady_clock::time_point round_time =
137
147
std::chrono::steady_clock::now ();
@@ -147,7 +157,7 @@ cda_rail::sim::RoutingSolver::random_search(
147
157
best_result.emplace (round_result);
148
158
hist.push_back ({std::chrono::duration_cast<std::chrono::milliseconds>(
149
159
round_time - initial_time),
150
- best_score });
160
+ round_score_set });
151
161
}
152
162
153
163
if (max_stall_time.has_value ()) {
@@ -184,7 +194,12 @@ cda_rail::sim::RoutingSolver::greedy_search(
184
194
for (;;) {
185
195
std::optional<cda_rail::sim::SolverResult> round_result_opt =
186
196
greedy_solution (params);
187
- double round_score = round_result_opt.value ().get_score ();
197
+
198
+ if (!round_result_opt.has_value ())
199
+ continue ;
200
+
201
+ ScoreSet round_score_set = round_result_opt.value ().get_score_set ();
202
+ double round_score = round_score_set.get_score ();
188
203
189
204
std::chrono::steady_clock::time_point round_time =
190
205
std::chrono::steady_clock::now ();
@@ -201,7 +216,7 @@ cda_rail::sim::RoutingSolver::greedy_search(
201
216
auto total_time_spent =
202
217
std::chrono::duration_cast<std::chrono::milliseconds>(round_time -
203
218
initial_time);
204
- hist.push_back ({total_time_spent, best_score });
219
+ hist.push_back ({total_time_spent, round_score_set });
205
220
}
206
221
207
222
if (max_stall_time.has_value ()) {
@@ -233,22 +248,24 @@ cda_rail::sim::RoutingSolver::local_search(
233
248
ScoreHistory hist;
234
249
double sampling_range_fraction = params.start_sampling_range_fraction ;
235
250
SolverResult last_result{instance, starting_solution};
236
- double last_score = last_result.get_score ();
251
+ ScoreSet last_score_set = last_result.get_score_set ();
252
+ double last_score = last_score_set.get_score ();
237
253
hist.push_back ({std::chrono::duration_cast<std::chrono::milliseconds>(
238
254
std::chrono::steady_clock::now () - initial_time),
239
- last_score });
255
+ last_score_set });
240
256
241
257
while (sampling_range_fraction > params.abort_sampling_range_fraction ) {
242
258
RoutingSolutionSet new_sol = last_result.get_solutions ();
243
259
new_sol.perturb (instance, sampling_range_fraction, rng_engine);
244
260
SolverResult new_result{instance, new_sol};
261
+ ScoreSet new_score_set = new_result.get_score_set ();
245
262
246
- if (double new_score = new_result .get_score (); new_score < last_score) {
263
+ if (double new_score = new_score_set .get_score (); new_score < last_score) {
247
264
last_result = new_result;
248
265
last_score = new_score;
249
266
hist.push_back ({std::chrono::duration_cast<std::chrono::milliseconds>(
250
267
std::chrono::steady_clock::now () - initial_time),
251
- last_score });
268
+ new_score_set });
252
269
} else {
253
270
sampling_range_fraction =
254
271
sampling_range_fraction * params.contraction_coeff ;
@@ -268,22 +285,24 @@ cda_rail::sim::RoutingSolver::local_search(
268
285
ScoreHistory hist;
269
286
double sampling_range_fraction = params.start_sampling_range_fraction ;
270
287
SolverResult last_result{instance, starting_solution};
271
- double last_score = last_result.get_score ();
288
+ ScoreSet last_score_set = last_result.get_score_set ();
289
+ double last_score = last_score_set.get_score ();
272
290
hist.push_back ({std::chrono::duration_cast<std::chrono::milliseconds>(
273
291
std::chrono::steady_clock::now () - initial_time),
274
- last_score });
292
+ last_score_set });
275
293
276
294
while (sampling_range_fraction > params.abort_sampling_range_fraction ) {
277
295
RoutingSolutionSet new_sol = last_result.get_solutions ();
278
296
new_sol.perturb (instance, sampling_range_fraction, rng01);
279
297
SolverResult new_result{instance, new_sol};
298
+ ScoreSet new_score_set = new_result.get_score_set ();
280
299
281
- if (double new_score = new_result .get_score (); new_score < last_score) {
300
+ if (double new_score = new_score_set .get_score (); new_score < last_score) {
282
301
last_result = new_result;
283
302
last_score = new_score;
284
303
hist.push_back ({std::chrono::duration_cast<std::chrono::milliseconds>(
285
304
std::chrono::steady_clock::now () - initial_time),
286
- last_score });
305
+ new_score_set });
287
306
} else {
288
307
sampling_range_fraction =
289
308
sampling_range_fraction * params.contraction_coeff ;
@@ -320,7 +339,8 @@ cda_rail::sim::RoutingSolver::greedy_solution(GreedyParams params) {
320
339
RoutingSolution round_sol{instance, train, rng_engine};
321
340
TrainTrajectory round_traj{instance, train, round_sol};
322
341
result.insert_or_assign (round_sol, round_traj);
323
- double round_score = result.get_score ();
342
+ ScoreSet round_score_set = result.get_score_set ();
343
+ double round_score = round_score_set.get_score ();
324
344
325
345
std::chrono::steady_clock::time_point round_time =
326
346
std::chrono::steady_clock::now ();
@@ -411,7 +431,7 @@ void cda_rail::sim::RoutingSolver::init_genes(
411
431
bool cda_rail::sim::RoutingSolver::eval_solution (const RoutingSolutionSet& p,
412
432
MiddleCost& c) {
413
433
SolverResult round_result{instance, p};
414
- c.score = round_result.get_score ();
434
+ c.score = round_result.get_score_set (). get_score ();
415
435
return true ;
416
436
}
417
437
@@ -474,5 +494,8 @@ void cda_rail::sim::RoutingSolver::SO_report_generation(
474
494
std::chrono::milliseconds past_time =
475
495
std::chrono::duration_cast<std::chrono::milliseconds>(round_time -
476
496
starting_time);
477
- hist.push_back ({past_time, last_generation.best_total_cost });
497
+ auto best_individual =
498
+ last_generation.chromosomes [last_generation.best_chromosome_index ];
499
+ SolverResult best_result{instance, best_individual.genes };
500
+ hist.push_back ({past_time, best_result.get_score_set ()});
478
501
}
0 commit comments