LBJ2.infer
Class XpressMPHook

java.lang.Object
  extended by LBJ2.infer.XpressMPHook
All Implemented Interfaces:
ILPSolver

public class XpressMPHook
extends java.lang.Object
implements ILPSolver

This interface to Xpress-MP from Dash Optimization is designed to work with Xpress-Optimizer 15.25.02. Make sure the file xprs.jar is on your CLASSPATH and that the Xpress-MP libraries are installed appropriately on your system before attempting to compile and use this class.

Note to users: When invoking a classifier in your application that calls this algorithm to do inference, files whose names start with xmp and end with .sol or .glb will be created in the directory from which you invoked your application. These files are created by the Xpress-MP library, and I haven't found a way to avoid it.

Xpress-MP represents an ILP problem as a coefficient matrix in which rows represent constraints and columns represent variables. In the documentation below, the terms "row" and "constraint" are used interchangeably, as are the terms "column" and "variable".


Nested Class Summary
protected static class XpressMPHook.Vector2D
          A two dimensional vector representation implemented here because we are not assuming Java 5.
protected static class XpressMPHook.Vector2DDouble
          A two dimensional vector representation implemented here because we are not assuming Java 5.
protected static class XpressMPHook.Vector2DInt
          A two dimensional vector representation implemented here because we are not assuming Java 5.
 
Field Summary
private  XpressMPHook.Vector2DDouble coefficients
          Contains one vector for each added variable representing a list of non-zero coefficients associated with the variable in the various constraints in the problem.
private  boolean maximize
          Remembers whether the objective function should be maximized or minimzed.
private static int nextID
          Keeps track of the next ID number for an instance of this class.
private  XpressMPHook.Vector2DDouble objective
          Contains one value for each added variable representing that variable's coefficient in the objective function.
private  boolean printProblem
          If true, the parameters to be sent to XPRSloadglobal will be printed to STDOUT just before XPRSloadglobal is called in the solve() method.
private  com.dashoptimization.XPRSprob problem
          Xpress-MP's ILP problem representation is stored here for easy access to the solution values of variables.
private  int problemID
          The ID of an instance is used in the name of the solution file.
private  XpressMPHook.Vector2DDouble rhs
          Contains one value for each added constraint representing its constant right hand side.
private static int ROWEQUAL
          Index into the typeCodes array representing "equal".
private static int ROWGREATERTHAN
          Index into the typeCodes array representing "greater than".
private  XpressMPHook.Vector2DInt rowIndexes
          Contains one vector for each added variable representing a list of row indexes at which the associated variable has a non-zero coefficient.
private static int ROWLESSTHAN
          Index into the typeCodes array representing "less than".
private  XpressMPHook.Vector2DInt rowTypes
          Contains one integer for each added constraint representing its type.
private  XpressMPHook.Vector2DInt setColumns
          Contains one vector for each SOS1 in the ILP problem representing the list of columns involved in the set.
private static byte[] typeCodes
          The elements of this array are suitable for use as elements of the qrtype array parameter of the XPRSloadglobal method.
private  double[] x
          The values of the primal variables after solving the problem.
 
Constructor Summary
XpressMPHook()
          Default constructor.
XpressMPHook(boolean p)
          Use this constructor to control printing of the problem's representation before its solution is carried out.
 
