Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ortools.sat.python.cp_model.CpModel Class Reference

Detailed Description

Methods for building a CP model.

Methods beginning with:

* ```new_``` create integer, boolean, or interval variables.
* ```add_``` create new constraints and add them to the model.

Definition at line 306 of file cp_model.py.

Inheritance diagram for ortools.sat.python.cp_model.CpModel:

Public Member Functions

None __init__ (self, Optional[cmh.CpModelProto] model_proto=None)
str name (self)
 name (self, str name)
IntVar new_int_var (self, IntegralT lb, IntegralT ub, str name)
IntVar new_int_var_from_domain (self, sorted_interval_list.Domain domain, str name)
IntVar new_bool_var (self, str name)
IntVar new_constant (self, IntegralT value)
pd.Series new_int_var_series (self, str name, pd.Index index, Union[IntegralT, pd.Series] lower_bounds, Union[IntegralT, pd.Series] upper_bounds)
pd.Series new_bool_var_series (self, str name, pd.Index index)
Constraint add_linear_constraint (self, LinearExprT linear_expr, IntegralT lb, IntegralT ub)
Constraint add_linear_expression_in_domain (self, LinearExprT linear_expr, sorted_interval_list.Domain domain)
Constraint add (self, Union[BoundedLinearExpression, bool, np.bool_] ct)
Constraint add_all_different (self, Iterable[LinearExprT] expressions)
Constraint add_all_different (self, *LinearExprT expressions)
 add_all_different (self, *expressions)
Constraint add_element (self, LinearExprT index, Sequence[LinearExprT] expressions, LinearExprT target)
Constraint add_circuit (self, Sequence[ArcT] arcs)
Constraint add_multiple_circuit (self, Sequence[ArcT] arcs)
Constraint add_allowed_assignments (self, Sequence[LinearExprT] expressions, Iterable[Sequence[IntegralT]] tuples_list)
Constraint add_forbidden_assignments (self, Sequence[LinearExprT] expressions, Iterable[Sequence[IntegralT]] tuples_list)
Constraint add_automaton (self, Sequence[LinearExprT] transition_expressions, IntegralT starting_state, Sequence[IntegralT] final_states, Sequence[tuple[IntegralT, IntegralT, IntegralT]] transition_triples)
Constraint add_inverse (self, Sequence[VariableT] variables, Sequence[VariableT] inverse_variables)
Constraint add_reservoir_constraint (self, Sequence[LinearExprT] times, Sequence[LinearExprT] level_changes, int min_level, int max_level)
Constraint add_reservoir_constraint_with_active (self, Sequence[LinearExprT] times, Sequence[LinearExprT] level_changes, Sequence[LiteralT] actives, int min_level, int max_level)
 add_map_domain (self, IntVar var, Iterable[IntVar] bool_var_array, IntegralT offset=0)
Constraint add_implication (self, LiteralT a, LiteralT b)
Constraint add_bool_or (self, Iterable[LiteralT] literals)
Constraint add_bool_or (self, *LiteralT literals)
 add_bool_or (self, *literals)
Constraint add_at_least_one (self, Iterable[LiteralT] literals)
Constraint add_at_least_one (self, *LiteralT literals)
 add_at_least_one (self, *literals)
Constraint add_at_most_one (self, Iterable[LiteralT] literals)
Constraint add_at_most_one (self, *LiteralT literals)
Constraint add_at_most_one (self, *literals)
Constraint add_exactly_one (self, Iterable[LiteralT] literals)
Constraint add_exactly_one (self, *LiteralT literals)
 add_exactly_one (self, *literals)
Constraint add_bool_and (self, Iterable[LiteralT] literals)
Constraint add_bool_and (self, *LiteralT literals)
 add_bool_and (self, *literals)
Constraint add_bool_xor (self, Iterable[LiteralT] literals)
Constraint add_bool_xor (self, *LiteralT literals)
 add_bool_xor (self, *literals)
Constraint add_min_equality (self, LinearExprT target, Iterable[LinearExprT] expressions)
Constraint add_min_equality (self, LinearExprT target, *LinearExprT expressions)
Constraint add_min_equality (self, target, *expressions)
Constraint add_max_equality (self, LinearExprT target, Iterable[LinearExprT] expressions)
Constraint add_max_equality (self, LinearExprT target, *LinearExprT expressions)
Constraint add_max_equality (self, target, *expressions)
Constraint add_division_equality (self, LinearExprT target, LinearExprT num, LinearExprT denom)
Constraint add_abs_equality (self, LinearExprT target, LinearExprT expr)
Constraint add_modulo_equality (self, LinearExprT target, LinearExprT expr, LinearExprT mod)
Constraint add_multiplication_equality (self, LinearExprT target, *Union[Iterable[LinearExprT], LinearExprT] expressions)
IntervalVar new_interval_var (self, LinearExprT start, LinearExprT size, LinearExprT end, str name)
pd.Series new_interval_var_series (self, str name, pd.Index index, Union[LinearExprT, pd.Series] starts, Union[LinearExprT, pd.Series] sizes, Union[LinearExprT, pd.Series] ends)
IntervalVar new_fixed_size_interval_var (self, LinearExprT start, IntegralT size, str name)
pd.Series new_fixed_size_interval_var_series (self, str name, pd.Index index, Union[LinearExprT, pd.Series] starts, Union[IntegralT, pd.Series] sizes)
IntervalVar new_optional_interval_var (self, LinearExprT start, LinearExprT size, LinearExprT end, LiteralT is_present, str name)
pd.Series new_optional_interval_var_series (self, str name, pd.Index index, Union[LinearExprT, pd.Series] starts, Union[LinearExprT, pd.Series] sizes, Union[LinearExprT, pd.Series] ends, Union[LiteralT, pd.Series] are_present)
IntervalVar new_optional_fixed_size_interval_var (self, LinearExprT start, IntegralT size, LiteralT is_present, str name)
pd.Series new_optional_fixed_size_interval_var_series (self, str name, pd.Index index, Union[LinearExprT, pd.Series] starts, Union[IntegralT, pd.Series] sizes, Union[LiteralT, pd.Series] are_present)
Constraint add_no_overlap (self, Iterable[IntervalVar] intervals)
Constraint add_no_overlap_2d (self, Iterable[IntervalVar] x_intervals, Iterable[IntervalVar] y_intervals)
Constraint add_cumulative (self, Iterable[IntervalVar] intervals, Iterable[LinearExprT] demands, LinearExprT capacity)
"CpModel" clone (self)
 __copy__ (self)
 __deepcopy__ (self, memo)
