-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpar.cpp
223 lines (170 loc) · 5.92 KB
/
par.cpp
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
#include <string>
#include <sstream>
#include "population.hpp"
#include "avx.hpp"
#include "jtime.hpp"
#include "random.hpp"
#include "par.hpp"
inline auto toStr(const int n, float* vec, int width) -> std::string {
auto ss = std::stringstream();
auto s = std::string();
s.resize(width);
for (auto i = 0; i < n; ++i) {
std::fill(begin(s), end(s), '0');
s = std::to_string(vec[i]);
ss << s.substr(0, width);
if (i < n - 1) {
ss << ", ";
}
}
return ss.str();
}
auto crossover(const int n, float* p1X, float* p1Y, float* p2X, float* p2Y, float* childX, float* childY) -> void {
auto start = 0, end = 0;
std::tie(start, end) = std::minmax(random::randomInt(0, n), random::randomInt(0, n));
auto j = 0;
for (auto i = 0; i < n; ++i) {
if (start <= i && i < end) {
childX[i] = p1X[i];
childY[i] = p1Y[i];
} else {
while (true) {
auto idx = end;
auto x1 = p2X[j];
auto y1 = p2Y[j];
for (auto k = start; k < end; ++k) {
auto x2 = p1X[k];
auto y2 = p1Y[k];
if (x1 == x2 && y1 == y2) {
idx = k;
break;
}
}
if (idx < end) {
++j;
} else {
childX[i] = x1;
childY[i] = y1;
++j;
break;
}
}
}
}
}
auto mutate(const int n, float* xs, float* ys, const float mutationRate) -> void {
for (auto i = 0; i < n; ++i) {
if (random::randomFloat() < mutationRate) {
const auto j = random::randomInt(0, n);
std::swap(xs[i], xs[j]);
std::swap(ys[i], ys[j]);
}
}
}
auto mate(population::Population& pop1, population::Population& pop2, float* childX, float* childY, float* ds, const float mutationRate) -> void {
const auto n = pop1.cities;
const auto m = pop1.paths;
const auto range = 8;
const auto maxStart = m - range;
auto rand1 = random::randomInt(0, maxStart);
auto rand2 = random::randomInt(0, maxStart);
auto idx1 = rand1 + avx::indexOfMin(range, ds + rand1);
auto idx2 = rand2 + avx::indexOfMin(range, ds + rand2);
auto parent1X = &pop2.xs.at(idx1, 0);
auto parent1Y = &pop2.ys.at(idx1, 0);
auto parent2X = &pop2.xs.at(idx2, 0);
auto parent2Y = &pop2.ys.at(idx2, 0);
crossover(n, parent1X, parent1Y, parent2X, parent2Y, childX, childY);
mutate(n, childX, childY, mutationRate);
}
auto evolve(jtime::time_type& t, bool& isDone, float& result, int& generations, const int id, const float goal, const float mutationRate, population::Population& pop1, population::Population& pop2) -> void {
const auto n = pop1.cities;
const auto m = pop1.paths;
const auto limit = 1200;
auto nextGenX = std::vector<float>(n * m);
auto nextGenY = std::vector<float>(n * m);
auto tempX = static_cast<float*>(_mm_malloc(sizeof(float*) * m, 32));
auto tempY = static_cast<float*>(_mm_malloc(sizeof(float*) * m, 32));
auto ds = static_cast<float*>(_mm_malloc(sizeof(float*) * m, 32));
auto xs = pop1.xs.data;
auto ys = pop1.xs.data;
auto gen = 1;
auto bestOfGen = 0.f;
while (true) {
std::fill(ds, ds + m, 0.f);
avx::distance(n, m, xs, ys, tempX, tempY, ds);
bestOfGen = avx::min(m, ds);
printf("best of gen %4i: %8.1f, goal: %8.1f, thread: %1i\n", gen, bestOfGen, goal, id);
matrix::transpose(pop1.xs, pop2.xs);
matrix::transpose(pop1.ys, pop2.ys);
auto j = 0;
for (auto i = 0; i < m; ++i) {
auto childX = &nextGenX[j];
auto childY = &nextGenY[j];
mate(pop1, pop2, childX, childY, ds, mutationRate);
j += n;
}
matrix::transpose(m, n, nextGenX, pop1.xs);
matrix::transpose(m, n, nextGenY, pop1.ys);
if(bestOfGen < goal || isDone || limit < gen) {
break;
}
++gen;
}
if (!isDone) {
isDone = true;
t = jtime::now();
result = limit <= gen ? -1.f : bestOfGen;
generations = gen;
}
}
auto par::referenceDistance(const int paths, std::vector<point::Point>& ps) -> float {
auto pop = population::Population(ps.size(), paths);
population::randomPopulation(paths, ps, pop);
const auto n = pop.cities;
const auto m = pop.paths;
auto tempX = static_cast<float*>(_mm_malloc(sizeof(float*) * m, 32));
auto tempY = static_cast<float*>(_mm_malloc(sizeof(float*) * m, 32));
auto ds = static_cast<float*>(_mm_malloc(sizeof(float*) * m, 32));
std::fill(ds, ds + m, 0.f);
auto xs = pop.xs.data;
auto ys = pop.xs.data;
avx::distance(n, m, xs, ys, tempX, tempY, ds);
auto best = avx::min(m, ds);
_mm_free(tempX);
_mm_free(tempY);
_mm_free(ds);
return best;
}
struct PopulationPair {
population::Population* first;
population::Population* second;
};
auto par::travelingSalesperson(const int paths, const int threadCount, const float goal, const float mutationRate, std::vector<point::Point>& ps) -> Data {
const auto cities = ps.size();
auto popPairs = std::vector<PopulationPair>(threadCount);
auto threads = std::vector<std::thread>(threadCount);
for (auto& p : popPairs) {
p.first = new population::Population(cities, paths);
p.second = new population::Population(cities, paths);
population::randomPopulation(paths, ps, *p.first);
}
auto t1 = jtime::now();
auto t2 = jtime::time_type{};
auto isDone = false;
auto result = 0.f;
auto generations = 0;
for (auto i = 0; i < threadCount; ++i) {
auto firstPtr = popPairs[i].first;
auto secondPtr = popPairs[i].second;
threads[i] = std::thread(evolve, std::ref(t2), std::ref(isDone), std::ref(result), std::ref(generations), i, goal, mutationRate, std::ref(*firstPtr), std::ref(*secondPtr));
}
for (auto& thread : threads) {
thread.join();
}
for (auto& p : popPairs) {
delete p.first;
delete p.second;
}
return {t1, t2, result, generations};
}