-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.cpp
More file actions
399 lines (374 loc) · 14.1 KB
/
Solver.cpp
File metadata and controls
399 lines (374 loc) · 14.1 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//
// Created by qiming on 24-12-10.
//
#include "Solver.h"
#include "RandomGenerator.h"
#include "Solution.h"
#include <chrono>
#include <fstream>
#include <future>
#include <iostream>
#define MAX_ITER 80000
void Solver::solve(long long int time_limit)
{
if (time_limit < 0)
{
time_limit = INT32_MAX;
}
std::chrono::steady_clock::time_point limit_end_time = std::chrono::steady_clock::now() + std::chrono::seconds(time_limit);
qm::RandomGenerator random_generator(_seed);
#ifdef TABU
Solution solution(_gc, random_generator, init_type::greedy);
LocalSearch localSearch(_gc, _seed);
localSearch.search(solution, MAX_ITER);
for (int c: solution.colors)
{
std::cout << c << "\n";
}
return;
#endif
std::vector<Solution> population(4, Solution(_gc.node_num));
for (int i = 0; i < 4; i++)
{
population[i].init(_gc, random_generator, init_type::GREEDY);
}
Solution best_solution = population.at(0);
uint64_t cycle = 0;
uint64_t generation = 0;
uint64_t best_iteration = 0;
std::vector<Solution> children = {population.at(0), population.at(1)};
std::vector<LocalSearch> ls(2, LocalSearch(_gc, _seed));
std::atomic<bool> found_solution(false);
while (!found_solution.load() && std::chrono::steady_clock::now() < limit_end_time)
{
if (population[0] == population[1])
{
if (population[1] != best_solution)
population[1] = best_solution;
else if (population[1] != population[3])
population[1] = population[3];
else
population[1].crossover(Solution(_gc, random_generator, init_type::DSATUR), best_solution, _gc, random_generator);
}
children[0].crossover(population[0], population[1], _gc, random_generator);
children[1].crossover(population[1], population[0], _gc, random_generator);
std::vector<std::future<void>> futures;
for (int i = 0; i < 2; ++i)
{
futures.emplace_back(std::async(std::launch::async, [&, i] {
ls[i].search(children.at(i), MAX_ITER, found_solution);
}));
}
for (auto &future: futures)
{
future.wait();
}
for (int i = 0; i < 2; i++)
{
population[i] = children[i];
}
// elite1 <- best(p1, p2, elite1)
if (population[0].conflict_num < population[2].conflict_num)
{
population[2] = population[0];
}
if (population[1].conflict_num < population[2].conflict_num)
{
population[2] = population[1];
}
// best <- best(bestSolution, elite1)
if (population[2].conflict_num < best_solution.conflict_num)
{
best_solution = population[2];
best_iteration = generation;
}
if (generation % 10 == 0)
{
cycle++;
#ifdef DEBUG
std::clog << "cycle: " << cycle;
std::clog << "\tp1: " << population[0].conflict_num;
std::clog << "\tp2: " << population[1].conflict_num;
std::clog << "\telite1: " << population[2].conflict_num;
std::clog << "\tbest: " << best_solution.conflict_num << "(" << best_iteration << ")" << std::endl;
#endif
population[0] = population[3];
// elite2 <- elite1
population[3] = population[2];
// elite1 <- newSolution
population[2].init(_gc, random_generator, init_type::RANDOM);
}
generation++;
#ifdef AUTO_SAVE
if (generation % 100 == 0)
{
std::ofstream out("auto_save.txt");
for (int i = 0; i < _gc.node_num; i++)
{
out << best_solution.colors[i] << ", ";
}
out << "\n";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < _gc.node_num; j++)
{
out << population[i].colors[j] << ", ";
}
out << "\n";
}
out.close();
std::clog << "saved" << std::endl;
}
#endif
}
}
void LocalSearch::init()
{
int node_num = _gc.node_num;
int color_num = _gc.color_num;
tabu_table = new uint64_t *[node_num]();
adjacent_color_table = new int32_t *[node_num]();
adjacent_nodes.resize(node_num);
for (int i = 0; i < node_num; i++)
{
tabu_table[i] = new uint64_t[color_num];
adjacent_color_table[i] = new int32_t[color_num];
}
for (const auto &edge: _gc.edges)
{
adjacent_nodes[edge[0]].push_back(edge[1]);
adjacent_nodes[edge[1]].push_back(edge[0]);
}
}
void LocalSearch::search(Solution &solution, uint64_t max_iterations, std::atomic<bool> &found_solution)
{
iteration = 0;
for (int i = 0; i < _gc.node_num; i++)
{
for (int j = 0; j < _gc.color_num; j++)
{
adjacent_color_table[i][j] = 0;
tabu_table[i][j] = 0;
}
}
for (auto i = 0; i < _gc.node_num; i++)
{
for (auto adj_node: adjacent_nodes[i])
{
adjacent_color_table[adj_node][solution.colors[i]]++;
}
}
best_conflict_num = solution.conflict_num;
while (iteration < max_iterations)
{
if (found_solution.load())
{
std::clog << "Thread " << " stopped early.\n";
return;// 提前退出
}
find_move(solution);
make_move(solution, 1);
if (solution.conflict_num == 0)
{
found_solution.store(true);
for (int i = 0; i < _gc.node_num; i++)
{
std::cout << solution.colors[i] << "\n";
}
return;
}
#ifdef DEBUGLS
if (iteration % 100000 == 0)
{
std::clog << "iteration: " << iteration << " conflict_num: " << solution.conflict_num << std::endl;
}
#endif
iteration++;
}
solution.colors = best_solution;
solution.conflict_num = best_conflict_num;
}
void LocalSearch::find_move(Solution &solution)
{
int tabu_delta = INT32_MAX; // 禁忌移动的delta
int non_tabu_delta = INT32_MAX; // 非禁忌移动的delta
std::array<int, 3> best_tabu_move = {-1, -1, -1};// 最佳禁忌移动
int tabu_move_num = 0; // 禁忌移动的数量
std::array<int, 3> best_non_tabu_move = {-1, -1, -1};// 最佳非禁忌移动
int non_tabu_move_num = 0; // 非禁忌移动的数量
for (auto i = 0; i < solution.conflict_node_num(); i++)
{
auto node = solution.conflict_nodes_queue.conflict_nodes[i];
for (auto j = 0; j < _gc.color_num; j++)
{
if (j != solution.colors[node])
{
auto move_delta = adjacent_color_table[node][j] - adjacent_color_table[node][solution.colors[node]];
if (iteration < tabu_table[node][j])
{
if (move_delta < tabu_delta)
{
tabu_delta = move_delta;
best_tabu_move = {node, solution.colors[node], j};
tabu_move_num = 1;
} else if (move_delta == tabu_delta)
{
tabu_move_num++;
if (_random_generator.rand(tabu_move_num) == 0)
{
best_tabu_move = {node, solution.colors[node], j};
}
}
} else
{
if (move_delta < non_tabu_delta)
{
non_tabu_delta = move_delta;
best_non_tabu_move = {node, solution.colors[node], j};
non_tabu_move_num = 1;
} else if (move_delta == non_tabu_delta)
{
non_tabu_move_num++;
if (_random_generator.rand(non_tabu_move_num) == 0)
{
best_non_tabu_move = {node, solution.colors[node], j};
}
}
}
}
}
}
if (tabu_delta < best_conflict_num - solution.conflict_num && tabu_delta < non_tabu_delta)
{
move = {best_tabu_move.at(0), best_tabu_move.at(1), best_tabu_move.at(2), tabu_delta};
} else
{
move = {best_non_tabu_move.at(0), best_non_tabu_move.at(1), best_non_tabu_move.at(2), non_tabu_delta};
}
}
void LocalSearch::make_move(Solution &solution, double lambda)
{
// 更新冲突数
solution.conflict_num += move.at(3);
if (solution.conflict_num < best_conflict_num)
{
best_conflict_num = solution.conflict_num;
best_solution = solution.colors;
}
if (adjacent_color_table[move.at(0)][move.at(2)] == 0)
{
solution.conflict_nodes_queue.remove(move.at(0));
} else
{
solution.conflict_nodes_queue.add(move.at(0));
}
// 更新邻接颜色表
for (int adjNode: adjacent_nodes[move.at(0)])
{
// 如果新颜色是邻接结点的颜色,且邻接结点不在冲突结点中,则将邻接结点加入冲突结点
if (move.at(2) == solution.colors[adjNode] && solution.conflict_nodes_queue.conflict_nodes_pos[adjNode] == -1)
{
solution.conflict_nodes_queue.add(adjNode);
}
adjacent_color_table[adjNode][move.at(1)]--;
adjacent_color_table[adjNode][move.at(2)]++;
// 如果旧颜色是邻接结点的颜色,且邻接结点的旧颜色冲突数量为0,则将邻接结点从冲突结点中移除
if (move.at(1) == solution.colors[adjNode] && adjacent_color_table[adjNode][move.at(1)] == 0)
{
solution.conflict_nodes_queue.remove(adjNode);
}
}
// 更新禁忌表
tabu_table[move.at(0)][move.at(1)] = iteration + uint64_t(lambda * solution.conflict_num + _random_generator.rand(10));
// 更新解
solution.colors[move.at(0)] = move.at(2);
}
[[maybe_unused]] std::pair<std::array<int, 4>, std::array<int, 4>> LocalSearch::find_from_conflict_nodes(const Solution &solution, int start, int end)
{
int tabu_delta = INT32_MAX; // 禁忌移动的delta
int non_tabu_delta = INT32_MAX; // 非禁忌移动的delta
std::array<int, 4> best_tabu_move = {-1, -1, -1, INT32_MAX};// 最佳禁忌移动
int tabu_move_num = 0; // 禁忌移动的数量
std::array<int, 4> best_non_tabu_move = {-1, -1, -1, INT32_MAX};// 最佳非禁忌移动
int non_tabu_move_num = 0; // 非禁忌移动的数量
// 遍历冲突结点
for (auto i = start; i < end; i++)
{
auto node = solution.conflict_nodes_queue.conflict_nodes[i];
for (auto j = 0; j < _gc.color_num; j++)
{
if (j != solution.colors[node])
{
auto move_delta = adjacent_color_table[node][j] - adjacent_color_table[node][solution.colors[node]];
if (iteration < tabu_table[node][j])
{
if (move_delta < tabu_delta)
{
tabu_delta = move_delta;
best_tabu_move = {node, solution.colors[node], j, move_delta};
tabu_move_num = 1;
} else if (move_delta == tabu_delta)
{
tabu_move_num++;
if (_random_generator.rand(tabu_move_num) == 0)
{
best_tabu_move = {node, solution.colors[node], j, move_delta};
}
}
} else
{
if (move_delta < non_tabu_delta)
{
non_tabu_delta = move_delta;
best_non_tabu_move = {node, solution.colors[node], j, move_delta};
non_tabu_move_num = 1;
} else if (move_delta == non_tabu_delta)
{
non_tabu_move_num++;
if (_random_generator.rand(non_tabu_move_num) == 0)
{
best_non_tabu_move = {node, solution.colors[node], j, move_delta};
}
}
}
}
}
}
return {best_tabu_move, best_non_tabu_move};
}
void LocalSearch::search(Solution &solution, uint64_t max_iterations)
{
iteration = 0;
for (int i = 0; i < _gc.node_num; i++)
{
for (int j = 0; j < _gc.color_num; j++)
{
adjacent_color_table[i][j] = 0;
tabu_table[i][j] = 0;
}
}
for (auto i = 0; i < _gc.node_num; i++)
{
for (auto adj_node: adjacent_nodes[i])
{
adjacent_color_table[adj_node][solution.colors[i]]++;
}
}
best_conflict_num = solution.conflict_num;
while (iteration < max_iterations)
{
find_move(solution);
make_move(solution, 1);
if (solution.conflict_num == 0)
{
break;
}
#ifdef DEBUGLS
if (iteration % 100000 == 0)
{
std::clog << "iteration: " << iteration << " conflict_num: " << solution.conflict_num << std::endl;
}
#endif
iteration++;
}
}