-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdirected_evolution.h
More file actions
227 lines (187 loc) · 5.09 KB
/
directed_evolution.h
File metadata and controls
227 lines (187 loc) · 5.09 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
// Directed Evolution (DE) for the load-dependent Chinese postman problem
// Author: Dr. Truong Son Hy
// Copyright 2023
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <thread>
#include <algorithm>
#include <assert.h>
#include <thread>
#include "../graph_library/Graph.h"
#include "../graph_library/meta_heuristics.h"
using namespace std;
// +-----------------------+
// | k-MOVE (new proposal) |
// +-----------------------+
// For single-thread
pair< vector<Edge>, double> Method_k_MOVE(Graph *graph, const vector<Edge> sigma, const vector<int> k_indices) {
double best = INF;
vector<Edge> result;
// Initialization
vector<bool> mask;
mask.clear();
for (int i = 0; i < sigma.size(); ++i) {
mask.push_back(true);
}
for (int i = 0; i < k_indices.size(); ++i) {
const int index = k_indices[i];
assert(index >= 0);
assert(index < sigma.size());
mask[index] = false;
}
vector<Edge> A;
A.clear();
for (int i = 0; i < sigma.size(); ++i) {
if (mask[i]) {
A.push_back(Edge(sigma[i]));
}
}
assert(A.size() + k_indices.size() == sigma.size());
// Put them back
for (int i = 0; i < k_indices.size(); ++i) {
const int index = k_indices[i];
const Edge e = sigma[index];
// Search for the best place to put the i-th in
vector<Edge> B;
B.clear();
double B_value = INF;
for (int k = 0; k < A.size(); ++k) {
vector<Edge> candidate;
candidate.clear();
for (int t = 0; t < k; ++t) {
candidate.push_back(Edge(A[t]));
}
candidate.push_back(Edge(e));
for (int t = k; t < A.size(); ++t) {
candidate.push_back(Edge(A[t]));
}
// Update
pair< vector< vector<double> >, vector<int> > dp = dynamic_programming(graph, candidate);
const double cost = dp.first[0][0];
if (cost < B_value) {
B_value = cost;
B = candidate;
}
}
A = B;
best = B_value;
}
result = A;
assert(result.size() == sigma.size());
return make_pair(result, best);
}
// +-------------------------+
// | Directed Evolution (DE) |
// +-------------------------+
// Single-thread implementation
pair< vector<Edge>, double> Directed_Evolution(Graph *graph, const int input_k, const int num_variants = 10, const int early_stop = 5, const int num_iterations = 75, const bool verbose = true) {
// New Greedy Constructive Heuristic
pair< vector<Edge>, double > greedy = Greedy_Constructive_Heuristic(graph);
vector<Edge> sigma_star = greedy.first;
double best = greedy.second;
// Number of edges
const int m = sigma_star.size();
// If k >= m
int value = -1;
if (input_k >= m) {
value = m - 1;
} else {
value = input_k;
}
const int k = value;
// Mask
vector<bool> mask;
for (int i = 0; i < m; ++i) {
mask.push_back(false);
}
// Iterative
int count = 0;
for (int iter = 1; iter <= num_iterations; ++iter) {
// Check if improved
bool improve = false;
// Random indices
vector< vector<int> > all_indices;
all_indices.clear();
for (int variant = 0; variant < num_variants; ++variant) {
for (int i = 0; i < m; ++i) {
mask[i] = false;
}
vector<int> k_indices;
k_indices.clear();
for (int i = 0; i < k; ++i) {
while (true) {
const int index = rand() % m;
if (mask[index] == false) {
mask[index] = true;
k_indices.push_back(index);
break;
}
}
}
assert(k_indices.size() == k);
all_indices.push_back(k_indices);
}
assert(all_indices.size() == num_variants);
// k-MOVE
vector<Edge> sigma;
sigma.clear();
for (int i = 0; i < m; ++i) {
sigma.push_back(Edge(sigma_star[i]));
}
for (int variant = 0; variant < num_variants; ++variant) {
pair< vector<Edge>, double> Result_k_MOVE = Method_k_MOVE(graph, sigma, all_indices[variant]);
// Update
if (Result_k_MOVE.second < best) {
best = Result_k_MOVE.second;
sigma_star = Result_k_MOVE.first;
improve = true;
}
}
/*
// Random exchange
sigma = random_exchange(sigma_star);
// 1-OPT
pair< vector<Edge>, double> Result_1_OPT = Method_1_OPT(graph, sigma);
// 2-OPT
pair< vector<Edge>, double> Result_2_OPT = Method_2_OPT(graph, sigma);
// 2-EXCHANGE
pair< vector<Edge>, double> Result_2_EXCHANGE = Method_2_EXCHANGE(graph, sigma);
// Update
if (Result_1_OPT.second < best) {
best = Result_1_OPT.second;
sigma_star = Result_1_OPT.first;
improve = true;
}
if (Result_2_OPT.second < best) {
best = Result_2_OPT.second;
sigma_star = Result_2_OPT.first;
improve = true;
}
if (Result_2_EXCHANGE.second < best) {
best = Result_2_EXCHANGE.second;
sigma_star = Result_2_EXCHANGE.first;
improve = true;
}
*/
// Check early stopping
if (!improve) {
count += 1;
} else {
count = 0;
}
if (count == early_stop) {
cout << "Early stop!" << endl;
break;
}
if (verbose) {
cout << "Done " << iter << " iterations." << endl;
}
}
return make_pair(sigma_star, best);
}