Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
com.google.ortools.linearsolver.MPSolver Class Reference

Classes

enum  BasisStatus
 
enum  OptimizationProblemType
 
enum  ResultStatus
 

Public Member Functions

synchronized void delete ()
 
MPVariable[] makeVarArray (int count, double lb, double ub, boolean integer)
 
MPVariable[] makeVarArray (int count, double lb, double ub, boolean integer, String var_name)
 
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)
 
boolean isMip ()
 
MPSolver.OptimizationProblemType problemType ()
 
void clear ()
 
int numVariables ()
 
MPVariable[] variables ()
 
MPVariable variable (int index)
 
MPVariable lookupVariableOrNull (String var_name)
 
MPVariable makeVar (double lb, double ub, boolean integer, String name)
 
MPVariable makeNumVar (double lb, double ub, String name)
 
MPVariable makeIntVar (double lb, double ub, String name)
 
MPVariable makeBoolVar (String name)
 
int numConstraints ()
 
MPConstraint[] constraints ()
 
MPConstraint constraint (int index)
 
MPConstraint lookupConstraintOrNull (String constraint_name)
 
MPConstraint makeConstraint (double lb, double ub)
 
MPConstraint makeConstraint ()
 
MPConstraint makeConstraint (double lb, double ub, String name)
 
MPConstraint makeConstraint (String name)
 
MPObjective objective ()
 
MPSolver.ResultStatus solve ()
 
MPSolver.ResultStatus solve (MPSolverParameters param)
 
void write (String file_name)
 
double[] computeConstraintActivities ()
 
boolean verifySolution (double tolerance, boolean log_errors)
 
void reset ()
 
boolean interruptSolve ()
 
boolean setSolverSpecificParametersAsString (String parameters)
 
void enableOutput ()
 
void suppressOutput ()
 
long iterations ()
 
long nodes ()
 
String solverVersion ()
 
double computeExactConditionNumber ()
 
void setTimeLimit (long time_limit_milliseconds)
 
long wallTime ()
 
String loadModelFromProto (com.google.ortools.linearsolver.MPModelProto input_model)
 
String loadModelFromProtoKeepNames (com.google.ortools.linearsolver.MPModelProto input_model)
 
String loadModelFromProtoWithUniqueNamesOrDie (com.google.ortools.linearsolver.MPModelProto input_model)
 
com.google.ortools.linearsolver.MPModelProto exportModelToProto ()
 
com.google.ortools.linearsolver.MPSolutionResponse createSolutionResponseProto ()
 
boolean loadSolutionFromProto (com.google.ortools.linearsolver.MPSolutionResponse response)
 
String exportModelAsLpFormat (MPModelExportOptions options)
 
String exportModelAsLpFormat ()
 
String exportModelAsMpsFormat (MPModelExportOptions options)
 
String exportModelAsMpsFormat ()
 
void setHint (MPVariable[] variables, double[] values)
 
boolean setNumThreads (int num_theads)
 

Static Public Member Functions

static MPSolver createSolver (String solver_id)
 
static boolean supportsProblemType (MPSolver.OptimizationProblemType problem_type)
 
static double infinity ()
 
static com.google.ortools.linearsolver.MPSolutionResponse solveWithProto (com.google.ortools.linearsolver.MPModelRequest model_request)
 

Protected Member Functions

 MPSolver (long cPtr, boolean cMemoryOwn)
 
void finalize ()
 

Static Protected Member Functions

static long getCPtr (MPSolver obj)
 
static long swigRelease (MPSolver obj)
 

Protected Attributes

transient boolean swigCMemOwn
 

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.

Constructor & Destructor Documentation

◆ MPSolver() [1/2]

com.google.ortools.linearsolver.MPSolver.MPSolver ( long cPtr,
boolean cMemoryOwn )
protected

Definition at line 21 of file MPSolver.java.

◆ MPSolver() [2/2]

com.google.ortools.linearsolver.MPSolver.MPSolver ( String name,
MPSolver.OptimizationProblemType problem_type )

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

Definition at line 107 of file MPSolver.java.

Member Function Documentation

◆ clear()

void com.google.ortools.linearsolver.MPSolver.clear ( )

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 168 of file MPSolver.java.

◆ computeConstraintActivities()

double[] com.google.ortools.linearsolver.MPSolver.computeConstraintActivities ( )

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 350 of file MPSolver.java.

◆ computeExactConditionNumber()

double com.google.ortools.linearsolver.MPSolver.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. 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 482 of file MPSolver.java.

◆ constraint()

MPConstraint com.google.ortools.linearsolver.MPSolver.constraint ( int index)

