OpenMS
Loading...
Searching...
No Matches
LPWrapper Class Reference

A wrapper class for linear programming (LP) solvers. More...

#include <OpenMS/DATASTRUCTURES/LPWrapper.h>

Collaboration diagram for LPWrapper:
[legend]

Classes

struct  SolverParam
 Struct that holds the parameters of the LP solver. More...
 

Public Types

enum  Type {
  UNBOUNDED = 1 , LOWER_BOUND_ONLY , UPPER_BOUND_ONLY , DOUBLE_BOUNDED ,
  FIXED
}
 Enumeration for variable/constraint bound types. More...
 
enum  VariableType { CONTINUOUS = 1 , INTEGER , BINARY }
 Enumeration for variable types in the LP problem. More...
 
enum  Sense { MIN = 1 , MAX }
 Enumeration for optimization direction. More...
 
enum  WriteFormat { FORMAT_LP = 0 , FORMAT_MPS , FORMAT_GLPK }
 Enumeration for LP problem file formats. More...
 
enum  SOLVER { SOLVER_GLPK = 0 }
 Enumeration for available LP solvers. More...
 
enum  SolverStatus { UNDEFINED = 1 , OPTIMAL = 5 , FEASIBLE = 2 , NO_FEASIBLE_SOL = 4 }
 Enumeration for solver status after solving an LP problem. More...
 

Public Member Functions

 LPWrapper ()
 Default constructor.
 
virtual ~LPWrapper ()
 Virtual destructor.
 
Int addRow (const std::vector< Int > &row_indices, const std::vector< double > &row_values, const String &name)
 Adds a row to the LP matrix.
 
Int addColumn ()
 Adds an empty column to the LP matrix.
 
Int addColumn (const std::vector< Int > &column_indices, const std::vector< double > &column_values, const String &name)
 Adds a column to the LP matrix.
 
Int addRow (const std::vector< Int > &row_indices, const std::vector< double > &row_values, const String &name, double lower_bound, double upper_bound, Type type)
 Adds a row with boundaries to the LP matrix, returns index.
 
Int addColumn (const std::vector< Int > &column_indices, const std::vector< double > &column_values, const String &name, double lower_bound, double upper_bound, Type type)
 Adds a column with boundaries to the LP matrix, returns index.
 
void deleteRow (Int index)
 Delete the row at the specified index.
 
void setColumnName (Int index, const String &name)
 Set the name of a column.
 
String getColumnName (Int index)
 Get the name of a column.
 
String getRowName (Int index)
 Get the name of a row.
 
Int getRowIndex (const String &name)
 Find the index of a row by its name.
 
Int getColumnIndex (const String &name)
 Find the index of a column by its name.
 
double getColumnUpperBound (Int index)
 Get the upper bound of a column.
 
double getColumnLowerBound (Int index)
 Get the lower bound of a column.
 
double getRowUpperBound (Int index)
 Get the upper bound of a row.
 
double getRowLowerBound (Int index)
 Get the lower bound of a row.
 
void setRowName (Int index, const String &name)
 Set the name of a row.
 
void setColumnBounds (Int index, double lower_bound, double upper_bound, Type type)
 Set column bounds.
 
void setRowBounds (Int index, double lower_bound, double upper_bound, Type type)
 Set row bounds.
 
void setColumnType (Int index, VariableType type)
 Set column/variable type.
 
VariableType getColumnType (Int index)
 Get column/variable type.
 
void setObjective (Int index, double obj_value)
 Set the objective coefficient for a column/variable.
 
double getObjective (Int index)
 Get the objective coefficient for a column/variable.
 
void setObjectiveSense (Sense sense)
 Set objective direction.
 
Sense getObjectiveSense ()
 Get the current objective direction.
 
Int getNumberOfColumns ()
 Get the number of columns/variables in the LP problem.
 
Int getNumberOfRows ()
 Get the number of rows/constraints in the LP problem.
 
void setElement (Int row_index, Int column_index, double value)
 Set the value of a matrix element at the specified position.
 
double getElement (Int row_index, Int column_index)
 Get the value of a matrix element at the specified position.
 
void readProblem (const String &filename, const String &format)
 Read LP from file.
 
void writeProblem (const String &filename, const WriteFormat format) const
 Write LP formulation to a file.
 
