38#ifndef OR_TOOLS_SAT_CP_MODEL_H_
39#define OR_TOOLS_SAT_CP_MODEL_H_
42#include <initializer_list>
50#include "absl/container/flat_hash_map.h"
51#include "absl/strings/string_view.h"
52#include "absl/types/span.h"
53#include "ortools/sat/cp_model.pb.h"
57#include "ortools/sat/sat_parameters.pb.h"
88 std::string
Name()
const;
94 return other.builder_ == builder_ && other.index_ == index_;
98 return other.builder_ != builder_ || other.index_ != index_;
111 int index()
const {
return index_; }
128 int index_ = std::numeric_limits<int32_t>::min();
172 std::string
Name()
const;
175 return other.builder_ == builder_ && other.index_ == index_;
179 return other.builder_ != builder_ || other.index_ != index_;
190 int index()
const {
return index_; }
206 int index_ = std::numeric_limits<int32_t>::min();
271 absl::Span<const int64_t> coeffs);
275 absl::Span<const int64_t> coeffs);
292 const std::vector<int>&
variables()
const {
return variables_; }
295 const std::vector<int64_t>&
coefficients()
const {
return coefficients_; }
298 bool IsConstant()
const {
return variables_.empty(); }
301 int64_t
constant()
const {
return constant_; }
311 std::vector<int> variables_;
312 std::vector<int64_t> coefficients_;
313 int64_t constant_ = 0;
316std::ostream&
operator<<(std::ostream& os,
const LinearExpr& e);
348class DoubleLinearExpr {
399 absl::Span<const double> coeffs);
403 absl::Span<const double> coeffs);
406 const std::vector<int>&
variables()
const {
return variables_; }
409 const std::vector<double>&
coefficients()
const {
return coefficients_; }
412 bool IsConstant()
const {
return variables_.empty(); }
415 double constant()
const {
return constant_; }
421 std::vector<int> variables_;
422 std::vector<double> coefficients_;
423 double constant_ = 0;
426std::ostream&
operator<<(std::ostream& os,
const DoubleLinearExpr& e);
460 std::string
Name()
const;
483 return other.builder_ == builder_ && other.index_ == index_;
488 return other.builder_ != builder_ || other.index_ != index_;
495 int index()
const {
return index_; }
506 int index_ = std::numeric_limits<int32_t>::min();
514 return H::combine(std::move(h),
i.index());
519 return H::combine(std::move(h),
i.index());
559 absl::string_view
Name()
const;
562 const ConstraintProto&
Proto()
const {
return *
proto_; }
572 ConstraintProto*
proto_ =
nullptr;
629 void AddTuple(absl::Span<const int64_t> tuple);
813 absl::Span<const BoolVar> rhs) {
849 absl::Span<const IntVar> variables,
920 absl::Span<const IntVar> inverse_variables);
971 absl::Span<const IntVar> transition_variables,
int starting_state,
972 absl::Span<const int> final_states);
976 absl::Span<const IntVar> vars);
980 absl::Span<const LinearExpr> exprs);
984 std::initializer_list<LinearExpr> exprs);
988 absl::Span<const IntVar> vars);
992 absl::Span<const LinearExpr> exprs);
996 std::initializer_list<LinearExpr> exprs);
1012 absl::Span<const LinearExpr> exprs);
1016 absl::Span<const IntVar> vars);
1020 std::initializer_list<LinearExpr> exprs);
1068 absl::Span<const IntVar> variables,
1069 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1070 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1074 absl::Span<const BoolVar> variables,
1075 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1076 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1080 absl::Span<const LinearExpr> expressions,
1081 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1082 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1086 std::initializer_list<LinearExpr> expressions,
1087 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1088 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1108 const CpModelProto&
Build()
const {
return cp_model_; }
1109 const CpModelProto&
Proto()
const {
return cp_model_; }
1134 void ResetAndImport(
const CpModelProto& model_proto);
1137 LinearExpressionProto LinearExprToProto(
const LinearExpr& expr,
1138 bool negate =
false);
1141 int IndexFromConstant(int64_t
value);
1148 int GetOrCreateIntegerIndex(
int index);
1151 LinearConstraintProto*
proto);
1153 CpModelProto cp_model_;
1154 absl::flat_hash_map<int64_t, int> constant_to_index_map_;
1155 absl::flat_hash_map<int, int> bool_to_integer_index_map_;
1188 return std::move(lhs);
1192 return std::move(rhs);
1195 if (lhs.variables().size() < rhs.variables().size()) {
1196 rhs += std::move(lhs);
1197 return std::move(rhs);
1199 lhs += std::move(rhs);
1200 return std::move(lhs);
1204inline LinearExpr
operator-(
const LinearExpr& lhs,
const LinearExpr& rhs) {
1205 LinearExpr temp(lhs);
1211 return std::move(lhs);
1216 return std::move(rhs);
1219 lhs -= std::move(rhs);
1220 return std::move(lhs);
1245inline DoubleLinearExpr
operator+(DoubleLinearExpr&& lhs,
1246 const DoubleLinearExpr& rhs) {
1248 return std::move(lhs);
1253 return std::move(rhs);
1257 if (lhs.variables().size() < rhs.variables().size()) {
1258 rhs += std::move(lhs);
1259 return std::move(rhs);
1261 lhs += std::move(rhs);
1262 return std::move(lhs);
1266inline DoubleLinearExpr
operator+(DoubleLinearExpr expr,
double rhs) {
1270inline DoubleLinearExpr
operator+(
double lhs, DoubleLinearExpr expr) {
1281inline DoubleLinearExpr
operator-(DoubleLinearExpr&& lhs,
1282 const DoubleLinearExpr& rhs) {
1284 return std::move(lhs);
1290 return std::move(rhs);
1292inline DoubleLinearExpr
operator-(DoubleLinearExpr&& lhs,
1293 DoubleLinearExpr&& rhs) {
1294 lhs -= std::move(rhs);
1295 return std::move(lhs);
1302inline DoubleLinearExpr
operator-(
double lhs, DoubleLinearExpr expr) {
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
BoolVar WithName(absl::string_view name)
bool operator!=(const BoolVar &other) const
std::string Name() const
Returns the name of the variable.
friend bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
BoolVar operator~() const
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
std::string DebugString() const
bool operator==(const BoolVar &other) const
void AddArc(int tail, int head, BoolVar literal)
Constraint WithName(absl::string_view name)
Sets the name of the constraint.
Constraint OnlyEnforceIf(absl::Span< const BoolVar > literals)
Constraint(ConstraintProto *proto)
ConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
absl::string_view Name() const
Returns the name of the constraint (or the empty string if not set).
const ConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
Constraint AddVariableElement(IntVar index, absl::Span< const IntVar > variables, IntVar target)
Adds the element constraint: variables[index] == target.
bool HasObjective() const
Checks whether the model contains an objective.
const CpModelProto & Build() const
TableConstraint AddForbiddenAssignments(absl::Span< const IntVar > vars)
BoolVar GetBoolVarFromProtoIndex(int index)
Returns the Boolean variable from its index in the proto.
IntVar NewIntVar(const Domain &domain)
Creates an integer variable with the given domain.
Constraint AddInverseConstraint(absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
void Minimize(const LinearExpr &expr)
Adds a linear minimization objective.
NoOverlap2DConstraint AddNoOverlap2D()
Constraint AddBoolXor(absl::Span< const BoolVar > literals)
Adds the constraint that an odd number of literals is true.
IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
Creates an interval variable from 3 affine expressions.
IntVar GetIntVarFromProtoIndex(int index)
Returns the integer variable from its index in the proto.
Constraint AddMultiplicationEquality(const LinearExpr &target, absl::Span< const LinearExpr > exprs)
Adds target == prod(exprs).
CpModelBuilder Clone() const
Returns a cloned version of the current model.
IntVar NewConstant(int64_t value)
Constraint AddBoolAnd(absl::Span< const BoolVar > literals)
Adds the constraint that all literals must be true.
Constraint AddMaxEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == max(vars).
void Maximize(const LinearExpr &expr)
Adds a linear maximization objective.
void FixVariable(IntVar var, int64_t value)
ReservoirConstraint AddReservoirConstraint(int64_t min_level, int64_t max_level)
Constraint AddImplication(BoolVar a, BoolVar b)
Adds a => b.
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.
Constraint AddLessOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left <= right.
IntervalVar NewOptionalIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end, BoolVar presence)
void ClearAssumptions()
Remove all assumptions from the model.
Constraint AddLinearConstraint(const LinearExpr &expr, const Domain &domain)
Adds expr in domain.
AutomatonConstraint AddAutomaton(absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
void AddHint(IntVar var, int64_t value)
Adds hinting to a variable.
Constraint AddAllDifferent(absl::Span< const IntVar > vars)
This constraint forces all variables to have different values.
IntervalVar GetIntervalVarFromProtoIndex(int index)
Returns the interval variable from its index in the proto.
Constraint AddExactlyOne(absl::Span< const BoolVar > literals)
Exactly one literal is true. Sum literals == 1.
Constraint AddMinEquality(const LinearExpr &target, absl::Span< const IntVar > vars)
Adds target == min(vars).
void AddAssumption(BoolVar lit)
Adds a literal to the model as assumptions.
bool ExportToFile(absl::string_view filename) const
Export the model to file.
Constraint AddGreaterOrEqual(const LinearExpr &left, const LinearExpr &right)
Adds left >= right.
Constraint AddLessThan(const LinearExpr &left, const LinearExpr &right)
Adds left < right.
TableConstraint AddAllowedAssignments(absl::Span< const IntVar > vars)
Constraint AddElement(IntVar index, absl::Span< const int64_t > values, IntVar target)
Adds the element constraint: values[index] == target.
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Constraint AddNoOverlap(absl::Span< const IntervalVar > vars)
const CpModelProto & Proto() const
BoolVar NewBoolVar()
Creates a Boolean variable.
CpModelProto * MutableProto()
Constraint AddNotEqual(const LinearExpr &left, const LinearExpr &right)
Adds left != right.
Constraint AddAtLeastOne(absl::Span< const BoolVar > literals)
Same as AddBoolOr(). Sum literals >= 1.
void ClearObjective()
Removes the objective from the model.
Constraint AddAtMostOne(absl::Span< const BoolVar > literals)
At most one literal is true. Sum literals <= 1.
void ClearHints()
Removes all hints.
void AddAssumptions(absl::Span< const BoolVar > literals)
Adds multiple literals to the model as assumptions.
Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
Adds left > right.
CircuitConstraint AddCircuitConstraint()
Constraint AddDivisionEquality(const LinearExpr &target, const LinearExpr &numerator, const LinearExpr &denominator)
Adds target = num / denom (integer division rounded towards 0).
Constraint AddEquality(const LinearExpr &left, const LinearExpr &right)
Adds left == right.
void SetName(absl::string_view name)
Sets the name of the model.
Constraint AddAbsEquality(const LinearExpr &target, const LinearExpr &expr)
Adds target == abs(expr).
Constraint AddBoolOr(absl::Span< const BoolVar > literals)
Adds the constraint that at least one of the literals must be true.
IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
Creates an interval variable with a fixed size.
CumulativeConstraint AddCumulative(LinearExpr capacity)
Constraint AddModuloEquality(const LinearExpr &target, const LinearExpr &var, const LinearExpr &mod)
Adds target = var % mod.
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
void AddDemand(IntervalVar interval, LinearExpr demand)
Adds a pair (interval, demand) to the constraint.
const std::vector< double > & coefficients() const
Returns the vector of coefficients.
static DoubleLinearExpr Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
DoubleLinearExpr & operator*=(double coeff)
Multiply the linear expression by a constant.
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.
double constant() const
Returns the constant term.
static DoubleLinearExpr WeightedSum(absl::Span< const IntVar > vars, absl::Span< const double > coeffs)
Constructs the scalar product of variables and coefficients.
bool IsConstant() const
Returns true if the expression has no variable.
DoubleLinearExpr & operator-=(double value)
Adds a constant value to the linear expression.
const std::vector< int > & variables() const
Returns the vector of variable indices.
std::string DebugString(const CpModelProto *proto=nullptr) const
Debug string. See the documentation for LinearExpr::DebugString().
DoubleLinearExpr & AddExpression(const LinearExpr &exprs, double coeff=1.0)
Adds a linear expression to the double linear expression.
bool operator==(const IntVar &other) const
bool operator!=(const IntVar &other) const
std::string Name() const
Returns the name of the variable (or the empty string if not set).
std::string DebugString() 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.
IntVar WithName(absl::string_view name)
Sets the name of the variable.
BoolVar ToBoolVar() const
::operations_research::Domain Domain() const
IntervalVar WithName(absl::string_view name)
Sets the name of the variable.
LinearExpr StartExpr() const
bool operator==(const IntervalVar &other) const
Equality test with another interval variable.
std::string Name() const
Returns the name of the interval (or the empty string if not set).
bool operator!=(const IntervalVar &other) const
Difference test with another interval variable.
LinearExpr EndExpr() const
LinearExpr SizeExpr() const
BoolVar PresenceBoolVar() const
int index() const
Returns the index of the interval constraint in the model.
std::string DebugString() const
Returns a debug string.
friend std::ostream & operator<<(std::ostream &os, const IntervalVar &var)
bool IsConstant() const
Returns true if the expression has no variables.
LinearExpr & operator*=(int64_t factor)
static LinearExpr Sum(absl::Span< const IntVar > vars)
NOLINTEND(google-explicit-constructor)
std::string DebugString(const CpModelProto *proto=nullptr) const
static LinearExpr WeightedSum(absl::Span< const IntVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of variables and coefficients.
static LinearExpr Term(IntVar var, int64_t coefficient)
Constructs var * coefficient.
LinearExpr & operator-=(const LinearExpr &other)
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.
const std::vector< int > & variables() const
Returns the vector of variable indices.
const std::vector< int64_t > & coefficients() const
Returns the vector of coefficients.
LinearExpr & operator+=(const LinearExpr &other)
Operators.
void AddArc(int tail, int head, BoolVar literal)
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
void AddOptionalEvent(LinearExpr time, int64_t level_change, BoolVar is_active)
void AddEvent(LinearExpr time, int64_t level_change)
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
std::string VarDebugString(const CpModelProto &proto, int index)
LinearExpr operator+(const LinearExpr &lhs, const LinearExpr &rhs)
LinearExpr operator-(LinearExpr expr)
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
H AbslHashValue(H h, const IntVar &i)
– ABSL HASHING SUPPORT --------------------------------------------------—
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
Evaluates the value of an linear expression in a solver response.
int NegatedRef(int ref)
Small utility functions to deal with negative variable/literal references.
LinearExpr operator*(LinearExpr expr, int64_t factor)
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
In SWIG mode, we don't want anything besides these top-level includes.
std::optional< int64_t > end