IntVar get_bool_var_from_proto_index (self, int index)
IntVar get_int_var_from_proto_index (self, int index)
IntervalVar get_interval_var_from_proto_index (self, int index)
str __str__ (self)
cmh.CpModelProto proto (self)
int negated (self, int index)
 minimize (self, ObjLinearExprT obj)
 maximize (self, ObjLinearExprT obj)
bool has_objective (self)
 clear_objective (self)
None add_decision_strategy (self, Iterable[IntVar] variables, cmh.DecisionStrategyProto.VariableSelectionStrategy var_strategy, cmh.DecisionStrategyProto.DomainReductionStrategy domain_strategy)
str model_stats (self)
str validate (self)
bool export_to_file (self, str file)
None remove_all_names (self)
None add_hint (self, IntVar var, int value)
None add_hint (self, BoolVarT literal, bool value)
None add_hint (self, var, value)
 clear_hints (self)
None add_assumption (self, LiteralT lit)
None add_assumptions (self, Iterable[LiteralT] literals)
None clear_assumptions (self)
str Name (self)
None SetName (self, str name)
cmh.CpModelProto Proto (self)

Public Attributes

 model_proto
 proto
str name = name

Protected Member Functions

 _set_objective (self, ObjLinearExprT obj, bool maximize)
None _add_pre_pep8_methods (self)

Constructor & Destructor Documentation

◆ __init__()

None ortools.sat.python.cp_model.CpModel.__init__ ( self,
Optional[cmh.CpModelProto] model_proto = None )

Definition at line 315 of file cp_model.py.

Member Function Documentation

◆ __copy__()

ortools.sat.python.cp_model.CpModel.__copy__ ( self)

Definition at line 1483 of file cp_model.py.

◆ __deepcopy__()

ortools.sat.python.cp_model.CpModel.__deepcopy__ ( self,
memo )

Definition at line 1486 of file cp_model.py.

◆ __str__()

str ortools.sat.python.cp_model.CpModel.__str__ ( self)

Definition at line 1526 of file cp_model.py.

◆ _add_pre_pep8_methods()

None ortools.sat.python.cp_model.CpModel._add_pre_pep8_methods ( self)
protected

Definition at line 1684 of file cp_model.py.

◆ _set_objective()

ortools.sat.python.cp_model.CpModel._set_objective ( self,
ObjLinearExprT obj,
bool maximize )
protected
Sets the objective of the model.

Definition at line 1537 of file cp_model.py.

◆ add()

Constraint ortools.sat.python.cp_model.CpModel.add ( self,
Union[BoundedLinearExpression, bool, np.bool_] ct )
Adds a `BoundedLinearExpression` to the model.

