test/max_clique_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 16 Mar 2013 13:16:27 +0100
changeset 1218 d9d1cb759951
parent 999 c279b19abc62
child 1270 dceba191c00d
permissions -rw-r--r--
Add images to the doc of two groups (#459)
kpeter@999
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@999
     2
 *
kpeter@999
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@999
     4
 *
kpeter@999
     5
 * Copyright (C) 2003-2010
kpeter@999
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@999
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@999
     8
 *
kpeter@999
     9
 * Permission to use, modify and distribute this software is granted
kpeter@999
    10
 * provided that this copyright notice appears in all copies. For
kpeter@999
    11
 * precise terms see the accompanying LICENSE file.
kpeter@999
    12
 *
kpeter@999
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@999
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@999
    15
 * purpose.
kpeter@999
    16
 *
kpeter@999
    17
 */
kpeter@999
    18
kpeter@999
    19
#include <sstream>
kpeter@999
    20
#include <lemon/list_graph.h>
kpeter@999
    21
#include <lemon/full_graph.h>
kpeter@999
    22
#include <lemon/grid_graph.h>
kpeter@999
    23
#include <lemon/lgf_reader.h>
kpeter@999
    24
#include <lemon/grosso_locatelli_pullan_mc.h>
kpeter@999
    25
kpeter@999
    26
#include "test_tools.h"
kpeter@999
    27
kpeter@999
    28
using namespace lemon;
kpeter@999
    29
kpeter@999
    30
char test_lgf[] =
kpeter@999
    31
  "@nodes\n"
kpeter@999
    32
  "label max_clique\n"
kpeter@999
    33
  "1     0\n"
kpeter@999
    34
  "2     0\n"
kpeter@999
    35
  "3     0\n"
kpeter@999
    36
  "4     1\n"
kpeter@999
    37
  "5     1\n"
kpeter@999
    38
  "6     1\n"
kpeter@999
    39
  "7     1\n"
kpeter@999
    40
  "@edges\n"
kpeter@999
    41
  "    label\n"
kpeter@999
    42
  "1 2     1\n"
kpeter@999
    43
  "1 3     2\n"
kpeter@999
    44
  "1 4     3\n"
kpeter@999
    45
  "1 6     4\n"
kpeter@999
    46
  "2 3     5\n"
kpeter@999
    47
  "2 5     6\n"
kpeter@999
    48
  "2 7     7\n"
kpeter@999
    49
  "3 4     8\n"
kpeter@999
    50
  "3 5     9\n"
kpeter@999
    51
  "4 5    10\n"
kpeter@999
    52
  "4 6    11\n"
kpeter@999
    53
  "4 7    12\n"
kpeter@999
    54
  "5 6    13\n"
kpeter@999
    55
  "5 7    14\n"
kpeter@999
    56
  "6 7    15\n";
kpeter@999
    57
      
kpeter@999
    58
kpeter@999
    59
// Check with general graphs
kpeter@999
    60
template <typename Param>
kpeter@1022
    61
void checkMaxCliqueGeneral(Param rule) {
kpeter@999
    62
  typedef ListGraph GR;
kpeter@999
    63
  typedef GrossoLocatelliPullanMc<GR> McAlg;
kpeter@999
    64
  typedef McAlg::CliqueNodeIt CliqueIt;
kpeter@999
    65
  
kpeter@999
    66
  // Basic tests
kpeter@999
    67
  {
kpeter@999
    68
    GR g;
kpeter@999
    69
    GR::NodeMap<bool> map(g);
kpeter@999
    70
    McAlg mc(g);
kpeter@1022
    71
    mc.iterationLimit(50);
kpeter@1022
    72
    check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause");
kpeter@999
    73
    check(mc.cliqueSize() == 0, "Wrong clique size");
kpeter@999
    74
    check(CliqueIt(mc) == INVALID, "Wrong CliqueNodeIt");
kpeter@999
    75
kpeter@999
    76
    GR::Node u = g.addNode();
kpeter@1022
    77
    check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause");
kpeter@999
    78
    check(mc.cliqueSize() == 1, "Wrong clique size");
kpeter@999
    79
    mc.cliqueMap(map);
kpeter@999
    80
    check(map[u], "Wrong clique map");
kpeter@999
    81
    CliqueIt it1(mc);
kpeter@999
    82
    check(static_cast<GR::Node>(it1) == u && ++it1 == INVALID,
kpeter@999
    83
          "Wrong CliqueNodeIt");
kpeter@999
    84
    
kpeter@999
    85
    GR::Node v = g.addNode();
kpeter@1022
    86
    check(mc.run(rule) == McAlg::ITERATION_LIMIT, "Wrong termination cause");
kpeter@999
    87
    check(mc.cliqueSize() == 1, "Wrong clique size");
kpeter@999
    88
    mc.cliqueMap(map);
kpeter@999
    89
    check((map[u] && !map[v]) || (map[v] && !map[u]), "Wrong clique map");
kpeter@999
    90
    CliqueIt it2(mc);
kpeter@999
    91
    check(it2 != INVALID && ++it2 == INVALID, "Wrong CliqueNodeIt");
kpeter@999
    92
kpeter@999
    93
    g.addEdge(u, v);
kpeter@1022
    94
    check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause");
kpeter@999
    95
    check(mc.cliqueSize() == 2, "Wrong clique size");
kpeter@999
    96
    mc.cliqueMap(map);
kpeter@999
    97
    check(map[u] && map[v], "Wrong clique map");
kpeter@999
    98
    CliqueIt it3(mc);
kpeter@999
    99
    check(it3 != INVALID && ++it3 != INVALID && ++it3 == INVALID,
kpeter@999
   100
          "Wrong CliqueNodeIt");
kpeter@999
   101
  }
kpeter@999
   102
kpeter@999
   103
  // Test graph
kpeter@999
   104
  {
kpeter@999
   105
    GR g;
kpeter@999
   106
    GR::NodeMap<bool> max_clique(g);
kpeter@999
   107
    GR::NodeMap<bool> map(g);
kpeter@999
   108
    std::istringstream input(test_lgf);
kpeter@999
   109
    graphReader(g, input)
kpeter@999
   110
      .nodeMap("max_clique", max_clique)
kpeter@999
   111
      .run();
kpeter@999
   112
    
kpeter@999
   113
    McAlg mc(g);
kpeter@1022
   114
    mc.iterationLimit(50);
kpeter@1022
   115
    check(mc.run(rule) == McAlg::ITERATION_LIMIT, "Wrong termination cause");
kpeter@999
   116
    check(mc.cliqueSize() == 4, "Wrong clique size");
kpeter@999
   117
    mc.cliqueMap(map);
kpeter@999
   118
    for (GR::NodeIt n(g); n != INVALID; ++n) {
kpeter@999
   119
      check(map[n] == max_clique[n], "Wrong clique map");
kpeter@999
   120
    }
kpeter@999
   121
    int cnt = 0;
kpeter@999
   122
    for (CliqueIt n(mc); n != INVALID; ++n) {
kpeter@999
   123
      cnt++;
kpeter@999
   124
      check(map[n] && max_clique[n], "Wrong CliqueNodeIt");
kpeter@999
   125
    }
kpeter@999
   126
    check(cnt == 4, "Wrong CliqueNodeIt");
kpeter@999
   127
  }
kpeter@999
   128
}
kpeter@999
   129
kpeter@999
   130
// Check with full graphs
kpeter@999
   131
template <typename Param>
kpeter@1022
   132
void checkMaxCliqueFullGraph(Param rule) {
kpeter@999
   133
  typedef FullGraph GR;
kpeter@999
   134
  typedef GrossoLocatelliPullanMc<FullGraph> McAlg;
kpeter@999
   135
  typedef McAlg::CliqueNodeIt CliqueIt;
kpeter@999
   136
  
kpeter@999
   137
  for (int size = 0; size <= 40; size = size * 3 + 1) {
kpeter@999
   138
    GR g(size);
kpeter@999
   139
    GR::NodeMap<bool> map(g);
kpeter@999
   140
    McAlg mc(g);
kpeter@1022
   141
    check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause");
kpeter@999
   142
    check(mc.cliqueSize() == size, "Wrong clique size");
kpeter@999
   143
    mc.cliqueMap(map);
kpeter@999
   144
    for (GR::NodeIt n(g); n != INVALID; ++n) {
kpeter@999
   145
      check(map[n], "Wrong clique map");
kpeter@999
   146
    }
kpeter@999
   147
    int cnt = 0;
kpeter@999
   148
    for (CliqueIt n(mc); n != INVALID; ++n) cnt++;
kpeter@999
   149
    check(cnt == size, "Wrong CliqueNodeIt");
kpeter@999
   150
  }
kpeter@999
   151
}
kpeter@999
   152
kpeter@999
   153
// Check with grid graphs
kpeter@999
   154
template <typename Param>
kpeter@1022
   155
void checkMaxCliqueGridGraph(Param rule) {
kpeter@999
   156
  GridGraph g(5, 7);
kpeter@999
   157
  GridGraph::NodeMap<char> map(g);
kpeter@999
   158
  GrossoLocatelliPullanMc<GridGraph> mc(g);
kpeter@1022
   159
  
kpeter@1022
   160
  mc.iterationLimit(100);
kpeter@1022
   161
  check(mc.run(rule) == mc.ITERATION_LIMIT, "Wrong termination cause");
kpeter@1022
   162
  check(mc.cliqueSize() == 2, "Wrong clique size");
kpeter@1022
   163
kpeter@1022
   164
  mc.stepLimit(100);
kpeter@1022
   165
  check(mc.run(rule) == mc.STEP_LIMIT, "Wrong termination cause");
kpeter@1022
   166
  check(mc.cliqueSize() == 2, "Wrong clique size");
kpeter@1022
   167
kpeter@1022
   168
  mc.sizeLimit(2);
kpeter@1022
   169
  check(mc.run(rule) == mc.SIZE_LIMIT, "Wrong termination cause");
kpeter@999
   170
  check(mc.cliqueSize() == 2, "Wrong clique size");
kpeter@999
   171
}
kpeter@999
   172
kpeter@999
   173
kpeter@999
   174
int main() {
kpeter@1022
   175
  checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::RANDOM);
kpeter@1022
   176
  checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::DEGREE_BASED);
kpeter@1022
   177
  checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::PENALTY_BASED);
kpeter@999
   178
kpeter@1022
   179
  checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::RANDOM);
kpeter@1022
   180
  checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::DEGREE_BASED);
kpeter@1022
   181
  checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::PENALTY_BASED);
kpeter@999
   182
                       
kpeter@1022
   183
  checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::RANDOM);
kpeter@1022
   184
  checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::DEGREE_BASED);
kpeter@1022
   185
  checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::PENALTY_BASED);
kpeter@999
   186
                       
kpeter@999
   187
  return 0;
kpeter@999
   188
}