test/min_mean_cycle_test.cc
changeset 1095 ad40f7d32846
parent 864 d3ea191c3412
child 1012 21a9f829ab68
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/min_mean_cycle_test.cc	Sun Aug 11 15:28:12 2013 +0200
     1.3 @@ -0,0 +1,216 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2010
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <iostream>
    1.23 +#include <sstream>
    1.24 +
    1.25 +#include <lemon/smart_graph.h>
    1.26 +#include <lemon/lgf_reader.h>
    1.27 +#include <lemon/path.h>
    1.28 +#include <lemon/concepts/digraph.h>
    1.29 +#include <lemon/concept_check.h>
    1.30 +
    1.31 +#include <lemon/karp_mmc.h>
    1.32 +#include <lemon/hartmann_orlin_mmc.h>
    1.33 +#include <lemon/howard_mmc.h>
    1.34 +
    1.35 +#include "test_tools.h"
    1.36 +
    1.37 +using namespace lemon;
    1.38 +
    1.39 +char test_lgf[] =
    1.40 +  "@nodes\n"
    1.41 +  "label\n"
    1.42 +  "1\n"
    1.43 +  "2\n"
    1.44 +  "3\n"
    1.45 +  "4\n"
    1.46 +  "5\n"
    1.47 +  "6\n"
    1.48 +  "7\n"
    1.49 +  "@arcs\n"
    1.50 +  "    len1 len2 len3 len4  c1 c2 c3 c4\n"
    1.51 +  "1 2    1    1    1    1   0  0  0  0\n"
    1.52 +  "2 4    5    5    5    5   1  0  0  0\n"
    1.53 +  "2 3    8    8    8    8   0  0  0  0\n"
    1.54 +  "3 2   -2    0    0    0   1  0  0  0\n"
    1.55 +  "3 4    4    4    4    4   0  0  0  0\n"
    1.56 +  "3 7   -4   -4   -4   -4   0  0  0  0\n"
    1.57 +  "4 1    2    2    2    2   0  0  0  0\n"
    1.58 +  "4 3    3    3    3    3   1  0  0  0\n"
    1.59 +  "4 4    3    3    0    0   0  0  1  0\n"
    1.60 +  "5 2    4    4    4    4   0  0  0  0\n"
    1.61 +  "5 6    3    3    3    3   0  1  0  0\n"
    1.62 +  "6 5    2    2    2    2   0  1  0  0\n"
    1.63 +  "6 4   -1   -1   -1   -1   0  0  0  0\n"
    1.64 +  "6 7    1    1    1    1   0  0  0  0\n"
    1.65 +  "7 7    4    4    4   -1   0  0  0  1\n";
    1.66 +
    1.67 +
    1.68 +// Check the interface of an MMC algorithm
    1.69 +template <typename GR, typename Cost>
    1.70 +struct MmcClassConcept
    1.71 +{
    1.72 +  template <typename MMC>
    1.73 +  struct Constraints {
    1.74 +    void constraints() {
    1.75 +      const Constraints& me = *this;
    1.76 +
    1.77 +      typedef typename MMC
    1.78 +        ::template SetPath<ListPath<GR> >
    1.79 +        ::template SetLargeCost<Cost>
    1.80 +        ::Create MmcAlg;
    1.81 +      MmcAlg mmc(me.g, me.cost);
    1.82 +      const MmcAlg& const_mmc = mmc;
    1.83 +
    1.84 +      typename MmcAlg::Tolerance tol = const_mmc.tolerance();
    1.85 +      mmc.tolerance(tol);
    1.86 +
    1.87 +      b = mmc.cycle(p).run();
    1.88 +      b = mmc.findCycleMean();
    1.89 +      b = mmc.findCycle();
    1.90 +
    1.91 +      v = const_mmc.cycleCost();
    1.92 +      i = const_mmc.cycleSize();
    1.93 +      d = const_mmc.cycleMean();
    1.94 +      p = const_mmc.cycle();
    1.95 +    }
    1.96 +
    1.97 +    typedef concepts::ReadMap<typename GR::Arc, Cost> CM;
    1.98 +
    1.99 +    GR g;
   1.100 +    CM cost;
   1.101 +    ListPath<GR> p;
   1.102 +    Cost v;
   1.103 +    int i;
   1.104 +    double d;
   1.105 +    bool b;
   1.106 +  };
   1.107 +};
   1.108 +
   1.109 +// Perform a test with the given parameters
   1.110 +template <typename MMC>
   1.111 +void checkMmcAlg(const SmartDigraph& gr,
   1.112 +                 const SmartDigraph::ArcMap<int>& lm,
   1.113 +                 const SmartDigraph::ArcMap<int>& cm,
   1.114 +                 int cost, int size) {
   1.115 +  MMC alg(gr, lm);
   1.116 +  alg.findCycleMean();
   1.117 +  check(alg.cycleMean() == static_cast<double>(cost) / size,
   1.118 +        "Wrong cycle mean");
   1.119 +  alg.findCycle();
   1.120 +  check(alg.cycleCost() == cost && alg.cycleSize() == size,
   1.121 +        "Wrong path");
   1.122 +  SmartDigraph::ArcMap<int> cycle(gr, 0);
   1.123 +  for (typename MMC::Path::ArcIt a(alg.cycle()); a != INVALID; ++a) {
   1.124 +    ++cycle[a];
   1.125 +  }
   1.126 +  for (SmartDigraph::ArcIt a(gr); a != INVALID; ++a) {
   1.127 +    check(cm[a] == cycle[a], "Wrong path");
   1.128 +  }
   1.129 +}
   1.130 +
   1.131 +// Class for comparing types
   1.132 +template <typename T1, typename T2>
   1.133 +struct IsSameType {
   1.134 +  static const int result = 0;
   1.135 +};
   1.136 +
   1.137 +template <typename T>
   1.138 +struct IsSameType<T,T> {
   1.139 +  static const int result = 1;
   1.140 +};
   1.141 +
   1.142 +
   1.143 +int main() {
   1.144 +  #ifdef LEMON_HAVE_LONG_LONG
   1.145 +    typedef long long long_int;
   1.146 +  #else
   1.147 +    typedef long long_int;
   1.148 +  #endif
   1.149 +
   1.150 +  // Check the interface
   1.151 +  {
   1.152 +    typedef concepts::Digraph GR;
   1.153 +
   1.154 +    // KarpMmc
   1.155 +    checkConcept< MmcClassConcept<GR, int>,
   1.156 +                  KarpMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
   1.157 +    checkConcept< MmcClassConcept<GR, float>,
   1.158 +                  KarpMmc<GR, concepts::ReadMap<GR::Arc, float> > >();
   1.159 +
   1.160 +    // HartmannOrlinMmc
   1.161 +    checkConcept< MmcClassConcept<GR, int>,
   1.162 +                  HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
   1.163 +    checkConcept< MmcClassConcept<GR, float>,
   1.164 +                  HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, float> > >();
   1.165 +
   1.166 +    // HowardMmc
   1.167 +    checkConcept< MmcClassConcept<GR, int>,
   1.168 +                  HowardMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
   1.169 +    checkConcept< MmcClassConcept<GR, float>,
   1.170 +                  HowardMmc<GR, concepts::ReadMap<GR::Arc, float> > >();
   1.171 +
   1.172 +    check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, int> >
   1.173 +           ::LargeCost, long_int>::result == 1), "Wrong LargeCost type");
   1.174 +    check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, float> >
   1.175 +           ::LargeCost, double>::result == 1), "Wrong LargeCost type");
   1.176 +  }
   1.177 +
   1.178 +  // Run various tests
   1.179 +  {
   1.180 +    typedef SmartDigraph GR;
   1.181 +    DIGRAPH_TYPEDEFS(GR);
   1.182 +
   1.183 +    GR gr;
   1.184 +    IntArcMap l1(gr), l2(gr), l3(gr), l4(gr);
   1.185 +    IntArcMap c1(gr), c2(gr), c3(gr), c4(gr);
   1.186 +
   1.187 +    std::istringstream input(test_lgf);
   1.188 +    digraphReader(gr, input).
   1.189 +      arcMap("len1", l1).
   1.190 +      arcMap("len2", l2).
   1.191 +      arcMap("len3", l3).
   1.192 +      arcMap("len4", l4).
   1.193 +      arcMap("c1", c1).
   1.194 +      arcMap("c2", c2).
   1.195 +      arcMap("c3", c3).
   1.196 +      arcMap("c4", c4).
   1.197 +      run();
   1.198 +
   1.199 +    // Karp
   1.200 +    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
   1.201 +    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
   1.202 +    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
   1.203 +    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);
   1.204 +
   1.205 +    // HartmannOrlin
   1.206 +    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
   1.207 +    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
   1.208 +    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
   1.209 +    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);
   1.210 +
   1.211 +    // Howard
   1.212 +    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
   1.213 +    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
   1.214 +    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
   1.215 +    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);
   1.216 +  }
   1.217 +
   1.218 +  return 0;
   1.219 +}