test/max_clique_test.cc
changeset 1000 de428ebb47ab
child 1022 8583fb74238c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/max_clique_test.cc	Sun Sep 12 08:32:46 2010 +0200
     1.3 @@ -0,0 +1,176 @@
     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-2010
     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 <sstream>
    1.23 +#include <lemon/list_graph.h>
    1.24 +#include <lemon/full_graph.h>
    1.25 +#include <lemon/grid_graph.h>
    1.26 +#include <lemon/lgf_reader.h>
    1.27 +#include <lemon/grosso_locatelli_pullan_mc.h>
    1.28 +
    1.29 +#include "test_tools.h"
    1.30 +
    1.31 +using namespace lemon;
    1.32 +
    1.33 +char test_lgf[] =
    1.34 +  "@nodes\n"
    1.35 +  "label max_clique\n"
    1.36 +  "1     0\n"
    1.37 +  "2     0\n"
    1.38 +  "3     0\n"
    1.39 +  "4     1\n"
    1.40 +  "5     1\n"
    1.41 +  "6     1\n"
    1.42 +  "7     1\n"
    1.43 +  "@edges\n"
    1.44 +  "    label\n"
    1.45 +  "1 2     1\n"
    1.46 +  "1 3     2\n"
    1.47 +  "1 4     3\n"
    1.48 +  "1 6     4\n"
    1.49 +  "2 3     5\n"
    1.50 +  "2 5     6\n"
    1.51 +  "2 7     7\n"
    1.52 +  "3 4     8\n"
    1.53 +  "3 5     9\n"
    1.54 +  "4 5    10\n"
    1.55 +  "4 6    11\n"
    1.56 +  "4 7    12\n"
    1.57 +  "5 6    13\n"
    1.58 +  "5 7    14\n"
    1.59 +  "6 7    15\n";
    1.60 +      
    1.61 +
    1.62 +// Check with general graphs
    1.63 +template <typename Param>
    1.64 +void checkMaxCliqueGeneral(int max_sel, Param rule) {
    1.65 +  typedef ListGraph GR;
    1.66 +  typedef GrossoLocatelliPullanMc<GR> McAlg;
    1.67 +  typedef McAlg::CliqueNodeIt CliqueIt;
    1.68 +  
    1.69 +  // Basic tests
    1.70 +  {
    1.71 +    GR g;
    1.72 +    GR::NodeMap<bool> map(g);
    1.73 +    McAlg mc(g);
    1.74 +    check(mc.run(max_sel, rule) == 0, "Wrong clique size");
    1.75 +    check(mc.cliqueSize() == 0, "Wrong clique size");
    1.76 +    check(CliqueIt(mc) == INVALID, "Wrong CliqueNodeIt");
    1.77 +
    1.78 +    GR::Node u = g.addNode();
    1.79 +    check(mc.run(max_sel, rule) == 1, "Wrong clique size");
    1.80 +    check(mc.cliqueSize() == 1, "Wrong clique size");
    1.81 +    mc.cliqueMap(map);
    1.82 +    check(map[u], "Wrong clique map");
    1.83 +    CliqueIt it1(mc);
    1.84 +    check(static_cast<GR::Node>(it1) == u && ++it1 == INVALID,
    1.85 +          "Wrong CliqueNodeIt");
    1.86 +    
    1.87 +    GR::Node v = g.addNode();
    1.88 +    check(mc.run(max_sel, rule) == 1, "Wrong clique size");
    1.89 +    check(mc.cliqueSize() == 1, "Wrong clique size");
    1.90 +    mc.cliqueMap(map);
    1.91 +    check((map[u] && !map[v]) || (map[v] && !map[u]), "Wrong clique map");
    1.92 +    CliqueIt it2(mc);
    1.93 +    check(it2 != INVALID && ++it2 == INVALID, "Wrong CliqueNodeIt");
    1.94 +
    1.95 +    g.addEdge(u, v);
    1.96 +    check(mc.run(max_sel, rule) == 2, "Wrong clique size");
    1.97 +    check(mc.cliqueSize() == 2, "Wrong clique size");
    1.98 +    mc.cliqueMap(map);
    1.99 +    check(map[u] && map[v], "Wrong clique map");
   1.100 +    CliqueIt it3(mc);
   1.101 +    check(it3 != INVALID && ++it3 != INVALID && ++it3 == INVALID,
   1.102 +          "Wrong CliqueNodeIt");
   1.103 +  }
   1.104 +
   1.105 +  // Test graph
   1.106 +  {
   1.107 +    GR g;
   1.108 +    GR::NodeMap<bool> max_clique(g);
   1.109 +    GR::NodeMap<bool> map(g);
   1.110 +    std::istringstream input(test_lgf);
   1.111 +    graphReader(g, input)
   1.112 +      .nodeMap("max_clique", max_clique)
   1.113 +      .run();
   1.114 +    
   1.115 +    McAlg mc(g);
   1.116 +    check(mc.run(max_sel, rule) == 4, "Wrong clique size");
   1.117 +    check(mc.cliqueSize() == 4, "Wrong clique size");
   1.118 +    mc.cliqueMap(map);
   1.119 +    for (GR::NodeIt n(g); n != INVALID; ++n) {
   1.120 +      check(map[n] == max_clique[n], "Wrong clique map");
   1.121 +    }
   1.122 +    int cnt = 0;
   1.123 +    for (CliqueIt n(mc); n != INVALID; ++n) {
   1.124 +      cnt++;
   1.125 +      check(map[n] && max_clique[n], "Wrong CliqueNodeIt");
   1.126 +    }
   1.127 +    check(cnt == 4, "Wrong CliqueNodeIt");
   1.128 +  }
   1.129 +}
   1.130 +
   1.131 +// Check with full graphs
   1.132 +template <typename Param>
   1.133 +void checkMaxCliqueFullGraph(int max_sel, Param rule) {
   1.134 +  typedef FullGraph GR;
   1.135 +  typedef GrossoLocatelliPullanMc<FullGraph> McAlg;
   1.136 +  typedef McAlg::CliqueNodeIt CliqueIt;
   1.137 +  
   1.138 +  for (int size = 0; size <= 40; size = size * 3 + 1) {
   1.139 +    GR g(size);
   1.140 +    GR::NodeMap<bool> map(g);
   1.141 +    McAlg mc(g);
   1.142 +    check(mc.run(max_sel, rule) == size, "Wrong clique size");
   1.143 +    check(mc.cliqueSize() == size, "Wrong clique size");
   1.144 +    mc.cliqueMap(map);
   1.145 +    for (GR::NodeIt n(g); n != INVALID; ++n) {
   1.146 +      check(map[n], "Wrong clique map");
   1.147 +    }
   1.148 +    int cnt = 0;
   1.149 +    for (CliqueIt n(mc); n != INVALID; ++n) cnt++;
   1.150 +    check(cnt == size, "Wrong CliqueNodeIt");
   1.151 +  }
   1.152 +}
   1.153 +
   1.154 +// Check with grid graphs
   1.155 +template <typename Param>
   1.156 +void checkMaxCliqueGridGraph(int max_sel, Param rule) {
   1.157 +  GridGraph g(5, 7);
   1.158 +  GridGraph::NodeMap<char> map(g);
   1.159 +  GrossoLocatelliPullanMc<GridGraph> mc(g);
   1.160 +  check(mc.run(max_sel, rule) == 2, "Wrong clique size");
   1.161 +  check(mc.cliqueSize() == 2, "Wrong clique size");
   1.162 +}
   1.163 +
   1.164 +
   1.165 +int main() {
   1.166 +  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::RANDOM);
   1.167 +  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::DEGREE_BASED);
   1.168 +  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::PENALTY_BASED);
   1.169 +
   1.170 +  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::RANDOM);
   1.171 +  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::DEGREE_BASED);
   1.172 +  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::PENALTY_BASED);
   1.173 +                       
   1.174 +  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::RANDOM);
   1.175 +  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::DEGREE_BASED);
   1.176 +  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::PENALTY_BASED);
   1.177 +                       
   1.178 +  return 0;
   1.179 +}