demo/circulation_demo.cc
author deba
Sun, 25 Nov 2007 22:56:44 +0000
changeset 2521 05c0ba99cc27
parent 2375 e30a0fdad0d7
child 2526 b7727edd44f2
permissions -rw-r--r--
Bugfix: using read-write map instead reference map
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@2391
     5
 * Copyright (C) 2003-2007
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
    EdgeMap x(g);
alpar@2375
    56
    NodeMap delta(g);
alpar@2375
    57
    NodeMap nid(g);
alpar@2375
    58
    EdgeMap eid(g);
alpar@2375
    59
    DNodeMap cx(g);
alpar@2375
    60
    DNodeMap cy(g);
alpar@2375
    61
    
alpar@2375
    62
    GraphReader<Graph>("circulation-input.lgf", g).
alpar@2375
    63
      readEdgeMap("lo_cap", lo).
alpar@2375
    64
      readEdgeMap("up_cap", up).
alpar@2375
    65
      readNodeMap("delta", delta).
alpar@2375
    66
      readEdgeMap("label", eid).
alpar@2375
    67
      readNodeMap("label", nid).
alpar@2375
    68
      readNodeMap("coordinates_x", cx).
alpar@2375
    69
      readNodeMap("coordinates_y", cy).
alpar@2375
    70
      run();
alpar@2375
    71
alpar@2375
    72
    Circulation<Graph,int> gen(g,lo,up,delta,x);
alpar@2375
    73
    int ret=gen.run();
alpar@2375
    74
    if(ret==-1)
alpar@2375
    75
      {
alpar@2375
    76
	std::cout << "\nA feasible flow has been found.\n";
alpar@2375
    77
	if(!gen.checkX(x)) std::cerr << "Oops!!!\n";
alpar@2375
    78
	GraphWriter<Graph>("circulation-output.lgf", g).
alpar@2375
    79
	  writeEdgeMap("lo_cap", lo).
alpar@2375
    80
	  writeEdgeMap("up_cap", up).
alpar@2375
    81
	  writeEdgeMap("flow", x).
alpar@2375
    82
	  writeNodeMap("delta", delta).
alpar@2375
    83
	  writeEdgeMap("label", eid).
alpar@2375
    84
	  writeNodeMap("label", nid).
alpar@2375
    85
	  writeNodeMap("coordinates_x", cx).
alpar@2375
    86
	  writeNodeMap("coordinates_y", cy).
alpar@2375
    87
	  run();
alpar@2375
    88
      }
alpar@2375
    89
    else {
alpar@2375
    90
      std::cout << "\nThere is no such a flow\n";
alpar@2375
    91
      Graph::NodeMap<int> bar(g);
alpar@2375
    92
      gen.barrier(bar,ret);
alpar@2375
    93
      if(!gen.checkBarrier(bar)) std::cerr << "Dual Oops!!!\n";
alpar@2375
    94
alpar@2375
    95
      GraphWriter<Graph>("circulation-output.lgf", g).
alpar@2375
    96
	writeEdgeMap("lo_cap", lo).
alpar@2375
    97
	writeEdgeMap("up_cap", up).
alpar@2375
    98
	writeNodeMap("barrier", bar).
alpar@2375
    99
	writeNodeMap("delta", delta).
alpar@2375
   100
	writeEdgeMap("label", eid).
alpar@2375
   101
	writeNodeMap("label", nid).
alpar@2375
   102
	writeNodeMap("coordinates_x", cx).
alpar@2375
   103
	writeNodeMap("coordinates_y", cy).
alpar@2375
   104
	run();
alpar@2375
   105
    }
alpar@2375
   106
  std::cout << "The output is written to file 'circulation-output.lgf'\n\n";
alpar@2375
   107
    
alpar@2375
   108
}