OR-Tools  8.2
CpModelBuilder

Detailed Description

Wrapper class around the cp_model proto.

This class provides two types of methods:

  • NewXXX to create integer, boolean, or interval variables.
  • AddXXX to create new constraints and add them to the model.

Definition at line 599 of file cp_model.h.

Public Member Functions

IntVar NewIntVar (const Domain &domain)
 Creates an integer variable with the given domain. More...
 
BoolVar NewBoolVar ()
 Creates a Boolean variable. More...
 
IntVar NewConstant (int64 value)
 Creates a constant variable. More...
 
BoolVar TrueVar ()
 Creates an always true Boolean variable. More...
 
BoolVar FalseVar ()
 Creates an always false Boolean variable. More...
 
IntervalVar NewIntervalVar (IntVar start, IntVar size, IntVar end)
 Creates an interval variable. More...
 
IntervalVar NewOptionalIntervalVar (IntVar start, IntVar size, IntVar end, BoolVar presence)
 Creates an optional interval variable. More...
 
Constraint AddBoolOr (absl::Span< const BoolVar > literals)
 Adds the constraint that at least one of the literals must be true. More...
 
Constraint AddBoolAnd (absl::Span< const BoolVar > literals)
 Adds the constraint that all literals must be true. More...
 
Constraint AddBoolXor (absl::Span< const BoolVar > literals)
 Adds the constraint that a odd number of literal is true. More...
 
Constraint AddImplication (BoolVar a, BoolVar b)
 Adds a => b. More...
 
Constraint AddEquality (const LinearExpr &left, const LinearExpr &right)
 Adds left == right. More...
 
Constraint AddGreaterOrEqual (const LinearExpr &left, const LinearExpr &right)
 Adds left >= right. More...
 
Constraint AddGreaterThan (const LinearExpr &left, const LinearExpr &right)
 Adds left > right. More...
 
Constraint AddLessOrEqual (const LinearExpr &left, const LinearExpr &right)
 Adds left <= right. More...
 
Constraint AddLessThan (const LinearExpr &left, const LinearExpr &right)
 Adds left < right. More...
 
Constraint AddLinearConstraint (const LinearExpr &expr, const Domain &domain)
 Adds expr in domain. More...
 
Constraint AddNotEqual (const LinearExpr &left, const LinearExpr &right)
 Adds left != right. More...
 
Constraint AddAllDifferent (absl::Span< const IntVar > vars)
 this constraint forces all variables to have different values. More...
 
Constraint AddVariableElement (IntVar index, absl::Span< const IntVar > variables, IntVar target)
 Adds the element constraint: variables[index] == target. More...
 
Constraint AddElement (IntVar index, absl::Span< const int64 > values, IntVar target)
 Adds the element constraint: values[index] == target. More...
 
CircuitConstraint AddCircuitConstraint ()
 Adds a circuit constraint. More...
 
MultipleCircuitConstraint AddMultipleCircuitConstraint ()
 Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint. More...
 
TableConstraint AddAllowedAssignments (absl::Span< const IntVar > vars)
 Adds an allowed assignments constraint. More...
 
TableConstraint AddForbiddenAssignments (absl::Span< const IntVar > vars)
 Adds an forbidden assignments constraint. More...
 
Constraint AddInverseConstraint (absl::Span< const IntVar > variables, absl::Span< const IntVar > inverse_variables)
 An inverse constraint. More...
 
ReservoirConstraint AddReservoirConstraint (int64 min_level, int64 max_level)
 Adds a reservoir constraint with optional refill/emptying events. More...
 
AutomatonConstraint AddAutomaton (absl::Span< const IntVar > transition_variables, int starting_state, absl::Span< const int > final_states)
 An automaton constraint/. More...
 
Constraint AddMinEquality (IntVar target, absl::Span< const IntVar > vars)
 Adds target == min(vars). More...
 
Constraint AddLinMinEquality (const LinearExpr &target, absl::Span< const LinearExpr > exprs)
 Adds target == min(exprs). More...
 
Constraint AddMaxEquality (IntVar target, absl::Span< const IntVar > vars)
 Adds target == max(vars). More...
 
