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