demo/circulation_demo.cc
author kpeter
Thu, 13 Nov 2008 15:29:04 +0000
changeset 2629 84354c78b068
parent 2526 b7727edd44f2
permissions -rw-r--r--
Improved constructors for min cost flow classes
Removing the non-zero lower bounds is faster
alpar@2375
     1
/* -*- C++ -*-
alpar@2375
     2
 *
alpar@2375
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2375
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@2375
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2375
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2375
     8
 *
alpar@2375
     9
 * Permission to use, modify and distribute this software is granted
alpar@2375
    10
 * provided that this copyright notice appears in all copies. For
alpar@2375
    11
 * precise terms see the accompanying LICENSE file.
alpar@2375
    12
 *
alpar@2375
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2375
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2375
    15
 * purpose.
alpar@2375
    16
 *
alpar@2375
    17
 */
alpar@2375
    18
alpar@2375
    19
///\ingroup demos
alpar@2375
    20
///\file
alpar@2375
    21
///\brief Demonstrating the usage of LEMON's General Flow algorithm
alpar@2375
    22
///
alpar@2375
    23
/// This demo program reads a general network circulation problem from the
alpar@2375
    24
/// file 'circulation-input.lgf', runs the preflow based algorithm for
alpar@2375
    25
/// finding a feasible solution and writes the output
alpar@2375
    26
/// to 'circulation-input.lgf'
alpar@2375
    27
///
alpar@2375
    28
/// \include circulation_demo.cc
alpar@2375
    29
alpar@2375
    30
#include <iostream>
alpar@2375
    31
alpar@2375
    32
#include <lemon/list_graph.h>
alpar@2375
    33
#include <lemon/circulation.h>
alpar@2375
    34
#include <lemon/graph_reader.h>
alpar@2375
    35
#include <lemon/graph_writer.h>
alpar@2375
    36
alpar@2375
    37
using namespace lemon;
alpar@2375
    38
alpar@2375
    39
alpar@2375
    40
int main (int, char*[])
alpar@2375
    41
{
alpar@2375
    42
alpar@2375
    43
    typedef ListGraph Graph;
alpar@2375
    44
    typedef Graph::Node Node;
alpar@2375
    45
    typedef Graph::NodeIt NodeIt;
alpar@2375
    46
    typedef Graph::Edge Edge;
alpar@2375
    47
    typedef Graph::EdgeIt EdgeIt;
alpar@2375
    48
    typedef Graph::EdgeMap<int> EdgeMap;
alpar@2375
    49
    typedef Graph::NodeMap<int> NodeMap;
alpar@2375
    50
    typedef Graph::NodeMap<double> DNodeMap;
alpar@2375
    51
alpar@2375
    52
    Graph g;
alpar@2375
    53
    EdgeMap lo(g);
alpar@2375
    54
    EdgeMap up(g);
alpar@2375
    55
    NodeMap delta(g);
alpar@2375
    56
    NodeMap nid(g);
alpar@2375
    57
    EdgeMap eid(g);
alpar@2375
    58
    DNodeMap cx(g);
alpar@2375
    59
    DNodeMap cy(g);
alpar@2375
    60
    
alpar@2375
    61
    GraphReader<Graph>("circulation-input.lgf", g).
alpar@2375
    62
      readEdgeMap("lo_cap", lo).
alpar@2375
    63
      readEdgeMap("up_cap", up).
alpar@2375
    64
      readNodeMap("delta", delta).
alpar@2375
    65
      readEdgeMap("label", eid).
alpar@2375
    66
      readNodeMap("label", nid).
alpar@2375
    67
      readNodeMap("coordinates_x", cx).
alpar@2375
    68
      readNodeMap("coordinates_y", cy).
alpar@2375
    69
      run();
alpar@2375
    70
deba@2526
    71
    Circulation<Graph> gen(g,lo,up,delta);
deba@2526
    72
    bool ret=gen.run();
deba@2526
    73
    if(ret)
alpar@2375
    74
      {
alpar@2375
    75
	std::cout << "\nA feasible flow has been found.\n";
deba@2526
    76
	if(!gen.checkFlow()) std::cerr << "Oops!!!\n";
alpar@2375
    77
	GraphWriter<Graph>("circulation-output.lgf", g).
alpar@2375
    78
	  writeEdgeMap("lo_cap", lo).
alpar@2375
    79
	  writeEdgeMap("up_cap", up).
deba@2526
    80
	  writeEdgeMap("flow", gen.flowMap()).
alpar@2375
    81
	  writeNodeMap("delta", delta).
alpar@2375
    82
	  writeEdgeMap("label", eid).
alpar@2375
    83
	  writeNodeMap("label", nid).
alpar@2375
    84
	  writeNodeMap("coordinates_x", cx).
alpar@2375
    85
	  writeNodeMap("coordinates_y", cy).
alpar@2375
    86
	  run();
alpar@2375
    87
      }
alpar@2375
    88
    else {
alpar@2375
    89
      std::cout << "\nThere is no such a flow\n";
alpar@2375
    90
      Graph::NodeMap<int> bar(g);
deba@2526
    91
      gen.barrierMap(bar);
deba@2526
    92
      if(!gen.checkBarrier()) std::cerr << "Dual Oops!!!\n";
alpar@2375
    93
alpar@2375
    94
      GraphWriter<Graph>("circulation-output.lgf", g).
alpar@2375
    95
	writeEdgeMap("lo_cap", lo).
alpar@2375
    96
	writeEdgeMap("up_cap", up).
alpar@2375
    97
	writeNodeMap("barrier", bar).
alpar@2375
    98
	writeNodeMap("delta", delta).
alpar@2375
    99
	writeEdgeMap("label", eid).
alpar@2375
   100
	writeNodeMap("label", nid).
alpar@2375
   101
	writeNodeMap("coordinates_x", cx).
alpar@2375
   102
	writeNodeMap("coordinates_y", cy).
alpar@2375
   103
	run();
alpar@2375
   104
    }
alpar@2375
   105
  std::cout << "The output is written to file 'circulation-output.lgf'\n\n";
alpar@2375
   106
    
alpar@2375
   107
}