1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
22 #include <lemon/smart_graph.h>
23 #include <lemon/lgf_reader.h>
24 #include <lemon/path.h>
25 #include <lemon/concepts/digraph.h>
26 #include <lemon/concept_check.h>
28 #include <lemon/karp.h>
29 #include <lemon/hartmann_orlin.h>
30 #include <lemon/howard.h>
32 #include "test_tools.h"
34 using namespace lemon;
47 " len1 len2 len3 len4 c1 c2 c3 c4\n"
48 "1 2 1 1 1 1 0 0 0 0\n"
49 "2 4 5 5 5 5 1 0 0 0\n"
50 "2 3 8 8 8 8 0 0 0 0\n"
51 "3 2 -2 0 0 0 1 0 0 0\n"
52 "3 4 4 4 4 4 0 0 0 0\n"
53 "3 7 -4 -4 -4 -4 0 0 0 0\n"
54 "4 1 2 2 2 2 0 0 0 0\n"
55 "4 3 3 3 3 3 1 0 0 0\n"
56 "4 4 3 3 0 0 0 0 1 0\n"
57 "5 2 4 4 4 4 0 0 0 0\n"
58 "5 6 3 3 3 3 0 1 0 0\n"
59 "6 5 2 2 2 2 0 1 0 0\n"
60 "6 4 -1 -1 -1 -1 0 0 0 0\n"
61 "6 7 1 1 1 1 0 0 0 0\n"
62 "7 7 4 4 4 -1 0 0 0 1\n";
65 // Check the interface of an MMC algorithm
66 template <typename GR, typename Value>
67 struct MmcClassConcept
69 template <typename MMC>
72 const Constraints& me = *this;
75 ::template SetPath<ListPath<GR> >
76 ::template SetLargeValue<Value>
78 MmcAlg mmc(me.g, me.length);
79 const MmcAlg& const_mmc = mmc;
81 b = mmc.cycle(p).run();
82 b = mmc.findMinMean();
85 v = const_mmc.cycleLength();
86 i = const_mmc.cycleArcNum();
87 d = const_mmc.cycleMean();
88 p = const_mmc.cycle();
91 typedef concepts::ReadMap<typename GR::Arc, Value> LM;
103 // Perform a test with the given parameters
104 template <typename MMC>
105 void checkMmcAlg(const SmartDigraph& gr,
106 const SmartDigraph::ArcMap<int>& lm,
107 const SmartDigraph::ArcMap<int>& cm,
108 int length, int size) {
111 check(alg.cycleMean() == static_cast<double>(length) / size,
114 check(alg.cycleLength() == length && alg.cycleArcNum() == size,
116 SmartDigraph::ArcMap<int> cycle(gr, 0);
117 for (typename MMC::Path::ArcIt a(alg.cycle()); a != INVALID; ++a) {
120 for (SmartDigraph::ArcIt a(gr); a != INVALID; ++a) {
121 check(cm[a] == cycle[a], "Wrong path");
125 // Class for comparing types
126 template <typename T1, typename T2>
128 static const int result = 0;
131 template <typename T>
132 struct IsSameType<T,T> {
133 static const int result = 1;
138 #ifdef LEMON_HAVE_LONG_LONG
139 typedef long long long_int;
141 typedef long long_int;
144 // Check the interface
146 typedef concepts::Digraph GR;
149 checkConcept< MmcClassConcept<GR, int>,
150 Karp<GR, concepts::ReadMap<GR::Arc, int> > >();
151 checkConcept< MmcClassConcept<GR, float>,
152 Karp<GR, concepts::ReadMap<GR::Arc, float> > >();
155 checkConcept< MmcClassConcept<GR, int>,
156 HartmannOrlin<GR, concepts::ReadMap<GR::Arc, int> > >();
157 checkConcept< MmcClassConcept<GR, float>,
158 HartmannOrlin<GR, concepts::ReadMap<GR::Arc, float> > >();
161 checkConcept< MmcClassConcept<GR, int>,
162 Howard<GR, concepts::ReadMap<GR::Arc, int> > >();
163 checkConcept< MmcClassConcept<GR, float>,
164 Howard<GR, concepts::ReadMap<GR::Arc, float> > >();
166 if (IsSameType<Howard<GR, concepts::ReadMap<GR::Arc, int> >::LargeValue,
167 long_int>::result == 0) check(false, "Wrong LargeValue type");
168 if (IsSameType<Howard<GR, concepts::ReadMap<GR::Arc, float> >::LargeValue,
169 double>::result == 0) check(false, "Wrong LargeValue type");
174 typedef SmartDigraph GR;
175 DIGRAPH_TYPEDEFS(GR);
178 IntArcMap l1(gr), l2(gr), l3(gr), l4(gr);
179 IntArcMap c1(gr), c2(gr), c3(gr), c4(gr);
181 std::istringstream input(test_lgf);
182 digraphReader(gr, input).
194 checkMmcAlg<Karp<GR, IntArcMap> >(gr, l1, c1, 6, 3);
195 checkMmcAlg<Karp<GR, IntArcMap> >(gr, l2, c2, 5, 2);
196 checkMmcAlg<Karp<GR, IntArcMap> >(gr, l3, c3, 0, 1);
197 checkMmcAlg<Karp<GR, IntArcMap> >(gr, l4, c4, -1, 1);
200 checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l1, c1, 6, 3);
201 checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l2, c2, 5, 2);
202 checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l3, c3, 0, 1);
203 checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l4, c4, -1, 1);
206 checkMmcAlg<Howard<GR, IntArcMap> >(gr, l1, c1, 6, 3);
207 checkMmcAlg<Howard<GR, IntArcMap> >(gr, l2, c2, 5, 2);
208 checkMmcAlg<Howard<GR, IntArcMap> >(gr, l3, c3, 0, 1);
209 checkMmcAlg<Howard<GR, IntArcMap> >(gr, l4, c4, -1, 1);