forked from HackerPoet/Beryl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.cpp
More file actions
359 lines (334 loc) · 12.6 KB
/
Copy pathsolver.cpp
File metadata and controls
359 lines (334 loc) · 12.6 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
#include "solver.h"
#include "globals.h"
#include <algorithm>
#include <iostream>
#include <filesystem>
RNG eng;
struct Sample {
VectorXf x;
float score;
int time;
bool operator<(const Sample& rhs) const {
return this->score < rhs.score;
}
};
void set_rand_seed(int seed) {
eng.seed(seed);
}
void add_random_noise(const VectorXf& x, float sigma, VectorXf& result) {
std::normal_distribution<float> rand_normal(0.0f, sigma);
result = x;
const int num_k = std::uniform_int_distribution<int>(1, 3)(eng);
for (int k = 0; k < num_k; ++k) {
if (g_sym.perms.empty()) {
const int i = std::uniform_int_distribution<int>(0, (int)x.size() / 3 - 1)(eng);
result[i * 3 + 0] += rand_normal(eng);
result[i * 3 + 1] += rand_normal(eng);
result[i * 3 + 2] += rand_normal(eng);
} else {
const Face& group = g_sym.perms[std::uniform_int_distribution<size_t>(0, g_sym.perms.size() - 1)(eng)].cycle;
for (int i : group) {
result[i * 3 + 0] += rand_normal(eng);
result[i * 3 + 1] += rand_normal(eng);
result[i * 3 + 2] += rand_normal(eng);
}
}
}
}
void set_random_mags(VectorXf& x, float sigma) {
std::normal_distribution<float> rand_normal(0.0f, sigma);
for (int i = 0; i < x.size(); i += 3) {
Eigen::Map<Vector3f> sub_x(x.data() + i);
sub_x.normalize();
sub_x *= rand_normal(eng);
}
}
void initialize_random_noise(VectorXf& x, float sigma, size_t size) {
std::normal_distribution<float> rand_normal(0.0f, sigma);
x.resize(size);
for (int i = 0; i < x.size(); ++i) {
x[i] = rand_normal(eng);
}
}
void initialize_random_noise(std::vector<std::pair<float, VectorXf>>& xvec, float sigma) {
size_t size = (g_dual ? g_tris.size() : g_polys.size()) * 3;
for (auto& x : xvec) {
initialize_random_noise(x.second, sigma, size);
}
}
void initialize_random_noise(std::vector<std::pair<float, VectorXf>>& xvec, const VectorXf& guess, float sigma) {
for (auto& x : xvec) {
initialize_random_noise(x.second, sigma, guess.size());
x.second += guess;
}
}
void initialize_random_mag(std::vector<VectorXf>& yvec, const VectorXf& n, float sigma) {
std::normal_distribution<float> rand_normal(0.0f, sigma);
for (VectorXf& y : yvec) {
y.resize(g_polys.size());
for (int i = 0; i < y.size(); ++i) {
y[i] = rand_normal(eng);
}
}
}
bool v_pred(const std::pair<float, VectorXf>& left, const std::pair<float, VectorXf>& right) {
return left.first < right.first;
}
float main_optimizer(float (*objective_function)(const Verts3D&, const Planes&), VectorXf& result, int max_iters,
float sigma, float beta, int clusters) {
static const int extra_tries = 1;
static Planes planes;
static Verts3D v3ds;
std::vector<std::pair<float, VectorXf>> xv(clusters * extra_tries);
VectorXf new_pt;
initialize_random_noise(xv, sigma);
for (auto& x : xv) {
if (!g_sym.Apply(x.second)) {
return -1.0f;
}
if (g_dual) {
y_to_v3ds(x.second, v3ds);
v3ds_to_planes(v3ds, g_polys, planes);
} else {
x_to_planes(x.second, planes);
planes_to_v3ds(g_tris, planes, v3ds);
}
x.first = objective_function(v3ds, planes);
}
std::sort(xv.begin(), xv.end(), v_pred);
xv.resize(clusters);
int iter = 0;
int last_best_iter = 0;
float best_cost = 99999;
const size_t x_size = xv[0].second.size();
new_pt.resize(x_size);
while (true) {
const size_t min_ix = std::min_element(xv.begin(), xv.end(), v_pred) - xv.begin();
const float min_cost = xv[min_ix].first;
iter += 1;
if (min_cost < best_cost) {
std::cout << min_cost << " " << iter << std::endl;
best_cost = min_cost;
result = xv[min_ix].second;
last_best_iter = iter;
}
if (iter - last_best_iter > max_iters) { break; }
if (best_cost == 0.0f) { break; }
for (size_t i = 0; i < clusters; ++i) {
static const float gamma = 1.25f;
size_t ix = i;
if (xv[i].first > best_cost * gamma) {
ix = std::uniform_int_distribution<size_t>(0, clusters-1)(eng);
}
add_random_noise(xv[ix].second, 1.0f, new_pt);
new_pt *= beta;
if (!g_sym.Apply(new_pt)) {
return -1.0f;
}
if (g_dual) {
y_to_v3ds(new_pt, v3ds);
v3ds_to_planes(v3ds, g_polys, planes);
} else {
x_to_planes(new_pt, planes);
planes_to_v3ds(g_tris, planes, v3ds);
if (!is_finite(v3ds)) { continue; }
}
const float new_cost = objective_function(v3ds, planes);
float cost_mult = 1.25f; //1.0f + 0.05f * std::uniform_int_distribution<int>(0, 5)(eng);
if (i == min_ix) { cost_mult = 1.0f; }
if ((new_cost <= xv[i].first) || (new_cost <= best_cost * cost_mult)) {
xv[i].first = new_cost;
xv[i].second = new_pt;
}
}
}
return best_cost;
}
void truncate_normalize(VectorXf& result, float smallintMul) {
if (smallintMul > 0.0f) {
result *= (smallintMul * result.size() / result.norm());
trunc_x(result);
} else {
result *= (result.size() / result.norm());
}
}
bool study_sample(float (*objective_function)(const Verts3D&, const Planes&), VectorXf& result, int max_iters, int clusters, float sigma, float beta, bool aggressive, float smallintMul, float q_score) {
static Planes planes;
static Verts3D v3ds;
const size_t x_size = result.size();
if (!g_sym.Apply(result)) { return false; }
truncate_normalize(result, smallintMul);
if (g_dual) {
y_to_v3ds(result, v3ds);
v3ds_to_planes(v3ds, g_polys, planes);
if (!is_finite(planes)) {
std::cout << "ERROR: NaN values in polyhedron." << std::endl;
return false;
}
} else {
x_to_planes(result, planes);
planes_to_v3ds(g_tris, planes, v3ds);
if (!is_finite(v3ds)) {
std::cout << "ERROR: NaN values in polyhedron." << std::endl;
return false;
}
}
float min_score = objective_function(v3ds, planes) + q_penalty(v3ds, planes, count_crossings(v3ds, planes));
std::vector<VectorXf> xv(clusters, result);
std::vector<float> scores(clusters, min_score);
VectorXf new_x(x_size);
for (int iter = 0; iter < max_iters; ++iter) {
int num_updated = 0;
for (int i = 0; i < clusters; ++i) {
add_random_noise(xv[i], sigma, new_x);
if (!g_sym.Apply(new_x)) { return false; }
truncate_normalize(new_x, smallintMul);
if (g_dual) {
y_to_v3ds(new_x, v3ds);
v3ds_to_planes(v3ds, g_polys, planes);
if (!is_finite(planes)) { continue; }
} else {
x_to_planes(new_x, planes);
planes_to_v3ds(g_tris, planes, v3ds);
if (!is_finite(v3ds)) { continue; }
}
float new_cost = objective_function(v3ds, planes);
if (new_cost < scores[i] || (!aggressive && int(new_cost) <= int(scores[i]))) {
//Only compute costly q_penalty when score is low enough to matter
if (int(new_cost) <= int(q_score)) {
new_cost += q_penalty(v3ds, planes, count_crossings(v3ds, planes));
}
if (new_cost < min_score) {
std::cout << "==== New Best! ==== (" << new_cost << ")" << std::endl;
result = new_x;
if (int(new_cost) < int(min_score) || new_cost <= 1.0f) {
std::fill(xv.begin(), xv.end(), new_x);
std::fill(scores.begin(), scores.end(), new_cost);
}
min_score = new_cost;
}
if (!aggressive || new_cost == min_score) {
xv[i] = new_x;
scores[i] = new_cost;
num_updated += 1;
}
}
}
if (iter % 10 == 0) {
std::cout << "[" << iter << "] Updated: " << num_updated << "/" << clusters << " " << sigma << std::endl;
}
if (num_updated < clusters / 10) {
sigma *= beta;
} else if (num_updated > clusters / 5) {
sigma *= 1.005f;
}
if (sigma < 5e-7f) { break; }
}
return true;
}
bool explore_shape(const std::string& load_fname, const std::string& save_dir, int max_iters, float smallintMul) {
//Import an example obj file
int f_iter = 0;
Verts3D obj_verts;
Planes obj_planes;
VectorXf obj_x;
if (load_fname.empty()) {
dual_graph(g_tris, g_polys, g_edges);
fix_face_ordering(g_polys, g_edges);
if (g_dual) {
std::swap(g_tris, g_polys);
make_edges(g_polys, g_edges);
fix_face_ordering(g_polys, g_edges);
}
initialize_random_noise(obj_x, 1.0f, g_polys.size() * 3);
if (g_dual) {
y_to_v3ds(obj_x, obj_verts);
} else {
x_to_planes(obj_x, obj_planes);
planes_to_v3ds(g_tris, obj_planes, obj_verts);
}
} else {
std::vector<std::string> name_split = split(std::filesystem::path(load_fname).stem().string(), '_');
f_iter = std::atoi(name_split[std::min(size_t(3), name_split.size() - 1)].c_str());
if (!import_obj(load_fname.c_str(), obj_verts, g_polys)) {
std::cout << "ERROR: Failed to load obj file: " << load_fname << std::endl;
return false;
}
}
#if 1
Edges dual_edges;
make_edges(g_polys, g_edges);
dual_graph(g_polys, g_tris, dual_edges);
v3ds_to_planes(obj_verts, g_polys, obj_planes);
if (g_dual) {
v3ds_to_y(obj_verts, obj_x);
} else {
planes_to_x(obj_planes, obj_x);
}
#else
dual_graph(g_polys, g_tris, g_edges);
make_edges(g_polys, g_edges);
std::swap(g_polys, g_tris);
v3ds_to_y(obj_verts, obj_x);
x_to_planes(obj_x, obj_planes);
planes_to_v3ds(g_tris, obj_planes, obj_verts);
#endif
//Print characteristics
std::cout << "Petrie Length: " << petrie_length(g_dual ? g_polys : g_tris) << std::endl;
std::cout << "===================" << std::endl;
if (!load_fname.empty()) {
std::cout << "Loaded: " << load_fname << std::endl;
}
save_sample("refined", obj_planes, obj_verts, f_iter, false);
std::cout << "===================" << std::endl;
auto objective = (g_dual ? objective_int : objective_sum);
float best_score = objective(obj_verts, obj_planes) + q_penalty(obj_verts, obj_planes, count_crossings(obj_verts, obj_planes));
float noise_level = 8.0f;
while (true) {
VectorXf new_x = obj_x;
new_x *= new_x.size() / new_x.norm();
if (best_score >= 1.0f) {
std::cout << "Noise level: " << noise_level << std::endl;
std::normal_distribution<float> rand_noise(0.0f, 1.0f);
for (int i = 0; i < new_x.size() / 3; ++i) {
if (rand_noise(eng) > 1.25f) {
new_x[i * 3 + 0] += noise_level * rand_noise(eng);
new_x[i * 3 + 1] += noise_level * rand_noise(eng);
new_x[i * 3 + 2] += noise_level * rand_noise(eng);
} else {
new_x[i * 3 + 0] += noise_level * 0.05f * rand_noise(eng);
new_x[i * 3 + 1] += noise_level * 0.05f * rand_noise(eng);
new_x[i * 3 + 2] += noise_level * 0.05f * rand_noise(eng);
}
}
}
//Run optimizer
if (!study_sample(objective, new_x, max_iters, 100, 10.0f, 0.997f, best_score < 1.0f, smallintMul, best_score)) {
return false;
}
if (g_dual) {
y_to_v3ds(new_x, obj_verts);
v3ds_to_planes(obj_verts, g_polys, obj_planes);
} else {
x_to_planes(new_x, obj_planes);
planes_to_v3ds(g_tris, obj_planes, obj_verts);
}
const float new_score = objective(obj_verts, obj_planes) + q_penalty(obj_verts, obj_planes, count_crossings(obj_verts, obj_planes));
if (int(new_score) <= int(best_score)) {
noise_level *= 1.5f;
} else {
noise_level *= 0.85f;
}
if (new_score < best_score) {
std::cout << "########################################" << std::endl;
std::cout << " NEW BEST SCORE: " << best_score << " --> " << new_score << std::endl;
std::cout << "########################################" << std::endl;
obj_x = new_x;
best_score = new_score;
} else {
std::cout << "Best score: " << best_score << std::endl;
}
save_sample((save_dir + "/refined").c_str(), obj_planes, obj_verts, f_iter, best_score < 1.0f || new_score == best_score);
}
return true;
}