deba@512: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@512: * deba@512: * This file is a part of LEMON, a generic C++ optimization library. deba@512: * alpar@761: * Copyright (C) 2003-2011 deba@512: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@512: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@512: * deba@512: * Permission to use, modify and distribute this software is granted deba@512: * provided that this copyright notice appears in all copies. For deba@512: * precise terms see the accompanying LICENSE file. deba@512: * deba@512: * This software is provided "AS IS" with no warranty of any kind, deba@512: * express or implied, and with no claim as to its suitability for any deba@512: * purpose. deba@512: * deba@512: */ deba@512: deba@512: #include deba@512: #include deba@512: #include deba@512: #include deba@512: deba@512: #include deba@512: #include deba@512: #include kpeter@617: #include deba@512: deba@512: #include "test_tools.h" deba@512: deba@512: using namespace lemon; deba@512: using namespace std; deba@512: deba@512: const char test_lgf[] = deba@512: "@nodes\n" deba@512: "label\n" deba@512: "0\n" deba@512: "1\n" deba@512: "2\n" deba@512: "3\n" deba@512: "4\n" deba@512: "5\n" deba@512: "6\n" deba@512: "7\n" deba@512: "8\n" deba@512: "9\n" deba@512: "@arcs\n" deba@512: " label cost\n" deba@512: "1 8 0 107\n" deba@512: "0 3 1 70\n" deba@512: "2 1 2 46\n" deba@512: "4 1 3 28\n" deba@512: "4 4 4 91\n" deba@512: "3 9 5 76\n" deba@512: "9 8 6 61\n" deba@512: "8 1 7 39\n" deba@512: "9 8 8 74\n" deba@512: "8 0 9 39\n" deba@512: "4 3 10 45\n" deba@512: "2 2 11 34\n" deba@512: "0 1 12 100\n" deba@512: "6 3 13 95\n" deba@512: "4 1 14 22\n" deba@512: "1 1 15 31\n" deba@512: "7 2 16 51\n" deba@512: "2 6 17 29\n" deba@512: "8 3 18 115\n" deba@512: "6 9 19 32\n" deba@512: "1 1 20 60\n" deba@512: "0 3 21 40\n" deba@512: "@attributes\n" deba@512: "source 0\n"; deba@512: kpeter@617: kpeter@617: void checkMinCostArborescenceCompile() kpeter@617: { kpeter@617: typedef double VType; kpeter@617: typedef concepts::Digraph Digraph; kpeter@617: typedef concepts::ReadMap CostMap; kpeter@617: typedef Digraph::Node Node; kpeter@617: typedef Digraph::Arc Arc; kpeter@617: typedef concepts::WriteMap ArbMap; kpeter@617: typedef concepts::ReadWriteMap PredMap; kpeter@617: kpeter@617: typedef MinCostArborescence:: kpeter@617: SetArborescenceMap:: kpeter@617: SetPredMap::Create MinCostArbType; kpeter@617: kpeter@617: Digraph g; kpeter@617: Node s, n; kpeter@617: Arc e; kpeter@617: VType c; kpeter@617: bool b; alpar@796: ignore_unused_variable_warning(c,b); kpeter@617: int i; kpeter@617: CostMap cost; kpeter@617: ArbMap arb; kpeter@617: PredMap pred; kpeter@617: kpeter@617: MinCostArbType mcarb_test(g, cost); kpeter@617: const MinCostArbType& const_mcarb_test = mcarb_test; kpeter@617: kpeter@617: mcarb_test kpeter@617: .arborescenceMap(arb) kpeter@617: .predMap(pred) kpeter@617: .run(s); kpeter@617: kpeter@617: mcarb_test.init(); kpeter@617: mcarb_test.addSource(s); kpeter@617: mcarb_test.start(); kpeter@617: n = mcarb_test.processNextNode(); kpeter@617: b = const_mcarb_test.emptyQueue(); kpeter@617: i = const_mcarb_test.queueSize(); alpar@761: kpeter@617: c = const_mcarb_test.arborescenceCost(); kpeter@617: b = const_mcarb_test.arborescence(e); kpeter@617: e = const_mcarb_test.pred(n); kpeter@617: const MinCostArbType::ArborescenceMap &am = kpeter@617: const_mcarb_test.arborescenceMap(); kpeter@617: const MinCostArbType::PredMap &pm = kpeter@617: const_mcarb_test.predMap(); kpeter@617: b = const_mcarb_test.reached(n); kpeter@617: b = const_mcarb_test.processed(n); alpar@761: kpeter@617: i = const_mcarb_test.dualNum(); kpeter@617: c = const_mcarb_test.dualValue(); kpeter@617: i = const_mcarb_test.dualSize(i); kpeter@617: c = const_mcarb_test.dualValue(i); alpar@761: kpeter@617: ignore_unused_variable_warning(am); kpeter@617: ignore_unused_variable_warning(pm); kpeter@617: } kpeter@617: deba@512: int main() { deba@512: typedef SmartDigraph Digraph; deba@512: DIGRAPH_TYPEDEFS(Digraph); deba@512: deba@512: typedef Digraph::ArcMap CostMap; deba@512: deba@512: Digraph digraph; deba@512: CostMap cost(digraph); deba@512: Node source; deba@512: deba@512: std::istringstream is(test_lgf); deba@512: digraphReader(digraph, is). deba@512: arcMap("cost", cost). deba@512: node("source", source).run(); deba@512: deba@512: MinCostArborescence mca(digraph, cost); deba@512: mca.run(source); deba@512: deba@512: vector > > dualSolution(mca.dualNum()); deba@512: deba@512: for (int i = 0; i < mca.dualNum(); ++i) { deba@512: dualSolution[i].first = mca.dualValue(i); deba@512: for (MinCostArborescence::DualIt it(mca, i); deba@512: it != INVALID; ++it) { deba@512: dualSolution[i].second.insert(it); deba@512: } deba@512: } deba@512: deba@512: for (ArcIt it(digraph); it != INVALID; ++it) { deba@512: if (mca.reached(digraph.source(it))) { deba@512: double sum = 0.0; deba@512: for (int i = 0; i < int(dualSolution.size()); ++i) { deba@512: if (dualSolution[i].second.find(digraph.target(it)) deba@512: != dualSolution[i].second.end() && deba@512: dualSolution[i].second.find(digraph.source(it)) deba@512: == dualSolution[i].second.end()) { deba@512: sum += dualSolution[i].first; deba@512: } deba@512: } deba@512: if (mca.arborescence(it)) { kpeter@617: check(sum == cost[it], "Invalid dual solution"); deba@512: } kpeter@617: check(sum <= cost[it], "Invalid dual solution"); deba@512: } deba@512: } deba@512: deba@512: kpeter@617: check(mca.dualValue() == mca.arborescenceCost(), "Invalid dual solution"); deba@512: kpeter@617: check(mca.reached(source), "Invalid arborescence"); deba@512: for (ArcIt a(digraph); a != INVALID; ++a) { deba@512: check(!mca.reached(digraph.source(a)) || kpeter@617: mca.reached(digraph.target(a)), "Invalid arborescence"); deba@512: } deba@512: deba@512: for (NodeIt n(digraph); n != INVALID; ++n) { deba@512: if (!mca.reached(n)) continue; deba@512: int cnt = 0; deba@512: for (InArcIt a(digraph, n); a != INVALID; ++a) { deba@512: if (mca.arborescence(a)) { kpeter@617: check(mca.pred(n) == a, "Invalid arborescence"); deba@512: ++cnt; deba@512: } deba@512: } kpeter@617: check((n == source ? cnt == 0 : cnt == 1), "Invalid arborescence"); deba@512: } deba@512: deba@512: Digraph::ArcMap arborescence(digraph); kpeter@617: check(mca.arborescenceCost() == deba@512: minCostArborescence(digraph, cost, source, arborescence), kpeter@617: "Wrong result of the function interface"); deba@512: deba@512: return 0; deba@512: }