alpar@357: /* -*- C++ -*- alpar@357: * alpar@357: * This file is a part of LEMON, a generic C++ optimization library alpar@357: * alpar@357: * Copyright (C) 2003-2008 alpar@357: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@357: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@357: * alpar@357: * Permission to use, modify and distribute this software is granted alpar@357: * provided that this copyright notice appears in all copies. For alpar@357: * precise terms see the accompanying LICENSE file. alpar@357: * alpar@357: * This software is provided "AS IS" with no warranty of any kind, alpar@357: * express or implied, and with no claim as to its suitability for any alpar@357: * purpose. alpar@357: * alpar@357: */ alpar@357: alpar@357: #include alpar@357: alpar@357: #include alpar@357: #include alpar@357: #include alpar@357: #include alpar@357: alpar@357: #include "test_tools.h" alpar@357: alpar@357: using namespace lemon; alpar@357: alpar@442: char test_lgf[] = alpar@442: "@nodes\n" alpar@442: "label supply1 supply2 supply3\n" alpar@442: "1 0 20 27\n" alpar@442: "2 0 -4 0\n" alpar@442: "3 0 0 0\n" alpar@442: "4 0 0 0\n" alpar@442: "5 0 9 0\n" alpar@442: "6 0 -6 0\n" alpar@442: "7 0 0 0\n" alpar@442: "8 0 0 0\n" alpar@442: "9 0 3 0\n" alpar@442: "10 0 -2 0\n" alpar@442: "11 0 0 0\n" alpar@442: "12 0 -20 -27\n" alpar@442: "@arcs\n" alpar@442: " cost capacity lower1 lower2\n" alpar@442: " 1 2 70 11 0 8\n" alpar@442: " 1 3 150 3 0 1\n" alpar@442: " 1 4 80 15 0 2\n" alpar@442: " 2 8 80 12 0 0\n" alpar@442: " 3 5 140 5 0 3\n" alpar@442: " 4 6 60 10 0 1\n" alpar@442: " 4 7 80 2 0 0\n" alpar@442: " 4 8 110 3 0 0\n" alpar@442: " 5 7 60 14 0 0\n" alpar@442: " 5 11 120 12 0 0\n" alpar@442: " 6 3 0 3 0 0\n" alpar@442: " 6 9 140 4 0 0\n" alpar@442: " 6 10 90 8 0 0\n" alpar@442: " 7 1 30 5 0 0\n" alpar@442: " 8 12 60 16 0 4\n" alpar@442: " 9 12 50 6 0 0\n" alpar@442: "10 12 70 13 0 5\n" alpar@442: "10 2 100 7 0 0\n" alpar@442: "10 7 60 10 0 0\n" alpar@442: "11 10 20 14 0 6\n" alpar@442: "12 11 30 10 0 0\n" alpar@442: "@attributes\n" alpar@442: "source 1\n" alpar@442: "target 12\n" alpar@442: "@end\n"; alpar@442: kpeter@358: // Check the feasibility of the flow alpar@357: template alpar@357: bool checkFlow( const Digraph& gr, const FlowMap& flow, alpar@357: typename Digraph::Node s, typename Digraph::Node t, alpar@357: int value ) alpar@357: { alpar@357: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); alpar@357: for (ArcIt e(gr); e != INVALID; ++e) alpar@357: if (!(flow[e] == 0 || flow[e] == 1)) return false; alpar@357: alpar@357: for (NodeIt n(gr); n != INVALID; ++n) { alpar@357: int sum = 0; alpar@357: for (OutArcIt e(gr, n); e != INVALID; ++e) alpar@357: sum += flow[e]; alpar@357: for (InArcIt e(gr, n); e != INVALID; ++e) alpar@357: sum -= flow[e]; alpar@357: if (n == s && sum != value) return false; alpar@357: if (n == t && sum != -value) return false; alpar@357: if (n != s && n != t && sum != 0) return false; alpar@357: } alpar@357: alpar@357: return true; alpar@357: } alpar@357: kpeter@358: // Check the optimalitiy of the flow alpar@357: template < typename Digraph, typename CostMap, alpar@357: typename FlowMap, typename PotentialMap > alpar@357: bool checkOptimality( const Digraph& gr, const CostMap& cost, alpar@357: const FlowMap& flow, const PotentialMap& pi ) alpar@357: { kpeter@358: // Check the "Complementary Slackness" optimality condition alpar@357: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); alpar@357: bool opt = true; alpar@357: for (ArcIt e(gr); e != INVALID; ++e) { alpar@357: typename CostMap::Value red_cost = alpar@357: cost[e] + pi[gr.source(e)] - pi[gr.target(e)]; alpar@357: opt = (flow[e] == 0 && red_cost >= 0) || alpar@357: (flow[e] == 1 && red_cost <= 0); alpar@357: if (!opt) break; alpar@357: } alpar@357: return opt; alpar@357: } alpar@357: kpeter@358: // Check a path kpeter@358: template alpar@357: bool checkPath( const Digraph& gr, const Path& path, alpar@357: typename Digraph::Node s, typename Digraph::Node t) alpar@357: { kpeter@358: // Check the "Complementary Slackness" optimality condition alpar@357: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); alpar@357: Node n = s; alpar@357: for (int i = 0; i < path.length(); ++i) { alpar@357: if (gr.source(path.nth(i)) != n) return false; alpar@357: n = gr.target(path.nth(i)); alpar@357: } alpar@357: return n == t; alpar@357: } alpar@357: alpar@357: alpar@357: int main() alpar@357: { alpar@357: DIGRAPH_TYPEDEFS(ListDigraph); alpar@357: kpeter@358: // Read the test digraph alpar@357: ListDigraph digraph; alpar@357: ListDigraph::ArcMap length(digraph); alpar@357: Node source, target; alpar@357: alpar@442: std::istringstream input(test_lgf); alpar@357: DigraphReader(digraph, input). alpar@357: arcMap("cost", length). alpar@357: node("source", source). alpar@357: node("target", target). alpar@357: run(); alpar@357: kpeter@358: // Find 2 paths alpar@357: { alpar@357: Suurballe suurballe(digraph, length, source, target); alpar@357: check(suurballe.run(2) == 2, "Wrong number of paths"); alpar@357: check(checkFlow(digraph, suurballe.flowMap(), source, target, 2), alpar@357: "The flow is not feasible"); alpar@357: check(suurballe.totalLength() == 510, "The flow is not optimal"); alpar@357: check(checkOptimality(digraph, length, suurballe.flowMap(), alpar@357: suurballe.potentialMap()), alpar@357: "Wrong potentials"); alpar@357: for (int i = 0; i < suurballe.pathNum(); ++i) alpar@357: check(checkPath(digraph, suurballe.path(i), source, target), alpar@357: "Wrong path"); alpar@357: } alpar@357: kpeter@358: // Find 3 paths alpar@357: { alpar@357: Suurballe suurballe(digraph, length, source, target); alpar@357: check(suurballe.run(3) == 3, "Wrong number of paths"); alpar@357: check(checkFlow(digraph, suurballe.flowMap(), source, target, 3), alpar@357: "The flow is not feasible"); alpar@357: check(suurballe.totalLength() == 1040, "The flow is not optimal"); alpar@357: check(checkOptimality(digraph, length, suurballe.flowMap(), alpar@357: suurballe.potentialMap()), alpar@357: "Wrong potentials"); alpar@357: for (int i = 0; i < suurballe.pathNum(); ++i) alpar@357: check(checkPath(digraph, suurballe.path(i), source, target), alpar@357: "Wrong path"); alpar@357: } alpar@357: kpeter@358: // Find 5 paths (only 3 can be found) alpar@357: { alpar@357: Suurballe suurballe(digraph, length, source, target); alpar@357: check(suurballe.run(5) == 3, "Wrong number of paths"); alpar@357: check(checkFlow(digraph, suurballe.flowMap(), source, target, 3), alpar@357: "The flow is not feasible"); alpar@357: check(suurballe.totalLength() == 1040, "The flow is not optimal"); alpar@357: check(checkOptimality(digraph, length, suurballe.flowMap(), alpar@357: suurballe.potentialMap()), alpar@357: "Wrong potentials"); alpar@357: for (int i = 0; i < suurballe.pathNum(); ++i) alpar@357: check(checkPath(digraph, suurballe.path(i), source, target), alpar@357: "Wrong path"); alpar@357: } alpar@357: alpar@357: return 0; alpar@357: }