test/min_cost_flow_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 13 Nov 2009 00:24:39 +0100
changeset 885 d93490b861e9
parent 884 bc75ee2ad082
child 898 75c97c3786d6
permissions -rw-r--r--
Adds tests for the new MCF algorithms (#180)
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@687
    21
#include <limits>
kpeter@648
    22
kpeter@648
    23
#include <lemon/list_graph.h>
kpeter@648
    24
#include <lemon/lgf_reader.h>
kpeter@648
    25
kpeter@648
    26
#include <lemon/network_simplex.h>
kpeter@885
    27
#include <lemon/capacity_scaling.h>
kpeter@885
    28
#include <lemon/cost_scaling.h>
kpeter@885
    29
#include <lemon/cycle_canceling.h>
kpeter@648
    30
kpeter@648
    31
#include <lemon/concepts/digraph.h>
kpeter@885
    32
#include <lemon/concepts/heap.h>
kpeter@648
    33
#include <lemon/concept_check.h>
kpeter@648
    34
kpeter@648
    35
#include "test_tools.h"
kpeter@648
    36
kpeter@648
    37
using namespace lemon;
kpeter@648
    38
kpeter@884
    39
// Test networks
kpeter@648
    40
char test_lgf[] =
kpeter@648
    41
  "@nodes\n"
kpeter@687
    42
  "label  sup1 sup2 sup3 sup4 sup5 sup6\n"
kpeter@687
    43
  "    1    20   27    0   30   20   30\n"
kpeter@687
    44
  "    2    -4    0    0    0   -8   -3\n"
kpeter@687
    45
  "    3     0    0    0    0    0    0\n"
kpeter@687
    46
  "    4     0    0    0    0    0    0\n"
kpeter@687
    47
  "    5     9    0    0    0    6   11\n"
kpeter@687
    48
  "    6    -6    0    0    0   -5   -6\n"
kpeter@687
    49
  "    7     0    0    0    0    0    0\n"
kpeter@687
    50
  "    8     0    0    0    0    0    3\n"
kpeter@687
    51
  "    9     3    0    0    0    0    0\n"
kpeter@687
    52
  "   10    -2    0    0    0   -7   -2\n"
kpeter@687
    53
  "   11     0    0    0    0  -10    0\n"
kpeter@687
    54
  "   12   -20  -27    0  -30  -30  -20\n"
kpeter@687
    55
  "\n"                
kpeter@648
    56
  "@arcs\n"
kpeter@687
    57
  "       cost  cap low1 low2 low3\n"
kpeter@687
    58
  " 1  2    70   11    0    8    8\n"
kpeter@687
    59
  " 1  3   150    3    0    1    0\n"
kpeter@687
    60
  " 1  4    80   15    0    2    2\n"
kpeter@687
    61
  " 2  8    80   12    0    0    0\n"
kpeter@687
    62
  " 3  5   140    5    0    3    1\n"
kpeter@687
    63
  " 4  6    60   10    0    1    0\n"
kpeter@687
    64
  " 4  7    80    2    0    0    0\n"
kpeter@687
    65
  " 4  8   110    3    0    0    0\n"
kpeter@687
    66
  " 5  7    60   14    0    0    0\n"
kpeter@687
    67
  " 5 11   120   12    0    0    0\n"
kpeter@687
    68
  " 6  3     0    3    0    0    0\n"
kpeter@687
    69
  " 6  9   140    4    0    0    0\n"
kpeter@687
    70
  " 6 10    90    8    0    0    0\n"
kpeter@687
    71
  " 7  1    30    5    0    0   -5\n"
kpeter@687
    72
  " 8 12    60   16    0    4    3\n"
kpeter@687
    73
  " 9 12    50    6    0    0    0\n"
kpeter@687
    74
  "10 12    70   13    0    5    2\n"
kpeter@687
    75
  "10  2   100    7    0    0    0\n"
kpeter@687
    76
  "10  7    60   10    0    0   -3\n"
kpeter@687
    77
  "11 10    20   14    0    6  -20\n"
kpeter@687
    78
  "12 11    30   10    0    0  -10\n"
kpeter@648
    79
  "\n"
kpeter@648
    80
  "@attributes\n"
kpeter@648
    81
  "source 1\n"
kpeter@648
    82
  "target 12\n";
kpeter@648
    83
kpeter@884
    84
char test_neg1_lgf[] =
kpeter@884
    85
  "@nodes\n"
kpeter@884
    86
  "label   sup\n"
kpeter@884
    87
  "    1   100\n"
kpeter@884
    88
  "    2     0\n"
kpeter@884
    89
  "    3     0\n"
kpeter@884
    90
  "    4  -100\n"
kpeter@884
    91
  "    5     0\n"
kpeter@884
    92
  "    6     0\n"
kpeter@884
    93
  "    7     0\n"
kpeter@884
    94
  "@arcs\n"
kpeter@884
    95
  "      cost   low1   low2\n"
kpeter@884
    96
  "1 2    100      0      0\n"
kpeter@884
    97
  "1 3     30      0      0\n"
kpeter@884
    98
  "2 4     20      0      0\n"
kpeter@884
    99
  "3 4     80      0      0\n"
kpeter@884
   100
  "3 2     50      0      0\n"
kpeter@884
   101
  "5 3     10      0      0\n"
kpeter@884
   102
  "5 6     80      0   1000\n"
kpeter@884
   103
  "6 7     30      0  -1000\n"
kpeter@884
   104
  "7 5   -120      0      0\n";
kpeter@884
   105
  
kpeter@884
   106
char test_neg2_lgf[] =
kpeter@884
   107
  "@nodes\n"
kpeter@884
   108
  "label   sup\n"
kpeter@884
   109
  "    1   100\n"
kpeter@884
   110
  "    2  -300\n"
kpeter@884
   111
  "@arcs\n"
kpeter@884
   112
  "      cost\n"
kpeter@884
   113
  "1 2     -1\n";
kpeter@884
   114
kpeter@884
   115
kpeter@884
   116
// Test data
kpeter@884
   117
typedef ListDigraph Digraph;
kpeter@884
   118
DIGRAPH_TYPEDEFS(ListDigraph);
kpeter@884
   119
kpeter@884
   120
Digraph gr;
kpeter@884
   121
Digraph::ArcMap<int> c(gr), l1(gr), l2(gr), l3(gr), u(gr);
kpeter@884
   122
Digraph::NodeMap<int> s1(gr), s2(gr), s3(gr), s4(gr), s5(gr), s6(gr);
kpeter@884
   123
ConstMap<Arc, int> cc(1), cu(std::numeric_limits<int>::max());
kpeter@884
   124
Node v, w;
kpeter@884
   125
kpeter@884
   126
Digraph neg1_gr;
kpeter@884
   127
Digraph::ArcMap<int> neg1_c(neg1_gr), neg1_l1(neg1_gr), neg1_l2(neg1_gr);
kpeter@884
   128
ConstMap<Arc, int> neg1_u1(std::numeric_limits<int>::max()), neg1_u2(5000);
kpeter@884
   129
Digraph::NodeMap<int> neg1_s(neg1_gr);
kpeter@884
   130
kpeter@884
   131
Digraph neg2_gr;
kpeter@884
   132
Digraph::ArcMap<int> neg2_c(neg2_gr);
kpeter@884
   133
ConstMap<Arc, int> neg2_l(0), neg2_u(1000);
kpeter@884
   134
Digraph::NodeMap<int> neg2_s(neg2_gr);
kpeter@884
   135
kpeter@648
   136
kpeter@687
   137
enum SupplyType {
kpeter@656
   138
  EQ,
kpeter@656
   139
  GEQ,
kpeter@656
   140
  LEQ
kpeter@656
   141
};
kpeter@656
   142
kpeter@884
   143
kpeter@648
   144
// Check the interface of an MCF algorithm
kpeter@689
   145
template <typename GR, typename Value, typename Cost>
kpeter@648
   146
class McfClassConcept
kpeter@648
   147
{
kpeter@648
   148
public:
kpeter@648
   149
kpeter@648
   150
  template <typename MCF>
kpeter@648
   151
  struct Constraints {
kpeter@648
   152
    void constraints() {
kpeter@648
   153
      checkConcept<concepts::Digraph, GR>();
kpeter@716
   154
      
kpeter@716
   155
      const Constraints& me = *this;
kpeter@648
   156
kpeter@716
   157
      MCF mcf(me.g);
kpeter@689
   158
      const MCF& const_mcf = mcf;
kpeter@648
   159
kpeter@653
   160
      b = mcf.reset()
kpeter@716
   161
             .lowerMap(me.lower)
kpeter@716
   162
             .upperMap(me.upper)
kpeter@716
   163
             .costMap(me.cost)
kpeter@716
   164
             .supplyMap(me.sup)
kpeter@716
   165
             .stSupply(me.n, me.n, me.k)
kpeter@652
   166
             .run();
kpeter@652
   167
kpeter@687
   168
      c = const_mcf.totalCost();
kpeter@689
   169
      x = const_mcf.template totalCost<double>();
kpeter@716
   170
      v = const_mcf.flow(me.a);
kpeter@716
   171
      c = const_mcf.potential(me.n);
kpeter@689
   172
      const_mcf.flowMap(fm);
kpeter@689
   173
      const_mcf.potentialMap(pm);
kpeter@648
   174
    }
kpeter@648
   175
kpeter@648
   176
    typedef typename GR::Node Node;
kpeter@648
   177
    typedef typename GR::Arc Arc;
kpeter@689
   178
    typedef concepts::ReadMap<Node, Value> NM;
kpeter@689
   179
    typedef concepts::ReadMap<Arc, Value> VAM;
kpeter@654
   180
    typedef concepts::ReadMap<Arc, Cost> CAM;
kpeter@689
   181
    typedef concepts::WriteMap<Arc, Value> FlowMap;
kpeter@689
   182
    typedef concepts::WriteMap<Node, Cost> PotMap;
kpeter@716
   183
  
kpeter@716
   184
    GR g;
kpeter@716
   185
    VAM lower;
kpeter@716
   186
    VAM upper;
kpeter@716
   187
    CAM cost;
kpeter@716
   188
    NM sup;
kpeter@716
   189
    Node n;
kpeter@716
   190
    Arc a;
kpeter@716
   191
    Value k;
kpeter@648
   192
kpeter@689
   193
    FlowMap fm;
kpeter@689
   194
    PotMap pm;
kpeter@652
   195
    bool b;
kpeter@689
   196
    double x;
kpeter@689
   197
    typename MCF::Value v;
kpeter@689
   198
    typename MCF::Cost c;
kpeter@648
   199
  };
kpeter@648
   200
kpeter@648
   201
};
kpeter@648
   202
kpeter@648
   203
kpeter@648
   204
// Check the feasibility of the given flow (primal soluiton)
kpeter@648
   205
template < typename GR, typename LM, typename UM,
kpeter@648
   206
           typename SM, typename FM >
kpeter@648
   207
bool checkFlow( const GR& gr, const LM& lower, const UM& upper,
kpeter@656
   208
                const SM& supply, const FM& flow,
kpeter@687
   209
                SupplyType type = EQ )
kpeter@648
   210
{
kpeter@648
   211
  TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@648
   212
kpeter@648
   213
  for (ArcIt e(gr); e != INVALID; ++e) {
kpeter@648
   214
    if (flow[e] < lower[e] || flow[e] > upper[e]) return false;
kpeter@648
   215
  }
kpeter@648
   216
kpeter@648
   217
  for (NodeIt n(gr); n != INVALID; ++n) {
kpeter@648
   218
    typename SM::Value sum = 0;
kpeter@648
   219
    for (OutArcIt e(gr, n); e != INVALID; ++e)
kpeter@648
   220
      sum += flow[e];
kpeter@648
   221
    for (InArcIt e(gr, n); e != INVALID; ++e)
kpeter@648
   222
      sum -= flow[e];
kpeter@656
   223
    bool b = (type ==  EQ && sum == supply[n]) ||
kpeter@656
   224
             (type == GEQ && sum >= supply[n]) ||
kpeter@656
   225
             (type == LEQ && sum <= supply[n]);
kpeter@656
   226
    if (!b) return false;
kpeter@648
   227
  }
kpeter@648
   228
kpeter@648
   229
  return true;
kpeter@648
   230
}
kpeter@648
   231
kpeter@648
   232
// Check the feasibility of the given potentials (dual soluiton)
kpeter@652
   233
// using the "Complementary Slackness" optimality condition
kpeter@648
   234
template < typename GR, typename LM, typename UM,
kpeter@656
   235
           typename CM, typename SM, typename FM, typename PM >
kpeter@648
   236
bool checkPotential( const GR& gr, const LM& lower, const UM& upper,
kpeter@656
   237
                     const CM& cost, const SM& supply, const FM& flow, 
kpeter@711
   238
                     const PM& pi, SupplyType type )
kpeter@648
   239
{
kpeter@648
   240
  TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@648
   241
kpeter@648
   242
  bool opt = true;
kpeter@648
   243
  for (ArcIt e(gr); opt && e != INVALID; ++e) {
kpeter@648
   244
    typename CM::Value red_cost =
kpeter@648
   245
      cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
kpeter@648
   246
    opt = red_cost == 0 ||
kpeter@648
   247
          (red_cost > 0 && flow[e] == lower[e]) ||
kpeter@648
   248
          (red_cost < 0 && flow[e] == upper[e]);
kpeter@648
   249
  }
kpeter@656
   250
  
kpeter@656
   251
  for (NodeIt n(gr); opt && n != INVALID; ++n) {
kpeter@656
   252
    typename SM::Value sum = 0;
kpeter@656
   253
    for (OutArcIt e(gr, n); e != INVALID; ++e)
kpeter@656
   254
      sum += flow[e];
kpeter@656
   255
    for (InArcIt e(gr, n); e != INVALID; ++e)
kpeter@656
   256
      sum -= flow[e];
kpeter@711
   257
    if (type != LEQ) {
kpeter@711
   258
      opt = (pi[n] <= 0) && (sum == supply[n] || pi[n] == 0);
kpeter@711
   259
    } else {
kpeter@711
   260
      opt = (pi[n] >= 0) && (sum == supply[n] || pi[n] == 0);
kpeter@711
   261
    }
kpeter@656
   262
  }
kpeter@656
   263
  
kpeter@648
   264
  return opt;
kpeter@648
   265
}
kpeter@648
   266
kpeter@711
   267
// Check whether the dual cost is equal to the primal cost
kpeter@711
   268
template < typename GR, typename LM, typename UM,
kpeter@711
   269
           typename CM, typename SM, typename PM >
kpeter@711
   270
bool checkDualCost( const GR& gr, const LM& lower, const UM& upper,
kpeter@711
   271
                    const CM& cost, const SM& supply, const PM& pi,
kpeter@711
   272
                    typename CM::Value total )
kpeter@711
   273
{
kpeter@711
   274
  TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@711
   275
kpeter@711
   276
  typename CM::Value dual_cost = 0;
kpeter@711
   277
  SM red_supply(gr);
kpeter@711
   278
  for (NodeIt n(gr); n != INVALID; ++n) {
kpeter@711
   279
    red_supply[n] = supply[n];
kpeter@711
   280
  }
kpeter@711
   281
  for (ArcIt a(gr); a != INVALID; ++a) {
kpeter@711
   282
    if (lower[a] != 0) {
kpeter@711
   283
      dual_cost += lower[a] * cost[a];
kpeter@711
   284
      red_supply[gr.source(a)] -= lower[a];
kpeter@711
   285
      red_supply[gr.target(a)] += lower[a];
kpeter@711
   286
    }
kpeter@711
   287
  }
kpeter@711
   288
  
kpeter@711
   289
  for (NodeIt n(gr); n != INVALID; ++n) {
kpeter@711
   290
    dual_cost -= red_supply[n] * pi[n];
kpeter@711
   291
  }
kpeter@711
   292
  for (ArcIt a(gr); a != INVALID; ++a) {
kpeter@711
   293
    typename CM::Value red_cost =
kpeter@711
   294
      cost[a] + pi[gr.source(a)] - pi[gr.target(a)];
kpeter@711
   295
    dual_cost -= (upper[a] - lower[a]) * std::max(-red_cost, 0);
kpeter@711
   296
  }
kpeter@711
   297
  
kpeter@711
   298
  return dual_cost == total;
kpeter@711
   299
}
kpeter@711
   300
kpeter@648
   301
// Run a minimum cost flow algorithm and check the results
kpeter@648
   302
template < typename MCF, typename GR,
kpeter@648
   303
           typename LM, typename UM,
kpeter@687
   304
           typename CM, typename SM,
kpeter@687
   305
           typename PT >
kpeter@687
   306
void checkMcf( const MCF& mcf, PT mcf_result,
kpeter@648
   307
               const GR& gr, const LM& lower, const UM& upper,
kpeter@648
   308
               const CM& cost, const SM& supply,
kpeter@687
   309
               PT result, bool optimal, typename CM::Value total,
kpeter@656
   310
               const std::string &test_id = "",
kpeter@687
   311
               SupplyType type = EQ )
kpeter@648
   312
{
kpeter@648
   313
  check(mcf_result == result, "Wrong result " + test_id);
kpeter@687
   314
  if (optimal) {
kpeter@689
   315
    typename GR::template ArcMap<typename SM::Value> flow(gr);
kpeter@689
   316
    typename GR::template NodeMap<typename CM::Value> pi(gr);
kpeter@689
   317
    mcf.flowMap(flow);
kpeter@689
   318
    mcf.potentialMap(pi);
kpeter@689
   319
    check(checkFlow(gr, lower, upper, supply, flow, type),
kpeter@648
   320
          "The flow is not feasible " + test_id);
kpeter@648
   321
    check(mcf.totalCost() == total, "The flow is not optimal " + test_id);
kpeter@711
   322
    check(checkPotential(gr, lower, upper, cost, supply, flow, pi, type),
kpeter@648
   323
          "Wrong potentials " + test_id);
kpeter@711
   324
    check(checkDualCost(gr, lower, upper, cost, supply, pi, total),
kpeter@711
   325
          "Wrong dual cost " + test_id);
kpeter@648
   326
  }
kpeter@648
   327
}
kpeter@648
   328
kpeter@884
   329
template < typename MCF, typename Param >
kpeter@884
   330
void runMcfGeqTests( Param param,
kpeter@884
   331
                     const std::string &test_str = "",
kpeter@884
   332
                     bool full_neg_cost_support = false )
kpeter@884
   333
{
kpeter@884
   334
  MCF mcf1(gr), mcf2(neg1_gr), mcf3(neg2_gr);
kpeter@884
   335
  
kpeter@884
   336
  // Basic tests
kpeter@884
   337
  mcf1.upperMap(u).costMap(c).supplyMap(s1);
kpeter@884
   338
  checkMcf(mcf1, mcf1.run(param), gr, l1, u, c, s1,
kpeter@884
   339
           mcf1.OPTIMAL, true,     5240, test_str + "-1");
kpeter@884
   340
  mcf1.stSupply(v, w, 27);
kpeter@884
   341
  checkMcf(mcf1, mcf1.run(param), gr, l1, u, c, s2,
kpeter@884
   342
           mcf1.OPTIMAL, true,     7620, test_str + "-2");
kpeter@884
   343
  mcf1.lowerMap(l2).supplyMap(s1);
kpeter@884
   344
  checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s1,
kpeter@884
   345
           mcf1.OPTIMAL, true,     5970, test_str + "-3");
kpeter@884
   346
  mcf1.stSupply(v, w, 27);
kpeter@884
   347
  checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s2,
kpeter@884
   348
           mcf1.OPTIMAL, true,     8010, test_str + "-4");
kpeter@884
   349
  mcf1.reset().supplyMap(s1);
kpeter@884
   350
  checkMcf(mcf1, mcf1.run(param), gr, l1, cu, cc, s1,
kpeter@884
   351
           mcf1.OPTIMAL, true,       74, test_str + "-5");
kpeter@884
   352
  mcf1.lowerMap(l2).stSupply(v, w, 27);
kpeter@884
   353
  checkMcf(mcf1, mcf1.run(param), gr, l2, cu, cc, s2,
kpeter@884
   354
           mcf1.OPTIMAL, true,       94, test_str + "-6");
kpeter@884
   355
  mcf1.reset();
kpeter@884
   356
  checkMcf(mcf1, mcf1.run(param), gr, l1, cu, cc, s3,
kpeter@884
   357
           mcf1.OPTIMAL, true,        0, test_str + "-7");
kpeter@884
   358
  mcf1.lowerMap(l2).upperMap(u);
kpeter@884
   359
  checkMcf(mcf1, mcf1.run(param), gr, l2, u, cc, s3,
kpeter@884
   360
           mcf1.INFEASIBLE, false,    0, test_str + "-8");
kpeter@884
   361
  mcf1.lowerMap(l3).upperMap(u).costMap(c).supplyMap(s4);
kpeter@884
   362
  checkMcf(mcf1, mcf1.run(param), gr, l3, u, c, s4,
kpeter@884
   363
           mcf1.OPTIMAL, true,     6360, test_str + "-9");
