Skip to content

Commit

Permalink
renamed old MaxCliqueNeighbourhood to MaxCliqueRandomNeighbour.
Browse files Browse the repository at this point in the history
introduced new MaxCliqueNeighbourhood which swap the vertex which causes the largest clique with all other vertices.

changed return type of has_next_neighbour_solution from const std::vector<unsigned int> to std::vector<unsigned int>.
  • Loading branch information
thomas.hammerl committed Aug 16, 2008
1 parent c4eb64f commit 5c827c6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
58 changes: 53 additions & 5 deletions acotreewidth/trunk/include/acotreewidth/decomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ template <class T> std::vector<unsigned int> get_max_clique_positions(const T &g
return max_clique_positions;
}

template <class T> class MaxCliqueNeighbourhood : public Neighbourhood {
template <class T> class MaxCliqueRandomNeighbour : public Neighbourhood {
private:
T *graph_;
std::vector<unsigned int> solution_;
std::vector<unsigned int> neighbour_;
bool has_next_neighbour_;
public:
MaxCliqueNeighbourhood(const T &graph, std::vector<unsigned int> solution) {
MaxCliqueRandomNeighbour(const T &graph, std::vector<unsigned int> solution) {
graph_ = new T(graph);
solution_ = solution;
neighbour_ = solution;
has_next_neighbour_ = true;
}

~MaxCliqueNeighbourhood() {
~MaxCliqueRandomNeighbour() {
delete graph_;
}

Expand All @@ -96,7 +96,7 @@ template <class T> class MaxCliqueNeighbourhood : public Neighbourhood {
return has_next_neighbour_;
}

const std::vector<unsigned int> &next_neighbour_solution() {
std::vector<unsigned int> next_neighbour_solution() {
std::vector<unsigned int> max_cliques = get_max_clique_positions<T>(*graph_, solution_);
unsigned int max_clique_pos = 0;
unsigned int other_pos = 0;
Expand All @@ -113,6 +113,50 @@ template <class T> class MaxCliqueNeighbourhood : public Neighbourhood {
}
};

template <class T> class MaxCliqueNeighbourhood : public Neighbourhood {
private:
T *graph_;
std::vector<unsigned int> solution_;
unsigned int max_clique_pos_;
unsigned int swap_pos_;
public:
MaxCliqueNeighbourhood(const T &graph, std::vector<unsigned int> solution) {
graph_ = new T(graph);
solution_ = solution;
std::vector<unsigned int> max_cliques = get_max_clique_positions<T>(*graph_, solution_);
max_clique_pos_ = max_cliques[random_number(max_cliques.size())];
swap_pos_ = 0;
}

~MaxCliqueNeighbourhood() {
delete graph_;
}

void set_solution(std::vector<unsigned int> solution) {
solution_ = solution;
std::vector<unsigned int> max_cliques = get_max_clique_positions<T>(*graph_, solution_);
max_clique_pos_ = max_cliques[random_number(max_cliques.size())];
swap_pos_ = 0;
}

std::vector<unsigned int> get_solution() {
return solution_;
}

bool has_next_neighbour_solution() {
return swap_pos_ < solution_.size();
}

std::vector<unsigned int> next_neighbour_solution() {
std::vector<unsigned int> neighbour = solution_;
unsigned int tmp = neighbour[max_clique_pos_];
neighbour[max_clique_pos_] = neighbour[swap_pos_];
neighbour[swap_pos_] = tmp;
swap_pos_++;
return neighbour;
}
};

class DecompLocalSearch : public LocalSearch {
private:
void search_neighbourhood();
Expand Down Expand Up @@ -217,10 +261,14 @@ template <class T> class DecompProblem : public OptimizationProblem, public Eval
}

std::vector<unsigned int> apply_local_search(const std::vector<unsigned int> &tour) {
MaxCliqueNeighbourhood<T> neighbourhood(*graph_, tour);
/*MaxCliqueRandomNeighbour<T> neighbourhood(*graph_, tour);
DecompLocalSearch local_search(tour, *this, neighbourhood);
IterativeLocalSearch search(&local_search, this);
search.run(100, 10);
return local_search.get_best_so_far_solution();*/
MaxCliqueNeighbourhood<T> neighbourhood(*graph_, tour);
HillClimbing local_search(tour, *this, neighbourhood);
local_search.search_iterations_without_improve(1);
return local_search.get_best_so_far_solution();
}

Expand Down
4 changes: 2 additions & 2 deletions acotreewidth/trunk/include/liblocalsearch/localsearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Neighbourhood {
virtual void set_solution(std::vector<unsigned int> solution) = 0;
virtual std::vector<unsigned int> get_solution() = 0;
virtual bool has_next_neighbour_solution() = 0;
virtual const std::vector<unsigned int> &next_neighbour_solution() = 0;
virtual std::vector<unsigned int> next_neighbour_solution() = 0;
virtual void reset();
};

Expand All @@ -23,7 +23,7 @@ class TwoOptNeighbourhood : public Neighbourhood {
void set_solution(std::vector<unsigned int> solution);
std::vector<unsigned int> get_solution();
bool has_next_neighbour_solution();
const std::vector<unsigned int> &next_neighbour_solution();
std::vector<unsigned int> next_neighbour_solution();
void swap(std::vector<unsigned int> &solution, unsigned int i, unsigned int j);
};

Expand Down
Binary file modified acotreewidth/trunk/lib/libaco.a
Binary file not shown.
Binary file modified acotreewidth/trunk/lib/liblocalsearch.a
Binary file not shown.
4 changes: 2 additions & 2 deletions liblocalsearch/trunk/include/liblocalsearch/localsearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Neighbourhood {
virtual void set_solution(std::vector<unsigned int> solution) = 0;
virtual std::vector<unsigned int> get_solution() = 0;
virtual bool has_next_neighbour_solution() = 0;
virtual const std::vector<unsigned int> &next_neighbour_solution() = 0;
virtual std::vector<unsigned int> next_neighbour_solution() = 0;
virtual void reset();
};

Expand All @@ -23,7 +23,7 @@ class TwoOptNeighbourhood : public Neighbourhood {
void set_solution(std::vector<unsigned int> solution);
std::vector<unsigned int> get_solution();
bool has_next_neighbour_solution();
const std::vector<unsigned int> &next_neighbour_solution();
std::vector<unsigned int> next_neighbour_solution();
void swap(std::vector<unsigned int> &solution, unsigned int i, unsigned int j);
};

Expand Down
2 changes: 1 addition & 1 deletion liblocalsearch/trunk/src/localsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool TwoOptNeighbourhood::has_next_neighbour_solution() {
return j_ < solution_.size();
}

const std::vector<unsigned int> &TwoOptNeighbourhood::next_neighbour_solution() {
std::vector<unsigned int> TwoOptNeighbourhood::next_neighbour_solution() {
swap(neighbour_, prev_i_, prev_j_);
swap(neighbour_, i_, j_);
prev_i_ = i_;
Expand Down

0 comments on commit 5c827c6

Please sign in to comment.