Args:
  ct: A [`BoundedLinearExpression`](#boundedlinearexpression).

Returns:
  An instance of the `Constraint` class.

Raises:
  TypeError: If the `ct` is not a `BoundedLinearExpression` or a Boolean.

Definition at line 509 of file cp_model.py.

◆ add_abs_equality()

Constraint ortools.sat.python.cp_model.CpModel.add_abs_equality ( self,
LinearExprT target,
LinearExprT expr )
Adds `target == Abs(expr)`.

Definition at line 1053 of file cp_model.py.

◆ add_all_different() [1/3]

ortools.sat.python.cp_model.CpModel.add_all_different ( self,
* expressions )
Adds AllDifferent(expressions).

This constraint forces all expressions to have different values.

Args:
  *expressions: simple expressions of the form a * var + constant.

Returns:
  An instance of the `Constraint` class.

Definition at line 537 of file cp_model.py.

◆ add_all_different() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_all_different ( self,
*LinearExprT expressions )

Definition at line 535 of file cp_model.py.

◆ add_all_different() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_all_different ( self,
Iterable[LinearExprT] expressions )

Definition at line 532 of file cp_model.py.

◆ add_allowed_assignments()

Constraint ortools.sat.python.cp_model.CpModel.add_allowed_assignments ( self,
Sequence[LinearExprT] expressions,
Iterable[Sequence[IntegralT]] tuples_list )
Adds AllowedAssignments(expressions, tuples_list).

An AllowedAssignments constraint is a constraint on an array of affine
expressions, which requires that when all expressions are assigned values,
the
resulting array equals one of the  tuples in `tuple_list`.

Args:
  expressions: A list of affine expressions (a * var + b).
  tuples_list: A list of admissible tuples. Each tuple must have the same
    length as the expressions, and the ith value of a tuple corresponds to
    the ith expression.

Returns:
  An instance of the `Constraint` class.

Raises:
  TypeError: If a tuple does not have the same size as the list of
      expressions.
  ValueError: If the array of expressions is empty.

Definition at line 632 of file cp_model.py.

◆ add_assumption()

None ortools.sat.python.cp_model.CpModel.add_assumption ( self,
LiteralT lit )
Adds the literal to the model as assumptions.

Definition at line 1668 of file cp_model.py.

◆ add_assumptions()

None ortools.sat.python.cp_model.CpModel.add_assumptions ( self,
Iterable[LiteralT] literals )
Adds the literals to the model as assumptions.

Definition at line 1672 of file cp_model.py.

◆ add_at_least_one() [1/3]

ortools.sat.python.cp_model.CpModel.add_at_least_one ( self,
* literals )
Same as `add_bool_or`: `sum(literals) >= 1`.

Definition at line 949 of file cp_model.py.

◆ add_at_least_one() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_at_least_one ( self,
*LiteralT literals )

Definition at line 947 of file cp_model.py.

◆ add_at_least_one() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_at_least_one ( self,
Iterable[LiteralT] literals )

Definition at line 944 of file cp_model.py.

◆ add_at_most_one() [1/3]

Constraint ortools.sat.python.cp_model.CpModel.add_at_most_one ( self,
* literals )
Adds `AtMostOne(literals)`: `sum(literals) <= 1`.

Definition at line 961 of file cp_model.py.

◆ add_at_most_one() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_at_most_one ( self,
*LiteralT literals )

Definition at line 959 of file cp_model.py.

◆ add_at_most_one() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_at_most_one ( self,
Iterable[LiteralT] literals )

Definition at line 956 of file cp_model.py.

◆ add_automaton()

Constraint ortools.sat.python.cp_model.CpModel.add_automaton ( self,
Sequence[LinearExprT] transition_expressions,
IntegralT starting_state,
Sequence[IntegralT] final_states,
Sequence[tuple[IntegralT, IntegralT, IntegralT]] transition_triples )
Adds an automaton constraint.

An automaton constraint takes a list of affine expressions (a * var + b) (of
size *n*), an initial state, a set of final states, and a set of
transitions. A transition is a triplet (*tail*, *transition*, *head*), where
*tail* and *head* are states, and *transition* is the label of an arc from
*head* to *tail*, corresponding to the value of one expression in the list
of
expressions.

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*, *transition*, *head*), it will add
an arc from the state *tail* of phase *i* and the state *head* of phase
*i* + 1. This arc is labeled by the value *transition* of the expression
`expressions[i]`. That is, this arc can only be selected if `expressions[i]`
is assigned the value *transition*.

A feasible solution of this constraint is an assignment of expressions such
that, starting from the initial state in phase 0, there is a path labeled by
the values of the expressions that ends in one of the final states in the
final phase.

Args:
  transition_expressions: A non-empty list of affine expressions (a * var +
    b) whose values correspond to the labels of the arcs traversed by the
    automaton.
  starting_state: The initial state of the automaton.
  final_states: A non-empty list of admissible final states.
  transition_triples: A list of transitions for the automaton, in the
    following format (current_state, variable_value, next_state).

Returns:
  An instance of the `Constraint` class.

Raises:
  ValueError: if `transition_expressions`, `final_states`, or
    `transition_triples` are empty.

Definition at line 699 of file cp_model.py.

◆ add_bool_and() [1/3]

ortools.sat.python.cp_model.CpModel.add_bool_and ( self,
* literals )
Adds `And(literals) == true`.

Definition at line 985 of file cp_model.py.

◆ add_bool_and() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_bool_and ( self,
*LiteralT literals )

Definition at line 983 of file cp_model.py.

◆ add_bool_and() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_bool_and ( self,
Iterable[LiteralT] literals )

Definition at line 980 of file cp_model.py.

◆ add_bool_or() [1/3]

ortools.sat.python.cp_model.CpModel.add_bool_or ( self,
* literals )
Adds `Or(literals) == true`: sum(literals) >= 1.

Definition at line 937 of file cp_model.py.

◆ add_bool_or() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_bool_or ( self,
*LiteralT literals )

Definition at line 935 of file cp_model.py.

◆ add_bool_or() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_bool_or ( self,
Iterable[LiteralT] literals )

Definition at line 932 of file cp_model.py.

◆ add_bool_xor() [1/3]

ortools.sat.python.cp_model.CpModel.add_bool_xor ( self,
* literals )
Adds `XOr(literals) == true`.

In contrast to add_bool_or and add_bool_and, it does not support
    .only_enforce_if().

Args:
  *literals: the list of literals in the constraint.

Returns:
  An `Constraint` object.

Definition at line 997 of file cp_model.py.

◆ add_bool_xor() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_bool_xor ( self,
*LiteralT literals )

Definition at line 995 of file cp_model.py.

◆ add_bool_xor() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_bool_xor ( self,
Iterable[LiteralT] literals )

Definition at line 992 of file cp_model.py.

◆ add_circuit()

Constraint ortools.sat.python.cp_model.CpModel.add_circuit ( self,
Sequence[ArcT] arcs )
Adds Circuit(arcs).

Adds a circuit constraint from a sparse list of arcs that encode the graph.

A circuit is a unique Hamiltonian cycle in a subgraph of the total
graph. In case a node 'i' is not in the cycle, then there must be a
loop arc 'i -> i' associated with a true literal. Otherwise
this constraint will fail.

Args:
  arcs: a list of arcs. An arc is a tuple (source_node, destination_node,
    literal). The arc is selected in the circuit if the literal is true.
    Both source_node and destination_node must be integers between 0 and the
    number of nodes - 1.

Returns:
  An instance of the `Constraint` class.

Raises:
  ValueError: If the list of arcs is empty.

Definition at line 578 of file cp_model.py.

