38#ifndef ORTOOLS_SAT_CP_MODEL_H_
39#define ORTOOLS_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"
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;
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;
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;
629 void AddTuple(absl::Span<const int64_t> tuple);
659 void AddOptionalEvent(
LinearExpr time, int64_t level_change,
679 void AddTransition(
int tail,
int head, int64_t transition_label);
736 void SetName(absl::string_view name);
774 int64_t size,
BoolVar presence);
792 Constraint AddAtLeastOne(absl::Span<const BoolVar> literals);
795 Constraint AddAtMostOne(absl::Span<const BoolVar> literals);
798 Constraint AddExactlyOne(absl::Span<const BoolVar> literals);
801 Constraint AddBoolAnd(absl::Span<const BoolVar> literals);
804 Constraint AddBoolXor(absl::Span<const BoolVar> literals);
813 absl::Span<const BoolVar>
rhs) {
839 Constraint AddAllDifferent(absl::Span<const IntVar> vars);
842 Constraint AddAllDifferent(absl::Span<const LinearExpr> exprs);
845 Constraint AddAllDifferent(std::initializer_list<LinearExpr> exprs);
849 absl::Span<const IntVar> variables,
854 absl::Span<const LinearExpr> expressions,
859 std::initializer_list<LinearExpr> expressions,
911 absl::Span<const LinearExpr> expressions);
916 TableConstraint AddAllowedAssignments(absl::Span<const IntVar> variables);
922 std::initializer_list<LinearExpr> expressions);
935 absl::Span<const LinearExpr> expression);
940 TableConstraint AddForbiddenAssignments(absl::Span<const IntVar> variables);
946 std::initializer_list<LinearExpr> expressions);
953 Constraint AddInverseConstraint(absl::Span<const IntVar> variables,
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);
1079 Constraint AddNoOverlap(absl::Span<const IntervalVar> vars);
1109 void ClearObjective();
1112 bool HasObjective()
const;
1115 void AddDecisionStrategy(
1116 absl::Span<const IntVar> variables,
1121 void AddDecisionStrategy(
1122 absl::Span<const BoolVar> variables,
1127 void AddDecisionStrategy(
1128 absl::Span<const LinearExpr> expressions,
1133 void AddDecisionStrategy(
1134 std::initializer_list<LinearExpr> expressions,
1148 void AddAssumption(
BoolVar lit);
1151 void AddAssumptions(absl::Span<const BoolVar> literals);
1154 void ClearAssumptions();
1156 const CpModelProto& Build()
const {
return cp_model_; }
1157 const CpModelProto& Proto()
const {
return cp_model_; }
1158 CpModelProto* MutableProto() {
return &cp_model_; }
1161 bool ExportToFile(absl::string_view filename)
const;
1164 CpModelBuilder Clone()
const;
1167 BoolVar GetBoolVarFromProtoIndex(
int index);
1170 IntVar GetIntVarFromProtoIndex(
int index);
1173 IntervalVar GetIntervalVarFromProtoIndex(
int index);
1176 friend class CumulativeConstraint;
1177 friend class ReservoirConstraint;
1178 friend class IntervalVar;
1179 friend class IntVar;
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);
1198 void FillLinearTerms(
const LinearExpr&
left,
const LinearExpr& right,
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
*Adds a transitions to the automaton void AddTransition(int tail, int head, int64_t transition_label)
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)
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
*Creates an integer variable with the given domain IntVar NewIntVar(const Domain &domain)
*Creates an interval variable from affine expressions IntervalVar NewIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end)
*It is sometime convenient when building a model to create a bunch of *variables that will later be fixed Instead of doing these functions modify directly the *underlying variable domain *note this ignore completely the original variable domain and just fix *the given variable to the given value
*Creates an optional interval variable from affine expressions and a *Boolean variable IntervalVar NewOptionalIntervalVar(const LinearExpr &start, const LinearExpr &size, const LinearExpr &end, BoolVar presence)
*Creates an always true Boolean variable *If this is called multiple the same variable will always be *returned BoolVar TrueVar()
*Same as AddBoolOr(). Sum literals >
*Creates a Boolean variable BoolVar NewBoolVar()
*Creates an always false Boolean variable *If this is called multiple the same variable will always be *returned BoolVar FalseVar()
*Adds left right Constraint AddGreaterThan(const LinearExpr &left, const LinearExpr &right)
*Sets the name of the model void SetName(absl::string_view name)
*It is sometime convenient when building a model to create a bunch of *variables that will later be fixed Instead of doing these functions modify directly the *underlying variable domain *note this ignore completely the original variable domain and just fix *the given variable to the given even if it was outside the given *variable domain You can still use AddEquality() if this is not what you *want. void FixVariable(IntVar var
*Adds absl::Span< const BoolVar > rhs
*Creates an interval variable with a fixed size IntervalVar NewFixedSizeIntervalVar(const LinearExpr &start, int64_t size)
*At most one literal is true Sum literals<=1. Constraint AddAtMostOne(absl::Span< const BoolVar > literals); *Exactly one literal is true. Sum literals==1. Constraint AddExactlyOne(absl::Span< const BoolVar > literals); *Adds the constraint that all literals must be true. Constraint AddBoolAnd(absl::Span< const BoolVar > literals); *Adds the constraint that an odd number of literals is true. Constraint AddBoolXor(absl::Span< const BoolVar > literals); *Adds a=> b Constraint AddImplication(BoolVar a, BoolVar b)
*Creates an optional interval variable with a fixed size IntervalVar NewOptionalFixedSizeIntervalVar(const LinearExpr &start, int64_t size, BoolVar presence)
void FixVariable(BoolVar var, bool value)
*Adds a LinearExpr demand
friend class CpModelBuilder
DecisionStrategyProto_DomainReductionStrategy DomainReductionStrategy
DecisionStrategyProto_VariableSelectionStrategy VariableSelectionStrategy
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.
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)
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
Cast IntVar -> BoolVar.
::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)
Constructs the sum of a list of variables.
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)
void AddArc(int tail, int head, BoolVar literal)
friend class CpModelBuilder
*Adds a IntervalVar y_coordinate
friend class CpModelBuilder
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.
friend class CpModelBuilder
std::string VarDebugString(const CpModelProto &proto, int index)
LinearExpr operator+(const LinearExpr &lhs, const LinearExpr &rhs)
int64_t SolutionIntegerValue(const CpSolverResponse &r, const LinearExpr &expr)
LinearExpr operator-(LinearExpr expr)
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
void AddReservoirConstraint(absl::Span< const Literal > enforcement_literals, absl::Span< const AffineExpression > times, absl::Span< const AffineExpression > deltas, absl::Span< const Literal > presences, int64_t min_level, int64_t max_level, Model *model)
H AbslHashValue(H h, const IntVar &i)
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
LinearExpr operator*(LinearExpr expr, int64_t factor)
ClosedInterval::Iterator end(ClosedInterval interval)