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