Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sched_expr.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#include <cstdint>
15#include <string>
16#include <vector>
17
18#include "absl/strings/str_format.h"
21
22#if defined(_MSC_VER)
23#pragma warning(disable : 4351 4355 4804 4805)
24#endif
25
26namespace operations_research {
27namespace {
28class IntervalVarStartExpr : public BaseIntExpr {
29 public:
30 explicit IntervalVarStartExpr(IntervalVar* const i)
31 : BaseIntExpr(i->solver()), interval_(i) {}
32
33 // This type is neither copyable nor movable.
34 IntervalVarStartExpr(const IntervalVarStartExpr&) = delete;
35 IntervalVarStartExpr& operator=(const IntervalVarStartExpr&) = delete;
36 ~IntervalVarStartExpr() override {}
37
38 int64_t Min() const override { return interval_->StartMin(); }
39
40 void SetMin(int64_t m) override { interval_->SetStartMin(m); }
41
42 int64_t Max() const override { return interval_->StartMax(); }
43
44 void SetMax(int64_t m) override { interval_->SetStartMax(m); }
45
46 void SetRange(int64_t l, int64_t u) override {
47 interval_->SetStartRange(l, u);
48 }
49
50 void SetValue(int64_t v) override { interval_->SetStartRange(v, v); }
51
52 bool Bound() const override {
53 return interval_->StartMin() == interval_->StartMax();
54 }
55
56 void WhenRange(Demon* d) override { interval_->WhenStartRange(d); }
57
58 std::string DebugString() const override {
59 return absl::StrFormat("start(%s)", interval_->DebugString());
60 }
61
62 void Accept(ModelVisitor* const visitor) const override {
63 visitor->BeginVisitIntegerExpression(ModelVisitor::kStartExpr, this);
64 visitor->VisitIntervalArgument(ModelVisitor::kIntervalArgument, interval_);
65 visitor->EndVisitIntegerExpression(ModelVisitor::kStartExpr, this);
66 }
67
68 private:
69 IntervalVar* interval_;
70};
71
72class IntervalVarEndExpr : public BaseIntExpr {
73 public:
74 explicit IntervalVarEndExpr(IntervalVar* const i)
75 : BaseIntExpr(i->solver()), interval_(i) {}
76
77 // This type is neither copyable nor movable.
78 IntervalVarEndExpr(const IntervalVarEndExpr&) = delete;
79 IntervalVarEndExpr& operator=(const IntervalVarEndExpr&) = delete;
80 ~IntervalVarEndExpr() override {}
81
82 int64_t Min() const override { return interval_->EndMin(); }
83
84 void SetMin(int64_t m) override { interval_->SetEndMin(m); }
85
86 int64_t Max() const override { return interval_->EndMax(); }
87
88 void SetMax(int64_t m) override { interval_->SetEndMax(m); }
89
90 void SetRange(int64_t l, int64_t u) override { interval_->SetEndRange(l, u); }
91
92 void SetValue(int64_t v) override { interval_->SetEndRange(v, v); }
93
94 bool Bound() const override {
95 return interval_->EndMin() == interval_->EndMax();
96 }
97
98 void WhenRange(Demon* d) override { interval_->WhenEndRange(d); }
99
100 std::string DebugString() const override {
101 return absl::StrFormat("end(%s)", interval_->DebugString());
102 }
103
104 void Accept(ModelVisitor* const visitor) const override {
105 visitor->BeginVisitIntegerExpression(ModelVisitor::kEndExpr, this);
106 visitor->VisitIntervalArgument(ModelVisitor::kIntervalArgument, interval_);
107 visitor->EndVisitIntegerExpression(ModelVisitor::kEndExpr, this);
108 }
109
110 private:
111 IntervalVar* interval_;
112};
113
114class IntervalVarDurationExpr : public BaseIntExpr {
115 public:
116 explicit IntervalVarDurationExpr(IntervalVar* const i)
117 : BaseIntExpr(i->solver()), interval_(i) {}
118
119 // This type is neither copyable nor movable.
120 IntervalVarDurationExpr(const IntervalVarDurationExpr&) = delete;
121 IntervalVarDurationExpr& operator=(const IntervalVarDurationExpr&) = delete;
122 ~IntervalVarDurationExpr() override {}
123
124 int64_t Min() const override { return interval_->DurationMin(); }
125
126 void SetMin(int64_t m) override { interval_->SetDurationMin(m); }
127
128 int64_t Max() const override { return interval_->DurationMax(); }
129
130 void SetMax(int64_t m) override { interval_->SetDurationMax(m); }
131
132 void SetRange(int64_t l, int64_t u) override {
133 interval_->SetDurationRange(l, u);
134 }
135
136 void SetValue(int64_t v) override { interval_->SetDurationRange(v, v); }
137
138 bool Bound() const override {
139 return interval_->DurationMin() == interval_->DurationMax();
140 }
141
142 void WhenRange(Demon* d) override { interval_->WhenDurationRange(d); }
143
144 std::string DebugString() const override {
145 return absl::StrFormat("duration(%s)", interval_->DebugString());
146 }
147
148 void Accept(ModelVisitor* const visitor) const override {
149 visitor->BeginVisitIntegerExpression(ModelVisitor::kDurationExpr, this);
150 visitor->VisitIntervalArgument(ModelVisitor::kIntervalArgument, interval_);
151 visitor->EndVisitIntegerExpression(ModelVisitor::kDurationExpr, this);
152 }
153
154 private:
155 IntervalVar* interval_;
156};
157} // namespace
158
159// ----- API -----
160
162 Solver* const s = var->solver();
163 IntExpr* const expr =
164 s->RegisterIntExpr(s->RevAlloc(new IntervalVarStartExpr(var)));
165 if (var->HasName()) {
166 expr->set_name(absl::StrFormat("start<%s>", var->name()));
167 }
168 return expr;
169}
170
172 Solver* const s = var->solver();
173 IntExpr* const expr =
174 s->RegisterIntExpr(s->RevAlloc(new IntervalVarDurationExpr(var)));
175 if (var->HasName()) {
176 expr->set_name(absl::StrFormat("duration<%s>", var->name()));
177 }
178 return expr;
179}
180
182 Solver* const s = var->solver();
183 IntExpr* const expr =
184 s->RegisterIntExpr(s->RevAlloc(new IntervalVarEndExpr(var)));
185 if (var->HasName()) {
186 expr->set_name(absl::StrFormat("end<%s>", var->name()));
187 }
188 return expr;
189}
190
191IntExpr* BuildSafeStartExpr(IntervalVar* var, int64_t unperformed_value) {
193 var->PerformedExpr()->Var(), var->StartExpr(), unperformed_value);
194}
195
196IntExpr* BuildSafeDurationExpr(IntervalVar* var, int64_t unperformed_value) {
198 var->PerformedExpr()->Var(), var->DurationExpr(), unperformed_value);
199}
200
201IntExpr* BuildSafeEndExpr(IntervalVar* var, int64_t unperformed_value) {
203 var->PerformedExpr()->Var(), var->EndExpr(), unperformed_value);
204}
205} // namespace operations_research
IntVar * Var() override
Creates a variable from the expression.
virtual std::string name() const
Object naming.
bool HasName() const
Returns whether the object has been named or not.
For the time being, Solver is neither MT_SAFE nor MT_HOT.
IntExpr * RegisterIntExpr(IntExpr *expr)
Registers a new IntExpr and wraps it inside a TraceIntExpr if necessary.
Definition trace.cc:848
IntExpr * MakeConditionalExpression(IntVar *condition, IntExpr *expr, int64_t unperformed_value)
Conditional Expr condition ? expr : unperformed_value.
IntVar * var
In SWIG mode, we don't want anything besides these top-level includes.
IntExpr * BuildStartExpr(IntervalVar *var)
--— Expression builders ---—
IntExpr * BuildSafeStartExpr(IntervalVar *var, int64_t unperformed_value)
IntExpr * BuildSafeEndExpr(IntervalVar *var, int64_t unperformed_value)
IntExpr * BuildEndExpr(IntervalVar *var)
IntExpr * BuildDurationExpr(IntervalVar *var)
IntExpr * BuildSafeDurationExpr(IntervalVar *var, int64_t unperformed_value)