1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/min_mean_cycle_test.cc Fri Aug 07 14:52:40 2009 +0200
1.3 @@ -0,0 +1,184 @@
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-2009
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/min_mean_cycle.h>
1.28 +#include <lemon/path.h>
1.29 +#include <lemon/concepts/digraph.h>
1.30 +#include <lemon/concept_check.h>
1.31 +
1.32 +#include "test_tools.h"
1.33 +
1.34 +using namespace lemon;
1.35 +
1.36 +char test_lgf[] =
1.37 + "@nodes\n"
1.38 + "label\n"
1.39 + "1\n"
1.40 + "2\n"
1.41 + "3\n"
1.42 + "4\n"
1.43 + "5\n"
1.44 + "6\n"
1.45 + "7\n"
1.46 + "@arcs\n"
1.47 + " len1 len2 len3 len4 c1 c2 c3 c4\n"
1.48 + "1 2 1 1 1 1 0 0 0 0\n"
1.49 + "2 4 5 5 5 5 1 0 0 0\n"
1.50 + "2 3 8 8 8 8 0 0 0 0\n"
1.51 + "3 2 -2 0 0 0 1 0 0 0\n"
1.52 + "3 4 4 4 4 4 0 0 0 0\n"
1.53 + "3 7 -4 -4 -4 -4 0 0 0 0\n"
1.54 + "4 1 2 2 2 2 0 0 0 0\n"
1.55 + "4 3 3 3 3 3 1 0 0 0\n"
1.56 + "4 4 3 3 0 0 0 0 1 0\n"
1.57 + "5 2 4 4 4 4 0 0 0 0\n"
1.58 + "5 6 3 3 3 3 0 1 0 0\n"
1.59 + "6 5 2 2 2 2 0 1 0 0\n"
1.60 + "6 4 -1 -1 -1 -1 0 0 0 0\n"
1.61 + "6 7 1 1 1 1 0 0 0 0\n"
1.62 + "7 7 4 4 4 -1 0 0 0 1\n";
1.63 +
1.64 +
1.65 +// Check the interface of an MMC algorithm
1.66 +template <typename GR, typename Value>
1.67 +struct MmcClassConcept
1.68 +{
1.69 + template <typename MMC>
1.70 + struct Constraints {
1.71 + void constraints() {
1.72 + const Constraints& me = *this;
1.73 +
1.74 + typedef typename MMC
1.75 + ::template SetPath<ListPath<GR> >
1.76 + ::template SetLargeValue<Value>
1.77 + ::Create MmcAlg;
1.78 + MmcAlg mmc(me.g, me.length);
1.79 + const MmcAlg& const_mmc = mmc;
1.80 +
1.81 + b = mmc.cycle(p).run();
1.82 + b = mmc.findMinMean();
1.83 + b = mmc.findCycle();
1.84 +
1.85 + v = const_mmc.cycleLength();
1.86 + i = const_mmc.cycleArcNum();
1.87 + d = const_mmc.cycleMean();
1.88 + p = const_mmc.cycle();
1.89 + }
1.90 +
1.91 + typedef concepts::ReadMap<typename GR::Arc, Value> LM;
1.92 +
1.93 + GR g;
1.94 + LM length;
1.95 + ListPath<GR> p;
1.96 + Value v;
1.97 + int i;
1.98 + double d;
1.99 + bool b;
1.100 + };
1.101 +};
1.102 +
1.103 +// Perform a test with the given parameters
1.104 +template <typename MMC>
1.105 +void checkMmcAlg(const SmartDigraph& gr,
1.106 + const SmartDigraph::ArcMap<int>& lm,
1.107 + const SmartDigraph::ArcMap<int>& cm,
1.108 + int length, int size) {
1.109 + MMC alg(gr, lm);
1.110 + alg.findMinMean();
1.111 + check(alg.cycleMean() == static_cast<double>(length) / size,
1.112 + "Wrong cycle mean");
1.113 + alg.findCycle();
1.114 + check(alg.cycleLength() == length && alg.cycleArcNum() == size,
1.115 + "Wrong path");
1.116 + SmartDigraph::ArcMap<int> cycle(gr, 0);
1.117 + for (typename MMC::Path::ArcIt a(alg.cycle()); a != INVALID; ++a) {
1.118 + ++cycle[a];
1.119 + }
1.120 + for (SmartDigraph::ArcIt a(gr); a != INVALID; ++a) {
1.121 + check(cm[a] == cycle[a], "Wrong path");
1.122 + }
1.123 +}
1.124 +
1.125 +// Class for comparing types
1.126 +template <typename T1, typename T2>
1.127 +struct IsSameType {
1.128 + static const int result = 0;
1.129 +};
1.130 +
1.131 +template <typename T>
1.132 +struct IsSameType<T,T> {
1.133 + static const int result = 1;
1.134 +};
1.135 +
1.136 +
1.137 +int main() {
1.138 + #ifdef LEMON_HAVE_LONG_LONG
1.139 + typedef long long long_int;
1.140 + #else
1.141 + typedef long long_int;
1.142 + #endif
1.143 +
1.144 + // Check the interface
1.145 + {
1.146 + typedef concepts::Digraph GR;
1.147 + typedef MinMeanCycle<GR, concepts::ReadMap<GR::Arc, int> > IntMmcAlg;
1.148 + typedef MinMeanCycle<GR, concepts::ReadMap<GR::Arc, float> > FloatMmcAlg;
1.149 +
1.150 + checkConcept<MmcClassConcept<GR, int>, IntMmcAlg>();
1.151 + checkConcept<MmcClassConcept<GR, float>, FloatMmcAlg>();
1.152 +
1.153 + if (IsSameType<IntMmcAlg::LargeValue, long_int>::result == 0)
1.154 + check(false, "Wrong LargeValue type");
1.155 + if (IsSameType<FloatMmcAlg::LargeValue, double>::result == 0)
1.156 + check(false, "Wrong LargeValue type");
1.157 + }
1.158 +
1.159 + // Run various tests
1.160 + {
1.161 + typedef SmartDigraph GR;
1.162 + DIGRAPH_TYPEDEFS(GR);
1.163 +
1.164 + GR gr;
1.165 + IntArcMap l1(gr), l2(gr), l3(gr), l4(gr);
1.166 + IntArcMap c1(gr), c2(gr), c3(gr), c4(gr);
1.167 +
1.168 + std::istringstream input(test_lgf);
1.169 + digraphReader(gr, input).
1.170 + arcMap("len1", l1).
1.171 + arcMap("len2", l2).
1.172 + arcMap("len3", l3).
1.173 + arcMap("len4", l4).
1.174 + arcMap("c1", c1).
1.175 + arcMap("c2", c2).
1.176 + arcMap("c3", c3).
1.177 + arcMap("c4", c4).
1.178 + run();
1.179 +
1.180 + checkMmcAlg<MinMeanCycle<GR, IntArcMap> >(gr, l1, c1, 6, 3);
1.181 + checkMmcAlg<MinMeanCycle<GR, IntArcMap> >(gr, l2, c2, 5, 2);
1.182 + checkMmcAlg<MinMeanCycle<GR, IntArcMap> >(gr, l3, c3, 0, 1);
1.183 + checkMmcAlg<MinMeanCycle<GR, IntArcMap> >(gr, l4, c4, -1, 1);
1.184 + }
1.185 +
1.186 + return 0;
1.187 +}