OR-Tools  9.1
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"
50 #include "ortools/sat/model.h"
53 
54 namespace operations_research {
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 
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:
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 
840 
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 
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,
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_
int64_t head
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:301
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:969
BoolVar FalseVar()
Creates an always false Boolean variable.
Definition: cp_model.cc:465
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
Definition: cp_model.cc:497
Constraint AddModuloEquality(IntVar target, IntVar var, IntVar mod)
Adds target = var % mod.
Definition: cp_model.cc:807
LinearExpr & AddConstant(int64_t value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:188
Specialized reservoir constraint.
Definition: cp_model.h:532
LinearExpr EndExpr() const
Returns the end linear expression.
Definition: cp_model.cc:374
BoolVar TrueVar()
Creates an always true Boolean variable.
Definition: cp_model.cc:461
Constraint AddLinMaxEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == max(exprs).
Definition: cp_model.cc:777
const CpModelProto & Proto() const
Definition: cp_model.h:911
int64_t min
Definition: alldiff_cst.cc:139
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
Definition: cp_model.cc:909
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
Definition: cp_model.cc:969
std::string DebugString() const
Debug string.
Definition: cp_model.cc:39
Constraint AddAbsEquality(IntVar target, IntVar var)
Adds target == abs(var).
Definition: cp_model.cc:798
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
Definition: cp_model.cc:164
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
Definition: cp_model.cc:662
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.
Definition: cp_model.cc:582
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
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:875
LinearExpr AddConstant(int64_t value) const
Adds a constant value to an integer variable and returns a linear expression.
Definition: cp_model.cc:100
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
this constraint forces all variables to have different values.
Definition: cp_model.cc:631
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
Definition: cp_model.cc:552
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.
Definition: cp_model.cc:670
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
Definition: cp_model.cc:402
int index() const
Returns the index of the interval constraint in the model.
Definition: cp_model.h:395
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:703
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:996
friend int64_t SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
Definition: cp_model.cc:980
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
Definition: cp_model.cc:307
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:361
friend int64_t SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
Definition: cp_model.cc:988
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.
Definition: cp_model.cc:511
Constraint AddProductEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == prod(vars).
Definition: cp_model.cc:816
IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
Creates an interval variable with a fixed size.
Definition: cp_model.cc:475
LinearExpr & AddExpression(const LinearExpr &expr)
Adds another linear expression to the linear expression.
Definition: cp_model.cc:211
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
Definition: cp_model.cc:937
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
An automaton constraint/.
Definition: cp_model.cc:711
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.
Definition: cp_model.cc:527
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
int64_t coefficient
void AddDemand(IntervalVar interval, IntVar demand)
Adds a pair (interval, demand) to the constraint.
Definition: cp_model.cc:350
An integer variable.
Definition: cp_model.h:148
int64_t tail
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
Definition: cp_model.cc:846
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:96
int64_t b
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
IntVar NewConstant(int64_t value)
Creates a constant variable.
Definition: cp_model.cc:457
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
Definition: cp_model.cc:857
LinearExpr & AddTerm(IntVar var, int64_t coeff)
Adds a term (var * coeff) to the linear expression.
Definition: cp_model.cc:198
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
Definition: cp_model.cc:953
IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
Creates an interval variable from 3 affine expressions.
Definition: cp_model.cc:469
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
Definition: cp_model.cc:619
void ClearAssumptions()
Remove all assumptions from the model.
Definition: cp_model.cc:919
Constraint AddMaxEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == max(vars).
Definition: cp_model.cc:767
IntVar Var() const
Checks that the expression is 1 * var + 0, and returns var.
Definition: cp_model.cc:220
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
CpModelProto proto
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
An inverse constraint.
Definition: cp_model.cc:689
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
Definition: cp_model.cc:90
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.
Definition: cp_model.cc:666
int64_t SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
Definition: cp_model.cc:988
LinearExpr & AddVar(IntVar var)
Adds a single integer variable to the linear expression.
Definition: cp_model.cc:193
BoolVar PresenceBoolVar() const
Returns a BoolVar indicating the presence of this interval.
Definition: cp_model.cc:378
static LinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of variables and coefficients.
Definition: cp_model.cc:154
int64_t SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
Definition: cp_model.cc:980
const ::operations_research::sat::ConstraintProto & constraints(int index) const
int64_t demand
Definition: resource.cc:125
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:193
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:996
Constraint AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
Definition: cp_model.cc:650
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
Definition: cp_model.cc:959
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Definition: cp_model.cc:281
std::string DebugString() const
Returns a debug string.
Definition: cp_model.cc:387
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:66
int64_t capacity
int index
Definition: pack.cc:509
void AddEvent(IntVar time, int64_t demand)
Adds a mandatory event.
Definition: cp_model.cc:318
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
Definition: cp_model.cc:439
const CpModelProto & Build() const
Definition: cp_model.h:909
::operations_research::sat::IntervalConstraintProto * mutable_interval()
Constraint AddMinEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == min(vars).
Definition: cp_model.cc:725
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.
Definition: cp_model.cc:283
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:146
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.
Definition: cp_model.cc:913
Constraint(ConstraintProto *proto)
Definition: cp_model.cc:274
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
Definition: cp_model.cc:519
::operations_research::sat::ConstraintProto * mutable_constraints(int index)
void AddOptionalEvent(IntVar time, int64_t demand, BoolVar is_active)
Adds a optional event.
Definition: cp_model.cc:325
CpModelProto const * model_proto
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:295
static LinearExpr BooleanScalProd(absl::Span< const BoolVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of Booleans and coefficients.
Definition: cp_model.cc:178
void ClearHints()
Remove all hints.
Definition: cp_model.cc:905
void ScaleObjectiveBy(double scaling)
Sets scaling of the objective.
Definition: cp_model.cc:869
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).
Definition: cp_model.cc:756
Specialized automaton constraint.
Definition: cp_model.h:563
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Definition: cp_model.cc:562
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
Definition: cp_model.cc:639
BoolVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:34
std::string DebugString() const
Debug string.
Definition: cp_model.cc:232
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
Definition: cp_model.cc:592
IntVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:80
BoolVar NewBoolVar()
Creates a Boolean variable.
Definition: cp_model.cc:449
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:826
Constraint WithName(const std::string &name)
Sets the name of the constraint.
Definition: cp_model.cc:276
std::string DebugString() const
Returns a debug string.
Definition: cp_model.cc:104
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.
Definition: cp_model.cc:340
NoOverlap2DConstraint AddNoOverlap2D()
The no_overlap_2d constraint prevents a set of boxes from overlapping.
Definition: cp_model.cc:835
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Definition: linear_expr.h:114
int64_t Value() const
Checks that the expression is constant and returns its value.
Definition: cp_model.cc:227
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:333
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Adds an forbidden assignments constraint.
Definition: cp_model.cc:679
int64_t time
Definition: resource.cc:1691
const ::operations_research::sat::IntervalConstraintProto & interval() const
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
Definition: cp_model.cc:899
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:480
LinearExpr StartExpr() const
Returns the start linear expression.
Definition: cp_model.cc:366
LinearExpr SizeExpr() const
Returns the size linear expression.
Definition: cp_model.cc:370
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
Definition: cp_model.cc:68
IntVar * var
Definition: expr_array.cc:1874
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
Definition: cp_model.cc:572
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.
Definition: cp_model.cc:839
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).
Definition: cp_model.cc:383
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
Definition: cp_model.cc:602
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
::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).
Definition: cp_model.cc:788
Represents a Interval variable.
Definition: cp_model.h:341
int64_t value
Literal literal
Definition: optimization.cc:85
IntervalVar * interval
Definition: resource.cc:100
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Constructs the sum of a list of Booleans.
Definition: cp_model.cc:170
void CopyFrom(const CpModelProto &model_proto)
Replace the current model with the one from the given proto.
Definition: cp_model.cc:923
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:183
int64_t a