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;
116  friend class LinearExpr;
117  friend class ReservoirConstraint;
118  friend bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
119 
120  BoolVar(int index, CpModelProto* cp_model);
121 
122  CpModelProto* cp_model_ = nullptr;
123  int index_ = kint32min;
124 };
125 
126 std::ostream& operator<<(std::ostream& os, const BoolVar& var);
127 
132 BoolVar Not(BoolVar x);
133 
144 class IntVar {
145  public:
146  IntVar();
147 
149  IntVar(const BoolVar& var); // NOLINT(runtime/explicit)
150 
152  IntVar WithName(const std::string& name);
153 
155  const std::string& Name() const { return Proto().name(); }
156 
158  bool operator==(const IntVar& other) const {
159  return other.cp_model_ == cp_model_ && other.index_ == index_;
160  }
161 
163  bool operator!=(const IntVar& other) const {
164  return other.cp_model_ != cp_model_ || other.index_ != index_;
165  }
166 
168  std::string DebugString() const;
169 
171  const IntegerVariableProto& Proto() const {
172  return cp_model_->variables(index_);
173  }
174 
177  return cp_model_->mutable_variables(index_);
178  }
179 
181  int index() const { return index_; }
182 
183  private:
184  friend class CpModelBuilder;
185  friend class CumulativeConstraint;
186  friend class LinearExpr;
187  friend class IntervalVar;
188  friend class ReservoirConstraint;
189  friend int64 SolutionIntegerValue(const CpSolverResponse& r,
190  const LinearExpr& expr);
191  friend int64 SolutionIntegerMin(const CpSolverResponse& r, IntVar x);
192  friend int64 SolutionIntegerMax(const CpSolverResponse& r, IntVar x);
193 
194  IntVar(int index, CpModelProto* cp_model);
195 
196  CpModelProto* cp_model_ = nullptr;
197  int index_ = kint32min;
198 };
199 
200 std::ostream& operator<<(std::ostream& os, const IntVar& var);
201 
237 class LinearExpr {
238  public:
239  LinearExpr();
240 
246  LinearExpr(BoolVar var); // NOLINT(runtime/explicit)
247 
249  LinearExpr(IntVar var); // NOLINT(runtime/explicit)
250 
252  LinearExpr(int64 constant); // NOLINT(runtime/explicit)
253 
255  LinearExpr& AddConstant(int64 value);
256 
258  void AddVar(IntVar var);
259 
261  void AddTerm(IntVar var, int64 coeff);
262 
264  static LinearExpr Sum(absl::Span<const IntVar> vars);
265 
267  static LinearExpr ScalProd(absl::Span<const IntVar> vars,
268  absl::Span<const int64> coeffs);
269 
271  static LinearExpr BooleanSum(absl::Span<const BoolVar> vars);
272 
274  static LinearExpr BooleanScalProd(absl::Span<const BoolVar> vars,
275  absl::Span<const int64> coeffs);
277  static LinearExpr Term(IntVar var, int64 coefficient);
278 
280  const std::vector<IntVar>& variables() const { return variables_; }
281 
283  const std::vector<int64>& coefficients() const { return coefficients_; }
284 
286  int64 constant() const { return constant_; }
287 
288  // TODO(user): LinearExpr.DebugString() and operator<<.
289 
290  private:
291  std::vector<IntVar> variables_;
292  std::vector<int64> coefficients_;
293  int64 constant_ = 0;
294 };
295 
315 class IntervalVar {
316  public:
318  IntervalVar();
319 
321  IntervalVar WithName(const std::string& name);
322 
324  std::string Name() const;
325 
327  IntVar StartVar() const;
328 
330  IntVar SizeVar() const;
331 
333  IntVar EndVar() const;
334 
340  BoolVar PresenceBoolVar() const;
341 
343  bool operator==(const IntervalVar& other) const {
344  return other.cp_model_ == cp_model_ && other.index_ == index_;
345  }
346 
348  bool operator!=(const IntervalVar& other) const {
349  return other.cp_model_ != cp_model_ || other.index_ != index_;
350  }
351 
353  std::string DebugString() const;
354 
357  return cp_model_->constraints(index_).interval();
358  }
359 
362  return cp_model_->mutable_constraints(index_)->mutable_interval();
363  }
364 
366  int index() const { return index_; }
367 
368  private:
369  friend class CpModelBuilder;
370  friend class CumulativeConstraint;
371  friend class NoOverlap2DConstraint;
372  friend std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
373 
374  IntervalVar(int index, CpModelProto* cp_model);
375 
376  CpModelProto* cp_model_ = nullptr;
377  int index_ = kint32min;
378 };
379 
380 std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
381 
391 class Constraint {
392  public:
410  Constraint OnlyEnforceIf(absl::Span<const BoolVar> literals);
411 
414 
416  Constraint WithName(const std::string& name);
417 
419  const std::string& Name() const;
420 
422  const ConstraintProto& Proto() const { return *proto_; }
423 
425  ConstraintProto* MutableProto() const { return proto_; }
426 
427  protected:
428  friend class CpModelBuilder;
429 
430  explicit Constraint(ConstraintProto* proto);
431 
433 };
434 
441  public:
449  void AddArc(int tail, int head, BoolVar literal);
450 
451  private:
452  friend class CpModelBuilder;
453 
455 };
456 
463 class TableConstraint : public Constraint {
464  public:
466  void AddTuple(absl::Span<const int64> tuple);
467 
468  private:
469  friend class CpModelBuilder;
470 
472 };
473 
481  public:
487  void AddEvent(IntVar time, int64 demand);
488 
495  void AddOptionalEvent(IntVar time, int64 demand, BoolVar is_active);
496 
497  private:
498  friend class CpModelBuilder;
499 
501 
502  CpModelBuilder* builder_;
503 };
504 
512  public:
514  void AddTransition(int tail, int head, int64 transition_label);
515 
516  private:
517  friend class CpModelBuilder;
518 
520 };
521 
529  public:
531  void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate);
532 
533  private:
534  friend class CpModelBuilder;
535 
537 };
538 
546  public:
548  void AddDemand(IntervalVar interval, IntVar demand);
549 
550  private:
551  friend class CpModelBuilder;
552 
554 
555  CpModelBuilder* builder_;
556 };
557 
566  public:
568  IntVar NewIntVar(const Domain& domain);
569 
572 
574  IntVar NewConstant(int64 value);
575 
577  BoolVar TrueVar();
578 
580  BoolVar FalseVar();
581 
583  IntervalVar NewIntervalVar(IntVar start, IntVar size, IntVar end);
584 
587  BoolVar presence);
588 
590  Constraint AddBoolOr(absl::Span<const BoolVar> literals);
591 
593  Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
594 
596  Constraint AddBoolXor(absl::Span<const BoolVar> literals);
597 
600  return AddBoolOr({a.Not(), b});
601  }
602 
604  Constraint AddEquality(const LinearExpr& left, const LinearExpr& right);
605 
607  Constraint AddGreaterOrEqual(const LinearExpr& left, const LinearExpr& right);
608 
610  Constraint AddGreaterThan(const LinearExpr& left, const LinearExpr& right);
611 
613  Constraint AddLessOrEqual(const LinearExpr& left, const LinearExpr& right);
614 
616  Constraint AddLessThan(const LinearExpr& left, const LinearExpr& right);
617 
619  Constraint AddLinearConstraint(const LinearExpr& expr, const Domain& domain);
620 
622  Constraint AddNotEqual(const LinearExpr& left, const LinearExpr& right);
623 
625  Constraint AddAllDifferent(absl::Span<const IntVar> vars);
626 
629  absl::Span<const IntVar> variables,
630  IntVar target);
631 
633  Constraint AddElement(IntVar index, absl::Span<const int64> values,
634  IntVar target);
635 
652 
664  TableConstraint AddAllowedAssignments(absl::Span<const IntVar> vars);
665 
676  TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> vars);
677 
683  Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
684  absl::Span<const IntVar> inverse_variables);
685 
704  ReservoirConstraint AddReservoirConstraint(int64 min_level, int64 max_level);
705 
734  absl::Span<const IntVar> transition_variables, int starting_state,
735  absl::Span<const int> final_states);
736 
738  Constraint AddMinEquality(IntVar target, absl::Span<const IntVar> vars);
739 
741  Constraint AddMaxEquality(IntVar target, absl::Span<const IntVar> vars);
742 
744  Constraint AddDivisionEquality(IntVar target, IntVar numerator,
745  IntVar denominator);
746 
748  Constraint AddAbsEquality(IntVar target, IntVar var);
749 
751  Constraint AddModuloEquality(IntVar target, IntVar var, IntVar mod);
752 
754  Constraint AddProductEquality(IntVar target, absl::Span<const IntVar> vars);
755 
760  Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
761 
766 
773 
775  void Minimize(const LinearExpr& expr);
776 
778  void Maximize(const LinearExpr& expr);
779 
787  void ScaleObjectiveBy(double scaling);
788 
790  void AddDecisionStrategy(
791  absl::Span<const IntVar> variables,
794 
796  void AddDecisionStrategy(
797  absl::Span<const BoolVar> variables,
800 
802  void AddHint(IntVar var, int64 value);
803 
804  // TODO(user) : add MapDomain?
805 
806  const CpModelProto& Build() const { return Proto(); }
807 
808  const CpModelProto& Proto() const { return cp_model_; }
809  CpModelProto* MutableProto() { return &cp_model_; }
810 
811  private:
812  friend class CumulativeConstraint;
813  friend class ReservoirConstraint;
814 
815  // Returns a (cached) integer variable index with a constant value.
816  int IndexFromConstant(int64 value);
817 
818  // Returns a valid integer index from a BoolVar index.
819  // If the input index is a positive, it returns this index.
820  // If the input index is negative, it creates a cached IntVar equal to
821  // 1 - BoolVar(PositiveRef(index)), and returns the index of this new
822  // variable.
823  int GetOrCreateIntegerIndex(int index);
824 
825  void FillLinearTerms(const LinearExpr& left, const LinearExpr& right,
826  LinearConstraintProto* proto);
827 
828  CpModelProto cp_model_;
829  absl::flat_hash_map<int64, int> constant_to_index_map_;
830  absl::flat_hash_map<int, int> bool_to_integer_index_map_;
831 };
832 
834 int64 SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
835 
837 int64 SolutionIntegerMin(const CpSolverResponse& r, IntVar x);
838 
840 int64 SolutionIntegerMax(const CpSolverResponse& r, IntVar x);
841 
844 
845 } // namespace sat
846 } // namespace operations_research
847 
848 #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:9075
bool operator!=(const IntVar &other) const
Difference test with anpther IntVar.
Definition: cp_model.h:163
const std::string & Name() const
Returns the name of the variable (or the empty std::string if not set).
Definition: cp_model.h:155
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:425
LinearExpr()
Definition: cp_model.pb.h:1202
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 std::string.
BoolVar NewBoolVar()
Creates a Boolean variable.
An integer variable.
Definition: cp_model.h:144
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:176
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition: cp_model.h:348
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:191
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:440
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:422
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
const std::vector< int64 > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:283
We call domain any subset of Int64 = [kint64min, kint64max].
const std::vector< IntVar > & variables() const
Returns the vector of variables.
Definition: cp_model.h:280
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:599
::operations_research::sat::IntegerVariableProto * mutable_variables(int index)
Definition: cp_model.pb.h:9063
A Boolean variable.
Definition: cp_model.h:66
const CpModelProto & Build() const
Definition: cp_model.h:806
ConstraintProto * proto_
Definition: cp_model.h:432
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:366
Specialized reservoir constraint.
Definition: cp_model.h:480
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:3249
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:809
const ::operations_research::sat::ConstraintProto & constraints(int index) const
Definition: cp_model.pb.h:9114
std::string DebugString() const
Returns a debug std::string.
DecisionStrategyProto_VariableSelectionStrategy
Definition: cp_model.pb.h:163
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:4649
A constraint.
Definition: cp_model.h:391
const std::string & Name() const
Returns the name of the constraint (or the empty std::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:8335
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:511
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:181
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:9102
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:249
Specialized cumulative constraint.
Definition: cp_model.h:545
std::string Name() const
Returns the name of the interval (or the empty std::string if not set).
Definition: cp_model.pb.h:4878
const std::string & name() const
Definition: cp_model.pb.h:5264
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:565
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:808
const IntervalConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:356
Constraint WithName(const std::string &name)
Sets the name of the constraint.
Represents a Interval variable.
Definition: cp_model.h:315
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:171
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:8348
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:237
void ScaleObjectiveBy(double scaling)
Sets scaling of the objective.
int64 constant() const
Returns the constant term.
Definition: cp_model.h:286
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:343
std::string DebugString() const
Returns a debug std::string.
Specialized no_overlap2D constraint.
Definition: cp_model.h:528
bool operator==(const IntVar &other) const
Equality test with another IntVar.
Definition: cp_model.h:158
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:463
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:361
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.pb.h:846
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.