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

Protected Attributes

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

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
column_indicesIndices of the rows that have non-zero coefficients in this column
column_valuesValues of the non-zero coefficients in this column
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
column_indices
column_values
name
lower_bound
upper_bound
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
row_indicesIndices of the columns that have non-zero coefficients in this row
row_valuesValues of the non-zero coefficients in this row
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
row_indices
row_values
name
lower_bound
upper_bound
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
indexIndex of the row to delete

◆ getColumnIndex()

Int getColumnIndex ( const String name)

Find the index of a column by its name.

Parameters
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
indexIndex of the column
Returns
Lower bound value of the column

◆ getColumnName()

String getColumnName ( Int  index)

Get the name of a column.

Parameters
indexIndex of the column
Returns
Name of the column

◆ getColumnType()

VariableType getColumnType ( Int  index)

Get column/variable type.

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

◆ getColumnUpperBound()

double getColumnUpperBound ( Int  index)

Get the upper bound of a column.

Parameters
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
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
row_indexIndex of the row
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
idxIndex of the row
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
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
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
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
indexIndex of the row
Returns
Lower bound value of the row

◆ getRowName()

String getRowName ( Int  index)

Get the name of a row.

Parameters
indexIndex of the row
Returns
Name of the row

◆ getRowUpperBound()

double getRowUpperBound ( Int  index)

Get the upper bound of a row.

Parameters
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
filenameFilename where to store the LP problem.
formatLP, MPS or GLPK.

◆ setColumnBounds()

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

Set column bounds.

Parameters
index
lower_bound
upper_bound
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
indexIndex of the column to rename
nameNew name for the column

◆ setColumnType()

void setColumnType ( Int  index,
VariableType  type 
)

Set column/variable type.

Parameters
index
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
row_indexIndex of the row
column_indexIndex of the column
valueValue to set at the specified position

◆ setObjective()

void setObjective ( Int  index,
double  obj_value 
)

Set the objective coefficient for a column/variable.

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

◆ setObjectiveSense()

void setObjectiveSense ( Sense  sense)

Set objective direction.

Parameters
sense1- minimize, 2- maximize

◆ setRowBounds()

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

Set row bounds.

Parameters
index
lower_bound
upper_bound
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
indexIndex of the row to rename
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
solver_param
verbose_level
Returns
solver dependent (todo: fix)

◆ writeProblem()

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

Write LP formulation to a file.

Parameters
filenameoutput filename, if the filename ends with '.gz' it will be compressed
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.