test/nagamochi_ibaraki_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 09 Jan 2011 00:56:52 +0100
changeset 1034 ef200e268af2
child 1011 9a6c9d4ee77b
permissions -rw-r--r--
Unifications and improvements in TSP algorithms (#386)
deba@913
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@913
     2
 *
deba@913
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@913
     4
 *
deba@913
     5
 * Copyright (C) 2003-2010
deba@913
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@913
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@913
     8
 *
deba@913
     9
 * Permission to use, modify and distribute this software is granted
deba@913
    10
 * provided that this copyright notice appears in all copies. For
deba@913
    11
 * precise terms see the accompanying LICENSE file.
deba@913
    12
 *
deba@913
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@913
    14
 * express or implied, and with no claim as to its suitability for any
deba@913
    15
 * purpose.
deba@913
    16
 *
deba@913
    17
 */
deba@913
    18
deba@913
    19
#include <sstream>
deba@913
    20
deba@913
    21
#include <lemon/smart_graph.h>
deba@913
    22
#include <lemon/adaptors.h>
deba@913
    23
#include <lemon/concepts/graph.h>
deba@913
    24
#include <lemon/concepts/maps.h>
deba@913
    25
#include <lemon/lgf_reader.h>
deba@913
    26
#include <lemon/nagamochi_ibaraki.h>
deba@913
    27
deba@913
    28
#include "test_tools.h"
deba@913
    29
deba@913
    30
using namespace lemon;
deba@913
    31
using namespace std;
deba@913
    32
deba@913
    33
const std::string lgf =
deba@913
    34
  "@nodes\n"
deba@913
    35
  "label\n"
deba@913
    36
  "0\n"
deba@913
    37
  "1\n"
deba@913
    38
  "2\n"
deba@913
    39
  "3\n"
deba@913
    40
  "4\n"
deba@913
    41
  "5\n"
deba@913
    42
  "@edges\n"
deba@913
    43
  "     cap1 cap2 cap3\n"
deba@913
    44
  "0 1  1    1    1   \n"
deba@913
    45
  "0 2  2    2    4   \n"
deba@913
    46
  "1 2  4    4    4   \n"
deba@913
    47
  "3 4  1    1    1   \n"
deba@913
    48
  "3 5  2    2    4   \n"
deba@913
    49
  "4 5  4    4    4   \n"
deba@913
    50
  "2 3  1    6    6   \n";
deba@913
    51
deba@913
    52
void checkNagamochiIbarakiCompile()
deba@913
    53
{
deba@913
    54
  typedef int Value;
deba@913
    55
  typedef concepts::Graph Graph;
deba@913
    56
deba@913
    57
  typedef Graph::Node Node;
deba@913
    58
  typedef Graph::Edge Edge;
deba@913
    59
  typedef concepts::ReadMap<Edge, Value> CapMap;
deba@913
    60
  typedef concepts::WriteMap<Node, bool> CutMap;
deba@913
    61
deba@913
    62
  Graph g;
deba@913
    63
  Node n;
deba@913
    64
  CapMap cap;
deba@913
    65
  CutMap cut;
deba@913
    66
  Value v;
deba@913
    67
  bool b;
deba@913
    68
deba@913
    69
  NagamochiIbaraki<Graph, CapMap> ni_test(g, cap);
deba@913
    70
  const NagamochiIbaraki<Graph, CapMap>& const_ni_test = ni_test;
deba@913
    71
deba@913
    72
  ni_test.init();
deba@913
    73
  ni_test.start();
deba@913
    74
  b = ni_test.processNextPhase();
deba@913
    75
  ni_test.run();
deba@913
    76
deba@913
    77
  v = const_ni_test.minCutValue();
deba@913
    78
  v = const_ni_test.minCutMap(cut);
deba@913
    79
}
deba@913
    80
deba@913
    81
template <typename Graph, typename CapMap, typename CutMap>
deba@913
    82
typename CapMap::Value
deba@913
    83
  cutValue(const Graph& graph, const CapMap& cap, const CutMap& cut)
deba@913
    84
{
deba@913
    85
  typename CapMap::Value sum = 0;
deba@913
    86
  for (typename Graph::EdgeIt e(graph); e != INVALID; ++e) {
deba@913
    87
    if (cut[graph.u(e)] != cut[graph.v(e)]) {
deba@913
    88
      sum += cap[e];
deba@913
    89
    }
deba@913
    90
  }
deba@913
    91
  return sum;
deba@913
    92
}
deba@913
    93
deba@913
    94
int main() {
deba@913
    95
  SmartGraph graph;
deba@913
    96
  SmartGraph::EdgeMap<int> cap1(graph), cap2(graph), cap3(graph);
deba@913
    97
  SmartGraph::NodeMap<bool> cut(graph);
deba@913
    98
deba@913
    99
  istringstream input(lgf);
deba@913
   100
  graphReader(graph, input)
deba@913
   101
    .edgeMap("cap1", cap1)
deba@913
   102
    .edgeMap("cap2", cap2)
deba@913
   103
    .edgeMap("cap3", cap3)
deba@913
   104
    .run();
deba@913
   105
deba@913
   106
  {
deba@913
   107
    NagamochiIbaraki<SmartGraph> ni(graph, cap1);
deba@913
   108
    ni.run();
deba@913
   109
    ni.minCutMap(cut);
deba@913
   110
deba@913
   111
    check(ni.minCutValue() == 1, "Wrong cut value");
deba@913
   112
    check(ni.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value");
deba@913
   113
  }
deba@913
   114
  {
deba@913
   115
    NagamochiIbaraki<SmartGraph> ni(graph, cap2);
deba@913
   116
    ni.run();
deba@913
   117
    ni.minCutMap(cut);
deba@913
   118
deba@913
   119
    check(ni.minCutValue() == 3, "Wrong cut value");
deba@913
   120
    check(ni.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value");
deba@913
   121
  }
deba@913
   122
  {
deba@913
   123
    NagamochiIbaraki<SmartGraph> ni(graph, cap3);
deba@913
   124
    ni.run();
deba@913
   125
    ni.minCutMap(cut);
deba@913
   126
deba@913
   127
    check(ni.minCutValue() == 5, "Wrong cut value");
deba@913
   128
    check(ni.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value");
deba@913
   129
  }
deba@913
   130
  {
deba@913
   131
    NagamochiIbaraki<SmartGraph>::SetUnitCapacity::Create ni(graph);
deba@913
   132
    ni.run();
deba@913
   133
    ni.minCutMap(cut);
deba@913
   134
deba@913
   135
    ConstMap<SmartGraph::Edge, int> cap4(1);
deba@913
   136
    check(ni.minCutValue() == 1, "Wrong cut value");
deba@913
   137
    check(ni.minCutValue() == cutValue(graph, cap4, cut), "Wrong cut value");
deba@913
   138
  }
deba@913
   139
deba@913
   140
  return 0;
deba@913
   141
}