kpeter@593: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@593: * kpeter@593: * This file is a part of LEMON, a generic C++ optimization library. kpeter@593: * kpeter@593: * Copyright (C) 2003-2009 kpeter@593: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@593: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@593: * kpeter@593: * Permission to use, modify and distribute this software is granted kpeter@593: * provided that this copyright notice appears in all copies. For kpeter@593: * precise terms see the accompanying LICENSE file. kpeter@593: * kpeter@593: * This software is provided "AS IS" with no warranty of any kind, kpeter@593: * express or implied, and with no claim as to its suitability for any kpeter@593: * purpose. kpeter@593: * kpeter@593: */ kpeter@593: kpeter@593: #include kpeter@593: #include kpeter@593: kpeter@593: #include kpeter@593: #include kpeter@593: kpeter@593: #include kpeter@593: kpeter@593: #include kpeter@593: #include kpeter@593: kpeter@593: #include "test_tools.h" kpeter@593: kpeter@593: using namespace lemon; kpeter@593: kpeter@593: char test_lgf[] = kpeter@593: "@nodes\n" kpeter@601: "label sup1 sup2 sup3 sup4 sup5\n" kpeter@601: " 1 20 27 0 20 30\n" kpeter@601: " 2 -4 0 0 -8 -3\n" kpeter@601: " 3 0 0 0 0 0\n" kpeter@601: " 4 0 0 0 0 0\n" kpeter@601: " 5 9 0 0 6 11\n" kpeter@601: " 6 -6 0 0 -5 -6\n" kpeter@601: " 7 0 0 0 0 0\n" kpeter@601: " 8 0 0 0 0 3\n" kpeter@601: " 9 3 0 0 0 0\n" kpeter@601: " 10 -2 0 0 -7 -2\n" kpeter@601: " 11 0 0 0 -10 0\n" kpeter@601: " 12 -20 -27 0 -30 -20\n" kpeter@593: "\n" kpeter@593: "@arcs\n" kpeter@593: " cost cap low1 low2\n" kpeter@593: " 1 2 70 11 0 8\n" kpeter@593: " 1 3 150 3 0 1\n" kpeter@593: " 1 4 80 15 0 2\n" kpeter@593: " 2 8 80 12 0 0\n" kpeter@593: " 3 5 140 5 0 3\n" kpeter@593: " 4 6 60 10 0 1\n" kpeter@593: " 4 7 80 2 0 0\n" kpeter@593: " 4 8 110 3 0 0\n" kpeter@593: " 5 7 60 14 0 0\n" kpeter@593: " 5 11 120 12 0 0\n" kpeter@593: " 6 3 0 3 0 0\n" kpeter@593: " 6 9 140 4 0 0\n" kpeter@593: " 6 10 90 8 0 0\n" kpeter@593: " 7 1 30 5 0 0\n" kpeter@593: " 8 12 60 16 0 4\n" kpeter@593: " 9 12 50 6 0 0\n" kpeter@593: "10 12 70 13 0 5\n" kpeter@593: "10 2 100 7 0 0\n" kpeter@593: "10 7 60 10 0 0\n" kpeter@593: "11 10 20 14 0 6\n" kpeter@593: "12 11 30 10 0 0\n" kpeter@593: "\n" kpeter@593: "@attributes\n" kpeter@593: "source 1\n" kpeter@593: "target 12\n"; kpeter@593: kpeter@593: kpeter@601: enum ProblemType { kpeter@601: EQ, kpeter@601: GEQ, kpeter@601: LEQ kpeter@601: }; kpeter@601: kpeter@593: // Check the interface of an MCF algorithm kpeter@599: template kpeter@593: class McfClassConcept kpeter@593: { kpeter@593: public: kpeter@593: kpeter@593: template kpeter@593: struct Constraints { kpeter@593: void constraints() { kpeter@593: checkConcept(); kpeter@593: kpeter@597: MCF mcf(g); kpeter@593: kpeter@598: b = mcf.reset() kpeter@598: .lowerMap(lower) kpeter@597: .upperMap(upper) kpeter@597: .capacityMap(upper) kpeter@597: .boundMaps(lower, upper) kpeter@597: .costMap(cost) kpeter@597: .supplyMap(sup) kpeter@597: .stSupply(n, n, k) kpeter@601: .flowMap(flow) kpeter@601: .potentialMap(pot) kpeter@597: .run(); kpeter@601: kpeter@601: const MCF& const_mcf = mcf; kpeter@593: kpeter@601: const typename MCF::FlowMap &fm = const_mcf.flowMap(); kpeter@601: const typename MCF::PotentialMap &pm = const_mcf.potentialMap(); kpeter@597: kpeter@601: v = const_mcf.totalCost(); kpeter@601: double x = const_mcf.template totalCost(); kpeter@601: v = const_mcf.flow(a); kpeter@601: v = const_mcf.potential(n); kpeter@597: kpeter@593: ignore_unused_variable_warning(fm); kpeter@593: ignore_unused_variable_warning(pm); kpeter@597: ignore_unused_variable_warning(x); kpeter@593: } kpeter@593: kpeter@593: typedef typename GR::Node Node; kpeter@593: typedef typename GR::Arc Arc; kpeter@599: typedef concepts::ReadMap NM; kpeter@599: typedef concepts::ReadMap FAM; kpeter@599: typedef concepts::ReadMap CAM; kpeter@593: kpeter@593: const GR &g; kpeter@599: const FAM &lower; kpeter@599: const FAM &upper; kpeter@599: const CAM &cost; kpeter@593: const NM ⊃ kpeter@593: const Node &n; kpeter@593: const Arc &a; kpeter@599: const Flow &k; kpeter@599: Flow v; kpeter@597: bool b; kpeter@593: kpeter@593: typename MCF::FlowMap &flow; kpeter@593: typename MCF::PotentialMap &pot; kpeter@593: }; kpeter@593: kpeter@593: }; kpeter@593: kpeter@593: kpeter@593: // Check the feasibility of the given flow (primal soluiton) kpeter@593: template < typename GR, typename LM, typename UM, kpeter@593: typename SM, typename FM > kpeter@593: bool checkFlow( const GR& gr, const LM& lower, const UM& upper, kpeter@601: const SM& supply, const FM& flow, kpeter@601: ProblemType type = EQ ) kpeter@593: { kpeter@593: TEMPLATE_DIGRAPH_TYPEDEFS(GR); kpeter@593: kpeter@593: for (ArcIt e(gr); e != INVALID; ++e) { kpeter@593: if (flow[e] < lower[e] || flow[e] > upper[e]) return false; kpeter@593: } kpeter@593: kpeter@593: for (NodeIt n(gr); n != INVALID; ++n) { kpeter@593: typename SM::Value sum = 0; kpeter@593: for (OutArcIt e(gr, n); e != INVALID; ++e) kpeter@593: sum += flow[e]; kpeter@593: for (InArcIt e(gr, n); e != INVALID; ++e) kpeter@593: sum -= flow[e]; kpeter@601: bool b = (type == EQ && sum == supply[n]) || kpeter@601: (type == GEQ && sum >= supply[n]) || kpeter@601: (type == LEQ && sum <= supply[n]); kpeter@601: if (!b) return false; kpeter@593: } kpeter@593: kpeter@593: return true; kpeter@593: } kpeter@593: kpeter@593: // Check the feasibility of the given potentials (dual soluiton) kpeter@597: // using the "Complementary Slackness" optimality condition kpeter@593: template < typename GR, typename LM, typename UM, kpeter@601: typename CM, typename SM, typename FM, typename PM > kpeter@593: bool checkPotential( const GR& gr, const LM& lower, const UM& upper, kpeter@601: const CM& cost, const SM& supply, const FM& flow, kpeter@601: const PM& pi ) kpeter@593: { kpeter@593: TEMPLATE_DIGRAPH_TYPEDEFS(GR); kpeter@593: kpeter@593: bool opt = true; kpeter@593: for (ArcIt e(gr); opt && e != INVALID; ++e) { kpeter@593: typename CM::Value red_cost = kpeter@593: cost[e] + pi[gr.source(e)] - pi[gr.target(e)]; kpeter@593: opt = red_cost == 0 || kpeter@593: (red_cost > 0 && flow[e] == lower[e]) || kpeter@593: (red_cost < 0 && flow[e] == upper[e]); kpeter@593: } kpeter@601: kpeter@601: for (NodeIt n(gr); opt && n != INVALID; ++n) { kpeter@601: typename SM::Value sum = 0; kpeter@601: for (OutArcIt e(gr, n); e != INVALID; ++e) kpeter@601: sum += flow[e]; kpeter@601: for (InArcIt e(gr, n); e != INVALID; ++e) kpeter@601: sum -= flow[e]; kpeter@601: opt = (sum == supply[n]) || (pi[n] == 0); kpeter@601: } kpeter@601: kpeter@593: return opt; kpeter@593: } kpeter@593: kpeter@593: // Run a minimum cost flow algorithm and check the results kpeter@593: template < typename MCF, typename GR, kpeter@593: typename LM, typename UM, kpeter@593: typename CM, typename SM > kpeter@593: void checkMcf( const MCF& mcf, bool mcf_result, kpeter@593: const GR& gr, const LM& lower, const UM& upper, kpeter@593: const CM& cost, const SM& supply, kpeter@593: bool result, typename CM::Value total, kpeter@601: const std::string &test_id = "", kpeter@601: ProblemType type = EQ ) kpeter@593: { kpeter@593: check(mcf_result == result, "Wrong result " + test_id); kpeter@593: if (result) { kpeter@601: check(checkFlow(gr, lower, upper, supply, mcf.flowMap(), type), kpeter@593: "The flow is not feasible " + test_id); kpeter@593: check(mcf.totalCost() == total, "The flow is not optimal " + test_id); kpeter@601: check(checkPotential(gr, lower, upper, cost, supply, mcf.flowMap(), kpeter@593: mcf.potentialMap()), kpeter@593: "Wrong potentials " + test_id); kpeter@593: } kpeter@593: } kpeter@593: kpeter@593: int main() kpeter@593: { kpeter@593: // Check the interfaces kpeter@593: { kpeter@599: typedef int Flow; kpeter@599: typedef int Cost; kpeter@607: typedef concepts::Digraph GR; kpeter@599: checkConcept< McfClassConcept, kpeter@599: NetworkSimplex >(); kpeter@593: } kpeter@593: kpeter@593: // Run various MCF tests kpeter@593: typedef ListDigraph Digraph; kpeter@593: DIGRAPH_TYPEDEFS(ListDigraph); kpeter@593: kpeter@593: // Read the test digraph kpeter@593: Digraph gr; kpeter@593: Digraph::ArcMap c(gr), l1(gr), l2(gr), u(gr); kpeter@601: Digraph::NodeMap s1(gr), s2(gr), s3(gr), s4(gr), s5(gr); kpeter@597: ConstMap cc(1), cu(std::numeric_limits::max()); kpeter@593: Node v, w; kpeter@593: kpeter@593: std::istringstream input(test_lgf); kpeter@593: DigraphReader(gr, input) kpeter@593: .arcMap("cost", c) kpeter@593: .arcMap("cap", u) kpeter@593: .arcMap("low1", l1) kpeter@593: .arcMap("low2", l2) kpeter@593: .nodeMap("sup1", s1) kpeter@593: .nodeMap("sup2", s2) kpeter@593: .nodeMap("sup3", s3) kpeter@601: .nodeMap("sup4", s4) kpeter@601: .nodeMap("sup5", s5) kpeter@593: .node("source", v) kpeter@593: .node("target", w) kpeter@593: .run(); kpeter@593: kpeter@597: // A. Test NetworkSimplex with the default pivot rule kpeter@593: { kpeter@598: NetworkSimplex mcf(gr); kpeter@593: kpeter@601: // Check the equality form kpeter@598: mcf.upperMap(u).costMap(c); kpeter@598: checkMcf(mcf, mcf.supplyMap(s1).run(), kpeter@597: gr, l1, u, c, s1, true, 5240, "#A1"); kpeter@598: checkMcf(mcf, mcf.stSupply(v, w, 27).run(), kpeter@597: gr, l1, u, c, s2, true, 7620, "#A2"); kpeter@598: mcf.lowerMap(l2); kpeter@598: checkMcf(mcf, mcf.supplyMap(s1).run(), kpeter@597: gr, l2, u, c, s1, true, 5970, "#A3"); kpeter@598: checkMcf(mcf, mcf.stSupply(v, w, 27).run(), kpeter@597: gr, l2, u, c, s2, true, 8010, "#A4"); kpeter@598: mcf.reset(); kpeter@598: checkMcf(mcf, mcf.supplyMap(s1).run(), kpeter@597: gr, l1, cu, cc, s1, true, 74, "#A5"); kpeter@598: checkMcf(mcf, mcf.lowerMap(l2).stSupply(v, w, 27).run(), kpeter@597: gr, l2, cu, cc, s2, true, 94, "#A6"); kpeter@598: mcf.reset(); kpeter@598: checkMcf(mcf, mcf.run(), kpeter@597: gr, l1, cu, cc, s3, true, 0, "#A7"); kpeter@598: checkMcf(mcf, mcf.boundMaps(l2, u).run(), kpeter@597: gr, l2, u, cc, s3, false, 0, "#A8"); kpeter@601: kpeter@601: // Check the GEQ form kpeter@601: mcf.reset().upperMap(u).costMap(c).supplyMap(s4); kpeter@601: checkMcf(mcf, mcf.run(), kpeter@601: gr, l1, u, c, s4, true, 3530, "#A9", GEQ); kpeter@601: mcf.problemType(mcf.GEQ); kpeter@601: checkMcf(mcf, mcf.lowerMap(l2).run(), kpeter@601: gr, l2, u, c, s4, true, 4540, "#A10", GEQ); kpeter@601: mcf.problemType(mcf.CARRY_SUPPLIES).supplyMap(s5); kpeter@601: checkMcf(mcf, mcf.run(), kpeter@601: gr, l2, u, c, s5, false, 0, "#A11", GEQ); kpeter@601: kpeter@601: // Check the LEQ form kpeter@601: mcf.reset().problemType(mcf.LEQ); kpeter@601: mcf.upperMap(u).costMap(c).supplyMap(s5); kpeter@601: checkMcf(mcf, mcf.run(), kpeter@601: gr, l1, u, c, s5, true, 5080, "#A12", LEQ); kpeter@601: checkMcf(mcf, mcf.lowerMap(l2).run(), kpeter@601: gr, l2, u, c, s5, true, 5930, "#A13", LEQ); kpeter@601: mcf.problemType(mcf.SATISFY_DEMANDS).supplyMap(s4); kpeter@601: checkMcf(mcf, mcf.run(), kpeter@601: gr, l2, u, c, s4, false, 0, "#A14", LEQ); kpeter@593: } kpeter@593: kpeter@597: // B. Test NetworkSimplex with each pivot rule kpeter@593: { kpeter@598: NetworkSimplex mcf(gr); kpeter@598: mcf.supplyMap(s1).costMap(c).capacityMap(u).lowerMap(l2); kpeter@593: kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::FIRST_ELIGIBLE), kpeter@597: gr, l2, u, c, s1, true, 5970, "#B1"); kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::BEST_ELIGIBLE), kpeter@597: gr, l2, u, c, s1, true, 5970, "#B2"); kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::BLOCK_SEARCH), kpeter@597: gr, l2, u, c, s1, true, 5970, "#B3"); kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::CANDIDATE_LIST), kpeter@597: gr, l2, u, c, s1, true, 5970, "#B4"); kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::ALTERING_LIST), kpeter@597: gr, l2, u, c, s1, true, 5970, "#B5"); kpeter@593: } kpeter@593: kpeter@593: return 0; kpeter@593: }