public interface Solver
solve
or adding any constraints,
the LP problem should first be initialized with initVar
.
The objective function can be set with the method addMinimize
or
addMaximize
.
The constraints can be added with addGe
,
addEq
and addLe
.
The solver can import a LP model from a file with readLP and
export to a file with writeLP
.
The solve
method should return the objective value of the LP
found by the solver. That value can also be retrieve with
getObjValue
. The values of the decision variables are returned by
calling getSolution
.
Note : the simplicity of this interface requires that to modify the LP model (add constraints, add objective), a coefficient for each decision variable must be specified for those methods. This simplifies the usage since the expression equals to the scalar product between the coefficients and the decision variables. However, other classes may be implemented or extended to add more suitable functions.
Modifier and Type | Method and Description |
---|---|
int |
addEq(double[] coefficients,
double rhs)
Adds an "Equal" constraint to the LP.
|
int |
addGe(double[] coefficients,
double rhs)
Adds a "Greater or Equal" constraint to the LP.
|
int |
addLe(double[] coefficients,
double rhs)
Adds a "Less or Equal" constraint to the LP.
|
void |
addMaximize(double[] coefficients)
Sets the objective function to maximize.
|
void |
addMaximize(double[] coefficients,
double constant)
Sets the objective function to maximize.
|
void |
addMinimize(double[] coefficients)
Sets the objective function to minimize.
|
void |
addMinimize(double[] coefficients,
double constant)
Sets the objective function to minimize.
|
boolean |
canSolveIP()
Indicates if this class can solve IP problem
|
void |
changeConstraint(int row,
double[] coefficients,
double rhs,
char sense)
Changes a constraint.
|
boolean |
free()
Free the memory used by the solver and erases the current LP/IP.
|
double[] |
getObjCoefficients()
Returns the coefficients of the objective function.
|
double |
getObjConstant()
Returns the constant value of the objective function.
|
String |
getObjSense()
Retrieve the objective sense of the problem.
|
double |
getObjValue()
Returns the objective value found by the solver after
solve
has been called. |
double |
getSlack(int row)
Retrieve the slack value of a specific row (constraint).
|
double[] |
getSolution()
Returns the value for each decision variable of the LP.
|
void |
initIntVar(int[] lowerBound,
int[] upperBound)
If the solver can solve IP, this initializes the integer decision
variables with their lower and upper bounds, else it does nothing.
|
void |
initIntVar(int[] lowerBound,
int[] upperBound,
String[] names)
If the solver can solve IP, this initializes the integer decision
variables with their lower and upper bounds, else it does nothing.
|
void |
initVar(double[] lowerBound,
double[] upperBound)
Initializes the decision variables with their lower and upper bounds.
|
void |
initVar(double[] lowerBound,
double[] upperBound,
String[] names)
Initializes the decision variables with their lower and upper
bounds.
|
void |
readLP(String filename)
Loads a LP model from a file named filename.
|
double |
solve()
Solves the linear problem and returns the objective value.
|
void |
writeLP(String filename)
Writes the current LP model of this solver to a file named
filename with the LP format.
|
double solve() throws SolverException
getSolution
. Other results (solution, slack, pi, reduced costs, ...)
are stored in variables. To retrieve them, the user needs to call the
appropriate functions (which might not be always implemented).SolverException
- if an error occurred while solving.boolean free()
boolean canSolveIP()
void initVar(double[] lowerBound, double[] upperBound) throws IllegalArgumentException, SolverException
lowerBound
- the lower bound for the decision variables.upperBound
- the upper bound for the decision variables.IllegalArgumentException
- if the lower and upper bound
arrays don't have the same length.SolverException
- if could not add the variables to the LP/IP.void initVar(double[] lowerBound, double[] upperBound, String[] names) throws IllegalArgumentException, SolverException
lowerBound
- the lower bound for the decision variables.upperBound
- the upper bound for the decision variables.names
- the names of the variables, set null for default names.IllegalArgumentException
- if the lower and upper bound
arrays don't have the same length.SolverException
- if could not add the variables to the LP/IP.void initIntVar(int[] lowerBound, int[] upperBound) throws IllegalArgumentException, SolverException
initVar
.
Default names are used to name the variables.lowerBound
- the lower boudn for hte decision variables.upperBound
- the upper bound for the decision variables.IllegalArgumentException
- if the lower and upper bound
arrays don't have the same length.SolverException
- if could not add the variables.void initIntVar(int[] lowerBound, int[] upperBound, String[] names) throws IllegalArgumentException, SolverException
initVar
.lowerBound
- the lower boudn for hte decision variables.upperBound
- the upper bound for the decision variables.names
- the names of the variables, set to null for default names.IllegalArgumentException
- if the lower and upper bound
arrays don't have the same length.SolverException
- if could not add the variables.void addMinimize(double[] coefficients) throws IllegalArgumentException, SolverException
coefficients
- coefficient for each decision variable of the objective function.IllegalArgumentException
- if the length of the coefficients array
is not equal to the number of decision variables.SolverException
- if could not add the objective to the LP/IP.void addMinimize(double[] coefficients, double constant) throws IllegalArgumentException, SolverException
coefficients
- coefficient for each decision variable of the objective function.constant
- the constant of the objective function, if there is one.IllegalArgumentException
- if the length of the coefficients array
is not equal to the number of decision variables.SolverException
- if could not add the objective to the LP/IP.void addMaximize(double[] coefficients) throws IllegalArgumentException, SolverException
coefficients
- coefficient for each decision variable of the objective
function.IllegalArgumentException
- if the length of the coefficients array is n
ot equal to the number of decision variables.SolverException
- if could not add the objective to the LP/IP.void addMaximize(double[] coefficients, double constant) throws IllegalArgumentException, SolverException
coefficients
- coefficient for each decision variable of the objective
function.constant
- the constant of the objective function, if there is one.IllegalArgumentException
- if the length of the coefficients array is
not equal to the number of decision variables.SolverException
- if could not add the objective to the LP/IP.double getObjValue() throws NullPointerException
solve
has been called.NullPointerException
- if solve hasn't been called or no solution has been found.double[] getSolution() throws NullPointerException
solve
must has been called first.NullPointerException
- if solve
hasn't been called or
no solution has been found.int addGe(double[] coefficients, double rhs) throws IllegalArgumentException, SolverException
Constraint type : expression >= rhs
coefficients
- the coefficients for each decision variable in the constraint.rhs
- the right hand side value of the constraint.IllegalArgumentException
- if the length of the coefficients array
is not equal to the number of decision variables.SolverException
- if could not add the constraint to the LP/IP.int addEq(double[] coefficients, double rhs) throws IllegalArgumentException, SolverException
Constraint type : expression = rhs
coefficients
- the coefficients for each decision variable in the constraint.rhs
- the right hand side value of the constraint.IllegalArgumentException
- if the length of the coefficients array
is not equal to the number of decision variables.SolverException
- if could not add the constraint to the LP/IP.int addLe(double[] coefficients, double rhs) throws IllegalArgumentException, SolverException
Constraint type : expression <= rhs
coefficients
- the coefficients for each decision variable in the constraintrhs
- the right hand side value of the constraint.IllegalArgumentException
- if the length of the coefficients array is
not equal to the number of decision variables.SolverException
- if could not add the constraint to the LP/IP.void changeConstraint(int row, double[] coefficients, double rhs, char sense) throws SolverException
row
- the constraint's row that is going to change.coefficients
- the coefficients for each decision variable in the
constraint. If null then use the old coefficient.rhs
- the right hand side value of the constraint.sense
- Must the 'g', 'e', 'l'.SolverException
- if could not change the constraint to the LP/IP.double getSlack(int row) throws SolverException
SolverException
- if an error occurred while retrieving the slack.String getObjSense() throws SolverException
SolverException
- if an error occurred while retrieving the objective.double[] getObjCoefficients() throws SolverException
SolverException
- if an error occurred while retrieving the objective coefficients.double getObjConstant() throws SolverException
SolverException
- if an error occurred while retrieving the objective constant.void readLP(String filename) throws IOException
filename
- the name of the LP model file.IOException
- if an error occurred while reading the model from a file.void writeLP(String filename) throws IOException
filename
- the name of the file where the solver will write the current LP model.IOException
- if an error occurred while writing the model to a file.