test/min_cost_flow_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Wed, 25 Mar 2009 15:58:44 +0100
changeset 652 5232721b3f14
parent 648 e8349c6f12ca
child 653 c7d160f73d52
permissions -rw-r--r--
Rework the interface of NetworkSimplex (#234)

The parameters of the problem can be set with separate functions
instead of different constructors.
kpeter@648
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@648
     2
 *
kpeter@648
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@648
     4
 *
kpeter@648
     5
 * Copyright (C) 2003-2009
kpeter@648
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@648
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@648
     8
 *
kpeter@648
     9
 * Permission to use, modify and distribute this software is granted
kpeter@648
    10
 * provided that this copyright notice appears in all copies. For
kpeter@648
    11
 * precise terms see the accompanying LICENSE file.
kpeter@648
    12
 *
kpeter@648
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@648
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@648
    15
 * purpose.
kpeter@648
    16
 *
kpeter@648
    17
 */
kpeter@648
    18
kpeter@648
    19
#include <iostream>
kpeter@648
    20
#include <fstream>
kpeter@648
    21
kpeter@648
    22
#include <lemon/list_graph.h>
kpeter@648
    23
#include <lemon/lgf_reader.h>
kpeter@648
    24
kpeter@648
    25
#include <lemon/network_simplex.h>
kpeter@648
    26
kpeter@648
    27
#include <lemon/concepts/digraph.h>
kpeter@648
    28
#include <lemon/concept_check.h>
kpeter@648
    29
kpeter@648
    30
#include "test_tools.h"
kpeter@648
    31
kpeter@648
    32
using namespace lemon;
kpeter@648
    33
kpeter@648
    34
char test_lgf[] =
kpeter@648
    35
  "@nodes\n"
kpeter@648
    36
  "label  sup1 sup2 sup3\n"
kpeter@648
    37
  "    1    20   27    0\n"
kpeter@648
    38
  "    2    -4    0    0\n"
kpeter@648
    39
  "    3     0    0    0\n"
kpeter@648
    40
  "    4     0    0    0\n"
kpeter@648
    41
  "    5     9    0    0\n"
kpeter@648
    42
  "    6    -6    0    0\n"
kpeter@648
    43
  "    7     0    0    0\n"
kpeter@648
    44
  "    8     0    0    0\n"
kpeter@648
    45
  "    9     3    0    0\n"
kpeter@648
    46
  "   10    -2    0    0\n"
kpeter@648
    47
  "   11     0    0    0\n"
kpeter@648
    48
  "   12   -20  -27    0\n"
kpeter@648
    49
  "\n"
kpeter@648
    50
  "@arcs\n"
kpeter@648
    51
  "       cost  cap low1 low2\n"
kpeter@648
    52
  " 1  2    70   11    0    8\n"
kpeter@648
    53
  " 1  3   150    3    0    1\n"
kpeter@648
    54
  " 1  4    80   15    0    2\n"
kpeter@648
    55
  " 2  8    80   12    0    0\n"
kpeter@648
    56
  " 3  5   140    5    0    3\n"
kpeter@648
    57
  " 4  6    60   10    0    1\n"
kpeter@648
    58
  " 4  7    80    2    0    0\n"
kpeter@648
    59
  " 4  8   110    3    0    0\n"
kpeter@648
    60
  " 5  7    60   14    0    0\n"
kpeter@648
    61
  " 5 11   120   12    0    0\n"
kpeter@648
    62
  " 6  3     0    3    0    0\n"
kpeter@648
    63
  " 6  9   140    4    0    0\n"
kpeter@648
    64
  " 6 10    90    8    0    0\n"
kpeter@648
    65
  " 7  1    30    5    0    0\n"
kpeter@648
    66
  " 8 12    60   16    0    4\n"
kpeter@648
    67
  " 9 12    50    6    0    0\n"
kpeter@648
    68
  "10 12    70   13    0    5\n"
kpeter@648
    69
  "10  2   100    7    0    0\n"
kpeter@648
    70
  "10  7    60   10    0    0\n"
kpeter@648
    71
  "11 10    20   14    0    6\n"
kpeter@648
    72
  "12 11    30   10    0    0\n"
kpeter@648
    73
  "\n"
kpeter@648
    74
  "@attributes\n"
kpeter@648
    75
  "source 1\n"
kpeter@648
    76
  "target 12\n";
kpeter@648
    77
kpeter@648
    78
kpeter@648
    79
// Check the interface of an MCF algorithm
kpeter@648
    80
template <typename GR, typename Value>
kpeter@648
    81
class McfClassConcept
kpeter@648
    82
{
kpeter@648
    83
public:
kpeter@648
    84
kpeter@648
    85
  template <typename MCF>
kpeter@648
    86
  struct Constraints {
kpeter@648
    87
    void constraints() {
kpeter@648
    88
      checkConcept<concepts::Digraph, GR>();
kpeter@648
    89
kpeter@652
    90
      MCF mcf(g);
kpeter@648
    91
kpeter@652
    92
      b = mcf.lowerMap(lower)
kpeter@652
    93
             .upperMap(upper)
kpeter@652
    94
             .capacityMap(upper)
kpeter@652
    95
             .boundMaps(lower, upper)
kpeter@652
    96
             .costMap(cost)
kpeter@652
    97
             .supplyMap(sup)
kpeter@652
    98
             .stSupply(n, n, k)
kpeter@652
    99
             .run();
kpeter@648
   100
kpeter@652
   101
      const typename MCF::FlowMap &fm = mcf.flowMap();
kpeter@652
   102
      const typename MCF::PotentialMap &pm = mcf.potentialMap();
kpeter@652
   103
kpeter@652
   104
      v = mcf.totalCost();
kpeter@652
   105
      double x = mcf.template totalCost<double>();
kpeter@652
   106
      v = mcf.flow(a);
kpeter@652
   107
      v = mcf.potential(n);
kpeter@652
   108
      mcf.flowMap(flow);
kpeter@652
   109
      mcf.potentialMap(pot);
kpeter@652
   110
kpeter@648
   111
      ignore_unused_variable_warning(fm);
kpeter@648
   112
      ignore_unused_variable_warning(pm);
kpeter@652
   113
      ignore_unused_variable_warning(x);
kpeter@648
   114
    }
kpeter@648
   115
kpeter@648
   116
    typedef typename GR::Node Node;
kpeter@648
   117
    typedef typename GR::Arc Arc;
kpeter@648
   118
    typedef concepts::ReadMap<Node, Value> NM;
kpeter@648
   119
    typedef concepts::ReadMap<Arc, Value> AM;
kpeter@648
   120
kpeter@648
   121
    const GR &g;
kpeter@648
   122
    const AM &lower;
kpeter@648
   123
    const AM &upper;
kpeter@648
   124
    const AM &cost;
kpeter@648
   125
    const NM &sup;
kpeter@648
   126
    const Node &n;
kpeter@648
   127
    const Arc &a;
kpeter@648
   128
    const Value &k;
kpeter@648
   129
    Value v;
kpeter@652
   130
    bool b;
kpeter@648
   131
kpeter@648
   132
    typename MCF::FlowMap &flow;
kpeter@648
   133
    typename MCF::PotentialMap &pot;
kpeter@648
   134
  };
kpeter@648
   135
kpeter@648
   136
};
kpeter@648
   137
kpeter@648
   138
kpeter@648
   139
// Check the feasibility of the given flow (primal soluiton)
kpeter@648
   140
template < typename GR, typename LM, typename UM,
kpeter@648
   141
           typename SM, typename FM >
kpeter@648
   142
bool checkFlow( const GR& gr, const LM& lower, const UM& upper,
kpeter@648
   143
                const SM& supply, const FM& flow )
kpeter@648
   144
{
kpeter@648
   145
  TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@648
   146
kpeter@648
   147
  for (ArcIt e(gr); e != INVALID; ++e) {
kpeter@648
   148
    if (flow[e] < lower[e] || flow[e] > upper[e]) return false;
kpeter@648
   149
  }
kpeter@648
   150
kpeter@648
   151
  for (NodeIt n(gr); n != INVALID; ++n) {
kpeter@648
   152
    typename SM::Value sum = 0;
kpeter@648
   153
    for (OutArcIt e(gr, n); e != INVALID; ++e)
kpeter@648
   154
      sum += flow[e];
kpeter@648
   155
    for (InArcIt e(gr, n); e != INVALID; ++e)
kpeter@648
   156
      sum -= flow[e];
kpeter@648
   157
    if (sum != supply[n]) return false;
kpeter@648
   158
  }
kpeter@648
   159
kpeter@648
   160
  return true;
kpeter@648
   161
}
kpeter@648
   162
kpeter@648
   163
// Check the feasibility of the given potentials (dual soluiton)
kpeter@652
   164
// using the "Complementary Slackness" optimality condition
kpeter@648
   165
template < typename GR, typename LM, typename UM,
kpeter@648
   166
           typename CM, typename FM, typename PM >
kpeter@648
   167
bool checkPotential( const GR& gr, const LM& lower, const UM& upper,
kpeter@648
   168
                     const CM& cost, const FM& flow, const PM& pi )
kpeter@648
   169
{
kpeter@648
   170
  TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@648
   171
kpeter@648
   172
  bool opt = true;
kpeter@648
   173
  for (ArcIt e(gr); opt && e != INVALID; ++e) {
kpeter@648
   174
    typename CM::Value red_cost =
kpeter@648
   175
      cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
kpeter@648
   176
    opt = red_cost == 0 ||
kpeter@648
   177
          (red_cost > 0 && flow[e] == lower[e]) ||
kpeter@648
   178
          (red_cost < 0 && flow[e] == upper[e]);
kpeter@648
   179
  }
kpeter@648
   180
  return opt;
kpeter@648
   181
}
kpeter@648
   182
kpeter@648
   183
// Run a minimum cost flow algorithm and check the results
kpeter@648
   184
template < typename MCF, typename GR,
kpeter@648
   185
           typename LM, typename UM,
kpeter@648
   186
           typename CM, typename SM >
kpeter@648
   187
void checkMcf( const MCF& mcf, bool mcf_result,
kpeter@648
   188
               const GR& gr, const LM& lower, const UM& upper,
kpeter@648
   189
               const CM& cost, const SM& supply,
kpeter@648
   190
               bool result, typename CM::Value total,
kpeter@648
   191
               const std::string &test_id = "" )
kpeter@648
   192
{
kpeter@648
   193
  check(mcf_result == result, "Wrong result " + test_id);
kpeter@648
   194
  if (result) {
kpeter@648
   195
    check(checkFlow(gr, lower, upper, supply, mcf.flowMap()),
kpeter@648
   196
          "The flow is not feasible " + test_id);
kpeter@648
   197
    check(mcf.totalCost() == total, "The flow is not optimal " + test_id);
kpeter@648
   198
    check(checkPotential(gr, lower, upper, cost, mcf.flowMap(),
kpeter@648
   199
                         mcf.potentialMap()),
kpeter@648
   200
          "Wrong potentials " + test_id);
kpeter@648
   201
  }
kpeter@648
   202
}
kpeter@648
   203
kpeter@648
   204
int main()
kpeter@648
   205
{
kpeter@648
   206
  // Check the interfaces
kpeter@648
   207
  {
kpeter@648
   208
    typedef int Value;
kpeter@652
   209
    // TODO: This typedef should be enabled if the standard maps are
kpeter@652
   210
    // reference maps in the graph concepts (See #190).
kpeter@652
   211
/**/
kpeter@648
   212
    //typedef concepts::Digraph GR;
kpeter@648
   213
    typedef ListDigraph GR;
kpeter@652
   214
/**/
kpeter@648
   215
    checkConcept< McfClassConcept<GR, Value>,
kpeter@652
   216
                  NetworkSimplex<GR, Value> >();
kpeter@648
   217
  }
kpeter@648
   218
kpeter@648
   219
  // Run various MCF tests
kpeter@648
   220
  typedef ListDigraph Digraph;
kpeter@648
   221
  DIGRAPH_TYPEDEFS(ListDigraph);
kpeter@648
   222
kpeter@648
   223
  // Read the test digraph
kpeter@648
   224
  Digraph gr;
kpeter@648
   225
  Digraph::ArcMap<int> c(gr), l1(gr), l2(gr), u(gr);
kpeter@648
   226
  Digraph::NodeMap<int> s1(gr), s2(gr), s3(gr);
kpeter@652
   227
  ConstMap<Arc, int> cc(1), cu(std::numeric_limits<int>::max());
kpeter@648
   228
  Node v, w;
kpeter@648
   229
kpeter@648
   230
  std::istringstream input(test_lgf);
kpeter@648
   231
  DigraphReader<Digraph>(gr, input)
kpeter@648
   232
    .arcMap("cost", c)
kpeter@648
   233
    .arcMap("cap", u)
kpeter@648
   234
    .arcMap("low1", l1)
kpeter@648
   235
    .arcMap("low2", l2)
kpeter@648
   236
    .nodeMap("sup1", s1)
kpeter@648
   237
    .nodeMap("sup2", s2)
kpeter@648
   238
    .nodeMap("sup3", s3)
kpeter@648
   239
    .node("source", v)
kpeter@648
   240
    .node("target", w)
kpeter@648
   241
    .run();
kpeter@648
   242
kpeter@652
   243
  // A. Test NetworkSimplex with the default pivot rule
kpeter@648
   244
  {
kpeter@652
   245
    NetworkSimplex<Digraph> mcf1(gr), mcf2(gr), mcf3(gr), mcf4(gr),
kpeter@652
   246
                            mcf5(gr), mcf6(gr), mcf7(gr), mcf8(gr);
kpeter@648
   247
kpeter@652
   248
    checkMcf(mcf1, mcf1.upperMap(u).costMap(c).supplyMap(s1).run(),
kpeter@652
   249
             gr, l1, u, c, s1, true,  5240, "#A1");
kpeter@652
   250
    checkMcf(mcf2, mcf2.upperMap(u).costMap(c).stSupply(v, w, 27).run(),
kpeter@652
   251
             gr, l1, u, c, s2, true,  7620, "#A2");
kpeter@652
   252
    checkMcf(mcf3, mcf3.boundMaps(l2, u).costMap(c).supplyMap(s1).run(),
kpeter@652
   253
             gr, l2, u, c, s1, true,  5970, "#A3");
kpeter@652
   254
    checkMcf(mcf4, mcf4.boundMaps(l2, u).costMap(c).stSupply(v, w, 27).run(),
kpeter@652
   255
             gr, l2, u, c, s2, true,  8010, "#A4");
kpeter@652
   256
    checkMcf(mcf5, mcf5.supplyMap(s1).run(),
kpeter@652
   257
             gr, l1, cu, cc, s1, true,  74, "#A5");
kpeter@652
   258
    checkMcf(mcf6, mcf6.stSupply(v, w, 27).lowerMap(l2).run(),
kpeter@652
   259
             gr, l2, cu, cc, s2, true,  94, "#A6");
kpeter@652
   260
    checkMcf(mcf7, mcf7.run(),
kpeter@652
   261
             gr, l1, cu, cc, s3, true,   0, "#A7");
kpeter@652
   262
    checkMcf(mcf8, mcf8.boundMaps(l2, u).run(),
kpeter@652
   263
             gr, l2, u, cc, s3, false,   0, "#A8");
kpeter@648
   264
  }
kpeter@648
   265
kpeter@652
   266
  // B. Test NetworkSimplex with each pivot rule
kpeter@648
   267
  {
kpeter@652
   268
    NetworkSimplex<Digraph> mcf1(gr), mcf2(gr), mcf3(gr), mcf4(gr), mcf5(gr);
kpeter@652
   269
    NetworkSimplex<Digraph>::PivotRule pr;
kpeter@648
   270
kpeter@652
   271
    pr = NetworkSimplex<Digraph>::FIRST_ELIGIBLE;
kpeter@652
   272
    checkMcf(mcf1, mcf1.boundMaps(l2, u).costMap(c).supplyMap(s1).run(pr),
kpeter@652
   273
             gr, l2, u, c, s1, true,  5970, "#B1");
kpeter@652
   274
    pr = NetworkSimplex<Digraph>::BEST_ELIGIBLE;
kpeter@652
   275
    checkMcf(mcf2, mcf2.boundMaps(l2, u).costMap(c).supplyMap(s1).run(pr),
kpeter@652
   276
             gr, l2, u, c, s1, true,  5970, "#B2");
kpeter@652
   277
    pr = NetworkSimplex<Digraph>::BLOCK_SEARCH;
kpeter@652
   278
    checkMcf(mcf3, mcf3.boundMaps(l2, u).costMap(c).supplyMap(s1).run(pr),
kpeter@652
   279
             gr, l2, u, c, s1, true,  5970, "#B3");
kpeter@652
   280
    pr = NetworkSimplex<Digraph>::CANDIDATE_LIST;
kpeter@652
   281
    checkMcf(mcf4, mcf4.boundMaps(l2, u).costMap(c).supplyMap(s1).run(pr),
kpeter@652
   282
             gr, l2, u, c, s1, true,  5970, "#B4");
kpeter@652
   283
    pr = NetworkSimplex<Digraph>::ALTERING_LIST;
kpeter@652
   284
    checkMcf(mcf5, mcf5.boundMaps(l2, u).costMap(c).supplyMap(s1).run(pr),
kpeter@652
   285
             gr, l2, u, c, s1, true,  5970, "#B5");
kpeter@648
   286
  }
kpeter@648
   287
kpeter@648
   288
  return 0;
kpeter@648
   289
}