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 
39 #ifndef OR_TOOLS_SAT_CP_MODEL_H_
40 #define OR_TOOLS_SAT_CP_MODEL_H_
41 
42 #include <cstdint>
43 #include <initializer_list>
44 #include <limits>
45 #include <string>
46 #include <utility>
47 
48 #include "absl/container/flat_hash_map.h"
49 #include "absl/types/span.h"
53 #include "ortools/sat/model.h"
56 
57 namespace operations_research {
58 namespace sat {
59 
60 class CpModelBuilder;
61 class LinearExpr;
62 class IntVar;
63 
72 class BoolVar {
73  public:
78  BoolVar();
79 
82  BoolVar WithName(const std::string& name);
83 
85  std::string Name() const;
86 
88  BoolVar Not() const { return BoolVar(NegatedRef(index_), builder_); }
89 
90  bool operator==(const BoolVar& other) const {
91  return other.builder_ == builder_ && other.index_ == index_;
92  }
93 
94  bool operator!=(const BoolVar& other) const {
95  return other.builder_ != builder_ || other.index_ != index_;
96  }
97 
98  std::string DebugString() const;
99 
106  int index() const { return index_; }
107 
108  private:
109  friend class CircuitConstraint;
110  friend class Constraint;
111  friend class CpModelBuilder;
112  friend class DoubleLinearExpr;
113  friend class IntVar;
114  friend class IntervalVar;
116  friend class LinearExpr;
117  friend class ReservoirConstraint;
118  friend bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
119 
120  BoolVar(int index, CpModelBuilder* builder);
121 
122  CpModelBuilder* builder_ = nullptr;
123  int index_ = std::numeric_limits<int32_t>::min();
124 };
125 
126 std::ostream& operator<<(std::ostream& os, const BoolVar& var);
127 
132 BoolVar Not(BoolVar x);
133 
140 class IntVar {
141  public:
146  IntVar();
147 
154  explicit IntVar(const BoolVar& var);
155 
161  BoolVar ToBoolVar() const;
162 
164  IntVar WithName(const std::string& name);
165 
167  std::string Name() const;
168 
169  bool operator==(const IntVar& other) const {
170  return other.builder_ == builder_ && other.index_ == index_;
171  }
172 
173  bool operator!=(const IntVar& other) const {
174  return other.builder_ != builder_ || other.index_ != index_;
175  }
176 
177  // Returns the domain of the variable.
178  // Note that we keep the fully qualified return type as compilation fails with
179  // gcc otherwise.
181 
182  std::string DebugString() const;
183 
185  int index() const { return index_; }
186 
187  private:
188  friend class BoolVar;
189  friend class CpModelBuilder;
190  friend class CumulativeConstraint;
191  friend class DoubleLinearExpr;
192  friend class LinearExpr;
193  friend class IntervalVar;
194  friend class ReservoirConstraint;
195  friend int64_t SolutionIntegerValue(const CpSolverResponse& r,
196  const LinearExpr& expr);
197 
198  IntVar(int index, CpModelBuilder* builder);
199 
200  CpModelBuilder* builder_ = nullptr;
201  int index_ = std::numeric_limits<int32_t>::min();
202 };
203 
204 std::ostream& operator<<(std::ostream& os, const IntVar& var);
205 
239 class LinearExpr {
240  public:
242  LinearExpr() = default;
243 
244  // NOLINTBEGIN(google-explicit-constructor)
245 
249 
252 
254  LinearExpr(int64_t constant);
255 
256  // NOLINTEND(google-explicit-constructor)
257 
259  static LinearExpr Sum(absl::Span<const IntVar> vars);
260 
262  static LinearExpr Sum(absl::Span<const BoolVar> vars);
263 
265  static LinearExpr WeightedSum(absl::Span<const IntVar> vars,
266  absl::Span<const int64_t> coeffs);
267 
269  static LinearExpr WeightedSum(absl::Span<const BoolVar> vars,
270  absl::Span<const int64_t> coeffs);
271 
273  static LinearExpr Term(IntVar var, int64_t coefficient);
274 
276  static LinearExpr Term(BoolVar var, int64_t coefficient);
277 
280 
281  // Operators.
282  LinearExpr& operator+=(const LinearExpr& other);
283  LinearExpr& operator-=(const LinearExpr& other);
284  LinearExpr& operator*=(int64_t factor);
285 
287  const std::vector<int>& variables() const { return variables_; }
288 
290  const std::vector<int64_t>& coefficients() const { return coefficients_; }
291 
293  const bool IsConstant() const { return variables_.empty(); }
294 
296  int64_t constant() const { return constant_; }
297 
303  std::string DebugString(const CpModelProto* proto = nullptr) const;
304 
305  private:
306  std::vector<int> variables_;
307  std::vector<int64_t> coefficients_;
308  int64_t constant_ = 0;
309 };
310 
311 std::ostream& operator<<(std::ostream& os, const LinearExpr& e);
312 
350  public:
352 
355  explicit DoubleLinearExpr(BoolVar var);
356 
358  explicit DoubleLinearExpr(IntVar var);
359 
361  explicit DoubleLinearExpr(double constant);
362 
365 
369 
372 
374  DoubleLinearExpr& AddTerm(IntVar var, double coeff);
375  DoubleLinearExpr& AddTerm(BoolVar var, double coeff);
376 
379 
382 
385 
387  DoubleLinearExpr& operator*=(double coeff);
388 
390  static DoubleLinearExpr Sum(absl::Span<const IntVar> vars);
391 
393  static DoubleLinearExpr Sum(absl::Span<const BoolVar> vars);
394 
396  static DoubleLinearExpr WeightedSum(absl::Span<const IntVar> vars,
397  absl::Span<const double> coeffs);
398 
400  static DoubleLinearExpr WeightedSum(absl::Span<const BoolVar> vars,
401  absl::Span<const double> coeffs);
402 
404  const std::vector<int>& variables() const { return variables_; }
405 
407  const std::vector<double>& coefficients() const { return coefficients_; }
408 
409  // Returns true if the expression has no variable.
410  const bool IsConstant() const { return variables_.empty(); }
411 
413  double constant() const { return constant_; }
414 
416  std::string DebugString(const CpModelProto* proto = nullptr) const;
417 
418  private:
419  std::vector<int> variables_;
420  std::vector<double> coefficients_;
421  double constant_ = 0;
422 };
423 
424 std::ostream& operator<<(std::ostream& os, const DoubleLinearExpr& e);
425 
446 class IntervalVar {
447  public:
452  IntervalVar();
453 
455  IntervalVar WithName(const std::string& name);
456 
458  std::string Name() const;
459 
462  LinearExpr StartExpr() const;
463 
466  LinearExpr SizeExpr() const;
467 
470  LinearExpr EndExpr() const;
471 
477  BoolVar PresenceBoolVar() const;
478 
480  bool operator==(const IntervalVar& other) const {
481  return other.builder_ == builder_ && other.index_ == index_;
482  }
483 
485  bool operator!=(const IntervalVar& other) const {
486  return other.builder_ != builder_ || other.index_ != index_;
487  }
488 
490  std::string DebugString() const;
491 
493  int index() const { return index_; }
494 
495  private:
496  friend class CpModelBuilder;
497  friend class CumulativeConstraint;
498  friend class NoOverlap2DConstraint;
499  friend std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
500 
501  IntervalVar(int index, CpModelBuilder* builder);
502 
503  CpModelBuilder* builder_ = nullptr;
504  int index_ = std::numeric_limits<int32_t>::min();
505 };
506 
507 std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
508 
518 class Constraint {
519  public:
537  Constraint OnlyEnforceIf(absl::Span<const BoolVar> literals);
538 
541 
543  Constraint WithName(const std::string& name);
544 
546  const std::string& Name() const;
547 
549  const ConstraintProto& Proto() const { return *proto_; }
550 
552  ConstraintProto* MutableProto() const { return proto_; }
553 
554  protected:
555  friend class CpModelBuilder;
556 
557  explicit Constraint(ConstraintProto* proto);
558 
560 };
561 
568  public:
576  void AddArc(int tail, int head, BoolVar literal);
577 
578  private:
579  friend class CpModelBuilder;
580 
582 };
583 
591  public:
599  void AddArc(int tail, int head, BoolVar literal);
600 
601  private:
602  friend class CpModelBuilder;
603 
605 };
606 
613 class TableConstraint : public Constraint {
614  public:
616  void AddTuple(absl::Span<const int64_t> tuple);
617 
618  private:
619  friend class CpModelBuilder;
620 
622 };
623 
631  public:
638  void AddEvent(LinearExpr time, int64_t level_change);
639 
646  void AddOptionalEvent(LinearExpr time, int64_t level_change,
647  BoolVar is_active);
648 
649  private:
650  friend class CpModelBuilder;
651 
653 
654  CpModelBuilder* builder_;
655 };
656 
664  public:
666  void AddTransition(int tail, int head, int64_t transition_label);
667 
668  private:
669  friend class CpModelBuilder;
670 
672 };
673 
681  public:
683  void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate);
684 
685  private:
686  friend class CpModelBuilder;
687 
689 };
690 
701  public:
704 
705  private:
706  friend class CpModelBuilder;
707 
709 
710  CpModelBuilder* builder_;
711 };
712 
721  public:
723  void SetName(const std::string& name);
724 
726  IntVar NewIntVar(const Domain& domain);
727 
730 
734  IntVar NewConstant(int64_t value);
735 
739  BoolVar TrueVar();
740 
744  BoolVar FalseVar();
745 
747  IntervalVar NewIntervalVar(const LinearExpr& start, const LinearExpr& size,
748  const LinearExpr& end);
749 
751  IntervalVar NewFixedSizeIntervalVar(const LinearExpr& start, int64_t size);
752 
756  const LinearExpr& size,
757  const LinearExpr& end, BoolVar presence);
758 
761  int64_t size, BoolVar presence);
762 
764  Constraint AddBoolOr(absl::Span<const BoolVar> literals);
765 
767  Constraint AddAtLeastOne(absl::Span<const BoolVar> literals);
768 
770  Constraint AddAtMostOne(absl::Span<const BoolVar> literals);
771 
773  Constraint AddExactlyOne(absl::Span<const BoolVar> literals);
774 
776  Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
777 
779  Constraint AddBoolXor(absl::Span<const BoolVar> literals);
780 
783  return AddBoolOr({a.Not(), b});
784  }
785 
787  Constraint AddEquality(const LinearExpr& left, const LinearExpr& right);
788 
790  Constraint AddGreaterOrEqual(const LinearExpr& left, const LinearExpr& right);
791 
793  Constraint AddGreaterThan(const LinearExpr& left, const LinearExpr& right);
794 
796  Constraint AddLessOrEqual(const LinearExpr& left, const LinearExpr& right);
797 
799  Constraint AddLessThan(const LinearExpr& left, const LinearExpr& right);
800 
802  Constraint AddLinearConstraint(const LinearExpr& expr, const Domain& domain);
803 
805  Constraint AddNotEqual(const LinearExpr& left, const LinearExpr& right);
806 
808  Constraint AddAllDifferent(absl::Span<const IntVar> vars);
809 
811  Constraint AddAllDifferent(absl::Span<const LinearExpr> exprs);
812 
814  Constraint AddAllDifferent(std::initializer_list<LinearExpr> exprs);
815 
818  absl::Span<const IntVar> variables,
819  IntVar target);
820 
822  Constraint AddElement(IntVar index, absl::Span<const int64_t> values,
823  IntVar target);
824 
842 
857 
869  TableConstraint AddAllowedAssignments(absl::Span<const IntVar> vars);
870 
881  TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> vars);
882 
888  Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
889  absl::Span<const IntVar> inverse_variables);
890 
911  int64_t max_level);
912 
940  absl::Span<const IntVar> transition_variables, int starting_state,
941  absl::Span<const int> final_states);
942 
944  Constraint AddMinEquality(const LinearExpr& target,
945  absl::Span<const IntVar> vars);
946 
948  Constraint AddMinEquality(const LinearExpr& target,
949  absl::Span<const LinearExpr> exprs);
950 
952  Constraint AddMinEquality(const LinearExpr& target,
953  std::initializer_list<LinearExpr> exprs);
954 
956  Constraint AddMaxEquality(const LinearExpr& target,
957  absl::Span<const IntVar> vars);
958 
960  Constraint AddMaxEquality(const LinearExpr& target,
961  absl::Span<const LinearExpr> exprs);
962 
964  Constraint AddMaxEquality(const LinearExpr& target,
965  std::initializer_list<LinearExpr> exprs);
966 
969  const LinearExpr& numerator,
970  const LinearExpr& denominator);
971 
973  Constraint AddAbsEquality(const LinearExpr& target, const LinearExpr& expr);
974 
976  Constraint AddModuloEquality(const LinearExpr& target, const LinearExpr& var,
977  const LinearExpr& mod);
978 
981  absl::Span<const LinearExpr> exprs);
982 
985  absl::Span<const IntVar> vars);
986 
989  std::initializer_list<LinearExpr> exprs);
990 
993  const LinearExpr& left,
994  const LinearExpr& right);
995 
1000  Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
1001 
1006 
1014 
1016  void Minimize(const LinearExpr& expr);
1017 
1020  void Minimize(const DoubleLinearExpr& expr);
1021 
1023  void Maximize(const LinearExpr& expr);
1024 
1027  void Maximize(const DoubleLinearExpr& expr);
1028 
1030  void AddDecisionStrategy(
1031  absl::Span<const IntVar> variables,
1034 
1036  void AddDecisionStrategy(
1037  absl::Span<const BoolVar> variables,
1040 
1042  void AddHint(IntVar var, int64_t value);
1043 
1045  void AddHint(BoolVar var, bool value);
1046 
1048  void ClearHints();
1049 
1051  void AddAssumption(BoolVar lit);
1052 
1054  void AddAssumptions(absl::Span<const BoolVar> literals);
1055 
1057  void ClearAssumptions();
1058 
1059  const CpModelProto& Build() const { return Proto(); }
1060 
1061  const CpModelProto& Proto() const { return cp_model_; }
1062  CpModelProto* MutableProto() { return &cp_model_; }
1063 
1065  void CopyFrom(const CpModelProto& model_proto);
1066 
1069 
1072 
1075 
1076  private:
1077  friend class CumulativeConstraint;
1078  friend class ReservoirConstraint;
1079  friend class IntervalVar;
1080  friend class IntVar;
1081 
1082  // Fills the 'expr_proto' with the linear expression represented by 'expr'.
1083  LinearExpressionProto LinearExprToProto(const LinearExpr& expr,
1084  bool negate = false);
1085 
1086  // Returns a (cached) integer variable index with a constant value.
1087  int IndexFromConstant(int64_t value);
1088 
1089  // Returns a valid integer index from a BoolVar index.
1090  // If the input index is a positive, it returns this index.
1091  // If the input index is negative, it creates a cached IntVar equal to
1092  // 1 - BoolVar(PositiveRef(index)), and returns the index of this new
1093  // variable.
1094  int GetOrCreateIntegerIndex(int index);
1095 
1096  void FillLinearTerms(const LinearExpr& left, const LinearExpr& right,
1098 
1099  CpModelProto cp_model_;
1100  absl::flat_hash_map<int64_t, int> constant_to_index_map_;
1101  absl::flat_hash_map<int, int> bool_to_integer_index_map_;
1102 };
1103 
1105 int64_t SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
1106 
1109 
1110 // Returns a more readable and compact DebugString() than
1111 // proto.variables(index).DebugString(). This is used by IntVar::DebugString()
1112 // but also allow to get the same string from a const proto.
1113 std::string VarDebugString(const CpModelProto& proto, int index);
1114 
1115 // ============================================================================
1116 // Minimal support for "natural" API to create LinearExpr.
1117 //
1118 // Note(user): This might be optimized further by optimizing LinearExpr for
1119 // holding one term, or introducing an LinearTerm class, but these should mainly
1120 // be used to construct small expressions. Revisit if we run into performance
1121 // issues. Note that if perf become a bottleneck for a client, then probably
1122 // directly writing the proto will be even faster.
1123 // ============================================================================
1124 
1125 inline LinearExpr operator-(LinearExpr expr) { return expr *= -1; }
1126 
1127 inline LinearExpr operator+(const LinearExpr& lhs, const LinearExpr& rhs) {
1128  LinearExpr temp(lhs);
1129  temp += rhs;
1130  return temp;
1131 }
1132 inline LinearExpr operator+(LinearExpr&& lhs, const LinearExpr& rhs) {
1133  lhs += rhs;
1134  return std::move(lhs);
1135 }
1136 inline LinearExpr operator+(const LinearExpr& lhs, LinearExpr&& rhs) {
1137  rhs += lhs;
1138  return std::move(rhs);
1139 }
1141  if (lhs.variables().size() < rhs.variables().size()) {
1142  rhs += std::move(lhs);
1143  return std::move(rhs);
1144  } else {
1145  lhs += std::move(rhs);
1146  return std::move(lhs);
1147  }
1148 }
1149 
1150 inline LinearExpr operator-(const LinearExpr& lhs, const LinearExpr& rhs) {
1151  LinearExpr temp(lhs);
1152  temp -= rhs;
1153  return temp;
1154 }
1155 inline LinearExpr operator-(LinearExpr&& lhs, const LinearExpr& rhs) {
1156  lhs -= rhs;
1157  return std::move(lhs);
1158 }
1159 inline LinearExpr operator-(const LinearExpr& lhs, LinearExpr&& rhs) {
1160  rhs *= -1;
1161  rhs += lhs;
1162  return std::move(rhs);
1163 }
1165  lhs -= std::move(rhs);
1166  return std::move(lhs);
1167 }
1168 
1169 inline LinearExpr operator*(LinearExpr expr, int64_t factor) {
1170  expr *= factor;
1171  return expr;
1172 }
1173 inline LinearExpr operator*(int64_t factor, LinearExpr expr) {
1174  expr *= factor;
1175  return expr;
1176 }
1177 
1178 // For DoubleLinearExpr.
1179 
1181  expr *= -1;
1182  return expr;
1183 }
1184 
1186  const DoubleLinearExpr& rhs) {
1187  DoubleLinearExpr temp(lhs);
1188  temp += rhs;
1189  return temp;
1190 }
1192  const DoubleLinearExpr& rhs) {
1193  lhs += rhs;
1194  return std::move(lhs);
1195 }
1197  DoubleLinearExpr&& rhs) {
1198  rhs += lhs;
1199  return std::move(rhs);
1200 }
1202  DoubleLinearExpr&& rhs) {
1203  if (lhs.variables().size() < rhs.variables().size()) {
1204  rhs += std::move(lhs);
1205  return std::move(rhs);
1206  } else {
1207  lhs += std::move(rhs);
1208  return std::move(lhs);
1209  }
1210 }
1211 
1212 inline DoubleLinearExpr operator+(DoubleLinearExpr expr, double rhs) {
1213  expr += rhs;
1214  return expr;
1215 }
1216 inline DoubleLinearExpr operator+(double lhs, DoubleLinearExpr expr) {
1217  expr += lhs;
1218  return expr;
1219 }
1220 
1222  const DoubleLinearExpr& rhs) {
1223  DoubleLinearExpr temp(lhs);
1224  temp -= rhs;
1225  return temp;
1226 }
1228  const DoubleLinearExpr& rhs) {
1229  lhs -= rhs;
1230  return std::move(lhs);
1231 }
1233  DoubleLinearExpr&& rhs) {
1234  rhs *= -1;
1235  rhs += lhs;
1236  return std::move(rhs);
1237 }
1239  DoubleLinearExpr&& rhs) {
1240  lhs -= std::move(rhs);
1241  return std::move(lhs);
1242 }
1243 
1244 inline DoubleLinearExpr operator-(DoubleLinearExpr epxr, double rhs) {
1245  epxr -= rhs;
1246  return epxr;
1247 }
1248 inline DoubleLinearExpr operator-(double lhs, DoubleLinearExpr expr) {
1249  expr *= -1;
1250  expr += lhs;
1251  return expr;
1252 }
1253 
1254 inline DoubleLinearExpr operator*(DoubleLinearExpr expr, double factor) {
1255  expr *= factor;
1256  return expr;
1257 }
1258 
1259 inline DoubleLinearExpr operator*(double factor, DoubleLinearExpr expr) {
1260  expr *= factor;
1261  return expr;
1262 }
1263 
1264 } // namespace sat
1265 } // namespace operations_research
1266 
1267 #endif // OR_TOOLS_SAT_CP_MODEL_H_
int64_t head
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:507
LinearExpr operator-(LinearExpr expr)
Definition: cp_model.h:1125
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:1312
BoolVar FalseVar()
Creates an always false Boolean variable.
Definition: cp_model.cc:695
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
Definition: cp_model.cc:726
Specialized reservoir constraint.
Definition: cp_model.h:630
LinearExpr EndExpr() const
Returns the end linear expression.
Definition: cp_model.cc:590
IntervalVar()
A default constructed IntervalVar can be used to mean not defined yet.
Definition: cp_model.cc:564
BoolVar TrueVar()
Creates an always true Boolean variable.
Definition: cp_model.cc:691
Constraint AddMinEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == min(vars).
Definition: cp_model.cc:1005
const CpModelProto & Proto() const
Definition: cp_model.h:1061
int64_t min
Definition: alldiff_cst.cc:139
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
Definition: cp_model.cc:1252
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
Definition: cp_model.cc:1312
std::string DebugString() const
Definition: cp_model.cc:56
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
Definition: cp_model.cc:242
A dedicated container for linear expressions with double coefficients.
Definition: cp_model.h:349
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
Definition: cp_model.cc:929
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
Definition: cp_model.cc:830
Specialized no_overlap2D constraint.
Definition: cp_model.h:680
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
Definition: cp_model.h:88
int64_t constant() const
Returns the constant term.
Definition: cp_model.h:296
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:1209
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
This constraint forces all variables to have different values.
Definition: cp_model.cc:879
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
Definition: cp_model.cc:800
ConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:552
bool operator==(const BoolVar &other) const
Definition: cp_model.h:90
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Adds an allowed assignments constraint.
Definition: cp_model.cc:937
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
Definition: cp_model.cc:628
int index() const
Returns the index of the interval constraint in the model.
Definition: cp_model.h:493
const std::string name
ReservoirConstraint AddReservoirConstraint(int64_t min_level, int64_t max_level)
Adds a reservoir constraint with optional refill/emptying events.
Definition: cp_model.cc:969
const std::vector< double > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:407
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:1323
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
Definition: cp_model.cc:513
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:569
LinearExpr & operator+=(const LinearExpr &other)
Definition: cp_model.cc:254
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
Definition: cp_model.h:480
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
Definition: cp_model.cc:739
IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
Creates an interval variable with a fixed size.
Definition: cp_model.cc:705
Constraint AddMultiplicationEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == prod(exprs).
Definition: cp_model.cc:1111
std::string Name() const
Returns the name of the variable (or the empty string if not set).
Definition: cp_model.cc:125
std::string VarDebugString(const CpModelProto &proto, int index)
Definition: cp_model.cc:142
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
Definition: cp_model.cc:1280
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
An automaton constraint.
Definition: cp_model.cc:977
Specialized circuit constraint.
Definition: cp_model.h:567
Constraint AddBoolXor(absl::Span< const BoolVar > literals)
Adds the constraint that an odd number of literals is true.
Definition: cp_model.cc:775
::operations_research::Domain Domain() const
Definition: cp_model.cc:130
Specialized cumulative constraint.
Definition: cp_model.h:700
bool operator!=(const IntVar &other) const
Definition: cp_model.h:173
Specialized assignment constraint.
Definition: cp_model.h:613
int64_t coefficient
void AddEvent(LinearExpr time, int64_t level_change)
Adds a mandatory event.
Definition: cp_model.cc:524
An integer variable.
Definition: cp_model.h:140
int64_t tail
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
Definition: cp_model.cc:1160
IntVar()
A default constructed IntVar can be used to mean not defined yet.
Definition: cp_model.cc:91
void AddDemand(IntervalVar interval, LinearExpr demand)
Adds a pair (interval, demand) to the constraint.
Definition: cp_model.cc:558
int64_t b
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:290
DoubleLinearExpr & operator *=(double coeff)
Multiply the linear expression by a constant.
Definition: cp_model.cc:429
CumulativeConstraint AddCumulative(LinearExpr capacity)
The cumulative constraint.
Definition: cp_model.cc:1153
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition: cp_model.h:485
IntVar NewConstant(int64_t value)
Creates a constant variable.
Definition: cp_model.cc:687
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
Definition: cp_model.cc:1172
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
Definition: cp_model.cc:1296
IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
Creates an interval variable from 3 affine expressions.
Definition: cp_model.cc:699
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
Definition: cp_model.cc:867
void SetName(const std::string &name)
Sets the name of the model.
Definition: cp_model.cc:633
void ClearAssumptions()
Remove all assumptions from the model.
Definition: cp_model.cc:1262
A Boolean variable.
Definition: cp_model.h:72
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:549
CpModelProto proto
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
An inverse constraint.
Definition: cp_model.cc:956
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
Definition: cp_model.cc:108
std::string Name() const
Returns the name of the variable.
Definition: cp_model.cc:45
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.
Definition: cp_model.cc:933
Constraint AddModuloEquality(const LinearExpr &target, const LinearExpr &var, const LinearExpr &mod)
Adds target = var % mod.
Definition: cp_model.cc:1091
Constraint AddExactlyOne(absl::Span< const BoolVar > literals)
Exactly one literal is true. Sum literals == 1.
Definition: cp_model.cc:759
BoolVar()
A default constructed BoolVar can be used to mean not defined yet.
Definition: cp_model.cc:31
BoolVar PresenceBoolVar() const
Returns a BoolVar indicating the presence of this interval.
Definition: cp_model.cc:597
DoubleLinearExpr & AddTerm(IntVar var, double coeff)
Adds a term (var * coeff) to the linear expression.
Definition: cp_model.cc:390
Constraint AddAtMostOne(absl::Span< const BoolVar > literals)
At most one literal is true. Sum literals <= 1.
Definition: cp_model.cc:751
int64_t demand
Definition: resource.cc:125
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string. See the documentation for LinearExpr::DebugString().
Definition: cp_model.cc:437
LinearExpr operator *(LinearExpr expr, int64_t factor)
Definition: cp_model.h:1169
int index() const
Returns the index of the variable in the model. This will be non-negative.
Definition: cp_model.h:185
Constraint AddDivisionEquality(const LinearExpr &target, const LinearExpr &numerator, const LinearExpr &denominator)
Adds target = num / denom (integer division rounded towards 0).
Definition: cp_model.cc:1071
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:1323
Constraint AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
Definition: cp_model.cc:917
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
Definition: cp_model.cc:1302
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Definition: cp_model.cc:487
std::string DebugString() const
Returns a debug string.
Definition: cp_model.cc:609
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:84
int64_t capacity
int index
Definition: pack.cc:509
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
Definition: cp_model.cc:669
const CpModelProto & Build() const
Definition: cp_model.h:1059
Constraint AddAtLeastOne(absl::Span< const BoolVar > literals)
Same as AddBoolOr. Sum literals >= 1.
Definition: cp_model.cc:747
static LinearExpr FromProto(const LinearExpressionProto &proto)
Constructs a linear expr from its proto representation.
Definition: cp_model.cc:197
A dedicated container for linear expressions.
Definition: cp_model.h:239
Constraint AddAbsEquality(const LinearExpr &target, const LinearExpr &expr)
Adds target == abs(expr).
Definition: cp_model.cc:1081
LinearExpr()=default
Creates an empty linear expression with value zero.
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
The constraint will be enforced iff all literals listed here are true.
Definition: cp_model.cc:489
static DoubleLinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:330
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:206
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
Definition: cp_model.h:782
void AddAssumptions(absl::Span< const BoolVar > literals)
Adds multiple literals to the model as assumptions.
Definition: cp_model.cc:1256
Constraint(ConstraintProto *proto)
Definition: cp_model.cc:480
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
Definition: cp_model.cc:767
void AddOptionalEvent(LinearExpr time, int64_t level_change, BoolVar is_active)
Adds an optional event.
Definition: cp_model.cc:532
CpModelProto const * model_proto
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:501
void ClearHints()
Removes all hints.
Definition: cp_model.cc:1248
bool operator!=(const BoolVar &other) const
Definition: cp_model.h:94
We call domain any subset of Int64 = [kint64min, kint64max].
const std::vector< int > & variables() const
Returns the vector of variable indices.
Definition: cp_model.h:287
LinearExpr operator+(const LinearExpr &lhs, const LinearExpr &rhs)
Definition: cp_model.h:1127
Specialized automaton constraint.
Definition: cp_model.h:663
LinearExpr & operator *=(int64_t factor)
Definition: cp_model.cc:273
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:222
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Definition: cp_model.cc:810
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
Definition: cp_model.cc:906
double constant() const
Returns the constant term.
Definition: cp_model.h:413
BoolVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:36
DoubleLinearExpr & operator+=(double value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:366
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
Definition: cp_model.cc:840
DoubleLinearExpr & operator-=(double value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:409
IntVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:118
BoolVar NewBoolVar()
Creates a Boolean variable.
Definition: cp_model.cc:679
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:1141
Constraint WithName(const std::string &name)
Sets the name of the constraint.
Definition: cp_model.cc:482
std::string DebugString() const
Definition: cp_model.cc:135
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:106
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
Definition: cp_model.cc:548
NoOverlap2DConstraint AddNoOverlap2D()
The no_overlap_2d constraint prevents a set of boxes from overlapping.
Definition: cp_model.cc:1149
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Definition: linear_expr.h:114
Collection of objects used to extend the Constraint Solver library.
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
Definition: cp_model.cc:541
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Adds an forbidden assignments constraint.
Definition: cp_model.cc:946
int64_t time
Definition: resource.cc:1691
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
Definition: cp_model.cc:1233
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:710
LinearExpr StartExpr() const
Returns the start linear expression.
Definition: cp_model.cc:576
LinearExpr SizeExpr() const
Returns the size linear expression.
Definition: cp_model.cc:583
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
Definition: cp_model.cc:86
IntVar * var
Definition: expr_array.cc:1874
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
Definition: cp_model.cc:820
LinearExpr & operator-=(const LinearExpr &other)
Definition: cp_model.cc:263
bool operator==(const IntVar &other) const
Definition: cp_model.h:169
std::string Name() const
Returns the name of the interval (or the empty string if not set).
Definition: cp_model.cc:604
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string.
Definition: cp_model.cc:279
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
Definition: cp_model.cc:850
Wrapper class around the cp_model proto.
Definition: cp_model.h:720
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:346
Represents a Interval variable.
Definition: cp_model.h:446
const std::vector< int > & variables() const
Returns the vector of variable indices.
Definition: cp_model.h:404
int64_t value
Literal literal
Definition: optimization.cc:85
IntervalVar * interval
Definition: resource.cc:100
void CopyFrom(const CpModelProto &model_proto)
Replaces the current model with the one from the given proto.
Definition: cp_model.cc:1266
Constraint AddMaxEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == max(vars).
Definition: cp_model.cc:1041
int64_t a
const bool IsConstant() const
Returns true if the expression has no variables.
Definition: cp_model.h:293