OR-Tools  9.2
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 <limits>
44#include <string>
45
46#include "absl/container/flat_hash_map.h"
47#include "absl/types/span.h"
51#include "ortools/sat/model.h"
54
55namespace operations_research {
56namespace sat {
57
58class CpModelBuilder;
59class LinearExpr;
60class IntVar;
61
70class BoolVar {
71 public:
76 BoolVar();
77
80 BoolVar WithName(const std::string& name);
81
83 std::string Name() const;
84
86 BoolVar Not() const { return BoolVar(NegatedRef(index_), builder_); }
87
88 bool operator==(const BoolVar& other) const {
89 return other.builder_ == builder_ && other.index_ == index_;
90 }
91
92 bool operator!=(const BoolVar& other) const {
93 return other.builder_ != builder_ || other.index_ != index_;
94 }
95
96 std::string DebugString() const;
97
104 int index() const { return index_; }
105
106 private:
107 friend class CircuitConstraint;
108 friend class Constraint;
109 friend class CpModelBuilder;
110 friend class DoubleLinearExpr;
111 friend class IntVar;
112 friend class IntervalVar;
114 friend class LinearExpr;
116 friend bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
117
118 BoolVar(int index, CpModelBuilder* builder);
119
120 CpModelBuilder* builder_ = nullptr;
122};
123
124std::ostream& operator<<(std::ostream& os, const BoolVar& var);
125
131
138class IntVar {
139 public:
144 IntVar();
145
152 IntVar(const BoolVar& var); // NOLINT(runtime/explicit)
153
159 BoolVar ToBoolVar() const;
160
162 IntVar WithName(const std::string& name);
163
165 std::string Name() const;
166
167 bool operator==(const IntVar& other) const {
168 return other.builder_ == builder_ && other.index_ == index_;
169 }
170
171 bool operator!=(const IntVar& other) const {
172 return other.builder_ != builder_ || other.index_ != index_;
173 }
174
175 // Returns the domain of the variable.
177
178 std::string DebugString() const;
179
181 int index() const { return index_; }
182
184 LinearExpr AddConstant(int64_t value) const;
185
186 private:
187 friend class BoolVar;
188 friend class CpModelBuilder;
190 friend class DoubleLinearExpr;
191 friend class LinearExpr;
192 friend class IntervalVar;
194 friend int64_t SolutionIntegerValue(const CpSolverResponse& r,
195 const LinearExpr& expr);
196
197 IntVar(int index, CpModelBuilder* builder);
198
199 CpModelBuilder* builder_ = nullptr;
201};
202
203std::ostream& operator<<(std::ostream& os, const IntVar& var);
204
239 public:
241 LinearExpr() = default;
242
245 LinearExpr(BoolVar var); // NOLINT(runtime/explicit)
246
248 LinearExpr(IntVar var); // NOLINT(runtime/explicit)
249
251 LinearExpr(int64_t constant); // NOLINT(runtime/explicit)
252
254 static LinearExpr Sum(absl::Span<const IntVar> vars);
255
257 static LinearExpr Sum(absl::Span<const BoolVar> vars);
258
260 // TODO(user): Remove when the operators + and * are implemented.
261 static LinearExpr Sum(std::initializer_list<IntVar> vars);
262
264 static LinearExpr ScalProd(absl::Span<const IntVar> vars,
265 absl::Span<const int64_t> coeffs);
266
268 static LinearExpr ScalProd(absl::Span<const BoolVar> vars,
269 absl::Span<const int64_t> coeffs);
270
272 // TODO(user): Remove when the operators + and * are implemented.
273 static LinearExpr ScalProd(std::initializer_list<IntVar> vars,
274 absl::Span<const int64_t> coeffs);
275
277 static LinearExpr Term(IntVar var, int64_t coefficient);
278
280 static LinearExpr Term(BoolVar var, int64_t coefficient);
281
283 static LinearExpr BooleanSum(absl::Span<const BoolVar> vars);
284
286 static LinearExpr BooleanScalProd(absl::Span<const BoolVar> vars,
287 absl::Span<const int64_t> coeffs);
288
291
292 // Operators.
293 LinearExpr& operator+=(const LinearExpr& other);
294 LinearExpr& operator-=(const LinearExpr& other);
295 LinearExpr& operator*=(int64_t factor);
296
297 // Deprecated. Use operators instead.
298 LinearExpr& AddConstant(int64_t value);
301 LinearExpr& AddTerm(BoolVar var, int64_t coeff);
302 LinearExpr& AddTerm(IntVar var, int64_t coeff);
303 LinearExpr& AddExpression(const LinearExpr& expr) { return *this += expr; }
304
306 const std::vector<int>& variables() const { return variables_; }
307
309 const std::vector<int64_t>& coefficients() const { return coefficients_; }
310
312 const bool IsConstant() const { return variables_.empty(); }
313
315 int64_t constant() const { return constant_; }
316
322 std::string DebugString(const CpModelProto* proto = nullptr) const;
323
324 private:
325 std::vector<int> variables_;
326 std::vector<int64_t> coefficients_;
327 int64_t constant_ = 0;
328};
329
330std::ostream& operator<<(std::ostream& os, const LinearExpr& e);
331
369 public:
371
374 explicit DoubleLinearExpr(BoolVar var);
375
377 explicit DoubleLinearExpr(IntVar var);
378
380 explicit DoubleLinearExpr(double constant);
381
384
388
391
393 DoubleLinearExpr& AddTerm(IntVar var, double coeff);
394 DoubleLinearExpr& AddTerm(BoolVar var, double coeff);
395
398
401
404
407
409 DoubleLinearExpr& operator*=(double coeff);
410
412 static DoubleLinearExpr Sum(absl::Span<const IntVar> vars);
413
415 static DoubleLinearExpr Sum(absl::Span<const BoolVar> vars);
416
418 static DoubleLinearExpr Sum(std::initializer_list<IntVar> vars);
419
421 static DoubleLinearExpr ScalProd(absl::Span<const IntVar> vars,
422 absl::Span<const double> coeffs);
423
425 static DoubleLinearExpr ScalProd(absl::Span<const BoolVar> vars,
426 absl::Span<const double> coeffs);
427
429 static DoubleLinearExpr ScalProd(std::initializer_list<IntVar> vars,
430 absl::Span<const double> coeffs);
431
433 const std::vector<int>& variables() const { return variables_; }
434
436 const std::vector<double>& coefficients() const { return coefficients_; }
437
438 // Returns true if the expression has no variable.
439 const bool IsConstant() const { return variables_.empty(); }
440
442 double constant() const { return constant_; }
443
445 std::string DebugString(const CpModelProto* proto = nullptr) const;
446
447 private:
448 std::vector<int> variables_;
449 std::vector<double> coefficients_;
450 double constant_ = 0;
451};
452
453std::ostream& operator<<(std::ostream& os, const DoubleLinearExpr& e);
454
476 public:
481 IntervalVar();
482
484 IntervalVar WithName(const std::string& name);
485
487 std::string Name() const;
488
491 LinearExpr StartExpr() const;
492
495 LinearExpr SizeExpr() const;
496
499 LinearExpr EndExpr() const;
500
506 BoolVar PresenceBoolVar() const;
507
509 bool operator==(const IntervalVar& other) const {
510 return other.builder_ == builder_ && other.index_ == index_;
511 }
512
514 bool operator!=(const IntervalVar& other) const {
515 return other.builder_ != builder_ || other.index_ != index_;
516 }
517
519 std::string DebugString() const;
520
522 int index() const { return index_; }
523
524 private:
525 friend class CpModelBuilder;
528 friend std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
529
530 IntervalVar(int index, CpModelBuilder* builder);
531
532 CpModelBuilder* builder_ = nullptr;
534};
535
536std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
537
548 public:
566 Constraint OnlyEnforceIf(absl::Span<const BoolVar> literals);
567
570
572 Constraint WithName(const std::string& name);
573
575 const std::string& Name() const;
576
578 const ConstraintProto& Proto() const { return *proto_; }
579
582
583 protected:
584 friend class CpModelBuilder;
585
587
589};
590
597 public:
605 void AddArc(int tail, int head, BoolVar literal);
606
607 private:
608 friend class CpModelBuilder;
609
611};
612
620 public:
628 void AddArc(int tail, int head, BoolVar literal);
629
630 private:
631 friend class CpModelBuilder;
632
634};
635
643 public:
645 void AddTuple(absl::Span<const int64_t> tuple);
646
647 private:
648 friend class CpModelBuilder;
649
651};
652
660 public:
666 void AddEvent(LinearExpr time, int64_t level_change);
667
674 void AddOptionalEvent(LinearExpr time, int64_t level_change,
675 BoolVar is_active);
676
677 private:
678 friend class CpModelBuilder;
679
681
682 CpModelBuilder* builder_;
683};
684
692 public:
694 void AddTransition(int tail, int head, int64_t transition_label);
695
696 private:
697 friend class CpModelBuilder;
698
700};
701
709 public:
711 void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate);
712
713 private:
714 friend class CpModelBuilder;
715
717};
718
729 public:
732
733 private:
734 friend class CpModelBuilder;
735
737
738 CpModelBuilder* builder_;
739};
740
749 public:
751 void SetName(const std::string& name);
752
754 IntVar NewIntVar(const Domain& domain);
755
758
762 IntVar NewConstant(int64_t value);
763
768
773
775 IntervalVar NewIntervalVar(const LinearExpr& start, const LinearExpr& size,
776 const LinearExpr& end);
777
779 IntervalVar NewFixedSizeIntervalVar(const LinearExpr& start, int64_t size);
780
784 const LinearExpr& size,
785 const LinearExpr& end, BoolVar presence);
786
789 int64_t size, BoolVar presence);
790
792 Constraint AddBoolOr(absl::Span<const BoolVar> literals);
793
795 Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
796
798 Constraint AddBoolXor(absl::Span<const BoolVar> literals);
799
802 return AddBoolOr({a.Not(), b});
803 }
804
806 Constraint AddEquality(const LinearExpr& left, const LinearExpr& right);
807
809 Constraint AddGreaterOrEqual(const LinearExpr& left, const LinearExpr& right);
810
812 Constraint AddGreaterThan(const LinearExpr& left, const LinearExpr& right);
813
815 Constraint AddLessOrEqual(const LinearExpr& left, const LinearExpr& right);
816
818 Constraint AddLessThan(const LinearExpr& left, const LinearExpr& right);
819
821 Constraint AddLinearConstraint(const LinearExpr& expr, const Domain& domain);
822
824 Constraint AddNotEqual(const LinearExpr& left, const LinearExpr& right);
825
827 Constraint AddAllDifferent(absl::Span<const IntVar> vars);
828
830 Constraint AddAllDifferent(absl::Span<const LinearExpr> exprs);
831
833 Constraint AddAllDifferent(std::initializer_list<LinearExpr> exprs);
834
837 absl::Span<const IntVar> variables,
838 IntVar target);
839
841 Constraint AddElement(IntVar index, absl::Span<const int64_t> values,
842 IntVar target);
843
861
876
888 TableConstraint AddAllowedAssignments(absl::Span<const IntVar> vars);
889
900 TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> vars);
901
907 Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
908 absl::Span<const IntVar> inverse_variables);
909
930 int64_t max_level);
931
959 absl::Span<const IntVar> transition_variables, int starting_state,
960 absl::Span<const int> final_states);
961
964 absl::Span<const IntVar> vars);
965
968 absl::Span<const LinearExpr> exprs);
969
972 std::initializer_list<LinearExpr> exprs);
973
974 // Deprecated, use AddMinEquality.
976 absl::Span<const LinearExpr> exprs) {
977 return AddMinEquality(target, exprs);
978 }
979
982 absl::Span<const IntVar> vars);
983
986 absl::Span<const LinearExpr> exprs);
987
990 std::initializer_list<LinearExpr> exprs);
991
992 // Deprecated, use AddMaxEquality.
994 absl::Span<const LinearExpr> exprs) {
995 return AddMaxEquality(target, exprs);
996 }
997
1000 const LinearExpr& numerator,
1001 const LinearExpr& denominator);
1002
1004 Constraint AddAbsEquality(const LinearExpr& target, const LinearExpr& expr);
1005
1008 const LinearExpr& mod);
1009
1012 absl::Span<const LinearExpr> exprs);
1013
1016 absl::Span<const IntVar> vars);
1017
1020 std::initializer_list<LinearExpr> exprs);
1021
1026 Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
1027
1032
1040
1042 void Minimize(const LinearExpr& expr);
1043
1046 void Minimize(const DoubleLinearExpr& expr);
1047
1049 void Maximize(const LinearExpr& expr);
1050
1053 void Maximize(const DoubleLinearExpr& expr);
1054
1057 absl::Span<const IntVar> variables,
1060
1063 absl::Span<const BoolVar> variables,
1066
1068 void AddHint(IntVar var, int64_t value);
1069
1071 void AddHint(BoolVar var, bool value);
1072
1074 void ClearHints();
1075
1077 void AddAssumption(BoolVar lit);
1078
1080 void AddAssumptions(absl::Span<const BoolVar> literals);
1081
1083 void ClearAssumptions();
1084
1085 const CpModelProto& Build() const { return Proto(); }
1086
1087 const CpModelProto& Proto() const { return cp_model_; }
1088 CpModelProto* MutableProto() { return &cp_model_; }
1089
1091 void CopyFrom(const CpModelProto& model_proto);
1092
1095
1098
1101
1102 private:
1105 friend class IntervalVar;
1106 friend class IntVar;
1107
1108 // Fills the 'expr_proto' with the linear expression represented by 'expr'.
1109 LinearExpressionProto LinearExprToProto(const LinearExpr& expr,
1110 bool negate = false);
1111
1112 // Returns a (cached) integer variable index with a constant value.
1113 int IndexFromConstant(int64_t value);
1114
1115 // Returns a valid integer index from a BoolVar index.
1116 // If the input index is a positive, it returns this index.
1117 // If the input index is negative, it creates a cached IntVar equal to
1118 // 1 - BoolVar(PositiveRef(index)), and returns the index of this new
1119 // variable.
1120 int GetOrCreateIntegerIndex(int index);
1121
1122 void FillLinearTerms(const LinearExpr& left, const LinearExpr& right,
1124
1125 CpModelProto cp_model_;
1126 absl::flat_hash_map<int64_t, int> constant_to_index_map_;
1127 absl::flat_hash_map<int, int> bool_to_integer_index_map_;
1128};
1129
1131int64_t SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
1132
1135
1136// Returns a more readable and compact DebugString() than
1137// proto.variables(index).DebugString(). This is used by IntVar::DebugString()
1138// but also allow to get the same string from a const proto.
1139std::string VarDebugString(const CpModelProto& proto, int index);
1140
1141// ============================================================================
1142// Minimal support for "natural" API to create LinearExpr.
1143//
1144// Note(user): This might be optimized further by optimizing LinearExpr for
1145// holding one term, or introducing an LinearTerm class, but these should mainly
1146// be used to construct small expressions. Revisit if we run into performance
1147// issues. Note that if perf become a bottleneck for a client, then probably
1148// directly writing the proto will be even faster.
1149// ============================================================================
1150
1151inline LinearExpr operator-(LinearExpr expr) { return expr *= -1; }
1152
1153inline LinearExpr operator+(const LinearExpr& lhs, const LinearExpr& rhs) {
1154 LinearExpr temp(lhs);
1155 temp += rhs;
1156 return temp;
1157}
1158inline LinearExpr operator+(LinearExpr&& lhs, const LinearExpr& rhs) {
1159 lhs += rhs;
1160 return std::move(lhs);
1161}
1162inline LinearExpr operator+(const LinearExpr& lhs, LinearExpr&& rhs) {
1163 rhs += lhs;
1164 return std::move(rhs);
1165}
1167 if (lhs.variables().size() < rhs.variables().size()) {
1168 rhs += std::move(lhs);
1169 return std::move(rhs);
1170 } else {
1171 lhs += std::move(rhs);
1172 return std::move(lhs);
1173 }
1174}
1175
1176inline LinearExpr operator-(const LinearExpr& lhs, const LinearExpr& rhs) {
1177 LinearExpr temp(lhs);
1178 temp -= rhs;
1179 return temp;
1180}
1181inline LinearExpr operator-(LinearExpr&& lhs, const LinearExpr& rhs) {
1182 lhs -= rhs;
1183 return std::move(lhs);
1184}
1185inline LinearExpr operator-(const LinearExpr& lhs, LinearExpr&& rhs) {
1186 rhs -= lhs;
1187 return std::move(rhs);
1188}
1190 if (lhs.variables().size() < rhs.variables().size()) {
1191 rhs -= std::move(lhs);
1192 return std::move(rhs);
1193 } else {
1194 lhs -= std::move(rhs);
1195 return std::move(lhs);
1196 }
1197}
1198
1199inline LinearExpr operator*(LinearExpr expr, int64_t factor) {
1200 expr *= factor;
1201 return expr;
1202}
1203inline LinearExpr operator*(int64_t factor, LinearExpr expr) {
1204 expr *= factor;
1205 return expr;
1206}
1207
1208// For DoubleLinearExpr.
1209
1211 expr *= -1;
1212 return expr;
1213}
1214
1216 const DoubleLinearExpr& rhs) {
1217 DoubleLinearExpr temp(lhs);
1218 temp += rhs;
1219 return temp;
1220}
1222 const DoubleLinearExpr& rhs) {
1223 lhs += rhs;
1224 return std::move(lhs);
1225}
1227 DoubleLinearExpr&& rhs) {
1228 rhs += lhs;
1229 return std::move(rhs);
1230}
1232 DoubleLinearExpr&& rhs) {
1233 if (lhs.variables().size() < rhs.variables().size()) {
1234 rhs += std::move(lhs);
1235 return std::move(rhs);
1236 } else {
1237 lhs += std::move(rhs);
1238 return std::move(lhs);
1239 }
1240}
1241
1243 expr += rhs;
1244 return expr;
1245}
1247 expr += lhs;
1248 return expr;
1249}
1250
1252 const DoubleLinearExpr& rhs) {
1253 DoubleLinearExpr temp(lhs);
1254 temp -= rhs;
1255 return temp;
1256}
1258 const DoubleLinearExpr& rhs) {
1259 lhs -= rhs;
1260 return std::move(lhs);
1261}
1263 DoubleLinearExpr&& rhs) {
1264 rhs -= lhs;
1265 return std::move(rhs);
1266}
1268 DoubleLinearExpr&& rhs) {
1269 if (lhs.variables().size() < rhs.variables().size()) {
1270 rhs -= std::move(lhs);
1271 return std::move(rhs);
1272 } else {
1273 lhs -= std::move(rhs);
1274 return std::move(lhs);
1275 }
1276}
1277
1279 epxr -= rhs;
1280 return epxr;
1281}
1283 expr *= -1;
1284 expr += lhs;
1285 return expr;
1286}
1287
1288inline DoubleLinearExpr operator*(DoubleLinearExpr expr, double factor) {
1289 expr *= factor;
1290 return expr;
1291}
1292
1293inline DoubleLinearExpr operator*(double factor, DoubleLinearExpr expr) {
1294 expr *= factor;
1295 return expr;
1296}
1297
1298} // namespace sat
1299} // namespace operations_research
1300
1301#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:691
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
Definition: cp_model.cc:620
A Boolean variable.
Definition: cp_model.h:70
std::string Name() const
Returns the name of the variable.
Definition: cp_model.cc:44
BoolVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:35
std::string DebugString() const
Definition: cp_model.cc:55
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:1373
BoolVar()
A default constructed BoolVar can be used to mean not defined yet.
Definition: cp_model.cc:30
bool operator!=(const BoolVar &other) const
Definition: cp_model.h:92
bool operator==(const BoolVar &other) const
Definition: cp_model.h:88
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:104
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
Definition: cp_model.h:86
Specialized circuit constraint.
Definition: cp_model.h:596
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:580
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
The constraint will be enforced iff all literals listed here are true.
Definition: cp_model.cc:568
Constraint WithName(const std::string &name)
Sets the name of the constraint.
Definition: cp_model.cc:561
ConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:581
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Definition: cp_model.cc:566
Constraint(ConstraintProto *proto)
Definition: cp_model.cc:559
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:578
Wrapper class around the cp_model proto.
Definition: cp_model.h:748
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
Definition: cp_model.cc:1283
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Adds an forbidden assignments constraint.
Definition: cp_model.cc:1006
Constraint AddMinEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == min(vars).
Definition: cp_model.cc:1065
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
Definition: cp_model.cc:910
void ClearAssumptions()
Remove all assumptions from the model.
Definition: cp_model.cc:1312
Constraint AddAbsEquality(const LinearExpr &target, const LinearExpr &expr)
Adds target == abs(expr).
Definition: cp_model.cc:1141
void AddAssumptions(absl::Span< const BoolVar > literals)
Adds multiple literals to the model as assumptions.
Definition: cp_model.cc:1306
IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
Creates an interval variable with a fixed size.
Definition: cp_model.cc:784
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.
Definition: cp_model.cc:993
BoolVar TrueVar()
Creates an always true Boolean variable.
Definition: cp_model.cc:770
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
Definition: cp_model.cc:748
void ClearHints()
Removes all hints.
Definition: cp_model.cc:1298
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
Definition: cp_model.cc:1222
BoolVar NewBoolVar()
Creates a Boolean variable.
Definition: cp_model.cc:758
Constraint AddMaxEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == max(vars).
Definition: cp_model.cc:1101
Constraint AddMultiplicationEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == prod(exprs).
Definition: cp_model.cc:1171
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:1259
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:789
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
Definition: cp_model.cc:989
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
Definition: cp_model.cc:966
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
Definition: cp_model.cc:890
void CopyFrom(const CpModelProto &model_proto)
Replaces the current model with the one from the given proto.
Definition: cp_model.cc:1316
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
Definition: cp_model.cc:900
Constraint AddBoolXor(absl::Span< const BoolVar > literals)
Adds the constraint that an odd number of literals is true.
Definition: cp_model.cc:835
void SetName(const std::string &name)
Sets the name of the model.
Definition: cp_model.cc:712
Constraint AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
Definition: cp_model.cc:977
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
Definition: cp_model.cc:1302
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
Definition: cp_model.cc:1210
BoolVar FalseVar()
Creates an always false Boolean variable.
Definition: cp_model.cc:774
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
Definition: cp_model.h:801
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
Definition: cp_model.cc:827
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
Definition: cp_model.cc:1352
CumulativeConstraint AddCumulative(LinearExpr capacity)
The cumulative constraint.
Definition: cp_model.cc:1203
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
Definition: cp_model.cc:880
ReservoirConstraint AddReservoirConstraint(int64_t min_level, int64_t max_level)
Adds a reservoir constraint with optional refill/emptying events.
Definition: cp_model.cc:1029
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
Definition: cp_model.cc:860
NoOverlap2DConstraint AddNoOverlap2D()
The no_overlap_2d constraint prevents a set of boxes from overlapping.
Definition: cp_model.cc:1199
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Definition: cp_model.cc:870
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
Definition: cp_model.cc:819
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
Definition: cp_model.cc:1346
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
An automaton constraint.
Definition: cp_model.cc:1037
Constraint AddLinMinEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Definition: cp_model.h:975
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
Definition: cp_model.cc:806
Constraint AddLinMaxEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Definition: cp_model.h:993
const CpModelProto & Build() const
Definition: cp_model.h:1085
Constraint AddDivisionEquality(const LinearExpr &target, const LinearExpr &numerator, const LinearExpr &denominator)
Adds target = num / denom (integer division rounded towards 0).
Definition: cp_model.cc:1131
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
Definition: cp_model.cc:1330
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
Definition: cp_model.cc:927
Constraint AddModuloEquality(const LinearExpr &target, const LinearExpr &var, const LinearExpr &mod)
Adds target = var % mod.
Definition: cp_model.cc:1151
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
This constraint forces all variables to have different values.
Definition: cp_model.cc:939
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Adds an allowed assignments constraint.
Definition: cp_model.cc:997
IntVar NewConstant(int64_t value)
Creates a constant variable.
Definition: cp_model.cc:766
IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
Creates an interval variable from 3 affine expressions.
Definition: cp_model.cc:778
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
An inverse constraint.
Definition: cp_model.cc:1016
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:1191
const CpModelProto & Proto() const
Definition: cp_model.h:1087
Specialized cumulative constraint.
Definition: cp_model.h:728
void AddDemand(IntervalVar interval, LinearExpr demand)
Adds a pair (interval, demand) to the constraint.
Definition: cp_model.cc:637
A dedicated container for linear expressions with double coefficients.
Definition: cp_model.h:368
static DoubleLinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const double > coeffs)
Constructs the scalar product of variables and coefficients.
Definition: cp_model.cc:409
double constant() const
Returns the constant term.
Definition: cp_model.h:442
DoubleLinearExpr & operator+=(double value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:440
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string. See the documentation for LinearExpr::DebugString().
Definition: cp_model.cc:516
const std::vector< double > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:436
const std::vector< int > & variables() const
Returns the vector of variable indices.
Definition: cp_model.h:433
DoubleLinearExpr & operator-=(double value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:488
DoubleLinearExpr & AddTerm(IntVar var, double coeff)
Adds a term (var * coeff) to the linear expression.
Definition: cp_model.cc:469
DoubleLinearExpr & operator*=(double coeff)
Multiply the linear expression by a constant.
Definition: cp_model.cc:508
DoubleLinearExpr & AddConstant(double constant)
Deprecated. Use +=.
Definition: cp_model.cc:445
static DoubleLinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:385
An integer variable.
Definition: cp_model.h:138
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
Definition: cp_model.cc:107
std::string Name() const
Returns the name of the variable (or the empty string if not set).
Definition: cp_model.cc:124
bool operator==(const IntVar &other) const
Definition: cp_model.h:167
IntVar()
A default constructed IntVar can be used to mean not defined yet.
Definition: cp_model.cc:90
std::string DebugString() const
Definition: cp_model.cc:138
bool operator!=(const IntVar &other) const
Definition: cp_model.h:171
IntVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:117
::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:1362
int index() const
Returns the index of the variable in the model. This will be non-negative.
Definition: cp_model.h:181
LinearExpr AddConstant(int64_t value) const
Deprecated. Just do var + cte where needed.
Definition: cp_model.cc:129
Represents a Interval variable.
Definition: cp_model.h:475
LinearExpr SizeExpr() const
Returns the size linear expression.
Definition: cp_model.cc:662
LinearExpr StartExpr() const
Returns the start linear expression.
Definition: cp_model.cc:655
BoolVar PresenceBoolVar() const
Returns a BoolVar indicating the presence of this interval.
Definition: cp_model.cc:676
std::string Name() const
Returns the name of the interval (or the empty string if not set).
Definition: cp_model.cc:683
std::string DebugString() const
Returns a debug string.
Definition: cp_model.cc:688
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition: cp_model.h:514
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
Definition: cp_model.h:509
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:648
LinearExpr EndExpr() const
Returns the end linear expression.
Definition: cp_model.cc:669
IntervalVar()
A default constructed IntervalVar can be used to mean not defined yet.
Definition: cp_model.cc:643
int index() const
Returns the index of the interval constraint in the model.
Definition: cp_model.h:522
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
Definition: cp_model.cc:707
A dedicated container for linear expressions.
Definition: cp_model.h:238
LinearExpr & operator+=(const LinearExpr &other)
Definition: cp_model.cc:309
LinearExpr & AddVar(IntVar var)
Definition: cp_model.cc:283
LinearExpr & operator-=(const LinearExpr &other)
Definition: cp_model.cc:318
LinearExpr & AddExpression(const LinearExpr &expr)
Definition: cp_model.h:303
LinearExpr & operator*=(int64_t factor)
Definition: cp_model.cc:328
const bool IsConstant() const
Returns true if the expression has no variables.
Definition: cp_model.h:312
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:193
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string.
Definition: cp_model.cc:334
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:309
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Deprecated. Use Sum() instead.
Definition: cp_model.cc:260
const std::vector< int > & variables() const
Returns the vector of variable indices.
Definition: cp_model.h:306
int64_t constant() const
Returns the constant term.
Definition: cp_model.h:315
LinearExpr()=default
Creates an empty linear expression with value zero.
static LinearExpr BooleanScalProd(absl::Span< const BoolVar > vars, absl::Span< const int64_t > coeffs)
Deprecated. Use ScalProd() instead.
Definition: cp_model.cc:268
static LinearExpr FromProto(const LinearExpressionProto &proto)
Constructs a linear expr from its proto representation.
Definition: cp_model.cc:184
static LinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of variables and coefficients.
Definition: cp_model.cc:217
LinearExpr & AddTerm(BoolVar var, int64_t coeff)
Definition: cp_model.cc:294
LinearExpr & AddConstant(int64_t value)
Definition: cp_model.cc:278
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
Definition: cp_model.cc:248
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:586
Specialized no_overlap2D constraint.
Definition: cp_model.h:708
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
Definition: cp_model.cc:627
Specialized reservoir constraint.
Definition: cp_model.h:659
void AddOptionalEvent(LinearExpr time, int64_t level_change, BoolVar is_active)
Adds a optional event.
Definition: cp_model.cc:611
void AddEvent(LinearExpr time, int64_t level_change)
Adds a mandatory event.
Definition: cp_model.cc:603
Specialized assignment constraint.
Definition: cp_model.h:642
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
Definition: cp_model.cc:592
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
LinearExpr operator+(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: cp_model.h:1153
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
Definition: cp_model.cc:85
std::string VarDebugString(const CpModelProto &proto, int index)
Definition: cp_model.cc:145
LinearExpr operator-(LinearExpr expr)
Definition: cp_model.h:1151
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:83
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:1373
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
Definition: cp_model.cc:1362
LinearExpr operator*(LinearExpr expr, int64_t factor)
Definition: cp_model.h:1199
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:85
int index
Definition: pack.cc:509
int64_t demand
Definition: resource.cc:125
int64_t time
Definition: resource.cc:1691
IntervalVar * interval
Definition: resource.cc:100
int64_t coefficient
int64_t capacity
int64_t tail
int64_t head