test/min_cost_arborescence_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 07 Aug 2013 06:29:34 +0200
changeset 1257 3e711ee55d31
parent 1171 7e368d9b67f7
child 1258 bdfc038f364c
child 1259 8b2d4e5d96e4
permissions -rw-r--r--
Add explicit namespace to ignore_unused_variable_warning() usages (#294)
deba@522
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@522
     2
 *
deba@522
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@522
     4
 *
deba@522
     5
 * Copyright (C) 2003-2008
deba@522
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@522
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@522
     8
 *
deba@522
     9
 * Permission to use, modify and distribute this software is granted
deba@522
    10
 * provided that this copyright notice appears in all copies. For
deba@522
    11
 * precise terms see the accompanying LICENSE file.
deba@522
    12
 *
deba@522
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@522
    14
 * express or implied, and with no claim as to its suitability for any
deba@522
    15
 * purpose.
deba@522
    16
 *
deba@522
    17
 */
deba@522
    18
deba@522
    19
#include <iostream>
deba@522
    20
#include <set>
deba@522
    21
#include <vector>
deba@522
    22
#include <iterator>
deba@522
    23
deba@522
    24
#include <lemon/smart_graph.h>
deba@522
    25
#include <lemon/min_cost_arborescence.h>
deba@522
    26
#include <lemon/lgf_reader.h>
kpeter@672
    27
#include <lemon/concepts/digraph.h>
deba@522
    28
deba@522
    29
#include "test_tools.h"
deba@522
    30
deba@522
    31
using namespace lemon;
deba@522
    32
using namespace std;
deba@522
    33
deba@522
    34
const char test_lgf[] =
deba@522
    35
  "@nodes\n"
deba@522
    36
  "label\n"
deba@522
    37
  "0\n"
deba@522
    38
  "1\n"
deba@522
    39
  "2\n"
deba@522
    40
  "3\n"
deba@522
    41
  "4\n"
deba@522
    42
  "5\n"
deba@522
    43
  "6\n"
deba@522
    44
  "7\n"
deba@522
    45
  "8\n"
deba@522
    46
  "9\n"
deba@522
    47
  "@arcs\n"
deba@522
    48
  "     label  cost\n"
deba@522
    49
  "1 8  0      107\n"
deba@522
    50
  "0 3  1      70\n"
deba@522
    51
  "2 1  2      46\n"
deba@522
    52
  "4 1  3      28\n"
deba@522
    53
  "4 4  4      91\n"
deba@522
    54
  "3 9  5      76\n"
deba@522
    55
  "9 8  6      61\n"
deba@522
    56
  "8 1  7      39\n"
deba@522
    57
  "9 8  8      74\n"
deba@522
    58
  "8 0  9      39\n"
deba@522
    59
  "4 3  10     45\n"
deba@522
    60
  "2 2  11     34\n"
deba@522
    61
  "0 1  12     100\n"
deba@522
    62
  "6 3  13     95\n"
deba@522
    63
  "4 1  14     22\n"
deba@522
    64
  "1 1  15     31\n"
deba@522
    65
  "7 2  16     51\n"
deba@522
    66
  "2 6  17     29\n"
deba@522
    67
  "8 3  18     115\n"
deba@522
    68
  "6 9  19     32\n"
deba@522
    69
  "1 1  20     60\n"
deba@522
    70
  "0 3  21     40\n"
deba@522
    71
  "@attributes\n"
deba@522
    72
  "source 0\n";
deba@522
    73
kpeter@672
    74
kpeter@672
    75
void checkMinCostArborescenceCompile()
kpeter@672
    76
{
kpeter@672
    77
  typedef double VType;
kpeter@672
    78
  typedef concepts::Digraph Digraph;
kpeter@672
    79
  typedef concepts::ReadMap<Digraph::Arc, VType> CostMap;
kpeter@672
    80
  typedef Digraph::Node Node;
kpeter@672
    81
  typedef Digraph::Arc Arc;
kpeter@672
    82
  typedef concepts::WriteMap<Digraph::Arc, bool> ArbMap;
kpeter@672
    83
  typedef concepts::ReadWriteMap<Digraph::Node, Digraph::Arc> PredMap;
kpeter@672
    84
kpeter@672
    85
  typedef MinCostArborescence<Digraph, CostMap>::
kpeter@672
    86
            SetArborescenceMap<ArbMap>::
kpeter@672
    87
            SetPredMap<PredMap>::Create MinCostArbType;
kpeter@672
    88
kpeter@672
    89
  Digraph g;
kpeter@672
    90
  Node s, n;
kpeter@672
    91
  Arc e;
kpeter@672
    92
  VType c;
kpeter@672
    93
  bool b;
alpar@1257
    94
  ::lemon::ignore_unused_variable_warning(c,b);
kpeter@672
    95
  int i;
kpeter@672
    96
  CostMap cost;
kpeter@672
    97
  ArbMap arb;
kpeter@672
    98
  PredMap pred;
kpeter@672
    99
kpeter@672
   100
  MinCostArbType mcarb_test(g, cost);
kpeter@672
   101
  const MinCostArbType& const_mcarb_test = mcarb_test;
kpeter@672
   102
kpeter@672
   103
  mcarb_test
kpeter@672
   104
    .arborescenceMap(arb)
kpeter@672
   105
    .predMap(pred)
kpeter@672
   106
    .run(s);
kpeter@672
   107
kpeter@672
   108
  mcarb_test.init();
kpeter@672
   109
  mcarb_test.addSource(s);
kpeter@672
   110
  mcarb_test.start();
kpeter@672
   111
  n = mcarb_test.processNextNode();
kpeter@672
   112
  b = const_mcarb_test.emptyQueue();
kpeter@672
   113
  i = const_mcarb_test.queueSize();
kpeter@672
   114
  
kpeter@672
   115
  c = const_mcarb_test.arborescenceCost();
kpeter@672
   116
  b = const_mcarb_test.arborescence(e);
kpeter@672
   117
  e = const_mcarb_test.pred(n);
kpeter@672
   118
  const MinCostArbType::ArborescenceMap &am =
kpeter@672
   119
    const_mcarb_test.arborescenceMap();
kpeter@672
   120
  const MinCostArbType::PredMap &pm =
kpeter@672
   121
    const_mcarb_test.predMap();
kpeter@672
   122
  b = const_mcarb_test.reached(n);
kpeter@672
   123
  b = const_mcarb_test.processed(n);
kpeter@672
   124
  
kpeter@672
   125
  i = const_mcarb_test.dualNum();
kpeter@672
   126
  c = const_mcarb_test.dualValue();
kpeter@672
   127
  i = const_mcarb_test.dualSize(i);
kpeter@672
   128
  c = const_mcarb_test.dualValue(i);
kpeter@672
   129
  
alpar@1257
   130
  ::lemon::ignore_unused_variable_warning(am);
alpar@1257
   131
  ::lemon::ignore_unused_variable_warning(pm);
kpeter@672
   132
}
kpeter@672
   133
deba@522
   134
int main() {
deba@522
   135
  typedef SmartDigraph Digraph;
deba@522
   136
  DIGRAPH_TYPEDEFS(Digraph);
deba@522
   137
deba@522
   138
  typedef Digraph::ArcMap<double> CostMap;
deba@522
   139
deba@522
   140
  Digraph digraph;
deba@522
   141
  CostMap cost(digraph);
deba@522
   142
  Node source;
deba@522
   143
deba@522
   144
  std::istringstream is(test_lgf);
deba@522
   145
  digraphReader(digraph, is).
deba@522
   146
    arcMap("cost", cost).
deba@522
   147
    node("source", source).run();
deba@522
   148
deba@522
   149
  MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
deba@522
   150
  mca.run(source);
deba@522
   151
deba@522
   152
  vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
deba@522
   153
deba@522
   154
  for (int i = 0; i < mca.dualNum(); ++i) {
deba@522
   155
    dualSolution[i].first = mca.dualValue(i);
deba@522
   156
    for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
deba@522
   157
         it != INVALID; ++it) {
deba@522
   158
      dualSolution[i].second.insert(it);
deba@522
   159
    }
deba@522
   160
  }
deba@522
   161
deba@522
   162
  for (ArcIt it(digraph); it != INVALID; ++it) {
deba@522
   163
    if (mca.reached(digraph.source(it))) {
deba@522
   164
      double sum = 0.0;
deba@522
   165
      for (int i = 0; i < int(dualSolution.size()); ++i) {
deba@522
   166
        if (dualSolution[i].second.find(digraph.target(it))
deba@522
   167
            != dualSolution[i].second.end() &&
deba@522
   168
            dualSolution[i].second.find(digraph.source(it))
deba@522
   169
            == dualSolution[i].second.end()) {
deba@522
   170
          sum += dualSolution[i].first;
deba@522
   171
        }
deba@522
   172
      }
deba@522
   173
      if (mca.arborescence(it)) {
kpeter@672
   174
        check(sum == cost[it], "Invalid dual solution");
deba@522
   175
      }
kpeter@672
   176
      check(sum <= cost[it], "Invalid dual solution");
deba@522
   177
    }
deba@522
   178
  }
deba@522
   179
deba@522
   180
kpeter@672
   181
  check(mca.dualValue() == mca.arborescenceCost(), "Invalid dual solution");
deba@522
   182
kpeter@672
   183
  check(mca.reached(source), "Invalid arborescence");
deba@522
   184
  for (ArcIt a(digraph); a != INVALID; ++a) {
deba@522
   185
    check(!mca.reached(digraph.source(a)) ||
kpeter@672
   186
          mca.reached(digraph.target(a)), "Invalid arborescence");
deba@522
   187
  }
deba@522
   188
deba@522
   189
  for (NodeIt n(digraph); n != INVALID; ++n) {
deba@522
   190
    if (!mca.reached(n)) continue;
deba@522
   191
    int cnt = 0;
deba@522
   192
    for (InArcIt a(digraph, n); a != INVALID; ++a) {
deba@522
   193
      if (mca.arborescence(a)) {
kpeter@672
   194
        check(mca.pred(n) == a, "Invalid arborescence");
deba@522
   195
        ++cnt;
deba@522
   196
      }
deba@522
   197
    }
kpeter@672
   198
    check((n == source ? cnt == 0 : cnt == 1), "Invalid arborescence");
deba@522
   199
  }
deba@522
   200
deba@522
   201
  Digraph::ArcMap<bool> arborescence(digraph);
kpeter@672
   202
  check(mca.arborescenceCost() ==
deba@522
   203
        minCostArborescence(digraph, cost, source, arborescence),
kpeter@672
   204
        "Wrong result of the function interface");
deba@522
   205
deba@522
   206
  return 0;
deba@522
   207
}