Files
ortools-clone/ortools/constraint_solver/timetabling.cc

414 lines
13 KiB
C++
Raw Normal View History

2024-01-04 13:43:15 +01:00
// Copyright 2010-2024 Google LLC
2010-09-15 12:42:33 +00:00
// 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.
2021-04-01 12:13:35 +02:00
#include <cstdint>
2011-09-21 15:16:48 +00:00
#include <string>
#include "absl/log/check.h"
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
#include "absl/strings/str_format.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/constraint_solver/constraint_solveri.h"
2010-09-15 12:42:33 +00:00
namespace operations_research {
// ----- interval <unary relation> date -----
namespace {
2020-10-29 14:25:39 +01:00
const char* kUnaryNames[] = {
2020-10-22 23:36:58 +02:00
"ENDS_AFTER", "ENDS_AT", "ENDS_BEFORE", "STARTS_AFTER",
"STARTS_AT", "STARTS_BEFORE", "CROSS_DATE", "AVOID_DATE",
};
2010-09-15 12:42:33 +00:00
2020-10-29 14:25:39 +01:00
const char* kBinaryNames[] = {
2020-10-22 23:36:58 +02:00
"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"};
2010-09-15 12:42:33 +00:00
class IntervalUnaryRelation : public Constraint {
2020-10-22 23:36:58 +02:00
public:
2021-04-01 12:13:35 +02:00
IntervalUnaryRelation(Solver* const s, IntervalVar* const t, int64_t d,
2010-09-15 12:42:33 +00:00
Solver::UnaryIntervalRelation rel)
: Constraint(s), t_(t), d_(d), rel_(rel) {}
~IntervalUnaryRelation() override {}
2010-09-15 12:42:33 +00:00
void Post() override;
2011-07-11 20:13:14 +00:00
void InitialPropagate() override;
2010-09-15 12:42:33 +00:00
std::string DebugString() const override {
return absl::StrFormat("(%s %s %d)", t_->DebugString(), kUnaryNames[rel_],
d_);
2010-09-15 12:42:33 +00:00
}
2011-07-11 20:13:14 +00:00
2020-10-29 14:25:39 +01:00
void Accept(ModelVisitor* const visitor) const override {
2011-07-11 20:13:14 +00:00
visitor->BeginVisitConstraint(ModelVisitor::kIntervalUnaryRelation, this);
visitor->VisitIntervalArgument(ModelVisitor::kIntervalArgument, t_);
visitor->VisitIntegerArgument(ModelVisitor::kRelationArgument, rel_);
visitor->VisitIntegerArgument(ModelVisitor::kValueArgument, d_);
2011-07-11 20:13:14 +00:00
visitor->EndVisitConstraint(ModelVisitor::kIntervalUnaryRelation, this);
}
2020-10-22 23:36:58 +02:00
private:
2020-10-29 14:25:39 +01:00
IntervalVar* const t_;
2021-04-01 12:13:35 +02:00
const int64_t d_;
2010-09-15 12:42:33 +00:00
const Solver::UnaryIntervalRelation rel_;
};
void IntervalUnaryRelation::Post() {
if (t_->MayBePerformed()) {
2020-10-29 14:25:39 +01:00
Demon* d = solver()->MakeConstraintInitialPropagateCallback(this);
2010-11-17 16:43:58 +00:00
t_->WhenAnything(d);
2010-09-15 12:42:33 +00:00
}
}
void IntervalUnaryRelation::InitialPropagate() {
if (t_->MayBePerformed()) {
2010-09-15 12:42:33 +00:00
switch (rel_) {
2020-10-22 23:36:58 +02:00
case Solver::ENDS_AFTER:
t_->SetEndMin(d_);
break;
case Solver::ENDS_AT:
t_->SetEndRange(d_, d_);
break;
case Solver::ENDS_BEFORE:
t_->SetEndMax(d_);
2020-10-22 23:36:58 +02:00
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;
2010-09-15 12:42:33 +00:00
}
}
}
2020-10-22 23:36:58 +02:00
} // namespace
2010-09-15 12:42:33 +00:00
2020-10-29 14:25:39 +01:00
Constraint* Solver::MakeIntervalVarRelation(IntervalVar* const t,
2010-09-15 12:42:33 +00:00
Solver::UnaryIntervalRelation r,
2021-04-01 12:13:35 +02:00
int64_t d) {
2010-09-15 12:42:33 +00:00
return RevAlloc(new IntervalUnaryRelation(this, t, d, r));
}
// ----- interval <binary relation> interval -----
namespace {
2010-09-15 12:42:33 +00:00
class IntervalBinaryRelation : public Constraint {
2020-10-22 23:36:58 +02:00
public:
2020-10-29 14:25:39 +01:00
IntervalBinaryRelation(Solver* const s, IntervalVar* const t1,
IntervalVar* const t2,
2021-04-01 12:13:35 +02:00
Solver::BinaryIntervalRelation rel, int64_t delay)
: Constraint(s), t1_(t1), t2_(t2), rel_(rel), delay_(delay) {}
~IntervalBinaryRelation() override {}
2010-09-15 12:42:33 +00:00
void Post() override;
2011-07-11 20:13:14 +00:00
void InitialPropagate() override;
2010-09-15 12:42:33 +00:00
std::string DebugString() const override {
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
return absl::StrFormat("(%s %s %s)", t1_->DebugString(), kBinaryNames[rel_],
t2_->DebugString());
2010-09-15 12:42:33 +00:00
}
2011-07-11 20:13:14 +00:00
2020-10-29 14:25:39 +01:00
void Accept(ModelVisitor* const visitor) const override {
2011-07-11 20:13:14 +00:00
visitor->BeginVisitConstraint(ModelVisitor::kIntervalBinaryRelation, this);
visitor->VisitIntervalArgument(ModelVisitor::kLeftArgument, t1_);
visitor->VisitIntegerArgument(ModelVisitor::kRelationArgument, rel_);
visitor->VisitIntervalArgument(ModelVisitor::kRightArgument, t2_);
2011-07-11 20:13:14 +00:00
visitor->EndVisitConstraint(ModelVisitor::kIntervalBinaryRelation, this);
}
2020-10-22 23:36:58 +02:00
private:
2020-10-29 14:25:39 +01:00
IntervalVar* const t1_;
IntervalVar* const t2_;
2010-09-15 12:42:33 +00:00
const Solver::BinaryIntervalRelation rel_;
2021-04-01 12:13:35 +02:00
const int64_t delay_;
2010-09-15 12:42:33 +00:00
};
void IntervalBinaryRelation::Post() {
if (t1_->MayBePerformed() && t2_->MayBePerformed()) {
2020-10-29 14:25:39 +01:00
Demon* d = solver()->MakeConstraintInitialPropagateCallback(this);
2010-11-17 16:43:58 +00:00
t1_->WhenAnything(d);
t2_->WhenAnything(d);
2010-09-15 12:42:33 +00:00
}
}
// TODO(user) : make code more compact, use function pointers?
void IntervalBinaryRelation::InitialPropagate() {
if (t2_->MustBePerformed() && t1_->MayBePerformed()) {
switch (rel_) {
2020-10-22 23:36:58 +02:00
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_) {
2020-10-22 23:36:58 +02:00
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;
}
2010-09-15 12:42:33 +00:00
}
}
2020-10-22 23:36:58 +02:00
} // namespace
2010-09-15 12:42:33 +00:00
2020-10-29 14:25:39 +01:00
Constraint* Solver::MakeIntervalVarRelation(IntervalVar* const t1,
2010-09-15 12:42:33 +00:00
Solver::BinaryIntervalRelation r,
2020-10-29 14:25:39 +01:00
IntervalVar* const t2) {
return RevAlloc(new IntervalBinaryRelation(this, t1, t2, r, 0));
}
2020-10-29 14:25:39 +01:00
Constraint* Solver::MakeIntervalVarRelationWithDelay(
IntervalVar* const t1, Solver::BinaryIntervalRelation r,
2021-04-01 12:13:35 +02:00
IntervalVar* const t2, int64_t delay) {
return RevAlloc(new IntervalBinaryRelation(this, t1, t2, r, delay));
2010-09-15 12:42:33 +00:00
}
// ----- activity a before activity b or activity b before activity a -----
namespace {
2010-09-15 12:42:33 +00:00
class TemporalDisjunction : public Constraint {
2020-10-22 23:36:58 +02:00
public:
enum State { ONE_BEFORE_TWO, TWO_BEFORE_ONE, UNDECIDED };
2020-10-29 14:25:39 +01:00
TemporalDisjunction(Solver* const s, IntervalVar* const t1,
IntervalVar* const t2, IntVar* const alt)
2010-09-15 12:42:33 +00:00
: Constraint(s), t1_(t1), t2_(t2), alt_(alt), state_(UNDECIDED) {}
~TemporalDisjunction() override {}
2010-09-15 12:42:33 +00:00
void Post() override;
void InitialPropagate() override;
std::string DebugString() const override;
2010-09-15 12:42:33 +00:00
void RangeDemon1();
void RangeDemon2();
void RangeAlt();
void Decide(State s);
void TryToDecide();
2011-07-11 20:13:14 +00:00
2020-10-29 14:25:39 +01:00
void Accept(ModelVisitor* const visitor) const override {
2011-07-11 20:13:14 +00:00
visitor->BeginVisitConstraint(ModelVisitor::kIntervalDisjunction, this);
visitor->VisitIntervalArgument(ModelVisitor::kLeftArgument, t1_);
visitor->VisitIntervalArgument(ModelVisitor::kRightArgument, t2_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kTargetArgument,
2011-07-11 20:13:14 +00:00
alt_);
visitor->EndVisitConstraint(ModelVisitor::kIntervalDisjunction, this);
}
2020-10-22 23:36:58 +02:00
private:
2020-10-29 14:25:39 +01:00
IntervalVar* const t1_;
IntervalVar* const t2_;
IntVar* const alt_;
2010-09-15 12:42:33 +00:00
State state_;
};
void TemporalDisjunction::Post() {
2020-10-29 14:25:39 +01:00
Solver* const s = solver();
Demon* d = MakeConstraintDemon0(s, this, &TemporalDisjunction::RangeDemon1,
2010-09-15 12:42:33 +00:00
"RangeDemon1");
2010-11-17 16:43:58 +00:00
t1_->WhenAnything(d);
d = MakeConstraintDemon0(s, this, &TemporalDisjunction::RangeDemon2,
2010-09-15 12:42:33 +00:00
"RangeDemon2");
2010-11-17 16:43:58 +00:00
t2_->WhenAnything(d);
if (alt_ != nullptr) {
d = MakeConstraintDemon0(s, this, &TemporalDisjunction::RangeAlt,
2010-09-15 12:42:33 +00:00
"RangeAlt");
alt_->WhenRange(d);
}
}
void TemporalDisjunction::InitialPropagate() {
if (alt_ != nullptr) {
2010-09-15 12:42:33 +00:00
alt_->SetRange(0, 1);
}
if (alt_ != nullptr && alt_->Bound()) {
2010-09-15 12:42:33 +00:00
RangeAlt();
} else {
RangeDemon1();
RangeDemon2();
}
}
std::string TemporalDisjunction::DebugString() const {
std::string out;
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
(out = absl::StrFormat("TemporalDisjunction(%s, %s", t1_->DebugString(),
t2_->DebugString()));
if (alt_ != nullptr) {
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
absl::StrAppendFormat(&out, " => %s", alt_->DebugString());
2010-09-15 12:42:33 +00:00
}
out += ") ";
return out;
}
void TemporalDisjunction::TryToDecide() {
DCHECK_EQ(UNDECIDED, state_);
if (t1_->MayBePerformed() && t2_->MayBePerformed() &&
(t1_->MustBePerformed() || t2_->MustBePerformed())) {
2010-09-15 12:42:33 +00:00
if (t1_->EndMin() > t2_->StartMax()) {
Decide(TWO_BEFORE_ONE);
} else if (t2_->EndMin() > t1_->StartMax()) {
Decide(ONE_BEFORE_TWO);
}
}
}
void TemporalDisjunction::RangeDemon1() {
switch (state_) {
2020-10-22 23:36:58 +02:00
case ONE_BEFORE_TWO: {
if (t1_->MustBePerformed() && t2_->MayBePerformed()) {
t2_->SetStartMin(t1_->EndMin());
}
break;
2010-09-15 12:42:33 +00:00
}
2020-10-22 23:36:58 +02:00
case TWO_BEFORE_ONE: {
if (t1_->MustBePerformed() && t2_->MayBePerformed()) {
t2_->SetEndMax(t1_->StartMax());
}
break;
}
case UNDECIDED: {
TryToDecide();
}
2010-09-15 12:42:33 +00:00
}
}
void TemporalDisjunction::RangeDemon2() {
if (t1_->MayBePerformed() || t2_->MayBePerformed()) {
2010-09-15 12:42:33 +00:00
switch (state_) {
2020-10-22 23:36:58 +02:00
case ONE_BEFORE_TWO: {
if (t2_->MustBePerformed() && t1_->MayBePerformed()) {
t1_->SetEndMax(t2_->StartMax());
}
break;
2010-09-15 12:42:33 +00:00
}
2020-10-22 23:36:58 +02:00
case TWO_BEFORE_ONE: {
if (t2_->MustBePerformed() && t1_->MayBePerformed()) {
t1_->SetStartMin(t2_->EndMin());
}
break;
}
case UNDECIDED: {
TryToDecide();
}
2010-09-15 12:42:33 +00:00
}
}
}
void TemporalDisjunction::RangeAlt() {
DCHECK(alt_ != nullptr);
2010-09-15 12:42:33 +00:00
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();
}
2020-10-29 14:25:39 +01:00
solver()->SaveValue(reinterpret_cast<int*>(&state_));
2010-09-15 12:42:33 +00:00
state_ = s;
if (alt_ != nullptr) {
2010-09-15 12:42:33 +00:00
if (s == ONE_BEFORE_TWO) {
alt_->SetValue(0);
} else {
alt_->SetValue(1);
}
}
RangeDemon1();
RangeDemon2();
}
2020-10-22 23:36:58 +02:00
} // namespace
2010-09-15 12:42:33 +00:00
2020-10-29 14:25:39 +01:00
Constraint* Solver::MakeTemporalDisjunction(IntervalVar* const t1,
IntervalVar* const t2,
IntVar* const alt) {
2010-09-15 12:42:33 +00:00
return RevAlloc(new TemporalDisjunction(this, t1, t2, alt));
}
2020-10-29 14:25:39 +01:00
Constraint* Solver::MakeTemporalDisjunction(IntervalVar* const t1,
IntervalVar* const t2) {
return RevAlloc(new TemporalDisjunction(this, t1, t2, nullptr));
2010-09-15 12:42:33 +00:00
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research