Returns the constraint at the given index.

Definition at line 260 of file MPSolver.java.

◆ constraints()

MPConstraint[] com.google.ortools.linearsolver.MPSolver.constraints ( )

Returns the array of constraints handled by the MPSolver.

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

Definition at line 253 of file MPSolver.java.

◆ createSolutionResponseProto()

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

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

Definition at line 533 of file MPSolver.java.

◆ createSolver()

static MPSolver com.google.ortools.linearsolver.MPSolver.createSolver ( String solver_id)
static

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. This can occur if the relevant interface is not linked in, or if
a needed license is not accessible for commercial solvers.

Ownership of the solver is passed on to the caller of this method.
It will accept both string names of the OptimizationProblemType enum, as
well as a short version (i.e. "SCIP_MIXED_INTEGER_PROGRAMMING" or "SCIP").

solver_id is case insensitive, and the following names are supported:

  • CLP_LINEAR_PROGRAMMING or CLP
  • CBC_MIXED_INTEGER_PROGRAMMING or CBC
  • GLOP_LINEAR_PROGRAMMING or GLOP
  • BOP_INTEGER_PROGRAMMING or BOP
  • SAT_INTEGER_PROGRAMMING or SAT or CP_SAT
  • SCIP_MIXED_INTEGER_PROGRAMMING or SCIP
  • GUROBI_LINEAR_PROGRAMMING or GUROBI_LP
  • GUROBI_MIXED_INTEGER_PROGRAMMING or GUROBI or GUROBI_MIP
  • CPLEX_LINEAR_PROGRAMMING or CPLEX_LP
  • CPLEX_MIXED_INTEGER_PROGRAMMING or CPLEX or CPLEX_MIP
  • XPRESS_LINEAR_PROGRAMMING or XPRESS_LP
  • XPRESS_MIXED_INTEGER_PROGRAMMING or XPRESS or XPRESS_MIP
  • GLPK_LINEAR_PROGRAMMING or GLPK_LP
  • GLPK_MIXED_INTEGER_PROGRAMMING or GLPK or GLPK_MIP

Definition at line 139 of file MPSolver.java.

◆ delete()

synchronized void com.google.ortools.linearsolver.MPSolver.delete ( )

Definition at line 47 of file MPSolver.java.

◆ enableOutput()

void com.google.ortools.linearsolver.MPSolver.enableOutput ( )

Enables solver logging.

Definition at line 424 of file MPSolver.java.

◆ exportModelAsLpFormat() [1/2]

String com.google.ortools.linearsolver.MPSolver.exportModelAsLpFormat ( )

Export the loaded model in LP format.

Definition at line 607 of file MPSolver.java.

◆ exportModelAsLpFormat() [2/2]

String com.google.ortools.linearsolver.MPSolver.exportModelAsLpFormat ( MPModelExportOptions options)

Export the loaded model in LP format.

Definition at line 600 of file MPSolver.java.

◆ exportModelAsMpsFormat() [1/2]

String com.google.ortools.linearsolver.MPSolver.exportModelAsMpsFormat ( )

Export the loaded model in MPS format.

Definition at line 621 of file MPSolver.java.

◆ exportModelAsMpsFormat() [2/2]

String com.google.ortools.linearsolver.MPSolver.exportModelAsMpsFormat ( MPModelExportOptions options)

Export the loaded model in MPS format.

Definition at line 614 of file MPSolver.java.

◆ exportModelToProto()

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

Export the loaded model to proto and returns it.

Definition at line 517 of file MPSolver.java.

◆ finalize()

void com.google.ortools.linearsolver.MPSolver.finalize ( )
protected

Definition at line 43 of file MPSolver.java.

◆ getCPtr()

static long com.google.ortools.linearsolver.MPSolver.getCPtr ( MPSolver obj)
staticprotected

Definition at line 26 of file MPSolver.java.

◆ infinity()

static double com.google.ortools.linearsolver.MPSolver.infinity ( )
static

Infinity.

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

Definition at line 417 of file MPSolver.java.

◆ interruptSolve()

boolean com.google.ortools.linearsolver.MPSolver.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. The Solve()
call may still linger for a while depending on the conditions. If
interruption is not supported; returns false and does nothing.
MPSolver::SolverTypeSupportsInterruption can be used to check if
interruption is supported for a given solver type.

Definition at line 398 of file MPSolver.java.

◆ isMip()

boolean com.google.ortools.linearsolver.MPSolver.isMip ( )

Definition at line 152 of file MPSolver.java.

◆ iterations()

long com.google.ortools.linearsolver.MPSolver.iterations ( )

Returns the number of simplex iterations.

