38#ifndef OR_TOOLS_SAT_CP_MODEL_H_
39#define OR_TOOLS_SAT_CP_MODEL_H_
42#include <initializer_list>
49#include "absl/container/flat_hash_map.h"
50#include "absl/types/span.h"
51#include "ortools/sat/cp_model.pb.h"
55#include "ortools/sat/sat_parameters.pb.h"
86 std::string
Name()
const;
92 return other.builder_ == builder_ && other.index_ == index_;
96 return other.builder_ != builder_ || other.index_ != index_;
107 int index()
const {
return index_; }
168 std::string
Name()
const;
171 return other.builder_ == builder_ && other.index_ == index_;
175 return other.builder_ != builder_ || other.index_ != index_;
186 int index()
const {
return index_; }
267 absl::Span<const int64_t> coeffs);
271 absl::Span<const int64_t> coeffs);
288 const std::vector<int>&
variables()
const {
return variables_; }
291 const std::vector<int64_t>&
coefficients()
const {
return coefficients_; }
307 std::vector<int> variables_;
308 std::vector<int64_t> coefficients_;
309 int64_t constant_ = 0;
398 absl::Span<const double> coeffs);
402 absl::Span<const double> coeffs);
405 const std::vector<int>&
variables()
const {
return variables_; }
408 const std::vector<double>&
coefficients()
const {
return coefficients_; }
420 std::vector<int> variables_;
421 std::vector<double> coefficients_;
422 double constant_ = 0;
425std::ostream&
operator<<(std::ostream& os,
const DoubleLinearExpr& e);
459 std::string
Name()
const;
482 return other.builder_ == builder_ && other.index_ == index_;
487 return other.builder_ != builder_ || other.index_ != index_;
494 int index()
const {
return index_; }
547 const std::string&
Name()
const;
617 void AddTuple(absl::Span<const int64_t> tuple);
762 int64_t size,
BoolVar presence);
831 absl::Span<const IntVar> variables,
902 absl::Span<const IntVar> inverse_variables);
953 absl::Span<const IntVar> transition_variables,
int starting_state,
954 absl::Span<const int> final_states);
958 absl::Span<const IntVar> vars);
962 absl::Span<const LinearExpr> exprs);
966 std::initializer_list<LinearExpr> exprs);
970 absl::Span<const IntVar> vars);
974 absl::Span<const LinearExpr> exprs);
978 std::initializer_list<LinearExpr> exprs);
994 absl::Span<const LinearExpr> exprs);
998 absl::Span<const IntVar> vars);
1002 std::initializer_list<LinearExpr> exprs);
1044 absl::Span<const IntVar> variables,
1045 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1046 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1050 absl::Span<const BoolVar> variables,
1051 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1052 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1074 const CpModelProto&
Proto()
const {
return cp_model_; }
1096 LinearExpressionProto LinearExprToProto(
const LinearExpr& expr,
1097 bool negate =
false);
1100 int IndexFromConstant(int64_t
value);
1107 int GetOrCreateIntegerIndex(
int index);
1110 LinearConstraintProto*
proto);
1112 CpModelProto cp_model_;
1113 absl::flat_hash_map<int64_t, int> constant_to_index_map_;
1114 absl::flat_hash_map<int, int> bool_to_integer_index_map_;
1147 return std::move(lhs);
1151 return std::move(rhs);
1154 if (lhs.variables().size() < rhs.variables().size()) {
1155 rhs += std::move(lhs);
1156 return std::move(rhs);
1158 lhs += std::move(rhs);
1159 return std::move(lhs);
1170 return std::move(lhs);
1175 return std::move(rhs);
1178 lhs -= std::move(rhs);
1179 return std::move(lhs);
1207 return std::move(lhs);
1212 return std::move(rhs);
1216 if (lhs.variables().size() < rhs.variables().size()) {
1217 rhs += std::move(lhs);
1218 return std::move(rhs);
1220 lhs += std::move(rhs);
1221 return std::move(lhs);
1243 return std::move(lhs);
1249 return std::move(rhs);
1253 lhs -= std::move(rhs);
1254 return std::move(lhs);
We call domain any subset of Int64 = [kint64min, kint64max].
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Specialized automaton constraint.
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
std::string Name() const
Returns the name of the variable.
BoolVar WithName(const std::string &name)
Sets the name of the variable.
std::string DebugString() const
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
BoolVar()
A default constructed BoolVar can be used to mean not defined yet.
bool operator!=(const BoolVar &other) const
bool operator==(const BoolVar &other) const
int index() const
Returns the index of the variable in the model.
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
Specialized circuit constraint.
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).
const std::string & Name() const
Returns the name of the constraint (or the empty string if not set).
Constraint(ConstraintProto *proto)
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Wrapper class around the cp_model proto.
Constraint AddAtMostOne(absl::Span< const BoolVar > literals)
At most one literal is true. Sum literals <= 1.
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 AddMinEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == min(vars).
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
void ClearAssumptions()
Remove all assumptions from the model.
Constraint AddAbsEquality(const LinearExpr &target, const LinearExpr &expr)
Adds target == abs(expr).
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.
BoolVar TrueVar()
Creates an always true Boolean variable.
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
void ClearHints()
Removes all hints.
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
BoolVar NewBoolVar()
Creates a Boolean variable.
Constraint AddAtLeastOne(absl::Span< const BoolVar > literals)
Same as AddBoolOr. Sum literals >= 1.
Constraint AddMaxEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == max(vars).
Constraint AddMultiplicationEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == prod(exprs).
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.
CircuitConstraint AddCircuitConstraint()
Adds a circuit constraint.
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
void CopyFrom(const CpModelProto &model_proto)
Replaces 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 an odd number of literals is true.
void SetName(const std::string &name)
Sets the name of the model.
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.
CpModelProto * MutableProto()
BoolVar FalseVar()
Creates an always false Boolean variable.
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
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.
CumulativeConstraint AddCumulative(LinearExpr capacity)
The cumulative constraint.
void FixVariable(IntVar var, int64_t value)
It is sometime convenient when building a model to create a bunch of variables that will later be fix...
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 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.
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
const CpModelProto & Build() const
Constraint AddDivisionEquality(const LinearExpr &target, const LinearExpr &numerator, const LinearExpr &denominator)
Adds target = num / denom (integer division rounded towards 0).
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 AddModuloEquality(const LinearExpr &target, const LinearExpr &var, const LinearExpr &mod)
Adds target = var % mod.
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 AddExactlyOne(absl::Span< const BoolVar > literals)
Exactly one literal is true. Sum literals == 1.
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
Specialized cumulative constraint.
void AddDemand(IntervalVar interval, LinearExpr demand)
Adds a pair (interval, demand) to the constraint.
A dedicated container for linear expressions with double coefficients.
const bool IsConstant() const
double constant() const
Returns the constant term.
DoubleLinearExpr & operator+=(double value)
Adds a constant value to the linear expression.
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string. See the documentation for LinearExpr::DebugString().
static DoubleLinearExpr WeightedSum(absl::Span< const IntVar > vars, absl::Span< const double > coeffs)
Constructs the scalar product of variables and coefficients.
const std::vector< double > & coefficients() const
Returns the vector of coefficients.
const std::vector< int > & variables() const
Returns the vector of variable indices.
DoubleLinearExpr & operator-=(double value)
Adds a constant value to the linear expression.
DoubleLinearExpr & AddTerm(IntVar var, double coeff)
Adds a term (var * coeff) to the linear expression.
DoubleLinearExpr & operator*=(double coeff)
Multiply the linear expression by a constant.
static DoubleLinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
std::string Name() const
Returns the name of the variable (or the empty string if not set).
bool operator==(const IntVar &other) const
IntVar()
A default constructed IntVar can be used to mean not defined yet.
std::string DebugString() const
bool operator!=(const IntVar &other) const
IntVar WithName(const std::string &name)
Sets the name of the variable.
::operations_research::Domain Domain() const
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. This will be non-negative.
Represents a Interval variable.
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).
std::string DebugString() const
Returns a debug string.
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
IntervalVar WithName(const std::string &name)
Sets the name of the variable.
LinearExpr EndExpr() const
Returns the end linear expression.
IntervalVar()
A default constructed IntervalVar can be used to mean not defined yet.
int index() const
Returns the index of the interval constraint in the model.
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
A dedicated container for linear expressions.
LinearExpr & operator+=(const LinearExpr &other)
LinearExpr & operator-=(const LinearExpr &other)
LinearExpr & operator*=(int64_t factor)
const bool IsConstant() const
Returns true if the expression has no variables.
static LinearExpr WeightedSum(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.
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string.
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
const std::vector< int > & variables() const
Returns the vector of variable indices.
int64_t constant() const
Returns the constant term.
LinearExpr()=default
Creates an empty linear expression with value zero.
static LinearExpr FromProto(const LinearExpressionProto &proto)
Constructs a linear expr from its proto representation.
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
Specialized circuit constraint.
void AddArc(int tail, int head, BoolVar literal)
Add an arc to the circuit.
Specialized no_overlap2D constraint.
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
Specialized reservoir constraint.
void AddOptionalEvent(LinearExpr time, int64_t level_change, BoolVar is_active)
Adds an optional event.
void AddEvent(LinearExpr time, int64_t level_change)
Adds a mandatory event.
Specialized assignment constraint.
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
CpModelProto const * model_proto
LinearExpr operator+(const LinearExpr &lhs, const LinearExpr &rhs)
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
std::string VarDebugString(const CpModelProto &proto, int index)
LinearExpr operator-(LinearExpr expr)
BoolVar Not(BoolVar x)
A convenient wrapper so we can write Not(x) instead of x.Not() which is sometimes clearer.
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
LinearExpr operator*(LinearExpr expr, int64_t factor)
Collection of objects used to extend the Constraint Solver library.
std::optional< int64_t > end