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