38#ifndef OR_TOOLS_SAT_CP_MODEL_H_
39#define OR_TOOLS_SAT_CP_MODEL_H_
45#include "absl/container/flat_hash_map.h"
46#include "absl/types/span.h"
84 return other.cp_model_ == cp_model_ && other.index_ == index_;
89 return other.cp_model_ != cp_model_ || other.index_ != index_;
111 int index()
const {
return index_; }
171 return other.cp_model_ == cp_model_ && other.index_ == index_;
176 return other.cp_model_ != cp_model_ || other.index_ != index_;
193 int index()
const {
return index_; }
284 absl::Span<const int64_t> coeffs);
291 absl::Span<const int64_t> coeffs);
296 const std::vector<IntVar>&
variables()
const {
return variables_; }
299 const std::vector<int64_t>&
coefficients()
const {
return coefficients_; }
308 int64_t
Value()
const;
314 std::vector<IntVar> variables_;
315 std::vector<int64_t> coefficients_;
316 int64_t constant_ = 0;
350 std::string
Name()
const;
373 return other.cp_model_ == cp_model_ && other.index_ == index_;
378 return other.cp_model_ != cp_model_ || other.index_ != index_;
395 int index()
const {
return index_; }
448 const std::string&
Name()
const;
518 void AddTuple(absl::Span<const int64_t> tuple);
653 int64_t size,
BoolVar presence);
695 absl::Span<const IntVar> variables,
766 absl::Span<const IntVar> inverse_variables);
817 absl::Span<const IntVar> transition_variables,
int starting_state,
818 absl::Span<const int> final_states);
825 absl::Span<const LinearExpr> exprs);
832 absl::Span<const LinearExpr> exprs);
882 absl::Span<const IntVar> variables,
888 absl::Span<const BoolVar> variables,
932 void LinearExprToProto(
const LinearExpr& expr,
942 int IndexFromConstant(int64_t
value);
949 int GetOrCreateIntegerIndex(
int index);
955 absl::flat_hash_map<int64_t, int> constant_to_index_map_;
956 absl::flat_hash_map<int, int> bool_to_integer_index_map_;
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.
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
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.
bool operator!=(const BoolVar &other) const
Dis-Equality test.
bool operator==(const BoolVar &other) const
Equality test with another boolvar.
int index() const
Returns the index of the variable in the model.
BoolVar Not() const
Returns the logical negation of the current Boolean variable.
const IntegerVariableProto & Proto() const
Returns the underlying protobuf object (useful for testing).
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).
::operations_research::sat::IntervalConstraintProto * mutable_interval()
const ::operations_research::sat::IntervalConstraintProto & interval() const
Wrapper class around the cp_model proto.
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 ClearHints()
Remove all hints.
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.
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.
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
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
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.
void AddDemand(IntervalVar interval, IntVar demand)
Adds a pair (interval, demand) to the constraint.
BoolVar ToBoolVar() const
Cast IntVar -> BoolVar.
IntegerVariableProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
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.
std::string DebugString() const
Returns a debug string.
bool operator!=(const IntVar &other) const
Difference test with anpther IntVar.
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).
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.
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).
const std::string & name() const
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).
const IntervalConstraintProto & Proto() const
Returns the underlying protobuf object (useful for testing).
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.
IntervalConstraintProto * MutableProto() const
Returns the mutable underlying protobuf object (useful for model edition).
LinearExpr EndExpr() const
Returns the end linear expression.
IntervalVar()
Default ctor.
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.
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 Sum(absl::Span< const IntVar > vars)
Constructs the sum of a list of variables.
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.
static LinearExpr BooleanSum(absl::Span< const BoolVar > vars)
Constructs the sum of a list of Booleans.
std::string DebugString() const
Debug string.
int64_t constant() const
Returns the constant term.
static LinearExpr BooleanScalProd(absl::Span< const BoolVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of Booleans and coefficients.
static LinearExpr ScalProd(absl::Span< const IntVar > vars, absl::Span< const int64_t > coeffs)
Constructs the scalar product of variables and coefficients.
LinearExpr & AddConstant(int64_t value)
Adds a constant value to the linear expression.
const std::vector< IntVar > & variables() const
Returns the vector of variables.
LinearExpr & AddTerm(IntVar var, int64_t coeff)
Adds a term (var * coeff) to the linear expression.
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 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.
void AddTuple(absl::Span< const int64_t > tuple)
Adds a tuple of possible values to the constraint.
CpModelProto const * model_proto
DecisionStrategyProto_DomainReductionStrategy
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
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.
bool SolutionBooleanValue(const CpSolverResponse &r, BoolVar x)
Evaluates the value of a Boolean literal in a solver response.
DecisionStrategyProto_VariableSelectionStrategy
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.
Collection of objects used to extend the Constraint Solver library.