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