demo/circulation_demo.cc
changeset 399 5a7dbeaed70e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/circulation_demo.cc	Fri Nov 21 14:42:47 2008 +0000
     1.3 @@ -0,0 +1,107 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +///\ingroup demos
    1.23 +///\file
    1.24 +///\brief Demonstrating the usage of LEMON's General Flow algorithm
    1.25 +///
    1.26 +/// This demo program reads a general network circulation problem from the
    1.27 +/// file 'circulation-input.lgf', runs the preflow based algorithm for
    1.28 +/// finding a feasible solution and writes the output
    1.29 +/// to 'circulation-input.lgf'
    1.30 +///
    1.31 +/// \include circulation_demo.cc
    1.32 +
    1.33 +#include <iostream>
    1.34 +
    1.35 +#include <lemon/list_graph.h>
    1.36 +#include <lemon/circulation.h>
    1.37 +#include <lemon/lgf_reader.h>
    1.38 +#include <lemon/lgf_writer.h>
    1.39 +
    1.40 +using namespace lemon;
    1.41 +
    1.42 +
    1.43 +int main (int, char*[])
    1.44 +{
    1.45 +
    1.46 +    typedef ListDigraph Digraph;
    1.47 +    typedef Digraph::Node Node;
    1.48 +    typedef Digraph::NodeIt NodeIt;
    1.49 +    typedef Digraph::Arc Arc;
    1.50 +    typedef Digraph::ArcIt ArcIt;
    1.51 +    typedef Digraph::ArcMap<int> ArcMap;
    1.52 +    typedef Digraph::NodeMap<int> NodeMap;
    1.53 +    typedef Digraph::NodeMap<double> DNodeMap;
    1.54 +
    1.55 +    Digraph g;
    1.56 +    ArcMap lo(g);
    1.57 +    ArcMap up(g);
    1.58 +    NodeMap delta(g);
    1.59 +    NodeMap nid(g);
    1.60 +    ArcMap eid(g);
    1.61 +    DNodeMap cx(g);
    1.62 +    DNodeMap cy(g);
    1.63 +
    1.64 +    DigraphReader<Digraph>(g,"circulation-input.lgf").
    1.65 +      arcMap("lo_cap", lo).
    1.66 +      arcMap("up_cap", up).
    1.67 +      nodeMap("delta", delta).
    1.68 +      arcMap("label", eid).
    1.69 +      nodeMap("label", nid).
    1.70 +      nodeMap("coordinates_x", cx).
    1.71 +      nodeMap("coordinates_y", cy).
    1.72 +      run();
    1.73 +
    1.74 +    Circulation<Digraph> gen(g,lo,up,delta);
    1.75 +    bool ret=gen.run();
    1.76 +    if(ret)
    1.77 +      {
    1.78 +        std::cout << "\nA feasible flow has been found.\n";
    1.79 +        if(!gen.checkFlow()) std::cerr << "Oops!!!\n";
    1.80 +        DigraphWriter<Digraph>(g, "circulation-output.lgf").
    1.81 +          arcMap("lo_cap", lo).
    1.82 +          arcMap("up_cap", up).
    1.83 +          arcMap("flow", gen.flowMap()).
    1.84 +          nodeMap("delta", delta).
    1.85 +          arcMap("label", eid).
    1.86 +          nodeMap("label", nid).
    1.87 +          nodeMap("coordinates_x", cx).
    1.88 +          nodeMap("coordinates_y", cy).
    1.89 +          run();
    1.90 +      }
    1.91 +    else {
    1.92 +      std::cout << "\nThere is no such a flow\n";
    1.93 +      Digraph::NodeMap<int> bar(g);
    1.94 +      gen.barrierMap(bar);
    1.95 +      if(!gen.checkBarrier()) std::cerr << "Dual Oops!!!\n";
    1.96 +
    1.97 +      DigraphWriter<Digraph>(g, "circulation-output.lgf").
    1.98 +        arcMap("lo_cap", lo).
    1.99 +        arcMap("up_cap", up).
   1.100 +        nodeMap("barrier", bar).
   1.101 +        nodeMap("delta", delta).
   1.102 +        arcMap("label", eid).
   1.103 +        nodeMap("label", nid).
   1.104 +        nodeMap("coordinates_x", cx).
   1.105 +        nodeMap("coordinates_y", cy).
   1.106 +        run();
   1.107 +    }
   1.108 +  std::cout << "The output is written to file 'circulation-output.lgf'\n\n";
   1.109 +
   1.110 +}