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

A wrapper class for the linear programming (LP) solver. More...

#include <OpenMS/DATASTRUCTURES/LPWrapper.h>

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 }
 Enumeration for LP problem file formats. More...
 
enum  SOLVER { SOLVER_HIGHS }
 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 std::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 std::string &name)
 Adds a column to the LP matrix.
 
Int addRow (const std::vector< Int > &row_indices, const std::vector< double > &row_values, const std::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 std::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 std::string &name)
 Set the name of a column.
 
std::string getColumnName (Int index)
 Get the name of a column.
 
std::string getRowName (Int index)
 Get the name of a row.
 
Int getRowIndex (const std::string &name)
 Find the index of a row by its name.
 
Int getColumnIndex (const std::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 std::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 std::string &filename, const std::string &format)
 Read LP from file.
 
void writeProblem (const std::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 Member Functions

void flushIntegrality_ () const
 Apply staged_integrality_ to the HiGHS model (const: also needed by writeProblem())
 

Protected Attributes

std::unique_ptr< Highs > highs_
 HiGHS solver instance.
 
std::vector< double > solution_
 
std::vector< uint8_t > staged_integrality_
 

Detailed Description

A wrapper class for the linear programming (LP) solver.

This class provides an interface to the HiGHS linear and mixed-integer programming solver (https://highs.dev).

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
  • Access the solution and status information

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


Class Documentation

◆ OpenMS::LPWrapper::SolverParam

struct OpenMS::LPWrapper::SolverParam

Struct that holds the parameters of the LP solver.

The defaults solve to proven optimality without a time limit.

Class Members
bool enable_presolve = true Enable presolve techniques.
double mip_gap = 0.0 Relative gap tolerance for MIP problems (0 = solve to proven optimality)
Int time_limit = (std::numeric_limits<Int>::max)() Time limit per solve() in milliseconds.

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.

HiGHS is the only supported backend (COIN-OR and GLPK were removed).

Enumerator
SOLVER_HIGHS 

HiGHS 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)

Constructor & Destructor Documentation

◆ LPWrapper()

LPWrapper ( )

Default constructor.

Initializes a new, empty LP problem.

◆ ~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 std::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 std::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 std::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 std::string &  name,
double  lower_bound,
double  upper_bound,
Type  type 
)

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

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

◆ flushIntegrality_()

void flushIntegrality_ ( ) const
protected

Apply staged_integrality_ to the HiGHS model (const: also needed by writeProblem())

◆ getColumnIndex()

Int getColumnIndex ( const std::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()

std::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 std::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()

std::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 (always SOLVER_HIGHS)

◆ getStatus()

SolverStatus getStatus ( )

Get solution status.

Call this after solve(): only the states OPTIMAL and FEASIBLE indicate that the solver produced a usable solution, i.e. that the values returned by getColumnValue() and getObjectiveValue() are meaningful.

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

◆ readProblem()

void readProblem ( const std::string &  filename,
const std::string &  format 
)

Read LP from file.

Parameters
[in]filenameFile to read; HiGHS determines the format from the extension (e.g. '.mps', '.lp').
[in]formatIgnored; kept for backwards compatibility.

◆ 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 std::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
Exceptions
Exception::IndexOverflowif index is not a valid column index

◆ 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 std::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
the raw HighsStatus returned by Highs::run(). A code indicating a completed run does not imply that a solution exists. Use getStatus() after solving to check whether an optimal/feasible solution was actually found before reading it via getColumnValue() or getObjectiveValue(). If no feasible solution was produced, a warning is logged.

◆ writeProblem()

void writeProblem ( const std::string &  filename,
const WriteFormat  format 
) const

Write LP formulation to a file.

Parameters
[out]filenameoutput filename; HiGHS determines the format from the extension (e.g. '.mps', '.lp')
[in]formatIgnored; kept for backwards compatibility.

Member Data Documentation

◆ highs_

std::unique_ptr<Highs> highs_
protected

HiGHS solver instance.

◆ solution_

std::vector<double> solution_
protected

Solution vector when using HiGHS

◆ staged_integrality_

std::vector<uint8_t> staged_integrality_
mutableprotected

Column integrality staged by setColumnType() (values of HighsVarType; empty = nothing pending). Highs::changeColIntegrality() is expensive per call, so changes are applied in one batch by flushIntegrality_().