1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/max_clique_test.cc Wed Oct 17 19:14:07 2018 +0200
1.3 @@ -0,0 +1,188 @@
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-2013
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(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 + mc.iterationLimit(50);
1.75 + check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause");
1.76 + check(mc.cliqueSize() == 0, "Wrong clique size");
1.77 + check(CliqueIt(mc) == INVALID, "Wrong CliqueNodeIt");
1.78 +
1.79 + GR::Node u = g.addNode();
1.80 + check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause");
1.81 + check(mc.cliqueSize() == 1, "Wrong clique size");
1.82 + mc.cliqueMap(map);
1.83 + check(map[u], "Wrong clique map");
1.84 + CliqueIt it1(mc);
1.85 + check(static_cast<GR::Node>(it1) == u && ++it1 == INVALID,
1.86 + "Wrong CliqueNodeIt");
1.87 +
1.88 + GR::Node v = g.addNode();
1.89 + check(mc.run(rule) == McAlg::ITERATION_LIMIT, "Wrong termination cause");
1.90 + check(mc.cliqueSize() == 1, "Wrong clique size");
1.91 + mc.cliqueMap(map);
1.92 + check((map[u] && !map[v]) || (map[v] && !map[u]), "Wrong clique map");
1.93 + CliqueIt it2(mc);
1.94 + check(it2 != INVALID && ++it2 == INVALID, "Wrong CliqueNodeIt");
1.95 +
1.96 + g.addEdge(u, v);
1.97 + check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause");
1.98 + check(mc.cliqueSize() == 2, "Wrong clique size");
1.99 + mc.cliqueMap(map);
1.100 + check(map[u] && map[v], "Wrong clique map");
1.101 + CliqueIt it3(mc);
1.102 + check(it3 != INVALID && ++it3 != INVALID && ++it3 == INVALID,
1.103 + "Wrong CliqueNodeIt");
1.104 + }
1.105 +
1.106 + // Test graph
1.107 + {
1.108 + GR g;
1.109 + GR::NodeMap<bool> max_clique(g);
1.110 + GR::NodeMap<bool> map(g);
1.111 + std::istringstream input(test_lgf);
1.112 + graphReader(g, input)
1.113 + .nodeMap("max_clique", max_clique)
1.114 + .run();
1.115 +
1.116 + McAlg mc(g);
1.117 + mc.iterationLimit(50);
1.118 + check(mc.run(rule) == McAlg::ITERATION_LIMIT, "Wrong termination cause");
1.119 + check(mc.cliqueSize() == 4, "Wrong clique size");
1.120 + mc.cliqueMap(map);
1.121 + for (GR::NodeIt n(g); n != INVALID; ++n) {
1.122 + check(map[n] == max_clique[n], "Wrong clique map");
1.123 + }
1.124 + int cnt = 0;
1.125 + for (CliqueIt n(mc); n != INVALID; ++n) {
1.126 + cnt++;
1.127 + check(map[n] && max_clique[n], "Wrong CliqueNodeIt");
1.128 + }
1.129 + check(cnt == 4, "Wrong CliqueNodeIt");
1.130 + }
1.131 +}
1.132 +
1.133 +// Check with full graphs
1.134 +template <typename Param>
1.135 +void checkMaxCliqueFullGraph(Param rule) {
1.136 + typedef FullGraph GR;
1.137 + typedef GrossoLocatelliPullanMc<FullGraph> McAlg;
1.138 + typedef McAlg::CliqueNodeIt CliqueIt;
1.139 +
1.140 + for (int size = 0; size <= 40; size = size * 3 + 1) {
1.141 + GR g(size);
1.142 + GR::NodeMap<bool> map(g);
1.143 + McAlg mc(g);
1.144 + check(mc.run(rule) == McAlg::SIZE_LIMIT, "Wrong termination cause");
1.145 + check(mc.cliqueSize() == size, "Wrong clique size");
1.146 + mc.cliqueMap(map);
1.147 + for (GR::NodeIt n(g); n != INVALID; ++n) {
1.148 + check(map[n], "Wrong clique map");
1.149 + }
1.150 + int cnt = 0;
1.151 + for (CliqueIt n(mc); n != INVALID; ++n) cnt++;
1.152 + check(cnt == size, "Wrong CliqueNodeIt");
1.153 + }
1.154 +}
1.155 +
1.156 +// Check with grid graphs
1.157 +template <typename Param>
1.158 +void checkMaxCliqueGridGraph(Param rule) {
1.159 + GridGraph g(5, 7);
1.160 + GridGraph::NodeMap<char> map(g);
1.161 + GrossoLocatelliPullanMc<GridGraph> mc(g);
1.162 +
1.163 + mc.iterationLimit(100);
1.164 + check(mc.run(rule) == mc.ITERATION_LIMIT, "Wrong termination cause");
1.165 + check(mc.cliqueSize() == 2, "Wrong clique size");
1.166 +
1.167 + mc.stepLimit(100);
1.168 + check(mc.run(rule) == mc.STEP_LIMIT, "Wrong termination cause");
1.169 + check(mc.cliqueSize() == 2, "Wrong clique size");
1.170 +
1.171 + mc.sizeLimit(2);
1.172 + check(mc.run(rule) == mc.SIZE_LIMIT, "Wrong termination cause");
1.173 + check(mc.cliqueSize() == 2, "Wrong clique size");
1.174 +}
1.175 +
1.176 +
1.177 +int main() {
1.178 + checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::RANDOM);
1.179 + checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::DEGREE_BASED);
1.180 + checkMaxCliqueGeneral(GrossoLocatelliPullanMc<ListGraph>::PENALTY_BASED);
1.181 +
1.182 + checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::RANDOM);
1.183 + checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::DEGREE_BASED);
1.184 + checkMaxCliqueFullGraph(GrossoLocatelliPullanMc<FullGraph>::PENALTY_BASED);
1.185 +
1.186 + checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::RANDOM);
1.187 + checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::DEGREE_BASED);
1.188 + checkMaxCliqueGridGraph(GrossoLocatelliPullanMc<GridGraph>::PENALTY_BASED);
1.189 +
1.190 + return 0;
1.191 +}