Constraint AddLinMaxEquality (const LinearExpr &target, absl::Span< const LinearExpr > exprs)
 Adds target == max(exprs). More...
 
Constraint AddDivisionEquality (IntVar target, IntVar numerator, IntVar denominator)
 Adds target = num / denom (integer division rounded towards 0). More...
 
Constraint AddAbsEquality (IntVar target, IntVar var)
 Adds target == abs(var). More...
 
Constraint AddModuloEquality (IntVar target, IntVar var, IntVar mod)
 Adds target = var % mod. More...
 
Constraint AddProductEquality (IntVar target, absl::Span< const IntVar > vars)
 Adds target == prod(vars). More...
 
Constraint AddNoOverlap (absl::Span< const IntervalVar > vars)
 Adds a no-overlap constraint that ensures that all present intervals do not overlap in time. More...
 
NoOverlap2DConstraint AddNoOverlap2D ()
 The no_overlap_2d constraint prevents a set of boxes from overlapping. More...
 
CumulativeConstraint AddCumulative (IntVar capacity)
 The cumulative constraint. More...
 
void Minimize (const LinearExpr &expr)
 Adds a linear minimization objective. More...
 
void Maximize (const LinearExpr &expr)
 Adds a linear maximization objective. More...
 
void ScaleObjectiveBy (double scaling)
 Sets scaling of the objective. More...
 
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. More...
 
void AddDecisionStrategy (absl::Span< const BoolVar > variables, DecisionStrategyProto::VariableSelectionStrategy var_strategy, DecisionStrategyProto::DomainReductionStrategy domain_strategy)
 Adds a decision strategy on a list of boolean variables. More...
 
void AddHint (IntVar var, int64 value)
 Adds hinting to a variable. More...
 
void ClearHints ()
 Remove all hints. More...
 
void AddAssumption (BoolVar lit)
 Adds a literal to the model as assumptions. More...
 
void AddAssumptions (absl::Span< const BoolVar > literals)
 Adds multiple literals to the model as assumptions. More...
 
void ClearAssumptions ()
 Remove all assumptions from the model. More...
 
const CpModelProto & Build () const
 
const CpModelProto & Proto () const
 
CpModelProto * MutableProto ()
 
void CopyFrom (const CpModelProto &model_proto)
 Replace the current model with the one from the given proto. More...
 
BoolVar GetBoolVarFromProtoIndex (int index)
 Returns the Boolean variable from its index in the proto. More...
 
IntVar GetIntVarFromProtoIndex (int index)
 Returns the integer variable from its index in the proto. More...
 
IntervalVar GetIntervalVarFromProtoIndex (int index)
 Returns the interval variable from its index in the proto. More...
 

Member Function Documentation

◆ AddAbsEquality()

Constraint AddAbsEquality ( IntVar  target,
IntVar  var 
)

Adds target == abs(var).

Definition at line 692 of file cp_model.cc.

◆ AddAllDifferent()

Constraint AddAllDifferent ( absl::Span< const IntVar vars)

this constraint forces all variables to have different values.

Definition at line 535 of file cp_model.cc.

◆ AddAllowedAssignments()

TableConstraint AddAllowedAssignments ( absl::Span< const IntVar vars)

Adds an allowed assignments constraint.

An AllowedAssignments constraint is a constraint on an array of variables that forces, when all variables are fixed to a single value, that the corresponding list of values is equal to one of the tuple added to the constraint.

It returns a table constraint that allows adding tuples incrementally after construction,

Definition at line 574 of file cp_model.cc.

◆ AddAssumption()

void AddAssumption ( BoolVar  lit)

Adds a literal to the model as assumptions.

Definition at line 803 of file cp_model.cc.

◆ AddAssumptions()

void AddAssumptions ( absl::Span< const BoolVar literals)

Adds multiple literals to the model as assumptions.

Definition at line 807 of file cp_model.cc.

◆ AddAutomaton()

AutomatonConstraint AddAutomaton ( absl::Span< const IntVar transition_variables,
int  starting_state,
absl::Span< const int >  final_states 
)

An automaton constraint/.

