AIToolbox
A library that offers tools for AI problem solving.
AIToolbox::LP Class Reference

This class presents a common interface for solving Linear Programming problems. More...

#include <AIToolbox/Utils/LP.hpp>

Public Types

enum  Constraint { Constraint::LessEqual, Constraint::Equal, Constraint::GreaterEqual }
 

Public Member Functions

 LP (size_t varNumber)
 Basic constructor. More...
 
 ~LP ()
 Basic destructor to avoid problems with std::unique_ptr. More...
 
void setObjective (size_t n, bool maximize)
 This function selects the variable to use as objective. More...
 
void setObjective (bool maximize)
 This function uses the currently set row as the objective. More...
 
void pushRow (Constraint c, double value)
 This function adds a constraint to the LP. More...
 
void popRow ()
 This function removes the last pushed constraint. More...
 
size_t addColumn ()
 This function adds a new column to the LP. More...
 
std::optional< Vectorsolve (size_t variables, double *objective=nullptr)
 This function solves the LP associated with all constraints in the stack. More...
 
void resize (size_t rows)
 This function resizes the underlying LP. More...
 
void setUnbounded (size_t n)
 This function sets the specified variable as unbounded. More...
 

Static Public Member Functions

static double getPrecision ()
 This function returns the maximum precision obtainable from the solution. More...
 

Public Attributes

Eigen::Map< Vectorrow
 Editable vector containing column coefficients. More...
 

Detailed Description

This class presents a common interface for solving Linear Programming problems.

This class presents a library-agnostic interface to solve Linear Programming problems. Ideally this class can be implemented through different libraries as needed, in order to avoid changing the rest of the library.

Constraints are added through editing the row public Vector. This vector has a number of elements equal to the number of variables specified to the LP class during construction. Each element in the Vector corresponds to the coefficient of the associated variable.

Member Enumeration Documentation

◆ Constraint

Enumerator
LessEqual 
Equal 
GreaterEqual 

Constructor & Destructor Documentation

◆ LP()

AIToolbox::LP::LP ( size_t  varNumber)

Basic constructor.

With this constructor one must specify the number of variables (columns) of the underlying LP problem.

By default all variables are assumed positive (>=0).

Parameters
varNumberThe number of variables.

◆ ~LP()

AIToolbox::LP::~LP ( )

Basic destructor to avoid problems with std::unique_ptr.

std::unique_ptr does not compile with incomplete types, since its generated default constructor needs to know the whole type. If we declare our own, we can postpone this step until the type is known in the source file.

Member Function Documentation

◆ addColumn()

size_t AIToolbox::LP::addColumn ( )

This function adds a new column to the LP.

The inserted column is empty (all previous rows are assumed to not need the newly added variable).

Warning: calling this function will reset the content of the row public variable to an uninitialized space.

Returns
The index of the newly inserted column.

◆ getPrecision()

static double AIToolbox::LP::getPrecision ( )
static

This function returns the maximum precision obtainable from the solution.

This is dependent on the underlying implementation. In general it is unwise to compare returned results from an LP with exact numbers, but if that needs to be done the idea is that this function will give some sort of upper bound on the messiness of the results.

No guarantees though!

Returns
The precision that we hope the solutions, if found, should have.

◆ popRow()

void AIToolbox::LP::popRow ( )

This function removes the last pushed constraint.

◆ pushRow()

void AIToolbox::LP::pushRow ( Constraint  c,
double  value 
)

This function adds a constraint to the LP.

This function adds the current contents of the public field row as a constraints. The row field remains untouched.

Rows are treated as a stack, and thus pushes the new row at the top of the stack.

Parameters
cThe type of constraint that should be enforced.
valueThe value on the other side of the constraint equation.

◆ resize()

void AIToolbox::LP::resize ( size_t  rows)

This function resizes the underlying LP.

This function can be used to both pre-allocate memory in advance before pushing many rows, and to rapidly prune already set rows.

The number passed represents the number of rows that one wants to leave to the LP. If the number is higher than the number of currently set rows, this function may allocate memory to reserve at least the input of rows.

If the number is lower, the effect is equivalent to calling popRow() until the remaining number of rules is equal to the number specified.

Parameters
rowsThe number of rows to preallocate/leave in the LP.

◆ setObjective() [1/2]

void AIToolbox::LP::setObjective ( bool  maximize)

This function uses the currently set row as the objective.

In addition it allows to specify whether the objective should be maximized or minimized.

Parameters
maximizeWhether the variable should be maximized (or minimized).

◆ setObjective() [2/2]

void AIToolbox::LP::setObjective ( size_t  n,
bool  maximize 
)

This function selects the variable to use as objective.

In addition it allows to specify whether that variable should be maximized or minimized.

Parameters
nThe id of the variable to select as objective.
maximizeWhether the variable should be maximized (or minimized).

◆ setUnbounded()

void AIToolbox::LP::setUnbounded ( size_t  n)

This function sets the specified variable as unbounded.

Normally all variables are assumed positive (>=0). This function is needed in case a variable needs to be unbounded.

Parameters
nThe variable to make unbounded.

◆ solve()

std::optional<Vector> AIToolbox::LP::solve ( size_t  variables,
double *  objective = nullptr 
)

This function solves the LP associated with all constraints in the stack.

This function solves the currently set LP problem. If solved, the return Vector will contain the values of the first N variables of the solution, where N is the input.

This function may also return the final result of the objective. The pointer may be written independently from whether the solution was successful or not.

Parameters
variablesThe number of variables one wants the solution of.
objectiveA pointer where to store the result value of the objective.
Returns
A Vector if the solving process succeeded.

Member Data Documentation

◆ row

Eigen::Map<Vector> AIToolbox::LP::row

Editable vector containing column coefficients.

This field will NEVER be touched by this class unless where noted, so you are free to set it as you please, and its value will not be modified.

By default it is NOT initialized!


The documentation for this class was generated from the following file: