-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_SDOT.cpp
More file actions
85 lines (67 loc) · 2.51 KB
/
test_SDOT.cpp
File metadata and controls
85 lines (67 loc) · 2.51 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
#include "power_voronoi.h"
#include "writer.h"
#include "lbfgs.h"
#include <iostream>
#include <vector>
#include <cmath>
// This was written by chatGPT-4, with some modifications, for testing purposes.
int main() {
std::vector<Vector> sites = {
Vector(0.25, 0.25, 0.0),
Vector(0.75, 0.25, 0.0),
Vector(0.5, 0.75, 0.0),
Vector(0.25, 0.75, 0.0),
Vector(0.75, 0.75, 0.0),
Vector(0.5, 0.5, 0.0),
Vector(0.25, 0.5, 0.0),
Vector(0.75, 0.5, 0.0),
Vector(0.1, 0.1, 0.0),
};
Polygon bounding_box;
bounding_box.vertices = {
Vector(0.0, 0.0, 0.0),
Vector(1.0, 0.0, 0.0),
Vector(1.0, 1.0, 0.0),
Vector(0.0, 1.0, 0.0)
};
size_t n = sites.size();
// Target area per site (equal partition)
std::vector<double> lambdas(n, 1.0 / n);
// Initial weights
lbfgsfloatval_t *w = lbfgs_malloc(n);
for (size_t i = 0; i < n; ++i) {
w[i] = 0.1 * ((double)rand() / RAND_MAX - 0.5);
}
lbfgs_parameter_t param;
lbfgs_parameter_init(¶m);
param.max_iterations = 500;
param.epsilon = 1e-7;
param.linesearch = LBFGS_LINESEARCH_BACKTRACKING;
param.max_linesearch = 100;
param.ftol = 1e-5;
param.gtol = 0.9;
// Setup context
SDOTContext ctx = {sites, lambdas, bounding_box};
lbfgsfloatval_t fx;
int ret = lbfgs(n, w, &fx, evaluate, nullptr, &ctx, ¶m);
std::cout << "L-BFGS optimization done. Status: " << ret << ", Final value: " << fx << std::endl;
// Convert final weights to vector
std::vector<double> final_weights(w, w + n);
// Compute final power diagram
std::vector<Polygon> cells = power_voronoi(sites, final_weights, bounding_box);
save_svg(cells, "test_sdot.svg", "none");
for (size_t i = 0; i < n; ++i) {
double area = cell_area(cells[i]);
std::cout << "Site " << i << " target area: " << lambdas[i]
<< ", actual area: " << area << ", final weight: " << final_weights[i] << std::endl;
}
if (ret != LBFGS_SUCCESS) {
std::cerr << "LBFGS failed: " << lbfgs_strerror(ret) << std::endl;
return 1;
}
std::vector<Polygon> opt_cells = power_voronoi(sites, final_weights, bounding_box);
save_svg(opt_cells, "test_sdot_optimized.svg", "none");
std::cout << "Optimal power Voronoi diagram saved to test_sdot_optimized.svg" << std::endl;
lbfgs_free(w);
return 0;
}