test/hao_orlin_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 28 Feb 2013 23:45:39 +0100
changeset 1229 473c71baff72
parent 956 141f9c0db4a3
parent 1171 7e368d9b67f7
child 1259 8b2d4e5d96e4
permissions -rw-r--r--
Avoid usage of alternative operator (#177)
deba@426
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@426
     2
 *
deba@426
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@426
     4
 *
alpar@956
     5
 * Copyright (C) 2003-2010
deba@426
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@426
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@426
     8
 *
deba@426
     9
 * Permission to use, modify and distribute this software is granted
deba@426
    10
 * provided that this copyright notice appears in all copies. For
deba@426
    11
 * precise terms see the accompanying LICENSE file.
deba@426
    12
 *
deba@426
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@426
    14
 * express or implied, and with no claim as to its suitability for any
deba@426
    15
 * purpose.
deba@426
    16
 *
deba@426
    17
 */
deba@426
    18
deba@426
    19
#include <sstream>
deba@426
    20
deba@426
    21
#include <lemon/smart_graph.h>
kpeter@643
    22
#include <lemon/adaptors.h>
kpeter@643
    23
#include <lemon/concepts/digraph.h>
kpeter@643
    24
#include <lemon/concepts/maps.h>
kpeter@643
    25
#include <lemon/lgf_reader.h>
deba@426
    26
#include <lemon/hao_orlin.h>
deba@426
    27
deba@426
    28
#include "test_tools.h"
deba@426
    29
deba@426
    30
using namespace lemon;
deba@426
    31
using namespace std;
deba@426
    32
deba@426
    33
const std::string lgf =
deba@426
    34
  "@nodes\n"
deba@426
    35
  "label\n"
deba@426
    36
  "0\n"
deba@426
    37
  "1\n"
deba@426
    38
  "2\n"
deba@426
    39
  "3\n"
deba@426
    40
  "4\n"
deba@426
    41
  "5\n"
deba@426
    42
  "@edges\n"
kpeter@643
    43
  "     cap1 cap2 cap3\n"
kpeter@643
    44
  "0 1  1    1    1   \n"
kpeter@643
    45
  "0 2  2    2    4   \n"
kpeter@643
    46
  "1 2  4    4    4   \n"
kpeter@643
    47
  "3 4  1    1    1   \n"
kpeter@643
    48
  "3 5  2    2    4   \n"
kpeter@643
    49
  "4 5  4    4    4   \n"
kpeter@643
    50
  "5 4  4    4    4   \n"
kpeter@643
    51
  "2 3  1    6    6   \n"
kpeter@643
    52
  "4 0  1    6    6   \n";
kpeter@643
    53
kpeter@643
    54
void checkHaoOrlinCompile()
kpeter@643
    55
{
kpeter@643
    56
  typedef int Value;
kpeter@643
    57
  typedef concepts::Digraph Digraph;
kpeter@643
    58
kpeter@643
    59
  typedef Digraph::Node Node;
kpeter@643
    60
  typedef Digraph::Arc Arc;
kpeter@643
    61
  typedef concepts::ReadMap<Arc, Value> CapMap;
kpeter@643
    62
  typedef concepts::WriteMap<Node, bool> CutMap;
kpeter@643
    63
kpeter@643
    64
  Digraph g;
kpeter@643
    65
  Node n;
kpeter@643
    66
  CapMap cap;
kpeter@643
    67
  CutMap cut;
kpeter@643
    68
  Value v;
alpar@1171
    69
  ignore_unused_variable_warning(v);
kpeter@643
    70
kpeter@643
    71
  HaoOrlin<Digraph, CapMap> ho_test(g, cap);
kpeter@643
    72
  const HaoOrlin<Digraph, CapMap>&
kpeter@643
    73
    const_ho_test = ho_test;
kpeter@643
    74
kpeter@643
    75
  ho_test.init();
kpeter@643
    76
  ho_test.init(n);
kpeter@643
    77
  ho_test.calculateOut();
kpeter@643
    78
  ho_test.calculateIn();
kpeter@643
    79
  ho_test.run();
kpeter@643
    80
  ho_test.run(n);
kpeter@643
    81
kpeter@643
    82
  v = const_ho_test.minCutValue();
kpeter@643
    83
  v = const_ho_test.minCutMap(cut);
kpeter@643
    84
}
kpeter@643
    85
kpeter@643
    86
template <typename Graph, typename CapMap, typename CutMap>
alpar@956
    87
typename CapMap::Value
kpeter@643
    88
  cutValue(const Graph& graph, const CapMap& cap, const CutMap& cut)
kpeter@643
    89
{
kpeter@643
    90
  typename CapMap::Value sum = 0;
kpeter@643
    91
  for (typename Graph::ArcIt a(graph); a != INVALID; ++a) {
kpeter@643
    92
    if (cut[graph.source(a)] && !cut[graph.target(a)])
kpeter@643
    93
      sum += cap[a];
kpeter@643
    94
  }
kpeter@643
    95
  return sum;
kpeter@643
    96
}
deba@426
    97
deba@426
    98
int main() {
kpeter@643
    99
  SmartDigraph graph;
kpeter@643
   100
  SmartDigraph::ArcMap<int> cap1(graph), cap2(graph), cap3(graph);
kpeter@643
   101
  SmartDigraph::NodeMap<bool> cut(graph);
deba@426
   102
kpeter@643
   103
  istringstream input(lgf);
kpeter@643
   104
  digraphReader(graph, input)
kpeter@643
   105
    .arcMap("cap1", cap1)
kpeter@643
   106
    .arcMap("cap2", cap2)
kpeter@643
   107
    .arcMap("cap3", cap3)
kpeter@643
   108
    .run();
deba@426
   109
kpeter@643
   110
  {
kpeter@643
   111
    HaoOrlin<SmartDigraph> ho(graph, cap1);
kpeter@643
   112
    ho.run();
kpeter@643
   113
    ho.minCutMap(cut);
alpar@956
   114
deba@644
   115
    check(ho.minCutValue() == 1, "Wrong cut value");
deba@644
   116
    check(ho.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value");
kpeter@643
   117
  }
kpeter@643
   118
  {
kpeter@643
   119
    HaoOrlin<SmartDigraph> ho(graph, cap2);
kpeter@643
   120
    ho.run();
kpeter@643
   121
    ho.minCutMap(cut);
deba@644
   122
deba@644
   123
    check(ho.minCutValue() == 1, "Wrong cut value");
deba@644
   124
    check(ho.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value");
kpeter@643
   125
  }
kpeter@643
   126
  {
kpeter@643
   127
    HaoOrlin<SmartDigraph> ho(graph, cap3);
kpeter@643
   128
    ho.run();
kpeter@643
   129
    ho.minCutMap(cut);
alpar@956
   130
deba@644
   131
    check(ho.minCutValue() == 1, "Wrong cut value");
deba@644
   132
    check(ho.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value");
kpeter@643
   133
  }
alpar@956
   134
kpeter@643
   135
  typedef Undirector<SmartDigraph> UGraph;
kpeter@643
   136
  UGraph ugraph(graph);
alpar@956
   137
kpeter@643
   138
  {
kpeter@643
   139
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap1);
kpeter@643
   140
    ho.run();
kpeter@643
   141
    ho.minCutMap(cut);
alpar@956
   142
deba@644
   143
    check(ho.minCutValue() == 2, "Wrong cut value");
deba@644
   144
    check(ho.minCutValue() == cutValue(ugraph, cap1, cut), "Wrong cut value");
kpeter@643
   145
  }
kpeter@643
   146
  {
kpeter@643
   147
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap2);
kpeter@643
   148
    ho.run();
kpeter@643
   149
    ho.minCutMap(cut);
alpar@956
   150
deba@644
   151
    check(ho.minCutValue() == 5, "Wrong cut value");
deba@644
   152
    check(ho.minCutValue() == cutValue(ugraph, cap2, cut), "Wrong cut value");
kpeter@643
   153
  }
kpeter@643
   154
  {
kpeter@643
   155
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap3);
kpeter@643
   156
    ho.run();
kpeter@643
   157
    ho.minCutMap(cut);
alpar@956
   158
kpeter@643
   159
    check(ho.minCutValue() == 5, "Wrong cut value");
deba@644
   160
    check(ho.minCutValue() == cutValue(ugraph, cap3, cut), "Wrong cut value");
kpeter@643
   161
  }
deba@426
   162
deba@426
   163
  return 0;
deba@426
   164
}