alpar@1254: #include"lp_solver_skeleton.h" alpar@1263: #include"lp_glpk.h" alpar@1264: #include alpar@1254: alpar@1254: using namespace lemon; alpar@1254: alpar@1263: void lpTest(LpSolverBase & lp) alpar@1254: { alpar@1263: typedef LpSolverBase LP; alpar@1256: alpar@1256: std::vector x; alpar@1256: for(int i=0;i<10;i++) x.push_back(lp.addCol()); alpar@1256: alpar@1256: std::vector y(10); alpar@1256: lp.addColSet(y); alpar@1256: alpar@1256: std::map z; alpar@1256: alpar@1256: z.insert(std::make_pair(12,INVALID)); alpar@1256: z.insert(std::make_pair(2,INVALID)); alpar@1256: z.insert(std::make_pair(7,INVALID)); alpar@1256: z.insert(std::make_pair(5,INVALID)); alpar@1256: alpar@1256: lp.addColSet(z); alpar@1256: alpar@1256: alpar@1272: LP::Expr e,f,g; alpar@1272: LP::Col p1,p2,p3,p4,p5; alpar@1272: LP::Constr c; alpar@1272: alpar@1272: e[p1]=2; alpar@1272: e.constComp()=12; alpar@1272: e[p1]+=2; alpar@1272: e.constComp()+=12; alpar@1272: e[p1]-=2; alpar@1272: e.constComp()-=12; alpar@1272: alpar@1272: e=2; alpar@1272: e=2.2; alpar@1272: e=p1; alpar@1272: e=f; alpar@1272: alpar@1272: e+=2; alpar@1272: e+=2.2; alpar@1272: e+=p1; alpar@1272: e+=f; alpar@1272: alpar@1272: e-=2; alpar@1272: e-=2.2; alpar@1272: e-=p1; alpar@1272: e-=f; alpar@1272: alpar@1272: e*=2; alpar@1272: e*=2.2; alpar@1272: e/=2; alpar@1272: e/=2.2; alpar@1272: alpar@1272: e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ alpar@1272: (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ alpar@1272: (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ alpar@1272: 2.2*f+f*2.2+f/2.2+ alpar@1272: 2*f+f*2+f/2+ alpar@1272: 2.2*p1+p1*2.2+p1/2.2+ alpar@1272: 2*p1+p1*2+p1/2 alpar@1272: ); alpar@1272: alpar@1272: alpar@1272: c = (e <= f ); alpar@1272: c = (e <= 2.2); alpar@1272: c = (e <= 2 ); alpar@1272: c = (e <= p1 ); alpar@1272: c = (2.2<= f ); alpar@1272: c = (2 <= f ); alpar@1272: c = (p1 <= f ); alpar@1272: c = (p1 <= p2 ); alpar@1272: c = (p1 <= 2.2); alpar@1272: c = (p1 <= 2 ); alpar@1272: c = (2.2<= p2 ); alpar@1272: c = (2 <= p2 ); alpar@1272: alpar@1272: c = (e >= f ); alpar@1272: c = (e >= 2.2); alpar@1272: c = (e >= 2 ); alpar@1272: c = (e >= p1 ); alpar@1272: c = (2.2>= f ); alpar@1272: c = (2 >= f ); alpar@1272: c = (p1 >= f ); alpar@1272: c = (p1 >= p2 ); alpar@1272: c = (p1 >= 2.2); alpar@1272: c = (p1 >= 2 ); alpar@1272: c = (2.2>= p2 ); alpar@1272: c = (2 >= p2 ); alpar@1272: alpar@1272: c = (e == f ); alpar@1272: c = (e == 2.2); alpar@1272: c = (e == 2 ); alpar@1272: c = (e == p1 ); alpar@1272: c = (2.2== f ); alpar@1272: c = (2 == f ); alpar@1272: c = (p1 == f ); alpar@1272: //c = (p1 == p2 ); alpar@1272: c = (p1 == 2.2); alpar@1272: c = (p1 == 2 ); alpar@1272: c = (2.2== p2 ); alpar@1272: c = (2 == p2 ); alpar@1272: alpar@1272: c = (2 <= e <= 3); alpar@1272: c = (2 <= p1<= 3); alpar@1272: alpar@1272: c = (2 >= e >= 3); alpar@1272: c = (2 >= p1>= 3); alpar@1272: alpar@1256: e[x[3]]=2; alpar@1256: e[x[3]]=4; alpar@1256: e[x[3]]=1; alpar@1256: e.constComp()=12; alpar@1259: alpar@1256: lp.addRow(LP::INF,e,23); alpar@1259: lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); alpar@1259: lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); alpar@1264: alpar@1264: lp.addRow(x[1]+x[3]<=x[5]-3); alpar@1264: lp.addRow(-7<=x[1]+x[3]-12<=3); alpar@1273: lp.addRow(x[1]<=x[5]); alpar@1272: alpar@1254: } alpar@1263: alpar@1263: alpar@1264: template alpar@1264: double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t) alpar@1264: { alpar@1264: LpGlpk lp; alpar@1264: alpar@1264: typedef G Graph; alpar@1264: typedef typename G::Node Node; alpar@1264: typedef typename G::NodeIt NodeIt; alpar@1264: typedef typename G::Edge Edge; alpar@1264: typedef typename G::EdgeIt EdgeIt; alpar@1264: typedef typename G::OutEdgeIt OutEdgeIt; alpar@1264: typedef typename G::InEdgeIt InEdgeIt; alpar@1264: alpar@1295: typename G::template EdgeMap x(g); alpar@1272: lp.addColSet(x); alpar@1264: alpar@1264: for(EdgeIt e(g);e!=INVALID;++e) { alpar@1293: lp.colUpperBound(x[e],cap[e]); alpar@1293: lp.colLowerBound(x[e],0); alpar@1264: } alpar@1264: alpar@1264: for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) { alpar@1264: LpGlpk::Expr ex; alpar@1264: for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e]; alpar@1264: for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e]; alpar@1273: lp.addRow(ex==0); alpar@1264: } alpar@1264: { alpar@1264: LpGlpk::Expr ex; alpar@1264: for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e]; alpar@1264: for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e]; alpar@1264: lp.setObj(ex); alpar@1264: } alpar@1264: alpar@1264: lp.solve(); alpar@1264: alpar@1264: return 0; alpar@1264: } alpar@1264: alpar@1263: int main() alpar@1263: { alpar@1263: LpSolverSkeleton lp_skel; alpar@1263: LpGlpk lp_glpk; alpar@1263: alpar@1263: lpTest(lp_skel); alpar@1263: lpTest(lp_glpk); alpar@1264: alpar@1264: ListGraph g; alpar@1273: ListGraph::Node s=g.addNode(); alpar@1273: ListGraph::Node t=g.addNode(); alpar@1273: alpar@1264: ListGraph::EdgeMap cap(g); alpar@1264: alpar@1273: maxFlow(g,cap,s,t); alpar@1264: alpar@1263: }