COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/lp/max_flow_expression.cc @ 1142:450f794dca81

Last change on this file since 1142:450f794dca81 was 1111:88ade201ffc6, checked in by marci, 19 years ago

lower and upper bound handling functions for rows

File size: 2.2 KB
Line 
1// -*- c++ -*-
2#include <iostream>
3#include <fstream>
4
5#include <lemon/smart_graph.h>
6#include <lemon/list_graph.h>
7#include <lemon/dimacs.h>
8#include <lemon/time_measure.h>
9#include <lp_solver_base.h>
10
11using std::cout;
12using std::endl;
13using namespace lemon;
14
15template<typename Edge, typename EdgeIndexMap>
16class PrimalMap {
17protected:
18  LPGLPK* lp;
19  EdgeIndexMap* edge_index_map;
20public:
21  PrimalMap(LPGLPK& _lp, EdgeIndexMap& _edge_index_map) :
22    lp(&_lp), edge_index_map(&_edge_index_map) { }
23  double operator[](Edge e) const {
24    return lp->getPrimal((*edge_index_map)[e]);
25  }
26};
27
28// Use a DIMACS max flow file as stdin.
29// max_flow_expresion < dimacs_max_flow_file
30
31int main(int, char **) {
32
33  typedef ListGraph Graph;
34  typedef Graph::Node Node;
35  typedef Graph::Edge Edge;
36  typedef Graph::EdgeIt EdgeIt;
37
38  Graph g;
39 
40  Node s, t;
41  Graph::EdgeMap<int> cap(g);
42  readDimacs(std::cin, g, cap, s, t);
43  Timer ts;
44 
45  typedef LPGLPK LPSolver;
46  LPSolver lp;
47  lp.setMaximize();
48  typedef LPSolver::ColIt ColIt;
49  typedef LPSolver::RowIt RowIt;
50  typedef Graph::EdgeMap<ColIt> EdgeIndexMap;
51  EdgeIndexMap edge_index_map(g);
52  PrimalMap<Edge, EdgeIndexMap> flow(lp, edge_index_map);
53
54  // nonnegativity of flow and capacity function
55  for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
56    ColIt col_it=lp.addCol();
57    edge_index_map.set(e, col_it);
58    // interesting property in GLPK:
59    // if you change the order of the following two lines, the
60    // two runs of GLPK are extremely different
61      lp.setColLowerBound(col_it, 0);
62      lp.setColUpperBound(col_it, cap[e]);
63  }
64 
65  for (Graph::NodeIt n(g); n!=INVALID; ++n) {
66    LPSolver::Expression expr;
67    for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e)
68      expr+=edge_index_map[e];
69    for (Graph::InEdgeIt e(g, n); e!=INVALID; ++e)
70      expr-=edge_index_map[e];
71    // cost function
72    if (n==s) {
73      lp.setObjCoeffs(expr);     
74    }
75    // flow conservation constraints
76    if ((n!=s) && (n!=t)) {
77      RowIt row_it=lp.addRow();
78      lp.setRowCoeffs(row_it, expr);
79      lp.setRowLowerBound(row_it, 0.0);
80      lp.setRowUpperBound(row_it, 0.0);
81    }
82  }
83  lp.solveSimplex();
84  cout << "elapsed time: " << ts << endl;
85}
Note: See TracBrowser for help on using the repository browser.