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