C++ Reference: class MPSolver

This documentation is automatically generated.

This mathematical programming (MP) solver class is the main class though which users build and solve problems.
Method
Clear

Return type: void

Clears the objective (including the optimization direction), all variables and constraints. All the other properties of the MPSolver (like the time limit) are kept untouched.

ComputeConstraintActivities

Return type: std::vector<double>

Advanced usage: compute the "activities" of all constraints, which are the sums of their linear terms. The activities are returned in the same order as constraints(), which is the order in which constraints were added; but you can also use MPConstraint::index() to get a constraint's index.

ComputeExactConditionNumber

Return type: double

Advanced usage: computes the exact condition number of the current scaled basis: L1norm(B) * L1norm(inverse(B)), where B is the scaled basis. This method requires that a basis exists: it should be called after Solve. It is only available for continuous problems. It is implemented for GLPK but not CLP because CLP does not provide the API for doing it. The condition number measures how well the constraint matrix is conditioned and can be used to predict whether numerical issues will arise during the solve: the model is declared infeasible whereas it is feasible (or vice-versa), the solution obtained is not optimal or violates some constraints, the resolution is slow because of repeated singularities. The rule of thumb to interpret the condition number kappa is: o kappa <= 1e7: virtually no chance of numerical issues o 1e7 < kappa <= 1e10: small chance of numerical issues o 1e10 < kappa <= 1e13: medium chance of numerical issues o kappa > 1e13: high chance of numerical issues The computation of the condition number depends on the quality of the LU decomposition, so it is not very accurate when the matrix is ill conditioned.

constraints

Return type: const std::vector<MPConstraint*>&

Returns the array of constraints handled by the MPSolver. (They are listed in the order in which they were created.)

EnableOutput

Return type: void

ExportModelAsLpFormat

Return type: bool

Arguments: bool obfuscated, std::string* model_str

Shortcuts to the homonymous MPModelProtoExporter methods, via exporting to a MPModelProto with ExportModelToProto() (see above).

ExportModelAsMpsFormat

Return type: bool

Arguments: bool fixed_format, bool obfuscated, std::string* model_str

ExportModelToProto

Return type: void

Arguments: MPModelProto* output_model

Exports model to protocol buffer.

FillSolutionResponseProto

Return type: void

Arguments: MPSolutionResponse* response

GetSolverSpecificParametersAsString

Return type: std::string

infinity

Return type: static double

Infinity. You can use -MPSolver::infinity() for negative infinity.

InterruptSolve

Return type: bool

Interrupts the Solve() execution to terminate processing early if possible. If the underlying interface supports interruption; it does that and returns true regardless of whether there's an ongoing Solve() or not. The Solve() call may still linger for a while depending on the conditions. If interruption is not supported; returns false and does nothing.

IsMIP

Return type: bool

iterations

Return type: int64

Returns the number of simplex iterations.

LoadModelFromProto

Return type: MPSolverResponseStatus

Arguments: const MPModelProto& input_model, std::string* error_message

Loads model from protocol buffer. Returns MPSOLVER_MODEL_IS_VALID if the model is valid, and another status otherwise (currently only MPSOLVER_MODEL_INVALID and MPSOLVER_INFEASIBLE). If the model isn't valid, populate "error_message".

LoadModelFromProtoWithUniqueNamesOrDie

Return type: MPSolverResponseStatus

Arguments: const MPModelProto& input_model, std::string* error_message

The same as above, except that the loading keeps original variable and constraint names. Caller should make sure that all variable names and constraint names are unique, respectively.

LoadSolutionFromProto

Return type: bool

Arguments: const MPSolutionResponse& response

Load a solution encoded in a protocol buffer onto this solver for easy access via the MPSolver interface. IMPORTANT: This may only be used in conjunction with ExportModel(), following this example: MPSolver my_solver; ... add variables and constraints ... MPModelProto model_proto; my_solver.ExportModelToProto(&model_proto); MPSolutionResponse solver_response; // This can be replaced by a stubby call to the linear solver server. MPSolver::SolveWithProto(model_proto, &solver_response); if (solver_response.result_status() == MPSolutionResponse::OPTIMAL) { CHECK(my_solver.LoadSolutionFromProto(solver_response)); ... inspect the solution using the usual API: solution_value(), etc... } This allows users of the pythonic API to conveniently communicate with a linear solver stubby server, via the MPSolver object as a proxy. See /.linear_solver_server_integration_test.py. The response must be in OPTIMAL or FEASIBLE status. Returns false if a problem arised (typically, if it wasn't used like it should be): - loading a solution whose variables don't correspond to the solver's current variables - loading a solution with a status other than OPTIMAL / FEASIBLE. Note: the variable and objective values aren't checked. You can use VerifySolution() for that.