An automaton constraint takes a list of variables (of size n), an initial state, a set of final states, and a set of transitions. A transition is a triplet ('tail', 'head', 'label'), where 'tail' and 'head' are states, and 'label' is the label of an arc from 'head' to 'tail', corresponding to the value of one variable in the list of variables.

This automaton will be unrolled into a flow with n + 1 phases. Each phase contains the possible states of the automaton. The first state contains the initial state. The last phase contains the final states.

Between two consecutive phases i and i + 1, the automaton creates a set of arcs. For each transition (tail, head, label), it will add an arc from the state 'tail' of phase i and the state 'head' of phase i + 1. This arc labeled by the value 'label' of the variables 'variables[i]'. That is, this arc can only be selected if 'variables[i]' is assigned the value 'label'. A feasible solution of this constraint is an assignment of variables such that, starting from the initial state in phase 0, there is a path labeled by the values of the variables that ends in one of the final states in the final phase.

It returns an AutomatonConstraint that allows adding transition incrementally after construction.

Definition at line 615 of file cp_model.cc.

◆ AddBoolAnd()

Constraint AddBoolAnd ( absl::Span< const BoolVar literals)

Adds the constraint that all literals must be true.

Definition at line 423 of file cp_model.cc.

◆ AddBoolOr()

Constraint AddBoolOr ( absl::Span< const BoolVar literals)

Adds the constraint that at least one of the literals must be true.

Definition at line 415 of file cp_model.cc.

◆ AddBoolXor()

Constraint AddBoolXor ( absl::Span< const BoolVar literals)

Adds the constraint that a odd number of literal is true.

Definition at line 431 of file cp_model.cc.

◆ AddCircuitConstraint()

CircuitConstraint AddCircuitConstraint ( )

Adds a circuit constraint.

The circuit constraint is defined on a graph where the arc presence is controlled by literals. That is the arc is part of the circuit of its corresponding literal is assigned to true.

For now, we ignore node indices with no incident arc. All the other nodes must have exactly one incoming and one outgoing selected arc (i.e. literal at true). All the selected arcs that are not self-loops must form a single circuit.

It returns a circuit constraint that allows adding arcs incrementally after construction.

Definition at line 566 of file cp_model.cc.

◆ AddCumulative()

CumulativeConstraint AddCumulative ( IntVar  capacity)

The cumulative constraint.

It ensures that for any integer point, the sum of the demands of the intervals containing that point does not exceed the capacity.

Definition at line 733 of file cp_model.cc.

◆ AddDecisionStrategy() [1/2]

void AddDecisionStrategy ( absl::Span< const BoolVar variables,
DecisionStrategyProto::VariableSelectionStrategy  var_strategy,
DecisionStrategyProto::DomainReductionStrategy  domain_strategy 
)

Adds a decision strategy on a list of boolean variables.

Definition at line 781 of file cp_model.cc.

◆ AddDecisionStrategy() [2/2]

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.

Definition at line 769 of file cp_model.cc.

◆ AddDivisionEquality()

Constraint AddDivisionEquality ( IntVar  target,
IntVar  numerator,
IntVar  denominator 
)

Adds target = num / denom (integer division rounded towards 0).

Definition at line 682 of file cp_model.cc.

◆ AddElement()

Constraint AddElement ( IntVar  index,
absl::Span< const int64 values,
IntVar  target 
)

Adds the element constraint: values[index] == target.

Definition at line 554 of file cp_model.cc.

◆ AddEquality()

Constraint AddEquality ( const LinearExpr left,
const LinearExpr right 
)

Adds left == right.

Definition at line 456 of file cp_model.cc.

◆ AddForbiddenAssignments()

TableConstraint AddForbiddenAssignments ( absl::Span< const IntVar vars)

Adds an forbidden assignments constraint.

A ForbiddenAssignments constraint is a constraint on an array of variables where the list of impossible combinations is provided in the tuples added to the constraint.

It returns a table constraint that allows adding tuples incrementally after construction,

Definition at line 583 of file cp_model.cc.

◆ AddGreaterOrEqual()

Constraint AddGreaterOrEqual ( const LinearExpr left,
const LinearExpr right 
)

Adds left >= right.

Definition at line 466 of file cp_model.cc.

◆ AddGreaterThan()

Constraint AddGreaterThan ( const LinearExpr left,
const LinearExpr right 
)

