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: #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: 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@357: std::string fname; alpar@357: if(getenv("srcdir")) alpar@357: fname = std::string(getenv("srcdir")); alpar@357: else fname = "."; alpar@357: fname += "/test/min_cost_flow_test.lgf"; alpar@357: alpar@357: std::ifstream input(fname.c_str()); alpar@357: check(input, "Input file '" << fname << "' not found"); 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: input.close(); 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: }