test/circulation_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 01 Dec 2008 14:11:31 +0000
changeset 400 fa341dd6ab23
child 403 940587667b47
permissions -rw-r--r--
Transform circulation demo to test
alpar@400
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@400
     2
 *
alpar@400
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@400
     4
 *
alpar@400
     5
 * Copyright (C) 2003-2008
alpar@400
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@400
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@400
     8
 *
alpar@400
     9
 * Permission to use, modify and distribute this software is granted
alpar@400
    10
 * provided that this copyright notice appears in all copies. For
alpar@400
    11
 * precise terms see the accompanying LICENSE file.
alpar@400
    12
 *
alpar@400
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@400
    14
 * express or implied, and with no claim as to its suitability for any
alpar@400
    15
 * purpose.
alpar@400
    16
 *
alpar@400
    17
 */
alpar@400
    18
alpar@400
    19
///\ingroup demos
alpar@400
    20
///\file
alpar@400
    21
///\brief Demonstrating the usage of LEMON's General Flow algorithm
alpar@400
    22
///
alpar@400
    23
/// This demo program reads a general network circulation problem from the
alpar@400
    24
/// file 'circulation-input.lgf', runs the preflow based algorithm for
alpar@400
    25
/// finding a feasible solution and writes the output
alpar@400
    26
/// to 'circulation-input.lgf'
alpar@400
    27
///
alpar@400
    28
/// \include circulation_demo.cc
alpar@400
    29
alpar@400
    30
#include <iostream>
alpar@400
    31
alpar@400
    32
#include "test_tools.h"
alpar@400
    33
#include <lemon/list_graph.h>
alpar@400
    34
#include <lemon/circulation.h>
alpar@400
    35
#include <lemon/lgf_reader.h>
alpar@400
    36
alpar@400
    37
using namespace lemon;
alpar@400
    38
alpar@400
    39
char test_lgf[] =
alpar@400
    40
  "@nodes\n"
alpar@400
    41
  "label delta\n"
alpar@400
    42
  "0     0\n"
alpar@400
    43
  "1     13\n"
alpar@400
    44
  "2     0\n"
alpar@400
    45
  "3     0\n"
alpar@400
    46
  "4     0\n"
alpar@400
    47
  "5     0\n"
alpar@400
    48
  "6     0\n"
alpar@400
    49
  "7     0\n"
alpar@400
    50
  "8     -13\n"
alpar@400
    51
  "9     0\n"
alpar@400
    52
  "@edges\n"
alpar@400
    53
  "    label lo_cap up_cap\n"
alpar@400
    54
  "0 1 0     0      20\n"
alpar@400
    55
  "0 2 1     0      0\n"
alpar@400
    56
  "1 1 2     0      3\n"
alpar@400
    57
  "1 2 3     0      8\n"
alpar@400
    58
  "1 3 4     0      8\n"
alpar@400
    59
  "2 5 5     0      5\n"
alpar@400
    60
  "3 2 6     0      5\n"
alpar@400
    61
  "3 5 7     0      5\n"
alpar@400
    62
  "3 6 8     0      5\n"
alpar@400
    63
  "4 3 9     0      3\n"
alpar@400
    64
  "5 7 10    0      3\n"
alpar@400
    65
  "5 6 11    0      10\n"
alpar@400
    66
  "5 8 12    0      10\n"
alpar@400
    67
  "6 8 13    0      8\n"
alpar@400
    68
  "8 9 14    0      20\n"
alpar@400
    69
  "8 1 15    0      5\n"
alpar@400
    70
  "9 5 16    0      5\n"
alpar@400
    71
  "@attributes\n"
alpar@400
    72
  "source 1\n"
alpar@400
    73
  "sink   8\n";
alpar@400
    74
alpar@400
    75
int main (int, char*[])
alpar@400
    76
{
alpar@400
    77
alpar@400
    78
    typedef ListDigraph Digraph;
alpar@400
    79
    typedef Digraph::Node Node;
alpar@400
    80
    typedef Digraph::NodeIt NodeIt;
alpar@400
    81
    typedef Digraph::Arc Arc;
alpar@400
    82
    typedef Digraph::ArcIt ArcIt;
alpar@400
    83
    typedef Digraph::ArcMap<int> ArcMap;
alpar@400
    84
    typedef Digraph::NodeMap<int> NodeMap;
alpar@400
    85
    typedef Digraph::NodeMap<double> DNodeMap;
alpar@400
    86
alpar@400
    87
    Digraph g;
alpar@400
    88
    ArcMap lo(g);
alpar@400
    89
    ArcMap up(g);
alpar@400
    90
    NodeMap delta(g);
alpar@400
    91
    NodeMap nid(g);
alpar@400
    92
    ArcMap eid(g);
alpar@400
    93
    Node source, sink;
alpar@400
    94
    
alpar@400
    95
    std::istringstream input(test_lgf);
alpar@400
    96
    DigraphReader<Digraph>(g,input).
alpar@400
    97
      arcMap("lo_cap", lo).
alpar@400
    98
      arcMap("up_cap", up).
alpar@400
    99
      nodeMap("delta", delta).
alpar@400
   100
      arcMap("label", eid).
alpar@400
   101
      nodeMap("label", nid).
alpar@400
   102
      node("source",source).
alpar@400
   103
      node("sink",sink).
alpar@400
   104
      run();
alpar@400
   105
alpar@400
   106
    Circulation<Digraph> gen(g,lo,up,delta);
alpar@400
   107
    bool ret=gen.run();
alpar@400
   108
    check(ret,"A feasible solution should have been found.");
alpar@400
   109
    check(gen.checkFlow(), "The found flow is corrupt.");
alpar@400
   110
    
alpar@400
   111
    delta[source]=14;
alpar@400
   112
    delta[sink]=-14;
alpar@400
   113
    
alpar@400
   114
    bool ret2=gen.run();
alpar@400
   115
    check(!ret2,"A feasible solution should not have been found.");
alpar@400
   116
    check(gen.checkBarrier(), "The found barrier is corrupt.");
alpar@400
   117
alpar@400
   118
}