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