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_; }
308 std::string
DebugString(
const CpModelProto* proto =
nullptr)
const;
311 std::vector<int> variables_;
312 std::vector<int64_t> coefficients_;
313 int64_t constant_ = 0;
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_; }
418 std::string
DebugString(
const CpModelProto* proto =
nullptr)
const;
421 std::vector<int> variables_;
422 std::vector<double> coefficients_;
423 double constant_ = 0;
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);
679 void AddTransition(
int tail,
int head, int64_t transition_label);
736 void SetName(absl::string_view name);
774 int64_t size,
BoolVar presence);
813 absl::Span<const BoolVar> rhs) {
849 absl::Span<const IntVar> variables,
854 absl::Span<const LinearExpr> expressions,
859 std::initializer_list<LinearExpr> expressions,
911 absl::Span<const LinearExpr> expressions);
922 std::initializer_list<LinearExpr> expressions);
935 absl::Span<const LinearExpr> expression);
946 std::initializer_list<LinearExpr> expressions);
954 absl::Span<const IntVar> inverse_variables);
1005 absl::Span<const LinearExpr> transition_expressions,
int starting_state,
1006 absl::Span<const int> final_states);
1012 absl::Span<const IntVar> transition_variables,
int starting_state,
1013 absl::Span<const int> final_states);
1019 std::initializer_list<LinearExpr> transition_expressions,
1020 int starting_state, absl::Span<const int> final_states);
1024 absl::Span<const IntVar> vars);
1028 absl::Span<const LinearExpr> exprs);
1032 std::initializer_list<LinearExpr> exprs);
1036 absl::Span<const IntVar> vars);
1040 absl::Span<const LinearExpr> exprs);
1044 std::initializer_list<LinearExpr> exprs);
1060 absl::Span<const LinearExpr> exprs);
1064 absl::Span<const IntVar> vars);
1068 std::initializer_list<LinearExpr> exprs);
1116 absl::Span<const IntVar> variables,
1117 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1118 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1122 absl::Span<const BoolVar> variables,
1123 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1124 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1128 absl::Span<const LinearExpr> expressions,
1129 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1130 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1134 std::initializer_list<LinearExpr> expressions,
1135 DecisionStrategyProto::VariableSelectionStrategy var_strategy,
1136 DecisionStrategyProto::DomainReductionStrategy domain_strategy);
1156 const CpModelProto&
Build()
const {
return cp_model_; }
1157 const CpModelProto&
Proto()
const {
return cp_model_; }
1182 void ResetAndImport(
const CpModelProto& model_proto);
1185 LinearExpressionProto LinearExprToProto(
const LinearExpr& expr,
1186 bool negate =
false);
1189 int IndexFromConstant(int64_t value);
1196 int GetOrCreateIntegerIndex(
int index);
1199 LinearConstraintProto* proto);
1201 CpModelProto cp_model_;
1202 absl::flat_hash_map<int64_t, int> constant_to_index_map_;
1203 absl::flat_hash_map<int, int> bool_to_integer_index_map_;
1236 return std::move(lhs);
1240 return std::move(rhs);
1243 if (lhs.variables().size() < rhs.variables().size()) {
1244 rhs += std::move(lhs);
1245 return std::move(rhs);
1247 lhs += std::move(rhs);
1248 return std::move(lhs);
1252inline LinearExpr
operator-(
const LinearExpr& lhs,
const LinearExpr& rhs) {
1253 LinearExpr temp(lhs);
1259 return std::move(lhs);
1264 return std::move(rhs);
1267 lhs -= std::move(rhs);
1268 return std::move(lhs);
1293inline DoubleLinearExpr
operator+(DoubleLinearExpr&& lhs,
1294 const DoubleLinearExpr& rhs) {
1296 return std::move(lhs);
1301 return std::move(rhs);
1305 if (lhs.variables().size() < rhs.variables().size()) {
1306 rhs += std::move(lhs);
1307 return std::move(rhs);
1309 lhs += std::move(rhs);
1310 return std::move(lhs);
1329inline DoubleLinearExpr
operator-(DoubleLinearExpr&& lhs,
1330 const DoubleLinearExpr& rhs) {
1332 return std::move(lhs);
1338 return std::move(rhs);
1340inline DoubleLinearExpr
operator-(DoubleLinearExpr&& lhs,
1341 DoubleLinearExpr&& rhs) {
1342 lhs -= std::move(rhs);
1343 return std::move(lhs);
1350inline DoubleLinearExpr
operator-(
double lhs, DoubleLinearExpr expr) {
friend class CpModelBuilder
void AddTransition(int tail, int head, int64_t transition_label)
Adds a transitions to the automaton.
friend class DoubleLinearExpr
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.
friend class CircuitConstraint
BoolVar operator~() const
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
friend class CpModelBuilder
friend class ReservoirConstraint
friend class MultipleCircuitConstraint
std::string DebugString() const
bool operator==(const BoolVar &other) const
void AddArc(int tail, int head, BoolVar literal)
friend class CpModelBuilder
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).
friend class CpModelBuilder
bool HasObjective() const
Checks whether the model contains an objective.
const CpModelProto & Build() const
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.
TableConstraint AddAllowedAssignments(absl::Span< const LinearExpr > expressions)
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.
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.
AutomatonConstraint AddAutomaton(absl::Span< const LinearExpr > transition_expressions, int starting_state, absl::Span< const int > final_states)
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.
MultipleCircuitConstraint AddMultipleCircuitConstraint()
Constraint AddNoOverlap(absl::Span< const IntervalVar > vars)
const CpModelProto & Proto() const
BoolVar NewBoolVar()
Creates a Boolean variable.
friend class CumulativeConstraint
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.
TableConstraint AddForbiddenAssignments(absl::Span< const LinearExpr > expression)
void SetName(absl::string_view name)
Sets the name of the model.
Constraint AddAbsEquality(const LinearExpr &target, const LinearExpr &expr)
Adds target == abs(expr).
friend class ReservoirConstraint
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.
Constraint AddVariableElement(LinearExpr index, absl::Span< const IntVar > variables, LinearExpr target)
Adds the element constraint: variables[index] == target.
IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
Creates an optional interval variable with a fixed size.
Constraint AddElement(LinearExpr index, absl::Span< const LinearExpr > expressions, LinearExpr target)
Adds the element constraint: expressions[index] == target.
void AddDemand(IntervalVar interval, LinearExpr demand)
Adds a pair (interval, demand) to the constraint.
friend class CpModelBuilder
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
friend class DoubleLinearExpr
bool operator!=(const IntVar &other) const
std::string Name() const
Returns the name of the variable (or the empty string if not set).
friend class CumulativeConstraint
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.
friend class CpModelBuilder
friend class ReservoirConstraint
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
friend class CumulativeConstraint
BoolVar PresenceBoolVar() const
friend class NoOverlap2DConstraint
int index() const
Returns the index of the interval constraint in the model.
friend class CpModelBuilder
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)
friend class CpModelBuilder
void AddRectangle(IntervalVar x_coordinate, IntervalVar y_coordinate)
Adds a rectangle (parallel to the axis) to the constraint.
friend class CpModelBuilder
void AddOptionalEvent(LinearExpr time, int64_t level_change, BoolVar is_active)
void AddEvent(LinearExpr time, int64_t level_change)
friend class CpModelBuilder
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
friend class CpModelBuilder
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.