-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRuinAndRecreate.hpp
185 lines (128 loc) · 6.32 KB
/
RuinAndRecreate.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Created by acco on 4/24/20.
//
#ifndef FILO__RUINANDRECREATE_HPP_
#define FILO__RUINANDRECREATE_HPP_
#include <cobra/Instance.hpp>
#include <random>
#include <cobra/Solution.hpp>
class RuinAndRecreate {
const cobra::Instance& instance;
std::mt19937& rand_engine;
std::uniform_int_distribution<int> boolean_dist;
std::uniform_int_distribution<int> customers_distribution;
std::uniform_int_distribution<int> rand_uniform;
public:
RuinAndRecreate(const cobra::Instance& instance_, std::mt19937& rand_engine_) : instance(instance_),
rand_engine(rand_engine_),
boolean_dist(std::uniform_int_distribution(0, 1)),
customers_distribution(instance.get_customers_begin(), instance.get_customers_end() - 1),
rand_uniform(0, 3){
}
int apply(cobra::Solution& solution, std::vector<int>& omega) {
auto removed = std::vector<int>();
auto routes = std::unordered_set<int>();
auto seed = customers_distribution(rand_engine);
const auto N = omega[seed];
auto curr = seed;
for(auto n = 0; n < N; n++) {
assert(curr != instance.get_depot());
auto next = cobra::Solution::dummy_vertex;
auto route = solution.get_route_index(curr);
removed.push_back(curr);
routes.insert(route);
if(solution.get_route_size(route) > 1 && boolean_dist(rand_engine)) {
// move within the current route
if(boolean_dist(rand_engine)) {
next = solution.get_next_vertex(curr);
if(next == instance.get_depot()) {
next = solution.get_next_vertex(route, next);
}
} else {
next = solution.get_prev_vertex(curr);
if(next == instance.get_depot()) {
next = solution.get_prev_vertex(route, next);
}
}
} else {
// jump to neighbor route
if(boolean_dist(rand_engine)) {
for(auto m = 1u; m < instance.get_neighbors_of(curr).size(); m++) {
const auto neighbor = instance.get_neighbors_of(curr)[m];
if(neighbor == instance.get_depot() || !solution.is_customer_in_solution(neighbor) || routes.count(solution.get_route_index(neighbor))) { continue; }
next = neighbor;
break;
}
} else {
for(auto m = 1u; m < instance.get_neighbors_of(curr).size(); m++) {
const auto neighbor = instance.get_neighbors_of(curr)[m];
if(neighbor == instance.get_depot() || !solution.is_customer_in_solution(neighbor)) { continue; }
next = neighbor;
break;
}
}
}
assert(next != instance.get_depot());
solution.remove_vertex(route, curr);
if(solution.is_route_empty(route)) {
solution.remove_route(route);
}
if(next == cobra::Solution::dummy_vertex) {
break;
}
curr = next;
}
switch (rand_uniform(rand_engine)) {
case 0:
std::shuffle(removed.begin(), removed.end(), rand_engine);
break;
case 1:
std::sort(removed.begin(), removed.end(),
[this](int a, int b) { return instance.get_demand(a) > instance.get_demand(b); });
break;
case 2:
std::sort(removed.begin(), removed.end(), [this](int a, int b) {
return instance.get_cost(a, instance.get_depot()) > instance.get_cost(b, instance.get_depot());
});
break;
case 3:
std::sort(removed.begin(), removed.end(), [this](int a, int b) {
return instance.get_cost(a, instance.get_depot()) < instance.get_cost(b, instance.get_depot());
});
break;
}
for (auto customer : removed) {
assert(customer != instance.get_depot());
auto best_route = cobra::Solution::dummy_route;
auto best_where = cobra::Solution::dummy_vertex;
auto best_cost = std::numeric_limits<float>::max();
for(auto route = solution.get_first_route(); route != cobra::Solution::dummy_route; route = solution.get_next_route(route)){
if (solution.get_route_load(route) + instance.get_demand(customer) > instance.get_vehicle_capacity()) { continue; }
for (auto where = solution.get_first_customer(route); where != instance.get_depot(); where = solution.get_next_vertex(where)) {
const auto prev = solution.get_prev_vertex(where);
const auto cost = -instance.get_cost(prev, where) + instance.get_cost(prev, customer) + instance.get_cost(customer, where);
if (cost < best_cost) {
best_cost = cost;
best_route = route;
best_where = where;
}
}
const auto cost = -instance.get_cost(instance.get_depot(), solution.get_last_customer(route))
+ instance.get_cost(solution.get_last_customer(route), customer)
+ instance.get_cost(customer, instance.get_depot());
if (cost < best_cost) {
best_cost = cost;
best_route = route;
best_where = instance.get_depot();
}
}
if (best_route == cobra::Solution::dummy_route) {
solution.build_one_customer_route(customer);
} else {
solution.insert_vertex_before(best_route, best_where, customer);
}
}
return seed;
}
};
#endif //FILO__RUINANDRECREATE_HPP_