Method Summary
 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.
 void addConstraint(int[] i, double[] a, double b, int 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.
 int[] addDiscreteVariable(Score[] 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.
 void addLessThanConstraint(int[] i, double[] a, double b)
          Adds a new upper bounded constraint to the problem.
private static java.lang.String arrayToString(byte[] a)
          Generates a string representation of the given array.
private static java.lang.String arrayToString(double[] a)
          Generates a string representation of the given array.
private static java.lang.String arrayToString(int[] a)
          Generates a string representation of the given array.
 boolean getBooleanValue(int index)
          When the problem has been solved, use this method to retrieve the value of any Boolean inference variable.
 boolean isSolved()
          Tests whether the problem represented by this ILPSolver instance has been solved already.
 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.
 boolean solve()
          Solves the ILP problem, saving the solution internally.
 void write(java.lang.StringBuffer buffer)
          Creates a textual representation of the ILP problem in an algebraic notation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

typeCodes

private static final byte[] typeCodes
The elements of this array are suitable for use as elements of the qrtype array parameter of the XPRSloadglobal method.


ROWLESSTHAN

private static final int ROWLESSTHAN
Index into the typeCodes array representing "less than".

See Also:
Constant Field Values

ROWEQUAL

private static final int ROWEQUAL
Index into the typeCodes array representing "equal".

See Also:
Constant Field Values

ROWGREATERTHAN

private static final int ROWGREATERTHAN
Index into the typeCodes array representing "greater than".

See Also:
Constant Field Values

nextID

private static int nextID
Keeps track of the next ID number for an instance of this class.


problemID

private int problemID
The ID of an instance is used in the name of the solution file.


printProblem

private boolean printProblem
If true, the parameters to be sent to XPRSloadglobal will be printed to STDOUT just before XPRSloadglobal is called in the solve() method.


problem

private com.dashoptimization.XPRSprob problem
Xpress-MP's ILP problem representation is stored here for easy access to the solution values of variables.


x

private double[] x
The values of the primal variables after solving the problem.


maximize

private boolean maximize
Remembers whether the objective function should be maximized or minimzed.


objective

private XpressMPHook.Vector2DDouble objective
Contains one value for each added variable representing that variable's coefficient in the objective function. This is really a one dimensional vector; I'm using XpressMPHook.Vector2DDouble to mitigate the need to cast things.


rowTypes

private XpressMPHook.Vector2DInt rowTypes
Contains one integer for each added constraint representing its type. This is really a one dimensional vector; I'm using XpressMPHook.Vector2DInt to mitigate the need to cast things.


rhs

private XpressMPHook.Vector2DDouble rhs
Contains one value for each added constraint representing its constant right hand side. This is really a one dimensional vector; I'm using XpressMPHook.Vector2DDouble to mitigate the need to cast things.


rowIndexes

private XpressMPHook.Vector2DInt rowIndexes
Contains one vector for each added variable representing a list of row indexes at which the associated variable has a non-zero coefficient.


coefficients

private XpressMPHook.Vector2DDouble coefficients
Contains one vector for each added variable representing a list of non-zero coefficients associated with the variable in the various constraints in the problem. These coefficients correspond do the row indexes at the same locations in the 2D rowIndexes array.


setColumns

private XpressMPHook.Vector2DInt setColumns
Contains one vector for each SOS1 in the ILP problem representing the list of columns involved in the set.

Constructor Detail

XpressMPHook

public XpressMPHook()
Default constructor.


XpressMPHook

public XpressMPHook(boolean p)
Use this constructor to control printing of the problem's representation before its solution is carried out.

Parameters:
p - If true, a textual representation of the ILP problem will be printed to STDOUT just before XPRSloadglobal is called in the solve() method.
Method Detail

reset

public 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.

Specified by:
reset in interface ILPSolver

setMaximize

public void setMaximize(boolean d)
Sets the direction of the objective function.

Specified by:
setMaximize in interface ILPSolver
Parameters:
d - true if the objective function is to be maximized.

addBooleanVariable

public 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.

Specified by:
addBooleanVariable in interface ILPSolver
Parameters:
c - The objective function coefficient for the new Boolean variable.
Returns:
The indexes of the created variable.

addDiscreteVariable

public 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.

Specified by:
addDiscreteVariable in interface ILPSolver
Parameters:
c - The objective function coefficients for the new Boolean variables.
Returns:
The indexes of the newly created variables.

addDiscreteVariable

public int[] addDiscreteVariable(Score[] 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.

Specified by:
addDiscreteVariable in interface ILPSolver
Parameters:
c - An array of Scores containing the objective function coefficients for the new Boolean variables.
Returns:
The indexes of the newly created variables.

addConstraint

public void addConstraint(int[] i,
                          double[] a,
                          double b,
                          int t)
Adds a new constraint to the problem with the specified type. This method is called by all the other add*Constraint() methods.

Parameters:
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 equality with this constant.
t - The type of linear inequality constraint to add.

addEqualityConstraint

public void addEqualityConstraint(int[] i,
                                  double[] a,
                                  double b)
Adds a new fixed constraint to the problem. The two array arguments must be the same length, as their elements correspond to each other. Variables whose coefficients are zero need not be mentioned. Variables that are mentioned must have previously been added via 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.

Specified by:
addEqualityConstraint in interface ILPSolver
Parameters:
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 equality with this constant.

addGreaterThanConstraint

public void addGreaterThanConstraint(int[] i,
                                     double[] a,
                                     double b)
Adds a new lower bounded constraint to the problem. The two array arguments must be the same length, as their elements correspond to each other. Variables whose coefficients are zero need not be mentioned. Variables that are mentioned must have previously been added via 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.

Specified by:
addGreaterThanConstraint in interface ILPSolver
Parameters:
i - 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.

addLessThanConstraint

public void addLessThanConstraint(int[] i,
                                  double[] a,
                                  double b)
Adds a new upper bounded constraint to the problem. The two array arguments must be the same length, as their elements correspond to each other. Variables whose coefficients are zero need not be mentioned. Variables that are mentioned must have previously been added via 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.

Specified by:
addLessThanConstraint in interface ILPSolver
Parameters:
i - 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.

solve

public boolean solve()
              throws java.lang.Exception
Solves the ILP problem, saving the solution internally.

Specified by:
solve in interface ILPSolver
Returns:
true iff a solution was found successfully.
Throws:
java.lang.Exception

arrayToString

private static java.lang.String arrayToString(byte[] a)
Generates a string representation of the given array.

Parameters:
a - The array to represent as a string.
Returns:
A string representation of a.

arrayToString

private static java.lang.String arrayToString(int[] a)
Generates a string representation of the given array.

Parameters:
a - The array to represent as a string.
Returns:
A string representation of a.

arrayToString

private static java.lang.String arrayToString(double[] a)
Generates a string representation of the given array.

Parameters:
a - The array to represent as a string.
Returns:
A string representation of a.

isSolved

public boolean isSolved()
Tests whether the problem represented by this ILPSolver instance has been solved already.

Specified by:
isSolved in interface ILPSolver

getBooleanValue

public boolean getBooleanValue(int index)
When the problem has been solved, use this method to retrieve the value of any Boolean inference variable. The result of this method is undefined when the problem has not yet been solved.

Specified by:
getBooleanValue in interface ILPSolver
Parameters:
index - The index of the variable whose value is requested.
Returns:
The value of the variable.

write

public void write(java.lang.StringBuffer buffer)
Creates a textual representation of the ILP problem in an algebraic notation.

Specified by:
write in interface ILPSolver
Parameters:
buffer - The created textual representation will be appended here.