LookupConstraintOrNull

Return type: MPConstraint*

Arguments: const std::string& constraint_name

Look up a constraint by name, and return NULL if it does not exist.

LookupVariableOrNull

Return type: MPVariable*

Arguments: const std::string& var_name

Look up a variable by name, and return NULL if it does not exist.

MakeBoolVar

Return type: MPVariable*

Arguments: const std::string& name

Creates a boolean variable.

MakeBoolVarArray

Return type: void

Arguments: int nb, const std::string& name, std::vector<MPVariable*>* vars

Creates an array of boolean variables.

MakeIntVar

Return type: MPVariable*

Arguments: double lb, double ub, const std::string& name

Creates an integer variable.

MakeIntVarArray

Return type: void

Arguments: int nb, double lb, double ub, const std::string& name, std::vector<MPVariable*>* vars

Creates an array of integer variables.

MakeNumVar

Return type: MPVariable*

Arguments: double lb, double ub, const std::string& name

Creates a continuous variable.

MakeNumVarArray

Return type: void

Arguments: int nb, double lb, double ub, const std::string& name, std::vector<MPVariable*>* vars

Creates an array of continuous variables.

MakeRowConstraint

Return type: MPConstraint*

Arguments: double lb, double ub

Creates a linear constraint with given bounds. Bounds can be finite or +/- MPSolver::infinity(). The MPSolver class assumes ownership of the constraint. @return a pointer to the newly created constraint.

MakeRowConstraint

Return type: MPConstraint*

Creates a constraint with -infinity and +infinity bounds.

MakeRowConstraint

Return type: MPConstraint*

Arguments: double lb, double ub, const std::string& name

Creates a named constraint with given bounds.

MakeRowConstraint

Return type: MPConstraint*

Arguments: const std::string& name

Creates a named constraint with -infinity and +infinity bounds.

MakeRowConstraint

Return type: MPConstraint*

Arguments: const LinearRange& range

Creates a constraint owned by MPSolver enforcing: range.lower_bound() <= range.linear_expr() <= range.upper_bound()

MakeRowConstraint

Return type: MPConstraint*

Arguments: const LinearRange& range, const std::string& name

As above, but also names the constraint.

MakeVar

Return type: MPVariable*

Arguments: double lb, double ub, bool integer, const std::string& name

Creates a variable with the given bounds, integrality requirement and name. Bounds can be finite or +/- MPSolver::infinity(). The MPSolver owns the variable (i.e. the returned pointer is borrowed). Variable names must be unique (it may crash otherwise). Empty variable names are allowed, an automated variable name will then be assigned.

MakeVarArray

Return type: void

Arguments: int nb, double lb, double ub, bool integer, const std::string& name_prefix, std::vector<MPVariable*>* vars

Creates an array of variables. All variables created have the same bounds and integrality requirement. If nb <= 0, no variables are created, the function crashes in non-opt mode. @param name the prefix of the variable names. Variables are named name0, name1, ...

MPSolver

Arguments: const std::string& name, OptimizationProblemType problem_type

~MPSolver

Return type: virtual

MutableObjective

Return type: MPObjective*

Name

Return type: std::string

nodes

Return type: int64

Returns the number of branch-and-bound nodes. Only available for discrete problems.

NumConstraints

Return type: int

----- Constraints ----- Returns the number of constraints.

NumVariables

Return type: int

----- Variables ------ Returns the number of variables.

Objective

Return type: const MPObjective&

----- Objective ----- Note that the objective is owned by the solver, and is initialized to its default value (see the MPObjective class below) at construction.

OutputIsEnabled

Return type: bool

Controls (or queries) the amount of output produced by the underlying solver. The output can surface to LOGs, or to stdout or stderr, depending on the implementation. The amount of output will greatly vary with each implementation and each problem. Output is suppressed by default.

