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@636: #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@636: "label sup1 sup2 sup3 sup4 sup5 sup6\n" kpeter@636: " 1 20 27 0 30 20 30\n" kpeter@636: " 2 -4 0 0 0 -8 -3\n" kpeter@636: " 3 0 0 0 0 0 0\n" kpeter@636: " 4 0 0 0 0 0 0\n" kpeter@636: " 5 9 0 0 0 6 11\n" kpeter@636: " 6 -6 0 0 0 -5 -6\n" kpeter@636: " 7 0 0 0 0 0 0\n" kpeter@636: " 8 0 0 0 0 0 3\n" kpeter@636: " 9 3 0 0 0 0 0\n" kpeter@636: " 10 -2 0 0 0 -7 -2\n" kpeter@636: " 11 0 0 0 0 -10 0\n" kpeter@636: " 12 -20 -27 0 -30 -30 -20\n" kpeter@636: "\n" kpeter@593: "@arcs\n" kpeter@636: " cost cap low1 low2 low3\n" kpeter@636: " 1 2 70 11 0 8 8\n" kpeter@636: " 1 3 150 3 0 1 0\n" kpeter@636: " 1 4 80 15 0 2 2\n" kpeter@636: " 2 8 80 12 0 0 0\n" kpeter@636: " 3 5 140 5 0 3 1\n" kpeter@636: " 4 6 60 10 0 1 0\n" kpeter@636: " 4 7 80 2 0 0 0\n" kpeter@636: " 4 8 110 3 0 0 0\n" kpeter@636: " 5 7 60 14 0 0 0\n" kpeter@636: " 5 11 120 12 0 0 0\n" kpeter@636: " 6 3 0 3 0 0 0\n" kpeter@636: " 6 9 140 4 0 0 0\n" kpeter@636: " 6 10 90 8 0 0 0\n" kpeter@636: " 7 1 30 5 0 0 -5\n" kpeter@636: " 8 12 60 16 0 4 3\n" kpeter@636: " 9 12 50 6 0 0 0\n" kpeter@636: "10 12 70 13 0 5 2\n" kpeter@636: "10 2 100 7 0 0 0\n" kpeter@636: "10 7 60 10 0 0 -3\n" kpeter@636: "11 10 20 14 0 6 -20\n" kpeter@636: "12 11 30 10 0 0 -10\n" kpeter@593: "\n" kpeter@593: "@attributes\n" kpeter@593: "source 1\n" kpeter@593: "target 12\n"; kpeter@593: kpeter@593: kpeter@636: enum SupplyType { kpeter@601: EQ, kpeter@601: GEQ, kpeter@601: LEQ kpeter@601: }; kpeter@601: kpeter@593: // Check the interface of an MCF algorithm kpeter@638: 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@638: const MCF& const_mcf = mcf; kpeter@593: kpeter@598: b = mcf.reset() kpeter@598: .lowerMap(lower) kpeter@597: .upperMap(upper) kpeter@597: .costMap(cost) kpeter@597: .supplyMap(sup) kpeter@597: .stSupply(n, n, k) kpeter@597: .run(); kpeter@597: kpeter@636: c = const_mcf.totalCost(); kpeter@638: x = const_mcf.template totalCost(); kpeter@601: v = const_mcf.flow(a); kpeter@636: c = const_mcf.potential(n); kpeter@638: const_mcf.flowMap(fm); kpeter@638: const_mcf.potentialMap(pm); kpeter@593: } kpeter@593: kpeter@593: typedef typename GR::Node Node; kpeter@593: typedef typename GR::Arc Arc; kpeter@638: typedef concepts::ReadMap NM; kpeter@638: typedef concepts::ReadMap VAM; kpeter@599: typedef concepts::ReadMap CAM; kpeter@638: typedef concepts::WriteMap FlowMap; kpeter@638: typedef concepts::WriteMap PotMap; kpeter@593: kpeter@593: const GR &g; kpeter@638: const VAM &lower; kpeter@638: const VAM &upper; kpeter@599: const CAM &cost; kpeter@593: const NM ⊃ kpeter@593: const Node &n; kpeter@593: const Arc &a; kpeter@638: const Value &k; kpeter@638: FlowMap fm; kpeter@638: PotMap pm; kpeter@597: bool b; kpeter@638: double x; kpeter@638: typename MCF::Value v; kpeter@638: typename MCF::Cost c; 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@636: SupplyType 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@662: const PM& pi, SupplyType type ) 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@662: if (type != LEQ) { kpeter@662: opt = (pi[n] <= 0) && (sum == supply[n] || pi[n] == 0); kpeter@662: } else { kpeter@662: opt = (pi[n] >= 0) && (sum == supply[n] || pi[n] == 0); kpeter@662: } kpeter@601: } kpeter@601: kpeter@593: return opt; kpeter@593: } kpeter@593: kpeter@662: // Check whether the dual cost is equal to the primal cost kpeter@662: template < typename GR, typename LM, typename UM, kpeter@662: typename CM, typename SM, typename PM > kpeter@662: bool checkDualCost( const GR& gr, const LM& lower, const UM& upper, kpeter@662: const CM& cost, const SM& supply, const PM& pi, kpeter@662: typename CM::Value total ) kpeter@662: { kpeter@662: TEMPLATE_DIGRAPH_TYPEDEFS(GR); kpeter@662: kpeter@662: typename CM::Value dual_cost = 0; kpeter@662: SM red_supply(gr); kpeter@662: for (NodeIt n(gr); n != INVALID; ++n) { kpeter@662: red_supply[n] = supply[n]; kpeter@662: } kpeter@662: for (ArcIt a(gr); a != INVALID; ++a) { kpeter@662: if (lower[a] != 0) { kpeter@662: dual_cost += lower[a] * cost[a]; kpeter@662: red_supply[gr.source(a)] -= lower[a]; kpeter@662: red_supply[gr.target(a)] += lower[a]; kpeter@662: } kpeter@662: } kpeter@662: kpeter@662: for (NodeIt n(gr); n != INVALID; ++n) { kpeter@662: dual_cost -= red_supply[n] * pi[n]; kpeter@662: } kpeter@662: for (ArcIt a(gr); a != INVALID; ++a) { kpeter@662: typename CM::Value red_cost = kpeter@662: cost[a] + pi[gr.source(a)] - pi[gr.target(a)]; kpeter@662: dual_cost -= (upper[a] - lower[a]) * std::max(-red_cost, 0); kpeter@662: } kpeter@662: kpeter@662: return dual_cost == total; kpeter@662: } kpeter@662: 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@636: typename CM, typename SM, kpeter@636: typename PT > kpeter@636: void checkMcf( const MCF& mcf, PT mcf_result, kpeter@593: const GR& gr, const LM& lower, const UM& upper, kpeter@593: const CM& cost, const SM& supply, kpeter@636: PT result, bool optimal, typename CM::Value total, kpeter@601: const std::string &test_id = "", kpeter@636: SupplyType type = EQ ) kpeter@593: { kpeter@593: check(mcf_result == result, "Wrong result " + test_id); kpeter@636: if (optimal) { kpeter@638: typename GR::template ArcMap flow(gr); kpeter@638: typename GR::template NodeMap pi(gr); kpeter@638: mcf.flowMap(flow); kpeter@638: mcf.potentialMap(pi); kpeter@638: check(checkFlow(gr, lower, upper, supply, flow, type), kpeter@593: "The flow is not feasible " + test_id); kpeter@593: check(mcf.totalCost() == total, "The flow is not optimal " + test_id); kpeter@662: check(checkPotential(gr, lower, upper, cost, supply, flow, pi, type), kpeter@593: "Wrong potentials " + test_id); kpeter@662: check(checkDualCost(gr, lower, upper, cost, supply, pi, total), kpeter@662: "Wrong dual cost " + test_id); kpeter@593: } kpeter@593: } kpeter@593: kpeter@593: int main() kpeter@593: { kpeter@593: // Check the interfaces kpeter@593: { kpeter@607: typedef concepts::Digraph GR; kpeter@638: checkConcept< McfClassConcept, kpeter@638: NetworkSimplex >(); kpeter@638: checkConcept< McfClassConcept, kpeter@638: NetworkSimplex >(); kpeter@638: checkConcept< McfClassConcept, kpeter@638: 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@636: Digraph::ArcMap c(gr), l1(gr), l2(gr), l3(gr), u(gr); kpeter@636: Digraph::NodeMap s1(gr), s2(gr), s3(gr), s4(gr), s5(gr), s6(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@636: .arcMap("low3", l3) 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@636: .nodeMap("sup6", s6) kpeter@593: .node("source", v) kpeter@593: .node("target", w) kpeter@593: .run(); kpeter@636: kpeter@662: // Build test digraphs with negative costs kpeter@662: Digraph neg_gr; kpeter@662: Node n1 = neg_gr.addNode(); kpeter@662: Node n2 = neg_gr.addNode(); kpeter@662: Node n3 = neg_gr.addNode(); kpeter@662: Node n4 = neg_gr.addNode(); kpeter@662: Node n5 = neg_gr.addNode(); kpeter@662: Node n6 = neg_gr.addNode(); kpeter@662: Node n7 = neg_gr.addNode(); kpeter@636: kpeter@662: Arc a1 = neg_gr.addArc(n1, n2); kpeter@662: Arc a2 = neg_gr.addArc(n1, n3); kpeter@662: Arc a3 = neg_gr.addArc(n2, n4); kpeter@662: Arc a4 = neg_gr.addArc(n3, n4); kpeter@662: Arc a5 = neg_gr.addArc(n3, n2); kpeter@662: Arc a6 = neg_gr.addArc(n5, n3); kpeter@662: Arc a7 = neg_gr.addArc(n5, n6); kpeter@662: Arc a8 = neg_gr.addArc(n6, n7); kpeter@662: Arc a9 = neg_gr.addArc(n7, n5); kpeter@636: kpeter@662: Digraph::ArcMap neg_c(neg_gr), neg_l1(neg_gr, 0), neg_l2(neg_gr, 0); kpeter@662: ConstMap neg_u1(std::numeric_limits::max()), neg_u2(5000); kpeter@662: Digraph::NodeMap neg_s(neg_gr, 0); kpeter@636: kpeter@662: neg_l2[a7] = 1000; kpeter@662: neg_l2[a8] = -1000; kpeter@636: kpeter@662: neg_s[n1] = 100; kpeter@662: neg_s[n4] = -100; kpeter@636: kpeter@662: neg_c[a1] = 100; kpeter@662: neg_c[a2] = 30; kpeter@662: neg_c[a3] = 20; kpeter@662: neg_c[a4] = 80; kpeter@662: neg_c[a5] = 50; kpeter@662: neg_c[a6] = 10; kpeter@662: neg_c[a7] = 80; kpeter@662: neg_c[a8] = 30; kpeter@662: neg_c[a9] = -120; kpeter@662: kpeter@662: Digraph negs_gr; kpeter@662: Digraph::NodeMap negs_s(negs_gr); kpeter@662: Digraph::ArcMap negs_c(negs_gr); kpeter@662: ConstMap negs_l(0), negs_u(1000); kpeter@662: n1 = negs_gr.addNode(); kpeter@662: n2 = negs_gr.addNode(); kpeter@662: negs_s[n1] = 100; kpeter@662: negs_s[n2] = -300; kpeter@662: negs_c[negs_gr.addArc(n1, n2)] = -1; kpeter@662: 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@636: gr, l1, u, c, s1, mcf.OPTIMAL, true, 5240, "#A1"); kpeter@598: checkMcf(mcf, mcf.stSupply(v, w, 27).run(), kpeter@636: gr, l1, u, c, s2, mcf.OPTIMAL, true, 7620, "#A2"); kpeter@598: mcf.lowerMap(l2); kpeter@598: checkMcf(mcf, mcf.supplyMap(s1).run(), kpeter@636: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#A3"); kpeter@598: checkMcf(mcf, mcf.stSupply(v, w, 27).run(), kpeter@636: gr, l2, u, c, s2, mcf.OPTIMAL, true, 8010, "#A4"); kpeter@598: mcf.reset(); kpeter@598: checkMcf(mcf, mcf.supplyMap(s1).run(), kpeter@636: gr, l1, cu, cc, s1, mcf.OPTIMAL, true, 74, "#A5"); kpeter@598: checkMcf(mcf, mcf.lowerMap(l2).stSupply(v, w, 27).run(), kpeter@636: gr, l2, cu, cc, s2, mcf.OPTIMAL, true, 94, "#A6"); kpeter@598: mcf.reset(); kpeter@598: checkMcf(mcf, mcf.run(), kpeter@636: gr, l1, cu, cc, s3, mcf.OPTIMAL, true, 0, "#A7"); kpeter@636: checkMcf(mcf, mcf.lowerMap(l2).upperMap(u).run(), kpeter@636: gr, l2, u, cc, s3, mcf.INFEASIBLE, false, 0, "#A8"); kpeter@636: mcf.reset().lowerMap(l3).upperMap(u).costMap(c).supplyMap(s4); kpeter@636: checkMcf(mcf, mcf.run(), kpeter@636: gr, l3, u, c, s4, mcf.OPTIMAL, true, 6360, "#A9"); kpeter@601: kpeter@601: // Check the GEQ form kpeter@636: mcf.reset().upperMap(u).costMap(c).supplyMap(s5); kpeter@601: checkMcf(mcf, mcf.run(), kpeter@636: gr, l1, u, c, s5, mcf.OPTIMAL, true, 3530, "#A10", GEQ); kpeter@636: mcf.supplyType(mcf.GEQ); kpeter@601: checkMcf(mcf, mcf.lowerMap(l2).run(), kpeter@636: gr, l2, u, c, s5, mcf.OPTIMAL, true, 4540, "#A11", GEQ); kpeter@662: mcf.supplyMap(s6); kpeter@601: checkMcf(mcf, mcf.run(), kpeter@636: gr, l2, u, c, s6, mcf.INFEASIBLE, false, 0, "#A12", GEQ); kpeter@601: kpeter@601: // Check the LEQ form kpeter@636: mcf.reset().supplyType(mcf.LEQ); kpeter@636: mcf.upperMap(u).costMap(c).supplyMap(s6); kpeter@601: checkMcf(mcf, mcf.run(), kpeter@636: gr, l1, u, c, s6, mcf.OPTIMAL, true, 5080, "#A13", LEQ); kpeter@601: checkMcf(mcf, mcf.lowerMap(l2).run(), kpeter@636: gr, l2, u, c, s6, mcf.OPTIMAL, true, 5930, "#A14", LEQ); kpeter@662: mcf.supplyMap(s5); kpeter@601: checkMcf(mcf, mcf.run(), kpeter@636: gr, l2, u, c, s5, mcf.INFEASIBLE, false, 0, "#A15", LEQ); kpeter@636: kpeter@636: // Check negative costs kpeter@662: NetworkSimplex neg_mcf(neg_gr); kpeter@662: neg_mcf.lowerMap(neg_l1).costMap(neg_c).supplyMap(neg_s); kpeter@662: checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l1, neg_u1, kpeter@662: neg_c, neg_s, neg_mcf.UNBOUNDED, false, 0, "#A16"); kpeter@662: neg_mcf.upperMap(neg_u2); kpeter@662: checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l1, neg_u2, kpeter@662: neg_c, neg_s, neg_mcf.OPTIMAL, true, -40000, "#A17"); kpeter@662: neg_mcf.reset().lowerMap(neg_l2).costMap(neg_c).supplyMap(neg_s); kpeter@662: checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l2, neg_u1, kpeter@662: neg_c, neg_s, neg_mcf.UNBOUNDED, false, 0, "#A18"); kpeter@662: kpeter@662: NetworkSimplex negs_mcf(negs_gr); kpeter@662: negs_mcf.costMap(negs_c).supplyMap(negs_s); kpeter@662: checkMcf(negs_mcf, negs_mcf.run(), negs_gr, negs_l, negs_u, kpeter@662: negs_c, negs_s, negs_mcf.OPTIMAL, true, -300, "#A19", GEQ); kpeter@593: } kpeter@593: kpeter@597: // B. Test NetworkSimplex with each pivot rule kpeter@593: { kpeter@598: NetworkSimplex mcf(gr); kpeter@636: mcf.supplyMap(s1).costMap(c).upperMap(u).lowerMap(l2); kpeter@593: kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::FIRST_ELIGIBLE), kpeter@636: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B1"); kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::BEST_ELIGIBLE), kpeter@636: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B2"); kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::BLOCK_SEARCH), kpeter@636: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B3"); kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::CANDIDATE_LIST), kpeter@636: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B4"); kpeter@598: checkMcf(mcf, mcf.run(NetworkSimplex::ALTERING_LIST), kpeter@636: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B5"); kpeter@593: } kpeter@593: kpeter@593: return 0; kpeter@593: }