[Lemon-commits] [lemon_svn] alpar: r1703 - hugo/trunk/src/work/athos/lp
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:47:05 CET 2006
Author: alpar
Date: Wed Mar 30 12:38:22 2005
New Revision: 1703
Modified:
hugo/trunk/src/work/athos/lp/lp_base.cc
hugo/trunk/src/work/athos/lp/lp_base.h
hugo/trunk/src/work/athos/lp/lp_solver_skeleton.cc
hugo/trunk/src/work/athos/lp/lp_solver_skeleton.h
hugo/trunk/src/work/athos/lp/lp_test.cc
Log:
- Better (but still incomplete) doc
- lp_test runs correctly
Modified: hugo/trunk/src/work/athos/lp/lp_base.cc
==============================================================================
--- hugo/trunk/src/work/athos/lp/lp_base.cc (original)
+++ hugo/trunk/src/work/athos/lp/lp_base.cc Wed Mar 30 12:38:22 2005
@@ -25,9 +25,9 @@
const LpSolverBase::Value
LpSolverBase::NaN = std::numeric_limits<Value>::quiet_NaN();
- const LpSolverBase::Constr::Coeff
+ const LpSolverBase::Constr::Value
LpSolverBase::Constr::INF = std::numeric_limits<Value>::infinity();
- const LpSolverBase::Constr::Coeff
+ const LpSolverBase::Constr::Value
LpSolverBase::Constr::NaN = std::numeric_limits<Value>::quiet_NaN();
} //namespace lemon
Modified: hugo/trunk/src/work/athos/lp/lp_base.h
==============================================================================
--- hugo/trunk/src/work/athos/lp/lp_base.h (original)
+++ hugo/trunk/src/work/athos/lp/lp_base.h Wed Mar 30 12:38:22 2005
@@ -20,6 +20,7 @@
#include<vector>
#include<map>
#include<limits>
+#include<math.h>
#include<lemon/utility.h>
#include<lemon/error.h>
@@ -72,6 +73,7 @@
}
return cross[n];
}
+ ///\todo Create an own exception type.
else throw LogicError(); //floatingId-s must form a continuous range;
}
///Remove a fix id.
@@ -126,8 +128,7 @@
///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
- ///new column (unless the referred column itself was also deleted,
- ///of course).
+ ///other columns.
///
///\todo Document what can one do with a Col (INVALID, comparing,
///it is similar to Node/Edge)
@@ -150,7 +151,7 @@
///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
- ///new rows (unless the referred row itself was also deleted, of course).
+ ///other rows.
///
///\todo Document what can one do with a Row (INVALID, comparing,
///it is similar to Node/Edge)
@@ -171,34 +172,34 @@
///Linear expression
// typedef SparseLinExpr<Col> Expr;
- class Expr : public std::map<Col,Col::ExprValue>
+ class Expr : public std::map<Col,Value>
{
public:
- typedef Col Var;
- typedef Col::ExprValue Coeff;
+ typedef LpSolverBase::Col Key;
+ typedef LpSolverBase::Value Value;
protected:
- typedef std::map<Col,Col::ExprValue> Base;
+ typedef std::map<Col,Value> Base;
- Coeff const_comp;
+ Value const_comp;
public:
typedef True IsLinExpression;
///\e
Expr() : Base(), const_comp(0) { }
///\e
- Expr(const Var &v) : const_comp(0) {
+ Expr(const Key &v) : const_comp(0) {
Base::insert(std::make_pair(v, 1));
}
///\e
- Expr(const Coeff &v) : const_comp(v) {}
+ Expr(const Value &v) : const_comp(v) {}
///\e
- void set(const Var &v,const Coeff &c) {
+ void set(const Key &v,const Value &c) {
Base::insert(std::make_pair(v, c));
}
///\e
- Coeff &constComp() { return const_comp; }
+ Value &constComp() { return const_comp; }
///\e
- const Coeff &constComp() const { return const_comp; }
+ const Value &constComp() const { return const_comp; }
///Removes the components with zero coefficient.
void simplify() {
@@ -209,7 +210,13 @@
j=i;
}
}
-
+
+ ///Sets all coefficients and the constant component to 0.
+ void clear() {
+ Base::clear();
+ const_comp=0;
+ }
+
///\e
Expr &operator+=(const Expr &e) {
for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
@@ -226,14 +233,14 @@
return *this;
}
///\e
- Expr &operator*=(const Coeff &c) {
+ Expr &operator*=(const Value &c) {
for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
j->second*=c;
const_comp*=c;
return *this;
}
///\e
- Expr &operator/=(const Coeff &c) {
+ Expr &operator/=(const Value &c) {
for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
j->second/=c;
const_comp/=c;
@@ -247,26 +254,50 @@
{
public:
typedef LpSolverBase::Expr Expr;
- typedef Expr::Var Var;
- typedef Expr::Coeff Coeff;
-
- static const Coeff INF;
- static const Coeff NaN;
- // static const Coeff INF=0;
- // static const Coeff NaN=1;
+ typedef Expr::Key Key;
+ typedef Expr::Value Value;
- Expr expr;
- Coeff lb,ub;
+ static const Value INF;
+ static const Value NaN;
+ // static const Value INF=0;
+ // static const Value NaN=1;
- Constr() : expr(), lb(NaN), ub(NaN) {}
- Constr(Coeff _lb,const Expr &e,Coeff _ub) :
- expr(e), lb(_lb), ub(_ub) {}
- Constr(const Expr &e,Coeff _ub) :
- expr(e), lb(NaN), ub(_ub) {}
- Constr(Coeff _lb,const Expr &e) :
- expr(e), lb(_lb), ub(NaN) {}
+ protected:
+ Expr _expr;
+ Value _lb,_ub;
+ public:
+ ///\e
+ Constr() : _expr(), _lb(NaN), _ub(NaN) {}
+ ///\e
+ Constr(Value lb,const Expr &e,Value ub) :
+ _expr(e), _lb(lb), _ub(ub) {}
+ ///\e
+ Constr(const Expr &e,Value ub) :
+ _expr(e), _lb(NaN), _ub(ub) {}
+ ///\e
+ Constr(Value lb,const Expr &e) :
+ _expr(e), _lb(lb), _ub(NaN) {}
+ ///\e
Constr(const Expr &e) :
- expr(e), lb(NaN), ub(NaN) {}
+ _expr(e), _lb(NaN), _ub(NaN) {}
+ ///\e
+ void clear()
+ {
+ _expr.clear();
+ _lb=_ub=NaN;
+ }
+ ///\e
+ Expr &expr() { return _expr; }
+ ///\e
+ const Expr &expr() const { return _expr; }
+ ///\e
+ Value &lowerBound() { return _lb; }
+ ///\e
+ const Value &lowerBound() const { return _lb; }
+ ///\e
+ Value &upperBound() { return _ub; }
+ ///\e
+ const Value &upperBound() const { return _ub; }
};
@@ -354,16 +385,28 @@
///\brief Fill the elements of a container with newly created columns
///(i.e a new variables)
///
- ///This magic function takes container as its argument
+ ///This magic function takes a container as its argument
///and fills its elements
///with new columns (i.e. variables)
- ///\param t can be either any standard STL iterable container with
- ///\ref Col \c values_type or \c mapped_type
- ///like <tt>std::vector<LpSolverBase::Col></tt>,
- /// <tt>std::list<LpSolverBase::Col></tt> or
- /// <tt>std::map<AnyType,LpSolverBase::Col></tt> or
- ///it can be an iterable lemon map like
- /// <tt>ListGraph::NodeMap<LpSolverBase::Col></tt>.
+ ///\param t can be
+ ///- a standard STL compatible iterable container with
+ ///\ref Col as its \c values_type
+ ///like
+ ///\code
+ ///std::vector<LpSolverBase::Col>
+ ///std::list<LpSolverBase::Col>
+ ///\endcode
+ ///- a standard STL compatible iterable container with
+ ///\ref Col as its \c mapped_type
+ ///like
+ ///\code
+ ///std::map<AnyType,LpSolverBase::Col>
+ ///\endcode
+ ///- an iterable lemon \ref concept::WriteMap "write map" like
+ ///\code
+ ///ListGraph::NodeMap<LpSolverBase::Col>
+ ///ListGraph::EdgeMap<LpSolverBase::Col>
+ ///\endcode
///\return The number of the created column.
///\bug Iterable nodemap hasn't been implemented yet.
#ifdef DOXYGEN
@@ -440,9 +483,10 @@
///\param r is the row to be modified
///\param c is a linear expression (see \ref Constr)
void setRow(Row r, const Constr &c) {
- Value lb= c.lb==NaN?-INF:lb;
- Value ub= c.ub==NaN?INF:lb;
- setRow(r,lb,c.expr,ub);
+ setRow(r,
+ isnan(c.lowerBound())?-INF:c.lowerBound(),
+ c.expr(),
+ isnan(c.upperBound())?INF:c.upperBound());
}
///Add a new row (i.e a new constaint) to the LP
@@ -563,7 +607,7 @@
///\relates LpSolverBase::Expr
///
inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
- const LpSolverBase::Expr::Coeff &b)
+ const LpSolverBase::Value &b)
{
LpSolverBase::Expr tmp(a);
tmp*=b; ///\todo Don't STL have some special 'merge' algorithm?
@@ -574,7 +618,7 @@
///\relates LpSolverBase::Expr
///
- inline LpSolverBase::Expr operator*(const LpSolverBase::Expr::Coeff &a,
+ inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
const LpSolverBase::Expr &b)
{
LpSolverBase::Expr tmp(b);
@@ -586,7 +630,7 @@
///\relates LpSolverBase::Expr
///
inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
- const LpSolverBase::Expr::Coeff &b)
+ const LpSolverBase::Value &b)
{
LpSolverBase::Expr tmp(a);
tmp/=b; ///\todo Don't STL have some special 'merge' algorithm?
@@ -607,7 +651,7 @@
///\relates LpSolverBase::Constr
///
- inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr::Coeff &e,
+ inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
const LpSolverBase::Expr &f)
{
return LpSolverBase::Constr(e,f);
@@ -618,7 +662,7 @@
///\relates LpSolverBase::Constr
///
inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
- const LpSolverBase::Expr::Coeff &f)
+ const LpSolverBase::Value &f)
{
return LpSolverBase::Constr(e,f);
}
@@ -638,7 +682,7 @@
///\relates LpSolverBase::Constr
///
- inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr::Coeff &e,
+ inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
const LpSolverBase::Expr &f)
{
return LpSolverBase::Constr(f,e);
@@ -650,7 +694,7 @@
///\relates LpSolverBase::Constr
///
inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
- const LpSolverBase::Expr::Coeff &f)
+ const LpSolverBase::Value &f)
{
return LpSolverBase::Constr(f,e);
}
@@ -669,12 +713,13 @@
///\relates LpSolverBase::Constr
///
- inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr::Coeff &n,
+ inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
const LpSolverBase::Constr&c)
{
LpSolverBase::Constr tmp(c);
- if(tmp.lb!=tmp.NaN) throw LogicError();
- else tmp.lb=n;
+ ///\todo Create an own exception type.
+ if(!isnan(tmp.lowerBound())) throw LogicError();
+ else tmp.lowerBound()=n;
return tmp;
}
///\e
@@ -682,11 +727,12 @@
///\relates LpSolverBase::Constr
///
inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
- const LpSolverBase::Constr::Coeff &n)
+ const LpSolverBase::Value &n)
{
LpSolverBase::Constr tmp(c);
- if(tmp.ub!=tmp.NaN) throw LogicError();
- else tmp.ub=n;
+ ///\todo Create an own exception type.
+ if(!isnan(tmp.upperBound())) throw LogicError();
+ else tmp.upperBound()=n;
return tmp;
}
@@ -694,12 +740,13 @@
///\relates LpSolverBase::Constr
///
- inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr::Coeff &n,
+ inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
const LpSolverBase::Constr&c)
{
LpSolverBase::Constr tmp(c);
- if(tmp.ub!=tmp.NaN) throw LogicError();
- else tmp.ub=n;
+ ///\todo Create an own exception type.
+ if(!isnan(tmp.upperBound())) throw LogicError();
+ else tmp.upperBound()=n;
return tmp;
}
///\e
@@ -707,11 +754,12 @@
///\relates LpSolverBase::Constr
///
inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
- const LpSolverBase::Constr::Coeff &n)
+ const LpSolverBase::Value &n)
{
LpSolverBase::Constr tmp(c);
- if(tmp.lb!=tmp.NaN) throw LogicError();
- else tmp.lb=n;
+ ///\todo Create an own exception type.
+ if(!isnan(tmp.lowerBound())) throw LogicError();
+ else tmp.lowerBound()=n;
return tmp;
}
Modified: hugo/trunk/src/work/athos/lp/lp_solver_skeleton.cc
==============================================================================
--- hugo/trunk/src/work/athos/lp/lp_solver_skeleton.cc (original)
+++ hugo/trunk/src/work/athos/lp/lp_solver_skeleton.cc Wed Mar 30 12:38:22 2005
@@ -23,12 +23,12 @@
int LpSolverSkeleton::_addCol()
{
- return 1;
+ return ++col_num;
}
int LpSolverSkeleton::_addRow()
{
- return 1;
+ return ++row_num;
}
void LpSolverSkeleton::_setRowCoeffs(int i,
Modified: hugo/trunk/src/work/athos/lp/lp_solver_skeleton.h
==============================================================================
--- hugo/trunk/src/work/athos/lp/lp_solver_skeleton.h (original)
+++ hugo/trunk/src/work/athos/lp/lp_solver_skeleton.h Wed Mar 30 12:38:22 2005
@@ -26,7 +26,8 @@
///A skeleton class to implement LP solver interfaces
class LpSolverSkeleton :public LpSolverBase {
-
+ int col_num,row_num;
+
protected:
virtual int _addCol();
virtual int _addRow();
@@ -45,7 +46,8 @@
virtual void _setObjCoeff(int i, Value obj_coef);
virtual SolutionType _solve();
virtual Value _getSolution(int i);
-
+ public:
+ LpSolverSkeleton() : LpSolverBase(), col_num(0), row_num(0) {}
};
} //namespace lemon
Modified: hugo/trunk/src/work/athos/lp/lp_test.cc
==============================================================================
--- hugo/trunk/src/work/athos/lp/lp_test.cc (original)
+++ hugo/trunk/src/work/athos/lp/lp_test.cc Wed Mar 30 12:38:22 2005
@@ -116,14 +116,12 @@
e.constComp()=12;
lp.addRow(LP::INF,e,23);
- lp.addRow(LP::INF,3.0*(p1+p2)-p3,23);
lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
- lp.addRow(LP::INF,3.0*(p1+p2*2-5*p3+12-p4/3)+2*p4-4,23);
lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
lp.addRow(x[1]+x[3]<=x[5]-3);
lp.addRow(-7<=x[1]+x[3]-12<=3);
- //lp.addRow(x[1]<=x[5]);
+ lp.addRow(x[1]<=x[5]);
}
@@ -143,7 +141,6 @@
typename G::EdgeMap<LpGlpk::Col> x(g);
lp.addColSet(x);
- //for(EdgeIt e(g);e!=INVALID;++e) x[e]=lp.addCol();
for(EdgeIt e(g);e!=INVALID;++e) {
lp.setColUpperBound(x[e],cap[e]);
@@ -154,7 +151,7 @@
LpGlpk::Expr ex;
for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e];
for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
- lp.addRow(0,ex,0);
+ lp.addRow(ex==0);
}
{
LpGlpk::Expr ex;
@@ -177,8 +174,11 @@
lpTest(lp_glpk);
ListGraph g;
+ ListGraph::Node s=g.addNode();
+ ListGraph::Node t=g.addNode();
+
ListGraph::EdgeMap<double> cap(g);
- maxFlow(g,cap,ListGraph::NodeIt(g),ListGraph::NodeIt(g));
+ maxFlow(g,cap,s,t);
}
More information about the Lemon-commits
mailing list