Definition at line 438 of file MPSolver.java.

◆ loadModelFromProto()

String com.google.ortools.linearsolver.MPSolver.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. Clears all names (see also loadModelFromProtoKeepNames()).

Definition at line 498 of file MPSolver.java.

◆ loadModelFromProtoKeepNames()

String com.google.ortools.linearsolver.MPSolver.loadModelFromProtoKeepNames ( com.google.ortools.linearsolver.MPModelProto input_model)

Like loadModelFromProto(), but keeps the names and returns an error if
there are duplicate names.

Definition at line 506 of file MPSolver.java.

◆ loadModelFromProtoWithUniqueNamesOrDie()

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

Definition at line 510 of file MPSolver.java.

◆ loadSolutionFromProto()

boolean com.google.ortools.linearsolver.MPSolver.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.

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 577 of file MPSolver.java.

◆ lookupConstraintOrNull()

MPConstraint com.google.ortools.linearsolver.MPSolver.lookupConstraintOrNull ( 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. Will crash if constraint names are not
unique.

Definition at line 272 of file MPSolver.java.

◆ lookupVariableOrNull()

MPVariable com.google.ortools.linearsolver.MPSolver.lookupVariableOrNull ( String var_name)

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 200 of file MPSolver.java.

◆ makeBoolVar()

MPVariable com.google.ortools.linearsolver.MPSolver.makeBoolVar ( String name)

Creates a boolean variable.

Definition at line 236 of file MPSolver.java.

◆ makeBoolVarArray() [1/2]

MPVariable[] com.google.ortools.linearsolver.MPSolver.makeBoolVarArray ( int count)

Definition at line 96 of file MPSolver.java.

◆ makeBoolVarArray() [2/2]

MPVariable[] com.google.ortools.linearsolver.MPSolver.makeBoolVarArray ( int count,
String var_name )

Definition at line 100 of file MPSolver.java.

◆ makeConstraint() [1/4]

MPConstraint com.google.ortools.linearsolver.MPSolver.makeConstraint ( )

Creates a constraint with -infinity and +infinity bounds.

Definition at line 293 of file MPSolver.java.

◆ makeConstraint() [2/4]

MPConstraint com.google.ortools.linearsolver.MPSolver.makeConstraint ( 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.

Returns
a pointer to the newly created constraint.

Definition at line 285 of file MPSolver.java.

◆ makeConstraint() [3/4]

MPConstraint com.google.ortools.linearsolver.MPSolver.makeConstraint ( double lb,
double ub,
String name )

Creates a named constraint with given bounds.

Definition at line 301 of file MPSolver.java.

◆ makeConstraint() [4/4]

MPConstraint com.google.ortools.linearsolver.MPSolver.makeConstraint ( String name)

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

Definition at line 309 of file MPSolver.java.

◆ makeIntVar()

MPVariable com.google.ortools.linearsolver.MPSolver.makeIntVar ( double lb,
double ub,
String name )

Creates an integer variable.

Definition at line 228 of file MPSolver.java.

◆ makeIntVarArray() [1/2]

MPVariable[] com.google.ortools.linearsolver.MPSolver.makeIntVarArray ( int count,
double lb,
double ub )

Definition at line 88 of file MPSolver.java.

◆ makeIntVarArray() [2/2]

MPVariable[] com.google.ortools.linearsolver.MPSolver.makeIntVarArray ( int count,
double lb,
double ub,
String var_name )

Definition at line 92 of file MPSolver.java.

◆ makeNumVar()

MPVariable com.google.ortools.linearsolver.MPSolver.makeNumVar ( double lb,
double ub,
String name )

Creates a continuous variable.

Definition at line 220 of file MPSolver.java.

◆ makeNumVarArray() [1/2]

MPVariable[] com.google.ortools.linearsolver.MPSolver.makeNumVarArray ( int count,
double lb,
double ub )

Definition at line 80 of file MPSolver.java.

◆ makeNumVarArray() [2/2]

MPVariable[] com.google.ortools.linearsolver.MPSolver.makeNumVarArray ( int count,
double lb,
double ub,
String var_name )

Definition at line 84 of file MPSolver.java.

◆ makeVar()

MPVariable com.google.ortools.linearsolver.MPSolver.makeVar ( double lb,
double ub,
boolean integer,
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 are
optional. If you give an empty name, name() will auto-generate one for you
upon request.

Definition at line 212 of file MPSolver.java.

◆ makeVarArray() [1/2]

MPVariable[] com.google.ortools.linearsolver.MPSolver.makeVarArray ( int count,
double lb,
double ub,
boolean integer )

Creates and returns an array of variables.

Definition at line 60 of file MPSolver.java.

◆ makeVarArray() [2/2]

MPVariable[] com.google.ortools.linearsolver.MPSolver.makeVarArray ( int count,
double lb,
double ub,
boolean integer,
String var_name )

Creates and returns an array of named variables.

Definition at line 71 of file MPSolver.java.

◆ nodes()

long com.google.ortools.linearsolver.MPSolver.nodes ( )

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

Only available for discrete problems.

Definition at line 447 of file MPSolver.java.

◆ numConstraints()

int com.google.ortools.linearsolver.MPSolver.numConstraints ( )

Returns the number of constraints.

Definition at line 244 of file MPSolver.java.

◆ numVariables()

int com.google.ortools.linearsolver.MPSolver.numVariables ( )

Returns the number of variables.

Definition at line 175 of file MPSolver.java.

◆ objective()

MPObjective com.google.ortools.linearsolver.MPSolver.objective ( )

Returns the mutable objective object.

Definition at line 317 of file MPSolver.java.

◆ problemType()

MPSolver.OptimizationProblemType com.google.ortools.linearsolver.MPSolver.problemType ( )

Returns the optimization problem type set at construction.

Definition at line 159 of file MPSolver.java.

◆ reset()

void com.google.ortools.linearsolver.MPSolver.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. It will just make sure that next Solve() will be as if
everything was reconstructed from scratch.

Definition at line 384 of file MPSolver.java.

◆ setHint()

void com.google.ortools.linearsolver.MPSolver.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. 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 638 of file MPSolver.java.

◆ setNumThreads()

boolean com.google.ortools.linearsolver.MPSolver.setNumThreads ( int num_theads)

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

Definition at line 645 of file MPSolver.java.

◆ setSolverSpecificParametersAsString()

boolean com.google.ortools.linearsolver.MPSolver.setSolverSpecificParametersAsString ( 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.

Definition at line 408 of file MPSolver.java.

◆ setTimeLimit()

void com.google.ortools.linearsolver.MPSolver.setTimeLimit ( long time_limit_milliseconds)

Definition at line 486 of file MPSolver.java.

◆ solve() [1/2]

MPSolver.ResultStatus com.google.ortools.linearsolver.MPSolver.solve ( )

Solves the problem using the default parameter values.

Definition at line 325 of file MPSolver.java.

◆ solve() [2/2]

MPSolver.ResultStatus com.google.ortools.linearsolver.MPSolver.solve ( MPSolverParameters param)

Solves the problem using the specified parameter values.

Definition at line 332 of file MPSolver.java.

◆ solverVersion()

String com.google.ortools.linearsolver.MPSolver.solverVersion ( )

Returns a string describing the underlying solver and its version.

Definition at line 454 of file MPSolver.java.

◆ solveWithProto()

static com.google.ortools.linearsolver.MPSolutionResponse com.google.ortools.linearsolver.MPSolver.solveWithProto ( com.google.ortools.linearsolver.MPModelRequest model_request)
static

Solves the given model proto and returns a response proto.

Definition at line 584 of file MPSolver.java.

◆ supportsProblemType()

static boolean com.google.ortools.linearsolver.MPSolver.supportsProblemType ( MPSolver.OptimizationProblemType problem_type)
static

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

Definition at line 148 of file MPSolver.java.

◆ suppressOutput()

void com.google.ortools.linearsolver.MPSolver.suppressOutput ( )

Suppresses solver logging.

Definition at line 431 of file MPSolver.java.

◆ swigRelease()

static long com.google.ortools.linearsolver.MPSolver.swigRelease ( MPSolver obj)
staticprotected

Definition at line 30 of file MPSolver.java.

◆ variable()

MPVariable com.google.ortools.linearsolver.MPSolver.variable ( int index)

Returns the variable at position index.

Definition at line 190 of file MPSolver.java.

◆ variables()

MPVariable[] com.google.ortools.linearsolver.MPSolver.variables ( )

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

Definition at line 183 of file MPSolver.java.

◆ verifySolution()

boolean com.google.ortools.linearsolver.MPSolver.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.

Definition at line 372 of file MPSolver.java.

◆ wallTime()

long com.google.ortools.linearsolver.MPSolver.wallTime ( )

Definition at line 490 of file MPSolver.java.

◆ write()

void com.google.ortools.linearsolver.MPSolver.write ( String file_name)

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

Definition at line 340 of file MPSolver.java.

Member Data Documentation

◆ swigCMemOwn

transient boolean com.google.ortools.linearsolver.MPSolver.swigCMemOwn
protected

Definition at line 19 of file MPSolver.java.


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