C++ Reference

C++ Reference: CP-SAT

cp_model.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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 <string>
42 
43 #include "absl/container/flat_hash_map.h"
44 #include "absl/types/span.h"
47 #include "ortools/sat/cp_model_utils.h"
48 #include "ortools/sat/model.h"
49 #include "ortools/sat/sat_parameters.pb.h"
51 
53 namespace sat {
54 
55 class CpModelBuilder;
56 class LinearExpr;
57 
66 class BoolVar {
67  public:
68  BoolVar();
69 
71  BoolVar WithName(const std::string& name);
72 
74  const std::string& Name() const { return Proto().name(); }
75 
77  BoolVar Not() const { return BoolVar(NegatedRef(index_), cp_model_); }
78 
80  bool operator==(const BoolVar& other) const {
81  return other.cp_model_ == cp_model_ && other.index_ == index_;
82  }
83 
85  bool operator!=(const BoolVar& other) const {
86  return other.cp_model_ != cp_model_ || other.index_ != index_;
87  }
88 
90  std::string DebugString() const;
91 
93  const IntegerVariableProto& Proto() const {
94  return cp_model_->variables(index_);
95  }
96 
99  return cp_model_->mutable_variables(index_);
100  }
101 
108  int index() const { return index_; }
109 
110  private:
111  friend class CircuitConstraint;
112  friend class Constraint;
113  friend class CpModelBuilder;
114  friend class IntVar;
115  friend class IntervalVar;
117  friend class LinearExpr;
118  friend class ReservoirConstraint;
119  friend bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
120 
121  BoolVar(int index, CpModelProto* cp_model);
122 
123  CpModelProto* cp_model_ = nullptr;
124  int index_ = kint32min;
125 };
126 
127 std::ostream& operator<<(std::ostream& os, const BoolVar& var);
128 
133 BoolVar Not(BoolVar x);
134 
145 class IntVar {
146  public:
147  IntVar();
148 
150  IntVar(const BoolVar& var); // NOLINT(runtime/explicit)
151 
153  IntVar WithName(const std::string& name);
154 
156  const std::string& Name() const { return Proto().name(); }
157 
160  LinearExpr AddConstant(int64 value) const;
161 
163  bool operator==(const IntVar& other) const {
164  return other.cp_model_ == cp_model_ && other.index_ == index_;
165  }
166 
168  bool operator!=(const IntVar& other) const {
169  return other.cp_model_ != cp_model_ || other.index_ != index_;
170  }
171 
173  std::string DebugString() const;
174 
176  const IntegerVariableProto& Proto() const {
177  return cp_model_->variables(index_);
178  }
179 
182  return cp_model_->mutable_variables(index_);
183  }
184 
186  int index() const { return index_; }
187 
188  private:
189  friend class CpModelBuilder;
190  friend class CumulativeConstraint;
191  friend class LinearExpr;
192  friend class IntervalVar;
193  friend class ReservoirConstraint;
194  friend int64 SolutionIntegerValue(const CpSolverResponse& r,
195  const LinearExpr& expr);
196  friend int64 SolutionIntegerMin(const CpSolverResponse& r, IntVar x);
197  friend int64 SolutionIntegerMax(const CpSolverResponse& r, IntVar x);
198 
199  IntVar(int index, CpModelProto* cp_model);
200 
201  CpModelProto* cp_model_ = nullptr;
202  int index_ = kint32min;
203 };
204 
205 std::ostream& operator<<(std::ostream& os, const IntVar& var);
206 
242 class LinearExpr {
243  public:
244  LinearExpr();
245 
251  LinearExpr(BoolVar var); // NOLINT(runtime/explicit)
252 
254  LinearExpr(IntVar var); // NOLINT(runtime/explicit)
255 
257  LinearExpr(int64 constant); // NOLINT(runtime/explicit)
258 
260  LinearExpr& AddConstant(int64 value);
261 
263  void AddVar(IntVar var);
264 
266  void AddTerm(IntVar var, int64 coeff);
267 
269  static LinearExpr Sum(absl::Span<const IntVar> vars);
270 
272  static LinearExpr ScalProd(absl::Span<const IntVar> vars,
273  absl::Span<const int64> coeffs);
274 
276  static LinearExpr BooleanSum(absl::Span<const BoolVar> vars);
277 
279  static LinearExpr BooleanScalProd(absl::Span<const BoolVar> vars,
280  absl::Span<const int64> coeffs);
282  static LinearExpr Term(IntVar var, int64 coefficient);
283 
285  const std::vector<IntVar>& variables() const { return variables_; }
286 
288  const std::vector<int64>& coefficients() const { return coefficients_; }
289 
291  int64 constant() const { return constant_; }
292 
293  // TODO(user): LinearExpr.DebugString() and operator<<.
294 
295  private:
296  std::vector<IntVar> variables_;
297  std::vector<int64> coefficients_;
298  int64 constant_ = 0;
299 };
300 
320 class IntervalVar {
321  public:
323  IntervalVar();
324 
326  IntervalVar WithName(const std::string& name);
327 
329  std::string Name() const;
330 
332  IntVar StartVar() const;
333 
335  IntVar SizeVar() const;
336 
338  IntVar EndVar() const;
339 
345  BoolVar PresenceBoolVar() const;
346 
348  bool operator==(const IntervalVar& other) const {
349  return other.cp_model_ == cp_model_ && other.index_ == index_;
350  }
351 
353  bool operator!=(const IntervalVar& other) const {
354  return other.cp_model_ != cp_model_ || other.index_ != index_;
355  }
356 
358  std::string DebugString() const;
359 
362  return cp_model_->constraints(index_).interval();
363  }
364 
367  return cp_model_->mutable_constraints(index_)->mutable_interval();
368  }
369 
371  int index() const { return index_; }
372 
373  private:
374  friend class CpModelBuilder;
375  friend class CumulativeConstraint;
376  friend class NoOverlap2DConstraint;
377  friend std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
378 
379  IntervalVar(int index, CpModelProto* cp_model);
380 
381  CpModelProto* cp_model_ = nullptr;
382  int index_ = kint32min;
383 };
384 
385 std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
386 
396 class Constraint {
397  public:
415  Constraint OnlyEnforceIf(absl::Span<const BoolVar> literals);
416 
419 
421  Constraint WithName(const std::string& name);
422 
424  const std::string& Name() const;
425 
427  const ConstraintProto& Proto() const { return *proto_; }
428 
430  ConstraintProto* MutableProto() const { return proto_; }
431 
432  protected:
433  friend class CpModelBuilder;
434 
435  explicit Constraint(ConstraintProto* proto);
436 
438 };
439 
446  public:
454  void AddArc(int tail, int head, BoolVar literal);
455 
456  private:
457  friend class CpModelBuilder;
458 
460 };
461 
469  public:
477  void AddArc(int tail, int head, BoolVar literal);
478 
479  private:
480  friend class CpModelBuilder;
481 
483 };
484 
491 class TableConstraint : public Constraint {
492  public:
494  void AddTuple(absl::Span<const int64> tuple);
495 
496  private:
497  friend class CpModelBuilder;
498 
500 };
501 
509  public:
515  void AddEvent(IntVar time, int64 demand);
516 
523  void AddOptionalEvent(IntVar time, int64 demand, BoolVar is_active);
524 
525  private:
526  friend class CpModelBuilder;
527 
529 
530  CpModelBuilder* builder_;
531 };
532 
540  public:
542  void AddTransition(int tail, int head, int64 transition_label);
543 
544  private:
545  friend class CpModelBuilder;
546 
548 };
549 
557  public:
559  void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate);
560 
561  private:
562  friend class CpModelBuilder;
563 
565 };
566 
574  public:
576  void AddDemand(IntervalVar interval, IntVar demand);
577 
578  private:
579  friend class CpModelBuilder;
580 
582 
583  CpModelBuilder* builder_;
584 };
585 
594  public:
596  IntVar NewIntVar(const Domain& domain);
597 
600 
602  IntVar NewConstant(int64 value);
603 
605  BoolVar TrueVar();
606 
608  BoolVar FalseVar();
609 
611  IntervalVar NewIntervalVar(IntVar start, IntVar size, IntVar end);
612 
615  BoolVar presence);
616 
618  Constraint AddBoolOr(absl::Span<const BoolVar> literals);
619 
621  Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
622 
624  Constraint AddBoolXor(absl::Span<const BoolVar> literals);
625 
628  return AddBoolOr({a.Not(), b});
629  }
630 
632  Constraint AddEquality(const LinearExpr& left, const LinearExpr& right);
633 
635  Constraint AddGreaterOrEqual(const LinearExpr& left, const LinearExpr& right);
636 
638  Constraint AddGreaterThan(const LinearExpr& left, const LinearExpr& right);
639 
641  Constraint AddLessOrEqual(const LinearExpr& left, const LinearExpr& right);
642 
644  Constraint AddLessThan(const LinearExpr& left, const LinearExpr& right);
645 
647  Constraint AddLinearConstraint(const LinearExpr& expr, const Domain& domain);
648 
650  Constraint AddNotEqual(const LinearExpr& left, const LinearExpr& right);
651 
653  Constraint AddAllDifferent(absl::Span<const IntVar> vars);
654 
657  absl::Span<const IntVar> variables,
658  IntVar target);
659 
661  Constraint AddElement(IntVar index, absl::Span<const int64> values,
662  IntVar target);
663 
681 
696 
708  TableConstraint AddAllowedAssignments(absl::Span<const IntVar> vars);
709 
720  TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> vars);
721 
727  Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
728  absl::Span<const IntVar> inverse_variables);
729 
748  ReservoirConstraint AddReservoirConstraint(int64 min_level, int64 max_level);
749 
778  absl::Span<const IntVar> transition_variables, int starting_state,
779  absl::Span<const int> final_states);
780 
782  Constraint AddMinEquality(IntVar target, absl::Span<const IntVar> vars);
783 
786  absl::Span<const LinearExpr> exprs);
787 
789  Constraint AddMaxEquality(IntVar target, absl::Span<const IntVar> vars);
790 
793  absl::Span<const LinearExpr> exprs);
794 
796  Constraint AddDivisionEquality(IntVar target, IntVar numerator,
797  IntVar denominator);
798 
800  Constraint AddAbsEquality(IntVar target, IntVar var);
801 
803  Constraint AddModuloEquality(IntVar target, IntVar var, IntVar mod);
804 
806  Constraint AddProductEquality(IntVar target, absl::Span<const IntVar> vars);
807 
812  Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
813 
818 
825 
827  void Minimize(const LinearExpr& expr);
828 
830  void Maximize(const LinearExpr& expr);
831 
839  void ScaleObjectiveBy(double scaling);
840 
842  void AddDecisionStrategy(
843  absl::Span<const IntVar> variables,
846 
848  void AddDecisionStrategy(
849  absl::Span<const BoolVar> variables,
852 
854  void AddHint(IntVar var, int64 value);
855 
856  // TODO(user) : add MapDomain?
857 
858  const CpModelProto& Build() const { return Proto(); }
859 
860  const CpModelProto& Proto() const { return cp_model_; }
861  CpModelProto* MutableProto() { return &cp_model_; }
862 
863  private:
864  friend class CumulativeConstraint;
865  friend class ReservoirConstraint;
866 
867  // Fills the 'expr_proto' with the linear expression represented by 'expr'.
868  void LinearExprToProto(const LinearExpr& expr,
869  LinearExpressionProto* expr_proto);
870 
871  // Returns a (cached) integer variable index with a constant value.
872  int IndexFromConstant(int64 value);
873 
874  // Returns a valid integer index from a BoolVar index.
875  // If the input index is a positive, it returns this index.
876  // If the input index is negative, it creates a cached IntVar equal to
877  // 1 - BoolVar(PositiveRef(index)), and returns the index of this new
878  // variable.
879  int GetOrCreateIntegerIndex(int index);
880 
881  void FillLinearTerms(const LinearExpr& left, const LinearExpr& right,
882  LinearConstraintProto* proto);
883 
884  CpModelProto cp_model_;
885  absl::flat_hash_map<int64, int> constant_to_index_map_;
886  absl::flat_hash_map<int, int> bool_to_integer_index_map_;
887 };
888 
890 int64 SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
891 
893 int64 SolutionIntegerMin(const CpSolverResponse& r, IntVar x);
894 
896 int64 SolutionIntegerMax(const CpSolverResponse& r, IntVar x);
897 
900 
901 } // namespace sat
902 } // namespace operations_research
903 
904 #endif // OR_TOOLS_SAT_CP_MODEL_H_
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:93
const ::operations_research::sat::IntegerVariableProto & variables(int index) const
Definition: cp_model.pb.h:9824
bool operator!=(const IntVar &other) const
Difference test with anpther IntVar.
Definition: cp_model.h:168
const std::string & Name() const
Returns the name of the variable (or the empty string if not set).
Definition: cp_model.h:156
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
ConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:430
LinearExpr()
Definition: cp_model.pb.h:1542
BoolVar WithName(const std::string &name)
Sets the name of the variable.
friend int64 SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
void AddDemand(IntervalVar interval, IntVar demand)
Adds a pair (interval, demand) to the constraint.
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
std::string DebugString() const
Debug string.
BoolVar NewBoolVar()
Creates a Boolean variable.
Definition: cp_model.pb.h:712
An integer variable.
Definition: cp_model.h:145
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:181
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition: cp_model.h:353
Constraint AddLinMinEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == min(exprs).
static LinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const int64 > coeffs)
Constructs the scalar product of variables and coefficients.
BoolVar PresenceBoolVar() const
Returns a BoolVar indicating the presence of this interval.
DecisionStrategyProto_DomainReductionStrategy
Definition: cp_model.pb.h:199
bool operator!=(const BoolVar &other) const
Dis-Equality test.
Definition: cp_model.h:85
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
BoolVar Not(BoolVar x)
A convenient wrapper so we can write Not(x) instead of x.Not() which is sometimes clearer.
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
Constraint(ConstraintProto *proto)
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
const std::string & Name() const
Returns the name of the variable.
Definition: cp_model.h:74
Specialized circuit constraint.
Definition: cp_model.h:445
void AddEvent(IntVar time, int64 demand)
Adds a mandatory event.
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
The constraint will be enforced iff all literals listed here are true.
Constraint AddProductEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == prod(vars).
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:427
Specialized circuit constraint.
Definition: cp_model.h:468
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Constraint AddBoolXor(absl::Span< const BoolVar > literals)
Adds the constraint that a odd number of literal is true.
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
friend int64 SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
Definition: cp_model.h:52
LinearExpr & AddConstant(int64 value)
Adds a constant value to the linear expression.
IntVar()
void AddOptionalEvent(IntVar time, int64 demand, BoolVar is_active)
Adds a optional event.
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:108
LinearExpr AddConstant(int64 value) const
Adds a constant value to an integer variable and returns a linear expression.
const std::vector< int64 > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:288
We call domain any subset of Int64 = [kint64min, kint64max].
const std::vector< IntVar > & variables() const
Returns the vector of variables.
Definition: cp_model.h:285
static LinearExpr Term(IntVar var, int64 coefficient)
Construncts var * coefficient.
Constraint AddAbsEquality(IntVar target, IntVar var)
Adds target == abs(var).
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
Definition: cp_model.h:627
::operations_research::sat::IntegerVariableProto * mutable_variables(int index)
Definition: cp_model.pb.h:9812
A Boolean variable.
Definition: cp_model.h:66
const CpModelProto & Build() const
Definition: cp_model.h:858
ConstraintProto * proto_
Definition: cp_model.h:437
int64 SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
int index() const
Returns the index of the interval constraint in the model.
Definition: cp_model.h:371
Specialized reservoir constraint.
Definition: cp_model.h:508
static LinearExpr BooleanScalProd(absl::Span< const BoolVar > vars, absl::Span< const int64 > coeffs)
Constructs the scalar product of Booleans and coefficients.
Definition: cp_model.pb.h:3589
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
An automaton constraint/.
BoolVar()
IntVar EndVar() const
Returns the end variable.
CpModelProto * MutableProto()
Definition: cp_model.h:861
const ::operations_research::sat::ConstraintProto & constraints(int index) const
Definition: cp_model.pb.h:9863
std::string DebugString() const
Returns a debug string.
DecisionStrategyProto_VariableSelectionStrategy
Definition: cp_model.pb.h:171
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
void AddHint(IntVar var, int64 value)
Adds hinting to a variable.
Definition: cp_model.pb.h:5027
A constraint.
Definition: cp_model.h:396
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Constraint AddModuloEquality(IntVar target, IntVar var, IntVar mod)
Adds target = var % mod.
void AddTransition(int tail, int head, int64 transition_label)
Adds a transitions to the automaton.
const ::operations_research::sat::IntervalConstraintProto & interval() const
Definition: cp_model.pb.h:9084
Constraint AddMinEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == min(vars).
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
int64 SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
Specialized automaton constraint.
Definition: cp_model.h:539
Constraint AddElement(IntVar index, absl::Span< const int64 > values, IntVar target)
Adds the element constraint: values[index] == target.
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:186
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
IntervalVar()
Default ctor.
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
CumulativeConstraint AddCumulative(IntVar capacity)
The cumulative constraint.
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
Definition: cp_model.h:77
::operations_research::sat::ConstraintProto * mutable_constraints(int index)
Definition: cp_model.pb.h:9851
bool operator==(const BoolVar &other) const
Equality test with another boolvar.
Definition: cp_model.h:80
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
An inverse constraint.
Definition: cp_model.pb.h:257
Specialized cumulative constraint.
Definition: cp_model.h:573
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
std::string Name() const
Returns the name of the interval (or the empty string if not set).
Definition: cp_model.pb.h:5281
Constraint AddLinMaxEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == max(exprs).
const std::string & name() const
Definition: cp_model.pb.h:5692
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.
BoolVar FalseVar()
Creates an always false Boolean variable.
Wrapper class around the cp_model proto.
Definition: cp_model.h:593
IntVar SizeVar() const
Returns the size variable.
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Adds an forbidden assignments constraint.
Constraint AddDivisionEquality(IntVar target, IntVar numerator, IntVar denominator)
Adds target = num / denom (integer division rounded towards 0).
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Constraint AddNoOverlap(absl::Span< const IntervalVar > vars)
Adds a no-overlap constraint that ensures that all present intervals do not overlap in time.
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
const CpModelProto & Proto() const
Definition: cp_model.h:860
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.
const IntervalConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:361
Constraint WithName(const std::string &name)
Sets the name of the constraint.
Represents a Interval variable.
Definition: cp_model.h:320
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:176
IntVar WithName(const std::string &name)
Sets the name of the variable.
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
this constraint forces all variables to have different values.
NoOverlap2DConstraint AddNoOverlap2D()
The no_overlap_2d constraint prevents a set of boxes from overlapping.
::operations_research::sat::IntervalConstraintProto * mutable_interval()
Definition: cp_model.pb.h:9097
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
IntVar NewConstant(int64 value)
Creates a constant variable.
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:98
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Adds an allowed assignments constraint.
void AddTuple(absl::Span< const int64 > tuple)
Adds a tuple of possible values to the constraint.
A dedicated container for linear expressions.
Definition: cp_model.h:242
void ScaleObjectiveBy(double scaling)
Sets scaling of the objective.
int64 constant() const
Returns the constant term.
Definition: cp_model.h:291
void AddVar(IntVar var)
Adds a single integer variable to the linear expression.
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
BoolVar TrueVar()
Creates an always true Boolean variable.
void AddTerm(IntVar var, int64 coeff)
Adds a term (var * coeff) to the linear expression.
IntVar StartVar() const
Returns the start variable.
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
Definition: cp_model.h:348
std::string DebugString() const
Returns a debug string.
Specialized no_overlap2D constraint.
Definition: cp_model.h:556
bool operator==(const IntVar &other) const
Equality test with another IntVar.
Definition: cp_model.h:163
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
Constraint AddMaxEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == max(vars).
IntervalVar NewOptionalIntervalVar(IntVar start, IntVar size, IntVar end, BoolVar presence)
Creates an optional interval variable.
Specialized assignment constraint.
Definition: cp_model.h:491
ReservoirConstraint AddReservoirConstraint(int64 min_level, int64 max_level)
Adds a reservoir constraint with optional refill/emptying events.
IntervalConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:366
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.pb.h:1186
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
IntervalVar NewIntervalVar(IntVar start, IntVar size, IntVar end)
Creates an interval variable.
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Constructs the sum of a list of Booleans.
int64 SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
friend int64 SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.