Int solve (SolverParam &solver_param, const Size verbose_level=0)
 solve problems, parameters like enabled heuristics can be given via solver_param
 
SolverStatus getStatus ()
 Get solution status.
 
double getObjectiveValue ()
 Get the objective function value of the solution.
 
double getColumnValue (Int index)
 Get the value of a variable in the solution.
 
Int getNumberOfNonZeroEntriesInRow (Int idx)
 Get the number of non-zero entries in a specific row.
 
void getMatrixRow (Int idx, std::vector< Int > &indexes)
 Get the indices of non-zero entries in a specific row.
 
SOLVER getSolver () const
 Get the currently active solver backend.
 

Protected Attributes

glp_prob * lp_problem_ = nullptr
 GLPK problem object for the LP problem.
 
SOLVER solver_
 Currently active solver backend.
 

Detailed Description

A wrapper class for linear programming (LP) solvers.

This class provides a unified interface to different linear programming solvers, including GLPK (GNU Linear Programming Kit) and COIN-OR (if available).

Linear programming is a method to find the best outcome in a mathematical model whose requirements are represented by linear relationships. It is used for optimization problems where the objective function and constraints are linear.

LPWrapper allows you to:

  • Create and manipulate LP problems (add rows, columns, set bounds)
  • Set objective functions and constraints
  • Solve the LP problem using different solvers
  • Access the solution and status information

The class supports both continuous and integer variables, allowing for mixed-integer linear programming (MILP) problems.

Member Enumeration Documentation

◆ Sense

enum Sense

Enumeration for optimization direction.

Defines whether the objective function should be minimized or maximized.

Enumerator
MIN 

Minimize the objective function.

MAX 

Maximize the objective function.

◆ SOLVER

enum SOLVER

Enumeration for available LP solvers.

Defines which solver backend to use for solving LP problems.

Enumerator
SOLVER_GLPK 

GNU Linear Programming Kit solver.

◆ SolverStatus

Enumeration for solver status after solving an LP problem.

Defines the possible outcomes after attempting to solve an LP problem.

Enumerator
UNDEFINED 

Status is undefined (e.g., solver not run yet)

OPTIMAL 

Optimal solution found.

FEASIBLE 

Feasible solution found (but not necessarily optimal)

NO_FEASIBLE_SOL 

No feasible solution exists for the problem.

◆ Type

enum Type

Enumeration for variable/constraint bound types.

Defines the type of bounds applied to variables or constraints in the LP problem.

Enumerator
UNBOUNDED 

No bounds (free variable)

LOWER_BOUND_ONLY 

Only lower bound is specified.

UPPER_BOUND_ONLY 

Only upper bound is specified.

DOUBLE_BOUNDED 

Both lower and upper bounds are specified.

FIXED 

Lower bound equals upper bound (fixed value)

◆ VariableType

Enumeration for variable types in the LP problem.

Defines whether variables are continuous or discrete (integer/binary).

Enumerator
CONTINUOUS 

Continuous variable (can take any real value within bounds)

INTEGER 

Integer variable (can only take integer values within bounds)

BINARY 

Binary variable (can only take values 0 or 1)

◆ WriteFormat

Enumeration for LP problem file formats.

Defines the file format used when writing LP problems to disk.

Enumerator
FORMAT_LP 

LP format (human-readable)

FORMAT_MPS 

MPS format (industry standard)

FORMAT_GLPK 

GLPK's native format.

Constructor & Destructor Documentation

◆ LPWrapper()

LPWrapper ( )

Default constructor.

Initializes a new LP problem with the default solver (GLPK or COIN-OR if available).

◆ ~LPWrapper()

virtual ~LPWrapper ( )
virtual

Virtual destructor.

Frees all resources associated with the LP problem.

Member Function Documentation

◆ addColumn() [1/3]

Int addColumn ( )

Adds an empty column to the LP matrix.

Returns
Index of the newly added column

◆ addColumn() [2/3]

Int addColumn ( const std::vector< Int > &  column_indices,
const std::vector< double > &  column_values,
const String name 
)

Adds a column to the LP matrix.

Parameters
[in]column_indicesIndices of the rows that have non-zero coefficients in this column
[in]column_valuesValues of the non-zero coefficients in this column
[in]nameName of the column (for identification purposes)
Returns
Index of the newly added column