kpeter@884
   364
kpeter@884
   365
  // Tests for the GEQ form
kpeter@884
   366
  mcf1.reset().upperMap(u).costMap(c).supplyMap(s5);
kpeter@884
   367
  checkMcf(mcf1, mcf1.run(param), gr, l1, u, c, s5,
kpeter@884
   368
           mcf1.OPTIMAL, true,     3530, test_str + "-10", GEQ);
kpeter@884
   369
  mcf1.lowerMap(l2);
kpeter@884
   370
  checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s5,
kpeter@884
   371
           mcf1.OPTIMAL, true,     4540, test_str + "-11", GEQ);
kpeter@884
   372
  mcf1.supplyMap(s6);
kpeter@884
   373
  checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s6,
kpeter@884
   374
           mcf1.INFEASIBLE, false,    0, test_str + "-12", GEQ);
kpeter@884
   375
kpeter@884
   376
  // Tests with negative costs
kpeter@884
   377
  mcf2.lowerMap(neg1_l1).costMap(neg1_c).supplyMap(neg1_s);
kpeter@884
   378
  checkMcf(mcf2, mcf2.run(param), neg1_gr, neg1_l1, neg1_u1, neg1_c, neg1_s,
kpeter@884
   379
           mcf2.UNBOUNDED, false,     0, test_str + "-13");
