OR-Tools  9.0
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 
101  IntegerVariableProto* MutableProto() const {
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 
188  IntegerVariableProto* MutableProto() const {
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  void AddVar(IntVar var);
272 
274  void AddTerm(IntVar var, int64_t coeff);
275 
277  static LinearExpr Sum(absl::Span<const IntVar> vars);
278 
280  static LinearExpr ScalProd(absl::Span<const IntVar> vars,
281  absl::Span<const int64_t> coeffs);
282 
284  static LinearExpr BooleanSum(absl::Span<const BoolVar> vars);
285 
287  static LinearExpr BooleanScalProd(absl::Span<const BoolVar> vars,
288  absl::Span<const int64_t> coeffs);
290  static LinearExpr Term(IntVar var, int64_t coefficient);
291 
293  const std::vector<IntVar>& variables() const { return variables_; }
294 
296  const std::vector<int64_t>& coefficients() const { return coefficients_; }
297 
299  int64_t constant() const { return constant_; }
300 
301  // TODO(user): LinearExpr.DebugString() and operator<<.
302 
303  private:
304  std::vector<IntVar> variables_;
305  std::vector<int64_t> coefficients_;
306  int64_t constant_ = 0;
307 };
308 
328 class IntervalVar {
329  public:
331  IntervalVar();
332 
334  IntervalVar WithName(const std::string& name);
335 
337  std::string Name() const;
338 
340  IntVar StartVar() const;
341 
343  IntVar SizeVar() const;
344 
346  IntVar EndVar() const;
347 
353  BoolVar PresenceBoolVar() const;
354 
356  bool operator==(const IntervalVar& other) const {
357  return other.cp_model_ == cp_model_ && other.index_ == index_;
358  }
359 
361  bool operator!=(const IntervalVar& other) const {
362  return other.cp_model_ != cp_model_ || other.index_ != index_;
363  }
364 
366  std::string DebugString() const;
367 
369  const IntervalConstraintProto& Proto() const {
370  return cp_model_->constraints(index_).interval();
371  }
372 
374  IntervalConstraintProto* MutableProto() const {
375  return cp_model_->mutable_constraints(index_)->mutable_interval();
376  }
377 
379  int index() const { return index_; }
380 
381  private:
382  friend class CpModelBuilder;
383  friend class CumulativeConstraint;
384  friend class NoOverlap2DConstraint;
385  friend std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
386 
387  IntervalVar(int index, CpModelProto* cp_model);
388 
389  CpModelProto* cp_model_ = nullptr;
390  int index_ = std::numeric_limits<int32_t>::min();
391 };
392 
393 std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
394 
404 class Constraint {
405  public:
423  Constraint OnlyEnforceIf(absl::Span<const BoolVar> literals);
424 
427 
429  Constraint WithName(const std::string& name);
430 
432  const std::string& Name() const;
433 
435  const ConstraintProto& Proto() const { return *proto_; }
436 
438  ConstraintProto* MutableProto() const { return proto_; }
439 
440  protected:
441  friend class CpModelBuilder;
442 
443  explicit Constraint(ConstraintProto* proto);
444 
445  ConstraintProto* proto_ = nullptr;
446 };
447 
454  public:
462  void AddArc(int tail, int head, BoolVar literal);
463 
464  private:
465  friend class CpModelBuilder;
466 
468 };
469 
477  public:
485  void AddArc(int tail, int head, BoolVar literal);
486 
487  private:
488  friend class CpModelBuilder;
489 
491 };
492 
499 class TableConstraint : public Constraint {
500  public:
502  void AddTuple(absl::Span<const int64_t> tuple);
503 
504  private:
505  friend class CpModelBuilder;
506 
508 };
509 
517  public:
523  void AddEvent(IntVar time, int64_t demand);
524 
531  void AddOptionalEvent(IntVar time, int64_t demand, BoolVar is_active);
532 
533  private:
534  friend class CpModelBuilder;
535 
536  ReservoirConstraint(ConstraintProto* proto, CpModelBuilder* builder);
537 
538  CpModelBuilder* builder_;
539 };
540 
548  public:
550  void AddTransition(int tail, int head, int64_t transition_label);
551 
552  private:
553  friend class CpModelBuilder;
554 
556 };
557 
565  public:
567  void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate);
568 
569  private:
570  friend class CpModelBuilder;
571 
573 };
574 
582  public:
585 
586  private:
587  friend class CpModelBuilder;
588 
589  CumulativeConstraint(ConstraintProto* proto, CpModelBuilder* builder);
590 
591  CpModelBuilder* builder_;
592 };
593 
602  public:
604  IntVar NewIntVar(const Domain& domain);
605 
608 
610  IntVar NewConstant(int64_t value);
611 
613  BoolVar TrueVar();
614 
616  BoolVar FalseVar();
617 
619  IntervalVar NewIntervalVar(IntVar start, IntVar size, IntVar end);
620 
623  BoolVar presence);
624 
626  Constraint AddBoolOr(absl::Span<const BoolVar> literals);
627 
629  Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
630 
632  Constraint AddBoolXor(absl::Span<const BoolVar> literals);
633 
636  return AddBoolOr({a.Not(), b});
637  }
638 
640  Constraint AddEquality(const LinearExpr& left, const LinearExpr& right);
641 
643  Constraint AddGreaterOrEqual(const LinearExpr& left, const LinearExpr& right);
644 
646  Constraint AddGreaterThan(const LinearExpr& left, const LinearExpr& right);
647 
649  Constraint AddLessOrEqual(const LinearExpr& left, const LinearExpr& right);
650 
652  Constraint AddLessThan(const LinearExpr& left, const LinearExpr& right);
653 
655  Constraint AddLinearConstraint(const LinearExpr& expr, const Domain& domain);
656 
658  Constraint AddNotEqual(const LinearExpr& left, const LinearExpr& right);
659 
661  Constraint AddAllDifferent(absl::Span<const IntVar> vars);
662 
665  absl::Span<const IntVar> variables,
666  IntVar target);
667 
669  Constraint AddElement(IntVar index, absl::Span<const int64_t> values,
670  IntVar target);
671 
689 
704 
716  TableConstraint AddAllowedAssignments(absl::Span<const IntVar> vars);
717 
728  TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> vars);
729 
735  Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
736  absl::Span<const IntVar> inverse_variables);
737 
757  int64_t max_level);
758 
787  absl::Span<const IntVar> transition_variables, int starting_state,
788  absl::Span<const int> final_states);
789 
791  Constraint AddMinEquality(IntVar target, absl::Span<const IntVar> vars);
792 
795  absl::Span<const LinearExpr> exprs);
796 
798  Constraint AddMaxEquality(IntVar target, absl::Span<const IntVar> vars);
799 
802  absl::Span<const LinearExpr> exprs);
803 
805  Constraint AddDivisionEquality(IntVar target, IntVar numerator,
806  IntVar denominator);
807 
810 
813 
815  Constraint AddProductEquality(IntVar target, absl::Span<const IntVar> vars);
816 
821  Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
822 
827 
834 
836  void Minimize(const LinearExpr& expr);
837 
839  void Maximize(const LinearExpr& expr);
840 
848  void ScaleObjectiveBy(double scaling);
849 
851  void AddDecisionStrategy(
852  absl::Span<const IntVar> variables,
853  DecisionStrategyProto::VariableSelectionStrategy var_strategy,
854  DecisionStrategyProto::DomainReductionStrategy domain_strategy);
855 
857  void AddDecisionStrategy(
858  absl::Span<const BoolVar> variables,
859  DecisionStrategyProto::VariableSelectionStrategy var_strategy,
860  DecisionStrategyProto::DomainReductionStrategy domain_strategy);
861 
863  void AddHint(IntVar var, int64_t value);
864 
866  void ClearHints();
867 
869  void AddAssumption(BoolVar lit);
870 
872  void AddAssumptions(absl::Span<const BoolVar> literals);
873 
875  void ClearAssumptions();
876 
877  // TODO(user) : add MapDomain?
878 
879  const CpModelProto& Build() const { return Proto(); }
880 
881  const CpModelProto& Proto() const { return cp_model_; }
882  CpModelProto* MutableProto() { return &cp_model_; }
883 
885  void CopyFrom(const CpModelProto& model_proto);
886 
889 
892 
895 
896  private:
897  friend class CumulativeConstraint;
898  friend class ReservoirConstraint;
899 
900  // Fills the 'expr_proto' with the linear expression represented by 'expr'.
901  void LinearExprToProto(const LinearExpr& expr,
902  LinearExpressionProto* expr_proto);
903 
904  // Returns a (cached) integer variable index with a constant value.
905  int IndexFromConstant(int64_t value);
906 
907  // Returns a valid integer index from a BoolVar index.
908  // If the input index is a positive, it returns this index.
909  // If the input index is negative, it creates a cached IntVar equal to
910  // 1 - BoolVar(PositiveRef(index)), and returns the index of this new
911  // variable.
912  int GetOrCreateIntegerIndex(int index);
913 
914  void FillLinearTerms(const LinearExpr& left, const LinearExpr& right,
915  LinearConstraintProto* proto);
916 
917  CpModelProto cp_model_;
918  absl::flat_hash_map<int64_t, int> constant_to_index_map_;
919  absl::flat_hash_map<int, int> bool_to_integer_index_map_;
920 };
921 
923 int64_t SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
924 
926 int64_t SolutionIntegerMin(const CpSolverResponse& r, IntVar x);
927 
929 int64_t SolutionIntegerMax(const CpSolverResponse& r, IntVar x);
930 
932 bool SolutionBooleanValue(const CpSolverResponse& r, BoolVar x);
933 
934 } // namespace sat
935 } // namespace operations_research
936 
937 #endif // OR_TOOLS_SAT_CP_MODEL_H_
int64_t min
Definition: alldiff_cst.cc:139
We call domain any subset of Int64 = [kint64min, kint64max].
Specialized automaton constraint.
Definition: cp_model.h:547
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
Definition: cp_model.cc:266
A Boolean variable.
Definition: cp_model.h:69
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:101
const std::string & Name() const
Returns the name of the variable.
Definition: cp_model.h:77
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:39
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:891
bool operator!=(const BoolVar &other) const
Dis-Equality test.
Definition: cp_model.h:88
bool operator==(const BoolVar &other) const
Equality test with another boolvar.
Definition: cp_model.h:83
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:111
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
Definition: cp_model.h:80
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:96
Specialized circuit constraint.
Definition: cp_model.h:453
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:228
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
The constraint will be enforced iff all literals listed here are true.
Definition: cp_model.cc:216
Constraint WithName(const std::string &name)
Sets the name of the constraint.
Definition: cp_model.cc:209
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:435
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Definition: cp_model.cc:214
ConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:438
Constraint(ConstraintProto *proto)
Definition: cp_model.cc:207
Wrapper class around the cp_model proto.
Definition: cp_model.h:601
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
Definition: cp_model.cc:796
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Adds an forbidden assignments constraint.
Definition: cp_model.cc:586
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
Definition: cp_model.cc:509
void ClearAssumptions()
Remove all assumptions from the model.
Definition: cp_model.cc:816
Constraint AddAbsEquality(IntVar target, IntVar var)
Adds target == abs(var).
Definition: cp_model.cc:695
void AddAssumptions(absl::Span< const BoolVar > literals)
Adds multiple literals to the model as assumptions.
Definition: cp_model.cc:810
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.
Definition: cp_model.cc:573
Constraint AddMinEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == min(vars).
Definition: cp_model.cc:632
BoolVar TrueVar()
Creates an always true Boolean variable.
Definition: cp_model.cc:392
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
Definition: cp_model.cc:370
Constraint AddMaxEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == max(vars).
Definition: cp_model.cc:664
Constraint AddDivisionEquality(IntVar target, IntVar numerator, IntVar denominator)
Adds target = num / denom (integer division rounded towards 0).
Definition: cp_model.cc:685
void ClearHints()
Remove all hints.
Definition: cp_model.cc:802
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
Definition: cp_model.cc:754
IntervalVar NewOptionalIntervalVar(IntVar start, IntVar size, IntVar end, BoolVar presence)
Creates an optional interval variable.
Definition: cp_model.cc:405
BoolVar NewBoolVar()
Creates a Boolean variable.
Definition: cp_model.cc:380
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:772
void ScaleObjectiveBy(double scaling)
Sets scaling of the objective.
Definition: cp_model.cc:766
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
Definition: cp_model.cc:569
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
Definition: cp_model.cc:546
const CpModelProto & Proto() const
Definition: cp_model.h:881
CumulativeConstraint AddCumulative(IntVar capacity)
The cumulative constraint.
Definition: cp_model.cc:736
const CpModelProto & Build() const
Definition: cp_model.h:879
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
Definition: cp_model.cc:489
void CopyFrom(const CpModelProto &model_proto)
Replace the current model with the one from the given proto.
Definition: cp_model.cc:820
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
Definition: cp_model.cc:499
Constraint AddBoolXor(absl::Span< const BoolVar > literals)
Adds the constraint that a odd number of literal is true.
Definition: cp_model.cc:434
Constraint AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
Definition: cp_model.cc:557
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
Definition: cp_model.cc:806
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
Definition: cp_model.cc:743
BoolVar FalseVar()
Creates an always false Boolean variable.
Definition: cp_model.cc:396
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
Definition: cp_model.h:635
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
Definition: cp_model.cc:426
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
Definition: cp_model.cc:856
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
Definition: cp_model.cc:479
ReservoirConstraint AddReservoirConstraint(int64_t min_level, int64_t max_level)
Adds a reservoir constraint with optional refill/emptying events.
Definition: cp_model.cc:610
Constraint AddModuloEquality(IntVar target, IntVar var, IntVar mod)
Adds target = var % mod.
Definition: cp_model.cc:704
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
Definition: cp_model.cc:459
NoOverlap2DConstraint AddNoOverlap2D()
The no_overlap_2d constraint prevents a set of boxes from overlapping.
Definition: cp_model.cc:732
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Definition: cp_model.cc:469
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
Definition: cp_model.cc:418
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
Definition: cp_model.cc:850
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
An automaton constraint/.
Definition: cp_model.cc:618
Constraint AddLinMinEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == min(exprs).
Definition: cp_model.cc:653
Constraint AddProductEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == prod(vars).
Definition: cp_model.cc:713
Constraint AddLinMaxEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == max(exprs).
Definition: cp_model.cc:674
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
Definition: cp_model.cc:834
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
Definition: cp_model.cc:526
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
this constraint forces all variables to have different values.
Definition: cp_model.cc:538
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Adds an allowed assignments constraint.
Definition: cp_model.cc:577
IntVar NewConstant(int64_t value)
Creates a constant variable.
Definition: cp_model.cc:388
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
An inverse constraint.
Definition: cp_model.cc:596
IntervalVar NewIntervalVar(IntVar start, IntVar size, IntVar end)
Creates an interval variable.
Definition: cp_model.cc:400
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:723
Specialized cumulative constraint.
Definition: cp_model.h:581
void AddDemand(IntervalVar interval, IntVar demand)
Adds a pair (interval, demand) to the constraint.
Definition: cp_model.cc:283
An integer variable.
Definition: cp_model.h:148
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:188
const std::string & Name() const
Returns the name of the variable (or the empty string if not set).
Definition: cp_model.h:163
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
Definition: cp_model.cc:90
friend int64_t SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
Definition: cp_model.cc:883
bool operator==(const IntVar &other) const
Equality test with another IntVar.
Definition: cp_model.h:170
std::string DebugString() const
Returns a debug string.
Definition: cp_model.cc:104
bool operator!=(const IntVar &other) const
Difference test with anpther IntVar.
Definition: cp_model.h:175
IntVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:80
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:866
int index() const
Returns the index of the variable in the model.
Definition: cp_model.h:193
LinearExpr AddConstant(int64_t value) const
Adds a constant value to an integer variable and returns a linear expression.
Definition: cp_model.cc:100
friend int64_t SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
Definition: cp_model.cc:875
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:183
Represents a Interval variable.
Definition: cp_model.h:328
BoolVar PresenceBoolVar() const
Returns a BoolVar indicating the presence of this interval.
Definition: cp_model.cc:309
std::string Name() const
Returns the name of the interval (or the empty string if not set).
Definition: cp_model.cc:314
IntVar SizeVar() const
Returns the size variable.
Definition: cp_model.cc:303
std::string DebugString() const
Returns a debug string.
Definition: cp_model.cc:318
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition: cp_model.h:361
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
Definition: cp_model.h:356
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
Definition: cp_model.cc:294
IntVar EndVar() const
Returns the end variable.
Definition: cp_model.cc:307
const IntervalConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:369
IntervalConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:374
int index() const
Returns the index of the interval constraint in the model.
Definition: cp_model.h:379
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
Definition: cp_model.cc:333
IntVar StartVar() const
Returns the start variable.
Definition: cp_model.cc:299
A dedicated container for linear expressions.
Definition: cp_model.h:250
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
Definition: cp_model.cc:146
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Constructs the sum of a list of Booleans.
Definition: cp_model.cc:170
void AddTerm(IntVar var, int64_t coeff)
Adds a term (var * coeff) to the linear expression.
Definition: cp_model.cc:195
int64_t constant() const
Returns the constant term.
Definition: cp_model.h:299
const std::vector< IntVar > & variables() const
Returns the vector of variables.
Definition: cp_model.h:293
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 AddVar(IntVar var)
Adds a single integer variable to the linear expression.
Definition: cp_model.cc:193
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
LinearExpr & AddConstant(int64_t value)
Adds a constant value to the linear expression.
Definition: cp_model.cc:188
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:296
static LinearExpr Term(IntVar var, int64_t coefficient)
Construncts var * coefficient.
Definition: cp_model.cc:164
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Definition: cp_model.cc:234
Specialized no_overlap2D constraint.
Definition: cp_model.h:564
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
Definition: cp_model.cc:273
Specialized reservoir constraint.
Definition: cp_model.h:516
void AddEvent(IntVar time, int64_t demand)
Adds a mandatory event.
Definition: cp_model.cc:251
void AddOptionalEvent(IntVar time, int64_t demand, BoolVar is_active)
Adds a optional event.
Definition: cp_model.cc:258
Specialized assignment constraint.
Definition: cp_model.h:499
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
Definition: cp_model.cc:240
int64_t b
int64_t a
CpModelProto proto
CpModelProto const * model_proto
const std::string name
int64_t value
IntVar * var
Definition: expr_array.cc:1874
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
Definition: cp_model.cc:68
int64_t SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
Definition: cp_model.cc:883
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
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
Definition: cp_model.cc:891
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
Definition: cp_model.cc:866
int64_t SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
Definition: cp_model.cc:875
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:85
int index
Definition: pack.cc:509
int64_t demand
Definition: resource.cc:125
int64_t time
Definition: resource.cc:1691
IntervalVar * interval
Definition: resource.cc:100
int64_t coefficient
int64_t capacity
int64_t tail
int64_t head