Java Reference

Java Reference

MPSolver

Detailed Description

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

Definition at line 17 of file MPSolver.java.

Classes

enum  BasisStatus
 Advanced usage: possible basis status values for a variable and the slack
variable of a linear constraint. More...
 
enum  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. More...
 
enum  ResultStatus
 The status of solving the problem. More...
 

Public Member Functions

synchronized void delete ()
 
MPVariable[] makeVarArray (int count, double lb, double ub, boolean integer)
 Creates and returns an array of variables. More...
 
MPVariable[] makeVarArray (int count, double lb, double ub, boolean integer, String var_name)
 Creates and returns an array of named variables. More...
 
MPVariable[] makeNumVarArray (int count, double lb, double ub)
 
MPVariable[] makeNumVarArray (int count, double lb, double ub, String var_name)
 
MPVariable[] makeIntVarArray (int count, double lb, double ub)
 
MPVariable[] makeIntVarArray (int count, double lb, double ub, String var_name)
 
MPVariable[] makeBoolVarArray (int count)
 
MPVariable[] makeBoolVarArray (int count, String var_name)
 
 MPSolver (String name, MPSolver.OptimizationProblemType problem_type)
 Create a solver with the given name and underlying solver backend. More...
 
void clear ()
 Clears the objective (including the optimization direction), all variables
and constraints. More...
 
int numVariables ()
 Returns the number of variables. More...
 
MPVariable[] variables ()
 Returns the array of variables handled by the MPSolver. More...
 
MPVariable lookupVariableOrNull (String var_name)
 Looks up a variable by name, and returns nullptr if it does not exist. More...
 
MPVariable makeVar (double lb, double ub, boolean integer, String name)
 Creates a variable with the given bounds, integrality requirement and
name. More...
 
MPVariable makeNumVar (double lb, double ub, String name)
 Creates a continuous variable. More...
 
MPVariable makeIntVar (double lb, double ub, String name)
 Creates an integer variable. More...
 
MPVariable makeBoolVar (String name)
 Creates a boolean variable. More...
 
int numConstraints ()
 Returns the number of constraints. More...
 
MPConstraint[] constraints ()
 Returns the array of constraints handled by the MPSolver. More...
 
MPConstraint lookupConstraintOrNull (String constraint_name)
 Looks up a constraint by name, and returns nullptr if it does not exist. More...
 
MPConstraint makeConstraint (double lb, double ub)
 Creates a linear constraint with given bounds. More...
 
MPConstraint makeConstraint ()
 Creates a constraint with -infinity and +infinity bounds. More...
 
MPConstraint makeConstraint (double lb, double ub, String name)
 Creates a named constraint with given bounds. More...
 
MPConstraint makeConstraint (String name)
 Creates a named constraint with -infinity and +infinity bounds. More...
 
MPObjective objective ()
 Returns the mutable objective object. More...
 
MPSolver.ResultStatus solve ()
 Solves the problem using the default parameter values. More...
 
MPSolver.ResultStatus solve (MPSolverParameters param)
 Solves the problem using the specified parameter values. More...
 
double[] computeConstraintActivities ()
 Advanced usage: compute the "activities" of all constraints, which are the
sums of their linear terms. More...
 
boolean verifySolution (double tolerance, boolean log_errors)
 Advanced usage: Verifies the correctness of the solution. More...
 
void reset ()
 Advanced usage: resets extracted model to solve from scratch. More...
 
boolean interruptSolve ()
 Interrupts the Solve() execution to terminate processing if possible. More...
 
boolean setSolverSpecificParametersAsString (String parameters)
 Advanced usage: pass solver specific parameters in text format. More...
 
void enableOutput ()
 Enables solver logging. More...
 
void suppressOutput ()
 Suppresses solver logging. More...
 
long iterations ()
 Returns the number of simplex iterations. More...
 
long nodes ()
 Returns the number of branch-and-bound nodes evaluated during the solve. More...
 
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. More...
 
void setTimeLimit (long time_limit_milliseconds)
 
long wallTime ()
 
String loadModelFromProto (com.google.ortools.linearsolver.MPModelProto input_model)
 Loads a model and returns the error message, which will be empty iff the
model is valid. More...
 
