-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjres_standard_solver.cpp
More file actions
455 lines (399 loc) · 21.3 KB
/
Copy pathjres_standard_solver.cpp
File metadata and controls
455 lines (399 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* @author popmonkey+jres@gmail.com
* @file src/jres_standard_solver.cpp
* @brief Standard solver for the JRES Solver library.
*/
#include "jres_standard_solver.hpp"
#include <algorithm>
#include <cmath>
#include <chrono>
#include "Highs.h"
static void add_consecutive_incentive(
Highs* highs,
const std::vector<jres::internal::TeamMember>& pool,
const std::map<std::pair<std::string, int>, int>& workVars,
size_t numStints,
double reward)
{
for (const auto &p : pool) {
if (p.maxStints <= 1) continue;
for (size_t s = 0; s < numStints - 1; ++s) {
if (workVars.count({p.name, s}) && workVars.count({p.name, s + 1})) {
int var_s = workVars.at({p.name, s});
int var_next = workVars.at({p.name, s + 1});
int consecutive_var = highs->getNumCol();
highs->addVar(0.0, 1.0);
highs->changeColIntegrality(consecutive_var, HighsVarType::kInteger);
highs->changeColCost(consecutive_var, reward);
// z <= x_s
highs->addRow(-kHighsInf, 0.0, 2, std::vector<int>{consecutive_var, var_s}.data(), std::vector<double>{1.0, -1.0}.data());
// z <= x_{s+1}
highs->addRow(-kHighsInf, 0.0, 2, std::vector<int>{consecutive_var, var_next}.data(), std::vector<double>{1.0, -1.0}.data());
// z >= x_s + x_{s+1} - 1
highs->addRow(-1.0, kHighsInf, 3, std::vector<int>{consecutive_var, var_s, var_next}.data(), std::vector<double>{1.0, -1.0, -1.0}.data());
}
}
}
}
JresStandardSolver::JresStandardSolver(const jres::internal::SolverInput& input, const JresSolverOptions& options)
: JresSolverBase(input, options)
{
m_highs = std::make_unique<Highs>();
// Set HiGHS Options
m_highs->setOptionValue("output_flag", false);
m_highs->setOptionValue("presolve", "on");
if (m_options.timeLimit > 0) {
m_highs->setOptionValue("time_limit", static_cast<double>(m_options.timeLimit));
}
m_highs->setOptionValue("mip_rel_gap", m_options.optimalityGap);
}
JresStandardSolver::~JresStandardSolver() = default;
void JresStandardSolver::add_participant_model(
Highs &highs,
const std::vector<jres::internal::TeamMember> &participants,
std::map<std::pair<std::string, int>, int>& workVars)
{
if (participants.empty()) return;
for (const auto &p : participants)
{
for (size_t s = 0; s < m_input.stints.size(); ++s)
{
auto stintStartTime = jres::internal::TimeHelpers::stringToTimePoint(m_input.stints[s].startTime);
std::string availabilityKey = jres::internal::TimeHelpers::timePointToKey(stintStartTime);
bool isUnavailable = false;
auto member_availability_it = m_input.availability.find(p.name);
if (member_availability_it != m_input.availability.end()) {
auto time_availability_it = member_availability_it->second.find(availabilityKey);
if (time_availability_it != member_availability_it->second.end()) {
if (time_availability_it->second == jres::internal::Availability::Unavailable) {
isUnavailable = true;
}
}
}
if (isUnavailable) {
continue; // Do not create a variable for unavailable participants
}
int workVarIdx = highs.getNumCol();
highs.addVar(0.0, 1.0); // Add binary variable
workVars[{p.name, s}] = workVarIdx;
double cost = 0.0;
if (member_availability_it != m_input.availability.end()) {
auto time_availability_it = member_availability_it->second.find(availabilityKey);
if (time_availability_it != member_availability_it->second.end()) {
if (time_availability_it->second == jres::internal::Availability::Preferred) {
cost = -1.0;
}
}
}
highs.changeColCost(workVarIdx, cost);
highs.changeColIntegrality(workVarIdx, HighsVarType::kInteger);
}
// --- Hard Constraint: Max Consecutive Stints ---
int maxConsecutive = p.maxStints;
if (maxConsecutive == 0 || m_input.stints.size() < static_cast<size_t>(maxConsecutive + 1)) continue; // No limit if maxStints is 0 (or less)
for (size_t s = 0; s <= m_input.stints.size() - (maxConsecutive + 1); ++s) {
std::vector<int> consIdx;
std::vector<double> consVal;
for (size_t i = 0; i < maxConsecutive + 1; ++i) { // Window of maxConsecutive + 1
if (workVars.count({p.name, s + i})) {
consIdx.push_back(workVars.at({p.name, s + i}));
consVal.push_back(1.0);
}
}
if (consIdx.empty()) continue; // No variables in this window for this driver
// Sum of window must be <= maxConsecutive
// This formulation is actually for "at most X stints in a window of X+1"
// So if maxConsecutive = 1, then at most 1 stint in a window of 2 (stints s and s+1)
// This means one stint can be driven, then there must be a break.
highs.addRow(-kHighsInf, maxConsecutive, (int)consIdx.size(), consIdx.data(), consVal.data());
}
}
}
jres::internal::SolverOutput JresStandardSolver::solve()
{
using namespace std::chrono;
auto startTotal = high_resolution_clock::now();
// --- Build Driver Model ---
add_participant_model(*m_highs, m_driverPool, m_driverWorkVars);
// --- Hard Constraint: Fair Share ---
const double num_stints = m_input.stints.size();
const double num_drivers = m_driverPool.size();
if (num_drivers > 0) {
const double min_stints_per_driver = std::floor((num_stints / num_drivers) / 4.0);
for (const auto &p : m_driverPool) {
std::vector<int> driver_stint_indices;
std::vector<double> driver_stint_values;
for (size_t s = 0; s < m_input.stints.size(); ++s) {
if (m_driverWorkVars.count({p.name, s})) {
driver_stint_indices.push_back(m_driverWorkVars.at({p.name, s}));
driver_stint_values.push_back(1.0);
}
}
if (!driver_stint_indices.empty()) {
m_highs->addRow(min_stints_per_driver, kHighsInf, (int)driver_stint_indices.size(), driver_stint_indices.data(), driver_stint_values.data());
}
}
}
// --- Add Coverage Constraints (One driver per stint) ---
for (size_t s = 0; s < m_input.stints.size(); ++s)
{
std::vector<int> indices;
std::vector<double> values;
for (const auto &p : m_driverPool)
{
if (m_driverWorkVars.count({p.name, s})) {
indices.push_back(m_driverWorkVars.at({p.name, s}));
values.push_back(1.0);
}
}
if (indices.empty()) {
throw std::runtime_error("Model is infeasible.");
}
m_highs->addRow(1.0, 1.0, (int)indices.size(), indices.data(), values.data());
}
// --- Add balancing variables and objective ---
const double avg_stints_per_driver = num_stints / num_drivers;
for (const auto &p : m_driverPool) {
std::vector<int> driver_stint_indices;
std::vector<double> driver_stint_values;
for (size_t s = 0; s < m_input.stints.size(); ++s) {
if (m_driverWorkVars.count({p.name, s})) {
driver_stint_indices.push_back(m_driverWorkVars.at({p.name, s}));
driver_stint_values.push_back(1.0);
}
}
if (driver_stint_indices.empty()) continue;
// Variable for total stints for this driver
int total_stints_var = m_highs->getNumCol();
m_highs->addVar(0.0, kHighsInf);
driver_stint_indices.push_back(total_stints_var);
driver_stint_values.push_back(-1.0);
m_highs->addRow(0.0, 0.0, (int)driver_stint_indices.size(), driver_stint_indices.data(), driver_stint_values.data());
// Deviation variables
int over_avg_var = m_highs->getNumCol();
m_highs->addVar(0.0, kHighsInf);
int under_avg_var = m_highs->getNumCol();
m_highs->addVar(0.0, kHighsInf);
// over_avg >= total_stints - avg
m_highs->addRow(0.0, kHighsInf, 2, std::vector<int>{over_avg_var, total_stints_var}.data(), std::vector<double>{1.0, -1.0}.data());
m_highs->changeRowBounds(m_highs->getNumRow() - 1, -avg_stints_per_driver, kHighsInf);
// under_avg >= avg - total_stints
m_highs->addRow(0.0, kHighsInf, 2, std::vector<int>{under_avg_var, total_stints_var}.data(), std::vector<double>{1.0, 1.0}.data());
m_highs->changeRowBounds(m_highs->getNumRow() - 1, avg_stints_per_driver, kHighsInf);
// Add to objective
m_highs->changeColCost(over_avg_var, 1.0);
m_highs->changeColCost(under_avg_var, 1.0);
}
// --- Incentivize Consecutive Stints ---
add_consecutive_incentive(m_highs.get(), m_driverPool, m_driverWorkVars, m_input.stints.size(), -2.0);
// --- Add Spotter Model (Integrated Mode) ---
if (m_options.spotterMode == JRES_SPOTTER_MODE_INTEGRATED)
{
if (m_spotterPool.empty() && !m_options.allowNoSpotter) {
throw std::runtime_error("Model is infeasible.");
}
add_participant_model(*m_highs, m_spotterPool, m_spotterWorkVars);
add_consecutive_incentive(m_highs.get(), m_spotterPool, m_spotterWorkVars, m_input.stints.size(), -2.0);
// Spotter Coverage
for (size_t s = 0; s < m_input.stints.size(); ++s) {
std::vector<int> indices;
std::vector<double> values;
for (const auto& p : m_spotterPool) {
if (m_spotterWorkVars.count({p.name, s})) {
indices.push_back(m_spotterWorkVars.at({p.name, s}));
values.push_back(1.0);
}
}
if (!indices.empty()) {
if (m_options.allowNoSpotter) {
m_highs->addRow(0.0, 1.0, (int)indices.size(), indices.data(), values.data());
} else {
m_highs->addRow(1.0, 1.0, (int)indices.size(), indices.data(), values.data());
}
} else if (!m_options.allowNoSpotter) {
throw std::runtime_error("Model is infeasible.");
}
}
for (const auto& p : m_input.teamMembers) {
if (p.isDriver && p.isSpotter) {
for (size_t s = 0; s < m_input.stints.size(); ++s) {
if (m_driverWorkVars.count({p.name, s}) && m_spotterWorkVars.count({p.name, s})) {
std::vector<int> idx = { m_driverWorkVars.at({p.name, s}), m_spotterWorkVars.at({p.name, s}) };
std::vector<double> val = {1.0, 1.0};
m_highs->addRow(0.0, 1.0, 2, idx.data(), val.data());
}
}
}
}
// --- Soft Constraint: Adjacency of spotter/driver duties ---
for (const auto& p : m_input.teamMembers) {
if (p.isDriver && p.isSpotter) {
for (size_t s = 0; s < m_input.stints.size(); ++s) {
if (m_spotterWorkVars.count({p.name, s}) == 0) continue;
auto spotterVar = m_spotterWorkVars.at({p.name, s});
// Adjacency with drive stint BEFORE
if (s > 0 && m_driverWorkVars.count({p.name, s - 1})) {
auto driverBeforeVar = m_driverWorkVars.at({p.name, s - 1});
int adjBeforeVar = m_highs->getNumCol();
m_highs->addVar(0.0, 1.0);
m_highs->changeColIntegrality(adjBeforeVar, HighsVarType::kInteger);
m_highs->changeColCost(adjBeforeVar, -0.5); // Reward for adjacency
// z <= x (spotter)
m_highs->addRow(-kHighsInf, 0.0, 2, std::vector<int>{adjBeforeVar, spotterVar}.data(), std::vector<double>{1.0, -1.0}.data());
// z <= y (driver before)
m_highs->addRow(-kHighsInf, 0.0, 2, std::vector<int>{adjBeforeVar, driverBeforeVar}.data(), std::vector<double>{1.0, -1.0}.data());
// z >= x + y - 1 => z - x - y >= -1
m_highs->addRow(-1.0, kHighsInf, 3, std::vector<int>{adjBeforeVar, spotterVar, driverBeforeVar}.data(), std::vector<double>{1.0, -1.0, -1.0}.data());
}
// Adjacency with drive stint AFTER
if (s < m_input.stints.size() - 1 && m_driverWorkVars.count({p.name, s + 1})) {
auto driverAfterVar = m_driverWorkVars.at({p.name, s + 1});
int adjAfterVar = m_highs->getNumCol();
m_highs->addVar(0.0, 1.0);
m_highs->changeColIntegrality(adjAfterVar, HighsVarType::kInteger);
m_highs->changeColCost(adjAfterVar, -0.5); // Reward for adjacency
// z <= x (spotter)
m_highs->addRow(-kHighsInf, 0.0, 2, std::vector<int>{adjAfterVar, spotterVar}.data(), std::vector<double>{1.0, -1.0}.data());
// z <= y (driver after)
m_highs->addRow(-kHighsInf, 0.0, 2, std::vector<int>{adjAfterVar, driverAfterVar}.data(), std::vector<double>{1.0, -1.0}.data());
// z >= x + y - 1 => z - x - y >= -1
m_highs->addRow(-1.0, kHighsInf, 3, std::vector<int>{adjAfterVar, spotterVar, driverAfterVar}.data(), std::vector<double>{1.0, -1.0, -1.0}.data());
}
}
}
}
}
auto endSetup = high_resolution_clock::now();
double setupDurationMs = duration<double, std::milli>(endSetup - startTotal).count();
auto solveStart = high_resolution_clock::now();
m_highs->run();
auto solveEnd = high_resolution_clock::now();
double driverSolveDurationMs = duration<double, std::milli>(solveEnd - solveStart).count();
jres::internal::SolverOutput output;
HighsModelStatus status = m_highs->getModelStatus();
// Populate stats regardless of outcome
const HighsInfo& info = m_highs->getInfo();
output.stats.modelColumns = m_highs->getNumCol();
output.stats.modelRows = m_highs->getNumRow();
output.stats.searchNodes = (int)info.mip_node_count;
output.stats.finalGap = info.mip_gap;
output.stats.setupDurationMs = setupDurationMs;
output.stats.driverSolveDurationMs = driverSolveDurationMs;
output.stats.spotterSolveDurationMs = 0.0; // will be populated by sequential solver
if (status == HighsModelStatus::kOptimal || status == HighsModelStatus::kTimeLimit) {
const auto& solution = m_highs->getSolution();
const std::vector<double>& colValues = solution.col_value;
for (size_t s = 0; s < m_input.stints.size(); ++s) {
jres::internal::ScheduleEntry entry;
entry.id = m_input.stints[s].id;
entry.startTime = m_input.stints[s].startTime;
entry.endTime = m_input.stints[s].endTime;
entry.driver = "N/A";
entry.spotter = "N/A";
for (const auto& p : m_driverPool) {
if (m_driverWorkVars.count({p.name, s}) && colValues[m_driverWorkVars.at({p.name, s})] > 0.5) {
entry.driver = p.name;
break;
}
}
output.schedule.push_back(entry);
}
if (m_options.spotterMode == JRES_SPOTTER_MODE_SEQUENTIAL && !m_spotterPool.empty()) {
Highs spotterSolver;
spotterSolver.setOptionValue("output_flag", false);
if (m_options.timeLimit > 0) {
spotterSolver.setOptionValue("time_limit", static_cast<double>(m_options.timeLimit));
}
spotterSolver.setOptionValue("mip_rel_gap", m_options.optimalityGap);
add_participant_model(spotterSolver, m_spotterPool, m_spotterWorkVars);
add_consecutive_incentive(&spotterSolver, m_spotterPool, m_spotterWorkVars, m_input.stints.size(), -2.0);
// --- Soft Constraint: Adjacency of spotter/driver duties ---
for (const auto& p : m_spotterPool) {
const auto member_it = std::find_if(m_input.teamMembers.begin(), m_input.teamMembers.end(),
[&](const jres::internal::TeamMember& tm){ return tm.name == p.name; });
if (member_it == m_input.teamMembers.end() || !member_it->isDriver) continue;
for (size_t s = 0; s < m_input.stints.size(); ++s) {
if (m_spotterWorkVars.count({p.name, s})) {
double cost = 0.0;
auto stintStartTime = jres::internal::TimeHelpers::stringToTimePoint(m_input.stints[s].startTime);
std::string availabilityKey = jres::internal::TimeHelpers::timePointToKey(stintStartTime);
auto member_availability_it = m_input.availability.find(p.name);
if (member_availability_it != m_input.availability.end()) {
auto time_availability_it = member_availability_it->second.find(availabilityKey);
if (time_availability_it != member_availability_it->second.end()) {
if (time_availability_it->second == jres::internal::Availability::Preferred) {
cost = -1.0;
}
}
}
// Adjacency reward
if (s > 0 && output.schedule[s - 1].driver == p.name) {
cost -= 0.5;
}
if (s < m_input.stints.size() - 1 && output.schedule[s + 1].driver == p.name) {
cost -= 0.5;
}
if (cost != 0.0) {
spotterSolver.changeColCost(m_spotterWorkVars.at({p.name, s}), cost);
}
}
}
}
for (size_t s = 0; s < m_input.stints.size(); ++s) {
std::vector<int> indices;
std::vector<double> values;
for (const auto& p : m_spotterPool) {
if (m_spotterWorkVars.count({p.name, s})) {
indices.push_back(m_spotterWorkVars.at({p.name, s}));
values.push_back(1.0);
}
}
if (!indices.empty()) {
if (m_options.allowNoSpotter) {
spotterSolver.addRow(0.0, 1.0, (int)indices.size(), indices.data(), values.data());
} else {
spotterSolver.addRow(1.0, 1.0, (int)indices.size(), indices.data(), values.data());
}
} else if (!m_options.allowNoSpotter) {
// This path should not be taken in sequential mode, because the driver schedule is already solved.
}
}
for (size_t s = 0; s < m_input.stints.size(); ++s) {
const std::string& driverName = output.schedule[s].driver;
if (driverName != "N/A" && m_spotterWorkVars.count({driverName, s})) {
spotterSolver.changeColBounds(m_spotterWorkVars.at({driverName, s}), 0.0, 0.0);
}
}
auto spotterStart = high_resolution_clock::now();
spotterSolver.run();
auto spotterEnd = high_resolution_clock::now();
output.stats.spotterSolveDurationMs = duration<double, std::milli>(spotterEnd - spotterStart).count();
HighsModelStatus spotterStatus = spotterSolver.getModelStatus();
if (spotterStatus == HighsModelStatus::kOptimal || spotterStatus == HighsModelStatus::kTimeLimit) {
const auto& spotterSolution = spotterSolver.getSolution();
const std::vector<double>& spotterColValues = spotterSolution.col_value;
for (size_t s = 0; s < m_input.stints.size(); ++s) {
for (const auto& p : m_spotterPool) {
if (m_spotterWorkVars.count({p.name, s}) && spotterColValues[m_spotterWorkVars.at({p.name, s})] > 0.5) {
output.schedule[s].spotter = p.name;
break;
}
}
}
}
} else if (m_options.spotterMode == JRES_SPOTTER_MODE_INTEGRATED) {
for (size_t s = 0; s < m_input.stints.size(); ++s) {
for (const auto& p : m_spotterPool) {
if (m_spotterWorkVars.count({p.name, s}) && colValues[m_spotterWorkVars.at({p.name, s})] > 0.5) {
output.schedule[s].spotter = p.name;
break;
}
}
}
}
} else if (status == HighsModelStatus::kInfeasible) {
throw std::runtime_error("Model is infeasible.");
}
output.teamMembers = m_input.teamMembers;
return output;
}