OR-Tools  9.3
cp_model.h
Go to the documentation of this file.
1// Copyright 2010-2021 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
38#ifndef OR_TOOLS_SAT_CP_MODEL_H_
39#define OR_TOOLS_SAT_CP_MODEL_H_
40
41#include <cstdint>
42#include <initializer_list>
43#include <iosfwd>
44#include <limits>
45#include <string>
46#include <utility>
47#include <vector>
48
49#include "absl/container/flat_hash_map.h"
50#include "absl/types/span.h"
51#include "ortools/sat/cp_model.pb.h"
54#include "ortools/sat/model.h"
55#include "ortools/sat/sat_parameters.pb.h"
57
58namespace operations_research {
59namespace sat {
60
61class CpModelBuilder;
62class IntVar;
63class LinearExpr;
64
73class BoolVar {
74 public:
79 BoolVar();
80
83 BoolVar WithName(const std::string& name);
84
86 std::string Name() const;
87
89 BoolVar Not() const { return BoolVar(NegatedRef(index_), builder_); }
90
91 bool operator==(const BoolVar& other) const {
92 return other.builder_ == builder_ && other.index_ == index_;
93 }
94
95 bool operator!=(const BoolVar& other) const {
96 return other.builder_ != builder_ || other.index_ != index_;
97 }
98
99 std::string DebugString() const;
100
107 int index() const { return index_; }
108
109 private:
110 friend class CircuitConstraint;
111 friend class Constraint;
112 friend class CpModelBuilder;
113 friend class DoubleLinearExpr;
114 friend class IntVar;
115 friend class IntervalVar;
117 friend class LinearExpr;
119 friend bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
120
121 BoolVar(int index, CpModelBuilder* builder);
122
123 CpModelBuilder* builder_ = nullptr;
125};
126
127std::ostream& operator<<(std::ostream& os, const BoolVar& var);
128
134
141class IntVar {
142 public:
147 IntVar();
148
155 explicit IntVar(const BoolVar& var);
156
162 BoolVar ToBoolVar() const;
163
165 IntVar WithName(const std::string& name);
166
168 std::string Name() const;
169
170 bool operator==(const IntVar& other) const {
171 return other.builder_ == builder_ && other.index_ == index_;
172 }
173
174 bool operator!=(const IntVar& other) const {
175 return other.builder_ != builder_ || other.index_ != index_;
176 }
177
178 // Returns the domain of the variable.
179 // Note that we keep the fully qualified return type as compilation fails with
180 // gcc otherwise.
182
183 std::string DebugString() const;
184
186 int index() const { return index_; }
187
188 private:
189 friend class BoolVar;
190 friend class CpModelBuilder;
192 friend class DoubleLinearExpr;
193 friend class LinearExpr;
194 friend class IntervalVar;
196 friend int64_t SolutionIntegerValue(const CpSolverResponse& r,
197 const LinearExpr& expr);
198
199 IntVar(int index, CpModelBuilder* builder);
200
201 CpModelBuilder* builder_ = nullptr;
203};
204
205std::ostream& operator<<(std::ostream& os, const IntVar& var);
206
241 public:
243 LinearExpr() = default;
244
245 // NOLINTBEGIN(google-explicit-constructor)
246
250
253
255 LinearExpr(int64_t constant);
256
257 // NOLINTEND(google-explicit-constructor)
258
260 static LinearExpr Sum(absl::Span<const IntVar> vars);
261
263 static LinearExpr Sum(absl::Span<const BoolVar> vars);
264
266 static LinearExpr WeightedSum(absl::Span<const IntVar> vars,
267 absl::Span<const int64_t> coeffs);
268
270 static LinearExpr WeightedSum(absl::Span<const BoolVar> vars,
271 absl::Span<const int64_t> coeffs);
272
274 static LinearExpr Term(IntVar var, int64_t coefficient);
275
277 static LinearExpr Term(BoolVar var, int64_t coefficient);
278
280 static LinearExpr FromProto(const LinearExpressionProto& proto);
281
282 // Operators.
283 LinearExpr& operator+=(const LinearExpr& other);
284 LinearExpr& operator-=(const LinearExpr& other);
285 LinearExpr& operator*=(int64_t factor);
286
288 const std::vector<int>& variables() const { return variables_; }
289
291 const std::vector<int64_t>& coefficients() const { return coefficients_; }
292
294 const bool IsConstant() const { return variables_.empty(); }
295
297 int64_t constant() const { return constant_; }
298
304 std::string DebugString(const CpModelProto* proto = nullptr) const;
305
306 private:
307 std::vector<int> variables_;
308 std::vector<int64_t> coefficients_;
309 int64_t constant_ = 0;
310};
311
312std::ostream& operator<<(std::ostream& os, const LinearExpr& e);
313
351 public:
353
356 explicit DoubleLinearExpr(BoolVar var);
357
359 explicit DoubleLinearExpr(IntVar var);
360
362 explicit DoubleLinearExpr(double constant);
363
366
370
373
377
380
383
386
389
391 static DoubleLinearExpr Sum(absl::Span<const IntVar> vars);
392
394 static DoubleLinearExpr Sum(absl::Span<const BoolVar> vars);
395
397 static DoubleLinearExpr WeightedSum(absl::Span<const IntVar> vars,
398 absl::Span<const double> coeffs);
399
401 static DoubleLinearExpr WeightedSum(absl::Span<const BoolVar> vars,
402 absl::Span<const double> coeffs);
403
405 const std::vector<int>& variables() const { return variables_; }
406
408 const std::vector<double>& coefficients() const { return coefficients_; }
409
410 // Returns true if the expression has no variable.
411 const bool IsConstant() const { return variables_.empty(); }
412
414 double constant() const { return constant_; }
415
417 std::string DebugString(const CpModelProto* proto = nullptr) const;
418
419 private:
420 std::vector<int> variables_;
421 std::vector<double> coefficients_;
422 double constant_ = 0;
423};
424
425std::ostream& operator<<(std::ostream& os, const DoubleLinearExpr& e);
426
448 public:
453 IntervalVar();
454
456 IntervalVar WithName(const std::string& name);
457
459 std::string Name() const;
460
463 LinearExpr StartExpr() const;
464
467 LinearExpr SizeExpr() const;
468
471 LinearExpr EndExpr() const;
472
478 BoolVar PresenceBoolVar() const;
479
481 bool operator==(const IntervalVar& other) const {
482 return other.builder_ == builder_ && other.index_ == index_;
483 }
484
486 bool operator!=(const IntervalVar& other) const {
487 return other.builder_ != builder_ || other.index_ != index_;
488 }
489
491 std::string DebugString() const;
492
494 int index() const { return index_; }
495
496 private:
497 friend class CpModelBuilder;
500 friend std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
501
502 IntervalVar(int index, CpModelBuilder* builder);
503
504 CpModelBuilder* builder_ = nullptr;
506};
507
508std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
509
520 public:
538 Constraint OnlyEnforceIf(absl::Span<const BoolVar> literals);
539
542
544 Constraint WithName(const std::string& name);
545
547 const std::string& Name() const;
548
550 const ConstraintProto& Proto() const { return *proto_; }
551
553 ConstraintProto* MutableProto() const { return proto_; }
554
555 protected:
556 friend class CpModelBuilder;
557
558 explicit Constraint(ConstraintProto* proto);
559
560 ConstraintProto* proto_ = nullptr;
561};
562
569 public:
577 void AddArc(int tail, int head, BoolVar literal);
578
579 private:
580 friend class CpModelBuilder;
581
583};
584
592 public:
600 void AddArc(int tail, int head, BoolVar literal);
601
602 private:
603 friend class CpModelBuilder;
604
606};
607
615 public:
617 void AddTuple(absl::Span<const int64_t> tuple);
618
619 private:
620 friend class CpModelBuilder;
621
623};
624
632 public:
639 void AddEvent(LinearExpr time, int64_t level_change);
640
647 void AddOptionalEvent(LinearExpr time, int64_t level_change,
648 BoolVar is_active);
649
650 private:
651 friend class CpModelBuilder;
652
653 ReservoirConstraint(ConstraintProto* proto, CpModelBuilder* builder);
654
655 CpModelBuilder* builder_;
656};
657
665 public:
667 void AddTransition(int tail, int head, int64_t transition_label);
668
669 private:
670 friend class CpModelBuilder;
671
673};
674
682 public:
684 void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate);
685
686 private:
687 friend class CpModelBuilder;
688
690};
691
702 public:
705
706 private:
707 friend class CpModelBuilder;
708
709 CumulativeConstraint(ConstraintProto* proto, CpModelBuilder* builder);
710
711 CpModelBuilder* builder_;
712};
713
722 public:
724 void SetName(const std::string& name);
725
727 IntVar NewIntVar(const Domain& domain);
728
731
735 IntVar NewConstant(int64_t value);
736
741
746
749 const LinearExpr& end);
750
753
757 const LinearExpr& size,
758 const LinearExpr& end, BoolVar presence);
759
762 int64_t size, BoolVar presence);
763
773 void FixVariable(IntVar var, int64_t value);
774 void FixVariable(BoolVar var, bool value);
775
777 Constraint AddBoolOr(absl::Span<const BoolVar> literals);
778
780 Constraint AddAtLeastOne(absl::Span<const BoolVar> literals);
781
783 Constraint AddAtMostOne(absl::Span<const BoolVar> literals);
784
786 Constraint AddExactlyOne(absl::Span<const BoolVar> literals);
787
789 Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
790
792 Constraint AddBoolXor(absl::Span<const BoolVar> literals);
793
796 return AddBoolOr({a.Not(), b});
797 }
798
800 Constraint AddEquality(const LinearExpr& left, const LinearExpr& right);
801
803 Constraint AddGreaterOrEqual(const LinearExpr& left, const LinearExpr& right);
804
806 Constraint AddGreaterThan(const LinearExpr& left, const LinearExpr& right);
807
809 Constraint AddLessOrEqual(const LinearExpr& left, const LinearExpr& right);
810
812 Constraint AddLessThan(const LinearExpr& left, const LinearExpr& right);
813
815 Constraint AddLinearConstraint(const LinearExpr& expr, const Domain& domain);
816
818 Constraint AddNotEqual(const LinearExpr& left, const LinearExpr& right);
819
821 Constraint AddAllDifferent(absl::Span<const IntVar> vars);
822
824 Constraint AddAllDifferent(absl::Span<const LinearExpr> exprs);
825
827 Constraint AddAllDifferent(std::initializer_list<LinearExpr> exprs);
828
831 absl::Span<const IntVar> variables,
832 IntVar target);
833
835 Constraint AddElement(IntVar index, absl::Span<const int64_t> values,
836 IntVar target);
837
855
870
882 TableConstraint AddAllowedAssignments(absl::Span<const IntVar> vars);
883
894 TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> vars);
895
901 Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
902 absl::Span<const IntVar> inverse_variables);
903
924 int64_t max_level);
925
953 absl::Span<const IntVar> transition_variables, int starting_state,
954 absl::Span<const int> final_states);
955
958 absl::Span<const IntVar> vars);
959
962 absl::Span<const LinearExpr> exprs);
963
966 std::initializer_list<LinearExpr> exprs);
967
970 absl::Span<const IntVar> vars);
971
974 absl::Span<const LinearExpr> exprs);
975
978 std::initializer_list<LinearExpr> exprs);
979
982 const LinearExpr& numerator,
983 const LinearExpr& denominator);
984
986 Constraint AddAbsEquality(const LinearExpr& target, const LinearExpr& expr);
987
990 const LinearExpr& mod);
991
994 absl::Span<const LinearExpr> exprs);
995
998 absl::Span<const IntVar> vars);
999
1002 std::initializer_list<LinearExpr> exprs);
1003
1006 const LinearExpr& left,
1007 const LinearExpr& right);
1008
1013 Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
1014
1019
1027
1029 void Minimize(const LinearExpr& expr);
1030
1033 void Minimize(const DoubleLinearExpr& expr);
1034
1036 void Maximize(const LinearExpr& expr);
1037
1040 void Maximize(const DoubleLinearExpr& expr);
1041
1044 absl::Span<const IntVar> variables,
1045 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1046 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1047
1050 absl::Span<const BoolVar> variables,
1051 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1052 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1053
1055 void AddHint(IntVar var, int64_t value);
1056
1058 void AddHint(BoolVar var, bool value);
1059
1061 void ClearHints();
1062
1064 void AddAssumption(BoolVar lit);
1065
1067 void AddAssumptions(absl::Span<const BoolVar> literals);
1068
1070 void ClearAssumptions();
1071
1072 const CpModelProto& Build() const { return Proto(); }
1073
1074 const CpModelProto& Proto() const { return cp_model_; }
1075 CpModelProto* MutableProto() { return &cp_model_; }
1076
1078 void CopyFrom(const CpModelProto& model_proto);
1079
1082
1085
1088
1089 private:
1092 friend class IntervalVar;
1093 friend class IntVar;
1094
1095 // Fills the 'expr_proto' with the linear expression represented by 'expr'.
1096 LinearExpressionProto LinearExprToProto(const LinearExpr& expr,
1097 bool negate = false);
1098
1099 // Returns a (cached) integer variable index with a constant value.
1100 int IndexFromConstant(int64_t value);
1101
1102 // Returns a valid integer index from a BoolVar index.
1103 // If the input index is a positive, it returns this index.
1104 // If the input index is negative, it creates a cached IntVar equal to
1105 // 1 - BoolVar(PositiveRef(index)), and returns the index of this new
1106 // variable.
1107 int GetOrCreateIntegerIndex(int index);
1108
1109 void FillLinearTerms(const LinearExpr& left, const LinearExpr& right,
1110 LinearConstraintProto* proto);
1111
1112 CpModelProto cp_model_;
1113 absl::flat_hash_map<int64_t, int> constant_to_index_map_;
1114 absl::flat_hash_map<int, int> bool_to_integer_index_map_;
1115};
1116
1118int64_t SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
1119
1121bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
1122
1123// Returns a more readable and compact DebugString() than
1124// proto.variables(index).DebugString(). This is used by IntVar::DebugString()
1125// but also allow to get the same string from a const proto.
1126std::string VarDebugString(const CpModelProto& proto, int index);
1127
1128// ============================================================================
1129// Minimal support for "natural" API to create LinearExpr.
1130//
1131// Note(user): This might be optimized further by optimizing LinearExpr for
1132// holding one term, or introducing an LinearTerm class, but these should mainly
1133// be used to construct small expressions. Revisit if we run into performance
1134// issues. Note that if perf become a bottleneck for a client, then probably
1135// directly writing the proto will be even faster.
1136// ============================================================================
1137
1138inline LinearExpr operator-(LinearExpr expr) { return expr *= -1; }
1139
1140inline LinearExpr operator+(const LinearExpr& lhs, const LinearExpr& rhs) {
1141 LinearExpr temp(lhs);
1142 temp += rhs;
1143 return temp;
1144}
1145inline LinearExpr operator+(LinearExpr&& lhs, const LinearExpr& rhs) {
1146 lhs += rhs;
1147 return std::move(lhs);
1148}
1149inline LinearExpr operator+(const LinearExpr& lhs, LinearExpr&& rhs) {
1150 rhs += lhs;
1151 return std::move(rhs);
1152}
1154 if (lhs.variables().size() < rhs.variables().size()) {
1155 rhs += std::move(lhs);
1156 return std::move(rhs);
1157 } else {
1158 lhs += std::move(rhs);
1159 return std::move(lhs);
1160 }
1161}
1162
1163inline LinearExpr operator-(const LinearExpr& lhs, const LinearExpr& rhs) {
1164 LinearExpr temp(lhs);
1165 temp -= rhs;
1166 return temp;
1167}
1168inline LinearExpr operator-(LinearExpr&& lhs, const LinearExpr& rhs) {
1169 lhs -= rhs;
1170 return std::move(lhs);
1171}
1172inline LinearExpr operator-(const LinearExpr& lhs, LinearExpr&& rhs) {
1173 rhs *= -1;
1174 rhs += lhs;
1175 return std::move(rhs);
1176}
1178 lhs -= std::move(rhs);
1179 return std::move(lhs);
1180}
1181
1182inline LinearExpr operator*(LinearExpr expr, int64_t factor) {
1183 expr *= factor;
1184 return expr;
1185}
1186inline LinearExpr operator*(int64_t factor, LinearExpr expr) {
1187 expr *= factor;
1188 return expr;
1189}
1190
1191// For DoubleLinearExpr.
1192
1194 expr *= -1;
1195 return expr;
1196}
1197
1199 const DoubleLinearExpr& rhs) {
1200 DoubleLinearExpr temp(lhs);
1201 temp += rhs;
1202 return temp;
1203}
1205 const DoubleLinearExpr& rhs) {
1206 lhs += rhs;
1207 return std::move(lhs);
1208}
1210 DoubleLinearExpr&& rhs) {
1211 rhs += lhs;
1212 return std::move(rhs);
1213}
1215 DoubleLinearExpr&& rhs) {
1216 if (lhs.variables().size() < rhs.variables().size()) {
1217 rhs += std::move(lhs);
1218 return std::move(rhs);
1219 } else {
1220 lhs += std::move(rhs);
1221 return std::move(lhs);
1222 }
1223}
1224
1226 expr += rhs;
1227 return expr;
1228}
1230 expr += lhs;
1231 return expr;
1232}
1233
1235 const DoubleLinearExpr& rhs) {
1236 DoubleLinearExpr temp(lhs);
1237 temp -= rhs;
1238 return temp;
1239}
1241 const DoubleLinearExpr& rhs) {
1242 lhs -= rhs;
1243 return std::move(lhs);
1244}
1246 DoubleLinearExpr&& rhs) {
1247 rhs *= -1;
1248 rhs += lhs;
1249 return std::move(rhs);
1250}
1252 DoubleLinearExpr&& rhs) {
1253 lhs -= std::move(rhs);
1254 return std::move(lhs);
1255}
1256
1258 epxr -= rhs;
1259 return epxr;
1260}
1262 expr *= -1;
1263 expr += lhs;
1264 return expr;
1265}
1266
1267inline DoubleLinearExpr operator*(DoubleLinearExpr expr, double factor) {
1268 expr *= factor;
1269 return expr;
1270}
1271
1272inline DoubleLinearExpr operator*(double factor, DoubleLinearExpr expr) {
1273 expr *= factor;
1274 return expr;
1275}
1276
1277} // namespace sat
1278} // namespace operations_research
1279
1280#endif // OR_TOOLS_SAT_CP_MODEL_H_
int64_t min
Definition: alldiff_cst.cc:139
We call domain any subset of Int64 = [kint64min, kint64max].
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Definition: linear_expr.h:114
Specialized automaton constraint.
Definition: cp_model.h:664
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
Definition: cp_model.cc:544
A Boolean variable.
Definition: cp_model.h:73
std::string Name() const
Returns the name of the variable.
Definition: cp_model.cc:48
BoolVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:39
std::string DebugString() const
Definition: cp_model.cc:59
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:1340
BoolVar()
A default constructed BoolVar can be used to mean not defined yet.
Definition: cp_model.cc:34
bool operator!=(const BoolVar &other) const
Definition: cp_model.h:95
bool operator==(const BoolVar &other) const
Definition: cp_model.h:91
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:107
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
Definition: cp_model.h:89
Specialized circuit constraint.
Definition: cp_model.h:568
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:504
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
The constraint will be enforced iff all literals listed here are true.
Definition: cp_model.cc:492
Constraint WithName(const std::string &name)
Sets the name of the constraint.
Definition: cp_model.cc:485
ConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:553
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Definition: cp_model.cc:490
Constraint(ConstraintProto *proto)
Definition: cp_model.cc:483
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:550
Wrapper class around the cp_model proto.
Definition: cp_model.h:721
Constraint AddAtMostOne(absl::Span< const BoolVar > literals)
At most one literal is true. Sum literals <= 1.
Definition: cp_model.cc:768
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
Definition: cp_model.cc:1250
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Adds an forbidden assignments constraint.
Definition: cp_model.cc:963
Constraint AddMinEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == min(vars).
Definition: cp_model.cc:1022
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
Definition: cp_model.cc:867
void ClearAssumptions()
Remove all assumptions from the model.
Definition: cp_model.cc:1279
Constraint AddAbsEquality(const LinearExpr &target, const LinearExpr &expr)
Adds target == abs(expr).
Definition: cp_model.cc:1098
void AddAssumptions(absl::Span< const BoolVar > literals)
Adds multiple literals to the model as assumptions.
Definition: cp_model.cc:1273
IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
Creates an interval variable with a fixed size.
Definition: cp_model.cc:708
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.
Definition: cp_model.cc:950
BoolVar TrueVar()
Creates an always true Boolean variable.
Definition: cp_model.cc:694
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
Definition: cp_model.cc:672
void ClearHints()
Removes all hints.
Definition: cp_model.cc:1265
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
Definition: cp_model.cc:1189
BoolVar NewBoolVar()
Creates a Boolean variable.
Definition: cp_model.cc:682
Constraint AddAtLeastOne(absl::Span< const BoolVar > literals)
Same as AddBoolOr. Sum literals >= 1.
Definition: cp_model.cc:764
Constraint AddMaxEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == max(vars).
Definition: cp_model.cc:1058
Constraint AddMultiplicationEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == prod(exprs).
Definition: cp_model.cc:1128
void AddDecisionStrategy(absl::Span< const IntVar > variables, DecisionStrategyProto::VariableSelectionStrategy var_strategy, DecisionStrategyProto::DomainReductionStrategy domain_strategy)
Adds a decision strategy on a list of integer variables.
Definition: cp_model.cc:1226
IntervalVar NewOptionalIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end, BoolVar presence)
Creates an optional interval variable from 3 affine expressions and a Boolean variable.
Definition: cp_model.cc:713
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
Definition: cp_model.cc:946
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
Definition: cp_model.cc:923
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
Definition: cp_model.cc:847
void CopyFrom(const CpModelProto &model_proto)
Replaces the current model with the one from the given proto.
Definition: cp_model.cc:1283
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
Definition: cp_model.cc:857
Constraint AddBoolXor(absl::Span< const BoolVar > literals)
Adds the constraint that an odd number of literals is true.
Definition: cp_model.cc:792
void SetName(const std::string &name)
Sets the name of the model.
Definition: cp_model.cc:636
Constraint AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
Definition: cp_model.cc:934
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
Definition: cp_model.cc:1269
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
Definition: cp_model.cc:1177
BoolVar FalseVar()
Creates an always false Boolean variable.
Definition: cp_model.cc:698
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
Definition: cp_model.h:795
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
Definition: cp_model.cc:784
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
Definition: cp_model.cc:1319
CumulativeConstraint AddCumulative(LinearExpr capacity)
The cumulative constraint.
Definition: cp_model.cc:1170
void FixVariable(IntVar var, int64_t value)
It is sometime convenient when building a model to create a bunch of variables that will later be fix...
Definition: cp_model.cc:742
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
Definition: cp_model.cc:837
ReservoirConstraint AddReservoirConstraint(int64_t min_level, int64_t max_level)
Adds a reservoir constraint with optional refill/emptying events.
Definition: cp_model.cc:986
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
Definition: cp_model.cc:817
NoOverlap2DConstraint AddNoOverlap2D()
The no_overlap_2d constraint prevents a set of boxes from overlapping.
Definition: cp_model.cc:1166
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Definition: cp_model.cc:827
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
Definition: cp_model.cc:756
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
Definition: cp_model.cc:1313
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
An automaton constraint.
Definition: cp_model.cc:994
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
Definition: cp_model.cc:729
const CpModelProto & Build() const
Definition: cp_model.h:1072
Constraint AddDivisionEquality(const LinearExpr &target, const LinearExpr &numerator, const LinearExpr &denominator)
Adds target = num / denom (integer division rounded towards 0).
Definition: cp_model.cc:1088
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
Definition: cp_model.cc:1297
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
Definition: cp_model.cc:884
Constraint AddModuloEquality(const LinearExpr &target, const LinearExpr &var, const LinearExpr &mod)
Adds target = var % mod.
Definition: cp_model.cc:1108
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
This constraint forces all variables to have different values.
Definition: cp_model.cc:896
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Adds an allowed assignments constraint.
Definition: cp_model.cc:954
IntVar NewConstant(int64_t value)
Creates a constant variable.
Definition: cp_model.cc:690
IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
Creates an interval variable from 3 affine expressions.
Definition: cp_model.cc:702
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
An inverse constraint.
Definition: cp_model.cc:973
Constraint AddExactlyOne(absl::Span< const BoolVar > literals)
Exactly one literal is true. Sum literals == 1.
Definition: cp_model.cc:776
Constraint AddNoOverlap(absl::Span< const IntervalVar > vars)
Adds a no-overlap constraint that ensures that all present intervals do not overlap in time.
Definition: cp_model.cc:1158
const CpModelProto & Proto() const
Definition: cp_model.h:1074
Specialized cumulative constraint.
Definition: cp_model.h:701
void AddDemand(IntervalVar interval, LinearExpr demand)
Adds a pair (interval, demand) to the constraint.
Definition: cp_model.cc:561
A dedicated container for linear expressions with double coefficients.
Definition: cp_model.h:350
double constant() const
Returns the constant term.
Definition: cp_model.h:414
DoubleLinearExpr & operator+=(double value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:369
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string. See the documentation for LinearExpr::DebugString().
Definition: cp_model.cc:440
static DoubleLinearExpr WeightedSum(absl::Span< const IntVar > vars, absl::Span< const double > coeffs)
Constructs the scalar product of variables and coefficients.
Definition: cp_model.cc:349
const std::vector< double > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:408
const std::vector< int > & variables() const
Returns the vector of variable indices.
Definition: cp_model.h:405
DoubleLinearExpr & operator-=(double value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:412
DoubleLinearExpr & AddTerm(IntVar var, double coeff)
Adds a term (var * coeff) to the linear expression.
Definition: cp_model.cc:393
DoubleLinearExpr & operator*=(double coeff)
Multiply the linear expression by a constant.
Definition: cp_model.cc:432
static DoubleLinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:333
An integer variable.
Definition: cp_model.h:141
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
Definition: cp_model.cc:111
std::string Name() const
Returns the name of the variable (or the empty string if not set).
Definition: cp_model.cc:128
bool operator==(const IntVar &other) const
Definition: cp_model.h:170
IntVar()
A default constructed IntVar can be used to mean not defined yet.
Definition: cp_model.cc:94
std::string DebugString() const
Definition: cp_model.cc:138
bool operator!=(const IntVar &other) const
Definition: cp_model.h:174
IntVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:121
::operations_research::Domain Domain() const
Definition: cp_model.cc:133
friend int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
Definition: cp_model.cc:1329
int index() const
Returns the index of the variable in the model. This will be non-negative.
Definition: cp_model.h:186
Represents a Interval variable.
Definition: cp_model.h:447
LinearExpr SizeExpr() const
Returns the size linear expression.
Definition: cp_model.cc:586
LinearExpr StartExpr() const
Returns the start linear expression.
Definition: cp_model.cc:579
BoolVar PresenceBoolVar() const
Returns a BoolVar indicating the presence of this interval.
Definition: cp_model.cc:600
std::string Name() const
Returns the name of the interval (or the empty string if not set).
Definition: cp_model.cc:607
std::string DebugString() const
Returns a debug string.
Definition: cp_model.cc:612
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition: cp_model.h:486
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
Definition: cp_model.h:481
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:572
LinearExpr EndExpr() const
Returns the end linear expression.
Definition: cp_model.cc:593
IntervalVar()
A default constructed IntervalVar can be used to mean not defined yet.
Definition: cp_model.cc:567
int index() const
Returns the index of the interval constraint in the model.
Definition: cp_model.h:494
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
Definition: cp_model.cc:631
A dedicated container for linear expressions.
Definition: cp_model.h:240
LinearExpr & operator+=(const LinearExpr &other)
Definition: cp_model.cc:257
LinearExpr & operator-=(const LinearExpr &other)
Definition: cp_model.cc:266
LinearExpr & operator*=(int64_t factor)
Definition: cp_model.cc:276
const bool IsConstant() const
Returns true if the expression has no variables.
Definition: cp_model.h:294
static LinearExpr WeightedSum(absl::Span< const IntVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of variables and coefficients.
Definition: cp_model.cc:225
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:209
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string.
Definition: cp_model.cc:282
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:291
const std::vector< int > & variables() const
Returns the vector of variable indices.
Definition: cp_model.h:288
int64_t constant() const
Returns the constant term.
Definition: cp_model.h:297
LinearExpr()=default
Creates an empty linear expression with value zero.
static LinearExpr FromProto(const LinearExpressionProto &proto)
Constructs a linear expr from its proto representation.
Definition: cp_model.cc:200
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
Definition: cp_model.cc:245
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:510
Specialized no_overlap2D constraint.
Definition: cp_model.h:681
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
Definition: cp_model.cc:551
Specialized reservoir constraint.
Definition: cp_model.h:631
void AddOptionalEvent(LinearExpr time, int64_t level_change, BoolVar is_active)
Adds an optional event.
Definition: cp_model.cc:535
void AddEvent(LinearExpr time, int64_t level_change)
Adds a mandatory event.
Definition: cp_model.cc:527
Specialized assignment constraint.
Definition: cp_model.h:614
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
Definition: cp_model.cc:516
int64_t b
int64_t a
CpModelProto proto
CpModelProto const * model_proto
const std::string name
int64_t value
IntVar * var
Definition: expr_array.cc:1874
int index
LinearExpr operator+(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: cp_model.h:1140
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
Definition: cp_model.cc:89
std::string VarDebugString(const CpModelProto &proto, int index)
Definition: cp_model.cc:145
LinearExpr operator-(LinearExpr expr)
Definition: cp_model.h:1138
BoolVar Not(BoolVar x)
A convenient wrapper so we can write Not(x) instead of x.Not() which is sometimes clearer.
Definition: cp_model.cc:87
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:1340
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
Definition: cp_model.cc:1329
LinearExpr operator*(LinearExpr expr, int64_t factor)
Definition: cp_model.h:1182
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:89
int64_t demand
Definition: resource.cc:125
int64_t time
Definition: resource.cc:1693
IntervalVar * interval
Definition: resource.cc:100
int64_t coefficient
int64_t capacity
int64_t tail
int64_t head
std::optional< int64_t > end
int64_t start
const double coeff