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
|
#pragma once
// #define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
// #include <execution>
#include <numbers>
#include <random>
/*
a, sigma
*/
// #define POP_SIZE 30
// #define NUM_IT 25
// #define COL_SIZE 16
// using Column = double;
static std::random_device random_device;
static std::mt19937 gen(random_device());
static std::uniform_real_distribution<> dis01(0., 1.);
static std::uniform_real_distribution<> disDelta2(-2., 2.);
template<typename T, size_t COL_SIZE = 16>
struct Item {
double a, sigma; // E, sigma = sqrt(D)
T* column;
double W;
Item() {}
Item(double a_, double s, T * column_) : a(a_), sigma(s), column(column_) {
update();
}
double gauss(double t) {
return std::exp(std::pow((t - a) / sigma,
2) /
2) /
std::sqrt(2 * std::numbers::pi) /
sigma;
}
double F() { // objective function
double s = 0;
int x0 = std::floor(.5 + a - 3 * sigma);
double x1 = std::round(a + 3 * sigma);
for (int j = x0; j <= x1; j++)
s += std::pow(gauss(j) - column[j], 2);
return s;
}
void update() {
W = F();
}
// action
Item move() {
double a1 = a + disDelta2(gen);
double sigma1 = sigma * (1 + disDelta2(gen)); // a: ~ +- 2 pixel
return Item(a1, sigma1, column);
}
// a = q * a1 + (1 - q) * a2, sigma = q * s1 + (1 - q) * s2
Item crossover(const Item& other) {
double q = dis01(gen);
double a_ = q * a + (1 - q) * other.a;
double sigma_ = q * sigma + (1 - q) * other.sigma;
return Item(a_, sigma_, column);
}
};
template <typename T = uint16_t,
size_t POP_SIZE = 30,
size_t COL_SIZE = 16,
size_t NUM_IT = 25,
double maxW = .01>
struct Algo {
T * column;
using I = Item<T, COL_SIZE>;
std::vector<I> population;
std::uniform_real_distribution<double> disA { 0., double(1.) };
double targetW;
Algo(T * column_): column(column_) {
init();
}
I getNewItem() {
double a = rand() % (COL_SIZE * 1000) / 1000.;
double sigma = rand() % (COL_SIZE * 100) / 1000.;
return I(a, sigma, column);
}
void init() {
for (size_t i = 0; i < POP_SIZE; i++) {
population.push_back(getNewItem());
}
}
bool stopCondition() {
// return population[0].W <= targetW;
return population[0].W <= maxW;
}
I run() {
for (int it = 0; it < NUM_IT; it++) {
work();
// if (stopCondition())
// break;
}
return population[0];
}
void work() {
move();
crossover();
addNew();
sort();
remove();
}
void sort() {
// sort vector ASC
std::sort(population.begin(), population.end(),
[](const auto & a, const auto & b)
{
return a.W < b.W;
});
}
void remove() {
population.erase(population.begin() + POP_SIZE, population.end());
}
void move() {
for (I& item : population) {
population.push_back(item.move());
}
}
void addNew() {
for (int i = 0; i < 5; i++) {
population.push_back(getNewItem());
}
}
void crossover() {
for (int i1 = 0; i1 < 4; i1++) {
for (int i2 = i1 + 1; i2 < 4; i2++) {
I& x1 = population[i1];
I& x2 = population[i2];
// a = q * a1 + (1 - q) * a2, sigma = q * s1 + (1 - q) * s2
population.push_back(x1.crossover(x2));
}
}
}
};
|