Adds left > right.

Definition at line 486 of file cp_model.cc.

◆ AddHint()

void AddHint ( IntVar  var,
int64  value 
)

Adds hinting to a variable.

Definition at line 793 of file cp_model.cc.

◆ AddImplication()

Constraint AddImplication ( BoolVar  a,
BoolVar  b 
)
inline

Adds a => b.

Definition at line 633 of file cp_model.h.

◆ AddInverseConstraint()

Constraint AddInverseConstraint ( absl::Span< const IntVar variables,
absl::Span< const IntVar inverse_variables 
)

An inverse constraint.

It enforces that if 'variables[i]' is assigned a value 'j', then inverse_variables[j] is assigned a value 'i'. And vice versa.

Definition at line 593 of file cp_model.cc.

◆ AddLessOrEqual()

Constraint AddLessOrEqual ( const LinearExpr left,
const LinearExpr right 
)

Adds left <= right.

Definition at line 476 of file cp_model.cc.

◆ AddLessThan()

Constraint AddLessThan ( const LinearExpr left,
const LinearExpr right 
)

Adds left < right.

Definition at line 496 of file cp_model.cc.

◆ AddLinearConstraint()

Constraint AddLinearConstraint ( const LinearExpr expr,
const Domain domain 
)

Adds expr in domain.

Definition at line 506 of file cp_model.cc.

◆ AddLinMaxEquality()

Constraint AddLinMaxEquality ( const LinearExpr target,
absl::Span< const LinearExpr exprs 
)

Adds target == max(exprs).

Definition at line 671 of file cp_model.cc.

◆ AddLinMinEquality()

Constraint AddLinMinEquality ( const LinearExpr target,
absl::Span< const LinearExpr exprs 
)

Adds target == min(exprs).

Definition at line 650 of file cp_model.cc.

◆ AddMaxEquality()

Constraint AddMaxEquality ( IntVar  target,
absl::Span< const IntVar vars 
)

Adds target == max(vars).

Definition at line 661 of file cp_model.cc.

◆ AddMinEquality()

Constraint AddMinEquality ( IntVar  target,
absl::Span< const IntVar vars 
)

Adds target == min(vars).

Definition at line 629 of file cp_model.cc.

◆ AddModuloEquality()

Constraint AddModuloEquality ( IntVar  target,
IntVar  var,
IntVar  mod 
)

Adds target = var % mod.

Definition at line 701 of file cp_model.cc.

◆ AddMultipleCircuitConstraint()

MultipleCircuitConstraint AddMultipleCircuitConstraint ( )

Adds a multiple circuit constraint, aka the "VRP" (Vehicle Routing Problem) constraint.

The direct graph where arc #i (from tails[i] to head[i]) is present iff literals[i] is true must satisfy this set of properties:

  • #incoming arcs == 1 except for node 0.
  • #outgoing arcs == 1 except for node 0.
  • for node zero, #incoming arcs == #outgoing arcs.
  • There are no duplicate arcs.
  • Self-arcs are allowed except for node 0.
  • There is no cycle in this graph, except through node 0.

Definition at line 570 of file cp_model.cc.

◆ AddNoOverlap()

Constraint AddNoOverlap ( absl::Span< const IntervalVar vars)

Adds a no-overlap constraint that ensures that all present intervals do not overlap in time.

Definition at line 720 of file cp_model.cc.

◆ AddNoOverlap2D()

NoOverlap2DConstraint AddNoOverlap2D ( )

The no_overlap_2d constraint prevents a set of boxes from overlapping.

Definition at line 729 of file cp_model.cc.

◆ AddNotEqual()

Constraint AddNotEqual ( const LinearExpr left,
const LinearExpr right 
)

Adds left != right.

Definition at line 523 of file cp_model.cc.

◆ AddProductEquality()

Constraint AddProductEquality ( IntVar  target,
absl::Span< const IntVar vars 
)

Adds target == prod(vars).

Definition at line 710 of file cp_model.cc.

◆ AddReservoirConstraint()

ReservoirConstraint AddReservoirConstraint ( int64  min_level,
int64  max_level 
)

Adds a reservoir constraint with optional refill/emptying events.