◆ addColumn() [3/3]

Int addColumn ( const std::vector< Int > &  column_indices,
const std::vector< double > &  column_values,
const String name,
double  lower_bound,
double  upper_bound,
Type  type 
)

Adds a column with boundaries to the LP matrix, returns index.

Parameters
[in]column_indices
[in]column_values
[in]name
[in]lower_bound
[in]upper_bound
[in]type1 - unbounded, 2 - only lower bound, 3 - only upper bound, 4 - double-bounded variable, 5 - fixed variable

◆ addRow() [1/2]

Int addRow ( const std::vector< Int > &  row_indices,
const std::vector< double > &  row_values,
const String name 
)

Adds a row to the LP matrix.

Parameters
[in]row_indicesIndices of the columns that have non-zero coefficients in this row
[in]row_valuesValues of the non-zero coefficients in this row
[in]nameName of the row (for identification purposes)
Returns
Index of the newly added row

◆ addRow() [2/2]

Int addRow ( const std::vector< Int > &  row_indices,
const std::vector< double > &  row_values,
const String name,
double  lower_bound,
double  upper_bound,
Type  type 
)

Adds a row with boundaries to the LP matrix, returns index.

If you have a fixed variable, GLPK requires to use the "fixed" type, instead of "double-bounded" with equal bounds.

Parameters
[in]row_indices
[in]row_values
[in]name
[in]lower_bound
[in]upper_bound
[in]typeType of the row 1 - unbounded, 2 - only lower bound, 3 - only upper bound, 4 - double-bounded variable, 5 - fixed variable

◆ deleteRow()

void deleteRow ( Int  index)

Delete the row at the specified index.

Parameters
[in]indexIndex of the row to delete

◆ getColumnIndex()

Int getColumnIndex ( const String name)

Find the index of a column by its name.

Parameters
[in]nameName of the column to find
Returns
Index of the column with the given name

◆ getColumnLowerBound()

double getColumnLowerBound ( Int  index)

Get the lower bound of a column.

Parameters
[in]indexIndex of the column
Returns
Lower bound value of the column

◆ getColumnName()

String getColumnName ( Int  index)

Get the name of a column.

Parameters
[in]indexIndex of the column
Returns
Name of the column

◆ getColumnType()

VariableType getColumnType ( Int  index)

Get column/variable type.

Parameters
[in]index
Returns
1- continuous, 2- integer, 3- binary variable

◆ getColumnUpperBound()

double getColumnUpperBound ( Int  index)

Get the upper bound of a column.

Parameters
[in]indexIndex of the column
Returns
Upper bound value of the column

◆ getColumnValue()

double getColumnValue ( Int  index)

Get the value of a variable in the solution.

Parameters
[in]indexIndex of the column/variable
Returns
Value of the variable in the optimal solution

◆ getElement()

double getElement ( Int  row_index,
Int  column_index 
)

Get the value of a matrix element at the specified position.

Parameters
[in]row_indexIndex of the row
[in]column_indexIndex of the column
Returns
Value at the specified position

◆ getMatrixRow()

void getMatrixRow ( Int  idx,
std::vector< Int > &  indexes 
)

Get the indices of non-zero entries in a specific row.

Parameters
[in]idxIndex of the row
[out]indexesVector to store the column indices of non-zero entries

◆ getNumberOfColumns()

Int getNumberOfColumns ( )

Get the number of columns/variables in the LP problem.

Returns
Number of columns in the LP matrix

◆ getNumberOfNonZeroEntriesInRow()

Int getNumberOfNonZeroEntriesInRow ( Int  idx)

Get the number of non-zero entries in a specific row.

Parameters
[in]idxIndex of the row
Returns
Number of non-zero coefficients in the row

◆ getNumberOfRows()

Int getNumberOfRows ( )

Get the number of rows/constraints in the LP problem.

Returns
Number of rows in the LP matrix

◆ getObjective()

double getObjective ( Int  index)

Get the objective coefficient for a column/variable.

Parameters
[in]indexIndex of the column/variable
Returns
Coefficient value in the objective function

◆ getObjectiveSense()

Sense getObjectiveSense ( )

