Repository navigation
Expand file tree
/
Copy pathtimetabling.cc
More file actions
370 lines (329 loc) · 11.8 KB
/
Copy pathtimetabling.cc
File metadata and controls
370 lines (329 loc) · 11.8 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
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/constraint_solver/timetabling.h"
#include <cstdint>
#include <string>
#include "absl/log/check.h"
#include "absl/strings/str_format.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/constraint_solver/interval.h"
namespace operations_research {
// ----- interval <unary relation> date -----
namespace {
const char* kUnaryNames[] = {
"ENDS_AFTER", "ENDS_AT", "ENDS_BEFORE", "STARTS_AFTER",
"STARTS_AT", "STARTS_BEFORE", "CROSS_DATE", "AVOID_DATE",
};
const char* kBinaryNames[] = {
"ENDS_AFTER_END", "ENDS_AFTER_START", "ENDS_AT_END",
"ENDS_AT_START", "STARTS_AFTER_END", "STARTS_AFTER_START",
"STARTS_AT_END", "STARTS_AT_START", "STAYS_IN_SYNC"};
} // namespace
IntervalUnaryRelation::IntervalUnaryRelation(Solver* s, IntervalVar* const t,
int64_t d,
Solver::UnaryIntervalRelation rel)
: Constraint(s), t_(t), d_(d), rel_(rel) {}
IntervalUnaryRelation::~IntervalUnaryRelation() {}
void IntervalUnaryRelation::Post() {
if (t_->MayBePerformed()) {
Demon* d = solver()->MakeConstraintInitialPropagateCallback(this);
t_->WhenAnything(d);
}
}
void IntervalUnaryRelation::InitialPropagate() {
if (t_->MayBePerformed()) {
switch (rel_) {
case Solver::ENDS_AFTER:
t_->SetEndMin(d_);
break;
case Solver::ENDS_AT:
t_->SetEndRange(d_, d_);
break;
case Solver::ENDS_BEFORE:
t_->SetEndMax(d_);
break;
case Solver::STARTS_AFTER:
t_->SetStartMin(d_);
break;
case Solver::STARTS_AT:
t_->SetStartRange(d_, d_);
break;
case Solver::STARTS_BEFORE:
t_->SetStartMax(d_);
break;
case Solver::CROSS_DATE:
t_->SetStartMax(d_);
t_->SetEndMin(d_);
break;
case Solver::AVOID_DATE:
if (t_->EndMin() > d_) {
t_->SetStartMin(d_);
} else if (t_->StartMax() < d_) {
t_->SetEndMax(d_);
}
break;
}
}
}
std::string IntervalUnaryRelation::DebugString() const {
return absl::StrFormat("(%s %s %d)", t_->DebugString(), kUnaryNames[rel_],
d_);
}
void IntervalUnaryRelation::Accept(ModelVisitor* visitor) const {
visitor->BeginVisitConstraint(ModelVisitor::kIntervalUnaryRelation, this);
visitor->VisitIntervalArgument(ModelVisitor::kIntervalArgument, t_);
visitor->VisitIntegerArgument(ModelVisitor::kRelationArgument, rel_);
visitor->VisitIntegerArgument(ModelVisitor::kValueArgument, d_);
visitor->EndVisitConstraint(ModelVisitor::kIntervalUnaryRelation, this);
}
// ----- interval <binary relation> interval -----
IntervalBinaryRelation::IntervalBinaryRelation(
Solver* s, IntervalVar* const t1, IntervalVar* const t2,
Solver::BinaryIntervalRelation rel, int64_t delay)
: Constraint(s), t1_(t1), t2_(t2), rel_(rel), delay_(delay) {}
IntervalBinaryRelation::~IntervalBinaryRelation() {}
void IntervalBinaryRelation::Post() {
if (t1_->MayBePerformed() && t2_->MayBePerformed()) {
Demon* d = solver()->MakeConstraintInitialPropagateCallback(this);
t1_->WhenAnything(d);
t2_->WhenAnything(d);
}
}
// TODO(user) : make code more compact, use function pointers?
void IntervalBinaryRelation::InitialPropagate() {
if (t2_->MustBePerformed() && t1_->MayBePerformed()) {
switch (rel_) {
case Solver::ENDS_AFTER_END:
t1_->SetEndMin(t2_->EndMin() + delay_);
break;
case Solver::ENDS_AFTER_START:
t1_->SetEndMin(t2_->StartMin() + delay_);
break;
case Solver::ENDS_AT_END:
t1_->SetEndRange(t2_->EndMin() + delay_, t2_->EndMax() + delay_);
break;
case Solver::ENDS_AT_START:
t1_->SetEndRange(t2_->StartMin() + delay_, t2_->StartMax() + delay_);
break;
case Solver::STARTS_AFTER_END:
t1_->SetStartMin(t2_->EndMin() + delay_);
break;
case Solver::STARTS_AFTER_START:
t1_->SetStartMin(t2_->StartMin() + delay_);
break;
case Solver::STARTS_AT_END:
t1_->SetStartRange(t2_->EndMin() + delay_, t2_->EndMax() + delay_);
break;
case Solver::STARTS_AT_START:
t1_->SetStartRange(t2_->StartMin() + delay_, t2_->StartMax() + delay_);
break;
case Solver::STAYS_IN_SYNC:
t1_->SetStartRange(t2_->StartMin() + delay_, t2_->StartMax() + delay_);
t1_->SetEndRange(t2_->EndMin() + delay_, t2_->EndMax() + delay_);
break;
}
}
if (t1_->MustBePerformed() && t2_->MayBePerformed()) {
switch (rel_) {
case Solver::ENDS_AFTER_END:
t2_->SetEndMax(t1_->EndMax() - delay_);
break;
case Solver::ENDS_AFTER_START:
t2_->SetStartMax(t1_->EndMax() - delay_);
break;
case Solver::ENDS_AT_END:
t2_->SetEndRange(t1_->EndMin() - delay_, t1_->EndMax() - delay_);
break;
case Solver::ENDS_AT_START:
t2_->SetStartRange(t1_->EndMin() - delay_, t1_->EndMax() - delay_);
break;
case Solver::STARTS_AFTER_END:
t2_->SetEndMax(t1_->StartMax() - delay_);
break;
case Solver::STARTS_AFTER_START:
t2_->SetStartMax(t1_->StartMax() - delay_);
break;
case Solver::STARTS_AT_END:
t2_->SetEndRange(t1_->StartMin() - delay_, t1_->StartMax() - delay_);
break;
case Solver::STARTS_AT_START:
t2_->SetStartRange(t1_->StartMin() - delay_, t1_->StartMax() - delay_);
break;
case Solver::STAYS_IN_SYNC:
t2_->SetStartRange(t1_->StartMin() - delay_, t1_->StartMax() - delay_);
t2_->SetEndRange(t1_->EndMin() - delay_, t1_->EndMax() - delay_);
break;
}
}
}
std::string IntervalBinaryRelation::DebugString() const {
return absl::StrFormat("(%s %s %s)", t1_->DebugString(), kBinaryNames[rel_],
t2_->DebugString());
}
void IntervalBinaryRelation::Accept(ModelVisitor* visitor) const {
visitor->BeginVisitConstraint(ModelVisitor::kIntervalBinaryRelation, this);
visitor->VisitIntervalArgument(ModelVisitor::kLeftArgument, t1_);
visitor->VisitIntegerArgument(ModelVisitor::kRelationArgument, rel_);
visitor->VisitIntervalArgument(ModelVisitor::kRightArgument, t2_);
visitor->EndVisitConstraint(ModelVisitor::kIntervalBinaryRelation, this);
}
// ----- activity a before activity b or activity b before activity a -----
TemporalDisjunction::TemporalDisjunction(Solver* s, IntervalVar* const t1,
IntervalVar* const t2,
IntVar* const alt)
: Constraint(s), t1_(t1), t2_(t2), alt_(alt), state_(UNDECIDED) {}
TemporalDisjunction::~TemporalDisjunction() {}
void TemporalDisjunction::Post() {
Solver* const s = solver();
Demon* d = MakeConstraintDemon0(s, this, &TemporalDisjunction::RangeDemon1,
"RangeDemon1");
t1_->WhenAnything(d);
d = MakeConstraintDemon0(s, this, &TemporalDisjunction::RangeDemon2,
"RangeDemon2");
t2_->WhenAnything(d);
if (alt_ != nullptr) {
d = MakeConstraintDemon0(s, this, &TemporalDisjunction::RangeAlt,
"RangeAlt");
alt_->WhenRange(d);
}
}
void TemporalDisjunction::InitialPropagate() {
if (alt_ != nullptr) {
alt_->SetRange(0, 1);
}
if (alt_ != nullptr && alt_->Bound()) {
RangeAlt();
} else {
RangeDemon1();
RangeDemon2();
}
}
std::string TemporalDisjunction::DebugString() const {
std::string out;
(out = absl::StrFormat("TemporalDisjunction(%s, %s", t1_->DebugString(),
t2_->DebugString()));
if (alt_ != nullptr) {
absl::StrAppendFormat(&out, " => %s", alt_->DebugString());
}
out += ") ";
return out;
}
void TemporalDisjunction::TryToDecide() {
DCHECK_EQ(UNDECIDED, state_);
if (t1_->MayBePerformed() && t2_->MayBePerformed() &&
(t1_->MustBePerformed() || t2_->MustBePerformed())) {
if (t1_->EndMin() > t2_->StartMax()) {
Decide(TWO_BEFORE_ONE);
} else if (t2_->EndMin() > t1_->StartMax()) {
Decide(ONE_BEFORE_TWO);
}
}
}
void TemporalDisjunction::RangeDemon1() {
switch (state_) {
case ONE_BEFORE_TWO: {
if (t1_->MustBePerformed() && t2_->MayBePerformed()) {
t2_->SetStartMin(t1_->EndMin());
}
break;
}
case TWO_BEFORE_ONE: {
if (t1_->MustBePerformed() && t2_->MayBePerformed()) {
t2_->SetEndMax(t1_->StartMax());
}
break;
}
case UNDECIDED: {
TryToDecide();
}
}
}
void TemporalDisjunction::RangeDemon2() {
if (t1_->MayBePerformed() || t2_->MayBePerformed()) {
switch (state_) {
case ONE_BEFORE_TWO: {
if (t2_->MustBePerformed() && t1_->MayBePerformed()) {
t1_->SetEndMax(t2_->StartMax());
}
break;
}
case TWO_BEFORE_ONE: {
if (t2_->MustBePerformed() && t1_->MayBePerformed()) {
t1_->SetStartMin(t2_->EndMin());
}
break;
}
case UNDECIDED: {
TryToDecide();
}
}
}
}
void TemporalDisjunction::RangeAlt() {
DCHECK(alt_ != nullptr);
if (alt_->Value() == 0) {
Decide(ONE_BEFORE_TWO);
} else {
Decide(TWO_BEFORE_ONE);
}
}
void TemporalDisjunction::Decide(State s) {
// Should Decide on a fixed state?
DCHECK_NE(s, UNDECIDED);
if (state_ != UNDECIDED && state_ != s) {
solver()->Fail();
}
solver()->SaveValue(reinterpret_cast<int*>(&state_));
state_ = s;
if (alt_ != nullptr) {
if (s == ONE_BEFORE_TWO) {
alt_->SetValue(0);
} else {
alt_->SetValue(1);
}
}
RangeDemon1();
RangeDemon2();
}
void TemporalDisjunction::Accept(ModelVisitor* visitor) const {
visitor->BeginVisitConstraint(ModelVisitor::kIntervalDisjunction, this);
visitor->VisitIntervalArgument(ModelVisitor::kLeftArgument, t1_);
visitor->VisitIntervalArgument(ModelVisitor::kRightArgument, t2_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kTargetArgument, alt_);
visitor->EndVisitConstraint(ModelVisitor::kIntervalDisjunction, this);
}
Constraint* Solver::MakeIntervalVarRelation(IntervalVar* const t,
Solver::UnaryIntervalRelation r,
int64_t d) {
return RevAlloc(new IntervalUnaryRelation(this, t, d, r));
}
Constraint* Solver::MakeIntervalVarRelation(IntervalVar* const t1,
Solver::BinaryIntervalRelation r,
IntervalVar* const t2) {
return RevAlloc(new IntervalBinaryRelation(this, t1, t2, r, 0));
}
Constraint* Solver::MakeIntervalVarRelationWithDelay(
IntervalVar* const t1, Solver::BinaryIntervalRelation r,
IntervalVar* const t2, int64_t delay) {
return RevAlloc(new IntervalBinaryRelation(this, t1, t2, r, delay));
}
Constraint* Solver::MakeTemporalDisjunction(IntervalVar* const t1,
IntervalVar* const t2,
IntVar* const alt) {
return RevAlloc(new TemporalDisjunction(this, t1, t2, alt));
}
Constraint* Solver::MakeTemporalDisjunction(IntervalVar* const t1,
IntervalVar* const t2) {
return RevAlloc(new TemporalDisjunction(this, t1, t2, nullptr));
}
} // namespace operations_research