Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_model.h
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
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 <ostream>
46#include <string>
47#include <utility>
48#include <vector>
49
50#include "absl/container/flat_hash_map.h"
51#include "absl/strings/string_view.h"
52#include "absl/types/span.h"
53#include "ortools/sat/cp_model.pb.h"
56#include "ortools/sat/model.h"
57#include "ortools/sat/sat_parameters.pb.h"
59
60namespace operations_research {
61namespace sat {
62
63class CpModelBuilder;
64class IntVar;
65class LinearExpr;
66
75class BoolVar {
76 public:
81 BoolVar() = default;
82
85 BoolVar WithName(absl::string_view name);
86
88 std::string Name() const;
89
91 BoolVar Not() const { return BoolVar(NegatedRef(index_), builder_); }
92
93 bool operator==(const BoolVar& other) const {
94 return other.builder_ == builder_ && other.index_ == index_;
95 }
96
97 bool operator!=(const BoolVar& other) const {
98 return other.builder_ != builder_ || other.index_ != index_;
99 }
100
101 BoolVar operator~() const { return Not(); }
102
103 std::string DebugString() const;
104
111 int index() const { return index_; }
112
113 private:
114 friend class CircuitConstraint;
115 friend class Constraint;
116 friend class CpModelBuilder;
117 friend class DoubleLinearExpr;
118 friend class IntVar;
119 friend class IntervalVar;
121 friend class LinearExpr;
123 friend bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
124
125 BoolVar(int index, CpModelBuilder* builder);
126
127 CpModelBuilder* builder_ = nullptr;
128 int index_ = std::numeric_limits<int32_t>::min();
129};
130
131std::ostream& operator<<(std::ostream& os, const BoolVar& var);
132
138
145class IntVar {
146 public:
151 IntVar() = default;
152
159 explicit IntVar(const BoolVar& var);
160
166 BoolVar ToBoolVar() const;
167
169 IntVar WithName(absl::string_view name);
170
172 std::string Name() const;
173
174 bool operator==(const IntVar& other) const {
175 return other.builder_ == builder_ && other.index_ == index_;
177
178 bool operator!=(const IntVar& other) const {
179 return other.builder_ != builder_ || other.index_ != index_;
181
182 // Returns the domain of the variable.
183 // Note that we keep the fully qualified return type as compilation fails with
184 // gcc otherwise.
186
187 std::string DebugString() const;
188
190 int index() const { return index_; }
191
192 private:
193 friend class BoolVar;
194 friend class CpModelBuilder;
196 friend class DoubleLinearExpr;
197 friend class LinearExpr;
198 friend class IntervalVar;
200 friend int64_t SolutionIntegerValue(const CpSolverResponse& r,
201 const LinearExpr& expr);
202
203 IntVar(int index, CpModelBuilder* builder);
204
205 CpModelBuilder* builder_ = nullptr;
206 int index_ = std::numeric_limits<int32_t>::min();
207};
208
209std::ostream& operator<<(std::ostream& os, const IntVar& var);
210
244class LinearExpr {
245 public:
247 LinearExpr() = default;
248
249 // NOLINTBEGIN(google-explicit-constructor)
250
254
257
259 LinearExpr(int64_t constant);
260
261 // NOLINTEND(google-explicit-constructor)
262
264 static LinearExpr Sum(absl::Span<const IntVar> vars);
265
267 static LinearExpr Sum(absl::Span<const BoolVar> vars);
268
270 static LinearExpr WeightedSum(absl::Span<const IntVar> vars,
271 absl::Span<const int64_t> coeffs);
272
274 static LinearExpr WeightedSum(absl::Span<const BoolVar> vars,
275 absl::Span<const int64_t> coeffs);
276
278 static LinearExpr Term(IntVar var, int64_t coefficient);
279
281 static LinearExpr Term(BoolVar var, int64_t coefficient);
282
284 static LinearExpr FromProto(const LinearExpressionProto& proto);
285
286 // Operators.
287 LinearExpr& operator+=(const LinearExpr& other);
288 LinearExpr& operator-=(const LinearExpr& other);
289 LinearExpr& operator*=(int64_t factor);
290
292 const std::vector<int>& variables() const { return variables_; }
293
295 const std::vector<int64_t>& coefficients() const { return coefficients_; }
296
298 bool IsConstant() const { return variables_.empty(); }
299
301 int64_t constant() const { return constant_; }
302
308 std::string DebugString(const CpModelProto* proto = nullptr) const;
309
310 private:
311 std::vector<int> variables_;
312 std::vector<int64_t> coefficients_;
313 int64_t constant_ = 0;
314};
315
316std::ostream& operator<<(std::ostream& os, const LinearExpr& e);
317
348class DoubleLinearExpr {
349 public:
351
354 explicit DoubleLinearExpr(BoolVar var);
355
357 explicit DoubleLinearExpr(IntVar var);
358
360 explicit DoubleLinearExpr(double constant);
361
364
368
371
373 DoubleLinearExpr& AddTerm(IntVar var, double coeff);
374 DoubleLinearExpr& AddTerm(BoolVar var, double coeff);
375
377 DoubleLinearExpr& AddExpression(const LinearExpr& exprs, double coeff = 1.0);
378
381
384
387
389 DoubleLinearExpr& operator*=(double coeff);
390
392 static DoubleLinearExpr Sum(absl::Span<const IntVar> vars);
393
395 static DoubleLinearExpr Sum(absl::Span<const BoolVar> vars);
396
398 static DoubleLinearExpr WeightedSum(absl::Span<const IntVar> vars,
399 absl::Span<const double> coeffs);
400
402 static DoubleLinearExpr WeightedSum(absl::Span<const BoolVar> vars,
403 absl::Span<const double> coeffs);
404
406 const std::vector<int>& variables() const { return variables_; }
407
409 const std::vector<double>& coefficients() const { return coefficients_; }
410
411 // Returns true if the expression has no variable.
412 bool IsConstant() const { return variables_.empty(); }
413
415 double constant() const { return constant_; }
416
418 std::string DebugString(const CpModelProto* proto = nullptr) const;
419
420 private:
421 std::vector<int> variables_;
422 std::vector<double> coefficients_;
423 double constant_ = 0;
424};
425
426std::ostream& operator<<(std::ostream& os, const DoubleLinearExpr& e);
427
448class IntervalVar {
449 public:
454 IntervalVar();
455
457 IntervalVar WithName(absl::string_view name);
458
460 std::string Name() const;
461
464 LinearExpr StartExpr() const;
465
468 LinearExpr SizeExpr() const;
469
472 LinearExpr EndExpr() const;
473
479 BoolVar PresenceBoolVar() const;
480
482 bool operator==(const IntervalVar& other) const {
483 return other.builder_ == builder_ && other.index_ == index_;
485
487 bool operator!=(const IntervalVar& other) const {
488 return other.builder_ != builder_ || other.index_ != index_;
490
492 std::string DebugString() const;
493
495 int index() const { return index_; }
496
497 private:
498 friend class CpModelBuilder;
499 friend class CumulativeConstraint;
501 friend std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
503 IntervalVar(int index, CpModelBuilder* builder);
504
505 CpModelBuilder* builder_ = nullptr;
506 int index_ = std::numeric_limits<int32_t>::min();
507};
508
509std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
510
511// -- ABSL HASHING SUPPORT -----------------------------------------------------
512template <typename H>
513H AbslHashValue(H h, const IntVar& i) {
514 return H::combine(std::move(h), i.index());
516
517template <typename H>
518H AbslHashValue(H h, const IntervalVar& i) {
519 return H::combine(std::move(h), i.index());
521
531class Constraint {
532 public:
550 Constraint OnlyEnforceIf(absl::Span<const BoolVar> literals);
551
554
556 Constraint WithName(absl::string_view name);
557
559 absl::string_view Name() const;
560
562 const ConstraintProto& Proto() const { return *proto_; }
563
565 ConstraintProto* MutableProto() const { return proto_; }
566
567 protected:
568 friend class CpModelBuilder;
569
570 explicit Constraint(ConstraintProto* proto);
571
572 ConstraintProto* proto_ = nullptr;
573};
580class CircuitConstraint : public Constraint {
581 public:
589 void AddArc(int tail, int head, BoolVar literal);
590
591 private:
592 friend class CpModelBuilder;
593
595};
596
604 public:
612 void AddArc(int tail, int head, BoolVar literal);
613
614 private:
615 friend class CpModelBuilder;
616
618};
619
626class TableConstraint : public Constraint {
627 public:
629 void AddTuple(absl::Span<const int64_t> tuple);
630
631 private:
632 friend class CpModelBuilder;
633
635};
636
643class ReservoirConstraint : public Constraint {
644 public:
651 void AddEvent(LinearExpr time, int64_t level_change);
652
659 void AddOptionalEvent(LinearExpr time, int64_t level_change,
660 BoolVar is_active);
661
662 private:
663 friend class CpModelBuilder;
664
665 ReservoirConstraint(ConstraintProto* proto, CpModelBuilder* builder);
666
667 CpModelBuilder* builder_;
668};
669
676class AutomatonConstraint : public Constraint {
677 public:
679 void AddTransition(int tail, int head, int64_t transition_label);
680
681 private:
682 friend class CpModelBuilder;
683
685};
686
693class NoOverlap2DConstraint : public Constraint {
694 public:
696 void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate);
697
698 private:
699 friend class CpModelBuilder;
700
702};
703
713class CumulativeConstraint : public Constraint {
714 public:
717
718 private:
719 friend class CpModelBuilder;
720
721 CumulativeConstraint(ConstraintProto* proto, CpModelBuilder* builder);
722
723 CpModelBuilder* builder_;
724};
725
733class CpModelBuilder {
734 public:
736 void SetName(absl::string_view name);
737
739 IntVar NewIntVar(const Domain& domain);
740
743
747 IntVar NewConstant(int64_t value);
748
753
758
761 const LinearExpr& end);
762
765
769 const LinearExpr& size,
770 const LinearExpr& end, BoolVar presence);
771
774 int64_t size, BoolVar presence);
775
785 void FixVariable(IntVar var, int64_t value);
786 void FixVariable(BoolVar var, bool value);
787
789 Constraint AddBoolOr(absl::Span<const BoolVar> literals);
790
792 Constraint AddAtLeastOne(absl::Span<const BoolVar> literals);
793
795 Constraint AddAtMostOne(absl::Span<const BoolVar> literals);
796
798 Constraint AddExactlyOne(absl::Span<const BoolVar> literals);
799
801 Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
802
804 Constraint AddBoolXor(absl::Span<const BoolVar> literals);
805
808 return AddBoolOr({a.Not(), b});
809 }
812 Constraint AddImplication(absl::Span<const BoolVar> lhs,
813 absl::Span<const BoolVar> rhs) {
814 return AddBoolAnd(rhs).OnlyEnforceIf(lhs);
816
818 Constraint AddEquality(const LinearExpr& left, const LinearExpr& right);
819
821 Constraint AddGreaterOrEqual(const LinearExpr& left, const LinearExpr& right);
822
824 Constraint AddGreaterThan(const LinearExpr& left, const LinearExpr& right);
825
827 Constraint AddLessOrEqual(const LinearExpr& left, const LinearExpr& right);
828
830 Constraint AddLessThan(const LinearExpr& left, const LinearExpr& right);
831
833 Constraint AddLinearConstraint(const LinearExpr& expr, const Domain& domain);
834
836 Constraint AddNotEqual(const LinearExpr& left, const LinearExpr& right);
837
839 Constraint AddAllDifferent(absl::Span<const IntVar> vars);
840
842 Constraint AddAllDifferent(absl::Span<const LinearExpr> exprs);
843
845 Constraint AddAllDifferent(std::initializer_list<LinearExpr> exprs);
846
849 absl::Span<const IntVar> variables,
850 IntVar target);
851
853 Constraint AddElement(IntVar index, absl::Span<const int64_t> values,
854 IntVar target);
855
873
888
900 TableConstraint AddAllowedAssignments(absl::Span<const IntVar> vars);
901
912 TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> vars);
913
919 Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
920 absl::Span<const IntVar> inverse_variables);
921
942 int64_t max_level);
943
971 absl::Span<const IntVar> transition_variables, int starting_state,
972 absl::Span<const int> final_states);
973
976 absl::Span<const IntVar> vars);
977
980 absl::Span<const LinearExpr> exprs);
981
984 std::initializer_list<LinearExpr> exprs);
985
988 absl::Span<const IntVar> vars);
989
992 absl::Span<const LinearExpr> exprs);
993
996 std::initializer_list<LinearExpr> exprs);
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
1024 const LinearExpr& left,
1025 const LinearExpr& right);
1026
1031 Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
1032
1037
1045
1047 void Minimize(const LinearExpr& expr);
1048
1051 void Minimize(const DoubleLinearExpr& expr);
1052
1054 void Maximize(const LinearExpr& expr);
1055
1058 void Maximize(const DoubleLinearExpr& expr);
1059
1061 void ClearObjective();
1062
1064 bool HasObjective() const;
1065
1068 absl::Span<const IntVar> variables,
1069 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1070 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1071
1074 absl::Span<const BoolVar> variables,
1075 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1076 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1077
1080 absl::Span<const LinearExpr> expressions,
1081 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1082 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1083
1086 std::initializer_list<LinearExpr> expressions,
1087 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1088 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1089
1091 void AddHint(IntVar var, int64_t value);
1092
1094 void AddHint(BoolVar var, bool value);
1095
1097 void ClearHints();
1098
1101
1103 void AddAssumptions(absl::Span<const BoolVar> literals);
1104
1106 void ClearAssumptions();
1107
1108 const CpModelProto& Build() const { return cp_model_; }
1109 const CpModelProto& Proto() const { return cp_model_; }
1110 CpModelProto* MutableProto() { return &cp_model_; }
1113 bool ExportToFile(absl::string_view filename) const;
1114
1116 CpModelBuilder Clone() const;
1117
1120
1123
1126
1127 private:
1128 friend class CumulativeConstraint;
1129 friend class ReservoirConstraint;
1130 friend class IntervalVar;
1131 friend class IntVar;
1133 // Used for cloning a model.
1134 void ResetAndImport(const CpModelProto& model_proto);
1135
1136 // Fills the 'expr_proto' with the linear expression represented by 'expr'.
1137 LinearExpressionProto LinearExprToProto(const LinearExpr& expr,
1138 bool negate = false);
1139
1140 // Returns a (cached) integer variable index with a constant value.
1141 int IndexFromConstant(int64_t value);
1142
1143 // Returns a valid integer index from a BoolVar index.
1144 // If the input index is a positive, it returns this index.
1145 // If the input index is negative, it creates a cached IntVar equal to
1146 // 1 - BoolVar(PositiveRef(index)), and returns the index of this new
1147 // variable.
1148 int GetOrCreateIntegerIndex(int index);
1149
1150 void FillLinearTerms(const LinearExpr& left, const LinearExpr& right,
1151 LinearConstraintProto* proto);
1152
1153 CpModelProto cp_model_;
1154 absl::flat_hash_map<int64_t, int> constant_to_index_map_;
1155 absl::flat_hash_map<int, int> bool_to_integer_index_map_;
1156};
1157
1159int64_t SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
1160
1162bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
1163
1164// Returns a more readable and compact DebugString() than
1165// proto.variables(index).DebugString(). This is used by IntVar::DebugString()
1166// but also allow to get the same string from a const proto.
1167std::string VarDebugString(const CpModelProto& proto, int index);
1168
1169// ============================================================================
1170// Minimal support for "natural" API to create LinearExpr.
1171//
1172// Note(user): This might be optimized further by optimizing LinearExpr for
1173// holding one term, or introducing an LinearTerm class, but these should mainly
1174// be used to construct small expressions. Revisit if we run into performance
1175// issues. Note that if perf become a bottleneck for a client, then probably
1176// directly writing the proto will be even faster.
1177// ============================================================================
1178
1179inline LinearExpr operator-(LinearExpr expr) { return expr *= -1; }
1180
1181inline LinearExpr operator+(const LinearExpr& lhs, const LinearExpr& rhs) {
1182 LinearExpr temp(lhs);
1183 temp += rhs;
1184 return temp;
1185}
1186inline LinearExpr operator+(LinearExpr&& lhs, const LinearExpr& rhs) {
1187 lhs += rhs;
1188 return std::move(lhs);
1189}
1190inline LinearExpr operator+(const LinearExpr& lhs, LinearExpr&& rhs) {
1191 rhs += lhs;
1192 return std::move(rhs);
1193}
1194inline LinearExpr operator+(LinearExpr&& lhs, LinearExpr&& rhs) {
1195 if (lhs.variables().size() < rhs.variables().size()) {
1196 rhs += std::move(lhs);
1197 return std::move(rhs);
1198 } else {
1199 lhs += std::move(rhs);
1200 return std::move(lhs);
1201 }
1202}
1203
1204inline LinearExpr operator-(const LinearExpr& lhs, const LinearExpr& rhs) {
1205 LinearExpr temp(lhs);
1206 temp -= rhs;
1207 return temp;
1208}
1209inline LinearExpr operator-(LinearExpr&& lhs, const LinearExpr& rhs) {
1210 lhs -= rhs;
1211 return std::move(lhs);
1212}
1213inline LinearExpr operator-(const LinearExpr& lhs, LinearExpr&& rhs) {
1214 rhs *= -1;
1215 rhs += lhs;
1216 return std::move(rhs);
1217}
1219 lhs -= std::move(rhs);
1220 return std::move(lhs);
1221}
1222
1223inline LinearExpr operator*(LinearExpr expr, int64_t factor) {
1224 expr *= factor;
1225 return expr;
1226}
1227inline LinearExpr operator*(int64_t factor, LinearExpr expr) {
1228 expr *= factor;
1229 return expr;
1230}
1231
1232// For DoubleLinearExpr.
1233
1235 expr *= -1;
1236 return expr;
1237}
1238
1240 const DoubleLinearExpr& rhs) {
1241 DoubleLinearExpr temp(lhs);
1242 temp += rhs;
1243 return temp;
1245inline DoubleLinearExpr operator+(DoubleLinearExpr&& lhs,
1246 const DoubleLinearExpr& rhs) {
1247 lhs += rhs;
1248 return std::move(lhs);
1249}
1251 DoubleLinearExpr&& rhs) {
1252 rhs += lhs;
1253 return std::move(rhs);
1254}
1256 DoubleLinearExpr&& rhs) {
1257 if (lhs.variables().size() < rhs.variables().size()) {
1258 rhs += std::move(lhs);
1259 return std::move(rhs);
1260 } else {
1261 lhs += std::move(rhs);
1262 return std::move(lhs);
1263 }
1264}
1265
1266inline DoubleLinearExpr operator+(DoubleLinearExpr expr, double rhs) {
1267 expr += rhs;
1268 return expr;
1269}
1270inline DoubleLinearExpr operator+(double lhs, DoubleLinearExpr expr) {
1271 expr += lhs;
1272 return expr;
1273}
1274
1276 const DoubleLinearExpr& rhs) {
1277 DoubleLinearExpr temp(lhs);
1278 temp -= rhs;
1279 return temp;
1281inline DoubleLinearExpr operator-(DoubleLinearExpr&& lhs,
1282 const DoubleLinearExpr& rhs) {
1283 lhs -= rhs;
1284 return std::move(lhs);
1285}
1287 DoubleLinearExpr&& rhs) {
1288 rhs *= -1;
1289 rhs += lhs;
1290 return std::move(rhs);
1292inline DoubleLinearExpr operator-(DoubleLinearExpr&& lhs,
1293 DoubleLinearExpr&& rhs) {
1294 lhs -= std::move(rhs);
1295 return std::move(lhs);
1296}
1298inline DoubleLinearExpr operator-(DoubleLinearExpr epxr, double rhs) {
1299 epxr -= rhs;
1300 return epxr;
1301}
1302inline DoubleLinearExpr operator-(double lhs, DoubleLinearExpr expr) {
1303 expr *= -1;
1304 expr += lhs;
1305 return expr;
1306}
1308inline DoubleLinearExpr operator*(DoubleLinearExpr expr, double factor) {
1309 expr *= factor;
1310 return expr;
1311}
1312
1313inline DoubleLinearExpr operator*(double factor, DoubleLinearExpr expr) {
1314 expr *= factor;
1315 return expr;
1316}
1317
1318} // namespace sat
1319} // namespace operations_research
1320
1321#endif // OR_TOOLS_SAT_CP_MODEL_H_
IntegerValue size
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
Definition cp_model.cc:554
BoolVar WithName(absl::string_view name)
Definition cp_model.cc:39
bool operator!=(const BoolVar &other) const
Definition cp_model.h:97
std::string Name() const
Returns the name of the variable.
Definition cp_model.cc:48
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition cp_model.cc:1403
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
Definition cp_model.h:91
std::string DebugString() const
Definition cp_model.cc:59
bool operator==(const BoolVar &other) const
Definition cp_model.h:93
void AddArc(int tail, int head, BoolVar literal)
Definition cp_model.cc:514
Constraint WithName(absl::string_view name)
Sets the name of the constraint.
Definition cp_model.cc:495
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
Definition cp_model.cc:502
Constraint(ConstraintProto *proto)
Definition cp_model.cc:493
ConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition cp_model.h:567
absl::string_view Name() const
Returns the name of the constraint (or the empty string if not set).
Definition cp_model.cc:500
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition cp_model.h:564
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
Definition cp_model.cc:931
bool HasObjective() const
Checks whether the model contains an objective.
Definition cp_model.cc:1235
const CpModelProto & Build() const
Definition cp_model.h:1111
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Definition cp_model.cc:971
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
Definition cp_model.cc:1356
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
Definition cp_model.cc:682
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
Definition cp_model.cc:981
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
Definition cp_model.cc:1185
NoOverlap2DConstraint AddNoOverlap2D()
Definition cp_model.cc:1174
Constraint AddBoolXor(absl::Span< const BoolVar > literals)
Adds the constraint that an odd number of literals is true.
Definition cp_model.cc:800
IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
Creates an interval variable from 3 affine expressions.
Definition cp_model.cc:712
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
Definition cp_model.cc:1372
Constraint AddMultiplicationEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == prod(exprs).
Definition cp_model.cc:1136
CpModelBuilder Clone() const
Returns a cloned version of the current model.
Definition cp_model.cc:1336
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
Definition cp_model.cc:792
Constraint AddMaxEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == max(vars).
Definition cp_model.cc:1066
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
Definition cp_model.cc:1196
void FixVariable(IntVar var, int64_t value)
Definition cp_model.cc:750
ReservoirConstraint AddReservoirConstraint(int64_t min_level, int64_t max_level)
Definition cp_model.cc:994
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
Definition cp_model.h:810
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:1239
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
Definition cp_model.cc:845
IntervalVar NewOptionalIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end, BoolVar presence)
Definition cp_model.cc:723
void ClearAssumptions()
Remove all assumptions from the model.
Definition cp_model.cc:1332
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
Definition cp_model.cc:875
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
Definition cp_model.cc:1002
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
Definition cp_model.cc:1303
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
This constraint forces all variables to have different values.
Definition cp_model.cc:904
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
Definition cp_model.cc:1378
Constraint AddExactlyOne(absl::Span< const BoolVar > literals)
Exactly one literal is true. Sum literals == 1.
Definition cp_model.cc:784
Constraint AddMinEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == min(vars).
Definition cp_model.cc:1030
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
Definition cp_model.cc:1322
bool ExportToFile(absl::string_view filename) const
Export the model to file.
Definition cp_model.cc:1388
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Definition cp_model.cc:835
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
Definition cp_model.cc:865
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Definition cp_model.cc:962
Constraint AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
Definition cp_model.cc:942
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Definition cp_model.cc:958
Constraint AddNoOverlap(absl::Span< const IntervalVar > vars)
Definition cp_model.cc:1166
const CpModelProto & Proto() const
Definition cp_model.h:1112
BoolVar NewBoolVar()
Creates a Boolean variable.
Definition cp_model.cc:692
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
Definition cp_model.cc:892
Constraint AddAtLeastOne(absl::Span< const BoolVar > literals)
Same as AddBoolOr(). Sum literals >= 1.
Definition cp_model.cc:772
void ClearObjective()
Removes the objective from the model.
Definition cp_model.cc:1230
Constraint AddAtMostOne(absl::Span< const BoolVar > literals)
At most one literal is true. Sum literals <= 1.
Definition cp_model.cc:776
void ClearHints()
Removes all hints.
Definition cp_model.cc:1318
void AddAssumptions(absl::Span< const BoolVar > literals)
Adds multiple literals to the model as assumptions.
Definition cp_model.cc:1326
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
Definition cp_model.cc:855
Constraint AddDivisionEquality(const LinearExpr &target, const LinearExpr &numerator, const LinearExpr &denominator)
Adds target = num / denom (integer division rounded towards 0).
Definition cp_model.cc:1096
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
Definition cp_model.cc:825
void SetName(absl::string_view name)
Sets the name of the model.
Definition cp_model.cc:646
Constraint AddAbsEquality(const LinearExpr &target, const LinearExpr &expr)
Adds target == abs(expr).
Definition cp_model.cc:1106
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
Definition cp_model.cc:764
IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
Creates an interval variable with a fixed size.
Definition cp_model.cc:718
CumulativeConstraint AddCumulative(LinearExpr capacity)
Definition cp_model.cc:1178
Constraint AddModuloEquality(const LinearExpr &target, const LinearExpr &var, const LinearExpr &mod)
Adds target = var % mod.
Definition cp_model.cc:1116
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
Definition cp_model.cc:737
void AddDemand(IntervalVar interval, LinearExpr demand)
Adds a pair (interval, demand) to the constraint.
Definition cp_model.cc:571
const std::vector< double > & coefficients() const
Returns the vector of coefficients.
Definition cp_model.h:411
static DoubleLinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition cp_model.cc:331
DoubleLinearExpr & operator*=(double coeff)
Multiply the linear expression by a constant.
Definition cp_model.cc:442
DoubleLinearExpr & operator+=(double value)
Adds a constant value to the linear expression.
Definition cp_model.cc:367
DoubleLinearExpr & AddTerm(IntVar var, double coeff)
Adds a term (var * coeff) to the linear expression.
Definition cp_model.cc:391
double constant() const
Returns the constant term.
Definition cp_model.h:417
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:347
bool IsConstant() const
Returns true if the expression has no variable.
Definition cp_model.h:414
DoubleLinearExpr & operator-=(double value)
Adds a constant value to the linear expression.
Definition cp_model.cc:422
const std::vector< int > & variables() const
Returns the vector of variable indices.
Definition cp_model.h:408
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string. See the documentation for LinearExpr::DebugString().
Definition cp_model.cc:450
DoubleLinearExpr & AddExpression(const LinearExpr &exprs, double coeff=1.0)
Adds a linear expression to the double linear expression.
Definition cp_model.cc:410
bool operator==(const IntVar &other) const
Definition cp_model.h:176
bool operator!=(const IntVar &other) const
Definition cp_model.h:180
std::string Name() const
Returns the name of the variable (or the empty string if not set).
Definition cp_model.cc:126
std::string DebugString() const
Definition cp_model.cc:136
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:1392
int index() const
Returns the index of the variable in the model. This will be non-negative.
Definition cp_model.h:192
IntVar WithName(absl::string_view name)
Sets the name of the variable.
Definition cp_model.cc:119
::operations_research::Domain Domain() const
Definition cp_model.cc:131
IntervalVar WithName(absl::string_view name)
Sets the name of the variable.
Definition cp_model.cc:582
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
Definition cp_model.h:484
std::string Name() const
Returns the name of the interval (or the empty string if not set).
Definition cp_model.cc:617
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition cp_model.h:489
int index() const
Returns the index of the interval constraint in the model.
Definition cp_model.h:497
std::string DebugString() const
Returns a debug string.
Definition cp_model.cc:622
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
Definition cp_model.cc:641
bool IsConstant() const
Returns true if the expression has no variables.
Definition cp_model.h:300
LinearExpr & operator*=(int64_t factor)
Definition cp_model.cc:274
static LinearExpr Sum(absl::Span< const IntVar > vars)
NOLINTEND(google-explicit-constructor)
Definition cp_model.cc:207
std::string DebugString(const CpModelProto *proto=nullptr) const
Definition cp_model.cc:280
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:223
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
Definition cp_model.cc:243
LinearExpr & operator-=(const LinearExpr &other)
Definition cp_model.cc:264
int64_t constant() const
Returns the constant term.
Definition cp_model.h:303
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:198
const std::vector< int > & variables() const
Returns the vector of variable indices.
Definition cp_model.h:294
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
Definition cp_model.h:297
LinearExpr & operator+=(const LinearExpr &other)
Operators.
Definition cp_model.cc:255
void AddArc(int tail, int head, BoolVar literal)
Definition cp_model.cc:520
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
Definition cp_model.cc:561
void AddOptionalEvent(LinearExpr time, int64_t level_change, BoolVar is_active)
Definition cp_model.cc:545
void AddEvent(LinearExpr time, int64_t level_change)
Definition cp_model.cc:537
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
Definition cp_model.cc:526
int64_t a
Definition table.cc:44
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
int64_t value
IntVar * var
int lit
int literal
int index
std::string VarDebugString(const CpModelProto &proto, int index)
Definition cp_model.cc:143
LinearExpr operator+(const LinearExpr &lhs, const LinearExpr &rhs)
Definition cp_model.h:1186
LinearExpr operator-(LinearExpr expr)
Definition cp_model.h:1184
BoolVar Not(BoolVar x)
Definition cp_model.cc:87
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
Definition cp_model.cc:89
H AbslHashValue(H h, const IntVar &i)
– ABSL HASHING SUPPORT --------------------------------------------------—
Definition cp_model.h:515
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
Definition cp_model.cc:1392
int NegatedRef(int ref)
Small utility functions to deal with negative variable/literal references.
LinearExpr operator*(LinearExpr expr, int64_t factor)
Definition cp_model.h:1228
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition cp_model.cc:1403
In SWIG mode, we don't want anything besides these top-level includes.
const Variable x
Definition qp_tests.cc:127
int64_t demand
Definition resource.cc:126
int64_t time
Definition resource.cc:1708
IntervalVar * interval
Definition resource.cc:101
int64_t coefficient
int head
int tail
std::optional< int64_t > end
int64_t start