◆ add_cumulative()

Constraint ortools.sat.python.cp_model.CpModel.add_cumulative ( self,
Iterable[IntervalVar] intervals,
Iterable[LinearExprT] demands,
LinearExprT capacity )
Adds Cumulative(intervals, demands, capacity).

This constraint enforces that:

    for all t:
      sum(demands[i]
        if (start(intervals[i]) <= t < end(intervals[i])) and
        (intervals[i] is present)) <= capacity

Args:
  intervals: The list of intervals.
  demands: The list of demands for each interval. Each demand must be >= 0.
    Each demand can be a 1-var affine expression (a * x + b).
  capacity: The maximum capacity of the cumulative constraint. It can be a
    1-var affine expression (a * x + b).

Returns:
  An instance of the `Constraint` class.

Definition at line 1448 of file cp_model.py.

◆ add_decision_strategy()

None ortools.sat.python.cp_model.CpModel.add_decision_strategy ( self,
Iterable[IntVar] variables,
cmh.DecisionStrategyProto.VariableSelectionStrategy var_strategy,
cmh.DecisionStrategyProto.DomainReductionStrategy domain_strategy )
Adds a search strategy to the model.

Args:
  variables: a list of variables this strategy will assign.
  var_strategy: heuristic to choose the next variable to assign.
  domain_strategy: heuristic to reduce the domain of the selected variable.
    Currently, this is advanced code: the union of all strategies added to
    the model must be complete, i.e. instantiates all variables. Otherwise,
    solve() will fail.

Definition at line 1589 of file cp_model.py.

◆ add_division_equality()

Constraint ortools.sat.python.cp_model.CpModel.add_division_equality ( self,
LinearExprT target,
LinearExprT num,
LinearExprT denom )
Adds `target == num // denom` (integer division rounded towards 0).

Definition at line 1045 of file cp_model.py.

◆ add_element()

Constraint ortools.sat.python.cp_model.CpModel.add_element ( self,
LinearExprT index,
Sequence[LinearExprT] expressions,
LinearExprT target )
Adds the element constraint: `expressions[index] == target`.

Args:
  index: The index of the selected expression in the array. It must be an
    affine expression (a * var + b).
  expressions: A list of affine expressions.
  target: The expression constrained to be equal to the selected expression.
    It must be an affine expression (a * var + b).

Returns:
  An instance of the `Constraint` class.

Definition at line 550 of file cp_model.py.

◆ add_exactly_one() [1/3]

ortools.sat.python.cp_model.CpModel.add_exactly_one ( self,
* literals )
Adds `ExactlyOne(literals)`: `sum(literals) == 1`.

Definition at line 973 of file cp_model.py.

◆ add_exactly_one() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_exactly_one ( self,
*LiteralT literals )

Definition at line 971 of file cp_model.py.

◆ add_exactly_one() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_exactly_one ( self,
Iterable[LiteralT] literals )

Definition at line 968 of file cp_model.py.

◆ add_forbidden_assignments()

Constraint ortools.sat.python.cp_model.CpModel.add_forbidden_assignments ( self,
Sequence[LinearExprT] expressions,
Iterable[Sequence[IntegralT]] tuples_list )
Adds add_forbidden_assignments(expressions, [tuples_list]).

A ForbiddenAssignments constraint is a constraint on an array of affine
expressions where the list of impossible combinations is provided in the
tuples list.

Args:
  expressions: A list of affine expressions (a * var + b).
  tuples_list: A list of forbidden tuples. Each tuple must have the same
    length as the expressions, and the *i*th value of a tuple corresponds to
    the *i*th expression.

Returns:
  An instance of the `Constraint` class.

Raises:
  TypeError: If a tuple does not have the same size as the list of
             expressions.
  ValueError: If the array of expressions is empty.

Definition at line 666 of file cp_model.py.

◆ add_hint() [1/3]

None ortools.sat.python.cp_model.CpModel.add_hint ( self,
BoolVarT literal,
bool value )

Definition at line 1653 of file cp_model.py.

◆ add_hint() [2/3]

None ortools.sat.python.cp_model.CpModel.add_hint ( self,
IntVar var,
int value )

Definition at line 1650 of file cp_model.py.

◆ add_hint() [3/3]

None ortools.sat.python.cp_model.CpModel.add_hint ( self,
var,
value )
Adds 'var == value' as a hint to the solver.

Definition at line 1655 of file cp_model.py.

◆ add_implication()

Constraint ortools.sat.python.cp_model.CpModel.add_implication ( self,
LiteralT a,
LiteralT b )
Adds `a => b` (`a` implies `b`).

Definition at line 927 of file cp_model.py.

◆ add_inverse()

Constraint ortools.sat.python.cp_model.CpModel.add_inverse ( self,
Sequence[VariableT] variables,
Sequence[VariableT] inverse_variables )
Adds Inverse(variables, inverse_variables).

An inverse constraint enforces that if `variables[i]` is assigned a value
`j`, then `inverse_variables[j]` is assigned a value `i`. And vice versa.

Args:
  variables: An array of integer variables.
  inverse_variables: An array of integer variables.

Returns:
  An instance of the `Constraint` class.

Raises:
  TypeError: if variables and inverse_variables have different lengths, or
      if they are empty.

Definition at line 766 of file cp_model.py.

◆ add_linear_constraint()

Constraint ortools.sat.python.cp_model.CpModel.add_linear_constraint ( self,
LinearExprT linear_expr,
IntegralT lb,
IntegralT ub )
Adds the constraint: `lb <= linear_expr <= ub`.

Definition at line 477 of file cp_model.py.

◆ add_linear_expression_in_domain()

Constraint ortools.sat.python.cp_model.CpModel.add_linear_expression_in_domain ( self,
LinearExprT linear_expr,
sorted_interval_list.Domain domain )
Adds the constraint: `linear_expr` in `domain`.

Definition at line 485 of file cp_model.py.

◆ add_map_domain()

ortools.sat.python.cp_model.CpModel.add_map_domain ( self,
IntVar var,
Iterable[IntVar] bool_var_array,
IntegralT offset = 0 )
Adds `var == i + offset <=> bool_var_array[i] == true for all i`.

Definition at line 919 of file cp_model.py.

◆ add_max_equality() [1/3]

Constraint ortools.sat.python.cp_model.CpModel.add_max_equality ( self,
LinearExprT target,
*LinearExprT expressions )

Definition at line 1035 of file cp_model.py.

◆ add_max_equality() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_max_equality ( self,
LinearExprT target,
Iterable[LinearExprT] expressions )

Definition at line 1030 of file cp_model.py.

◆ add_max_equality() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_max_equality ( self,
target,
* expressions )
Adds `target == Max(expressions)`.

Definition at line 1039 of file cp_model.py.

◆ add_min_equality() [1/3]

Constraint ortools.sat.python.cp_model.CpModel.add_min_equality ( self,
LinearExprT target,
*LinearExprT expressions )

Definition at line 1019 of file cp_model.py.

◆ add_min_equality() [2/3]

Constraint ortools.sat.python.cp_model.CpModel.add_min_equality ( self,
LinearExprT target,
Iterable[LinearExprT] expressions )

Definition at line 1014 of file cp_model.py.

◆ add_min_equality() [3/3]

Constraint ortools.sat.python.cp_model.CpModel.add_min_equality ( self,
target,
* expressions )
Adds `target == Min(expressions)`.

Definition at line 1023 of file cp_model.py.

◆ add_modulo_equality()

Constraint ortools.sat.python.cp_model.CpModel.add_modulo_equality ( self,
LinearExprT target,
LinearExprT expr,
LinearExprT mod )
Adds `target = expr % mod`.

It uses the C convention, that is the result is the remainder of the
integral division rounded towards 0.

    For example:
    * 10 % 3 = 1
    * -10 % 3 = -1
    * 10 % -3 = 1
    * -10 % -3 = -1

Args:
  target: the target expression.
  expr: the expression to compute the modulo of.
  mod: the modulus expression.

Returns:
  A `Constraint` object.

Definition at line 1059 of file cp_model.py.

◆ add_multiple_circuit()

Constraint ortools.sat.python.cp_model.CpModel.add_multiple_circuit ( self,
Sequence[ArcT] arcs )
Adds a multiple circuit constraint, aka the 'VRP' 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.

Args:
  arcs: a list of arcs. An arc is a tuple (source_node, destination_node,
    literal). The arc is selected in the circuit if the literal is true.
    Both source_node and destination_node must be integers between 0 and the
    number of nodes - 1.

Returns:
  An instance of the `Constraint` class.

Raises:
  ValueError: If the list of arcs is empty.

Definition at line 604 of file cp_model.py.

◆ add_multiplication_equality()

Constraint ortools.sat.python.cp_model.CpModel.add_multiplication_equality ( self,
LinearExprT target,
*Union[Iterable[LinearExprT], LinearExprT] expressions )
Adds `target == expressions[0] * .. * expressions[n]`.

Definition at line 1085 of file cp_model.py.

◆ add_no_overlap()

Constraint ortools.sat.python.cp_model.CpModel.add_no_overlap ( self,
Iterable[IntervalVar] intervals )
Adds NoOverlap(interval_vars).

A NoOverlap constraint ensures that all present intervals do not overlap
in time.

Args:
  intervals: The list of interval variables to constrain.

Returns:
  An instance of the `Constraint` class.

Definition at line 1411 of file cp_model.py.

◆ add_no_overlap_2d()

Constraint ortools.sat.python.cp_model.CpModel.add_no_overlap_2d ( self,
Iterable[IntervalVar] x_intervals,
Iterable[IntervalVar] y_intervals )
Adds NoOverlap2D(x_intervals, y_intervals).

A NoOverlap2D constraint ensures that all present rectangles do not overlap
on a plane. Each rectangle is aligned with the X and Y axis, and is defined
by two intervals which represent its projection onto the X and Y axis.

Furthermore, one box is optional if at least one of the x or y interval is
optional.

Args:
  x_intervals: The X coordinates of the rectangles.
  y_intervals: The Y coordinates of the rectangles.

Returns:
  An instance of the `Constraint` class.

Definition at line 1425 of file cp_model.py.

◆ add_reservoir_constraint()

Constraint ortools.sat.python.cp_model.CpModel.add_reservoir_constraint ( self,
Sequence[LinearExprT] times,
Sequence[LinearExprT] level_changes,
int min_level,
int max_level )
Adds Reservoir(times, level_changes, min_level, max_level).

Maintains a reservoir level within bounds. The water level starts at 0, and
at any time, it must be between min_level and max_level.

If the affine expression `times[i]` is assigned a value t, then the current
level changes by `level_changes[i]`, which is constant, at time t.

 Note that min level must be <= 0, and the max level must be >= 0. Please
 use fixed level_changes to simulate initial state.

 Therefore, at any time:
     sum(level_changes[i] if times[i] <= t) in [min_level, max_level]

Args:
  times: A list of 1-var affine expressions (a * x + b) which specify the
    time of the filling or emptying the reservoir.
  level_changes: A list of integer values that specifies the amount of the
    emptying or filling. Currently, variable demands are not supported.
  min_level: At any time, the level of the reservoir must be greater or
    equal than the min level.
  max_level: At any time, the level of the reservoir must be less or equal
    than the max level.