kpeter@884
   380
  mcf2.upperMap(neg1_u2);
kpeter@884
   381
  checkMcf(mcf2, mcf2.run(param), neg1_gr, neg1_l1, neg1_u2, neg1_c, neg1_s,
kpeter@884
   382
           mcf2.OPTIMAL, true,   -40000, test_str + "-14");
kpeter@884
   383
  mcf2.reset().lowerMap(neg1_l2).costMap(neg1_c).supplyMap(neg1_s);
kpeter@884
   384
  checkMcf(mcf2, mcf2.run(param), neg1_gr, neg1_l2, neg1_u1, neg1_c, neg1_s,
kpeter@884
   385
           mcf2.UNBOUNDED, false,     0, test_str + "-15");
kpeter@884
   386
kpeter@884
   387
  mcf3.costMap(neg2_c).supplyMap(neg2_s);
kpeter@884
   388
  if (full_neg_cost_support) {
kpeter@884
   389
    checkMcf(mcf3, mcf3.run(param), neg2_gr, neg2_l, neg2_u, neg2_c, neg2_s,
kpeter@884
   390
             mcf3.OPTIMAL, true,   -300, test_str + "-16", GEQ);
kpeter@884
   391
  } else {
kpeter@884
   392
    checkMcf(mcf3, mcf3.run(param), neg2_gr, neg2_l, neg2_u, neg2_c, neg2_s,
kpeter@884
   393
             mcf3.UNBOUNDED, false,   0, test_str + "-17", GEQ);
kpeter@884
   394
  }
