public class MPSolver
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
MPSolver.BasisStatus
Advanced usage: possible basis status values for a variable and the slack
variable of a linear constraint. |
static class |
MPSolver.OptimizationProblemType
The type of problems (LP or MIP) that will be solved and the underlying
solver (GLOP, GLPK, CLP, CBC or SCIP) that will solve them. |
static class |
MPSolver.ResultStatus
The status of solving the problem.
|
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
swigCMemOwn |
| Modifier | Constructor and Description |
|---|---|
protected |
MPSolver(long cPtr,
boolean cMemoryOwn) |
|
MPSolver(java.lang.String name,
MPSolver.OptimizationProblemType problem_type)
Create a solver with the given name and underlying solver backend.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears the objective (including the optimization direction), all variables
and constraints. |
double[] |
computeConstraintActivities()
Advanced usage: compute the "activities" of all constraints, which are the
sums of their linear terms. |
double |
computeExactConditionNumber()
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. |
MPConstraint |
constraint(int index)
Returns the constraint at the given index.
|
MPConstraint[] |
constraints()
Returns the array of constraints handled by the MPSolver.
They are listed in the order in which they were created. |
MPSolutionResponse |
createSolutionResponseProto()
Fills the solution found to a response proto and returns it.
|
static MPSolver |
createSolver(java.lang.String solver_id)
Recommended factory method to create a MPSolver instance, especially in
non C++ languages. It returns a newly created solver instance if successful, or a nullptr otherwise. |
void |
delete() |
void |
enableOutput()
Enables solver logging.
|
java.lang.String |
exportModelAsLpFormat()
Export the loaded model in LP format.
|
java.lang.String |
exportModelAsLpFormat(MPModelExportOptions options)
Export the loaded model in LP format.
|
java.lang.String |
exportModelAsMpsFormat()
Export the loaded model in MPS format.
|
java.lang.String |
exportModelAsMpsFormat(MPModelExportOptions options)
Export the loaded model in MPS format.
|
MPModelProto |
exportModelToProto()
Export the loaded model to proto and returns it.
|
protected void |
finalize() |
protected static long |
getCPtr(MPSolver obj) |
static double |
infinity()
Infinity.
You can use -MPSolver::infinity() for negative infinity. |
boolean |
interruptSolve()
Interrupts the Solve() execution to terminate processing if possible.
If the underlying interface supports interruption; it does that and returns true regardless of whether there's an ongoing Solve() or not. |
boolean |
isMip() |
long |
iterations()
Returns the number of simplex iterations.
|
java.lang.String |
loadModelFromProto(MPModelProto input_model)
Loads a model and returns the error message, which will be empty iff the
model is valid. |
java.lang.String |
loadModelFromProtoKeepNames(MPModelProto input_model)
Like loadModelFromProto(), but keeps the names and returns an error if
there are duplicate names. |
java.lang.String |
loadModelFromProtoWithUniqueNamesOrDie(MPModelProto input_model) |
boolean |
loadSolutionFromProto(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: {@code MPSolver my_solver; ... |
MPConstraint |
lookupConstraintOrNull(java.lang.String constraint_name)
Looks up a constraint by name, and returns nullptr if it does not exist.
The first call has a O(n) complexity, as the constraint name index is lazily created upon first use. |
MPVariable |
lookupVariableOrNull(java.lang.String var_name)
Looks up a variable by name, and returns nullptr if it does not exist.
|
MPVariable |
makeBoolVar(java.lang.String name)
Creates a boolean variable.
|
MPVariable[] |
makeBoolVarArray(int count) |
MPVariable[] |
makeBoolVarArray(int count,
java.lang.String var_name) |
MPConstraint |
makeConstraint()
Creates a constraint with -infinity and +infinity bounds.
|
MPConstraint |
makeConstraint(double lb,
double ub)
Creates a linear constraint with given bounds.
Bounds can be finite or +/- MPSolver::infinity(). |
MPConstraint |
makeConstraint(double lb,
double ub,
java.lang.String name)
Creates a named constraint with given bounds.
|
MPConstraint |
makeConstraint(java.lang.String name)
Creates a named constraint with -infinity and +infinity bounds.
|
MPVariable |
makeIntVar(double lb,
double ub,
java.lang.String name)
Creates an integer variable.
|
MPVariable[] |
makeIntVarArray(int count,
double lb,
double ub) |
MPVariable[] |
makeIntVarArray(int count,
double lb,
double ub,
java.lang.String var_name) |
MPVariable |
makeNumVar(double lb,
double ub,
java.lang.String name)
Creates a continuous variable.
|
MPVariable[] |
makeNumVarArray(int count,
double lb,
double ub) |
MPVariable[] |
makeNumVarArray(int count,
double lb,
double ub,
java.lang.String var_name) |
MPVariable |
makeVar(double lb,
double ub,
boolean integer,
java.lang.String name)
Creates a variable with the given bounds, integrality requirement and
name. |
MPVariable[] |
makeVarArray(int count,
double lb,
double ub,
boolean integer)
Creates and returns an array of variables.
|
MPVariable[] |
makeVarArray(int count,
double lb,
double ub,
boolean integer,
java.lang.String var_name)
Creates and returns an array of named variables.
|
long |
nodes()
Returns the number of branch-and-bound nodes evaluated during the solve.
Only available for discrete problems. |
int |
numConstraints()
Returns the number of constraints.
|
int |
numVariables()
Returns the number of variables.
|
MPObjective |
objective()
Returns the mutable objective object.
|
MPSolver.OptimizationProblemType |
problemType()
Returns the optimization problem type set at construction.
|
void |
reset()
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. |
void |
setHint(MPVariable[] variables,
double[] values)
Sets a hint for solution.
If a feasible or almost-feasible solution to the problem is already known, it may be helpful to pass it to the solver so that it can be used. |
boolean |
setNumThreads(int num_theads)
Sets the number of threads to be used by the solver.
|
boolean |
setSolverSpecificParametersAsString(java.lang.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. |
void |
setTimeLimit(long time_limit_milliseconds) |
MPSolver.ResultStatus |
solve()
Solves the problem using the default parameter values.
|
MPSolver.ResultStatus |
solve(MPSolverParameters param)
Solves the problem using the specified parameter values.
|
java.lang.String |
solverVersion()
Returns a string describing the underlying solver and its version.
|
static MPSolutionResponse |
solveWithProto(MPModelRequest model_request)
Solves the given model proto and returns a response proto.
|
static boolean |
supportsProblemType(MPSolver.OptimizationProblemType problem_type)
Whether the given problem type is supported (this will depend on the
targets that you linked). |
void |
suppressOutput()
Suppresses solver logging.
|
protected static long |
swigRelease(MPSolver obj) |
MPVariable |
variable(int index)
Returns the variable at position index.
|
MPVariable[] |
variables()
Returns the array of variables handled by the MPSolver.
|
boolean |
verifySolution(double tolerance,
boolean log_errors)
Advanced usage: Verifies the *correctness* of the solution.
It verifies that 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. |
long |
wallTime() |
void |
write(java.lang.String file_name)
Writes the model using the solver internal write function.
|
protected MPSolver(long cPtr,
boolean cMemoryOwn)
public MPSolver(java.lang.String name,
MPSolver.OptimizationProblemType problem_type)
protected static long getCPtr(MPSolver obj)
protected static long swigRelease(MPSolver obj)
protected void finalize()
finalize in class java.lang.Objectpublic void delete()
public MPVariable[] makeVarArray(int count, double lb, double ub, boolean integer)
public MPVariable[] makeVarArray(int count, double lb, double ub, boolean integer, java.lang.String var_name)
public MPVariable[] makeNumVarArray(int count, double lb, double ub)
public MPVariable[] makeNumVarArray(int count, double lb, double ub, java.lang.String var_name)
public MPVariable[] makeIntVarArray(int count, double lb, double ub)
public MPVariable[] makeIntVarArray(int count, double lb, double ub, java.lang.String var_name)
public MPVariable[] makeBoolVarArray(int count)
public MPVariable[] makeBoolVarArray(int count, java.lang.String var_name)
public static MPSolver createSolver(java.lang.String solver_id)
public static boolean supportsProblemType(MPSolver.OptimizationProblemType problem_type)
public boolean isMip()
public MPSolver.OptimizationProblemType problemType()
public void clear()
public int numVariables()
public MPVariable[] variables()
public MPVariable variable(int index)
public MPVariable lookupVariableOrNull(java.lang.String var_name)
public MPVariable makeVar(double lb, double ub, boolean integer, java.lang.String name)
public MPVariable makeNumVar(double lb, double ub, java.lang.String name)
public MPVariable makeIntVar(double lb, double ub, java.lang.String name)
public MPVariable makeBoolVar(java.lang.String name)
public int numConstraints()
public MPConstraint[] constraints()
public MPConstraint constraint(int index)
public MPConstraint lookupConstraintOrNull(java.lang.String constraint_name)
public MPConstraint makeConstraint(double lb, double ub)
public MPConstraint makeConstraint()
public MPConstraint makeConstraint(double lb, double ub, java.lang.String name)
public MPConstraint makeConstraint(java.lang.String name)
public MPObjective objective()
public MPSolver.ResultStatus solve()
public MPSolver.ResultStatus solve(MPSolverParameters param)
public void write(java.lang.String file_name)
public double[] computeConstraintActivities()
public boolean verifySolution(double tolerance,
boolean log_errors)
public void reset()
public boolean interruptSolve()
public boolean setSolverSpecificParametersAsString(java.lang.String parameters)
public static double infinity()
public void enableOutput()
public void suppressOutput()
public long iterations()
public long nodes()
public java.lang.String solverVersion()
public double computeExactConditionNumber()
public void setTimeLimit(long time_limit_milliseconds)
public long wallTime()
public java.lang.String loadModelFromProto(MPModelProto input_model)
public java.lang.String loadModelFromProtoKeepNames(MPModelProto input_model)
public java.lang.String loadModelFromProtoWithUniqueNamesOrDie(MPModelProto input_model)
public MPModelProto exportModelToProto()
public MPSolutionResponse createSolutionResponseProto()
public boolean loadSolutionFromProto(MPSolutionResponse response)
MPSolver my_solver;
... add variables and constraints ...
MPModelProto model_proto;
my_solver.ExportModelToProto(&model_proto);
MPSolutionResponse solver_response;
MPSolver::SolveWithProto(model_proto, &solver_response);
if (solver_response.result_status() == MPSolutionResponse::OPTIMAL) {
CHECK_OK(my_solver.LoadSolutionFromProto(solver_response));
... inspect the solution using the usual API: solution_value(), etc...
}
public static MPSolutionResponse solveWithProto(MPModelRequest model_request)
public java.lang.String exportModelAsLpFormat(MPModelExportOptions options)
public java.lang.String exportModelAsLpFormat()
public java.lang.String exportModelAsMpsFormat(MPModelExportOptions options)
public java.lang.String exportModelAsMpsFormat()
public void setHint(MPVariable[] variables, double[] values)
public boolean setNumThreads(int num_theads)
Copyright © 2024. All rights reserved.