/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
#include<lemon/bits/lp_id.h>
///\brief The interface of the LP solver interface.
/// Function to decide whether a floating point value is finite or not.
/// Retruns true if the argument is not infinity, minus infinity or NaN.
/// It does the same as the isfinite() function defined by C99.
typedef std::numeric_limits<T> Lim;
if ((Lim::has_infinity && (value == Lim::infinity() || value ==
((Lim::has_quiet_NaN || Lim::has_signaling_NaN) && value != value))
///Common base class for LP solvers
///Possible outcomes of an LP solving procedure
///This means that the problem has been successfully solved: either
///an optimal solution has been found or infeasibility/unboundedness
///Any other case (including the case when some user specified
///limit has been exceeded)
///Feasible solution hasn't been found (but may exist).
///\todo NOTFOUND might be a better name.
///The problem has no feasible solution
///Feasible solution found
///Optimal solution exists and found
///The cost function is unbounded
///\todo Give a feasible solution and an infinite ray (and the
///\e The type of the investigated LP problem
PRIMAL_DUAL_FEASIBLE = 0,
///Primal feasible dual infeasible
PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
///Primal infeasible dual feasible
PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
///Primal-dual infeasible
PRIMAL_DUAL_INFEASIBLE = 3,
///Could not determine so far
///The floating point type used by the solver
///The not a number constant
static inline bool isNaN(const Value& v) { return v!=v; }
///Refer to a column of the LP.
///This type is used to refer to a column of the LP.
///Its value remains valid and correct even after the addition or erase of
///\todo Document what can one do with a Col (INVALID, comparing,
///it is similar to Node/Edge)
friend class LpSolverBase;
friend class MipSolverBase;
explicit Col(int _id) : id(_id) {}
typedef True LpSolverCol;
Col(const Invalid&) : id(-1) {}
bool operator< (Col c) const {return id< c.id;}
bool operator> (Col c) const {return id> c.id;}
bool operator==(Col c) const {return id==c.id;}
bool operator!=(Col c) const {return id!=c.id;}
class ColIt : public Col {
ColIt(const LpSolverBase &lp) : _lp(&lp)
ColIt(const Invalid&) : Col(INVALID) {}
static int id(const Col& col) { return col.id; }
///Refer to a row of the LP.
///This type is used to refer to a row of the LP.
///Its value remains valid and correct even after the addition or erase of
///\todo Document what can one do with a Row (INVALID, comparing,
///it is similar to Node/Edge)
friend class LpSolverBase;
explicit Row(int _id) : id(_id) {}
typedef True LpSolverRow;
Row(const Invalid&) : id(-1) {}
bool operator< (Row c) const {return id< c.id;}
bool operator> (Row c) const {return id> c.id;}
bool operator==(Row c) const {return id==c.id;}
bool operator!=(Row c) const {return id!=c.id;}
class RowIt : public Row {
RowIt(const LpSolverBase &lp) : _lp(&lp)
RowIt(const Invalid&) : Row(INVALID) {}
static int id(const Row& row) { return row.id; }
int _lpId(const Col& c) const {
return cols.floatingId(id(c));
int _lpId(const Row& r) const {
return rows.floatingId(id(r));
Col _item(int i, Col) const {
return Col(cols.fixId(i));
Row _item(int i, Row) const {
return Row(rows.fixId(i));
///Linear expression of variables and a constant component
///This data structure stores a linear expression of the variables
///(\ref Col "Col"s) and also has a constant component.
///There are several ways to access and modify the contents of this
///- Its it fully compatible with \c std::map<Col,double>, so for expamle
///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
///read and modify the coefficients like
///or you can also iterate through its elements.
///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
///(This code computes the sum of all coefficients).
///- Numbers (<tt>double</tt>'s)
///and variables (\ref Col "Col"s) directly convert to an
///\ref Expr and the usual linear operations are defined, so
///v*2.1+(3*v+(v*12+w+6)*3)/2
///are valid \ref Expr "Expr"essions.
///The usual assignment operations are also defined.
///e+=2*v-3.12*(v-w/2)+2;
///- The constant member can be set and read by \ref constComp()
///double c=e.constComp();
///\note \ref clear() not only sets all coefficients to 0 but also
///clears the constant components.
class Expr : public std::map<Col,Value>
typedef LpSolverBase::Col Key;
typedef LpSolverBase::Value Value;
typedef std::map<Col,Value> Base;
typedef True IsLinExpression;
Expr() : Base(), const_comp(0) { }
Expr(const Key &v) : const_comp(0) {
Base::insert(std::make_pair(v, 1));
Expr(const Value &v) : const_comp(v) {}
void set(const Key &v,const Value &c) {
Base::insert(std::make_pair(v, c));
Value &constComp() { return const_comp; }
const Value &constComp() const { return const_comp; }
///Removes the components with zero coefficient.
for (Base::iterator i=Base::begin(); i!=Base::end();) {
if ((*i).second==0) Base::erase(i);
const_cast<Expr*>(this)->simplify();
///Removes the coefficients closer to zero than \c tolerance.
void simplify(double &tolerance) {
for (Base::iterator i=Base::begin(); i!=Base::end();) {
if (std::fabs((*i).second)<tolerance) Base::erase(i);
///Sets all coefficients and the constant component to 0.
Expr &operator+=(const Expr &e) {
for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
(*this)[j->first]+=j->second;
const_comp+=e.const_comp;
Expr &operator-=(const Expr &e) {
for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
(*this)[j->first]-=j->second;
const_comp-=e.const_comp;
Expr &operator*=(const Value &c) {
for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
Expr &operator/=(const Value &c) {
for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
///This data stucture represents a linear constraint in the LP.
///Basically it is a linear expression with a lower or an upper bound
///(or both). These parts of the constraint can be obtained by the member
///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
///There are two ways to construct a constraint.
///- You can set the linear expression and the bounds directly
/// by the functions above.
///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
/// are defined between expressions, or even between constraints whenever
/// it makes sense. Therefore if \c e and \c f are linear expressions and
/// \c s and \c t are numbers, then the followings are valid expressions
/// and thus they can be used directly e.g. in \ref addRow() whenever
///\warning The validity of a constraint is checked only at run time, so
///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw
typedef LpSolverBase::Expr Expr;
typedef Expr::Value Value;
Constr() : _expr(), _lb(NaN), _ub(NaN) {}
Constr(Value lb,const Expr &e,Value ub) :
_expr(e), _lb(lb), _ub(ub) {}
Constr(const Expr &e,Value ub) :
_expr(e), _lb(NaN), _ub(ub) {}
Constr(Value lb,const Expr &e) :
_expr(e), _lb(lb), _ub(NaN) {}
_expr(e), _lb(NaN), _ub(NaN) {}
///Reference to the linear expression
Expr &expr() { return _expr; }
///Cont reference to the linear expression
const Expr &expr() const { return _expr; }
///Reference to the lower bound.
///- \ref INF "INF": the constraint is lower unbounded.
///- \ref NaN "NaN": lower bound has not been set.
///- finite number: the lower bound
Value &lowerBound() { return _lb; }
///The const version of \ref lowerBound()
const Value &lowerBound() const { return _lb; }
///Reference to the upper bound.
///- \ref INF "INF": the constraint is upper unbounded.
///- \ref NaN "NaN": upper bound has not been set.
///- finite number: the upper bound
Value &upperBound() { return _ub; }
///The const version of \ref upperBound()
const Value &upperBound() const { return _ub; }
///Is the constraint lower bounded?
bool lowerBounded() const {
///Is the constraint upper bounded?
bool upperBounded() const {
///Linear expression of rows
///This data structure represents a column of the matrix,
///thas is it strores a linear expression of the dual variables
///There are several ways to access and modify the contents of this
///- Its it fully compatible with \c std::map<Row,double>, so for expamle
///if \c e is an DualExpr and \c v
///and \c w are of type \ref Row, then you can
///read and modify the coefficients like
///or you can also iterate through its elements.
///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
///(This code computes the sum of all coefficients).
///- Numbers (<tt>double</tt>'s)
///and variables (\ref Row "Row"s) directly convert to an
///\ref DualExpr and the usual linear operations are defined, so
///v*2.1+(3*v+(v*12+w)*3)/2
///are valid \ref DualExpr "DualExpr"essions.
///The usual assignment operations are also defined.
class DualExpr : public std::map<Row,Value>
typedef LpSolverBase::Row Key;
typedef LpSolverBase::Value Value;
typedef std::map<Row,Value> Base;
typedef True IsLinExpression;
Base::insert(std::make_pair(v, 1));
void set(const Key &v,const Value &c) {
Base::insert(std::make_pair(v, c));
///Removes the components with zero coefficient.
for (Base::iterator i=Base::begin(); i!=Base::end();) {
if ((*i).second==0) Base::erase(i);
const_cast<DualExpr*>(this)->simplify();
///Removes the coefficients closer to zero than \c tolerance.
void simplify(double &tolerance) {
for (Base::iterator i=Base::begin(); i!=Base::end();) {
if (std::fabs((*i).second)<tolerance) Base::erase(i);
///Sets all coefficients to 0.
DualExpr &operator+=(const DualExpr &e) {
for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
(*this)[j->first]+=j->second;
DualExpr &operator-=(const DualExpr &e) {
for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
(*this)[j->first]-=j->second;
DualExpr &operator*=(const Value &c) {
for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
DualExpr &operator/=(const Value &c) {
for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
template <typename _Expr>
class MappedOutputIterator {
typedef std::insert_iterator<_Expr> Base;
typedef std::output_iterator_tag iterator_category;
typedef void difference_type;
MappedOutputIterator(const Base& _base, const LpSolverBase& _lp)
: base(_base), lp(_lp) {}
MappedOutputIterator& operator*() {
MappedOutputIterator& operator=(const std::pair<int, Value>& value) {
*base = std::make_pair(lp._item(value.first, typename _Expr::Key()),
MappedOutputIterator& operator++() {
MappedOutputIterator operator++(int) {
MappedOutputIterator tmp(*this);
bool operator==(const MappedOutputIterator& it) const {
bool operator!=(const MappedOutputIterator& it) const {
class MappedInputIterator {
typedef typename Expr::const_iterator Base;
typedef typename Base::iterator_category iterator_category;
typedef typename Base::difference_type difference_type;
typedef const std::pair<int, Value> value_type;
typedef value_type reference;
pointer(value_type& _value) : value(_value) {}
value_type* operator->() { return &value; }
MappedInputIterator(const Base& _base, const LpSolverBase& _lp)
: base(_base), lp(_lp) {}
return std::make_pair(lp._lpId(base->first), base->second);
return pointer(operator*());
MappedInputIterator& operator++() {
MappedInputIterator operator++(int) {
MappedInputIterator tmp(*this);
bool operator==(const MappedInputIterator& it) const {
bool operator!=(const MappedInputIterator& it) const {
/// STL compatible iterator for lp col
typedef MappedInputIterator<Expr> ConstRowIterator;
/// STL compatible iterator for lp row
typedef MappedInputIterator<DualExpr> ConstColIterator;
/// STL compatible iterator for lp col
typedef MappedOutputIterator<Expr> RowIterator;
/// STL compatible iterator for lp row
typedef MappedOutputIterator<DualExpr> ColIterator;
//Abstract virtual functions
virtual LpSolverBase* _newLp() = 0;
virtual LpSolverBase* _copyLp(){
LpSolverBase* newlp = _newLp();
for (LpSolverBase::ColIt it(*this); it != INVALID; ++it) {
Col ccol = newlp->addCol();
newlp->colName(ccol, colName(it));
newlp->colLowerBound(ccol, colLowerBound(it));
newlp->colUpperBound(ccol, colUpperBound(it));
for (LpSolverBase::RowIt it(*this); it != INVALID; ++it) {
for (Expr::iterator jt = e.begin(); jt != e.end(); ++jt) {
ce[ref[jt->first]] = jt->second;
Row r = newlp->addRow(ce);
getRowBounds(it, lower, upper);
newlp->rowBounds(r, lower, upper);
virtual int _addCol() = 0;
virtual int _addRow() = 0;
virtual void _eraseCol(int col) = 0;
virtual void _eraseRow(int row) = 0;
virtual void _getColName(int col, std::string & name) const = 0;
virtual void _setColName(int col, const std::string & name) = 0;
virtual int _colByName(const std::string& name) const = 0;
virtual void _setRowCoeffs(int i, ConstRowIterator b,
virtual void _getRowCoeffs(int i, RowIterator b) const = 0;
virtual void _setColCoeffs(int i, ConstColIterator b,
virtual void _getColCoeffs(int i, ColIterator b) const = 0;
virtual void _setCoeff(int row, int col, Value value) = 0;
virtual Value _getCoeff(int row, int col) const = 0;
virtual void _setColLowerBound(int i, Value value) = 0;
virtual Value _getColLowerBound(int i) const = 0;
virtual void _setColUpperBound(int i, Value value) = 0;
virtual Value _getColUpperBound(int i) const = 0;
virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
virtual void _getRowBounds(int i, Value &lower, Value &upper) const = 0;
virtual void _setObjCoeff(int i, Value obj_coef) = 0;
virtual Value _getObjCoeff(int i) const = 0;
virtual void _clearObj()=0;
virtual SolveExitStatus _solve() = 0;
virtual Value _getPrimal(int i) const = 0;
virtual Value _getDual(int i) const = 0;
virtual Value _getPrimalValue() const = 0;
virtual bool _isBasicCol(int i) const = 0;
virtual SolutionStatus _getPrimalStatus() const = 0;
virtual SolutionStatus _getDualStatus() const = 0;
virtual ProblemTypes _getProblemType() const = 0;
virtual void _setMax() = 0;
virtual void _setMin() = 0;
virtual bool _isMax() const = 0;
//Constant component of the objective function
LpSolverBase() : obj_const_comp(0) {}
virtual ~LpSolverBase() {}
///Creates a new LP problem
LpSolverBase* newLp() {return _newLp();}
///Makes a copy of the LP problem
LpSolverBase* copyLp() {return _copyLp();}
///\name Build up and modify the LP
///Add a new empty column (i.e a new variable) to the LP
Col addCol() { Col c; _addCol(); c.id = cols.addId(); return c;}
///\brief Adds several new columns
///(i.e a variables) at once
///This magic function takes a container as its argument
///and fills its elements
///with new columns (i.e. variables)
///- a standard STL compatible iterable container with
///\ref Col as its \c values_type
///std::vector<LpSolverBase::Col>
///std::list<LpSolverBase::Col>
///- a standard STL compatible iterable container with
///\ref Col as its \c mapped_type
///std::map<AnyType,LpSolverBase::Col>
///- an iterable lemon \ref concepts::WriteMap "write map" like
///ListGraph::NodeMap<LpSolverBase::Col>
///ListGraph::EdgeMap<LpSolverBase::Col>
///\return The number of the created column.
int addColSet(T &t) { return 0;}
typename enable_if<typename T::value_type::LpSolverCol,int>::type
addColSet(T &t,dummy<0> = 0) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
typename enable_if<typename T::value_type::second_type::LpSolverCol,
addColSet(T &t,dummy<1> = 1) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {
typename enable_if<typename T::MapIt::Value::LpSolverCol,
addColSet(T &t,dummy<2> = 2) {
for(typename T::MapIt i(t); i!=INVALID; ++i)
///Set a column (i.e a dual constraint) of the LP
///\param c is the column to be modified
///\param e is a dual linear expression (see \ref DualExpr)
void col(Col c,const DualExpr &e) {
_setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this),
ConstColIterator(e.end(), *this));
///Get a column (i.e a dual constraint) of the LP
///\param r is the column to get
///\return the dual expression associated to the column
DualExpr col(Col c) const {
_getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this));
///Add a new column to the LP
///\param e is a dual linear expression (see \ref DualExpr)
///\param obj is the corresponding component of the objective
///function. It is 0 by default.
///\return The created column.
Col addCol(const DualExpr &e, Value o = 0) {
///Add a new empty row (i.e a new constraint) to the LP
///This function adds a new empty row (i.e a new constraint) to the LP.
///\return The created row
Row addRow() { Row r; _addRow(); r.id = rows.addId(); return r;}
///\brief Add several new rows
///(i.e a constraints) at once
///This magic function takes a container as its argument
///and fills its elements
///with new row (i.e. variables)
///- a standard STL compatible iterable container with
///\ref Row as its \c values_type
///std::vector<LpSolverBase::Row>
///std::list<LpSolverBase::Row>
///- a standard STL compatible iterable container with
///\ref Row as its \c mapped_type
///std::map<AnyType,LpSolverBase::Row>
///- an iterable lemon \ref concepts::WriteMap "write map" like
///ListGraph::NodeMap<LpSolverBase::Row>
///ListGraph::EdgeMap<LpSolverBase::Row>
///\return The number of rows created.
int addRowSet(T &t) { return 0;}
typename enable_if<typename T::value_type::LpSolverRow,int>::type
addRowSet(T &t,dummy<0> = 0) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
typename enable_if<typename T::value_type::second_type::LpSolverRow,
addRowSet(T &t,dummy<1> = 1) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {
typename enable_if<typename T::MapIt::Value::LpSolverRow,
addRowSet(T &t,dummy<2> = 2) {
for(typename T::MapIt i(t); i!=INVALID; ++i)
///Set a row (i.e a constraint) of the LP
///\param r is the row to be modified
///\param l is lower bound (-\ref INF means no bound)
///\param e is a linear expression (see \ref Expr)
///\param u is the upper bound (\ref INF means no bound)
///\bug This is a temporary function. The interface will change to
///\todo Option to control whether a constraint with a single variable is
void row(Row r, Value l, const Expr &e, Value u) {
_setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this),
ConstRowIterator(e.end(), *this));
_setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp());
///Set a row (i.e a constraint) of the LP
///\param r is the row to be modified
///\param c is a linear expression (see \ref Constr)
void row(Row r, const Constr &c) {
row(r, c.lowerBounded()?c.lowerBound():-INF,
c.expr(), c.upperBounded()?c.upperBound():INF);
///Get a row (i.e a constraint) of the LP
///\param r is the row to get
///\return the expression associated to the row
_getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this));
///Add a new row (i.e a new constraint) to the LP
///\param l is the lower bound (-\ref INF means no bound)
///\param e is a linear expression (see \ref Expr)
///\param u is the upper bound (\ref INF means no bound)
///\return The created row.
///\bug This is a temporary function. The interface will change to
Row addRow(Value l,const Expr &e, Value u) {
///Add a new row (i.e a new constraint) to the LP
///\param c is a linear expression (see \ref Constr)
///\return The created row.
Row addRow(const Constr &c) {
///Erase a coloumn (i.e a variable) from the LP
///\param c is the coloumn to be deleted
///\todo Please check this
///Erase a row (i.e a constraint) from the LP
///\param r is the row to be deleted
///\todo Please check this
/// Get the name of a column
///\param c is the coresponding coloumn
///\return The name of the colunm
std::string colName(Col c) const {
_getColName(_lpId(c), name);
/// Set the name of a column
///\param c is the coresponding coloumn
///\param name The name to be given
void colName(Col c, const std::string& name) {
_setColName(_lpId(c), name);
/// Get the column by its name
///\param name The name of the column
///\return the proper column or \c INVALID
Col colByName(const std::string& name) const {
int k = _colByName(name);
return k != -1 ? Col(cols.fixId(k)) : Col(INVALID);
/// Set an element of the coefficient matrix of the LP
///\param r is the row of the element to be modified
///\param c is the coloumn of the element to be modified
///\param val is the new value of the coefficient
void coeff(Row r, Col c, Value val) {
_setCoeff(_lpId(r),_lpId(c), val);
/// Get an element of the coefficient matrix of the LP
///\param r is the row of the element in question
///\param c is the coloumn of the element in question
///\return the corresponding coefficient
Value coeff(Row r, Col c) const {
return _getCoeff(_lpId(r),_lpId(c));
/// Set the lower bound of a column (i.e a variable)
/// The lower bound of a variable (column) has to be given by an
/// extended number of type Value, i.e. a finite number of type
void colLowerBound(Col c, Value value) {
_setColLowerBound(_lpId(c),value);
/// Get the lower bound of a column (i.e a variable)
/// This function returns the lower bound for column (variable) \t c
/// (this might be -\ref INF as well).
///\return The lower bound for coloumn \t c
Value colLowerBound(Col c) const {
return _getColLowerBound(_lpId(c));
///\brief Set the lower bound of several columns
///(i.e a variables) at once
///This magic function takes a container as its argument
///and applies the function on all of its elements.
/// The lower bound of a variable (column) has to be given by an
/// extended number of type Value, i.e. a finite number of type
void colLowerBound(T &t, Value value) { return 0;}
typename enable_if<typename T::value_type::LpSolverCol,void>::type
colLowerBound(T &t, Value value,dummy<0> = 0) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {
colLowerBound(*i, value);
typename enable_if<typename T::value_type::second_type::LpSolverCol,
colLowerBound(T &t, Value value,dummy<1> = 1) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {
colLowerBound(i->second, value);
typename enable_if<typename T::MapIt::Value::LpSolverCol,
colLowerBound(T &t, Value value,dummy<2> = 2) {
for(typename T::MapIt i(t); i!=INVALID; ++i){
colLowerBound(*i, value);
/// Set the upper bound of a column (i.e a variable)
/// The upper bound of a variable (column) has to be given by an
/// extended number of type Value, i.e. a finite number of type
void colUpperBound(Col c, Value value) {
_setColUpperBound(_lpId(c),value);
/// Get the upper bound of a column (i.e a variable)
/// This function returns the upper bound for column (variable) \t c
/// (this might be \ref INF as well).
///\return The upper bound for coloumn \t c
Value colUpperBound(Col c) const {
return _getColUpperBound(_lpId(c));
///\brief Set the upper bound of several columns
///(i.e a variables) at once
///This magic function takes a container as its argument
///and applies the function on all of its elements.
/// The upper bound of a variable (column) has to be given by an
/// extended number of type Value, i.e. a finite number of type
void colUpperBound(T &t, Value value) { return 0;}
typename enable_if<typename T::value_type::LpSolverCol,void>::type
colUpperBound(T &t, Value value,dummy<0> = 0) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {
colUpperBound(*i, value);
typename enable_if<typename T::value_type::second_type::LpSolverCol,
colUpperBound(T &t, Value value,dummy<1> = 1) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {
colUpperBound(i->second, value);
typename enable_if<typename T::MapIt::Value::LpSolverCol,
colUpperBound(T &t, Value value,dummy<2> = 2) {
for(typename T::MapIt i(t); i!=INVALID; ++i){
colUpperBound(*i, value);
/// Set the lower and the upper bounds of a column (i.e a variable)
/// The lower and the upper bounds of
/// a variable (column) have to be given by an
/// extended number of type Value, i.e. a finite number of type
/// Value, -\ref INF or \ref INF.
void colBounds(Col c, Value lower, Value upper) {
_setColLowerBound(_lpId(c),lower);
_setColUpperBound(_lpId(c),upper);
///\brief Set the lower and the upper bound of several columns
///(i.e a variables) at once
///This magic function takes a container as its argument
///and applies the function on all of its elements.
/// The lower and the upper bounds of
/// a variable (column) have to be given by an
/// extended number of type Value, i.e. a finite number of type
/// Value, -\ref INF or \ref INF.
void colBounds(T &t, Value lower, Value upper) { return 0;}
typename enable_if<typename T::value_type::LpSolverCol,void>::type
colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {
colBounds(*i, lower, upper);
typename enable_if<typename T::value_type::second_type::LpSolverCol,
colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
for(typename T::iterator i=t.begin();i!=t.end();++i) {
colBounds(i->second, lower, upper);
typename enable_if<typename T::MapIt::Value::LpSolverCol,
colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
for(typename T::MapIt i(t); i!=INVALID; ++i){
colBounds(*i, lower, upper);
/// Set the lower and the upper bounds of a row (i.e a constraint)
/// The lower and the upper bound of a constraint (row) have to be
/// given by an extended number of type Value, i.e. a finite
/// number of type Value, -\ref INF or \ref INF. There is no
/// separate function for the lower and the upper bound because
/// that would have been hard to implement for CPLEX.
void rowBounds(Row c, Value lower, Value upper) {
_setRowBounds(_lpId(c),lower, upper);
/// Get the lower and the upper bounds of a row (i.e a constraint)
/// The lower and the upper bound of
/// a constraint (row) are
/// extended numbers of type Value, i.e. finite numbers of type
/// Value, -\ref INF or \ref INF.
/// \todo There is no separate function for the
/// lower and the upper bound because we had problems with the
/// implementation of the setting functions for CPLEX:
/// check out whether this can be done for these functions.
void getRowBounds(Row c, Value &lower, Value &upper) const {
_getRowBounds(_lpId(c),lower, upper);
///Set an element of the objective function
void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); };
///Get an element of the objective function
Value objCoeff(Col c) const { return _getObjCoeff(_lpId(c)); };
///Set the objective function
///\param e is a linear expression of type \ref Expr.
for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
objCoeff((*i).first,(*i).second);
obj_const_comp=e.constComp();
///Get the objective function
///\return the objective function as a linear expression of type \ref Expr.
for (ColIt it(*this); it != INVALID; ++it) {
e.insert(std::make_pair(it, c));
void max() { _setMax(); }
void min() { _setMin(); }
///Query function: is this a maximization problem?
bool isMax() const {return _isMax(); }
///Query function: is this a minimization problem?
bool isMin() const {return !isMax(); }
///\e Solve the LP problem at hand
///\return The result of the optimization procedure. Possible
///values and their meanings can be found in the documentation of
///\todo Which method is used to solve the problem
SolveExitStatus solve() { return _solve(); }
///\name Obtain the solution
/// The status of the primal problem (the original LP problem)
SolutionStatus primalStatus() const {
return _getPrimalStatus();
/// The status of the dual (of the original LP) problem
SolutionStatus dualStatus() const {
///The type of the original LP problem
ProblemTypes problemType() const {
return _getProblemType();
Value primal(Col c) const { return _getPrimal(_lpId(c)); }
Value primal(const Expr& e) const {
double res = e.constComp();
for (std::map<Col, double>::const_iterator it = e.begin();
res += _getPrimal(_lpId(it->first)) * it->second;
Value dual(Row r) const { return _getDual(_lpId(r)); }
Value dual(const DualExpr& e) const {
for (std::map<Row, double>::const_iterator it = e.begin();
res += _getPrimal(_lpId(it->first)) * it->second;
bool isBasicCol(Col c) const { return _isBasicCol(_lpId(c)); }
///- \ref INF or -\ref INF means either infeasibility or unboundedness
/// of the primal problem, depending on whether we minimize or maximize.
///- \ref NaN if no primal solution is found.
///- The (finite) objective value if an optimal solution is found.
Value primalValue() const { return _getPrimalValue()+obj_const_comp;}
/// \brief Common base class for MIP solvers
class MipSolverBase : virtual public LpSolverBase{
///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
///Unfortunately, cplex 7.5 somewhere writes something like
///\todo No support for other types yet.
///Sets the type of the given coloumn to the given type
///Sets the type of the given coloumn to the given type.
void colType(Col c, ColTypes col_type) {
_colType(_lpId(c),col_type);
///Gives back the type of the column.
///Gives back the type of the column.
ColTypes colType(Col c) const {
return _colType(_lpId(c));
///Sets the type of the given Col to integer or remove that property.
///Sets the type of the given Col to integer or remove that property.
void integer(Col c, bool enable) {
///Gives back whether the type of the column is integer or not.
///Gives back the type of the column.
///\return true if the column has integer type and false if not.
bool integer(Col c) const {
return (colType(c)==INT);
/// The status of the MIP problem
SolutionStatus mipStatus() const {
virtual ColTypes _colType(int col) const = 0;
virtual void _colType(int col, ColTypes col_type) = 0;
virtual SolutionStatus _getMipStatus() const = 0;
///\relates LpSolverBase::Expr
inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
const LpSolverBase::Expr &b)
LpSolverBase::Expr tmp(a);
///\relates LpSolverBase::Expr
inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
const LpSolverBase::Expr &b)
LpSolverBase::Expr tmp(a);
///\relates LpSolverBase::Expr
inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
const LpSolverBase::Value &b)
LpSolverBase::Expr tmp(a);
///\relates LpSolverBase::Expr
inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
const LpSolverBase::Expr &b)
LpSolverBase::Expr tmp(b);
///\relates LpSolverBase::Expr
inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
const LpSolverBase::Value &b)
LpSolverBase::Expr tmp(a);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
const LpSolverBase::Expr &f)
return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
const LpSolverBase::Expr &f)
return LpSolverBase::Constr(e,f);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
const LpSolverBase::Value &f)
return LpSolverBase::Constr(-LpSolverBase::INF,e,f);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
const LpSolverBase::Expr &f)
return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
const LpSolverBase::Expr &f)
return LpSolverBase::Constr(f,e);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
const LpSolverBase::Value &f)
return LpSolverBase::Constr(f,e,LpSolverBase::INF);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
const LpSolverBase::Value &f)
return LpSolverBase::Constr(f,e,f);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
const LpSolverBase::Expr &f)
return LpSolverBase::Constr(0,e-f,0);
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
const LpSolverBase::Constr&c)
LpSolverBase::Constr tmp(c);
LEMON_ASSERT(LpSolverBase::isNaN(tmp.lowerBound()), "Wrong LP constraint");
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
const LpSolverBase::Value &n)
LpSolverBase::Constr tmp(c);
LEMON_ASSERT(LpSolverBase::isNaN(tmp.upperBound()), "Wrong LP constraint");
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
const LpSolverBase::Constr&c)
LpSolverBase::Constr tmp(c);
LEMON_ASSERT(LpSolverBase::isNaN(tmp.upperBound()), "Wrong LP constraint");
///\relates LpSolverBase::Constr
inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
const LpSolverBase::Value &n)
LpSolverBase::Constr tmp(c);
LEMON_ASSERT(LpSolverBase::isNaN(tmp.lowerBound()), "Wrong LP constraint");
///\relates LpSolverBase::DualExpr
inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
const LpSolverBase::DualExpr &b)
LpSolverBase::DualExpr tmp(a);
///\relates LpSolverBase::DualExpr
inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
const LpSolverBase::DualExpr &b)
LpSolverBase::DualExpr tmp(a);
///\relates LpSolverBase::DualExpr
inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
const LpSolverBase::Value &b)
LpSolverBase::DualExpr tmp(a);
///\relates LpSolverBase::DualExpr
inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
const LpSolverBase::DualExpr &b)
LpSolverBase::DualExpr tmp(b);
///\relates LpSolverBase::DualExpr
inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
const LpSolverBase::Value &b)
LpSolverBase::DualExpr tmp(a);