Returns:
  An instance of the `Constraint` class.

Raises:
  ValueError: if max_level < min_level.

  ValueError: if max_level < 0.

  ValueError: if min_level > 0

Definition at line 797 of file cp_model.py.

◆ add_reservoir_constraint_with_active()

Constraint ortools.sat.python.cp_model.CpModel.add_reservoir_constraint_with_active ( self,
Sequence[LinearExprT] times,
Sequence[LinearExprT] level_changes,
Sequence[LiteralT] actives,
int min_level,
int max_level )
Adds Reservoir(times, level_changes, actives, min_level, max_level).

Maintains a reservoir level within bounds. The water level starts at 0, and
at any time, it must be between min_level and max_level.

If the variable `times[i]` is assigned a value t, and `actives[i]` is
`True`, then the current level changes by `level_changes[i]`, which is
constant,
at time t.

 Note that min level must be <= 0, and the max level must be >= 0. Please
 use fixed level_changes to simulate initial state.

 Therefore, at any time:
     sum(level_changes[i] * actives[i] if times[i] <= t) in [min_level,
     max_level]


The array of boolean variables 'actives', if defined, indicates which
actions are actually performed.

Args:
  times: A list of 1-var affine expressions (a * x + b) which specify the
    time of the filling or emptying the reservoir.
  level_changes: A list of integer values that specifies the amount of the
    emptying or filling. Currently, variable demands are not supported.
  actives: a list of boolean variables. They indicates if the
    emptying/refilling events actually take place.
  min_level: At any time, the level of the reservoir must be greater or
    equal than the min level.
  max_level: At any time, the level of the reservoir must be less or equal
    than the max level.

Returns:
  An instance of the `Constraint` class.

Raises:
  ValueError: if max_level < min_level.

  ValueError: if max_level < 0.

  ValueError: if min_level > 0

Definition at line 847 of file cp_model.py.

◆ clear_assumptions()

None ortools.sat.python.cp_model.CpModel.clear_assumptions ( self)
Removes all assumptions from the model.

Definition at line 1677 of file cp_model.py.

◆ clear_hints()

ortools.sat.python.cp_model.CpModel.clear_hints ( self)
Removes any solution hint from the model.

Definition at line 1664 of file cp_model.py.

◆ clear_objective()

ortools.sat.python.cp_model.CpModel.clear_objective ( self)

Definition at line 1585 of file cp_model.py.

◆ clone()

"CpModel" ortools.sat.python.cp_model.CpModel.clone ( self)
Reset the model, and creates a new one from a CpModelProto instance.

Definition at line 1476 of file cp_model.py.

◆ export_to_file()

bool ortools.sat.python.cp_model.CpModel.export_to_file ( self,
str file )
Write the model as a protocol buffer to 'file'.

Args:
  file: file to write the model to. If the filename ends with 'txt', the
    model will be written as a text file, otherwise, the binary format will
    be used.

Returns:
  True if the model was correctly written.

Definition at line 1628 of file cp_model.py.

◆ get_bool_var_from_proto_index()

IntVar ortools.sat.python.cp_model.CpModel.get_bool_var_from_proto_index ( self,
int index )
Returns an already created Boolean variable from its index.

Definition at line 1489 of file cp_model.py.

◆ get_int_var_from_proto_index()

IntVar ortools.sat.python.cp_model.CpModel.get_int_var_from_proto_index ( self,
int index )
Returns an already created integer variable from its index.

Definition at line 1503 of file cp_model.py.

◆ get_interval_var_from_proto_index()

IntervalVar ortools.sat.python.cp_model.CpModel.get_interval_var_from_proto_index ( self,
int index )
Returns an already created interval variable from its index.

Definition at line 1511 of file cp_model.py.

◆ has_objective()

bool ortools.sat.python.cp_model.CpModel.has_objective ( self)

Definition at line 1579 of file cp_model.py.

◆ maximize()

ortools.sat.python.cp_model.CpModel.maximize ( self,
ObjLinearExprT obj )
Sets the objective of the model to maximize(obj).

Definition at line 1575 of file cp_model.py.

◆ minimize()

ortools.sat.python.cp_model.CpModel.minimize ( self,
ObjLinearExprT obj )
Sets the objective of the model to minimize(obj).

Definition at line 1571 of file cp_model.py.

◆ model_stats()

str ortools.sat.python.cp_model.CpModel.model_stats ( self)
Returns a string containing some model statistics.

Definition at line 1620 of file cp_model.py.

◆ Name()

str ortools.sat.python.cp_model.CpModel.Name ( self)

Definition at line 1719 of file cp_model.py.

◆ name() [1/2]

str ortools.sat.python.cp_model.CpModel.name ( self)
Returns the name of the model.

Definition at line 321 of file cp_model.py.

◆ name() [2/2]

ortools.sat.python.cp_model.CpModel.name ( self,
str name )
Sets the name of the model.

Definition at line 328 of file cp_model.py.

◆ negated()

int ortools.sat.python.cp_model.CpModel.negated ( self,
int index )

Definition at line 1534 of file cp_model.py.

◆ new_bool_var()

IntVar ortools.sat.python.cp_model.CpModel.new_bool_var ( self,
str name )
Creates a 0-1 variable with the given name.

Definition at line 372 of file cp_model.py.

◆ new_bool_var_series()

pd.Series ortools.sat.python.cp_model.CpModel.new_bool_var_series ( self,
str name,
pd.Index index )
Creates a series of (scalar-valued) variables with the given name.

