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 |
*
|
alpar@1270
|
5 |
* Copyright (C) 2003-2013
|
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 |
#ifndef LEMON_GROSSO_LOCATELLI_PULLAN_MC_H
|
kpeter@999
|
20 |
#define LEMON_GROSSO_LOCATELLI_PULLAN_MC_H
|
kpeter@999
|
21 |
|
kpeter@999
|
22 |
/// \ingroup approx_algs
|
kpeter@999
|
23 |
///
|
kpeter@999
|
24 |
/// \file
|
kpeter@999
|
25 |
/// \brief The iterated local search algorithm of Grosso, Locatelli, and Pullan
|
kpeter@999
|
26 |
/// for the maximum clique problem
|
kpeter@999
|
27 |
|
kpeter@999
|
28 |
#include <vector>
|
kpeter@999
|
29 |
#include <limits>
|
kpeter@999
|
30 |
#include <lemon/core.h>
|
kpeter@999
|
31 |
#include <lemon/random.h>
|
kpeter@999
|
32 |
|
kpeter@999
|
33 |
namespace lemon {
|
kpeter@999
|
34 |
|
kpeter@999
|
35 |
/// \addtogroup approx_algs
|
kpeter@999
|
36 |
/// @{
|
kpeter@999
|
37 |
|
kpeter@999
|
38 |
/// \brief Implementation of the iterated local search algorithm of Grosso,
|
kpeter@999
|
39 |
/// Locatelli, and Pullan for the maximum clique problem
|
kpeter@999
|
40 |
///
|
kpeter@999
|
41 |
/// \ref GrossoLocatelliPullanMc implements the iterated local search
|
kpeter@999
|
42 |
/// algorithm of Grosso, Locatelli, and Pullan for solving the \e maximum
|
alpar@1221
|
43 |
/// \e clique \e problem \cite grosso08maxclique.
|
kpeter@999
|
44 |
/// It is to find the largest complete subgraph (\e clique) in an
|
kpeter@999
|
45 |
/// undirected graph, i.e., the largest set of nodes where each
|
kpeter@999
|
46 |
/// pair of nodes is connected.
|
kpeter@999
|
47 |
///
|
kpeter@999
|
48 |
/// This class provides a simple but highly efficient and robust heuristic
|
kpeter@1022
|
49 |
/// method that quickly finds a quite large clique, but not necessarily the
|
kpeter@999
|
50 |
/// largest one.
|
kpeter@1022
|
51 |
/// The algorithm performs a certain number of iterations to find several
|
kpeter@1022
|
52 |
/// cliques and selects the largest one among them. Various limits can be
|
kpeter@1022
|
53 |
/// specified to control the running time and the effectiveness of the
|
kpeter@1022
|
54 |
/// search process.
|
kpeter@999
|
55 |
///
|
kpeter@999
|
56 |
/// \tparam GR The undirected graph type the algorithm runs on.
|
kpeter@999
|
57 |
///
|
kpeter@999
|
58 |
/// \note %GrossoLocatelliPullanMc provides three different node selection
|
kpeter@999
|
59 |
/// rules, from which the most powerful one is used by default.
|
kpeter@999
|
60 |
/// For more information, see \ref SelectionRule.
|
kpeter@999
|
61 |
template <typename GR>
|
kpeter@999
|
62 |
class GrossoLocatelliPullanMc
|
kpeter@999
|
63 |
{
|
kpeter@999
|
64 |
public:
|
kpeter@999
|
65 |
|
kpeter@999
|
66 |
/// \brief Constants for specifying the node selection rule.
|
kpeter@999
|
67 |
///
|
kpeter@999
|
68 |
/// Enum type containing constants for specifying the node selection rule
|
kpeter@999
|
69 |
/// for the \ref run() function.
|
kpeter@999
|
70 |
///
|
kpeter@999
|
71 |
/// During the algorithm, nodes are selected for addition to the current
|
kpeter@999
|
72 |
/// clique according to the applied rule.
|
kpeter@999
|
73 |
/// In general, the PENALTY_BASED rule turned out to be the most powerful
|
kpeter@999
|
74 |
/// and the most robust, thus it is the default option.
|
kpeter@999
|
75 |
/// However, another selection rule can be specified using the \ref run()
|
kpeter@999
|
76 |
/// function with the proper parameter.
|
kpeter@999
|
77 |
enum SelectionRule {
|
kpeter@999
|
78 |
|
kpeter@999
|
79 |
/// A node is selected randomly without any evaluation at each step.
|
kpeter@999
|
80 |
RANDOM,
|
kpeter@999
|
81 |
|
kpeter@999
|
82 |
/// A node of maximum degree is selected randomly at each step.
|
kpeter@999
|
83 |
DEGREE_BASED,
|
kpeter@999
|
84 |
|
kpeter@999
|
85 |
/// A node of minimum penalty is selected randomly at each step.
|
kpeter@999
|
86 |
/// The node penalties are updated adaptively after each stage of the
|
kpeter@999
|
87 |
/// search process.
|
kpeter@999
|
88 |
PENALTY_BASED
|
kpeter@999
|
89 |
};
|
kpeter@999
|
90 |
|
kpeter@1022
|
91 |
/// \brief Constants for the causes of search termination.
|
kpeter@1022
|
92 |
///
|
kpeter@1022
|
93 |
/// Enum type containing constants for the different causes of search
|
kpeter@1022
|
94 |
/// termination. The \ref run() function returns one of these values.
|
kpeter@1022
|
95 |
enum TerminationCause {
|
kpeter@1022
|
96 |
|
kpeter@1022
|
97 |
/// The iteration count limit is reached.
|
kpeter@1022
|
98 |
ITERATION_LIMIT,
|
kpeter@1022
|
99 |
|
kpeter@1022
|
100 |
/// The step count limit is reached.
|
kpeter@1022
|
101 |
STEP_LIMIT,
|
kpeter@1022
|
102 |
|
kpeter@1022
|
103 |
/// The clique size limit is reached.
|
kpeter@1022
|
104 |
SIZE_LIMIT
|
kpeter@1022
|
105 |
};
|
kpeter@1022
|
106 |
|
kpeter@999
|
107 |
private:
|
kpeter@999
|
108 |
|
kpeter@999
|
109 |
TEMPLATE_GRAPH_TYPEDEFS(GR);
|
kpeter@999
|
110 |
|
kpeter@999
|
111 |
typedef std::vector<int> IntVector;
|
kpeter@999
|
112 |
typedef std::vector<char> BoolVector;
|
kpeter@999
|
113 |
typedef std::vector<BoolVector> BoolMatrix;
|
kpeter@999
|
114 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons
|
kpeter@999
|
115 |
|
kpeter@1022
|
116 |
// The underlying graph
|
kpeter@999
|
117 |
const GR &_graph;
|
kpeter@999
|
118 |
IntNodeMap _id;
|
kpeter@999
|
119 |
|
kpeter@999
|
120 |
// Internal matrix representation of the graph
|
kpeter@999
|
121 |
BoolMatrix _gr;
|
kpeter@999
|
122 |
int _n;
|
alpar@1270
|
123 |
|
kpeter@1022
|
124 |
// Search options
|
kpeter@1022
|
125 |
bool _delta_based_restart;
|
kpeter@1022
|
126 |
int _restart_delta_limit;
|
alpar@1270
|
127 |
|
kpeter@1022
|
128 |
// Search limits
|
kpeter@1022
|
129 |
int _iteration_limit;
|
kpeter@1022
|
130 |
int _step_limit;
|
kpeter@1022
|
131 |
int _size_limit;
|
kpeter@999
|
132 |
|
kpeter@999
|
133 |
// The current clique
|
kpeter@999
|
134 |
BoolVector _clique;
|
kpeter@999
|
135 |
int _size;
|
kpeter@999
|
136 |
|
kpeter@999
|
137 |
// The best clique found so far
|
kpeter@999
|
138 |
BoolVector _best_clique;
|
kpeter@999
|
139 |
int _best_size;
|
kpeter@999
|
140 |
|
kpeter@999
|
141 |
// The "distances" of the nodes from the current clique.
|
kpeter@999
|
142 |
// _delta[u] is the number of nodes in the clique that are
|
kpeter@999
|
143 |
// not connected with u.
|
kpeter@999
|
144 |
IntVector _delta;
|
kpeter@999
|
145 |
|
kpeter@999
|
146 |
// The current tabu set
|
kpeter@999
|
147 |
BoolVector _tabu;
|
kpeter@999
|
148 |
|
kpeter@999
|
149 |
// Random number generator
|
kpeter@999
|
150 |
Random _rnd;
|
kpeter@999
|
151 |
|
kpeter@999
|
152 |
private:
|
kpeter@999
|
153 |
|
kpeter@999
|
154 |
// Implementation of the RANDOM node selection rule.
|
kpeter@999
|
155 |
class RandomSelectionRule
|
kpeter@999
|
156 |
{
|
kpeter@999
|
157 |
private:
|
kpeter@999
|
158 |
|
kpeter@999
|
159 |
// References to the algorithm instance
|
kpeter@999
|
160 |
const BoolVector &_clique;
|
kpeter@999
|
161 |
const IntVector &_delta;
|
kpeter@999
|
162 |
const BoolVector &_tabu;
|
kpeter@999
|
163 |
Random &_rnd;
|
kpeter@999
|
164 |
|
kpeter@999
|
165 |
// Pivot rule data
|
kpeter@999
|
166 |
int _n;
|
kpeter@999
|
167 |
|
kpeter@999
|
168 |
public:
|
kpeter@999
|
169 |
|
kpeter@999
|
170 |
// Constructor
|
kpeter@999
|
171 |
RandomSelectionRule(GrossoLocatelliPullanMc &mc) :
|
kpeter@999
|
172 |
_clique(mc._clique), _delta(mc._delta), _tabu(mc._tabu),
|
kpeter@999
|
173 |
_rnd(mc._rnd), _n(mc._n)
|
kpeter@999
|
174 |
{}
|
kpeter@999
|
175 |
|
kpeter@999
|
176 |
// Return a node index for a feasible add move or -1 if no one exists
|
kpeter@999
|
177 |
int nextFeasibleAddNode() const {
|
kpeter@999
|
178 |
int start_node = _rnd[_n];
|
kpeter@999
|
179 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
180 |
if (_delta[i] == 0 && !_tabu[i]) return i;
|
kpeter@999
|
181 |
}
|
kpeter@999
|
182 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
183 |
if (_delta[i] == 0 && !_tabu[i]) return i;
|
kpeter@999
|
184 |
}
|
kpeter@999
|
185 |
return -1;
|
kpeter@999
|
186 |
}
|
kpeter@999
|
187 |
|
kpeter@999
|
188 |
// Return a node index for a feasible swap move or -1 if no one exists
|
kpeter@999
|
189 |
int nextFeasibleSwapNode() const {
|
kpeter@999
|
190 |
int start_node = _rnd[_n];
|
kpeter@999
|
191 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
192 |
if (!_clique[i] && _delta[i] == 1 && !_tabu[i]) return i;
|
kpeter@999
|
193 |
}
|
kpeter@999
|
194 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
195 |
if (!_clique[i] && _delta[i] == 1 && !_tabu[i]) return i;
|
kpeter@999
|
196 |
}
|
kpeter@999
|
197 |
return -1;
|
kpeter@999
|
198 |
}
|
kpeter@999
|
199 |
|
kpeter@999
|
200 |
// Return a node index for an add move or -1 if no one exists
|
kpeter@999
|
201 |
int nextAddNode() const {
|
kpeter@999
|
202 |
int start_node = _rnd[_n];
|
kpeter@999
|
203 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
204 |
if (_delta[i] == 0) return i;
|
kpeter@999
|
205 |
}
|
kpeter@999
|
206 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
207 |
if (_delta[i] == 0) return i;
|
kpeter@999
|
208 |
}
|
kpeter@999
|
209 |
return -1;
|
kpeter@999
|
210 |
}
|
kpeter@999
|
211 |
|
kpeter@999
|
212 |
// Update internal data structures between stages (if necessary)
|
kpeter@999
|
213 |
void update() {}
|
kpeter@999
|
214 |
|
kpeter@999
|
215 |
}; //class RandomSelectionRule
|
kpeter@999
|
216 |
|
kpeter@999
|
217 |
|
kpeter@999
|
218 |
// Implementation of the DEGREE_BASED node selection rule.
|
kpeter@999
|
219 |
class DegreeBasedSelectionRule
|
kpeter@999
|
220 |
{
|
kpeter@999
|
221 |
private:
|
kpeter@999
|
222 |
|
kpeter@999
|
223 |
// References to the algorithm instance
|
kpeter@999
|
224 |
const BoolVector &_clique;
|
kpeter@999
|
225 |
const IntVector &_delta;
|
kpeter@999
|
226 |
const BoolVector &_tabu;
|
kpeter@999
|
227 |
Random &_rnd;
|
kpeter@999
|
228 |
|
kpeter@999
|
229 |
// Pivot rule data
|
kpeter@999
|
230 |
int _n;
|
kpeter@999
|
231 |
IntVector _deg;
|
kpeter@999
|
232 |
|
kpeter@999
|
233 |
public:
|
kpeter@999
|
234 |
|
kpeter@999
|
235 |
// Constructor
|
kpeter@999
|
236 |
DegreeBasedSelectionRule(GrossoLocatelliPullanMc &mc) :
|
kpeter@999
|
237 |
_clique(mc._clique), _delta(mc._delta), _tabu(mc._tabu),
|
kpeter@999
|
238 |
_rnd(mc._rnd), _n(mc._n), _deg(_n)
|
kpeter@999
|
239 |
{
|
kpeter@999
|
240 |
for (int i = 0; i != _n; i++) {
|
kpeter@999
|
241 |
int d = 0;
|
kpeter@999
|
242 |
BoolVector &row = mc._gr[i];
|
kpeter@999
|
243 |
for (int j = 0; j != _n; j++) {
|
kpeter@999
|
244 |
if (row[j]) d++;
|
kpeter@999
|
245 |
}
|
kpeter@999
|
246 |
_deg[i] = d;
|
kpeter@999
|
247 |
}
|
kpeter@999
|
248 |
}
|
kpeter@999
|
249 |
|
kpeter@999
|
250 |
// Return a node index for a feasible add move or -1 if no one exists
|
kpeter@999
|
251 |
int nextFeasibleAddNode() const {
|
kpeter@999
|
252 |
int start_node = _rnd[_n];
|
kpeter@999
|
253 |
int node = -1, max_deg = -1;
|
kpeter@999
|
254 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
255 |
if (_delta[i] == 0 && !_tabu[i] && _deg[i] > max_deg) {
|
kpeter@999
|
256 |
node = i;
|
kpeter@999
|
257 |
max_deg = _deg[i];
|
kpeter@999
|
258 |
}
|
kpeter@999
|
259 |
}
|
kpeter@999
|
260 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
261 |
if (_delta[i] == 0 && !_tabu[i] && _deg[i] > max_deg) {
|
kpeter@999
|
262 |
node = i;
|
kpeter@999
|
263 |
max_deg = _deg[i];
|
kpeter@999
|
264 |
}
|
kpeter@999
|
265 |
}
|
kpeter@999
|
266 |
return node;
|
kpeter@999
|
267 |
}
|
kpeter@999
|
268 |
|
kpeter@999
|
269 |
// Return a node index for a feasible swap move or -1 if no one exists
|
kpeter@999
|
270 |
int nextFeasibleSwapNode() const {
|
kpeter@999
|
271 |
int start_node = _rnd[_n];
|
kpeter@999
|
272 |
int node = -1, max_deg = -1;
|
kpeter@999
|
273 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
274 |
if (!_clique[i] && _delta[i] == 1 && !_tabu[i] &&
|
kpeter@999
|
275 |
_deg[i] > max_deg) {
|
kpeter@999
|
276 |
node = i;
|
kpeter@999
|
277 |
max_deg = _deg[i];
|
kpeter@999
|
278 |
}
|
kpeter@999
|
279 |
}
|
kpeter@999
|
280 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
281 |
if (!_clique[i] && _delta[i] == 1 && !_tabu[i] &&
|
kpeter@999
|
282 |
_deg[i] > max_deg) {
|
kpeter@999
|
283 |
node = i;
|
kpeter@999
|
284 |
max_deg = _deg[i];
|
kpeter@999
|
285 |
}
|
kpeter@999
|
286 |
}
|
kpeter@999
|
287 |
return node;
|
kpeter@999
|
288 |
}
|
kpeter@999
|
289 |
|
kpeter@999
|
290 |
// Return a node index for an add move or -1 if no one exists
|
kpeter@999
|
291 |
int nextAddNode() const {
|
kpeter@999
|
292 |
int start_node = _rnd[_n];
|
kpeter@999
|
293 |
int node = -1, max_deg = -1;
|
kpeter@999
|
294 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
295 |
if (_delta[i] == 0 && _deg[i] > max_deg) {
|
kpeter@999
|
296 |
node = i;
|
kpeter@999
|
297 |
max_deg = _deg[i];
|
kpeter@999
|
298 |
}
|
kpeter@999
|
299 |
}
|
kpeter@999
|
300 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
301 |
if (_delta[i] == 0 && _deg[i] > max_deg) {
|
kpeter@999
|
302 |
node = i;
|
kpeter@999
|
303 |
max_deg = _deg[i];
|
kpeter@999
|
304 |
}
|
kpeter@999
|
305 |
}
|
kpeter@999
|
306 |
return node;
|
kpeter@999
|
307 |
}
|
kpeter@999
|
308 |
|
kpeter@999
|
309 |
// Update internal data structures between stages (if necessary)
|
kpeter@999
|
310 |
void update() {}
|
kpeter@999
|
311 |
|
kpeter@999
|
312 |
}; //class DegreeBasedSelectionRule
|
kpeter@999
|
313 |
|
kpeter@999
|
314 |
|
kpeter@999
|
315 |
// Implementation of the PENALTY_BASED node selection rule.
|
kpeter@999
|
316 |
class PenaltyBasedSelectionRule
|
kpeter@999
|
317 |
{
|
kpeter@999
|
318 |
private:
|
kpeter@999
|
319 |
|
kpeter@999
|
320 |
// References to the algorithm instance
|
kpeter@999
|
321 |
const BoolVector &_clique;
|
kpeter@999
|
322 |
const IntVector &_delta;
|
kpeter@999
|
323 |
const BoolVector &_tabu;
|
kpeter@999
|
324 |
Random &_rnd;
|
kpeter@999
|
325 |
|
kpeter@999
|
326 |
// Pivot rule data
|
kpeter@999
|
327 |
int _n;
|
kpeter@999
|
328 |
IntVector _penalty;
|
kpeter@999
|
329 |
|
kpeter@999
|
330 |
public:
|
kpeter@999
|
331 |
|
kpeter@999
|
332 |
// Constructor
|
kpeter@999
|
333 |
PenaltyBasedSelectionRule(GrossoLocatelliPullanMc &mc) :
|
kpeter@999
|
334 |
_clique(mc._clique), _delta(mc._delta), _tabu(mc._tabu),
|
kpeter@999
|
335 |
_rnd(mc._rnd), _n(mc._n), _penalty(_n, 0)
|
kpeter@999
|
336 |
{}
|
kpeter@999
|
337 |
|
kpeter@999
|
338 |
// Return a node index for a feasible add move or -1 if no one exists
|
kpeter@999
|
339 |
int nextFeasibleAddNode() const {
|
kpeter@999
|
340 |
int start_node = _rnd[_n];
|
kpeter@999
|
341 |
int node = -1, min_p = std::numeric_limits<int>::max();
|
kpeter@999
|
342 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
343 |
if (_delta[i] == 0 && !_tabu[i] && _penalty[i] < min_p) {
|
kpeter@999
|
344 |
node = i;
|
kpeter@999
|
345 |
min_p = _penalty[i];
|
kpeter@999
|
346 |
}
|
kpeter@999
|
347 |
}
|
kpeter@999
|
348 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
349 |
if (_delta[i] == 0 && !_tabu[i] && _penalty[i] < min_p) {
|
kpeter@999
|
350 |
node = i;
|
kpeter@999
|
351 |
min_p = _penalty[i];
|
kpeter@999
|
352 |
}
|
kpeter@999
|
353 |
}
|
kpeter@999
|
354 |
return node;
|
kpeter@999
|
355 |
}
|
kpeter@999
|
356 |
|
kpeter@999
|
357 |
// Return a node index for a feasible swap move or -1 if no one exists
|
kpeter@999
|
358 |
int nextFeasibleSwapNode() const {
|
kpeter@999
|
359 |
int start_node = _rnd[_n];
|
kpeter@999
|
360 |
int node = -1, min_p = std::numeric_limits<int>::max();
|
kpeter@999
|
361 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
362 |
if (!_clique[i] && _delta[i] == 1 && !_tabu[i] &&
|
kpeter@999
|
363 |
_penalty[i] < min_p) {
|
kpeter@999
|
364 |
node = i;
|
kpeter@999
|
365 |
min_p = _penalty[i];
|
kpeter@999
|
366 |
}
|
kpeter@999
|
367 |
}
|
kpeter@999
|
368 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
369 |
if (!_clique[i] && _delta[i] == 1 && !_tabu[i] &&
|
kpeter@999
|
370 |
_penalty[i] < min_p) {
|
kpeter@999
|
371 |
node = i;
|
kpeter@999
|
372 |
min_p = _penalty[i];
|
kpeter@999
|
373 |
}
|
kpeter@999
|
374 |
}
|
kpeter@999
|
375 |
return node;
|
kpeter@999
|
376 |
}
|
kpeter@999
|
377 |
|
kpeter@999
|
378 |
// Return a node index for an add move or -1 if no one exists
|
kpeter@999
|
379 |
int nextAddNode() const {
|
kpeter@999
|
380 |
int start_node = _rnd[_n];
|
kpeter@999
|
381 |
int node = -1, min_p = std::numeric_limits<int>::max();
|
kpeter@999
|
382 |
for (int i = start_node; i != _n; i++) {
|
kpeter@999
|
383 |
if (_delta[i] == 0 && _penalty[i] < min_p) {
|
kpeter@999
|
384 |
node = i;
|
kpeter@999
|
385 |
min_p = _penalty[i];
|
kpeter@999
|
386 |
}
|
kpeter@999
|
387 |
}
|
kpeter@999
|
388 |
for (int i = 0; i != start_node; i++) {
|
kpeter@999
|
389 |
if (_delta[i] == 0 && _penalty[i] < min_p) {
|
kpeter@999
|
390 |
node = i;
|
kpeter@999
|
391 |
min_p = _penalty[i];
|
kpeter@999
|
392 |
}
|
kpeter@999
|
393 |
}
|
kpeter@999
|
394 |
return node;
|
kpeter@999
|
395 |
}
|
kpeter@999
|
396 |
|
kpeter@999
|
397 |
// Update internal data structures between stages (if necessary)
|
kpeter@999
|
398 |
void update() {}
|
kpeter@999
|
399 |
|
kpeter@999
|
400 |
}; //class PenaltyBasedSelectionRule
|
kpeter@999
|
401 |
|
kpeter@999
|
402 |
public:
|
kpeter@999
|
403 |
|
kpeter@999
|
404 |
/// \brief Constructor.
|
kpeter@999
|
405 |
///
|
kpeter@999
|
406 |
/// Constructor.
|
kpeter@999
|
407 |
/// The global \ref rnd "random number generator instance" is used
|
kpeter@999
|
408 |
/// during the algorithm.
|
kpeter@999
|
409 |
///
|
kpeter@999
|
410 |
/// \param graph The undirected graph the algorithm runs on.
|
kpeter@999
|
411 |
GrossoLocatelliPullanMc(const GR& graph) :
|
kpeter@999
|
412 |
_graph(graph), _id(_graph), _rnd(rnd)
|
kpeter@1022
|
413 |
{
|
kpeter@1022
|
414 |
initOptions();
|
kpeter@1022
|
415 |
}
|
kpeter@999
|
416 |
|
kpeter@999
|
417 |
/// \brief Constructor with random seed.
|
kpeter@999
|
418 |
///
|
kpeter@999
|
419 |
/// Constructor with random seed.
|
kpeter@999
|
420 |
///
|
kpeter@999
|
421 |
/// \param graph The undirected graph the algorithm runs on.
|
kpeter@999
|
422 |
/// \param seed Seed value for the internal random number generator
|
kpeter@999
|
423 |
/// that is used during the algorithm.
|
kpeter@999
|
424 |
GrossoLocatelliPullanMc(const GR& graph, int seed) :
|
kpeter@999
|
425 |
_graph(graph), _id(_graph), _rnd(seed)
|
kpeter@1022
|
426 |
{
|
kpeter@1022
|
427 |
initOptions();
|
kpeter@1022
|
428 |
}
|
kpeter@999
|
429 |
|
kpeter@999
|
430 |
/// \brief Constructor with random number generator.
|
kpeter@999
|
431 |
///
|
kpeter@999
|
432 |
/// Constructor with random number generator.
|
kpeter@999
|
433 |
///
|
kpeter@999
|
434 |
/// \param graph The undirected graph the algorithm runs on.
|
kpeter@999
|
435 |
/// \param random A random number generator that is used during the
|
kpeter@999
|
436 |
/// algorithm.
|
kpeter@999
|
437 |
GrossoLocatelliPullanMc(const GR& graph, const Random& random) :
|
kpeter@999
|
438 |
_graph(graph), _id(_graph), _rnd(random)
|
kpeter@1022
|
439 |
{
|
kpeter@1022
|
440 |
initOptions();
|
kpeter@1022
|
441 |
}
|
kpeter@999
|
442 |
|
kpeter@999
|
443 |
/// \name Execution Control
|
kpeter@1022
|
444 |
/// The \ref run() function can be used to execute the algorithm.\n
|
alpar@1270
|
445 |
/// The functions \ref iterationLimit(int), \ref stepLimit(int), and
|
kpeter@1022
|
446 |
/// \ref sizeLimit(int) can be used to specify various limits for the
|
kpeter@1022
|
447 |
/// search process.
|
alpar@1270
|
448 |
|
kpeter@999
|
449 |
/// @{
|
alpar@1270
|
450 |
|
kpeter@1022
|
451 |
/// \brief Sets the maximum number of iterations.
|
kpeter@1022
|
452 |
///
|
kpeter@1022
|
453 |
/// This function sets the maximum number of iterations.
|
kpeter@1022
|
454 |
/// Each iteration of the algorithm finds a maximal clique (but not
|
kpeter@1022
|
455 |
/// necessarily the largest one) by performing several search steps
|
kpeter@1022
|
456 |
/// (node selections).
|
kpeter@1022
|
457 |
///
|
kpeter@1022
|
458 |
/// This limit controls the running time and the success of the
|
kpeter@1022
|
459 |
/// algorithm. For larger values, the algorithm runs slower, but it more
|
kpeter@1022
|
460 |
/// likely finds larger cliques. For smaller values, the algorithm is
|
kpeter@1022
|
461 |
/// faster but probably gives worse results.
|
alpar@1270
|
462 |
///
|
kpeter@1022
|
463 |
/// The default value is \c 1000.
|
kpeter@1022
|
464 |
/// \c -1 means that number of iterations is not limited.
|
kpeter@1022
|
465 |
///
|
kpeter@1022
|
466 |
/// \warning You should specify a reasonable limit for the number of
|
kpeter@1022
|
467 |
/// iterations and/or the number of search steps.
|
kpeter@1022
|
468 |
///
|
kpeter@1022
|
469 |
/// \return <tt>(*this)</tt>
|
kpeter@1022
|
470 |
///
|
kpeter@1022
|
471 |
/// \sa stepLimit(int)
|
kpeter@1022
|
472 |
/// \sa sizeLimit(int)
|
kpeter@1022
|
473 |
GrossoLocatelliPullanMc& iterationLimit(int limit) {
|
kpeter@1022
|
474 |
_iteration_limit = limit;
|
kpeter@1022
|
475 |
return *this;
|
kpeter@1022
|
476 |
}
|
alpar@1270
|
477 |
|
kpeter@1022
|
478 |
/// \brief Sets the maximum number of search steps.
|
kpeter@1022
|
479 |
///
|
kpeter@1022
|
480 |
/// This function sets the maximum number of elementary search steps.
|
kpeter@1022
|
481 |
/// Each iteration of the algorithm finds a maximal clique (but not
|
kpeter@1022
|
482 |
/// necessarily the largest one) by performing several search steps
|
kpeter@1022
|
483 |
/// (node selections).
|
kpeter@1022
|
484 |
///
|
kpeter@1022
|
485 |
/// This limit controls the running time and the success of the
|
kpeter@1022
|
486 |
/// algorithm. For larger values, the algorithm runs slower, but it more
|
kpeter@1022
|
487 |
/// likely finds larger cliques. For smaller values, the algorithm is
|
kpeter@1022
|
488 |
/// faster but probably gives worse results.
|
alpar@1270
|
489 |
///
|
kpeter@1022
|
490 |
/// The default value is \c -1, which means that number of steps
|
kpeter@1022
|
491 |
/// is not limited explicitly. However, the number of iterations is
|
kpeter@1022
|
492 |
/// limited and each iteration performs a finite number of search steps.
|
kpeter@1022
|
493 |
///
|
kpeter@1022
|
494 |
/// \warning You should specify a reasonable limit for the number of
|
kpeter@1022
|
495 |
/// iterations and/or the number of search steps.
|
kpeter@1022
|
496 |
///
|
kpeter@1022
|
497 |
/// \return <tt>(*this)</tt>
|
kpeter@1022
|
498 |
///
|
kpeter@1022
|
499 |
/// \sa iterationLimit(int)
|
kpeter@1022
|
500 |
/// \sa sizeLimit(int)
|
kpeter@1022
|
501 |
GrossoLocatelliPullanMc& stepLimit(int limit) {
|
kpeter@1022
|
502 |
_step_limit = limit;
|
kpeter@1022
|
503 |
return *this;
|
kpeter@1022
|
504 |
}
|
alpar@1270
|
505 |
|
kpeter@1022
|
506 |
/// \brief Sets the desired clique size.
|
kpeter@1022
|
507 |
///
|
kpeter@1022
|
508 |
/// This function sets the desired clique size that serves as a search
|
kpeter@1022
|
509 |
/// limit. If a clique of this size (or a larger one) is found, then the
|
kpeter@1022
|
510 |
/// algorithm terminates.
|
alpar@1270
|
511 |
///
|
kpeter@1022
|
512 |
/// This function is especially useful if you know an exact upper bound
|
alpar@1270
|
513 |
/// for the size of the cliques in the graph or if any clique above
|
kpeter@1022
|
514 |
/// a certain size limit is sufficient for your application.
|
alpar@1270
|
515 |
///
|
kpeter@1022
|
516 |
/// The default value is \c -1, which means that the size limit is set to
|
kpeter@1022
|
517 |
/// the number of nodes in the graph.
|
kpeter@1022
|
518 |
///
|
kpeter@1022
|
519 |
/// \return <tt>(*this)</tt>
|
kpeter@1022
|
520 |
///
|
kpeter@1022
|
521 |
/// \sa iterationLimit(int)
|
kpeter@1022
|
522 |
/// \sa stepLimit(int)
|
kpeter@1022
|
523 |
GrossoLocatelliPullanMc& sizeLimit(int limit) {
|
kpeter@1022
|
524 |
_size_limit = limit;
|
kpeter@1022
|
525 |
return *this;
|
kpeter@1022
|
526 |
}
|
alpar@1270
|
527 |
|
kpeter@1022
|
528 |
/// \brief The maximum number of iterations.
|
kpeter@1022
|
529 |
///
|
kpeter@1022
|
530 |
/// This function gives back the maximum number of iterations.
|
kpeter@1022
|
531 |
/// \c -1 means that no limit is specified.
|
kpeter@1022
|
532 |
///
|
kpeter@1022
|
533 |
/// \sa iterationLimit(int)
|
kpeter@1022
|
534 |
int iterationLimit() const {
|
kpeter@1022
|
535 |
return _iteration_limit;
|
kpeter@1022
|
536 |
}
|
alpar@1270
|
537 |
|
kpeter@1022
|
538 |
/// \brief The maximum number of search steps.
|
kpeter@1022
|
539 |
///
|
kpeter@1022
|
540 |
/// This function gives back the maximum number of search steps.
|
kpeter@1022
|
541 |
/// \c -1 means that no limit is specified.
|
kpeter@1022
|
542 |
///
|
kpeter@1022
|
543 |
/// \sa stepLimit(int)
|
kpeter@1022
|
544 |
int stepLimit() const {
|
kpeter@1022
|
545 |
return _step_limit;
|
kpeter@1022
|
546 |
}
|
alpar@1270
|
547 |
|
kpeter@1022
|
548 |
/// \brief The desired clique size.
|
kpeter@1022
|
549 |
///
|
kpeter@1022
|
550 |
/// This function gives back the desired clique size that serves as a
|
kpeter@1022
|
551 |
/// search limit. \c -1 means that this limit is set to the number of
|
kpeter@1022
|
552 |
/// nodes in the graph.
|
kpeter@1022
|
553 |
///
|
kpeter@1022
|
554 |
/// \sa sizeLimit(int)
|
kpeter@1022
|
555 |
int sizeLimit() const {
|
kpeter@1022
|
556 |
return _size_limit;
|
kpeter@1022
|
557 |
}
|
kpeter@999
|
558 |
|
kpeter@999
|
559 |
/// \brief Runs the algorithm.
|
kpeter@999
|
560 |
///
|
kpeter@1022
|
561 |
/// This function runs the algorithm. If one of the specified limits
|
kpeter@1022
|
562 |
/// is reached, the search process terminates.
|
kpeter@999
|
563 |
///
|
kpeter@999
|
564 |
/// \param rule The node selection rule. For more information, see
|
kpeter@999
|
565 |
/// \ref SelectionRule.
|
kpeter@999
|
566 |
///
|
kpeter@1022
|
567 |
/// \return The termination cause of the search. For more information,
|
kpeter@1022
|
568 |
/// see \ref TerminationCause.
|
kpeter@1022
|
569 |
TerminationCause run(SelectionRule rule = PENALTY_BASED)
|
kpeter@999
|
570 |
{
|
kpeter@999
|
571 |
init();
|
kpeter@999
|
572 |
switch (rule) {
|
kpeter@999
|
573 |
case RANDOM:
|
kpeter@1022
|
574 |
return start<RandomSelectionRule>();
|
kpeter@999
|
575 |
case DEGREE_BASED:
|
kpeter@1022
|
576 |
return start<DegreeBasedSelectionRule>();
|
kpeter@1022
|
577 |
default:
|
kpeter@1022
|
578 |
return start<PenaltyBasedSelectionRule>();
|
kpeter@999
|
579 |
}
|
kpeter@999
|
580 |
}
|
kpeter@999
|
581 |
|
kpeter@999
|
582 |
/// @}
|
kpeter@999
|
583 |
|
kpeter@999
|
584 |
/// \name Query Functions
|
kpeter@1022
|
585 |
/// The results of the algorithm can be obtained using these functions.\n
|
alpar@1270
|
586 |
/// The run() function must be called before using them.
|
kpeter@1022
|
587 |
|
kpeter@999
|
588 |
/// @{
|
kpeter@999
|
589 |
|
kpeter@999
|
590 |
/// \brief The size of the found clique
|
kpeter@999
|
591 |
///
|
kpeter@999
|
592 |
/// This function returns the size of the found clique.
|
kpeter@999
|
593 |
///
|
kpeter@999
|
594 |
/// \pre run() must be called before using this function.
|
kpeter@999
|
595 |
int cliqueSize() const {
|
kpeter@999
|
596 |
return _best_size;
|
kpeter@999
|
597 |
}
|
kpeter@999
|
598 |
|
kpeter@999
|
599 |
/// \brief Gives back the found clique in a \c bool node map
|
kpeter@999
|
600 |
///
|
kpeter@999
|
601 |
/// This function gives back the characteristic vector of the found
|
kpeter@999
|
602 |
/// clique in the given node map.
|
kpeter@999
|
603 |
/// It must be a \ref concepts::WriteMap "writable" node map with
|
kpeter@999
|
604 |
/// \c bool (or convertible) value type.
|
kpeter@999
|
605 |
///
|
kpeter@999
|
606 |
/// \pre run() must be called before using this function.
|
kpeter@999
|
607 |
template <typename CliqueMap>
|
kpeter@999
|
608 |
void cliqueMap(CliqueMap &map) const {
|
kpeter@999
|
609 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@999
|
610 |
map[n] = static_cast<bool>(_best_clique[_id[n]]);
|
kpeter@999
|
611 |
}
|
kpeter@999
|
612 |
}
|
kpeter@999
|
613 |
|
kpeter@999
|
614 |
/// \brief Iterator to list the nodes of the found clique
|
kpeter@999
|
615 |
///
|
kpeter@999
|
616 |
/// This iterator class lists the nodes of the found clique.
|
kpeter@999
|
617 |
/// Before using it, you must allocate a GrossoLocatelliPullanMc instance
|
kpeter@999
|
618 |
/// and call its \ref GrossoLocatelliPullanMc::run() "run()" method.
|
kpeter@999
|
619 |
///
|
kpeter@999
|
620 |
/// The following example prints out the IDs of the nodes in the found
|
kpeter@999
|
621 |
/// clique.
|
kpeter@999
|
622 |
/// \code
|
kpeter@999
|
623 |
/// GrossoLocatelliPullanMc<Graph> mc(g);
|
kpeter@999
|
624 |
/// mc.run();
|
kpeter@999
|
625 |
/// for (GrossoLocatelliPullanMc<Graph>::CliqueNodeIt n(mc);
|
kpeter@999
|
626 |
/// n != INVALID; ++n)
|
kpeter@999
|
627 |
/// {
|
kpeter@999
|
628 |
/// std::cout << g.id(n) << std::endl;
|
kpeter@999
|
629 |
/// }
|
kpeter@999
|
630 |
/// \endcode
|
kpeter@999
|
631 |
class CliqueNodeIt
|
kpeter@999
|
632 |
{
|
kpeter@999
|
633 |
private:
|
kpeter@999
|
634 |
NodeIt _it;
|
kpeter@999
|
635 |
BoolNodeMap _map;
|
kpeter@999
|
636 |
|
kpeter@999
|
637 |
public:
|
kpeter@999
|
638 |
|
kpeter@999
|
639 |
/// Constructor
|
kpeter@999
|
640 |
|
kpeter@999
|
641 |
/// Constructor.
|
kpeter@999
|
642 |
/// \param mc The algorithm instance.
|
kpeter@999
|
643 |
CliqueNodeIt(const GrossoLocatelliPullanMc &mc)
|
kpeter@999
|
644 |
: _map(mc._graph)
|
kpeter@999
|
645 |
{
|
kpeter@999
|
646 |
mc.cliqueMap(_map);
|
kpeter@999
|
647 |
for (_it = NodeIt(mc._graph); _it != INVALID && !_map[_it]; ++_it) ;
|
kpeter@999
|
648 |
}
|
kpeter@999
|
649 |
|
kpeter@999
|
650 |
/// Conversion to \c Node
|
kpeter@999
|
651 |
operator Node() const { return _it; }
|
kpeter@999
|
652 |
|
kpeter@999
|
653 |
bool operator==(Invalid) const { return _it == INVALID; }
|
kpeter@999
|
654 |
bool operator!=(Invalid) const { return _it != INVALID; }
|
kpeter@999
|
655 |
|
kpeter@999
|
656 |
/// Next node
|
kpeter@999
|
657 |
CliqueNodeIt &operator++() {
|
kpeter@999
|
658 |
for (++_it; _it != INVALID && !_map[_it]; ++_it) ;
|
kpeter@999
|
659 |
return *this;
|
kpeter@999
|
660 |
}
|
kpeter@999
|
661 |
|
kpeter@999
|
662 |
/// Postfix incrementation
|
kpeter@999
|
663 |
|
kpeter@999
|
664 |
/// Postfix incrementation.
|
kpeter@999
|
665 |
///
|
kpeter@999
|
666 |
/// \warning This incrementation returns a \c Node, not a
|
kpeter@999
|
667 |
/// \c CliqueNodeIt as one may expect.
|
kpeter@999
|
668 |
typename GR::Node operator++(int) {
|
kpeter@999
|
669 |
Node n=*this;
|
kpeter@999
|
670 |
++(*this);
|
kpeter@999
|
671 |
return n;
|
kpeter@999
|
672 |
}
|
kpeter@999
|
673 |
|
kpeter@999
|
674 |
};
|
kpeter@999
|
675 |
|
kpeter@999
|
676 |
/// @}
|
kpeter@999
|
677 |
|
kpeter@999
|
678 |
private:
|
alpar@1270
|
679 |
|
kpeter@1022
|
680 |
// Initialize search options and limits
|
kpeter@1022
|
681 |
void initOptions() {
|
kpeter@1022
|
682 |
// Search options
|
kpeter@1022
|
683 |
_delta_based_restart = true;
|
kpeter@1022
|
684 |
_restart_delta_limit = 4;
|
alpar@1270
|
685 |
|
kpeter@1022
|
686 |
// Search limits
|
kpeter@1022
|
687 |
_iteration_limit = 1000;
|
kpeter@1022
|
688 |
_step_limit = -1; // this is disabled by default
|
kpeter@1022
|
689 |
_size_limit = -1; // this is disabled by default
|
kpeter@1022
|
690 |
}
|
kpeter@999
|
691 |
|
kpeter@999
|
692 |
// Adds a node to the current clique
|
kpeter@999
|
693 |
void addCliqueNode(int u) {
|
kpeter@999
|
694 |
if (_clique[u]) return;
|
kpeter@999
|
695 |
_clique[u] = true;
|
kpeter@999
|
696 |
_size++;
|
kpeter@999
|
697 |
BoolVector &row = _gr[u];
|
kpeter@999
|
698 |
for (int i = 0; i != _n; i++) {
|
kpeter@999
|
699 |
if (!row[i]) _delta[i]++;
|
kpeter@999
|
700 |
}
|
kpeter@999
|
701 |
}
|
kpeter@999
|
702 |
|
kpeter@999
|
703 |
// Removes a node from the current clique
|
kpeter@999
|
704 |
void delCliqueNode(int u) {
|
kpeter@999
|
705 |
if (!_clique[u]) return;
|
kpeter@999
|
706 |
_clique[u] = false;
|
kpeter@999
|
707 |
_size--;
|
kpeter@999
|
708 |
BoolVector &row = _gr[u];
|
kpeter@999
|
709 |
for (int i = 0; i != _n; i++) {
|
kpeter@999
|
710 |
if (!row[i]) _delta[i]--;
|
kpeter@999
|
711 |
}
|
kpeter@999
|
712 |
}
|
kpeter@999
|
713 |
|
kpeter@999
|
714 |
// Initialize data structures
|
kpeter@999
|
715 |
void init() {
|
kpeter@999
|
716 |
_n = countNodes(_graph);
|
kpeter@999
|
717 |
int ui = 0;
|
kpeter@999
|
718 |
for (NodeIt u(_graph); u != INVALID; ++u) {
|
kpeter@999
|
719 |
_id[u] = ui++;
|
kpeter@999
|
720 |
}
|
kpeter@999
|
721 |
_gr.clear();
|
kpeter@999
|
722 |
_gr.resize(_n, BoolVector(_n, false));
|
kpeter@999
|
723 |
ui = 0;
|
kpeter@999
|
724 |
for (NodeIt u(_graph); u != INVALID; ++u) {
|
kpeter@999
|
725 |
for (IncEdgeIt e(_graph, u); e != INVALID; ++e) {
|
kpeter@999
|
726 |
int vi = _id[_graph.runningNode(e)];
|
kpeter@999
|
727 |
_gr[ui][vi] = true;
|
kpeter@999
|
728 |
_gr[vi][ui] = true;
|
kpeter@999
|
729 |
}
|
kpeter@999
|
730 |
++ui;
|
kpeter@999
|
731 |
}
|
kpeter@999
|
732 |
|
kpeter@999
|
733 |
_clique.clear();
|
kpeter@999
|
734 |
_clique.resize(_n, false);
|
kpeter@999
|
735 |
_size = 0;
|
kpeter@999
|
736 |
_best_clique.clear();
|
kpeter@999
|
737 |
_best_clique.resize(_n, false);
|
kpeter@999
|
738 |
_best_size = 0;
|
kpeter@999
|
739 |
_delta.clear();
|
kpeter@999
|
740 |
_delta.resize(_n, 0);
|
kpeter@999
|
741 |
_tabu.clear();
|
kpeter@999
|
742 |
_tabu.resize(_n, false);
|
kpeter@999
|
743 |
}
|
kpeter@999
|
744 |
|
kpeter@999
|
745 |
// Executes the algorithm
|
kpeter@999
|
746 |
template <typename SelectionRuleImpl>
|
kpeter@1022
|
747 |
TerminationCause start() {
|
kpeter@1022
|
748 |
if (_n == 0) return SIZE_LIMIT;
|
kpeter@999
|
749 |
if (_n == 1) {
|
kpeter@999
|
750 |
_best_clique[0] = true;
|
kpeter@999
|
751 |
_best_size = 1;
|
kpeter@1022
|
752 |
return SIZE_LIMIT;
|
kpeter@999
|
753 |
}
|
kpeter@999
|
754 |
|
kpeter@1022
|
755 |
// Iterated local search algorithm
|
kpeter@1022
|
756 |
const int max_size = _size_limit >= 0 ? _size_limit : _n;
|
kpeter@1022
|
757 |
const int max_restart = _iteration_limit >= 0 ?
|
kpeter@1022
|
758 |
_iteration_limit : std::numeric_limits<int>::max();
|
kpeter@1022
|
759 |
const int max_select = _step_limit >= 0 ?
|
kpeter@1022
|
760 |
_step_limit : std::numeric_limits<int>::max();
|
kpeter@1022
|
761 |
|
kpeter@999
|
762 |
SelectionRuleImpl sel_method(*this);
|
kpeter@1022
|
763 |
int select = 0, restart = 0;
|
kpeter@999
|
764 |
IntVector restart_nodes;
|
kpeter@1022
|
765 |
while (select < max_select && restart < max_restart) {
|
kpeter@999
|
766 |
|
kpeter@999
|
767 |
// Perturbation/restart
|
kpeter@1022
|
768 |
restart++;
|
kpeter@1022
|
769 |
if (_delta_based_restart) {
|
kpeter@999
|
770 |
restart_nodes.clear();
|
kpeter@999
|
771 |
for (int i = 0; i != _n; i++) {
|
kpeter@1022
|
772 |
if (_delta[i] >= _restart_delta_limit)
|
kpeter@999
|
773 |
restart_nodes.push_back(i);
|
kpeter@999
|
774 |
}
|
kpeter@999
|
775 |
}
|
kpeter@999
|
776 |
int rs_node = -1;
|
kpeter@999
|
777 |
if (restart_nodes.size() > 0) {
|
kpeter@999
|
778 |
rs_node = restart_nodes[_rnd[restart_nodes.size()]];
|
kpeter@999
|
779 |
} else {
|
kpeter@999
|
780 |
rs_node = _rnd[_n];
|
kpeter@999
|
781 |
}
|
kpeter@999
|
782 |
BoolVector &row = _gr[rs_node];
|
kpeter@999
|
783 |
for (int i = 0; i != _n; i++) {
|
kpeter@999
|
784 |
if (_clique[i] && !row[i]) delCliqueNode(i);
|
kpeter@999
|
785 |
}
|
kpeter@999
|
786 |
addCliqueNode(rs_node);
|
kpeter@999
|
787 |
|
kpeter@999
|
788 |
// Local search
|
kpeter@999
|
789 |
_tabu.clear();
|
kpeter@999
|
790 |
_tabu.resize(_n, false);
|
kpeter@999
|
791 |
bool tabu_empty = true;
|
kpeter@999
|
792 |
int max_swap = _size;
|
kpeter@999
|
793 |
while (select < max_select) {
|
kpeter@999
|
794 |
select++;
|
kpeter@999
|
795 |
int u;
|
kpeter@999
|
796 |
if ((u = sel_method.nextFeasibleAddNode()) != -1) {
|
kpeter@999
|
797 |
// Feasible add move
|
kpeter@999
|
798 |
addCliqueNode(u);
|
kpeter@999
|
799 |
if (tabu_empty) max_swap = _size;
|
kpeter@999
|
800 |
}
|
kpeter@999
|
801 |
else if ((u = sel_method.nextFeasibleSwapNode()) != -1) {
|
kpeter@999
|
802 |
// Feasible swap move
|
kpeter@999
|
803 |
int v = -1;
|
kpeter@999
|
804 |
BoolVector &row = _gr[u];
|
kpeter@999
|
805 |
for (int i = 0; i != _n; i++) {
|
kpeter@999
|
806 |
if (_clique[i] && !row[i]) {
|
kpeter@999
|
807 |
v = i;
|
kpeter@999
|
808 |
break;
|
kpeter@999
|
809 |
}
|
kpeter@999
|
810 |
}
|
kpeter@999
|
811 |
addCliqueNode(u);
|
kpeter@999
|
812 |
delCliqueNode(v);
|
kpeter@999
|
813 |
_tabu[v] = true;
|
kpeter@999
|
814 |
tabu_empty = false;
|
kpeter@999
|
815 |
if (--max_swap <= 0) break;
|
kpeter@999
|
816 |
}
|
kpeter@999
|
817 |
else if ((u = sel_method.nextAddNode()) != -1) {
|
kpeter@999
|
818 |
// Non-feasible add move
|
kpeter@999
|
819 |
addCliqueNode(u);
|
kpeter@999
|
820 |
}
|
kpeter@999
|
821 |
else break;
|
kpeter@999
|
822 |
}
|
kpeter@999
|
823 |
if (_size > _best_size) {
|
kpeter@999
|
824 |
_best_clique = _clique;
|
kpeter@999
|
825 |
_best_size = _size;
|
kpeter@1022
|
826 |
if (_best_size >= max_size) return SIZE_LIMIT;
|
kpeter@999
|
827 |
}
|
kpeter@999
|
828 |
sel_method.update();
|
kpeter@999
|
829 |
}
|
kpeter@999
|
830 |
|
kpeter@1022
|
831 |
return (restart >= max_restart ? ITERATION_LIMIT : STEP_LIMIT);
|
kpeter@999
|
832 |
}
|
kpeter@999
|
833 |
|
kpeter@999
|
834 |
}; //class GrossoLocatelliPullanMc
|
kpeter@999
|
835 |
|
kpeter@999
|
836 |
///@}
|
kpeter@999
|
837 |
|
kpeter@999
|
838 |
} //namespace lemon
|
kpeter@999
|
839 |
|
kpeter@999
|
840 |
#endif //LEMON_GROSSO_LOCATELLI_PULLAN_MC_H
|