Maintain a reservoir level within bounds. The water level starts at 0, and at any time >= 0, it must be within min_level, and max_level. Furthermore, this constraints expect all times variables to be >= 0. Given an event (time, demand, active), if active is true, and if time is assigned a value t, then the level of the reservoir changes by demand (which is constant) at time t.

Note that level_min can be > 0, or level_max can be < 0. It just forces some demands to be executed at time 0 to make sure that we are within those bounds with the executed demands. Therefore, at any time t >= 0: sum(demands[i] * actives[i] if times[i] <= t) in [min_level, max_level]

It returns a ReservoirConstraint that allows adding optional and non optional events incrementally after construction.

Definition at line 607 of file cp_model.cc.

◆ AddVariableElement()

Constraint AddVariableElement ( IntVar  index,
absl::Span< const IntVar variables,
IntVar  target 
)

Adds the element constraint: variables[index] == target.

Definition at line 543 of file cp_model.cc.

◆ Build()

const CpModelProto& Build ( ) const
inline

Definition at line 876 of file cp_model.h.

◆ ClearAssumptions()

void ClearAssumptions ( )

Remove all assumptions from the model.

Definition at line 813 of file cp_model.cc.

◆ ClearHints()

void ClearHints ( )

Remove all hints.

Definition at line 799 of file cp_model.cc.

◆ CopyFrom()

void CopyFrom ( const CpModelProto &  model_proto)

Replace the current model with the one from the given proto.

Definition at line 817 of file cp_model.cc.

◆ FalseVar()

BoolVar FalseVar ( )

Creates an always false Boolean variable.

Definition at line 393 of file cp_model.cc.

◆ GetBoolVarFromProtoIndex()

BoolVar GetBoolVarFromProtoIndex ( int  index)

Returns the Boolean variable from its index in the proto.

Definition at line 831 of file cp_model.cc.

◆ GetIntervalVarFromProtoIndex()

IntervalVar GetIntervalVarFromProtoIndex ( int  index)

Returns the interval variable from its index in the proto.

Definition at line 853 of file cp_model.cc.

◆ GetIntVarFromProtoIndex()

IntVar GetIntVarFromProtoIndex ( int  index)

Returns the integer variable from its index in the proto.

Definition at line 847 of file cp_model.cc.

◆ Maximize()

void Maximize ( const LinearExpr expr)

Adds a linear maximization objective.

Definition at line 751 of file cp_model.cc.

◆ Minimize()

void Minimize ( const LinearExpr expr)

Adds a linear minimization objective.

Definition at line 740 of file cp_model.cc.

◆ MutableProto()

CpModelProto* MutableProto ( )
inline

Definition at line 879 of file cp_model.h.

◆ NewBoolVar()

BoolVar NewBoolVar ( )

Creates a Boolean variable.

Definition at line 377 of file cp_model.cc.

◆ NewConstant()

IntVar NewConstant ( int64  value)

Creates a constant variable.

Definition at line 385 of file cp_model.cc.

◆ NewIntervalVar()

IntervalVar NewIntervalVar ( IntVar  start,
IntVar  size,
IntVar  end 
)

Creates an interval variable.

Definition at line 397 of file cp_model.cc.

◆ NewIntVar()

IntVar NewIntVar ( const Domain domain)

Creates an integer variable with the given domain.

Definition at line 367 of file cp_model.cc.

◆ NewOptionalIntervalVar()

IntervalVar NewOptionalIntervalVar ( IntVar  start,
IntVar  size,
IntVar  end,
BoolVar  presence 
)

Creates an optional interval variable.

Definition at line 402 of file cp_model.cc.

◆ Proto()

const CpModelProto& Proto ( ) const
inline

Definition at line 878 of file cp_model.h.

◆ ScaleObjectiveBy()

void ScaleObjectiveBy ( double  scaling)

Sets scaling of the objective.

It must be called after Minimize() or Maximize()).

scaling must be > 0.0.

Definition at line 763 of file cp_model.cc.

◆ TrueVar()

BoolVar TrueVar ( )

Creates an always true Boolean variable.

Definition at line 389 of file cp_model.cc.


The documentation for this class was generated from the following files: