8#ifndef LIMBO_SOLVERS_SOLVERS_H
9#define LIMBO_SOLVERS_SOLVERS_H
72class VariableProperty;
76class LinearExpression;
78class LinearConstraint;
79template <
typename T,
typename V>
81template <
typename T,
typename I,
int StartingIndex>
95 explicit Variable(
unsigned int id = std::numeric_limits<unsigned int>::max())
123 bool valid()
const {
return m_id != std::numeric_limits<unsigned int>::max();}
596 template <
typename TermType>
597 bool operator()(TermType
const& t1, TermType
const& t2)
const
599 return t1.variable().id() < t2.variable().id();
822 for (
typename std::vector<term_type>::const_iterator it = expr.
terms().begin(), ite = expr.
terms().end(); it != ite; ++it)
850 for (
typename std::vector<term_type>::iterator it =
m_vTerm.begin(); it !=
m_vTerm.end(); ++it)
851 it->setCoefficient(c*it->coefficient());
870 for (
typename std::vector<term_type>::iterator it =
m_vTerm.begin(); it !=
m_vTerm.end(); ++it)
871 it->setCoefficient(it->coefficient()/c);
886 for (
typename std::vector<term_type>::iterator it =
m_vTerm.begin(); it !=
m_vTerm.end(); ++it)
887 it->setCoefficient(-it->coefficient());
935 typename std::vector<term_type>::iterator itw =
m_vTerm.begin();
936 typename std::vector<term_type>::iterator itr =
m_vTerm.begin();
937 typename std::vector<term_type>::iterator ite =
m_vTerm.end();
938 for (; itr != ite; ++itr)
942 if (itr->variable() == itw->variable())
943 itw->setCoefficient(itw->coefficient()+itr->coefficient());
956 if (itr->coefficient() == 0)
999 :
m_id (std::numeric_limits<unsigned int>::
max())
1050 limboAssertMsg(
m_expr.constant() == 0,
"expression constant should always be zero in constraint");
1057 limboAssertMsg(
m_expr.constant() == 0,
"expression constant should always be zero in constraint");
1066 if ((
m_sense ==
'<' && s ==
'>')
1067 || (
m_sense ==
'>' && s ==
'<'))
1079 limboAssertMsg(
m_expr.constant() == 0,
"expression constant should always be zero in constraint");
1159template <
typename T,
typename V>
1199 this->base_type::operator=(rhs);
1233 std::vector<term_type>
const& vTerm = expr.
terms();
1236 if (vTerm.size() == 1)
1400 for (
unsigned int i = 0; i < n; ++i)
1417 for (
typename std::vector<term_type>::const_iterator it = expr.
terms().begin(), ite = expr.
terms().end(); it != ite; ++it)
1418 result += it->coefficient()*vVariableSol.at(it->variable().id());
1442 if (constr.
sense() ==
'>')
1457 for (
unsigned int i = 0, ie =
m_vConstraint.size(); i < ie; ++i)
1464 void read(std::string
const& filename)
1483 if (r >= (
double)std::numeric_limits<variable_value_type>::max())
1484 ub = std::numeric_limits<variable_value_type>::max();
1485 limboAssertMsg(lb <= ub,
"failed to add bound %g <= %s <= %g", l, vname.c_str(), r);
1490 typename std::map<std::string, variable_type>::const_iterator found =
m_mName2Variable.find(vname);
1493 "failed to insert variable %s to hash table", vname.c_str());
1510 for (LpParser::TermArray::const_iterator it = terms.begin(); it != terms.end(); ++it)
1526 for (LpParser::TermArray::const_iterator it = terms.begin(); it != terms.end(); ++it)
1552 bool print(std::string
const& filename)
const
1554 std::ofstream out (filename.c_str());
1564 std::ostream&
print(std::ostream& os = std::cout)
const
1582 os <<
"\n\nSubject To\n";
1586 for (
typename std::vector<constraint_type>::const_iterator it =
constraints().begin(), ite =
constraints().end(); it != ite; ++it, ++i)
1589 os <<
"C" << i <<
": ";
1605 else if (it->upperBound() >= std::numeric_limits<variable_value_type>::max())
1617 if (it->numericType() ==
BINARY || it->numericType() ==
INTEGER)
1641 for (
typename std::vector<term_type>::const_iterator it = expr.
terms().begin(), ite = expr.
terms().end(); it != ite; ++it, ++i)
1658 if (constr.
sense() ==
'=')
1667 std::ofstream out (filename.c_str());
1680 os <<
"# Objective " << obj <<
"\n";
1714template <
typename T,
typename I,
int StartingIndex = 1>
1847 constraint_type
const* it = vConstraint;
1848 constraint_type
const* ite = vConstraint+nr;
1851 for (; it != ite; ++it, ++i)
1865 for (it = vConstraint; it != ite; ++it)
1867 for (
typename std::vector<typename constraint_type::term_type>::const_iterator itt = it->expression().terms().begin(), itte = it->expression().terms().end(); itt != itte; ++itt, ++i)
1879template <
typename T,
typename I,
int StartingIndex>
#define limboAssertMsg(condition, args...)
custom assertion with message
#define limboAssert(condition)
custom assertion without message
mathematical utilities such as abs
header to include PrintMsg.h and AssertMsg.h
Base class for lp database. Only pure virtual functions are defined. User needs to inheritate this ...
Describe linear constraint.
coefficient_value_type m_rhs
Variable< coefficient_value_type > variable_type
variable type
LinearConstraint & operator-=(term_type term)
LinearConstraint & operator+=(term_type const &term)
void simplify()
simplify expression by merge terms of the same variables
void copy(LinearConstraint const &rhs)
copy object
void setId(unsigned int i)
void swap(LinearConstraint &rhs)
swap with a constraint
void emplaceExpression(expression_type &expr)
coefficient_value_type rightHandSide() const
void scale(coefficient_value_type factor)
scale constraint by a scaling factor
void setRightHandSide(coefficient_value_type rhs)
LinearConstraint & operator-=(expression_type const &expr)
~LinearConstraint()
destructor
LinearTerm< coefficient_value_type > term_type
term type
LinearConstraint & operator=(LinearConstraint const &rhs)
assignment
expression_type const & expression() const
void clearConstant()
move the constant in the expression to rhs For example, x + 5 < 10 will become x < 5
LinearExpression< coefficient_value_type > expression_type
expression type
LinearConstraint(expression_type expr=expression_type(), coefficient_value_type rhs=0, char s='<')
constructor
void normalize(char s)
normalize sense
T coefficient_value_type
coefficient type
void setExpression(expression_type const &expr)
LinearConstraint(LinearConstraint const &rhs)
copy constructor
LinearConstraint & operator+=(expression_type const &expr)
void reserve(unsigned int n)
reserve space for expression terms
Describe linear expressions in optimization problem.
LinearTerm< coefficient_value_type > term_type
term type
coefficient_value_type m_constant
LinearExpression & setConstant(coefficient_value_type constant)
set constant term
friend LinearConstraint< coefficient_value_type > operator==(LinearExpression const &expr, coefficient_value_type rhs)
friend LinearExpression operator+(LinearExpression const &expr1, LinearExpression const &expr2)
friend LinearExpression operator+(term_type const &term, LinearExpression const &expr)
friend LinearConstraint< coefficient_value_type > operator<=(LinearExpression const &expr, coefficient_value_type rhs)
overload <=, same as <
friend LinearExpression operator-(term_type const &term, LinearExpression const &expr)
friend LinearExpression operator-(coefficient_value_type constant, LinearExpression const &expr)
friend LinearExpression operator*(LinearExpression const &expr, coefficient_value_type c)
friend LinearExpression operator+(LinearExpression const &expr, coefficient_value_type constant)
LinearExpression & operator+=(coefficient_value_type constant)
std::vector< term_type > const & terms() const
LinearExpression(term_type const &term, coefficient_value_type constant=0)
constructor
void reserve(unsigned int n)
reserve space for terms
LinearExpression & operator-=(coefficient_value_type constant)
friend LinearConstraint< coefficient_value_type > operator<(LinearExpression const &expr, coefficient_value_type rhs)
overload <
LinearExpression & operator-=(LinearExpression const &expr)
LinearExpression()
constructor
friend LinearExpression operator-(LinearExpression const &expr, coefficient_value_type constant)
coefficient_value_type constant() const
LinearExpression & incrementConstant(coefficient_value_type constant)
increment constant term
friend LinearExpression operator/(LinearExpression const &expr, coefficient_value_type c)
void clear()
clear expression
LinearExpression & operator*=(coefficient_value_type c)
friend LinearConstraint< coefficient_value_type > operator>=(LinearExpression const &expr, coefficient_value_type rhs)
overload >=, same as >
Variable< coefficient_value_type > variable_type
variable type
LinearExpression & operator-=(term_type term)
void swap(LinearExpression &rhs)
swap with an expression
LinearExpression & operator+=(LinearExpression const &expr)
~LinearExpression()
destructor
LinearExpression & operator/=(coefficient_value_type c)
void copy(LinearExpression const &rhs)
copy object
friend LinearExpression operator+(LinearExpression const &expr, term_type const &term)
LinearExpression & operator+=(term_type const &term)
friend LinearExpression operator-(LinearExpression const &expr1, LinearExpression const &expr2)
LinearExpression & operator=(LinearExpression const &rhs)
assignment
LinearExpression operator-() const
friend LinearExpression operator*(coefficient_value_type c, LinearExpression const &expr)
friend LinearExpression operator+(coefficient_value_type constant, LinearExpression const &expr)
LinearExpression(LinearExpression const &rhs)
copy constructor
friend LinearExpression operator-(LinearExpression const &expr, term_type const &term)
T coefficient_value_type
coefficient type
std::vector< term_type > m_vTerm
LinearExpression & negate()
friend LinearConstraint< coefficient_value_type > operator>(LinearExpression const &expr, coefficient_value_type rhs)
overload >
void simplify()
simplify expression by merge terms of the same variables and remove terms with zero coefficients
std::vector< property_type > & variableProperties()
int32_t variable_value_type
bool print(std::string const &filename) const
print problem in lp format to file
std::ostream & print(std::ostream &os, term_type const &term) const
print expression
std::string variableName(variable_type const &var) const
void setVariableUpperBound(variable_type const &var, variable_value_type ub)
set variable upper bound
void scaleConstraint(unsigned int id, coefficient_value_type factor)
scaling a constraint
void add_variable(std::string const &vname, double l=limbo::lowest< double >(), double r=std::numeric_limits< double >::max())
add variable that l <= vname <= r.
void add_constraint(std::string const &cname, LpParser::TermArray const &terms, char compare, double constant)
add constraint that terms compare constant.
void read(std::string const &filename)
read lp format
std::vector< constraint_type > & constraints()
VariableProperty< variable_value_type > property_type
Variable< coefficient_value_type > variable_type
void reserveConstraints(unsigned int n)
reserve space for constraints
std::vector< variable_value_type > const & variableSolutions() const
std::vector< property_type > m_vVariableProperty
bool addConstraint(constraint_type const &constr, std::string name="")
add a constraint
void setOptimizeType(SolverProperty optType)
std::map< std::string, variable_type > m_mName2Variable
void evaluateConstraint() const
evaluate slackness of all constraints given solutions of variables and print to screen
std::vector< variable_value_type > & variableSolutions()
void set_integer(std::string const &vname, bool binary)
set integer variables
void updateVariableLowerBound(variable_type const &var, variable_value_type lb)
update variable lower bound
std::vector< std::string > & constraintNames()
bool emplaceConstraint(expression_type &expr, char sense, coefficient_value_type rhs, std::string name="")
emplace a constraint
float coefficient_value_type
SolverProperty optimizeType() const
SolverProperty variableNumericType(variable_type const &var)
variable_type addVariable(variable_value_type lb, variable_value_type ub, SolverProperty nt, std::string name="")
add one variable
void scaleObjective(coefficient_value_type factor)
scale objective
expression_type const & objective() const
bool printSolution(std::string const &filename) const
print solutions to file
LinearConstraint< coefficient_value_type > constraint_type
coefficient_value_type evaluateConstraint(constraint_type const &constr, std::vector< variable_value_type > const &vVariableSol) const
evaluate slackness of a constraint given solutions of variables
void setVariableNumericType(variable_type const &var, SolverProperty type)
set numeric type of variable
variable_type variable(unsigned int id) const
LinearModel(LinearModel const &rhs)
copy constructor
coefficient_value_type evaluateObjective() const
evaluate objective
void add_objective(bool minimize, LpParser::TermArray const &terms)
add object terms
std::vector< std::string > m_vConstraintName
variable_value_type variableLowerBound(variable_type const &var)
std::string const & constraintName(constraint_type const &constr) const
void reserveVariables(unsigned int n)
reserve space for variables
std::vector< variable_value_type > m_vVariableSol
coefficient_value_type evaluateObjective(std::vector< variable_value_type > const &vVariableSol) const
evaluate objective
variable_value_type variableUpperBound(variable_type const &var)
std::vector< std::string > const & constraintNames() const
std::ostream & print(std::ostream &os, expression_type const &expr) const
print expression
LinearModel & operator=(LinearModel const &rhs)
assignment
void setVariableLowerBound(variable_type const &var, variable_value_type lb)
set variable lower bound
std::vector< constraint_type > const & constraints() const
coefficient_value_type evaluateExpression(expression_type const &expr, std::vector< variable_value_type > const &vVariableSol) const
evaluate expression given solutions of variables
void copy(LinearModel const &rhs)
copy object
expression_type m_objective
std::ostream & print(std::ostream &os, constraint_type const &constr) const
print constraint
LpParser::LpDataBase base_type
std::vector< constraint_type > m_vConstraint
void setVariableName(variable_type const &var, std::string const &name)
set variable name
void emplaceObjective(expression_type &expr)
set objective by swaping with the expression
void setObjective(expression_type const &expr)
set objective
void resizeConstraints(unsigned int n)
resize constraints
coefficient_value_type evaluateConstraint(constraint_type const &constr) const
evaluate slackness of a constraint given solutions of variables
void setVariableSolution(variable_type const &var, variable_value_type v)
void updateVariableUpperBound(variable_type const &var, variable_value_type ub)
update variable upper bound
std::ostream & print(std::ostream &os=std::cout) const
print problem in lp format
std::ostream & printSolution(std::ostream &os=std::cout) const
print solutions
LinearExpression< coefficient_value_type > expression_type
LinearTerm< coefficient_value_type > term_type
std::vector< property_type > const & variableProperties() const
variable_value_type variableSolution(variable_type const &var) const
unsigned int numVariables() const
friend LinearConstraint< coefficient_value_type > operator<(LinearTerm const &term, coefficient_value_type rhs)
overload <
void setVariable(variable_type const &var)
LinearTerm & operator*=(coefficient_value_type coef)
friend LinearConstraint< coefficient_value_type > operator==(LinearTerm const &term, coefficient_value_type rhs)
coefficient_value_type coefficient() const
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term1, LinearTerm const &term2)
Variable< coefficient_value_type > variable_type
variable type
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term1, LinearTerm const &term2)
friend LinearExpression< coefficient_value_type > operator+(variable_type const &var, LinearTerm const &term)
friend LinearTerm operator*(coefficient_value_type coef, LinearTerm const &term)
LinearTerm operator-() const
LinearTerm(variable_type var=variable_type(), coefficient_value_type coef=1)
constructor
void copy(LinearTerm const &rhs)
copy object
LinearTerm(LinearTerm const &rhs)
copy constructor
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term, variable_type const &var)
coefficient_value_type m_coef
friend LinearExpression< coefficient_value_type > operator-(coefficient_value_type constant, LinearTerm const &term)
friend LinearConstraint< coefficient_value_type > operator>=(LinearTerm const &term, coefficient_value_type rhs)
overload >=, same as >
friend LinearExpression< coefficient_value_type > operator-(variable_type const &var, LinearTerm const &term)
friend LinearTerm operator/(LinearTerm const &term, coefficient_value_type coef)
bool operator==(LinearTerm const &rhs) const
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term, coefficient_value_type constant)
friend LinearConstraint< coefficient_value_type > operator<=(LinearTerm const &term, coefficient_value_type rhs)
overload <=, same as <
variable_type const & variable() const
friend LinearExpression< coefficient_value_type > operator+(coefficient_value_type constant, LinearTerm const &term)
LinearTerm & operator=(LinearTerm const &rhs)
assignment
friend LinearConstraint< coefficient_value_type > operator>(LinearTerm const &term, coefficient_value_type rhs)
overload >
void setCoefficient(coefficient_value_type coef)
friend LinearTerm operator*(LinearTerm const &term, coefficient_value_type coef)
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term, variable_type const &var)
T coefficient_value_type
coefficient type
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term, coefficient_value_type constant)
LinearTerm & operator/=(coefficient_value_type coef)
Describe variables in optimization problem.
LinearTerm< coefficient_value_type > operator-() const
friend LinearExpression< coefficient_value_type > operator-(Variable const &var1, Variable const &var2)
friend LinearExpression< coefficient_value_type > operator+(Variable const &var1, Variable const &var2)
friend LinearExpression< coefficient_value_type > operator-(coefficient_value_type constant, Variable const &var)
friend LinearConstraint< coefficient_value_type > operator<(Variable const &var, coefficient_value_type rhs)
overload <
T coefficient_value_type
coefficient type
friend LinearTerm< coefficient_value_type > operator*(Variable const &var, coefficient_value_type coef)
friend LinearExpression< coefficient_value_type > operator+(coefficient_value_type constant, Variable const &var)
friend LinearConstraint< coefficient_value_type > operator==(Variable const &var, coefficient_value_type rhs)
overload ==
Variable & operator=(Variable const &rhs)
assignment
friend LinearTerm< coefficient_value_type > operator/(Variable const &var, coefficient_value_type coef)
bool operator==(Variable const &rhs) const
friend LinearConstraint< coefficient_value_type > operator<=(Variable const &var, coefficient_value_type rhs)
overload <=
friend LinearExpression< coefficient_value_type > operator-(Variable const &var, coefficient_value_type constant)
void copy(Variable const &rhs)
copy object
Variable(Variable const &rhs)
copy constructor
friend LinearTerm< coefficient_value_type > operator*(coefficient_value_type coef, Variable const &var)
Variable(unsigned int id=std::numeric_limits< unsigned int >::max())
constructor
friend LinearExpression< coefficient_value_type > operator+(Variable const &var, coefficient_value_type constant)
friend LinearConstraint< coefficient_value_type > operator>(Variable const &var, coefficient_value_type rhs)
overload >
friend LinearConstraint< coefficient_value_type > operator>=(Variable const &var, coefficient_value_type rhs)
overload >=
void setId(unsigned int id)
set variable index
bool operator!=(Variable const &rhs) const
Describe properties of a variable.
void setLowerBound(variable_value_type lb)
void updateLowerBound(variable_value_type lb)
void updateUpperBound(variable_value_type ub)
VariableProperty(variable_value_type lb, variable_value_type ub, SolverProperty nt, std::string const &n)
constructor
void setName(std::string const &n)
VariableProperty & operator=(VariableProperty const &rhs)
assignment
V variable_value_type
type of bounds
variable_value_type m_upperBound
variable_value_type m_lowerBound
SolverProperty numericType() const
void copy(VariableProperty const &rhs)
copy object
std::string const & name() const
void setNumericType(SolverProperty nt)
void setUpperBound(variable_value_type ub)
VariableProperty(VariableProperty const &rhs)
copy constructor
variable_value_type upperBound() const
variable_value_type lowerBound() const
SolverProperty m_numericType
std::vector< Term > TermArray
array of terms
bool read(LpDataBase &db, const string &lpFile)
API for LpParser. Read LP file and initialize database by calling user-defined callback functions.
namespace for Limbo.Solvers
SolverProperty
Some enums used in solver.
@ OPTIMAL
optimally solved
@ CONTINUOUS
floating point number
@ SUBOPTIMAL
the model is suboptimal
@ INFEASIBLE
the model is infeasible
@ UNBOUNDED
the model is unbounded
std::string toString(SolverProperty sp)
Convert limbo::solvers::SolverProperty to std::string.
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
int limboPrint(MessageType m, const char *format,...)
formatted print with prefix
int limboSPrint(MessageType m, char *buf, const char *format,...)
formatted print with prefix to buffer
T lowest()
generic function to get lowest value of numbers
Comapre term by variable.
bool operator()(TermType const &t1, TermType const &t2) const
MatrixCSR & operator=(MatrixCSR const &rhs)
assignment
static index_type s_startingIndex
void reset()
Destroy matrix and recycle memory.
void initialize(index_type nr, index_type nc, index_type nv)
Initialize matrix.
index_type * vRowBeginIndex
MatrixCSR(MatrixCSR const &rhs)
copy constructor
void copy(MatrixCSR const &rhs)
copy object
value_type at(index_type i, index_type j) const
get element
void set(index_type nr, index_type nc, LinearConstraint< value_type > const *vConstraint)
Set from array of constraints.
coefficient_value_type value_type