COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/lp_maxflow_demo.cc @ 1721:c0f5e8401373

Last change on this file since 1721:c0f5e8401373 was 1641:77f6ab7ad66f, checked in by Alpar Juttner, 15 years ago

Demos' documentations include the source.

File size: 3.1 KB
RevLine 
[1560]1/* -*- C++ -*-
2 * demo/lp_maxflow_demo.cc - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
17///\ingroup demos
18///\file
19///\brief Max flow problem solved with an LP solver (demo).
20///
[1583]21/// This demo program shows how to solve a maximum (or maximal) flow
22/// problem using the LEMON LP solver interface. We would like to lay
23/// the emphasis on the simplicity of the way one can formulate LP
24/// constraints that arise in graph theory in our library LEMON .
[1641]25///
26/// \include lp_maxflow_demo.cc
[1560]27
[1361]28#include<lemon/graph_reader.h>
29#include<lemon/list_graph.h>
[1610]30#include <lemon/lp.h>
[1361]31
[1560]32#include <fstream>
33#include <iostream>
34
[1381]35
36
[1361]37using namespace lemon;
38
39template<class G,class C>
40double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
41{
[1610]42  Lp lp;
[1361]43 
44  typedef G Graph;
45  typedef typename G::Node Node;
46  typedef typename G::NodeIt NodeIt;
47  typedef typename G::Edge Edge;
48  typedef typename G::EdgeIt EdgeIt;
49  typedef typename G::OutEdgeIt OutEdgeIt;
50  typedef typename G::InEdgeIt InEdgeIt;
51 
[1518]52  //Define a map on the edges for the variables of the LP problem
[1610]53  typename G::template EdgeMap<Lp::Col> x(g);
[1361]54  lp.addColSet(x);
55 
[1518]56  //Nonnegativity and capacity constraints
[1361]57  for(EdgeIt e(g);e!=INVALID;++e) {
58    lp.colUpperBound(x[e],cap[e]);
59    lp.colLowerBound(x[e],0);
60  }
61
[1518]62
63  //Flow conservation constraints for the nodes (except for 's' and 't')
[1361]64  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
[1610]65    Lp::Expr ex;
[1361]66    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
67    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
68    lp.addRow(ex==0);
69  }
[1518]70 
71  //Objective function: the flow value entering 't'
[1610]72  Lp::Expr obj;
[1571]73  for(InEdgeIt  e(g,t);e!=INVALID;++e) obj+=x[e];
74  for(OutEdgeIt e(g,t);e!=INVALID;++e) obj-=x[e];
75  lp.setObj(obj);
76
[1518]77
78  //Maximization
[1361]79  lp.max();
80
[1610]81#if DEFAULT_LP==GLPK
[1361]82  lp.presolver(true);
83  lp.messageLevel(3);
[1381]84#endif
[1361]85
[1577]86  std::cout<<"Solver used: "<<default_solver_name<<std::endl;
87
[1518]88  //Solve with the underlying solver
[1361]89  lp.solve();
90
91  return lp.primalValue();
92}
93
[1560]94int main(int argc, char *argv[])
[1361]95{
[1560]96  if(argc<2)
97  {
[1577]98      std::cerr << "  USAGE: lp_maxflow_demo input_file.lgf" << std::endl;
[1561]99      std::cerr << "  The file 'input_file.lgf' has to contain a max "
100                << "flow instance in\n"
101                << "  LEMON format (e.g. sample.lgf is such a file)."
102                << std::endl;
[1560]103      return 0;
104  }
105
106
107  //input stream to read the graph from
108  std::ifstream is(argv[1]);
109
110
[1361]111  ListGraph g;
112  ListGraph::Node s;
113  ListGraph::Node t;
114 
115  ListGraph::EdgeMap<double> cap(g);
116 
[1560]117  GraphReader<ListGraph> reader(is,g);
[1394]118  reader.readNode("source",s).readNode("target",t)
119    .readEdgeMap("capacity",cap).run();
[1361]120 
121  std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
122
123}
Note: See TracBrowser for help on using the repository browser.