Stay organized with collections
Save and categorize content based on your preferences.
C++ Reference: class MPCallbackContext
Note: This documentation is automatically generated.
When querying solution values or modifying the model during a callback, use
this API, rather than manipulating MPSolver directly. You should only
interact with this object from within MPCallback::RunCallback().
Adds a constraint to the model that strengths the LP relaxation.
Call only when the event is kMipNode.
Requires that MPCallback::might_add_cuts() is true.
This constraint must not cut off integer solutions, it should only
strengthen the LP (behavior is undefined otherwise). Use
MPCallbackContext::AddLazyConstriant() if you are cutting off integer
solutions.
Adds a constraint to the model that cuts off an undesired integer solution.
Call only when the event is kMipSolution or kMipNode.
Requires that MPCallback::might_add_lazy_constraints() is true.
Use this to avoid adding a large number of constraints to the model where
most are expected to not be needed.
Given an integral solution, AddLazyConstraint() MUST be able to detect if
there is a violated constraint, and it is guaranteed that every integer
solution will be checked by AddLazyConstraint().
Warning(rander): in some solvers, e.g. Gurobi, an integer solution may not
respect a previously added lazy constraint, so you may need to add a
constraint more than once (e.g. due to threading issues).
Always false if event is not kMipSolution or kMipNode, otherwise behavior
may be solver dependent.
For Gurobi, under kMipNode, may be false if the node was not solved to
optimality, see MIPNODE_REL here for details:
http://www.gurobi.com/documentation/8.0/refman/callback_codes.html
Returns the number of nodes explored so far in the branch and bound tree,
which 0 at the root node and > 0 otherwise.
Call only when the event is kMipSolution or kMipNode.
Suggests a (potentially partial) variable assignment to the solver, to be
used as a feasible solution (or part of one). If the assignment is partial,
certain solvers (e.g. Gurobi) will try to compute a feasible solution from
the partial assignment. Returns the objective value of the solution if the
solver supports it.
Call only when the event is kMipNode.
Returns the value of variable from the solver's current state.
Call only when CanQueryVariableValues() is true.
At kMipSolution, the solution is integer feasible, while at kMipNode, the
solution solves the current node's LP relaxation (so integer variables may
be fractional).
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-08-06 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eMPCallbackContext\u003c/code\u003e class provides an API for interacting with the solver during the solution process, such as adding cuts or lazy constraints.\u003c/p\u003e\n"],["\u003cp\u003eIt's crucial to utilize this API instead of directly manipulating the \u003ccode\u003eMPSolver\u003c/code\u003e within the \u003ccode\u003eMPCallback::RunCallback()\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eInteractions with the solver are event-driven, with the \u003ccode\u003eMPCallbackEvent\u003c/code\u003e determining the available operations at any given time.\u003c/p\u003e\n"],["\u003cp\u003eUsers can query variable values and suggest solutions through this context, but these capabilities are conditional based on the event and solver.\u003c/p\u003e\n"],["\u003cp\u003eThis class enables dynamic model modification during the optimization process, supporting techniques like adding cuts or lazy constraints to improve solution quality.\u003c/p\u003e\n"]]],["The `MPCallbackContext` class is used within `MPCallback::RunCallback()` to interact with the solver. Key actions include: `AddCut` to strengthen LP relaxations, `AddLazyConstraint` to cut off undesired integer solutions, and `SuggestSolution` to propose variable assignments. `VariableValue` retrieves current variable values. `Event` reveals the solver's current state. The number of explored branch and bound nodes can be accessed via `NumExploredNodes`. Finally `CanQueryVariableValues` checks if the values can be queried.\n"],null,["# MPCallbackContext\n\nC++ Reference: class MPCallbackContext\n======================================\n\n\nNote: This documentation is automatically generated.\nWhen querying solution values or modifying the model during a callback, use this API, rather than manipulating MPSolver directly. You should only interact with this object from within MPCallback::RunCallback().\n\n| Method ||\n|-----------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [`AddCut`](https://github.com/google/or-tools/blob/v9.4/ortools/linear_solver/linear_solver_callback.h#L101) | Return type: `virtual void ` Arguments: `const LinearRange& cutting_plane` Adds a constraint to the model that strengths the LP relaxation. Call only when the event is kMipNode. Requires that MPCallback::might_add_cuts() is true. This constraint must not cut off integer solutions, it should only strengthen the LP (behavior is undefined otherwise). Use MPCallbackContext::AddLazyConstriant() if you are cutting off integer solutions. |\n| [`AddLazyConstraint`](https://github.com/google/or-tools/blob/v9.4/ortools/linear_solver/linear_solver_callback.h#L119) | Return type: `virtual void ` Arguments: `const LinearRange& lazy_constraint` Adds a constraint to the model that cuts off an undesired integer solution. Call only when the event is kMipSolution or kMipNode. Requires that MPCallback::might_add_lazy_constraints() is true. Use this to avoid adding a large number of constraints to the model where most are expected to not be needed. Given an integral solution, AddLazyConstraint() MUST be able to detect if there is a violated constraint, and it is guaranteed that every integer solution will be checked by AddLazyConstraint(). Warning(rander): in some solvers, e.g. Gurobi, an integer solution may not respect a previously added lazy constraint, so you may need to add a constraint more than once (e.g. due to threading issues). |\n| [`CanQueryVariableValues`](https://github.com/google/or-tools/blob/v9.4/ortools/linear_solver/linear_solver_callback.h#L80) | Return type: `virtual bool ` Always false if event is not kMipSolution or kMipNode, otherwise behavior may be solver dependent. For Gurobi, under kMipNode, may be false if the node was not solved to optimality, see MIPNODE_REL here for details: http://www.gurobi.com/documentation/8.0/refman/callback_codes.html |\n| [`Event`](https://github.com/google/or-tools/blob/v9.4/ortools/linear_solver/linear_solver_callback.h#L72) | Return type: `virtual MPCallbackEvent ` What the solver is currently doing. How you can interact with the solver from the callback depends on this value. |\n| [`~MPCallbackContext`](https://github.com/google/or-tools/blob/v9.4/ortools/linear_solver/linear_solver_callback.h#L68) | Return type: `virtual ` \u003cbr /\u003e |\n| [`NumExploredNodes`](https://github.com/google/or-tools/blob/v9.4/ortools/linear_solver/linear_solver_callback.h#L135) | Return type: `virtual int64_t ` Returns the number of nodes explored so far in the branch and bound tree, which 0 at the root node and \\\u003e 0 otherwise. Call only when the event is kMipSolution or kMipNode. |\n| [`SuggestSolution`](https://github.com/google/or-tools/blob/v9.4/ortools/linear_solver/linear_solver_callback.h#L128) | Return type: `virtual double ` Arguments: ` const absl::flat_hash_map\u003cconst MPVariable*, double\u003e& solution` Suggests a (potentially partial) variable assignment to the solver, to be used as a feasible solution (or part of one). If the assignment is partial, certain solvers (e.g. Gurobi) will try to compute a feasible solution from the partial assignment. Returns the objective value of the solution if the solver supports it. Call only when the event is kMipNode. |\n| [`VariableValue`](https://github.com/google/or-tools/blob/v9.4/ortools/linear_solver/linear_solver_callback.h#L89) | Return type: `virtual double ` Arguments: `const MPVariable* variable` Returns the value of variable from the solver's current state. Call only when CanQueryVariableValues() is true. At kMipSolution, the solution is integer feasible, while at kMipNode, the solution solves the current node's LP relaxation (so integer variables may be fractional). |"]]