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
55namespace sat {
56
57class CpModelBuilder;
58class LinearExpr;
59class IntVar;
60
69class BoolVar {
70 public:
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;
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
130std::ostream& operator<<(std::ostream& os, const BoolVar& var);
131
137
148class IntVar {
149 public:
151
153 IntVar(const BoolVar& var); // NOLINT(runtime/explicit)
154
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
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;
199 friend class LinearExpr;
200 friend class IntervalVar;
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
213std::ostream& operator<<(std::ostream& os, const IntVar& var);
214
251 public:
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
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
319std::ostream& operator<<(std::ostream& os, const LinearExpr& e);
320
342 public:
345
347 IntervalVar WithName(const std::string& name);
348
350 std::string Name() const;
351
355
359
363
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;
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
409std::ostream& operator<<(std::ostream& os, const IntervalVar& var);
410
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
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
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
632
637
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
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
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
882 absl::Span<const IntVar> variables,
885
888 absl::Span<const BoolVar> variables,
891
893 void AddHint(IntVar var, int64_t value);
894
897
900
902 void AddAssumptions(absl::Span<const BoolVar> literals);
903
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:
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
960int64_t SolutionIntegerValue(const CpSolverResponse& r, const LinearExpr& expr);
961
964
967
970
971} // namespace sat
972} // namespace operations_research
973
974#endif // OR_TOOLS_SAT_CP_MODEL_H_
We call domain any subset of Int64 = [kint64min, kint64max].
Specialized automaton constraint.
Definition: cp_model.h:563
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
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
BoolVar WithName(const std::string &name)
Sets the name of the variable.
std::string DebugString() const
Debug string.
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
const std::string & Name() const
Returns the name of the variable.
Definition: cp_model.h:77
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:469
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
The constraint will be enforced iff all literals listed here are true.
Constraint WithName(const std::string &name)
Sets the name of the constraint.
ConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:454
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Constraint(ConstraintProto *proto)
Constraint OnlyEnforceIf(BoolVar literal)
See OnlyEnforceIf(absl::Span<const BoolVar> literals).
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:451
::operations_research::sat::IntervalConstraintProto * mutable_interval()
const ::operations_research::sat::IntervalConstraintProto & interval() const
Wrapper class around the cp_model proto.
Definition: cp_model.h:617
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
Adds an forbidden assignments constraint.
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
void ClearAssumptions()
Remove all assumptions from the model.
Constraint AddAbsEquality(IntVar target, IntVar var)
Adds target == abs(var).
void AddAssumptions(absl::Span< const BoolVar > literals)
Adds multiple literals to the model as assumptions.
IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
Creates an interval variable with a fixed size.
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.
Constraint AddMinEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == min(vars).
BoolVar TrueVar()
Creates an always true Boolean variable.
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).
Constraint AddDivisionEquality(IntVar target, IntVar numerator, IntVar denominator)
Adds target = num / denom (integer division rounded towards 0).
void AddDecisionStrategy(absl::Span< const BoolVar > variables, DecisionStrategyProto::VariableSelectionStrategy var_strategy, DecisionStrategyProto::DomainReductionStrategy domain_strategy)
Adds a decision strategy on a list of boolean variables.
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
BoolVar NewBoolVar()
Creates a Boolean variable.
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.
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.
void ScaleObjectiveBy(double scaling)
Sets scaling of the objective.
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
CumulativeConstraint AddCumulative(IntVar capacity)
The cumulative constraint.
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
void CopyFrom(const CpModelProto &model_proto)
Replace the current model with the one from the given proto.
Constraint AddLessThan(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 AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
BoolVar FalseVar()
Creates an always false Boolean variable.
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
Definition: cp_model.h:665
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
ReservoirConstraint AddReservoirConstraint(int64_t min_level, int64_t max_level)
Adds a reservoir constraint with optional refill/emptying events.
Constraint AddModuloEquality(IntVar target, IntVar var, IntVar mod)
Adds target = var % mod.
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
NoOverlap2DConstraint AddNoOverlap2D()
The no_overlap_2d constraint prevents a set of boxes from overlapping.
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
An automaton constraint/.
Constraint AddLinMinEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == min(exprs).
Constraint AddProductEquality(IntVar target, absl::Span< const IntVar > vars)
Adds target == prod(vars).
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
Constraint AddLinMaxEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == max(exprs).
const CpModelProto & Build() const
Definition: cp_model.h:909
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
this constraint forces all variables to have different values.
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Adds an allowed assignments constraint.
IntVar NewConstant(int64_t value)
Creates a constant variable.
IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
Creates an interval variable from 3 affine expressions.
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
An inverse constraint.
Constraint AddNoOverlap(absl::Span< const IntervalVar > vars)
Adds a no-overlap constraint that ensures that all present intervals do not overlap in time.
const CpModelProto & Proto() const
Definition: cp_model.h:911
const ::operations_research::sat::IntegerVariableProto & variables(int index) const
::operations_research::sat::ConstraintProto * mutable_constraints(int index)
::operations_research::sat::IntegerVariableProto * mutable_variables(int index)
const ::operations_research::sat::ConstraintProto & constraints(int index) const
Specialized cumulative constraint.
Definition: cp_model.h:597
void AddDemand(IntervalVar interval, IntVar demand)
Adds a pair (interval, demand) to the constraint.
An integer variable.
Definition: cp_model.h:148
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:188
friend int64_t SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
bool operator==(const IntVar &other) const
Equality test with another IntVar.
Definition: cp_model.h:170
std::string DebugString() const
Returns a debug string.
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.
const std::string & Name() const
Returns the name of the variable (or the empty string if not set).
Definition: cp_model.h:163
friend int64_t 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 variable in the model.
Definition: cp_model.h:193
IntVar(const BoolVar &var)
Implicit cast BoolVar -> IntVar.
LinearExpr AddConstant(int64_t value) const
Adds a constant value to an integer variable and returns a linear expression.
friend int64_t SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
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:341
LinearExpr SizeExpr() const
Returns the size linear expression.
LinearExpr StartExpr() const
Returns the start linear expression.
BoolVar PresenceBoolVar() const
Returns a BoolVar indicating the presence of this interval.
std::string Name() const
Returns the name of the interval (or the empty string if not set).
const IntervalConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Definition: cp_model.h:385
std::string DebugString() const
Returns a debug string.
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
Definition: cp_model.h:377
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
Definition: cp_model.h:372
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
IntervalConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
Definition: cp_model.h:390
LinearExpr EndExpr() const
Returns the end linear expression.
int index() const
Returns the index of the interval constraint in the model.
Definition: cp_model.h:395
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
A dedicated container for linear expressions.
Definition: cp_model.h:250
int64_t Value() const
Checks that the expression is constant and returns its value.
LinearExpr & AddVar(IntVar var)
Adds a single integer variable to the linear expression.
LinearExpr & AddExpression(const LinearExpr &expr)
Adds another linear expression to the linear expression.
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
IntVar Var() const
Checks that the expression is 1 * var + 0, and returns var.
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
Definition: cp_model.h:299
LinearExpr(IntVar var)
Constructs a linear expression from an integer variable.
std::string DebugString() const
Debug string.
static LinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of variables and coefficients.
static LinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
LinearExpr(BoolVar var)
Constructs a linear expression from a Boolean variable.
int64_t constant() const
Returns the constant term.
Definition: cp_model.h:302
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Constructs the sum of a list of Booleans.
LinearExpr & AddConstant(int64_t value)
Adds a constant value to the linear expression.
const std::vector< IntVar > & variables() const
Returns the vector of variables.
Definition: cp_model.h:296
LinearExpr & AddTerm(IntVar var, int64_t coeff)
Adds a term (var * coeff) to the linear expression.
static LinearExpr BooleanScalProd(absl::Span< const BoolVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of Booleans and coefficients.
LinearExpr(int64_t constant)
Constructs a constant linear expression.
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Specialized no_overlap2D constraint.
Definition: cp_model.h:580
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
Specialized reservoir constraint.
Definition: cp_model.h:532
void AddEvent(IntVar time, int64_t demand)
Adds a mandatory event.
void AddOptionalEvent(IntVar time, int64_t demand, BoolVar is_active)
Adds a optional event.
Specialized assignment constraint.
Definition: cp_model.h:515
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
int64_t SolutionIntegerMax(const CpSolverResponse &r, IntVar x)
Returns the max of an integer variable in a solution.
BoolVar Not(BoolVar x)
A convenient wrapper so we can write Not(x) instead of x.Not() which is sometimes clearer.
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
int64_t SolutionIntegerMin(const CpSolverResponse &r, IntVar x)
Returns the min of an integer variable in a solution.
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.