Args:
  name (str): Required. The name of the variable set.
  index (pd.Index): Required. The index to use for the variable set.

Returns:
  pd.Series: The variable set indexed by its corresponding dimensions.

Raises:
  TypeError: if the `index` is invalid (e.g. a `DataFrame`).
  ValueError: if the `name` is not a valid identifier or already exists.

Definition at line 442 of file cp_model.py.

◆ new_constant()

IntVar ortools.sat.python.cp_model.CpModel.new_constant ( self,
IntegralT value )
Declares a constant integer.

Definition at line 380 of file cp_model.py.

◆ new_fixed_size_interval_var()

IntervalVar ortools.sat.python.cp_model.CpModel.new_fixed_size_interval_var ( self,
LinearExprT start,
IntegralT size,
str name )
Creates an interval variable from start, and a fixed size.

An interval variable is a constraint, that is itself used in other
constraints like NoOverlap.

Args:
  start: The start of the interval. It must be of the form a * var + b.
  size: The size of the interval. It must be an integer value.
  name: The name of the interval variable.

Returns:
  An `IntervalVar` object.

Definition at line 1170 of file cp_model.py.

◆ new_fixed_size_interval_var_series()

pd.Series ortools.sat.python.cp_model.CpModel.new_fixed_size_interval_var_series ( self,
str name,
pd.Index index,
Union[LinearExprT, pd.Series] starts,
Union[IntegralT, pd.Series] sizes )
Creates a series of interval variables with the given name.

Args:
  name (str): Required. The name of the variable set.
  index (pd.Index): Required. The index to use for the variable set.
  starts (Union[LinearExprT, pd.Series]): The start of each interval in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  sizes (Union[IntegralT, pd.Series]): The fixed size of each interval in
    the set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.

Returns:
  pd.Series: The interval variable set indexed by its corresponding
  dimensions.

Raises:
  TypeError: if the `index` is invalid (e.g. a `DataFrame`).
  ValueError: if the `name` is not a valid identifier or already exists.
  ValueError: if the all the indexes do not match.

Definition at line 1188 of file cp_model.py.

◆ new_int_var()

IntVar ortools.sat.python.cp_model.CpModel.new_int_var ( self,
IntegralT lb,
IntegralT ub,
str name )
Create an integer variable with domain [lb, ub].

The CP-SAT solver is limited to integer variables. If you have fractional
values, scale them up so that they become integers; if you have strings,
encode them as integers.

Args:
  lb: Lower bound for the variable.
  ub: Upper bound for the variable.
  name: The name of the variable.

Returns:
  a variable whose domain is [lb, ub].

Definition at line 333 of file cp_model.py.

◆ new_int_var_from_domain()

IntVar ortools.sat.python.cp_model.CpModel.new_int_var_from_domain ( self,
sorted_interval_list.Domain domain,
str name )
Create an integer variable from a domain.

A domain is a set of integers specified by a collection of intervals.
For example, `model.new_int_var_from_domain(cp_model.
     Domain.from_intervals([[1, 2], [4, 6]]), 'x')`

Args:
  domain: An instance of the Domain class.
  name: The name of the variable.

Returns:
    a variable whose domain is the given domain.

Definition at line 354 of file cp_model.py.

◆ new_int_var_series()

pd.Series ortools.sat.python.cp_model.CpModel.new_int_var_series ( self,
str name,
pd.Index index,
Union[IntegralT, pd.Series] lower_bounds,
Union[IntegralT, pd.Series] upper_bounds )
Creates a series of (scalar-valued) variables with the given name.

Args:
  name (str): Required. The name of the variable set.
  index (pd.Index): Required. The index to use for the variable set.
  lower_bounds (Union[int, pd.Series]): A lower bound for variables in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  upper_bounds (Union[int, pd.Series]): An upper bound for variables in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.

Returns:
  pd.Series: The variable set indexed by its corresponding dimensions.

Raises:
  TypeError: if the `index` is invalid (e.g. a `DataFrame`).
  ValueError: if the `name` is not a valid identifier or already exists.
  ValueError: if the `lowerbound` is greater than the `upperbound`.
  ValueError: if the index of `lower_bound`, or `upper_bound` does not match
  the input index.

Definition at line 384 of file cp_model.py.

◆ new_interval_var()

IntervalVar ortools.sat.python.cp_model.CpModel.new_interval_var ( self,
LinearExprT start,
LinearExprT size,
LinearExprT end,
str name )
Creates an interval variable from start, size, and end.

An interval variable is a constraint, that is itself used in other
constraints like NoOverlap.

Internally, it ensures that `start + size == end`.

Args:
  start: The start of the interval. It must be of the form a * var + b.
  size: The size of the interval. It must be of the form a * var + b.
  end: The end of the interval. It must be of the form a * var + b.
  name: The name of the interval variable.

Returns:
  An `IntervalVar` object.

Definition at line 1097 of file cp_model.py.

◆ new_interval_var_series()

pd.Series ortools.sat.python.cp_model.CpModel.new_interval_var_series ( self,
str name,
pd.Index index,
Union[LinearExprT, pd.Series] starts,
Union[LinearExprT, pd.Series] sizes,
Union[LinearExprT, pd.Series] ends )
Creates a series of interval variables with the given name.

Args:
  name (str): Required. The name of the variable set.
  index (pd.Index): Required. The index to use for the variable set.
  starts (Union[LinearExprT, pd.Series]): The start of each interval in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  sizes (Union[LinearExprT, pd.Series]): The size of each interval in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  ends (Union[LinearExprT, pd.Series]): The ends of each interval in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.

Returns:
  pd.Series: The interval variable set indexed by its corresponding
  dimensions.

