test/hao_orlin_test.cc
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Mon, 27 Apr 2009 18:03:18 +0100
changeset 630 89705c452130
parent 588 293551ad254f
child 761 f1398882a928
child 796 7e368d9b67f7
permissions -rw-r--r--
Add CPLEX_ROOT_DIR variable to FindCPLEX (#277)
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@440
     5
 * Copyright (C) 2003-2009
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;
kpeter@588
    69
kpeter@588
    70
  HaoOrlin<Digraph, CapMap> ho_test(g, cap);
kpeter@588
    71
  const HaoOrlin<Digraph, CapMap>&
kpeter@588
    72
    const_ho_test = ho_test;
kpeter@588
    73
kpeter@588
    74
  ho_test.init();
kpeter@588
    75
  ho_test.init(n);
kpeter@588
    76
  ho_test.calculateOut();
kpeter@588
    77
  ho_test.calculateIn();
kpeter@588
    78
  ho_test.run();
kpeter@588
    79
  ho_test.run(n);
kpeter@588
    80
kpeter@588
    81
  v = const_ho_test.minCutValue();
kpeter@588
    82
  v = const_ho_test.minCutMap(cut);
kpeter@588
    83
}
kpeter@588
    84
kpeter@588
    85
template <typename Graph, typename CapMap, typename CutMap>
kpeter@588
    86
typename CapMap::Value 
kpeter@588
    87
  cutValue(const Graph& graph, const CapMap& cap, const CutMap& cut)
kpeter@588
    88
{
kpeter@588
    89
  typename CapMap::Value sum = 0;
kpeter@588
    90
  for (typename Graph::ArcIt a(graph); a != INVALID; ++a) {
kpeter@588
    91
    if (cut[graph.source(a)] && !cut[graph.target(a)])
kpeter@588
    92
      sum += cap[a];
kpeter@588
    93
  }
kpeter@588
    94
  return sum;
kpeter@588
    95
}
deba@410
    96
deba@410
    97
int main() {
kpeter@588
    98
  SmartDigraph graph;
kpeter@588
    99
  SmartDigraph::ArcMap<int> cap1(graph), cap2(graph), cap3(graph);
kpeter@588
   100
  SmartDigraph::NodeMap<bool> cut(graph);
deba@410
   101
kpeter@588
   102
  istringstream input(lgf);
kpeter@588
   103
  digraphReader(graph, input)
kpeter@588
   104
    .arcMap("cap1", cap1)
kpeter@588
   105
    .arcMap("cap2", cap2)
kpeter@588
   106
    .arcMap("cap3", cap3)
kpeter@588
   107
    .run();
deba@410
   108
kpeter@588
   109
  {
kpeter@588
   110
    HaoOrlin<SmartDigraph> ho(graph, cap1);
kpeter@588
   111
    ho.run();
kpeter@588
   112
    ho.minCutMap(cut);
kpeter@588
   113
    
deba@589
   114
    check(ho.minCutValue() == 1, "Wrong cut value");
deba@589
   115
    check(ho.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value");
kpeter@588
   116
  }
kpeter@588
   117
  {
kpeter@588
   118
    HaoOrlin<SmartDigraph> ho(graph, cap2);
kpeter@588
   119
    ho.run();
kpeter@588
   120
    ho.minCutMap(cut);
deba@589
   121
deba@589
   122
    check(ho.minCutValue() == 1, "Wrong cut value");
deba@589
   123
    check(ho.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value");
kpeter@588
   124
  }
kpeter@588
   125
  {
kpeter@588
   126
    HaoOrlin<SmartDigraph> ho(graph, cap3);
kpeter@588
   127
    ho.run();
kpeter@588
   128
    ho.minCutMap(cut);
kpeter@588
   129
    
deba@589
   130
    check(ho.minCutValue() == 1, "Wrong cut value");
deba@589
   131
    check(ho.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value");
kpeter@588
   132
  }
kpeter@588
   133
  
kpeter@588
   134
  typedef Undirector<SmartDigraph> UGraph;
kpeter@588
   135
  UGraph ugraph(graph);
kpeter@588
   136
  
kpeter@588
   137
  {
kpeter@588
   138
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap1);
kpeter@588
   139
    ho.run();
kpeter@588
   140
    ho.minCutMap(cut);
kpeter@588
   141
    
deba@589
   142
    check(ho.minCutValue() == 2, "Wrong cut value");
deba@589
   143
    check(ho.minCutValue() == cutValue(ugraph, cap1, cut), "Wrong cut value");
kpeter@588
   144
  }
kpeter@588
   145
  {
kpeter@588
   146
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap2);
kpeter@588
   147
    ho.run();
kpeter@588
   148
    ho.minCutMap(cut);
kpeter@588
   149
    
deba@589
   150
    check(ho.minCutValue() == 5, "Wrong cut value");
deba@589
   151
    check(ho.minCutValue() == cutValue(ugraph, cap2, cut), "Wrong cut value");
kpeter@588
   152
  }
kpeter@588
   153
  {
kpeter@588
   154
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap3);
kpeter@588
   155
    ho.run();
kpeter@588
   156
    ho.minCutMap(cut);
kpeter@588
   157
    
kpeter@588
   158
    check(ho.minCutValue() == 5, "Wrong cut value");
deba@589
   159
    check(ho.minCutValue() == cutValue(ugraph, cap3, cut), "Wrong cut value");
kpeter@588
   160
  }
deba@410
   161
deba@410
   162
  return 0;
deba@410
   163
}