-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
matplotpp_example.cpp
129 lines (114 loc) · 3.44 KB
/
matplotpp_example.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
#include <chrono>
#include <iostream>
#include <map>
#include <numeric>
#include <random>
#include <pareto/archive.h>
#include <pareto/matplot/front.h>
#include <pareto/matplot/archive.h>
int main() {
using namespace pareto;
// Generate a 2d front
front<double, 2, unsigned> pf2d({true, true});
auto f1 = matplot::randn(10000, 0., 1.);
auto f2 = matplot::randn(10000, 0., 1.);
for (size_t i = 0; i < f1.size(); ++i) {
pf2d(f1[i], f2[i]) = static_cast<unsigned>(i);
}
// Plot a single 2d front
matplot::hold(false);
plot_front(pf2d);
matplot::save("front2d_b.svg");
matplot::show();
// Generate a 2d archive
archive<double, 2, unsigned> ar2d(100, {true, true});
for (size_t i = 0; i < f1.size(); ++i) {
ar2d(f1[i], f2[i]) = static_cast<unsigned>(i);
}
// Plot a single 2d archive
matplot::hold(false);
plot_archive(ar2d);
matplot::save("archive2d.svg");
matplot::show();
// Generate a max/max front
front<double, 2, unsigned> pf2d_max({false, false});
for (size_t i = 0; i < f1.size(); ++i) {
pf2d_max(f1[i], f2[i]) = static_cast<unsigned>(i);
}
// Plot single front
matplot::cla();
matplot::hold(false);
plot_front(pf2d_max);
matplot::save("front2d.svg");
matplot::show();
// Generate a 3d front
front<double, 3, unsigned> pf3d({true, true, true});
auto f3 = matplot::randn(10000, 0., 1.);
for (size_t i = 0; i < f1.size(); ++i) {
pf3d(f1[i], f2[i], f3[i]) = static_cast<unsigned>(i);
}
// Plot a single 3d front
matplot::hold(false);
plot_front(pf3d);
matplot::xlabel("");
matplot::ylabel("");
matplot::save("front3d.svg");
matplot::show();
// Plot 4 fronts with subplots
matplot::cla();
for (size_t i = 0; i < 4; ++i) {
matplot::subplot(2,2,i);
front<double, 2, unsigned> pftm({i < 2, i % 2 == 0});
for (size_t j = 0; j < f1.size(); ++j) {
pftm(f1[j], f2[j]) = static_cast<unsigned>(j);
}
matplot::hold(false);
plot_front(pftm);
if (i < 2) {
matplot::xlabel("Minimize f1");
} else {
matplot::xlabel("Maximize f1");
}
if (i % 2 == 0) {
matplot::ylabel("Minimize f2");
} else {
matplot::ylabel("Maximize f2");
}
}
matplot::save("front2d_directions.svg");
matplot::show();
matplot::hold(false);
// Compare fronts and archives
auto f = matplot::gcf();
f->width(f->width() * 1.5);
auto ax = matplot::subplot(2,2,0);
ax->clear();
matplot::gcf()->current_axes(ax);
plot_front(pf2d);
ax = matplot::subplot(2,2,1);
ax->clear();
matplot::gcf()->current_axes(ax);
plot_front(pf3d);
matplot::xlabel("");
matplot::ylabel("");
ax->x_axis().ticklabels({"f1", "f2", "f3"});
ax = matplot::subplot(2,2,2);
ax->clear();
matplot::gcf()->current_axes(ax);
plot_archive(ar2d);
ax = matplot::subplot(2,2,3);
ax->clear();
matplot::gcf()->current_axes(ax);
archive<double, 3, unsigned> ar3d(100, {true, true, true});
for (size_t i = 0; i < f1.size(); ++i) {
ar3d(f1[i], f2[i], f3[i]) = static_cast<unsigned>(i);
}
plot_archive(ar3d);
matplot::xlabel("");
matplot::ylabel("");
ax->x_axis().ticklabels({"f1", "f2", "f3"});
matplot::save("pareto_cover.svg");
matplot::show();
matplot::hold(false);
return 0;
}