String loadModelFromProtoWithUniqueNamesOrDie (com.google.ortools.linearsolver.MPModelProto input_model)
 
com.google.ortools.linearsolver.MPModelProto exportModelToProto ()
 Export the loaded model to proto and returns it. More...
 
com.google.ortools.linearsolver.MPSolutionResponse createSolutionResponseProto ()
 Fills the solution found to a response proto and returns it. More...
 
boolean loadSolutionFromProto (com.google.ortools.linearsolver.MPSolutionResponse response)
 Load a solution encoded in a protocol buffer onto this solver for easy
access via the MPSolver interface. More...
 
String exportModelAsLpFormat (MPModelExportOptions options)
 Export the loaded model in LP format. More...
 
String exportModelAsLpFormat ()
 Export the loaded model in LP format. More...
 
String exportModelAsMpsFormat (MPModelExportOptions options)
 Export the loaded model in MPS format. More...
 
String exportModelAsMpsFormat ()
 Export the loaded model in MPS format. More...
 
void setHint (MPVariable[] variables, double[] values)
 Sets a hint for solution. More...
 
boolean setNumThreads (int num_theads)
 Sets the number of threads to be used by the solver. More...
 

Static Public Member Functions

static boolean supportsProblemType (MPSolver.OptimizationProblemType problem_type)
 Whether the given problem type is supported (this will depend on the
targets that you linked). More...
 
static double infinity ()
 Infinity. More...
 
static com.google.ortools.linearsolver.MPSolutionResponse solveWithProto (com.google.ortools.linearsolver.MPModelRequest model_request)
 Solves the given model proto and returns a response proto. More...
 

Protected Member Functions

 MPSolver (long cPtr, boolean cMemoryOwn)
 

Constructor & Destructor Documentation

◆ MPSolver() [1/2]

MPSolver ( long  cPtr,
boolean  cMemoryOwn 
)
inlineprotected

Definition at line 21 of file MPSolver.java.

◆ MPSolver() [2/2]

MPSolver ( String  name,
MPSolver.OptimizationProblemType  problem_type 
)
inline

Create a solver with the given name and underlying solver backend.

Definition at line 95 of file MPSolver.java.

Member Function Documentation

◆ clear()

void clear ( )
inline

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.

Definition at line 112 of file MPSolver.java.

◆ computeConstraintActivities()

double [] computeConstraintActivities ( )
inline

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.

Definition at line 270 of file MPSolver.java.

◆ computeExactConditionNumber()

double computeExactConditionNumber ( )
inline

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.

Definition at line 393 of file MPSolver.java.

◆ constraints()

MPConstraint [] constraints ( )
inline

Returns the array of constraints handled by the MPSolver.



They are listed in the order in which they were created.

Definition at line 189 of file MPSolver.java.

◆ createSolutionResponseProto()

com.google.ortools.linearsolver.MPSolutionResponse createSolutionResponseProto ( )
inline

Fills the solution found to a response proto and returns it.

Definition at line 436 of file MPSolver.java.

◆ delete()

synchronized void delete ( )
inline

Definition at line 35 of file MPSolver.java.

◆ enableOutput()

void enableOutput ( )
inline

Enables solver logging.

Definition at line 342 of file MPSolver.java.

◆ exportModelAsLpFormat() [1/2]

String exportModelAsLpFormat ( )
inline

Export the loaded model in LP format.

Definition at line 510 of file MPSolver.java.

◆ exportModelAsLpFormat() [2/2]

String exportModelAsLpFormat ( MPModelExportOptions  options)
inline

Export the loaded model in LP format.

Definition at line 503 of file MPSolver.java.

◆ exportModelAsMpsFormat() [1/2]

String exportModelAsMpsFormat ( )
inline

Export the loaded model in MPS format.

Definition at line 524 of file MPSolver.java.

◆ exportModelAsMpsFormat() [2/2]

String exportModelAsMpsFormat ( MPModelExportOptions  options)
inline

Export the loaded model in MPS format.

Definition at line 517 of file MPSolver.java.

◆ exportModelToProto()

com.google.ortools.linearsolver.MPModelProto exportModelToProto ( )
inline

Export the loaded model to proto and returns it.

Definition at line 420 of file MPSolver.java.

