1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/demo/lp_maxflow_demo.cc Mon May 23 04:48:14 2005 +0000
1.3 @@ -0,0 +1,86 @@
1.4 +#ifdef HAVE_CONFIG_H
1.5 +#include <config.h>
1.6 +#endif
1.7 +
1.8 +#include<lemon/graph_reader.h>
1.9 +#include<lemon/list_graph.h>
1.10 +
1.11 +
1.12 +#ifdef HAVE_GLPK
1.13 +#include <lemon/lp_glpk.h>
1.14 +#elif HAVE_CPLEX
1.15 +#include <lemon/lp_cplex.h>
1.16 +#endif
1.17 +
1.18 +using namespace lemon;
1.19 +
1.20 +#ifdef HAVE_GLPK
1.21 +typedef LpGlpk LpDefault;
1.22 +#elif HAVE_CPLEX
1.23 +typedef LpCplex LpDefault;
1.24 +#endif
1.25 +
1.26 +
1.27 +template<class G,class C>
1.28 +double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
1.29 +{
1.30 + LpDefault lp;
1.31 +
1.32 + typedef G Graph;
1.33 + typedef typename G::Node Node;
1.34 + typedef typename G::NodeIt NodeIt;
1.35 + typedef typename G::Edge Edge;
1.36 + typedef typename G::EdgeIt EdgeIt;
1.37 + typedef typename G::OutEdgeIt OutEdgeIt;
1.38 + typedef typename G::InEdgeIt InEdgeIt;
1.39 +
1.40 + typename G::template EdgeMap<LpDefault::Col> x(g);
1.41 + lp.addColSet(x);
1.42 +
1.43 + for(EdgeIt e(g);e!=INVALID;++e) {
1.44 + lp.colUpperBound(x[e],cap[e]);
1.45 + lp.colLowerBound(x[e],0);
1.46 + }
1.47 +
1.48 + for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
1.49 + LpDefault::Expr ex;
1.50 + for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e];
1.51 + for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
1.52 + lp.addRow(ex==0);
1.53 + }
1.54 + {
1.55 + LpDefault::Expr ex;
1.56 + for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e];
1.57 + for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
1.58 + lp.setObj(ex);
1.59 + }
1.60 + lp.max();
1.61 +
1.62 +#ifdef HAVE_GLPK
1.63 + lp.presolver(true);
1.64 + lp.messageLevel(3);
1.65 +#endif
1.66 +
1.67 + lp.solve();
1.68 +
1.69 + return lp.primalValue();
1.70 +}
1.71 +
1.72 +int main()
1.73 +{
1.74 + ListGraph g;
1.75 + ListGraph::Node s;
1.76 + ListGraph::Node t;
1.77 +
1.78 + ListGraph::EdgeMap<double> cap(g);
1.79 +
1.80 + GraphReader<ListGraph> reader(std::cin,g);
1.81 + reader.readNode("source",s).readNode("target",t)
1.82 + .readEdgeMap("capacity",cap).run();
1.83 +
1.84 + // std::ifstream file("../test/preflow_");
1.85 +// readDimacs(file, g, cap, s, t);
1.86 +
1.87 + std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
1.88 +
1.89 +}