Welcome to mirror list, hosted at ThFree Co, Russian Federation.

min_mean_cycle_test.cc « test « lemon-1.3.1 « 3rd « quadriflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7524546e548b2e77b8630d1f8bfb0ba0b29ec9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2013
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

#include <iostream>
#include <sstream>

#include <lemon/smart_graph.h>
#include <lemon/lgf_reader.h>
#include <lemon/path.h>
#include <lemon/concepts/digraph.h>
#include <lemon/concept_check.h>

#include <lemon/karp_mmc.h>
#include <lemon/hartmann_orlin_mmc.h>
#include <lemon/howard_mmc.h>

#include "test_tools.h"

using namespace lemon;

char test_lgf[] =
  "@nodes\n"
  "label\n"
  "1\n"
  "2\n"
  "3\n"
  "4\n"
  "5\n"
  "6\n"
  "7\n"
  "@arcs\n"
  "    len1 len2 len3 len4  c1 c2 c3 c4\n"
  "1 2    1    1    1    1   0  0  0  0\n"
  "2 4    5    5    5    5   1  0  0  0\n"
  "2 3    8    8    8    8   0  0  0  0\n"
  "3 2   -2    0    0    0   1  0  0  0\n"
  "3 4    4    4    4    4   0  0  0  0\n"
  "3 7   -4   -4   -4   -4   0  0  0  0\n"
  "4 1    2    2    2    2   0  0  0  0\n"
  "4 3    3    3    3    3   1  0  0  0\n"
  "4 4    3    3    0    0   0  0  1  0\n"
  "5 2    4    4    4    4   0  0  0  0\n"
  "5 6    3    3    3    3   0  1  0  0\n"
  "6 5    2    2    2    2   0  1  0  0\n"
  "6 4   -1   -1   -1   -1   0  0  0  0\n"
  "6 7    1    1    1    1   0  0  0  0\n"
  "7 7    4    4    4   -1   0  0  0  1\n";


// Check the interface of an MMC algorithm
template <typename GR, typename Cost>
struct MmcClassConcept
{
  template <typename MMC>
  struct Constraints {
    void constraints() {
      const Constraints& me = *this;

      typedef typename MMC
        ::template SetPath<ListPath<GR> >
        ::template SetLargeCost<Cost>
        ::Create MmcAlg;
      MmcAlg mmc(me.g, me.cost);
      const MmcAlg& const_mmc = mmc;

      typename MmcAlg::Tolerance tol = const_mmc.tolerance();
      mmc.tolerance(tol);

      b = mmc.cycle(p).run();
      b = mmc.findCycleMean();
      b = mmc.findCycle();

      v = const_mmc.cycleCost();
      i = const_mmc.cycleSize();
      d = const_mmc.cycleMean();
      p = const_mmc.cycle();
    }

    typedef concepts::ReadMap<typename GR::Arc, Cost> CM;

    GR g;
    CM cost;
    ListPath<GR> p;
    Cost v;
    int i;
    double d;
    bool b;
  };
};

// Perform a test with the given parameters
template <typename MMC>
void checkMmcAlg(const SmartDigraph& gr,
                 const SmartDigraph::ArcMap<int>& lm,
                 const SmartDigraph::ArcMap<int>& cm,
                 int cost, int size) {
  MMC alg(gr, lm);
  check(alg.findCycleMean(), "Wrong result");
  check(alg.cycleMean() == static_cast<double>(cost) / size,
        "Wrong cycle mean");
  alg.findCycle();
  check(alg.cycleCost() == cost && alg.cycleSize() == size,
        "Wrong path");
  SmartDigraph::ArcMap<int> cycle(gr, 0);
  for (typename MMC::Path::ArcIt a(alg.cycle()); a != INVALID; ++a) {
    ++cycle[a];
  }
  for (SmartDigraph::ArcIt a(gr); a != INVALID; ++a) {
    check(cm[a] == cycle[a], "Wrong path");
  }
}

// Class for comparing types
template <typename T1, typename T2>
struct IsSameType {
  static const int result = 0;
};

template <typename T>
struct IsSameType<T,T> {
  static const int result = 1;
};


int main() {
  #ifdef LEMON_HAVE_LONG_LONG
    typedef long long long_int;
  #else
    typedef long long_int;
  #endif

  // Check the interface
  {
    typedef concepts::Digraph GR;

    // KarpMmc
    checkConcept< MmcClassConcept<GR, int>,
                  KarpMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
    checkConcept< MmcClassConcept<GR, float>,
                  KarpMmc<GR, concepts::ReadMap<GR::Arc, float> > >();

    // HartmannOrlinMmc
    checkConcept< MmcClassConcept<GR, int>,
                  HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
    checkConcept< MmcClassConcept<GR, float>,
                  HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, float> > >();

    // HowardMmc
    checkConcept< MmcClassConcept<GR, int>,
                  HowardMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
    checkConcept< MmcClassConcept<GR, float>,
                  HowardMmc<GR, concepts::ReadMap<GR::Arc, float> > >();

    check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, int> >
           ::LargeCost, long_int>::result == 1), "Wrong LargeCost type");
    check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, float> >
           ::LargeCost, double>::result == 1), "Wrong LargeCost type");
  }

  // Run various tests
  {
    typedef SmartDigraph GR;
    DIGRAPH_TYPEDEFS(GR);

    GR gr;
    IntArcMap l1(gr), l2(gr), l3(gr), l4(gr);
    IntArcMap c1(gr), c2(gr), c3(gr), c4(gr);

    std::istringstream input(test_lgf);
    digraphReader(gr, input).
      arcMap("len1", l1).
      arcMap("len2", l2).
      arcMap("len3", l3).
      arcMap("len4", l4).
      arcMap("c1", c1).
      arcMap("c2", c2).
      arcMap("c3", c3).
      arcMap("c4", c4).
      run();

    // Karp
    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);

    // HartmannOrlin
    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);

    // Howard
    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);

    // Howard with iteration limit
    HowardMmc<GR, IntArcMap> mmc(gr, l1);
    check((mmc.findCycleMean(2) == HowardMmc<GR, IntArcMap>::ITERATION_LIMIT),
      "Wrong termination cause");
    check((mmc.findCycleMean(4) == HowardMmc<GR, IntArcMap>::OPTIMAL),
      "Wrong termination cause");
  }

  return 0;
}