Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ortools.math_opt.python.callback.CallbackResult Class Reference

Detailed Description

The value returned by a solve callback (produced by the user).

Attributes:
  terminate: When true it tells the solver to interrupt the solve as soon as
    possible.

    It can be set from any event. This is equivalent to using a
    SolveInterrupter and triggering it from the callback.

    Some solvers don't support interruption, in that case this is simply
    ignored and the solve terminates as usual. On top of that solvers may not
    immediately stop the solve. Thus the user should expect the callback to
    still be called after they set `terminate` to true in a previous
    call. Returning with `terminate` false after having previously returned
    true won't cancel the interruption.
  generated_constraints: Constraints to add to the model. For details, see
    GeneratedConstraint documentation.
  suggested_solutions: A list of solutions (or partially defined solutions) to
    suggest to the solver. Some solvers (e.g. gurobi) will try and convert a
    partial solution into a full solution by solving a MIP. Use only for
    Event.MIP_NODE.

Definition at line 222 of file callback.py.

Public Member Functions

None add_generated_constraint (self, Optional[Union[bool, variables.BoundedLinearTypes]] bounded_expr=None, *, Optional[float] lb=None, Optional[float] ub=None, Optional[variables.LinearTypes] expr=None, bool is_lazy)
None add_lazy_constraint (self, Optional[Union[bool, variables.BoundedLinearTypes]] bounded_expr=None, *, Optional[float] lb=None, Optional[float] ub=None, Optional[variables.LinearTypes] expr=None)
None add_user_cut (self, Optional[Union[bool, variables.BoundedLinearTypes]] bounded_expr=None, *, Optional[float] lb=None, Optional[float] ub=None, Optional[variables.LinearTypes] expr=None)
callback_pb2.CallbackResultProto to_proto (self)

Static Public Attributes

bool terminate = False
List generated_constraints
List suggested_solutions

Member Function Documentation

◆ add_generated_constraint()

None ortools.math_opt.python.callback.CallbackResult.add_generated_constraint ( self,
Optional[Union[bool, variables.BoundedLinearTypes]] bounded_expr = None,
* ,
Optional[float] lb = None,
Optional[float] ub = None,
Optional[variables.LinearTypes] expr = None,
bool is_lazy )
Adds a linear constraint to the list of generated constraints.

The constraint can be of two exclusive types: a "lazy constraint" or a
"user cut. A "user cut" is a constraint that excludes the current LP
solution, but does not cut off any integer-feasible points that satisfy the
already added constraints (either in callbacks or through
Model.add_linear_constraint()). A "lazy constraint" is a constraint that
excludes such integer-feasible points and hence is needed for corrctness of
the forlumation.

The simplest way to specify the constraint is by passing a one-sided or
two-sided linear inequality as in:
  * add_generated_constraint(x + y + 1.0 <= 2.0, is_lazy=True),
  * add_generated_constraint(x + y >= 2.0, is_lazy=True), or
  * add_generated_constraint((1.0 <= x + y) <= 2.0, is_lazy=True).

Note the extra parenthesis for two-sided linear inequalities, which is
required due to some language limitations (see
https://peps.python.org/pep-0335/ and https://peps.python.org/pep-0535/).
If the parenthesis are omitted, a TypeError will be raised explaining the
issue (if this error was not raised the first inequality would have been
silently ignored because of the noted language limitations).

The second way to specify the constraint is by setting lb, ub, and/o expr as
in:
  * add_generated_constraint(expr=x + y + 1.0, ub=2.0, is_lazy=True),
  * add_generated_constraint(expr=x + y, lb=2.0, is_lazy=True),
  * add_generated_constraint(expr=x + y, lb=1.0, ub=2.0, is_lazy=True), or
  * add_generated_constraint(lb=1.0, is_lazy=True).
Omitting lb is equivalent to setting it to -math.inf and omiting ub is
equivalent to setting it to math.inf.

These two alternatives are exclusive and a combined call like:
  * add_generated_constraint(x + y <= 2.0, lb=1.0, is_lazy=True), or
  * add_generated_constraint(x + y <= 2.0, ub=math.inf, is_lazy=True)
will raise a ValueError. A ValueError is also raised if expr's offset is
infinite.

Args:
  bounded_expr: a linear inequality describing the constraint. Cannot be
    specified together with lb, ub, or expr.
  lb: The constraint's lower bound if bounded_expr is omitted (if both
    bounder_expr and lb are omitted, the lower bound is -math.inf).
  ub: The constraint's upper bound if bounded_expr is omitted (if both
    bounder_expr and ub are omitted, the upper bound is math.inf).
  expr: The constraint's linear expression if bounded_expr is omitted.
  is_lazy: Whether the constraint is lazy or not.

Definition at line 254 of file callback.py.

◆ add_lazy_constraint()

None ortools.math_opt.python.callback.CallbackResult.add_lazy_constraint ( self,
Optional[Union[bool, variables.BoundedLinearTypes]] bounded_expr = None,
* ,
Optional[float] lb = None,
Optional[float] ub = None,
Optional[variables.LinearTypes] expr = None )
Shortcut for add_generated_constraint(..., is_lazy=True)..

Definition at line 323 of file callback.py.

◆ add_user_cut()

None ortools.math_opt.python.callback.CallbackResult.add_user_cut ( self,
Optional[Union[bool, variables.BoundedLinearTypes]] bounded_expr = None,
* ,
Optional[float] lb = None,
Optional[float] ub = None,
Optional[variables.LinearTypes] expr = None )
Shortcut for add_generated_constraint(..., is_lazy=False).

Definition at line 336 of file callback.py.

◆ to_proto()

callback_pb2.CallbackResultProto ortools.math_opt.python.callback.CallbackResult.to_proto ( self)
Returns a proto equivalent to this CallbackResult.

Definition at line 349 of file callback.py.

Member Data Documentation

◆ generated_constraints

List ortools.math_opt.python.callback.CallbackResult.generated_constraints
static
Initial value:
= dataclasses.field(
default_factory=list
)

Definition at line 247 of file callback.py.

◆ suggested_solutions

List ortools.math_opt.python.callback.CallbackResult.suggested_solutions
static
Initial value:
= dataclasses.field(
default_factory=list
)

Definition at line 250 of file callback.py.

◆ terminate

bool ortools.math_opt.python.callback.CallbackResult.terminate = False
static

Definition at line 246 of file callback.py.


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