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