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