kpeter@884
   395
  mcf3.upperMap(neg2_u);
kpeter@884
   396
  checkMcf(mcf3, mcf3.run(param), neg2_gr, neg2_l, neg2_u, neg2_c, neg2_s,
kpeter@884
   397
           mcf3.OPTIMAL, true,     -300, test_str + "-18", GEQ);
kpeter@884
   398
}
kpeter@884
   399
kpeter@884
   400
template < typename MCF, typename Param >
kpeter@884
   401
void runMcfLeqTests( Param param,
kpeter@884
   402
                     const std::string &test_str = "" )
kpeter@884
   403
{
kpeter@884
   404
  // Tests for the LEQ form
kpeter@884
   405
  MCF mcf1(gr);
kpeter@884
   406
  mcf1.supplyType(mcf1.LEQ);
kpeter@884
   407
  mcf1.upperMap(u).costMap(c).supplyMap(s6);
kpeter@884
   408
  checkMcf(mcf1, mcf1.run(param), gr, l1, u, c, s6,
kpeter@884
   409
           mcf1.OPTIMAL, true,   5080, test_str + "-19", LEQ);
kpeter@884
   410
  mcf1.lowerMap(l2);
kpeter@884
   411
  checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s6,
kpeter@884
   412
           mcf1.OPTIMAL, true,   5930, test_str + "-20", LEQ);