◆ infinity()

static double infinity ( )
inlinestatic

Infinity.



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

Definition at line 335 of file MPSolver.java.

◆ interruptSolve()

boolean interruptSolve ( )
inline

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. The Solve()
call may still linger for a while depending on the conditions. If
interruption is not supported; returns false and does nothing.

Definition at line 316 of file MPSolver.java.

◆ iterations()

long iterations ( )
inline

Returns the number of simplex iterations.

Definition at line 356 of file MPSolver.java.

◆ loadModelFromProto()

String loadModelFromProto ( com.google.ortools.linearsolver.MPModelProto  input_model)
inline

Loads a model and returns the error message, which will be empty iff the
model is valid.

Definition at line 409 of file MPSolver.java.

◆ loadModelFromProtoWithUniqueNamesOrDie()

String loadModelFromProtoWithUniqueNamesOrDie ( com.google.ortools.linearsolver.MPModelProto  input_model)
inline

Definition at line 413 of file MPSolver.java.

◆ loadSolutionFromProto()

boolean loadSolutionFromProto ( com.google.ortools.linearsolver.MPSolutionResponse  response)
inline

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;
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...
}



The response must be in OPTIMAL or FEASIBLE status.

Returns a 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 objective value isn't checked. You can use VerifySolution() for
    that.

Definition at line 480 of file MPSolver.java.

◆ lookupConstraintOrNull()

MPConstraint lookupConstraintOrNull ( String  constraint_name)
inline

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. Will crash if constraint names are not
unique.

Definition at line 200 of file MPSolver.java.

◆ lookupVariableOrNull()

MPVariable lookupVariableOrNull ( String  var_name)
inline

Looks up a variable by name, and returns nullptr if it does not exist.

The
first call has a O(n) complexity, as the variable name index is lazily
created upon first use. Will crash if variable names are not unique.

Definition at line 136 of file MPSolver.java.

◆ makeBoolVar()

MPVariable makeBoolVar ( String  name)
inline

Creates a boolean variable.

Definition at line 172 of file MPSolver.java.

◆ makeBoolVarArray() [1/2]

MPVariable [] makeBoolVarArray ( int  count)
inline

Definition at line 84 of file MPSolver.java.

◆ makeBoolVarArray() [2/2]

MPVariable [] makeBoolVarArray ( int  count,
String  var_name 
)
inline

Definition at line 88 of file MPSolver.java.

◆ makeConstraint() [1/4]

MPConstraint makeConstraint ( )
inline

Creates a constraint with -infinity and +infinity bounds.

Definition at line 221 of file MPSolver.java.

◆ makeConstraint() [2/4]

MPConstraint makeConstraint ( double  lb,
double  ub 
)
inline

Creates a linear constraint with given bounds.



Bounds can be finite or +/- MPSolver::infinity(). The MPSolver class
assumes ownership of the constraint.

Returns
a pointer to the newly created constraint.

Definition at line 213 of file MPSolver.java.

◆ makeConstraint() [3/4]

MPConstraint makeConstraint ( double  lb,
double  ub,
String  name 
)
inline

Creates a named constraint with given bounds.

Definition at line 229 of file MPSolver.java.

◆ makeConstraint() [4/4]

MPConstraint makeConstraint ( String  name)
inline

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

Definition at line 237 of file MPSolver.java.

◆ makeIntVar()

MPVariable makeIntVar ( double  lb,
double  ub,
String  name 
)
inline

Creates an integer variable.

Definition at line 164 of file MPSolver.java.

◆ makeIntVarArray() [1/2]

MPVariable [] makeIntVarArray ( int  count,
double  lb,
double  ub 
)
inline

Definition at line 76 of file MPSolver.java.

◆ makeIntVarArray() [2/2]

MPVariable [] makeIntVarArray ( int  count,
double  lb,
double  ub,
String  var_name 
)
inline

Definition at line 80 of file MPSolver.java.

◆ makeNumVar()

MPVariable makeNumVar ( double  lb,
double  ub,
String  name 
)
inline

Creates a continuous variable.

Definition at line 156 of file MPSolver.java.

◆ makeNumVarArray() [1/2]

MPVariable [] makeNumVarArray ( int  count,
double  lb,
double  ub 
)
inline

