test/max_clique_test.cc
author Gabor Varga <f4c3@inf.elte.hu>
Sat, 08 Jan 2011 21:59:56 +0100
changeset 1199 ae0b056593a7
child 1022 8583fb74238c
permissions -rw-r--r--
Heuristic algorithms for symmetric TSP (#386)
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@999
    61
void checkMaxCliqueGeneral(int max_sel, 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@999
    71
    check(mc.run(max_sel, rule) == 0, "Wrong clique size");
kpeter@999
    72
    check(mc.cliqueSize() == 0, "Wrong clique size");
kpeter@999
    73
    check(CliqueIt(mc) == INVALID, "Wrong CliqueNodeIt");
kpeter@999
    74
kpeter@999
    75
    GR::Node u = g.addNode();
kpeter@999
    76
    check(mc.run(max_sel, rule) == 1, "Wrong clique size");
kpeter@999
    77
    check(mc.cliqueSize() == 1, "Wrong clique size");
kpeter@999
    78
    mc.cliqueMap(map);
kpeter@999
    79
    check(map[u], "Wrong clique map");
kpeter@999
    80
    CliqueIt it1(mc);
kpeter@999
    81
    check(static_cast<GR::Node>(it1) == u && ++it1 == INVALID,
kpeter@999
    82
          "Wrong CliqueNodeIt");
kpeter@999
    83
    
kpeter@999
    84
    GR::Node v = g.addNode();
kpeter@999
    85
    check(mc.run(max_sel, rule) == 1, "Wrong clique size");
kpeter@999
    86
    check(mc.cliqueSize() == 1, "Wrong clique size");
kpeter@999
    87
    mc.cliqueMap(map);
kpeter@999
    88
    check((map[u] && !map[v]) || (map[v] && !map[u]), "Wrong clique map");
kpeter@999
    89
    CliqueIt it2(mc);
kpeter@999
    90
    check(it2 != INVALID && ++it2 == INVALID, "Wrong CliqueNodeIt");
kpeter@999
    91
kpeter@999
    92
    g.addEdge(u, v);
kpeter@999
    93
    check(mc.run(max_sel, rule) == 2, "Wrong clique size");
kpeter@999
    94
    check(mc.cliqueSize() == 2, "Wrong clique size");
kpeter@999
    95
    mc.cliqueMap(map);
kpeter@999
    96
    check(map[u] && map[v], "Wrong clique map");
kpeter@999
    97
    CliqueIt it3(mc);
kpeter@999
    98
    check(it3 != INVALID && ++it3 != INVALID && ++it3 == INVALID,
kpeter@999
    99
          "Wrong CliqueNodeIt");
kpeter@999
   100
  }
kpeter@999
   101
kpeter@999
   102
  // Test graph
kpeter@999
   103
  {
kpeter@999
   104
    GR g;
kpeter@999
   105
    GR::NodeMap<bool> max_clique(g);
kpeter@999
   106
    GR::NodeMap<bool> map(g);
kpeter@999
   107
    std::istringstream input(test_lgf);
kpeter@999
   108
    graphReader(g, input)
kpeter@999
   109
      .nodeMap("max_clique", max_clique)
kpeter@999
   110
      .run();
kpeter@999
   111
    
kpeter@999
   112
    McAlg mc(g);
kpeter@999
   113
    check(mc.run(max_sel, rule) == 4, "Wrong clique size");
kpeter@999
   114
    check(mc.cliqueSize() == 4, "Wrong clique size");
kpeter@999
   115
    mc.cliqueMap(map);
kpeter@999
   116
    for (GR::NodeIt n(g); n != INVALID; ++n) {
kpeter@999
   117
      check(map[n] == max_clique[n], "Wrong clique map");
kpeter@999
   118
    }
kpeter@999
   119
    int cnt = 0;
kpeter@999
   120
    for (CliqueIt n(mc); n != INVALID; ++n) {
kpeter@999
   121
      cnt++;
kpeter@999
   122
      check(map[n] && max_clique[n], "Wrong CliqueNodeIt");
kpeter@999
   123
    }
kpeter@999
   124
    check(cnt == 4, "Wrong CliqueNodeIt");
kpeter@999
   125
  }
kpeter@999
   126
}
kpeter@999
   127
kpeter@999
   128
// Check with full graphs
kpeter@999
   129
template <typename Param>
kpeter@999
   130
void checkMaxCliqueFullGraph(int max_sel, Param rule) {
kpeter@999
   131
  typedef FullGraph GR;
kpeter@999
   132
  typedef GrossoLocatelliPullanMc<FullGraph> McAlg;
kpeter@999
   133
  typedef McAlg::CliqueNodeIt CliqueIt;
kpeter@999
   134
  
kpeter@999
   135
  for (int size = 0; size <= 40; size = size * 3 + 1) {
kpeter@999
   136
    GR g(size);
kpeter@999
   137
    GR::NodeMap<bool> map(g);
kpeter@999
   138
    McAlg mc(g);
kpeter@999
   139
    check(mc.run(max_sel, rule) == size, "Wrong clique size");
kpeter@999
   140
    check(mc.cliqueSize() == size, "Wrong clique size");
kpeter@999
   141
    mc.cliqueMap(map);
kpeter@999
   142
    for (GR::NodeIt n(g); n != INVALID; ++n) {
kpeter@999
   143
      check(map[n], "Wrong clique map");
kpeter@999
   144
    }
kpeter@999
   145
    int cnt = 0;
kpeter@999
   146
    for (CliqueIt n(mc); n != INVALID; ++n) cnt++;
kpeter@999
   147
    check(cnt == size, "Wrong CliqueNodeIt");
kpeter@999
   148
  }
kpeter@999
   149
}
kpeter@999
   150
kpeter@999
   151
// Check with grid graphs
kpeter@999
   152
template <typename Param>
kpeter@999
   153
void checkMaxCliqueGridGraph(int max_sel, Param rule) {
kpeter@999
   154
  GridGraph g(5, 7);
kpeter@999
   155
  GridGraph::NodeMap<char> map(g);
kpeter@999
   156
  GrossoLocatelliPullanMc<GridGraph> mc(g);
kpeter@999
   157
  check(mc.run(max_sel, rule) == 2, "Wrong clique size");
kpeter@999
   158
  check(mc.cliqueSize() == 2, "Wrong clique size");
kpeter@999
   159
}
kpeter@999
   160
kpeter@999
   161
kpeter@999
   162
int main() {
kpeter@999
   163
  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::RANDOM);
kpeter@999
   164
  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::DEGREE_BASED);
kpeter@999
   165
  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::PENALTY_BASED);
kpeter@999
   166
kpeter@999
   167
  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::RANDOM);
kpeter@999
   168
  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::DEGREE_BASED);
kpeter@999
   169
  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::PENALTY_BASED);
kpeter@999
   170
                       
kpeter@999
   171
  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::RANDOM);
kpeter@999
   172
  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::DEGREE_BASED);
kpeter@999
   173
  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::PENALTY_BASED);
kpeter@999
   174
                       
kpeter@999
   175
  return 0;
kpeter@999
   176
}