kpeter@884
   413
  mcf1.supplyMap(s5);
kpeter@884
   414
  checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s5,
kpeter@884
   415
           mcf1.INFEASIBLE, false,  0, test_str + "-21", LEQ);
kpeter@884
   416
}
kpeter@884
   417
kpeter@884
   418
kpeter@648
   419
int main()
kpeter@648
   420
{
kpeter@884
   421
  // Read the test networks
kpeter@648
   422
  std::istringstream input(test_lgf);
kpeter@648
   423
  DigraphReader<Digraph>(gr, input)
kpeter@648
   424
    .arcMap("cost", c)
kpeter@648
   425
    .arcMap("cap", u)
kpeter@648
   426
    .arcMap("low1", l1)
kpeter@648
   427
    .arcMap("low2", l2)
kpeter@687
   428
    .arcMap("low3", l3)
kpeter@648
   429
    .nodeMap("sup1", s1)
kpeter@648
   430
    .nodeMap("sup2", s2)
kpeter@648
   431
    .nodeMap("sup3", s3)
kpeter@656
   432
    .nodeMap("sup4", s4)
kpeter@656
   433
    .nodeMap("sup5", s5)
kpeter@687
   434
    .nodeMap("sup6", s6)
kpeter@648
   435
    .node("source", v)
kpeter@648
   436
    .node("target", w)
kpeter@648
   437
    .run();
kpeter@687
   438
  
kpeter@884
   439
  std::istringstream neg_inp1(test_neg1_lgf);
kpeter@884
   440
  DigraphReader<Digraph>(neg1_gr, neg_inp1)
kpeter@884
   441
    .arcMap("cost", neg1_c)
kpeter@884
   442
    .arcMap("low1", neg1_l1)
kpeter@884
   443
    .arcMap("low2", neg1_l2)
kpeter@884
   444
    .nodeMap("sup", neg1_s)
kpeter@884
   445
    .run();
kpeter@687
   446
  
kpeter@884
   447
  std::istringstream neg_inp2(test_neg2_lgf);
kpeter@884
   448
  DigraphReader<Digraph>(neg2_gr, neg_inp2)
kpeter@884
   449
    .arcMap("cost", neg2_c)
kpeter@884
   450
    .nodeMap("sup", neg2_s)
kpeter@884
   451
    .run();
kpeter@687
   452
  
kpeter@884
   453
  // Check the interface of NetworkSimplex
kpeter@648
   454
  {
kpeter@884
   455
    typedef concepts::Digraph GR;
kpeter@884
   456
    checkConcept< McfClassConcept<GR, int, int>,
kpeter@884
   457
                  NetworkSimplex<GR> >();
kpeter@884
   458
    checkConcept< McfClassConcept<GR, double, double>,
kpeter@884
   459
                  NetworkSimplex<GR, double> >();
kpeter@884
   460
    checkConcept< McfClassConcept<GR, int, double>,
kpeter@884
   461
                  NetworkSimplex<GR, int, double> >();
kpeter@648
   462
  }
kpeter@648
   463
kpeter@885
   464
  // Check the interface of CapacityScaling
kpeter@885
   465
  {
kpeter@885
   466
    typedef concepts::Digraph GR;
kpeter@885
   467
    checkConcept< McfClassConcept<GR, int, int>,
kpeter@885
   468
                  CapacityScaling<GR> >();
kpeter@885
   469
    checkConcept< McfClassConcept<GR, double, double>,
kpeter@885
   470
                  CapacityScaling<GR, double> >();
kpeter@885
   471
    checkConcept< McfClassConcept<GR, int, double>,
kpeter@885
   472
                  CapacityScaling<GR, int, double> >();
kpeter@885
   473
    typedef CapacityScaling<GR>::
kpeter@885
   474
      SetHeap<concepts::Heap<int, RangeMap<int> > >::Create CAS;
kpeter@885
   475
    checkConcept< McfClassConcept<GR, int, int>, CAS >();
kpeter@885
   476
  }
kpeter@885
   477
kpeter@885
   478
  // Check the interface of CostScaling
kpeter@885
   479
  {
kpeter@885
   480
    typedef concepts::Digraph GR;
kpeter@885
   481
    checkConcept< McfClassConcept<GR, int, int>,
kpeter@885
   482
                  CostScaling<GR> >();
kpeter@885
   483
    checkConcept< McfClassConcept<GR, double, double>,
kpeter@885
   484
                  CostScaling<GR, double> >();
kpeter@885
   485
    checkConcept< McfClassConcept<GR, int, double>,
kpeter@885
   486
                  CostScaling<GR, int, double> >();
kpeter@885
   487
    typedef CostScaling<GR>::
kpeter@885
   488
      SetLargeCost<double>::Create COS;
kpeter@885
   489
    checkConcept< McfClassConcept<GR, int, int>, COS >();
kpeter@885
   490
  }
kpeter@885
   491
kpeter@885
   492
  // Check the interface of CycleCanceling
kpeter@885
   493
  {
kpeter@885
   494
    typedef concepts::Digraph GR;
kpeter@885
   495
    checkConcept< McfClassConcept<GR, int, int>,
kpeter@885
   496
                  CycleCanceling<GR> >();
kpeter@885
   497
    checkConcept< McfClassConcept<GR, double, double>,
kpeter@885
   498
                  CycleCanceling<GR, double> >();
kpeter@885
   499
    checkConcept< McfClassConcept<GR, int, double>,
kpeter@885
   500
                  CycleCanceling<GR, int, double> >();
kpeter@885
   501
  }
kpeter@885
   502
kpeter@884
   503
  // Test NetworkSimplex
kpeter@884
   504
  { 
kpeter@884
   505
    typedef NetworkSimplex<Digraph> MCF;
kpeter@884
   506
    runMcfGeqTests<MCF>(MCF::FIRST_ELIGIBLE, "NS-FE", true);
kpeter@884
   507
    runMcfLeqTests<MCF>(MCF::FIRST_ELIGIBLE, "NS-FE");
kpeter@884
   508
    runMcfGeqTests<MCF>(MCF::BEST_ELIGIBLE,  "NS-BE", true);
kpeter@884
   509
    runMcfLeqTests<MCF>(MCF::BEST_ELIGIBLE,  "NS-BE");
kpeter@884
   510
    runMcfGeqTests<MCF>(MCF::BLOCK_SEARCH,   "NS-BS", true);
kpeter@884
   511
    runMcfLeqTests<MCF>(MCF::BLOCK_SEARCH,   "NS-BS");
kpeter@884
   512
    runMcfGeqTests<MCF>(MCF::CANDIDATE_LIST, "NS-CL", true);
kpeter@884
   513
    runMcfLeqTests<MCF>(MCF::CANDIDATE_LIST, "NS-CL");
kpeter@884
   514
    runMcfGeqTests<MCF>(MCF::ALTERING_LIST,  "NS-AL", true);
kpeter@884
   515
    runMcfLeqTests<MCF>(MCF::ALTERING_LIST,  "NS-AL");
kpeter@648
   516
  }
kpeter@885
   517
  
kpeter@885
   518
  // Test CapacityScaling
kpeter@885
   519
  {
kpeter@885
   520
    typedef CapacityScaling<Digraph> MCF;
kpeter@885
   521
    runMcfGeqTests<MCF>(0, "SSP");
kpeter@885
   522
    runMcfGeqTests<MCF>(2, "CAS");
kpeter@885
   523
  }
kpeter@885
   524
kpeter@885
   525
  // Test CostScaling
kpeter@885
   526
  {
kpeter@885
   527
    typedef CostScaling<Digraph> MCF;
kpeter@885
   528
    runMcfGeqTests<MCF>(MCF::PUSH, "COS-PR");
kpeter@885
   529
    runMcfGeqTests<MCF>(MCF::AUGMENT, "COS-AR");
kpeter@885
   530
    runMcfGeqTests<MCF>(MCF::PARTIAL_AUGMENT, "COS-PAR");
kpeter@885
   531
  }
kpeter@885
   532
kpeter@885
   533
  // Test CycleCanceling
kpeter@885
   534
  {
kpeter@885
   535
    typedef CycleCanceling<Digraph> MCF;
kpeter@885
   536
    runMcfGeqTests<MCF>(MCF::SIMPLE_CYCLE_CANCELING, "SCC");
kpeter@885
   537
    runMcfGeqTests<MCF>(MCF::MINIMUM_MEAN_CYCLE_CANCELING, "MMCC");
kpeter@885
   538
    runMcfGeqTests<MCF>(MCF::CANCEL_AND_TIGHTEN, "CAT");
kpeter@885
   539
  }
kpeter@648
   540
kpeter@648
   541
  return 0;
kpeter@648
   542
}