OwnsVariable

Return type: bool

Arguments: const MPVariable* var

Debugging: verify that the given MPVariable* belongs to this solver.

ProblemType

Return type: virtual OptimizationProblemType

Reset

Return type: void

Advanced usage: resets extracted model to solve from scratch. This won't reset the parameters that were set with SetSolverSpecificParametersAsString() or set_time_limit() or even clear the linear program. It will just make sure that next Solve() will be as if everything was reconstructed from scratch.

set_time_limit

Return type: void

Arguments: int64 time_limit_milliseconds

SetSolverSpecificParametersAsString

Return type: bool

Arguments: const std::string& parameters

Advanced usage: pass solver specific parameters in text format. The format is solver-specific and is the same as the corresponding solver configuration file format. Returns true if the operation was successful. TODO(user): Currently SCIP will always return true even if the format is wrong (you can check the log if you suspect an issue there). This seems to be a bug in SCIP though.

SetStartingLpBasis

Return type: void

Arguments: const std::vector<MPSolver::BasisStatus>& variable_statuses, const std::vector<MPSolver::BasisStatus>& constraint_statuses

Advanced usage: Incrementality. This function takes a starting basis to be used in the next LP Solve() call. The statuses of a current solution can be retrieved via the basis_status() function of a MPVariable or a MPConstraint. WARNING: With Glop, you should disable presolve when using this because this information will not be modified in sync with the presolve and will likely not mean much on the presolved problem.

SolutionValue

Return type: double

Arguments: const LinearExpr& linear_expr

Call only after calling MPSolver::Solve. Evaluates "linear_expr" for the variable values at the solution found by solving.

Solve

Return type: ResultStatus

Solves the problem using default parameter values.

Solve

Return type: ResultStatus

Arguments: const MPSolverParameters& param

Solves the problem using the specified parameter values.

SolverVersion

Return type: std::string

Returns a std::string describing the underlying solver and its version.

SolveWithProto

Return type: static void

Arguments: const MPModelRequest& model_request, MPSolutionResponse* response

Solves the model encoded by a MPModelRequest protocol buffer and fills the solution encoded as a MPSolutionResponse. Note(user): This creates a temporary MPSolver and destroys it at the end. If you want to keep the MPSolver alive (for debugging, or for incremental solving), you should write another version of this function that creates the MPSolver object on the heap and returns it. TODO(user): populate an error message in the response.

SupportsProblemType

Return type: static bool

Arguments: OptimizationProblemType problem_type

Whether the given problem type is supported (this will depend on the targets that you linked).

SuppressOutput

Return type: void

time_limit

Return type: int64

In milliseconds.

time_limit_in_secs

Return type: double

In seconds. Note that this returns a double.

underlying_solver

Return type: void*

Advanced usage: returns the underlying solver so that the user can use solver-specific features or features that are not exposed in the simple API of MPSolver. This method is for advanced users, use at your own risk! In particular, if you modify the model or the solution by accessing the underlying solver directly, then the underlying solver will be out of sync with the information kept in the wrapper (MPSolver, MPVariable, MPConstraint, MPObjective). You need to cast the void* returned back to its original type that depends on the interface (CBC: OsiClpSolverInterface*, CLP: ClpSimplex*, GLPK: glp_prob*, SCIP: SCIP*).

variables

Return type: const std::vector<MPVariable*>&

Returns the array of variables handled by the MPSolver. (They are listed in the order in which they were created.)

VerifySolution

Return type: bool

Arguments: double tolerance, bool log_errors

Advanced usage: Verifies the *correctness* of the solution: all variables must be within their domains, all constraints must be satisfied, and the reported objective value must be accurate. Usage: - This can only be called after Solve() was called. - "tolerance" is interpreted as an absolute error threshold. - For the objective value only, if the absolute error is too large, the tolerance is interpreted as a relative error threshold instead. - If "log_errors" is true, every single violation will be logged. - If "tolerance" is negative, it will be set to infinity(). Most users should just set the --verify_solution flag and not bother using this method directly.

wall_time

Return type: int64

Returns wall_time() in milliseconds since the creation of the solver.

Write

Return type: void

Arguments: const std::string& file_name

Writes the model using the solver internal write function. Currently only available for Gurobi.

Enviar comentarios sobre…