Interface LPMatrix<S>

  • Type Parameters:
    S -
    All Known Implementing Classes:
    LPMatrix.DENSE.GUROBI, LPMatrix.DENSE.LPSOLVE, LPMatrix.SPARSE.GUROBI, LPMatrix.SPARSE.LPSOLVE

    public interface LPMatrix<S>
    This interface represents an LP matrix, consisting of an objective function, an internal matrix, a right-hand-side vector, row- and column names, variable types, constraint types and lower and upper bound. The type parameter refers to the type of the external solver used for solving the MIP represented by this object. The interface is borrowed mainly from LpSolve, with the distinct difference that all rows and columns start from 0, i.e. there are [0 .. getNRows()-1] rows and [0 .. getNColumns()-1] columns. The constants GE (greater or equal), LE (less or equal), and EQ (equal) represent the constraint types The constants REAL, INTEGER and BINARY represent the variable types. INFINITE = 1E30 defines infinity. A call to solve(double[] vars) will push the internal structure to the solver, call the external solve function (currently provided for LpSolve and Gurobi) and return OPTIMAL, in case an optimal solution is found, INFEASIBLE, if the model is infeasible or unbounded, or OTHER, if the external solver returned another result. If the solve() call returns OPTIMAL, the array vars is filled with the solution for all variables, i.e. vars needs to be dimensioned [0..getNColumns()-1] by the caller. If the solve() call returns INFEASIBLE or OTHER, the vars array is not touched. The exact result of the external solver can be obtained by a call to getLastSolverResult(). An LPMatrixException is thrown in all methods that are pushed to the external solver and the exception typically wraps the exception thrown by the external solver. The SPARSE and DENSE implementations provided by this interface differ in the way the internal matrix is stored. The SPARSE variants use considerably less memory at the expense of slightly higher time to set elements in the matrix. Additional to an esay solve() method, the interface also provides a toSolver() method, that returns an object of type S, which is a translation of the matrix into the external solver. This method is for more advanced use only. Note that the getOffset() method is to be used in conjunction to make sure that rows and columns are adressed properly in the external representation.
    Author:
    bfvdonge
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  LPMatrix.DENSE
      Static class wrapping DENSE variants of LPMatrix The dense variants of LPMatrix represent the internal MIP matrix explicitly, i.e.
      static class  LPMatrix.SPARSE
      Static class wrapping SPARSE variants of LPMatrix The internal MIP matrix is represented by a hashmap from a pair of coordinates to a value.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static byte BINARY  
      static byte EQ  
      static byte GE  
      static int INFEASIBLE  
      static double INFINITE  
      static byte INTEGER  
      static byte LE  
      static int OPTIMAL  
      static int OTHER  
      static byte REAL  
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void adjustMat​(int row, int column, double value)
      adjusts the value at index row, column in the matrix.
      java.lang.String getColName​(int column)
      Gets the name of a column, or null if there is none.
      java.lang.String[] getColNames()
      Returns the column names or null if there are none.
      byte getConstrType​(int r)  
      int getLastSolverResult()
      returns the last result returned by the external solver.
      double getlowBo​(int c)  
      double getMat​(int row, int column)
      Return the value at index row, column in the MIP matrix.
      int getNcolumns()  
      int getNrows()  
      double getObjective​(int column)  
      int getOffset()  
      double getRh​(int row)  
      java.lang.String getRowName​(int r)  
      java.lang.String[] getRowNames()  
      double getUpBo​(int c)  
      byte getVarType​(int c)  
      double objectiveValue​(double[] vars)  
      void printLp​(java.io.OutputStreamWriter writer, java.lang.String sep)
      prints the LP to the given outputWriter using the given separator character as separator.
      void printLpToCSV​(java.lang.String fileName)
      Prints the MIP to a CSV file using ";" as a separator character.
      double product​(double[] vars, int fromIncluding, int toExcluding, int row)  
      void setBinary​(int c, boolean b)  
      void setColName​(int column, java.lang.String name)
      Sets the name of a column.
      void setConstrType​(int row, byte type)  
      void setInt​(int column, boolean isInt)  
      void setLowbo​(int column, double value)  
      void setMat​(int row, int column, double value)
      set the value at index row, column in the matrix.
      void setMaxim()  
      void setMinim()  
      void setObjective​(int column, double value)  
      void setRh​(int row, double value)  
      void setRhVec​(double[] rhs)  
      void setRowName​(int row, java.lang.String name)
      Sets the name of a row.
      void setUpbo​(int column, double value)  
      int solve​(double[] vars)
      Constructs the solver and solves the MIP represented by this problem.
      S toSolver()
      Constructs the solver in its external representation
    • Method Detail

      • solve

        int solve​(double[] vars)
           throws LPMatrixException
        Constructs the solver and solves the MIP represented by this problem. The returned value is either OPTIMAL, INFEASIBLE or OTHER. The external solver's result can be obtained through getLastSolverResult()
        Parameters:
        vars - an array dimensioned in the number of columns, i.e. vars.length == getNColumns(). This is not checked!
        Returns:
        Throws:
        LPMatrixException - if the external solver throws an exception
      • getLastSolverResult

        int getLastSolverResult()
        returns the last result returned by the external solver. This method only makes sense after a call to solve() that did not throw an exception.
        Returns:
      • getMat

        double getMat​(int row,
                      int column)
        Return the value at index row, column in the MIP matrix.
        Parameters:
        row -
        column -
        Returns:
      • setMat

        void setMat​(int row,
                    int column,
                    double value)
        set the value at index row, column in the matrix.
        Parameters:
        row -
        column -
        value -
      • adjustMat

        void adjustMat​(int row,
                       int column,
                       double value)
        adjusts the value at index row, column in the matrix.
        Parameters:
        row -
        column -
        value -
      • setInt

        void setInt​(int column,
                    boolean isInt)
      • setUpbo

        void setUpbo​(int column,
                     double value)
      • setLowbo

        void setLowbo​(int column,
                      double value)
      • setColName

        void setColName​(int column,
                        java.lang.String name)
        Sets the name of a column. Setting names is not required and no memory is reserved for column names until the first name is set on a column.
        Parameters:
        column -
      • setRowName

        void setRowName​(int row,
                        java.lang.String name)
        Sets the name of a row. Setting names is not required and no memory is reserved for row names until the first name is set on a row.
        Parameters:
        row -
      • setConstrType

        void setConstrType​(int row,
                           byte type)
      • setMaxim

        void setMaxim()
      • setMinim

        void setMinim()
      • getNrows

        int getNrows()
      • setRhVec

        void setRhVec​(double[] rhs)
      • getNcolumns

        int getNcolumns()
      • setRh

        void setRh​(int row,
                   double value)
      • setObjective

        void setObjective​(int column,
                          double value)
      • getRh

        double getRh​(int row)
      • getColName

        java.lang.String getColName​(int column)
        Gets the name of a column, or null if there is none.
        Parameters:
        column -
        Returns:
      • getObjective

        double getObjective​(int column)
      • getColNames

        java.lang.String[] getColNames()
        Returns the column names or null if there are none.
        Parameters:
        column -
        Returns:
      • product

        double product​(double[] vars,
                       int fromIncluding,
                       int toExcluding,
                       int row)
      • setBinary

        void setBinary​(int c,
                       boolean b)
      • getOffset

        int getOffset()
      • printLpToCSV

        void printLpToCSV​(java.lang.String fileName)
                   throws java.io.IOException
        Prints the MIP to a CSV file using ";" as a separator character. If row and column names are provided they are printed, otherwise, they are numbered
        Parameters:
        fileName -
        Throws:
        java.io.IOException
      • printLp

        void printLp​(java.io.OutputStreamWriter writer,
                     java.lang.String sep)
              throws java.io.IOException
        prints the LP to the given outputWriter using the given separator character as separator. The output of this method on System.out, with "\t" as the separator returns the same representation as LpSolve's printLp() method.
        Parameters:
        writer -
        sep -
        Throws:
        java.io.IOException
      • getRowNames

        java.lang.String[] getRowNames()
      • getVarType

        byte getVarType​(int c)
      • getlowBo

        double getlowBo​(int c)
      • getUpBo

        double getUpBo​(int c)
      • getConstrType

        byte getConstrType​(int r)
      • getRowName

        java.lang.String getRowName​(int r)
      • objectiveValue

        double objectiveValue​(double[] vars)