Raises:
  TypeError: if the `index` is invalid (e.g. a `DataFrame`).
  ValueError: if the `name` is not a valid identifier or already exists.
  ValueError: if the all the indexes do not match.

Definition at line 1118 of file cp_model.py.

◆ new_optional_fixed_size_interval_var()

IntervalVar ortools.sat.python.cp_model.CpModel.new_optional_fixed_size_interval_var ( self,
LinearExprT start,
IntegralT size,
LiteralT is_present,
str name )
Creates an interval variable from start, and a fixed size.

An interval variable is a constraint, that is itself used in other
constraints like NoOverlap.

Args:
  start: The start of the interval. It must be of the form a * var + b.
  size: The size of the interval. It must be an integer value.
  is_present: A literal that indicates if the interval is active or not. A
    inactive interval is simply ignored by all constraints.
  name: The name of the interval variable.

Returns:
  An `IntervalVar` object.

Definition at line 1329 of file cp_model.py.

◆ new_optional_fixed_size_interval_var_series()

pd.Series ortools.sat.python.cp_model.CpModel.new_optional_fixed_size_interval_var_series ( self,
str name,
pd.Index index,
Union[LinearExprT, pd.Series] starts,
Union[IntegralT, pd.Series] sizes,
Union[LiteralT, pd.Series] are_present )
Creates a series of interval variables with the given name.

Args:
  name (str): Required. The name of the variable set.
  index (pd.Index): Required. The index to use for the variable set.
  starts (Union[LinearExprT, pd.Series]): The start of each interval in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  sizes (Union[IntegralT, pd.Series]): The fixed size of each interval in
    the set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  are_present (Union[LiteralT, pd.Series]): The performed literal of each
    interval in the set. If a `pd.Series` is passed in, it will be based on
    the corresponding values of the pd.Series.

Returns:
  pd.Series: The interval variable set indexed by its corresponding
  dimensions.

Raises:
  TypeError: if the `index` is invalid (e.g. a `DataFrame`).
  ValueError: if the `name` is not a valid identifier or already exists.
  ValueError: if the all the indexes do not match.

Definition at line 1359 of file cp_model.py.

◆ new_optional_interval_var()

IntervalVar ortools.sat.python.cp_model.CpModel.new_optional_interval_var ( self,
LinearExprT start,
LinearExprT size,
LinearExprT end,
LiteralT is_present,
str name )
Creates an optional interval var from start, size, end, and is_present.

An optional interval variable is a constraint, that is itself used in other
constraints like NoOverlap. This constraint is protected by a presence
literal that indicates if it is active or not.

Internally, it ensures that `is_present` implies `start + size ==
end`.

Args:
  start: The start of the interval. It must be of the form a * var + b.
  size: The size of the interval. It must be of the form a * var + b.
  end: The end of the interval. It must be of the form a * var + b.
  is_present: A literal that indicates if the interval is active or not. A
    inactive interval is simply ignored by all constraints.
  name: The name of the interval variable.

Returns:
  An `IntervalVar` object.

Definition at line 1234 of file cp_model.py.

◆ new_optional_interval_var_series()

pd.Series ortools.sat.python.cp_model.CpModel.new_optional_interval_var_series ( self,
str name,
pd.Index index,
Union[LinearExprT, pd.Series] starts,
Union[LinearExprT, pd.Series] sizes,
Union[LinearExprT, pd.Series] ends,
Union[LiteralT, pd.Series] are_present )
Creates a series of interval variables with the given name.

Args:
  name (str): Required. The name of the variable set.
  index (pd.Index): Required. The index to use for the variable set.
  starts (Union[LinearExprT, pd.Series]): The start of each interval in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  sizes (Union[LinearExprT, pd.Series]): The size of each interval in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  ends (Union[LinearExprT, pd.Series]): The ends of each interval in the
    set. If a `pd.Series` is passed in, it will be based on the
    corresponding values of the pd.Series.
  are_present (Union[LiteralT, pd.Series]): The performed literal of each
    interval in the set. If a `pd.Series` is passed in, it will be based on
    the corresponding values of the pd.Series.

Returns:
  pd.Series: The interval variable set indexed by its corresponding
  dimensions.

Raises:
  TypeError: if the `index` is invalid (e.g. a `DataFrame`).
  ValueError: if the `name` is not a valid identifier or already exists.
  ValueError: if the all the indexes do not match.

Definition at line 1270 of file cp_model.py.

◆ Proto()

cmh.CpModelProto ortools.sat.python.cp_model.CpModel.Proto ( self)

Definition at line 1727 of file cp_model.py.

◆ proto()

cmh.CpModelProto ortools.sat.python.cp_model.CpModel.proto ( self)
Returns the underlying CpModelProto.

Definition at line 1530 of file cp_model.py.

◆ remove_all_names()

None ortools.sat.python.cp_model.CpModel.remove_all_names ( self)
Removes all names from the model.

Definition at line 1641 of file cp_model.py.

◆ SetName()

None ortools.sat.python.cp_model.CpModel.SetName ( self,
str name )

Definition at line 1723 of file cp_model.py.

◆ validate()

str ortools.sat.python.cp_model.CpModel.validate ( self)
Returns a string indicating that the model is invalid.

Definition at line 1624 of file cp_model.py.

Member Data Documentation

◆ model_proto

ortools.sat.python.cp_model.CpModel.model_proto

Definition at line 349 of file cp_model.py.

◆ name

str ortools.sat.python.cp_model.CpModel.name = name

Definition at line 1724 of file cp_model.py.

◆ proto

ortools.sat.python.cp_model.CpModel.proto

Definition at line 1479 of file cp_model.py.


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