Definition at line 68 of file MPSolver.java.

◆ makeNumVarArray() [2/2]

MPVariable [] makeNumVarArray ( int  count,
double  lb,
double  ub,
String  var_name 
)
inline

Definition at line 72 of file MPSolver.java.

◆ makeVar()

MPVariable makeVar ( double  lb,
double  ub,
boolean  integer,
String  name 
)
inline

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 are
optional. If you give an empty name, name() will auto-generate one for you
upon request.

Definition at line 148 of file MPSolver.java.

◆ makeVarArray() [1/2]

MPVariable [] makeVarArray ( int  count,
double  lb,
double  ub,
boolean  integer 
)
inline

Creates and returns an array of variables.

Definition at line 48 of file MPSolver.java.

◆ makeVarArray() [2/2]

MPVariable [] makeVarArray ( int  count,
double  lb,
double  ub,
boolean  integer,
String  var_name 
)
inline

Creates and returns an array of named variables.

Definition at line 59 of file MPSolver.java.

◆ nodes()

long nodes ( )
inline

Returns the number of branch-and-bound nodes evaluated during the solve.



Only available for discrete problems.

Definition at line 365 of file MPSolver.java.

◆ numConstraints()

int numConstraints ( )
inline

Returns the number of constraints.

Definition at line 180 of file MPSolver.java.

◆ numVariables()

int numVariables ( )
inline

Returns the number of variables.

Definition at line 119 of file MPSolver.java.

◆ objective()

MPObjective objective ( )
inline

Returns the mutable objective object.

Definition at line 245 of file MPSolver.java.

◆ reset()

void reset ( )
inline

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.

Definition at line 304 of file MPSolver.java.

◆ setHint()

void setHint ( MPVariable[]  variables,
double[]  values 
)
inline

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. A
solver that supports this feature will try to use this information to
create its initial feasible solution.

Note that it may not always be faster to give a hint like this to the
solver. There is also no guarantee that the solver will use this hint or
try to return a solution "close" to this assignment in case of multiple
optimal solutions.

Definition at line 541 of file MPSolver.java.

◆ setNumThreads()

boolean setNumThreads ( int  num_theads)
inline

Sets the number of threads to be used by the solver.

Definition at line 548 of file MPSolver.java.

◆ setSolverSpecificParametersAsString()

boolean setSolverSpecificParametersAsString ( String  parameters)
inline

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.

Definition at line 326 of file MPSolver.java.

◆ setTimeLimit()

void setTimeLimit ( long  time_limit_milliseconds)
inline

Definition at line 397 of file MPSolver.java.

◆ solve() [1/2]

MPSolver.ResultStatus solve ( )
inline

Solves the problem using the default parameter values.

Definition at line 253 of file MPSolver.java.

◆ solve() [2/2]

MPSolver.ResultStatus solve ( MPSolverParameters  param)
inline

Solves the problem using the specified parameter values.

Definition at line 260 of file MPSolver.java.

◆ solveWithProto()

Solves the given model proto and returns a response proto.

Definition at line 487 of file MPSolver.java.

◆ supportsProblemType()

static boolean supportsProblemType ( MPSolver.OptimizationProblemType  problem_type)
inlinestatic

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

Definition at line 103 of file MPSolver.java.

◆ suppressOutput()

void suppressOutput ( )
inline

Suppresses solver logging.

Definition at line 349 of file MPSolver.java.

◆ variables()

MPVariable [] variables ( )
inline

Returns the array of variables handled by the MPSolver.

(They are listed in
the order in which they were created.)

Definition at line 127 of file MPSolver.java.

◆ verifySolution()

boolean verifySolution ( double  tolerance,
boolean  log_errors 
)
inline

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.

Definition at line 292 of file MPSolver.java.

◆ wallTime()

long wallTime ( )
inline

Definition at line 401 of file MPSolver.java.


The documentation for this class was generated from the following file:
MPConstraint[] constraints()
Returns the array of constraints handled by the MPSolver.
Definition: MPSolver.java:189
MPSolver(long cPtr, boolean cMemoryOwn)
Definition: MPSolver.java:21
MPVariable[] variables()
Returns the array of variables handled by the MPSolver.
Definition: MPSolver.java:127