Get the current objective direction.

Returns
Current optimization direction (MIN or MAX)

◆ getObjectiveValue()

double getObjectiveValue ( )

Get the objective function value of the solution.

Returns
Value of the objective function at the optimal solution

◆ getRowIndex()

Int getRowIndex ( const String name)

Find the index of a row by its name.

Parameters
[in]nameName of the row to find
Returns
Index of the row with the given name

◆ getRowLowerBound()

double getRowLowerBound ( Int  index)

Get the lower bound of a row.

Parameters
[in]indexIndex of the row
Returns
Lower bound value of the row

◆ getRowName()

String getRowName ( Int  index)

Get the name of a row.

Parameters
[in]indexIndex of the row
Returns
Name of the row

◆ getRowUpperBound()

double getRowUpperBound ( Int  index)

Get the upper bound of a row.

Parameters
[in]indexIndex of the row
Returns
Upper bound value of the row

◆ getSolver()

SOLVER getSolver ( ) const

Get the currently active solver backend.

Returns
Currently active solver (GLPK or COIN-OR)

◆ getStatus()

SolverStatus getStatus ( )

Get solution status.

Returns
status: 1 - undefined, 2 - integer optimal, 3- integer feasible (no optimality proven), 4- no integer feasible solution

◆ readProblem()

void readProblem ( const String filename,
const String format 
)

Read LP from file.

Parameters
[out]filenameFilename where to store the LP problem.
[in]formatLP, MPS or GLPK.

◆ setColumnBounds()

void setColumnBounds ( Int  index,
double  lower_bound,
double  upper_bound,
Type  type 
)

Set column bounds.

Parameters
[in]index
[in]lower_bound
[in]upper_bound
[in]type1 - unbounded, 2 - only lower bound, 3 - only upper bound, 4 - double-bounded variable, 5 - fixed variable

◆ setColumnName()

void setColumnName ( Int  index,
const String name 
)

Set the name of a column.

Parameters
[in]indexIndex of the column to rename
[in]nameNew name for the column

◆ setColumnType()

void setColumnType ( Int  index,
VariableType  type 
)

Set column/variable type.

Parameters
[in]index
[in]type1- continuous, 2- integer, 3- binary variable

◆ setElement()

void setElement ( Int  row_index,
Int  column_index,
double  value 
)

Set the value of a matrix element at the specified position.

Parameters
[in]row_indexIndex of the row
[in]column_indexIndex of the column
[in]valueValue to set at the specified position

◆ setObjective()

void setObjective ( Int  index,
double  obj_value 
)

Set the objective coefficient for a column/variable.

Parameters
[in]indexIndex of the column/variable
[in]obj_valueCoefficient value in the objective function

◆ setObjectiveSense()

void setObjectiveSense ( Sense  sense)

Set objective direction.

Parameters
[in]sense1- minimize, 2- maximize

◆ setRowBounds()

void setRowBounds ( Int  index,
double  lower_bound,
double  upper_bound,
Type  type 
)

Set row bounds.

Parameters
[in]index
[in]lower_bound
[in]upper_bound
[in]type1 - unbounded, 2 - only lower bound, 3 - only upper bound, 4 - double-bounded variable, 5 - fixed constraint

◆ setRowName()

void setRowName ( Int  index,
const String name 
)

Set the name of a row.

Parameters
[in]indexIndex of the row to rename
[in]nameNew name for the row

◆ solve()

Int solve ( SolverParam solver_param,
const Size  verbose_level = 0 
)

solve problems, parameters like enabled heuristics can be given via solver_param

The verbose level (0,1,2) determines if the solver prints status messages and internals.

Parameters
[in]solver_param
[in]verbose_level
Returns
solver dependent (todo: fix)

◆ writeProblem()

void writeProblem ( const String filename,
const WriteFormat  format 
) const

Write LP formulation to a file.

Parameters
[out]filenameoutput filename, if the filename ends with '.gz' it will be compressed
[in]formatMPS-format is supported by GLPK and COIN-OR; LP and GLPK-formats only by GLPK

Member Data Documentation

◆ lp_problem_

glp_prob* lp_problem_ = nullptr
protected

GLPK problem object for the LP problem.

◆ solver_

SOLVER solver_
protected

Currently active solver backend.