kpeter@601: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@601: * kpeter@601: * This file is a part of LEMON, a generic C++ optimization library. kpeter@601: * kpeter@601: * Copyright (C) 2003-2009 kpeter@601: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@601: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@601: * kpeter@601: * Permission to use, modify and distribute this software is granted kpeter@601: * provided that this copyright notice appears in all copies. For kpeter@601: * precise terms see the accompanying LICENSE file. kpeter@601: * kpeter@601: * This software is provided "AS IS" with no warranty of any kind, kpeter@601: * express or implied, and with no claim as to its suitability for any kpeter@601: * purpose. kpeter@601: * kpeter@601: */ kpeter@601: kpeter@601: #include kpeter@601: #include kpeter@640: #include kpeter@601: kpeter@601: #include kpeter@601: #include kpeter@601: kpeter@601: #include kpeter@601: kpeter@601: #include kpeter@601: #include kpeter@601: kpeter@601: #include "test_tools.h" kpeter@601: kpeter@601: using namespace lemon; kpeter@601: kpeter@601: char test_lgf[] = kpeter@601: "@nodes\n" kpeter@640: "label sup1 sup2 sup3 sup4 sup5 sup6\n" kpeter@640: " 1 20 27 0 30 20 30\n" kpeter@640: " 2 -4 0 0 0 -8 -3\n" kpeter@640: " 3 0 0 0 0 0 0\n" kpeter@640: " 4 0 0 0 0 0 0\n" kpeter@640: " 5 9 0 0 0 6 11\n" kpeter@640: " 6 -6 0 0 0 -5 -6\n" kpeter@640: " 7 0 0 0 0 0 0\n" kpeter@640: " 8 0 0 0 0 0 3\n" kpeter@640: " 9 3 0 0 0 0 0\n" kpeter@640: " 10 -2 0 0 0 -7 -2\n" kpeter@640: " 11 0 0 0 0 -10 0\n" kpeter@640: " 12 -20 -27 0 -30 -30 -20\n" kpeter@640: "\n" kpeter@601: "@arcs\n" kpeter@640: " cost cap low1 low2 low3\n" kpeter@640: " 1 2 70 11 0 8 8\n" kpeter@640: " 1 3 150 3 0 1 0\n" kpeter@640: " 1 4 80 15 0 2 2\n" kpeter@640: " 2 8 80 12 0 0 0\n" kpeter@640: " 3 5 140 5 0 3 1\n" kpeter@640: " 4 6 60 10 0 1 0\n" kpeter@640: " 4 7 80 2 0 0 0\n" kpeter@640: " 4 8 110 3 0 0 0\n" kpeter@640: " 5 7 60 14 0 0 0\n" kpeter@640: " 5 11 120 12 0 0 0\n" kpeter@640: " 6 3 0 3 0 0 0\n" kpeter@640: " 6 9 140 4 0 0 0\n" kpeter@640: " 6 10 90 8 0 0 0\n" kpeter@640: " 7 1 30 5 0 0 -5\n" kpeter@640: " 8 12 60 16 0 4 3\n" kpeter@640: " 9 12 50 6 0 0 0\n" kpeter@640: "10 12 70 13 0 5 2\n" kpeter@640: "10 2 100 7 0 0 0\n" kpeter@640: "10 7 60 10 0 0 -3\n" kpeter@640: "11 10 20 14 0 6 -20\n" kpeter@640: "12 11 30 10 0 0 -10\n" kpeter@601: "\n" kpeter@601: "@attributes\n" kpeter@601: "source 1\n" kpeter@601: "target 12\n"; kpeter@601: kpeter@601: kpeter@640: enum SupplyType { kpeter@609: EQ, kpeter@609: GEQ, kpeter@609: LEQ kpeter@609: }; kpeter@609: kpeter@601: // Check the interface of an MCF algorithm kpeter@642: template kpeter@601: class McfClassConcept kpeter@601: { kpeter@601: public: kpeter@601: kpeter@601: template kpeter@601: struct Constraints { kpeter@601: void constraints() { kpeter@601: checkConcept(); kpeter@669: kpeter@669: const Constraints& me = *this; kpeter@601: kpeter@669: MCF mcf(me.g); kpeter@642: const MCF& const_mcf = mcf; kpeter@601: kpeter@606: b = mcf.reset() kpeter@669: .lowerMap(me.lower) kpeter@669: .upperMap(me.upper) kpeter@669: .costMap(me.cost) kpeter@669: .supplyMap(me.sup) kpeter@669: .stSupply(me.n, me.n, me.k) kpeter@605: .run(); kpeter@605: kpeter@640: c = const_mcf.totalCost(); kpeter@642: x = const_mcf.template totalCost(); kpeter@669: v = const_mcf.flow(me.a); kpeter@669: c = const_mcf.potential(me.n); kpeter@642: const_mcf.flowMap(fm); kpeter@642: const_mcf.potentialMap(pm); kpeter@601: } kpeter@601: kpeter@601: typedef typename GR::Node Node; kpeter@601: typedef typename GR::Arc Arc; kpeter@642: typedef concepts::ReadMap NM; kpeter@642: typedef concepts::ReadMap VAM; kpeter@607: typedef concepts::ReadMap CAM; kpeter@642: typedef concepts::WriteMap FlowMap; kpeter@642: typedef concepts::WriteMap PotMap; kpeter@669: kpeter@669: GR g; kpeter@669: VAM lower; kpeter@669: VAM upper; kpeter@669: CAM cost; kpeter@669: NM sup; kpeter@669: Node n; kpeter@669: Arc a; kpeter@669: Value k; kpeter@601: kpeter@642: FlowMap fm; kpeter@642: PotMap pm; kpeter@605: bool b; kpeter@642: double x; kpeter@642: typename MCF::Value v; kpeter@642: typename MCF::Cost c; kpeter@601: }; kpeter@601: kpeter@601: }; kpeter@601: kpeter@601: kpeter@601: // Check the feasibility of the given flow (primal soluiton) kpeter@601: template < typename GR, typename LM, typename UM, kpeter@601: typename SM, typename FM > kpeter@601: bool checkFlow( const GR& gr, const LM& lower, const UM& upper, kpeter@609: const SM& supply, const FM& flow, kpeter@640: SupplyType type = EQ ) kpeter@601: { kpeter@601: TEMPLATE_DIGRAPH_TYPEDEFS(GR); kpeter@601: kpeter@601: for (ArcIt e(gr); e != INVALID; ++e) { kpeter@601: if (flow[e] < lower[e] || flow[e] > upper[e]) return false; kpeter@601: } kpeter@601: kpeter@601: for (NodeIt n(gr); 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@609: bool b = (type == EQ && sum == supply[n]) || kpeter@609: (type == GEQ && sum >= supply[n]) || kpeter@609: (type == LEQ && sum <= supply[n]); kpeter@609: if (!b) return false; kpeter@601: } kpeter@601: kpeter@601: return true; kpeter@601: } kpeter@601: kpeter@601: // Check the feasibility of the given potentials (dual soluiton) kpeter@605: // using the "Complementary Slackness" optimality condition kpeter@601: template < typename GR, typename LM, typename UM, kpeter@609: typename CM, typename SM, typename FM, typename PM > kpeter@601: bool checkPotential( const GR& gr, const LM& lower, const UM& upper, kpeter@609: const CM& cost, const SM& supply, const FM& flow, kpeter@664: const PM& pi, SupplyType type ) kpeter@601: { kpeter@601: TEMPLATE_DIGRAPH_TYPEDEFS(GR); kpeter@601: kpeter@601: bool opt = true; kpeter@601: for (ArcIt e(gr); opt && e != INVALID; ++e) { kpeter@601: typename CM::Value red_cost = kpeter@601: cost[e] + pi[gr.source(e)] - pi[gr.target(e)]; kpeter@601: opt = red_cost == 0 || kpeter@601: (red_cost > 0 && flow[e] == lower[e]) || kpeter@601: (red_cost < 0 && flow[e] == upper[e]); kpeter@601: } kpeter@609: kpeter@609: for (NodeIt n(gr); opt && n != INVALID; ++n) { kpeter@609: typename SM::Value sum = 0; kpeter@609: for (OutArcIt e(gr, n); e != INVALID; ++e) kpeter@609: sum += flow[e]; kpeter@609: for (InArcIt e(gr, n); e != INVALID; ++e) kpeter@609: sum -= flow[e]; kpeter@664: if (type != LEQ) { kpeter@664: opt = (pi[n] <= 0) && (sum == supply[n] || pi[n] == 0); kpeter@664: } else { kpeter@664: opt = (pi[n] >= 0) && (sum == supply[n] || pi[n] == 0); kpeter@664: } kpeter@609: } kpeter@609: kpeter@601: return opt; kpeter@601: } kpeter@601: kpeter@664: // Check whether the dual cost is equal to the primal cost kpeter@664: template < typename GR, typename LM, typename UM, kpeter@664: typename CM, typename SM, typename PM > kpeter@664: bool checkDualCost( const GR& gr, const LM& lower, const UM& upper, kpeter@664: const CM& cost, const SM& supply, const PM& pi, kpeter@664: typename CM::Value total ) kpeter@664: { kpeter@664: TEMPLATE_DIGRAPH_TYPEDEFS(GR); kpeter@664: kpeter@664: typename CM::Value dual_cost = 0; kpeter@664: SM red_supply(gr); kpeter@664: for (NodeIt n(gr); n != INVALID; ++n) { kpeter@664: red_supply[n] = supply[n]; kpeter@664: } kpeter@664: for (ArcIt a(gr); a != INVALID; ++a) { kpeter@664: if (lower[a] != 0) { kpeter@664: dual_cost += lower[a] * cost[a]; kpeter@664: red_supply[gr.source(a)] -= lower[a]; kpeter@664: red_supply[gr.target(a)] += lower[a]; kpeter@664: } kpeter@664: } kpeter@664: kpeter@664: for (NodeIt n(gr); n != INVALID; ++n) { kpeter@664: dual_cost -= red_supply[n] * pi[n]; kpeter@664: } kpeter@664: for (ArcIt a(gr); a != INVALID; ++a) { kpeter@664: typename CM::Value red_cost = kpeter@664: cost[a] + pi[gr.source(a)] - pi[gr.target(a)]; kpeter@664: dual_cost -= (upper[a] - lower[a]) * std::max(-red_cost, 0); kpeter@664: } kpeter@664: kpeter@664: return dual_cost == total; kpeter@664: } kpeter@664: kpeter@601: // Run a minimum cost flow algorithm and check the results kpeter@601: template < typename MCF, typename GR, kpeter@601: typename LM, typename UM, kpeter@640: typename CM, typename SM, kpeter@640: typename PT > kpeter@640: void checkMcf( const MCF& mcf, PT mcf_result, kpeter@601: const GR& gr, const LM& lower, const UM& upper, kpeter@601: const CM& cost, const SM& supply, kpeter@640: PT result, bool optimal, typename CM::Value total, kpeter@609: const std::string &test_id = "", kpeter@640: SupplyType type = EQ ) kpeter@601: { kpeter@601: check(mcf_result == result, "Wrong result " + test_id); kpeter@640: if (optimal) { kpeter@642: typename GR::template ArcMap flow(gr); kpeter@642: typename GR::template NodeMap pi(gr); kpeter@642: mcf.flowMap(flow); kpeter@642: mcf.potentialMap(pi); kpeter@642: check(checkFlow(gr, lower, upper, supply, flow, type), kpeter@601: "The flow is not feasible " + test_id); kpeter@601: check(mcf.totalCost() == total, "The flow is not optimal " + test_id); kpeter@664: check(checkPotential(gr, lower, upper, cost, supply, flow, pi, type), kpeter@601: "Wrong potentials " + test_id); kpeter@664: check(checkDualCost(gr, lower, upper, cost, supply, pi, total), kpeter@664: "Wrong dual cost " + test_id); kpeter@601: } kpeter@601: } kpeter@601: kpeter@601: int main() kpeter@601: { kpeter@601: // Check the interfaces kpeter@601: { kpeter@615: typedef concepts::Digraph GR; kpeter@642: checkConcept< McfClassConcept, kpeter@642: NetworkSimplex >(); kpeter@642: checkConcept< McfClassConcept, kpeter@642: NetworkSimplex >(); kpeter@642: checkConcept< McfClassConcept, kpeter@642: NetworkSimplex >(); kpeter@601: } kpeter@601: kpeter@601: // Run various MCF tests kpeter@601: typedef ListDigraph Digraph; kpeter@601: DIGRAPH_TYPEDEFS(ListDigraph); kpeter@601: kpeter@601: // Read the test digraph kpeter@601: Digraph gr; kpeter@640: Digraph::ArcMap c(gr), l1(gr), l2(gr), l3(gr), u(gr); kpeter@640: Digraph::NodeMap s1(gr), s2(gr), s3(gr), s4(gr), s5(gr), s6(gr); kpeter@605: ConstMap cc(1), cu(std::numeric_limits::max()); kpeter@601: Node v, w; kpeter@601: kpeter@601: std::istringstream input(test_lgf); kpeter@601: DigraphReader(gr, input) kpeter@601: .arcMap("cost", c) kpeter@601: .arcMap("cap", u) kpeter@601: .arcMap("low1", l1) kpeter@601: .arcMap("low2", l2) kpeter@640: .arcMap("low3", l3) kpeter@601: .nodeMap("sup1", s1) kpeter@601: .nodeMap("sup2", s2) kpeter@601: .nodeMap("sup3", s3) kpeter@609: .nodeMap("sup4", s4) kpeter@609: .nodeMap("sup5", s5) kpeter@640: .nodeMap("sup6", s6) kpeter@601: .node("source", v) kpeter@601: .node("target", w) kpeter@601: .run(); kpeter@640: kpeter@664: // Build test digraphs with negative costs kpeter@664: Digraph neg_gr; kpeter@664: Node n1 = neg_gr.addNode(); kpeter@664: Node n2 = neg_gr.addNode(); kpeter@664: Node n3 = neg_gr.addNode(); kpeter@664: Node n4 = neg_gr.addNode(); kpeter@664: Node n5 = neg_gr.addNode(); kpeter@664: Node n6 = neg_gr.addNode(); kpeter@664: Node n7 = neg_gr.addNode(); kpeter@640: kpeter@664: Arc a1 = neg_gr.addArc(n1, n2); kpeter@664: Arc a2 = neg_gr.addArc(n1, n3); kpeter@664: Arc a3 = neg_gr.addArc(n2, n4); kpeter@664: Arc a4 = neg_gr.addArc(n3, n4); kpeter@664: Arc a5 = neg_gr.addArc(n3, n2); kpeter@664: Arc a6 = neg_gr.addArc(n5, n3); kpeter@664: Arc a7 = neg_gr.addArc(n5, n6); kpeter@664: Arc a8 = neg_gr.addArc(n6, n7); kpeter@664: Arc a9 = neg_gr.addArc(n7, n5); kpeter@640: kpeter@664: Digraph::ArcMap neg_c(neg_gr), neg_l1(neg_gr, 0), neg_l2(neg_gr, 0); kpeter@664: ConstMap neg_u1(std::numeric_limits::max()), neg_u2(5000); kpeter@664: Digraph::NodeMap neg_s(neg_gr, 0); kpeter@640: kpeter@664: neg_l2[a7] = 1000; kpeter@664: neg_l2[a8] = -1000; kpeter@640: kpeter@664: neg_s[n1] = 100; kpeter@664: neg_s[n4] = -100; kpeter@640: kpeter@664: neg_c[a1] = 100; kpeter@664: neg_c[a2] = 30; kpeter@664: neg_c[a3] = 20; kpeter@664: neg_c[a4] = 80; kpeter@664: neg_c[a5] = 50; kpeter@664: neg_c[a6] = 10; kpeter@664: neg_c[a7] = 80; kpeter@664: neg_c[a8] = 30; kpeter@664: neg_c[a9] = -120; kpeter@664: kpeter@664: Digraph negs_gr; kpeter@664: Digraph::NodeMap negs_s(negs_gr); kpeter@664: Digraph::ArcMap negs_c(negs_gr); kpeter@664: ConstMap negs_l(0), negs_u(1000); kpeter@664: n1 = negs_gr.addNode(); kpeter@664: n2 = negs_gr.addNode(); kpeter@664: negs_s[n1] = 100; kpeter@664: negs_s[n2] = -300; kpeter@664: negs_c[negs_gr.addArc(n1, n2)] = -1; kpeter@664: kpeter@601: kpeter@605: // A. Test NetworkSimplex with the default pivot rule kpeter@601: { kpeter@606: NetworkSimplex mcf(gr); kpeter@601: kpeter@609: // Check the equality form kpeter@606: mcf.upperMap(u).costMap(c); kpeter@606: checkMcf(mcf, mcf.supplyMap(s1).run(), kpeter@640: gr, l1, u, c, s1, mcf.OPTIMAL, true, 5240, "#A1"); kpeter@606: checkMcf(mcf, mcf.stSupply(v, w, 27).run(), kpeter@640: gr, l1, u, c, s2, mcf.OPTIMAL, true, 7620, "#A2"); kpeter@606: mcf.lowerMap(l2); kpeter@606: checkMcf(mcf, mcf.supplyMap(s1).run(), kpeter@640: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#A3"); kpeter@606: checkMcf(mcf, mcf.stSupply(v, w, 27).run(), kpeter@640: gr, l2, u, c, s2, mcf.OPTIMAL, true, 8010, "#A4"); kpeter@606: mcf.reset(); kpeter@606: checkMcf(mcf, mcf.supplyMap(s1).run(), kpeter@640: gr, l1, cu, cc, s1, mcf.OPTIMAL, true, 74, "#A5"); kpeter@606: checkMcf(mcf, mcf.lowerMap(l2).stSupply(v, w, 27).run(), kpeter@640: gr, l2, cu, cc, s2, mcf.OPTIMAL, true, 94, "#A6"); kpeter@606: mcf.reset(); kpeter@606: checkMcf(mcf, mcf.run(), kpeter@640: gr, l1, cu, cc, s3, mcf.OPTIMAL, true, 0, "#A7"); kpeter@640: checkMcf(mcf, mcf.lowerMap(l2).upperMap(u).run(), kpeter@640: gr, l2, u, cc, s3, mcf.INFEASIBLE, false, 0, "#A8"); kpeter@640: mcf.reset().lowerMap(l3).upperMap(u).costMap(c).supplyMap(s4); kpeter@640: checkMcf(mcf, mcf.run(), kpeter@640: gr, l3, u, c, s4, mcf.OPTIMAL, true, 6360, "#A9"); kpeter@609: kpeter@609: // Check the GEQ form kpeter@640: mcf.reset().upperMap(u).costMap(c).supplyMap(s5); kpeter@609: checkMcf(mcf, mcf.run(), kpeter@640: gr, l1, u, c, s5, mcf.OPTIMAL, true, 3530, "#A10", GEQ); kpeter@640: mcf.supplyType(mcf.GEQ); kpeter@609: checkMcf(mcf, mcf.lowerMap(l2).run(), kpeter@640: gr, l2, u, c, s5, mcf.OPTIMAL, true, 4540, "#A11", GEQ); kpeter@664: mcf.supplyMap(s6); kpeter@609: checkMcf(mcf, mcf.run(), kpeter@640: gr, l2, u, c, s6, mcf.INFEASIBLE, false, 0, "#A12", GEQ); kpeter@609: kpeter@609: // Check the LEQ form kpeter@640: mcf.reset().supplyType(mcf.LEQ); kpeter@640: mcf.upperMap(u).costMap(c).supplyMap(s6); kpeter@609: checkMcf(mcf, mcf.run(), kpeter@640: gr, l1, u, c, s6, mcf.OPTIMAL, true, 5080, "#A13", LEQ); kpeter@609: checkMcf(mcf, mcf.lowerMap(l2).run(), kpeter@640: gr, l2, u, c, s6, mcf.OPTIMAL, true, 5930, "#A14", LEQ); kpeter@664: mcf.supplyMap(s5); kpeter@609: checkMcf(mcf, mcf.run(), kpeter@640: gr, l2, u, c, s5, mcf.INFEASIBLE, false, 0, "#A15", LEQ); kpeter@640: kpeter@640: // Check negative costs kpeter@664: NetworkSimplex neg_mcf(neg_gr); kpeter@664: neg_mcf.lowerMap(neg_l1).costMap(neg_c).supplyMap(neg_s); kpeter@664: checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l1, neg_u1, kpeter@664: neg_c, neg_s, neg_mcf.UNBOUNDED, false, 0, "#A16"); kpeter@664: neg_mcf.upperMap(neg_u2); kpeter@664: checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l1, neg_u2, kpeter@664: neg_c, neg_s, neg_mcf.OPTIMAL, true, -40000, "#A17"); kpeter@664: neg_mcf.reset().lowerMap(neg_l2).costMap(neg_c).supplyMap(neg_s); kpeter@664: checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l2, neg_u1, kpeter@664: neg_c, neg_s, neg_mcf.UNBOUNDED, false, 0, "#A18"); kpeter@664: kpeter@664: NetworkSimplex negs_mcf(negs_gr); kpeter@664: negs_mcf.costMap(negs_c).supplyMap(negs_s); kpeter@664: checkMcf(negs_mcf, negs_mcf.run(), negs_gr, negs_l, negs_u, kpeter@664: negs_c, negs_s, negs_mcf.OPTIMAL, true, -300, "#A19", GEQ); kpeter@601: } kpeter@601: kpeter@605: // B. Test NetworkSimplex with each pivot rule kpeter@601: { kpeter@606: NetworkSimplex mcf(gr); kpeter@640: mcf.supplyMap(s1).costMap(c).upperMap(u).lowerMap(l2); kpeter@601: kpeter@606: checkMcf(mcf, mcf.run(NetworkSimplex::FIRST_ELIGIBLE), kpeter@640: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B1"); kpeter@606: checkMcf(mcf, mcf.run(NetworkSimplex::BEST_ELIGIBLE), kpeter@640: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B2"); kpeter@606: checkMcf(mcf, mcf.run(NetworkSimplex::BLOCK_SEARCH), kpeter@640: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B3"); kpeter@606: checkMcf(mcf, mcf.run(NetworkSimplex::CANDIDATE_LIST), kpeter@640: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B4"); kpeter@606: checkMcf(mcf, mcf.run(NetworkSimplex::ALTERING_LIST), kpeter@640: gr, l2, u, c, s1, mcf.OPTIMAL, true, 5970, "#B5"); kpeter@601: } kpeter@601: kpeter@601: return 0; kpeter@601: }