| 43 | } |
| 44 | |
| 45 | void PerturbationManager::perturb(Solution &solution, |
| 46 | SearchSpace &searchSpace, |
| 47 | CostEvaluator const &costEvaluator) const |
| 48 | { |
| 49 | size_t movesLeft = numPerturbations_; |
| 50 | |
| 51 | if (!movesLeft) // nothing to do |
| 52 | return; |
| 53 | |
| 54 | // Clear the set of promising nodes. Perturbation determines the initial |
| 55 | // set of promising nodes for further (local search) improvement. |
| 56 | searchSpace.unmarkAllPromising(); |
| 57 | |
| 58 | DynamicBitset perturbed = {solution.nodes.size()}; |
| 59 | auto const perturb = [&](auto *node, PerturbType action) |
| 60 | { |
| 61 | // This node has already been touched by a previous perturbation, so |
| 62 | // we skip it here. |
| 63 | if (perturbed[node->client()]) |
| 64 | return; |
| 65 | |
| 66 | // Remove if node is in a route and we are currently removing. |
| 67 | auto *route = node->route(); |
| 68 | if (route && action == PerturbType::REMOVE) |
| 69 | { |
| 70 | searchSpace.markPromising(node); |
| 71 | route->remove(node->idx()); |
| 72 | route->update(); |
| 73 | } |
| 74 | // Insert if node is not in a route and we are currently inserting. |
| 75 | else if (!route && action == PerturbType::INSERT) |
| 76 | { |
| 77 | solution.insert(node, searchSpace, costEvaluator, true); |
| 78 | node->route()->update(); |
| 79 | searchSpace.markPromising(node); |
| 80 | } |
| 81 | else // no-op |
| 82 | return; |
| 83 | |
| 84 | perturbed[node->client()] = true; |
| 85 | movesLeft--; |
| 86 | }; |
| 87 | |
| 88 | // We do numPerturbations if we can. We perturb the local neighbourhood of |
| 89 | // randomly selected clients U: if U is in the solution, we remove it and |
| 90 | // its neighbours, while if it is not, we try to insert instead. Each |
| 91 | // removal or insertion counts as one perturbation. |
| 92 | for (auto const uClient : searchSpace.clientOrder()) |
| 93 | { |
| 94 | auto *U = &solution.nodes[uClient]; |
| 95 | auto action = U->route() ? PerturbType::REMOVE : PerturbType::INSERT; |
| 96 | perturb(U, action); |
| 97 | |
| 98 | if (!movesLeft) |
| 99 | return; |
| 100 | |
| 101 | for (auto const vClient : searchSpace.neighboursOf(U->client())) |
| 102 | { |