C++ Reference

C++ Reference: CP-SAT

cp_model.h
Go to the documentation of this file.
1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
38 #ifndef OR_TOOLS_SAT_CP_MODEL_H_
39 #define OR_TOOLS_SAT_CP_MODEL_H_
40 
41 #include <cstdint>
42 #include <limits>
43 #include <string>
44 
45 #include "absl/container/flat_hash_map.h"
46 #include "absl/types/span.h"
49 #include "ortools/sat/cp_model_utils.h"
50 #include "ortools/sat/model.h"
53 
55 namespace sat {
56 
57 class CpModelBuilder;
58 class LinearExpr;
59 class IntVar;
60 
69 class BoolVar {
70  public:
71  BoolVar();
72 
74  BoolVar WithName(const std::string& name);
75 
77  const std::string& Name() const { return Proto().name(); }
78 
80  BoolVar Not() const { return BoolVar(NegatedRef(index_), cp_model_); }
81 
83  bool operator==(const BoolVar& other) const {
84  return other.cp_model_ == cp_model_ && other.index_ == index_;
85  }
86 
88  bool operator!=(const BoolVar& other) const {
89  return other.cp_model_ != cp_model_ || other.index_ != index_;
90  }
91 
93  std::string DebugString() const;
94 
96  const IntegerVariableProto& Proto() const {
97  return cp_model_->variables(index_);
98  }
99 
102  return cp_model_->mutable_variables(index_);
103  }
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 IntVar;
118  friend class IntervalVar;
120  friend class LinearExpr;
121  friend class ReservoirConstraint;
122  friend bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
123 
124  BoolVar(int index, CpModelProto* cp_model);
125 
126  CpModelProto* cp_model_ = nullptr;
127  int index_ = std::numeric_limits<int32_t>::min();
128 };
129 
130 std::ostream& operator<<(std::ostream& os, const BoolVar& var);
131 
136 BoolVar Not(BoolVar x);
137 
148 class IntVar {
149  public:
150  IntVar();
151 
153  IntVar(const BoolVar& var); // NOLINT(runtime/explicit)
154 
157  BoolVar ToBoolVar() const;
158 
160  IntVar WithName(const std::string& name);
161 
163  const std::string& Name() const { return Proto().name(); }
164 
167  LinearExpr AddConstant(int64_t value) const;
168 
170  bool operator==(const IntVar& other) const {
171  return other.cp_model_ == cp_model_ && other.index_ == index_;
172  }
173 
175  bool operator!=(const IntVar& other) const {
176  return other.cp_model_ != cp_model_ || other.index_ != index_;
177  }
178 
180  std::string DebugString() const;
181 
183  const IntegerVariableProto& Proto() const {
184  return cp_model_->variables(index_);
185  }
186 
189  return cp_model_->mutable_variables(index_);
190  }
191 
193  int index() const { return index_; }
194 
195  private:
196  friend class BoolVar;
197  friend class CpModelBuilder;
198  friend class CumulativeConstraint;
199  friend class LinearExpr;
200  friend class IntervalVar;
201  friend class ReservoirConstraint;
202  friend int64_t SolutionIntegerValue(const CpSolverResponse& r,
203  const LinearExpr& expr);
204  friend int64_t SolutionIntegerMin(const CpSolverResponse& r, IntVar x);
205  friend int64_t SolutionIntegerMax(const CpSolverResponse& r, IntVar x);
206 
207  IntVar(int index, CpModelProto* cp_model);
208 
209  CpModelProto* cp_model_ = nullptr;
210  int index_ = std::numeric_limits<int32_t>::min();
211 };
212 
213 std::ostream& operator<<(std::ostream& os, const IntVar& var);
214 
250 class LinearExpr {
251  public:
252  LinearExpr();
253 
259  LinearExpr(BoolVar var); // NOLINT(runtime/explicit)
260 
262  LinearExpr(IntVar var); // NOLINT(runtime/explicit)
263 
265  LinearExpr(int64_t constant); // NOLINT(runtime/explicit)
266 
268  LinearExpr& AddConstant(int64_t value);
269 
271  LinearExpr& AddVar(IntVar var);
272 
274  LinearExpr& AddTerm(IntVar var, int64_t coeff);
275 
277  LinearExpr& AddExpression(const LinearExpr& expr);
278 
280  static LinearExpr Sum(absl::Span<const IntVar> vars);
281 
283  static LinearExpr ScalProd(absl::Span<const IntVar> vars,
284  absl::Span<const int64_t> coeffs);
285 
287  static LinearExpr BooleanSum(absl::Span<const BoolVar> vars);
288 
290  static LinearExpr BooleanScalProd(absl::Span<const BoolVar> vars,
291  absl::Span<const int64_t> coeffs);
293  static LinearExpr Term(IntVar var, int64_t coefficient);
294 
296  const std::vector<IntVar>& variables() const { return variables_; }
297 
299  const std::vector<int64_t>& coefficients() const { return coefficients_; }
300 
302  int64_t constant() const { return constant_; }
303 
305  IntVar Var() const;
306 
308  int64_t Value() const;
309 
311  std::string DebugString() const;
312 
313  private:
314  std::vector<IntVar> variables_;
315  std::vector<int64_t> coefficients_;
316  int64_t constant_ = 0;
317 };
318 
319 std::ostream& operator<<(std::ostream& os, const LinearExpr& e);
320 
341 class IntervalVar {
342  public:
344  IntervalVar();
345 
347  IntervalVar WithName(const std::string& name);
348 
350  std::string Name() const;
351 
354  LinearExpr StartExpr() const;
355 
358  LinearExpr SizeExpr() const;
359 
362  LinearExpr EndExpr() const;
363 
369  BoolVar PresenceBoolVar() const;
370 
372  bool operator==(const IntervalVar& other) const {
373  return other.cp_model_ == cp_model_ && other.index_ == index_;
374  }
375 
377  bool operator!=(const IntervalVar& other) const {
378  return other.cp_model_ != cp_model_ || other.index_ != index_;
379  }
380 
382  std::string DebugString() const;
383 
386  return cp_model_->constraints(index_).interval();
387  }
388 
391  return cp_model_->mutable_constraints(index_)->mutable_interval();
392  }
393 
395  int index() const { return index_; }
396 
397  private:
398  friend class CpModelBuilder;
399  friend class CumulativeConstraint;
400  friend class NoOverlap2DConstraint;
401  friend std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
402 
403  IntervalVar(int index, CpModelProto* cp_model);
404 
405  CpModelProto* cp_model_ = nullptr;
406  int index_ = std::numeric_limits<int32_t>::min();
407 };
408 
409 std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
410 
420 class Constraint {
421  public:
439  Constraint OnlyEnforceIf(absl::Span<const BoolVar> literals);
440 
443 
445  Constraint WithName(const std::string& name);
446 
448  const std::string& Name() const;
449 
451  const ConstraintProto& Proto() const { return *proto_; }
452 
454  ConstraintProto* MutableProto() const { return proto_; }
455 
456  protected:
457  friend class CpModelBuilder;
458 
459  explicit Constraint(ConstraintProto* proto);
460 
462 };
463 
470  public:
478  void AddArc(int tail, int head, BoolVar literal);
479 
480  private:
481  friend class CpModelBuilder;
482 
484 };
485 
493  public:
501  void AddArc(int tail, int head, BoolVar literal);
502 
503  private:
504  friend class CpModelBuilder;
505 
507 };
508 
515 class TableConstraint : public Constraint {
516  public:
518  void AddTuple(absl::Span<const int64_t> tuple);
519 
520  private:
521  friend class CpModelBuilder;
522 
524 };
525 
533  public:
539  void AddEvent(IntVar time, int64_t demand);
540 
547  void AddOptionalEvent(IntVar time, int64_t demand, BoolVar is_active);
548 
549  private:
550  friend class CpModelBuilder;
551 
553 
554  CpModelBuilder* builder_;
555 };
556 
564  public:
566  void AddTransition(int tail, int head, int64_t transition_label);
567 
568  private:
569  friend class CpModelBuilder;
570 
572 };
573 
581  public:
583  void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate);
584 
585  private:
586  friend class CpModelBuilder;
587 
589 };
590 
598  public:
600  void AddDemand(IntervalVar interval, IntVar demand);
601 
602  private:
603  friend class CpModelBuilder;
604 
606 
607  CpModelBuilder* builder_;
608 };
609 
618  public:
620  IntVar NewIntVar(const Domain& domain);
621 
624 
626  IntVar NewConstant(int64_t value);
627 
631  BoolVar TrueVar();
632 
636  BoolVar FalseVar();
637 
639  IntervalVar NewIntervalVar(const LinearExpr& start, const LinearExpr& size,
640  const LinearExpr& end);
641 
643  IntervalVar NewFixedSizeIntervalVar(const LinearExpr& start, int64_t size);
644 
648  const LinearExpr& size,
649  const LinearExpr& end, BoolVar presence);
650 
653  int64_t size, BoolVar presence);
654 
656  Constraint AddBoolOr(absl::Span<const BoolVar> literals);
657 
659  Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
660 
662  Constraint AddBoolXor(absl::Span<const BoolVar> literals);
663 
666  return AddBoolOr({a.Not(), b});
667  }
668 
670  Constraint AddEquality(const LinearExpr& left, const LinearExpr& right);
671 
673  Constraint AddGreaterOrEqual(const LinearExpr& left, const LinearExpr& right);
674 
676  Constraint AddGreaterThan(const LinearExpr& left, const LinearExpr& right);
677 
679  Constraint AddLessOrEqual(const LinearExpr& left, const LinearExpr& right);
680 
682  Constraint AddLessThan(const LinearExpr& left, const LinearExpr& right);
683 
685  Constraint AddLinearConstraint(const LinearExpr& expr, const Domain& domain);
686 
688  Constraint AddNotEqual(const LinearExpr& left, const LinearExpr& right);
689 
691  Constraint AddAllDifferent(absl::Span<const IntVar> vars);
692 
695  absl::Span<const IntVar> variables,
696  IntVar target);
697 
699  Constraint AddElement(IntVar index, absl::Span<const int64_t> values,
700  IntVar target);
701 
719 
734 
746  TableConstraint AddAllowedAssignments(absl::Span<const IntVar> vars);
747 
758  TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> vars);
759 
765  Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
766  absl::Span<const IntVar> inverse_variables);
767 
787  int64_t max_level);
788 
817  absl::Span<const IntVar> transition_variables, int starting_state,
818  absl::Span<const int> final_states);
819 
821  Constraint AddMinEquality(IntVar target, absl::Span<const IntVar> vars);
822 
825  absl::Span<const LinearExpr> exprs);
826 
828  Constraint AddMaxEquality(IntVar target, absl::Span<const IntVar> vars);
829 
832  absl::Span<const LinearExpr> exprs);
833 
835  Constraint AddDivisionEquality(IntVar target, IntVar numerator,
836  IntVar denominator);
837 
839  Constraint AddAbsEquality(IntVar target, IntVar var);
840 
842  Constraint AddModuloEquality(IntVar target, IntVar var, IntVar mod);
843 
845  Constraint AddProductEquality(IntVar target, absl::Span<const IntVar> vars);
846 
851  Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
852 
857 
864 
866  void Minimize(const LinearExpr& expr);
867 
869  void Maximize(const LinearExpr& expr);
870 
878  void ScaleObjectiveBy(double scaling);
879 
881  void AddDecisionStrategy(
882  absl::Span<const IntVar> variables,
885 
887  void AddDecisionStrategy(
888  absl::Span<const BoolVar> variables,
891 
893  void AddHint(IntVar var, int64_t value);
894 
896  void ClearHints();
897 
899  void AddAssumption(BoolVar lit);
900 
902  void AddAssumptions(absl::Span<const BoolVar> literals);
903 
905  void ClearAssumptions();
906 
907  // TODO(user) : add MapDomain?
908 
909  const CpModelProto& Build() const { return Proto(); }
910 
911  const CpModelProto& Proto() const { return cp_model_; }
912  CpModelProto* MutableProto() { return &cp_model_; }
913 
915  void CopyFrom(const CpModelProto& model_proto);
916 
919 
921  IntVar GetIntVarFromProtoIndex(int index);
922 
925 
926  private:
927  friend class CumulativeConstraint;
928  friend class ReservoirConstraint;
929  friend class IntervalVar;
930 
931  // Fills the 'expr_proto' with the linear expression represented by 'expr'.
932  void LinearExprToProto(const LinearExpr& expr,
933  LinearExpressionProto* expr_proto);
934 
935  // Rebuilds a LinearExpr from a LinearExpressionProto.
936  // This method is a member of CpModelBuilder because it needs to be friend
937  // with IntVar.
938  static LinearExpr LinearExprFromProto(const LinearExpressionProto& expr_proto,
939  CpModelProto* model_proto_);
940 
941  // Returns a (cached) integer variable index with a constant value.
942  int IndexFromConstant(int64_t value);
943 
944  // Returns a valid integer index from a BoolVar index.
945  // If the input index is a positive, it returns this index.
946  // If the input index is negative, it creates a cached IntVar equal to
947  // 1 - BoolVar(PositiveRef(index)), and returns the index of this new
948  // variable.
949  int GetOrCreateIntegerIndex(int index);
950 
951  void FillLinearTerms(const LinearExpr& left, const LinearExpr& right,
952  LinearConstraintProto* proto);
953 
954  CpModelProto cp_model_;
955  absl::flat_hash_map<int64_t, int> constant_to_index_map_;
956  absl::flat_hash_map<int, int> bool_to_integer_index_map_;
957 };
958 
960 int64_t SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
961 
963 int64_t SolutionIntegerMin(const CpSolverResponse& r, IntVar x);
964 
966 int64_t SolutionIntegerMax(const CpSolverResponse& r, IntVar x);
967 
970 
971 } // namespace sat
972 } // namespace operations_research
973 
974 #endif // OR_TOOLS_SAT_CP_MODEL_H_
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
friend int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
BoolVar FalseVar()
Creates an always false Boolean variable.
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
Constraint AddModuloEquality(IntVar target, IntVar var, IntVar mod)
Adds target = var % mod.
Specialized reservoir constraint.
Definition: cp_model.h:532
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
LinearExpr EndExpr() const
Returns the end linear expression.
BoolVar TrueVar()
Creates an always true Boolean variable.
Constraint AddLinMaxEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == max(exprs).
const CpModelProto & Proto() const
Definition: cp_model.h:911
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
std::string DebugString() const
Debug string.
Constraint AddAbsEquality(IntVar target, IntVar var)
Adds target == abs(var).
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
const std::string & Name() const
Returns the name of the variable.
Definition: cp_model.h:77
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
Specialized no_overlap2D constraint.
Definition: cp_model.h:580
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
Definition: cp_model.h:80
int64_t constant() const
Returns the constant term.
Definition: cp_model.h:302
int64_t SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
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.
LinearExpr AddConstant(int64_t value) const
Adds a constant value to an integer variable and returns a linear expression.
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
this constraint forces all variables to have different values.
Constraint AddEquality(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:454
bool operator==(const BoolVar &other) const
Equality test with another boolvar.
Definition: cp_model.h:83
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Adds an allowed assignments constraint.
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
int index() const
Returns the index of the interval constraint in the model.
Definition: cp_model.h:395
ReservoirConstraint AddReservoirConstraint(int64_t min_level, int64_t max_level)
Adds a reservoir constraint with optional refill/emptying events.
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
friend int64_t SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
friend int64_t SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
Definition: cp_model.h:372
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
LinearExpr & AddExpression(const LinearExpr &expr)
Adds another linear expression to the linear expression.
Constraint AddProductEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == prod(vars).
IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
Creates an interval variable with a fixed size.
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
LinearExpr & AddConstant(int64_t value)
Adds a constant value to the linear expression.
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
An automaton constraint/.
Specialized circuit constraint.
Definition: cp_model.h:469
Constraint AddBoolXor(absl::Span< const BoolVar > literals)
Adds the constraint that a odd number of literal is true.
Specialized cumulative constraint.
Definition: cp_model.h:597
bool operator!=(const IntVar &other) const
Difference test with anpther IntVar.
Definition: cp_model.h:175
Specialized assignment constraint.
Definition: cp_model.h:515
void AddDemand(IntervalVar interval, IntVar demand)
Adds a pair (interval, demand) to the constraint.
An integer variable.
Definition: cp_model.h:148
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:96
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:299
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:188
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition: cp_model.h:377
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
int64_t SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
IntVar NewConstant(int64_t value)
Creates a constant variable.
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
Creates an interval variable from 3 affine expressions.
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
void ClearAssumptions()
Remove all assumptions from the model.
Constraint AddMaxEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == max(vars).
IntVar Var() const
Checks that the expression is 1 * var + 0, and returns var.
A Boolean variable.
Definition: cp_model.h:69
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:451
const ::operations_research::sat::IntegerVariableProto & variables(int index) const
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
An inverse constraint.
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.
BoolVar PresenceBoolVar() const
Returns a BoolVar indicating the presence of this interval.
BoolVar Not(BoolVar x)
A convenient wrapper so we can write Not(x) instead of x.Not() which is sometimes clearer.
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
const ::operations_research::sat::ConstraintProto & constraints(int index) const
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:193
Constraint AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
std::string DebugString() const
Returns a debug string.
LinearExpr & AddTerm(IntVar var, int64_t coeff)
Adds a term (var * coeff) to the linear expression.
void AddEvent(IntVar time, int64_t demand)
Adds a mandatory event.
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
const CpModelProto & Build() const
Definition: cp_model.h:909
::operations_research::sat::IntervalConstraintProto * mutable_interval()
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Constraint AddMinEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == min(vars).
A dedicated container for linear expressions.
Definition: cp_model.h:250
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
The constraint will be enforced iff all literals listed here are true.
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
Definition: cp_model.h:665
void AddAssumptions(absl::Span< const BoolVar > literals)
Adds multiple literals to the model as assumptions.
Constraint(ConstraintProto *proto)
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
::operations_research::sat::ConstraintProto * mutable_constraints(int index)
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
void AddOptionalEvent(IntVar time, int64_t demand, BoolVar is_active)
Adds a optional event.
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
void ScaleObjectiveBy(double scaling)
Sets scaling of the objective.
bool operator!=(const BoolVar &other) const
Dis-Equality test.
Definition: cp_model.h:88
const std::string & Name() const
Returns the name of the variable (or the empty string if not set).
Definition: cp_model.h:163
We call domain any subset of Int64 = [kint64min, kint64max].
Constraint AddLinMinEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == min(exprs).
Specialized automaton constraint.
Definition: cp_model.h:563
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
LinearExpr & AddVar(IntVar var)
Adds a single integer variable to the linear expression.
BoolVar WithName(const std::string &name)
Sets the name of the variable.
std::string DebugString() const
Debug string.
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
IntVar WithName(const std::string &name)
Sets the name of the variable.
BoolVar NewBoolVar()
Creates a Boolean variable.
static LinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of variables and coefficients.
Constraint AddNoOverlap(absl::Span< const IntervalVar > vars)
Adds a no-overlap constraint that ensures that all present intervals do not overlap in time.
Constraint WithName(const std::string &name)
Sets the name of the constraint.
std::string DebugString() const
Returns a debug string.
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:111
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
NoOverlap2DConstraint AddNoOverlap2D()
The no_overlap_2d constraint prevents a set of boxes from overlapping.
int64_t Value() const
Checks that the expression is constant and returns its value.
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Adds an forbidden assignments constraint.
const ::operations_research::sat::IntervalConstraintProto & interval() const
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
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.
LinearExpr StartExpr() const
Returns the start linear expression.
LinearExpr SizeExpr() const
Returns the size linear expression.
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
IntervalConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:390
CumulativeConstraint AddCumulative(IntVar capacity)
The cumulative constraint.
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:101
bool operator==(const IntVar &other) const
Equality test with another IntVar.
Definition: cp_model.h:170
std::string Name() const
Returns the name of the interval (or the empty string if not set).
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
const IntervalConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:385
Wrapper class around the cp_model proto.
Definition: cp_model.h:617
const std::vector< IntVar > & variables() const
Returns the vector of variables.
Definition: cp_model.h:296
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Constructs the sum of a list of Booleans.
::operations_research::sat::IntegerVariableProto * mutable_variables(int index)
Constraint AddDivisionEquality(IntVar target, IntVar numerator, IntVar denominator)
Adds target = num / denom (integer division rounded towards 0).
Represents a Interval variable.
Definition: cp_model.h:341
static LinearExpr BooleanScalProd(absl::Span< const BoolVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of Booleans and coefficients.
void CopyFrom(const CpModelProto &model_proto)
Replace the current model with the one from the given proto.
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:183