# HG changeset patch # User alpar # Date 1112772288 0 # Node ID b3ce42a4d7d2c49cd90184adc65aecb2da4c98ae # Parent 0274efa2222f1beccd6d98384595c2158e5601ac lp_test added WARNING: Overall glpk dependency! (we should avoid) diff -r 0274efa2222f -r b3ce42a4d7d2 src/demo/lp_demo.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/demo/lp_demo.cc Wed Apr 06 07:24:48 2005 +0000 @@ -0,0 +1,61 @@ +#include +#include +#include + +using namespace lemon; + +template +double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t) +{ + LpGlpk lp; + + typedef G Graph; + typedef typename G::Node Node; + typedef typename G::NodeIt NodeIt; + typedef typename G::Edge Edge; + typedef typename G::EdgeIt EdgeIt; + typedef typename G::OutEdgeIt OutEdgeIt; + typedef typename G::InEdgeIt InEdgeIt; + + typename G::template EdgeMap x(g); + lp.addColSet(x); + + for(EdgeIt e(g);e!=INVALID;++e) { + lp.colUpperBound(x[e],cap[e]); + lp.colLowerBound(x[e],0); + } + + for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) { + 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(ex==0); + } + { + LpGlpk::Expr ex; + for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e]; + for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e]; + lp.setObj(ex); + } + + lp.solve(); + + return 0; +} + +int main() +{ + LpGlpk lp_glpk; + + ListGraph g; + ListGraph::Node s=g.addNode(); + ListGraph::Node t=g.addNode(); + + ListGraph::EdgeMap cap(g); + + GraphReader reader(std::cin,g); + reader.addEdgeMap("capacity",cap).run(); + + maxFlow(g,cap,s,t); + +} diff -r 0274efa2222f -r b3ce42a4d7d2 src/test/Makefile.am --- a/src/test/Makefile.am Tue Apr 05 22:37:19 2005 +0000 +++ b/src/test/Makefile.am Wed Apr 06 07:24:48 2005 +0000 @@ -1,4 +1,5 @@ AM_CPPFLAGS = -I$(top_srcdir)/src +LDADD = $(top_builddir)/src/lemon/libemon.la -lglpk EXTRA_DIST = preflow_graph.dim dijkstra_test.lgf noinst_HEADERS = \ @@ -17,6 +18,7 @@ graph_wrapper_test \ graph_utils_test \ kruskal_test \ + lp_test \ max_matching_test \ maps_test \ min_cost_flow_test \ @@ -41,6 +43,7 @@ graph_utils_test_SOURCES = graph_utils_test.cc graph_wrapper_test_SOURCES = graph_wrapper_test.cc kruskal_test_SOURCES = kruskal_test.cc +lp_test_SOURCES = lp_test.cc maps_test_SOURCES = maps_test.cc min_cost_flow_test_SOURCES = min_cost_flow_test.cc max_matching_test_SOURCES = max_matching_test.cc diff -r 0274efa2222f -r b3ce42a4d7d2 src/test/lp_test.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/lp_test.cc Wed Apr 06 07:24:48 2005 +0000 @@ -0,0 +1,139 @@ +#include +#include + +using namespace lemon; + +void lpTest(LpSolverBase & lp) +{ + typedef LpSolverBase LP; + + std::vector x(10); + // for(int i=0;i<10;i++) x.push_back(lp.addCol()); + lp.addColSet(x); + + std::vector y(10); + lp.addColSet(y); + + std::map z; + + z.insert(std::make_pair(12,INVALID)); + z.insert(std::make_pair(2,INVALID)); + z.insert(std::make_pair(7,INVALID)); + z.insert(std::make_pair(5,INVALID)); + + lp.addColSet(z); + + + LP::Expr e,f,g; + LP::Col p1,p2,p3,p4,p5; + LP::Constr c; + + e[p1]=2; + e.constComp()=12; + e[p1]+=2; + e.constComp()+=12; + e[p1]-=2; + e.constComp()-=12; + + e=2; + e=2.2; + e=p1; + e=f; + + e+=2; + e+=2.2; + e+=p1; + e+=f; + + e-=2; + e-=2.2; + e-=p1; + e-=f; + + e*=2; + e*=2.2; + e/=2; + e/=2.2; + + e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ + (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ + (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ + 2.2*f+f*2.2+f/2.2+ + 2*f+f*2+f/2+ + 2.2*p1+p1*2.2+p1/2.2+ + 2*p1+p1*2+p1/2 + ); + + + c = (e <= f ); + c = (e <= 2.2); + c = (e <= 2 ); + c = (e <= p1 ); + c = (2.2<= f ); + c = (2 <= f ); + c = (p1 <= f ); + c = (p1 <= p2 ); + c = (p1 <= 2.2); + c = (p1 <= 2 ); + c = (2.2<= p2 ); + c = (2 <= p2 ); + + c = (e >= f ); + c = (e >= 2.2); + c = (e >= 2 ); + c = (e >= p1 ); + c = (2.2>= f ); + c = (2 >= f ); + c = (p1 >= f ); + c = (p1 >= p2 ); + c = (p1 >= 2.2); + c = (p1 >= 2 ); + c = (2.2>= p2 ); + c = (2 >= p2 ); + + c = (e == f ); + c = (e == 2.2); + c = (e == 2 ); + c = (e == p1 ); + c = (2.2== f ); + c = (2 == f ); + c = (p1 == f ); + //c = (p1 == p2 ); + c = (p1 == 2.2); + c = (p1 == 2 ); + c = (2.2== p2 ); + c = (2 == p2 ); + + c = (2 <= e <= 3); + c = (2 <= p1<= 3); + + c = (2 >= e >= 3); + c = (2 >= p1>= 3); + + e[x[3]]=2; + e[x[3]]=4; + e[x[3]]=1; + e.constComp()=12; + + lp.addRow(LP::INF,e,23); + lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],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]); + + + +} + +int main() +{ + LpSolverSkeleton lp_skel; + LpGlpk lp_glpk; + + lpTest(lp_skel); + lpTest(lp_glpk); + + return 0; +} diff -r 0274efa2222f -r b3ce42a4d7d2 src/work/athos/lp/lp_test.cc --- a/src/work/athos/lp/lp_test.cc Tue Apr 05 22:37:19 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,184 +0,0 @@ -#include"lp_solver_skeleton.h" -#include"lp_glpk.h" -#include - -using namespace lemon; - -void lpTest(LpSolverBase & lp) -{ - typedef LpSolverBase LP; - - std::vector x; - for(int i=0;i<10;i++) x.push_back(lp.addCol()); - - std::vector y(10); - lp.addColSet(y); - - std::map z; - - z.insert(std::make_pair(12,INVALID)); - z.insert(std::make_pair(2,INVALID)); - z.insert(std::make_pair(7,INVALID)); - z.insert(std::make_pair(5,INVALID)); - - lp.addColSet(z); - - - LP::Expr e,f,g; - LP::Col p1,p2,p3,p4,p5; - LP::Constr c; - - e[p1]=2; - e.constComp()=12; - e[p1]+=2; - e.constComp()+=12; - e[p1]-=2; - e.constComp()-=12; - - e=2; - e=2.2; - e=p1; - e=f; - - e+=2; - e+=2.2; - e+=p1; - e+=f; - - e-=2; - e-=2.2; - e-=p1; - e-=f; - - e*=2; - e*=2.2; - e/=2; - e/=2.2; - - e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ - (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ - (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ - 2.2*f+f*2.2+f/2.2+ - 2*f+f*2+f/2+ - 2.2*p1+p1*2.2+p1/2.2+ - 2*p1+p1*2+p1/2 - ); - - - c = (e <= f ); - c = (e <= 2.2); - c = (e <= 2 ); - c = (e <= p1 ); - c = (2.2<= f ); - c = (2 <= f ); - c = (p1 <= f ); - c = (p1 <= p2 ); - c = (p1 <= 2.2); - c = (p1 <= 2 ); - c = (2.2<= p2 ); - c = (2 <= p2 ); - - c = (e >= f ); - c = (e >= 2.2); - c = (e >= 2 ); - c = (e >= p1 ); - c = (2.2>= f ); - c = (2 >= f ); - c = (p1 >= f ); - c = (p1 >= p2 ); - c = (p1 >= 2.2); - c = (p1 >= 2 ); - c = (2.2>= p2 ); - c = (2 >= p2 ); - - c = (e == f ); - c = (e == 2.2); - c = (e == 2 ); - c = (e == p1 ); - c = (2.2== f ); - c = (2 == f ); - c = (p1 == f ); - //c = (p1 == p2 ); - c = (p1 == 2.2); - c = (p1 == 2 ); - c = (2.2== p2 ); - c = (2 == p2 ); - - c = (2 <= e <= 3); - c = (2 <= p1<= 3); - - c = (2 >= e >= 3); - c = (2 >= p1>= 3); - - e[x[3]]=2; - e[x[3]]=4; - e[x[3]]=1; - e.constComp()=12; - - lp.addRow(LP::INF,e,23); - lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],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]); - -} - - -template -double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t) -{ - LpGlpk lp; - - typedef G Graph; - typedef typename G::Node Node; - typedef typename G::NodeIt NodeIt; - typedef typename G::Edge Edge; - typedef typename G::EdgeIt EdgeIt; - typedef typename G::OutEdgeIt OutEdgeIt; - typedef typename G::InEdgeIt InEdgeIt; - - typename G::template EdgeMap x(g); - lp.addColSet(x); - - for(EdgeIt e(g);e!=INVALID;++e) { - lp.colUpperBound(x[e],cap[e]); - lp.colLowerBound(x[e],0); - } - - for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) { - 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(ex==0); - } - { - LpGlpk::Expr ex; - for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e]; - for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e]; - lp.setObj(ex); - } - - lp.solve(); - - return 0; -} - -int main() -{ - LpSolverSkeleton lp_skel; - LpGlpk lp_glpk; - - lpTest(lp_skel); - lpTest(lp_glpk); - - ListGraph g; - ListGraph::Node s=g.addNode(); - ListGraph::Node t=g.addNode(); - - ListGraph::EdgeMap cap(g); - - maxFlow(g,cap,s,t); - -}