Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanf committed Apr 2, 2022
1 parent 88beb85 commit 5695261
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
41 changes: 22 additions & 19 deletions stablesolver/algorithms/localsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LocalScheme

public:

/** Global cost: <Overweight, Weight>; */
/** Global cost: <Weight>; */
using GlobalCost = std::tuple<Weight>;

inline Weight& weight(GlobalCost& global_cost) { return std::get<0>(global_cost); }
Expand Down Expand Up @@ -48,7 +48,7 @@ class LocalScheme
struct SolutionVertex
{
/**
* in == true iff vertex j is in the solution.
* in == true iff the vertex is in the solution.
*/
bool in = false;

Expand Down Expand Up @@ -232,16 +232,16 @@ class LocalScheme
// Loop through neighborhoods.
for (Counter neighborhood: neighborhoods) {
switch (neighborhood) {
case 0: { // Toggle neighborhood.
case 0: { // Add neighborhood.
std::shuffle(vertices_.begin(), vertices_.end(), generator);
VertexId v_best = -1;
GlobalCost c_best = global_cost(solution);
for (VertexId v: vertices_) {
if (v == tabu.v)
continue;
GlobalCost c = (contains(solution, v))?
cost_remove(solution, v, c_best):
cost_add(solution, v, c_best);
if (contains(solution, v))
continue;
GlobalCost c = cost_add(solution, v, c_best);
if (c >= c_best)
continue;
if (v_best != -1 && !dominates(c, c_best))
Expand All @@ -252,11 +252,7 @@ class LocalScheme
if (v_best != -1) {
improved = true;
// Apply move.
if (contains(solution, v_best)) {
remove(solution, v_best);
} else {
add(solution, v_best);
}
add(solution, v_best);
assert(global_cost(solution) == c_best);
}
break;
Expand Down Expand Up @@ -374,25 +370,30 @@ class LocalScheme
{
assert(v >= 0);
assert(!contains(solution, v));
solution.vertices[v].in = true;
Weight p = instance_.vertex(v).weight;
solution.weight += p;

Weight w = instance_.vertex(v).weight;

// Remove conflicting vertices.
for (const VertexEdge& edge: instance_.vertex(v).edges) {
if (contains(solution, edge.v))
remove(solution, edge.v);
solution.vertices[edge.v].neighbor_weight += p;
solution.vertices[edge.v].neighbor_weight += w;
}

solution.vertices[v].in = true;
solution.weight += w;
}

inline void remove(Solution& solution, VertexId v) const
{
assert(v >= 0);
assert(contains(solution, v));

solution.vertices[v].in = false;
Weight p = instance_.vertex(v).weight;
solution.weight -= p;
Weight w = instance_.vertex(v).weight;
solution.weight -= w;
for (const VertexEdge& edge: instance_.vertex(v).edges)
solution.vertices[edge.v].neighbor_weight -= p;
solution.vertices[edge.v].neighbor_weight -= w;
}

/*
Expand All @@ -409,7 +410,9 @@ class LocalScheme
inline GlobalCost cost_add(const Solution& solution, VertexId v, GlobalCost) const
{
return {
-(solution.weight + instance_.vertex(v).weight - solution.vertices[v].neighbor_weight),
-(solution.weight
+ instance_.vertex(v).weight
- solution.vertices[v].neighbor_weight),
};
}

Expand Down
18 changes: 9 additions & 9 deletions stablesolver/algorithms/localsearch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
* Local Search:
*
* Two neighborhoods are implemented:
* - Toggle: remove an item from the knapsack or add an item into the knapsack
* and remove its neighbors from the knapsack
* - Add: add a vertex to the solution and remove its neighbors.
* Complexity: O(n)
* - (2-1)-swap: remove an item from the knapsack and add two of its neighbors
* which are not neighbors.
* This neighborhood has originaly been proposed for the Maximum Independent
* Set Problem and for the Maximum-Weight Independent Set Problem.
* Complexity: O(number of conflicts)
* "Fast local search for the maximum independent set problem" (Andrade et al., 2012)
* https://doi.org/10.1007/s10732-012-9196-4
* "A hybrid iterated local search heuristic for the maximum weight independent set problem" (Nogueira et al., 2018)
* https://doi.org/10.1007/s11590-017-1128-7
* Complexity: O(number of edges)
* See:
* - "Fast local search for the maximum independent set problem" (Andrade et
* al., 2012)
* https://doi.org/10.1007/s10732-012-9196-4
* - "A hybrid iterated local search heuristic for the maximum weight
* independent set problem" (Nogueira et al., 2018)
* https://doi.org/10.1007/s11590-017-1128-7
*
*/

Expand Down

0 comments on commit 5695261

Please sign in to comment.