public class GurobiHook extends Object implements ILPSolver
CLASSPATH and that the Gurobi libraries are installed
appropriately on your system before attempting to compile and use this class.| Modifier and Type | Field and Description |
|---|---|
protected gurobi.GRBEnv |
environment
The model to be optimized will be associated with this environment.
|
protected boolean |
isSolved
Whether or not the model has been solved.
|
protected gurobi.GRBModel |
model
The model to be optimized.
|
protected boolean |
needsUpdate
Whether or not the
GRBModel.update() method needs to be called before adding
more constraints. |
protected DVector |
objectiveCoefficients
The coefficients of the variables in the objective function.
|
protected OVector |
SOSes
Contains all of the Gurobi SOS objects created for the model.
|
protected OVector |
variables
The model's decision variables.
|
protected int |
verbosity
Verbosity level.
|
VERBOSITY_HIGH, VERBOSITY_LOW, VERBOSITY_NONE| Constructor and Description |
|---|
GurobiHook()
Create a new Gurobi hook with the default environment parameters.
|
GurobiHook(gurobi.GRBEnv env)
Create a new Gurobi hook with the specified environment.
|
GurobiHook(gurobi.GRBEnv env,
int v)
Create a new Gurobi hook with the specified environment.
|
GurobiHook(int v)
Create a new Gurobi hook with the default environment parameters.
|
| Modifier and Type | Method and Description |
|---|---|
int |
addBooleanVariable(double c)
Adds a new Boolean variable (an integer variable constrained to take either the value 0 or
the value 1) with the specified coefficient in the objective function to the problem.
|
protected void |
addConstraint(int[] i,
double[] a,
double b,
char t)
Adds a new constraint to the problem with the specified type.
|
int[] |
addDiscreteVariable(double[] c)
Adds a general, multi-valued discrete variable, which is implemented as a set of Boolean
variables, one per value of the discrete variable, with exactly one of those variables set
true at any given time. |
void |
addEqualityConstraint(int[] i,
double[] a,
double b)
Adds a new fixed constraint to the problem.
|
void |
addGreaterThanConstraint(int[] i,
double[] a,
double b)
Adds a new lower bounded constraint to the problem.
|
int |
addIntegerVariable(double c)
Adds a new Integer variable with the specified coefficient in the objective function to the
problem.
|
void |
addLessThanConstraint(int[] i,
double[] a,
double b)
Adds a new upper bounded constraint to the problem.
|
int |
addRealVariable(double c)
Adds a new Real variable with the specified coefficient in the objective function to the
problem.
|
boolean |
getBooleanValue(int index)
When the problem has been solved, use this method to retrieve the value of any Boolean
inference variable.
|
int |
getIntegerValue(int index)
When the problem has been solved, use this method to retrieve the value of any Integer
inference variable.
|
double |
getRealValue(int index)
When the problem has been solved, use this method to retrieve the value of any Real inference
variable.
|
OVector |
getVariables() |
int |
getVariableSize() |
protected static void |
handleException(gurobi.GRBException e)
Prints an error message and exits the JVM.
|
protected gurobi.GRBVar[] |
idsToVariables(int[] ids)
Given an array of variable indexes, this method returns the corresponding Gurobi variable
objects in an array.
|
boolean |
isSolved()
Tests whether the problem represented by this
ILPSolver instance has been solved
already. |
boolean |
isTimedOut() |
protected gurobi.GRBLinExpr |
makeLinearExpression(int[] ids,
double[] c)
Creates a Gurobi linear expression object representing the dot product of the variables with
the specified indexes and the specified coefficients.
|
double |
objectiveCoeff(int index)
The coefficient of the variable in the objective function.
|
double |
objectiveValue()
When the problem has been solved, use this method to retrieve the value of the objective
function at the solution.
|
void |
printModelStatus() |
void |
printSolution() |
void |
reset()
This method clears the all constraints and variables out of the ILP solver's problem
representation, bringing the
ILPSolver instance back to the state it was in when
first constructed. |
void |
setMaximize(boolean d)
Sets the direction of the objective function.
|
void |
setTimeoutLimit(int limit) |
boolean |
solve()
Solves the ILP problem, saving the solution internally.
|
boolean |
unsat() |
void |
write(StringBuffer buffer)
Creates a textual representation of the ILP problem in an algebraic notation.
|
protected gurobi.GRBEnv environment
protected gurobi.GRBModel model
protected OVector variables
protected OVector SOSes
protected boolean needsUpdate
GRBModel.update() method needs to be called before adding
more constraints.protected boolean isSolved
protected int verbosity
ILPSolver.VERBOSITY_NONE produces no incidental output. If set to
ILPSolver.VERBOSITY_LOW, only variable and constraint counts are reported on
STDOUT. If set to ILPSolver.VERBOSITY_HIGH, a textual representation of
the entire optimization problem is also generated on STDOUT.protected DVector objectiveCoefficients
write(StringBuffer).
Once we get Gurobi 4.0, we can discard of it.public GurobiHook()
public GurobiHook(int v)
v - Setting for the verbosity level.public GurobiHook(gurobi.GRBEnv env)
env - An environment containing user-specified parameters.public GurobiHook(gurobi.GRBEnv env,
int v)
env - An environment containing user-specified parameters.v - Setting for the verbosity level.protected static void handleException(gurobi.GRBException e)
public void setTimeoutLimit(int limit)
public boolean isTimedOut()
public boolean unsat()
public void reset()
ILPSolver instance back to the state it was in when
first constructed.public void setMaximize(boolean d)
setMaximize in interface ILPSolverd - true if the objective function is to be maximized.public int addBooleanVariable(double c)
addBooleanVariable in interface ILPSolverc - The objective function coefficient for the new Boolean variable.public int addIntegerVariable(double c)
ILPSolveraddIntegerVariable in interface ILPSolverc - The objective function coefficient for the new Integer variable.public int addRealVariable(double c)
ILPSolveraddRealVariable in interface ILPSolverc - The objective function coefficient for the new Real variable.public int[] addDiscreteVariable(double[] c)
true at any given time.addDiscreteVariable in interface ILPSolverc - The objective function coefficients for the new Boolean variables.protected void addConstraint(int[] i,
double[] a,
double b,
char t)
add*Constraint() methods.i - The indexes of the variables with non-zero coefficients.a - The coefficients of the variables with the given indexes.b - The new constraint will enforce (in)equality with this constant.t - The type of linear inequality constraint to add.public void addEqualityConstraint(int[] i,
double[] a,
double b)
addBooleanVariable(double) or addDiscreteVariable(double[]). The resulting
constraint has the form: xi * a = b where
xi represents the inference variables whose indexes are contained in
the array i and * represents dot product.addEqualityConstraint in interface ILPSolveri - The indexes of the variables with non-zero coefficients.a - The coefficients of the variables with the given indexes.b - The new constraint will enforce equality with this constant.public void addGreaterThanConstraint(int[] i,
double[] a,
double b)
addBooleanVariable(double) or addDiscreteVariable(double[]). The resulting
constraint has the form: xi * a >= b
where xi represents the inference variables whose indexes are
contained in the array i and * represents dot product.addGreaterThanConstraint in interface ILPSolveri - The indexes of the variables with non-zero coefficients.a - The coefficients of the variables with the given indexes.b - The lower bound for the new constraint.public void addLessThanConstraint(int[] i,
double[] a,
double b)
addBooleanVariable(double) or addDiscreteVariable(double[]). The resulting
constraint has the form: xi * a <= b
where xi represents the inference variables whose indexes are
contained in the array i and * represents dot product.addLessThanConstraint in interface ILPSolveri - The indexes of the variables with non-zero coefficients.a - The coefficients of the variables with the given indexes.b - The upper bound for the new constraint.public void printSolution()
throws gurobi.GRBException
gurobi.GRBExceptionpublic boolean solve()
throws Exception
public void printModelStatus()
throws gurobi.GRBException
gurobi.GRBExceptionpublic boolean isSolved()
ILPSolver instance has been solved
already.public boolean getBooleanValue(int index)
getBooleanValue in interface ILPSolverindex - The index of the variable whose value is requested.public int getIntegerValue(int index)
ILPSolvergetIntegerValue in interface ILPSolverindex - The index of the variable whose value is requested.public double getRealValue(int index)
ILPSolvergetRealValue in interface ILPSolverindex - The index of the variable whose value is requested.public int getVariableSize()
public OVector getVariables()
public double objectiveValue()
objectiveValue in interface ILPSolverpublic double objectiveCoeff(int index)
ILPSolverobjectiveCoeff in interface ILPSolverprotected gurobi.GRBVar[] idsToVariables(int[] ids)
ids - The array of variable indexes.protected gurobi.GRBLinExpr makeLinearExpression(int[] ids,
double[] c)
ids - The indexes of the variables.c - The corresponding coefficients.public void write(StringBuffer buffer)
Copyright © 2017. All rights reserved.