-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMikroPF.cpp
465 lines (424 loc) · 12.7 KB
/
MikroPF.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// -----------------------------------------------------------------------------
// Copyright (c) 2022 Mohamed Aladem
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this softwareand associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright noticeand this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// -----------------------------------------------------------------------------
#include "MikroPF.h"
#include <chrono>
#include <random>
#include <thread>
#include <mutex>
#include <condition_variable>
namespace
{
inline int int_div(int a, int b)
{
return ((a + b - 1) / b);
}
}
struct ParticleChunk
{
std::vector<MikroPF::ParticleState>* particles;
std::vector<MikroPF::Real>* weights;
int begin, end;
MikroPF::Real weight_sum;
MikroPF::Real weight_sum_sq;
bool done;
};
class MikroPF::ExecutionEngine
{
public:
enum class Command
{
idle,
run_prior,
run_prediction_likelihood,
normalize_weights,
terminate
};
ExecutionEngine(int num_threads, int num_particles,
int state_dim, Real effective_sample_size_th);
~ExecutionEngine();
bool init() noexcept;
void execute(Command cmd);
MikroPF::PriorFn m_prior_fn = nullptr;
MikroPF::PredictionFn m_prediction_fn = nullptr;
MikroPF::LikelihoodFn m_likelihood_fn = nullptr;
MikroPF::ResamplingFn m_resampling_fn = nullptr;
const Real* m_measurement = nullptr;
void* m_optional_data = nullptr;
std::vector<MikroPF::ParticleState> m_particles;
std::vector<Real> m_state;
std::vector<Real> m_weights;
std::vector<std::thread> m_threads;
std::vector<ParticleChunk> m_chunks;
std::mutex m_mutex;
std::condition_variable m_to_workers_cv;
std::condition_variable m_from_workers_cv;
int m_finished_counter = 0;
Command m_command = Command::idle;
const int m_num_threads;
const int m_num_particles;
const int m_state_dim;
const Real m_effective_sample_size_th;
void thread_main(ParticleChunk* chunk);
void run_prior(ParticleChunk* chunk);
void run_prediction_likelihood(ParticleChunk* chunk);
void normalize_weights(ParticleChunk* chunk);
void resample_from_indices(const std::vector<int>& indices);
};
MikroPF::ExecutionEngine::ExecutionEngine(int num_threads, int num_particles,
int state_dim, Real effective_sample_size_th) :
m_num_threads(num_threads), m_num_particles(num_particles), m_state_dim(state_dim),
m_effective_sample_size_th(effective_sample_size_th)
{
}
MikroPF::ExecutionEngine::~ExecutionEngine()
{
if (!m_threads.empty())
{
m_command = Command::terminate;
m_to_workers_cv.notify_all();
for (auto& th : m_threads)
{
if (th.joinable())
{
th.join();
}
}
}
}
bool MikroPF::ExecutionEngine::init() noexcept
{
try
{
m_weights.resize(m_num_particles);
m_particles.resize(m_num_particles);
m_state.resize(m_num_particles * m_state_dim);
for (int i = 0; i < m_num_particles; ++i)
{
m_particles[i].m_state = &m_state;
m_particles[i].m_begin = i * m_state_dim;
m_particles[i].m_end = (i + 1) * m_state_dim;
}
if (m_num_threads > 1)
{
m_chunks.resize(m_num_threads);
m_threads.reserve(m_num_threads);
const int particles_per_thread = int_div(m_num_particles, m_num_threads);
for (int i = 0; i < m_num_threads; ++i)
{
m_chunks[i].begin = i * particles_per_thread;
m_chunks[i].end = std::min(m_chunks[i].begin + particles_per_thread, m_num_particles);
m_chunks[i].particles = &m_particles;
m_chunks[i].weights = &m_weights;
m_chunks[i].done = false;
m_threads.push_back(std::thread(&MikroPF::ExecutionEngine::thread_main, this, &m_chunks[i]));
}
}
}
catch (...)
{
return false;
}
return true;
}
void MikroPF::ExecutionEngine::execute(Command cmd)
{
assert(cmd != Command::terminate);
{
std::unique_lock<std::mutex> lock(m_mutex);
m_command = cmd;
m_to_workers_cv.notify_all();
}
std::unique_lock<std::mutex> lock(m_mutex);
m_from_workers_cv.wait(lock, [&]() { return m_num_threads == m_finished_counter; });
m_finished_counter = 0;
m_command = Command::idle;
for (auto& chunk : m_chunks)
{
chunk.done = false;
}
}
void MikroPF::ExecutionEngine::thread_main(ParticleChunk* chunk)
{
while (true)
{
{
std::unique_lock<std::mutex> lock(m_mutex);
m_to_workers_cv.wait(lock, [&]() { return m_command != Command::idle && !chunk->done; });
}
if (m_command == Command::terminate)
{
break;
}
switch (m_command)
{
case Command::run_prior:
run_prior(chunk);
break;
case Command::run_prediction_likelihood:
run_prediction_likelihood(chunk);
break;
case Command::normalize_weights:
normalize_weights(chunk);
break;
}
chunk->done = true;
{
std::unique_lock<std::mutex> lock(m_mutex);
m_finished_counter++;
m_from_workers_cv.notify_one();
}
}
}
void MikroPF::ExecutionEngine::run_prior(ParticleChunk* chunk)
{
for (int i = chunk->begin; i < chunk->end; ++i)
{
m_prior_fn(&(*chunk->particles)[i]);
}
const Real w = Real(1) / Real(m_num_particles);
for (int i = chunk->begin; i < chunk->end; ++i)
{
(*chunk->weights)[i] = w;
}
}
void MikroPF::ExecutionEngine::run_prediction_likelihood(ParticleChunk* chunk)
{
chunk->weight_sum = Real(0);
for (int i = chunk->begin; i < chunk->end; ++i)
{
m_prediction_fn(&(*chunk->particles)[i], m_optional_data);
Real likelihood = Real(1);
if (m_measurement)
{
m_likelihood_fn(&(*chunk->particles)[i], m_measurement, &likelihood, m_optional_data);
}
const Real new_weight = (*chunk->weights)[i] * likelihood;
assert(new_weight >= 0);
(*chunk->weights)[i] = new_weight;
chunk->weight_sum += new_weight;
}
}
void MikroPF::ExecutionEngine::normalize_weights(ParticleChunk* chunk)
{
chunk->weight_sum_sq = Real(0);
for (int i = chunk->begin; i < chunk->end; ++i)
{
const Real new_w = (*chunk->weights)[i] / chunk->weight_sum;
(*chunk->weights)[i] = new_w;
chunk->weight_sum_sq += (new_w * new_w);
}
}
void MikroPF::ExecutionEngine::resample_from_indices(const std::vector<int>& indices)
{
assert(indices.size() == m_num_particles);
assert(std::is_sorted(indices.begin(), indices.end()));
for (int i = 0; i < m_num_particles; ++i)
{
const int new_idx = indices[i];
if (i == new_idx)
{
continue;
}
const MikroPF::ParticleState& p = m_particles[new_idx];
for (int j = 0; j < m_state_dim; ++j)
{
m_state[i * m_state_dim + j] = p.GetAt(j);
}
}
const Real w = Real(1) / Real(m_num_particles);
for (int i = 0; i < m_num_particles; ++i)
{
m_weights[i] = w;
}
}
// ==================================================================================
namespace
{
using Real = MikroPF::Real;
void update_multi_threaded(MikroPF::ExecutionEngine* e, const MikroPF::Real* measurement, void* optional_data)
{
e->m_measurement = measurement;
e->m_optional_data = optional_data;
e->execute(MikroPF::ExecutionEngine::Command::run_prediction_likelihood);
e->m_measurement = nullptr;
e->m_optional_data = nullptr;
Real total_weight{};
for (const auto& chunk : e->m_chunks)
{
total_weight += chunk.weight_sum;
}
for (auto& chunk : e->m_chunks)
{
chunk.weight_sum = total_weight;
}
e->execute(MikroPF::ExecutionEngine::Command::normalize_weights);
Real total_weight_sq{};
for (const auto& chunk : e->m_chunks)
{
total_weight_sq += chunk.weight_sum_sq;
}
const Real effective_sample_size = (Real(1) / total_weight_sq) / Real(e->m_num_particles);
if (effective_sample_size < e->m_effective_sample_size_th)
{
std::vector<int> indices(e->m_num_particles);
e->m_resampling_fn(e->m_weights, &indices);
e->resample_from_indices(indices);
}
}
void update_single_threaded(MikroPF::ExecutionEngine* e, const MikroPF::Real* measurement, void* optional_data)
{
Real weight_sum{};
for (int i = 0; i < e->m_num_particles; ++i)
{
e->m_prediction_fn(&e->m_particles[i], optional_data);
Real likelihood = Real(1);
if (measurement)
{
e->m_likelihood_fn(&e->m_particles[i], measurement, &likelihood, optional_data);
}
Real new_weight = e->m_weights[i] * likelihood;
assert(new_weight >= 0);
e->m_weights[i] = new_weight;
weight_sum += new_weight;
}
Real total_weight_sq{};
for (int i = 0; i < e->m_num_particles; ++i)
{
const Real new_w = e->m_weights[i] / weight_sum;
e->m_weights[i] = new_w;
total_weight_sq += (new_w * new_w);
}
const Real effective_sample_size = (Real(1) / total_weight_sq) / Real(e->m_num_particles);
if (effective_sample_size < e->m_effective_sample_size_th)
{
std::vector<int> indices(e->m_num_particles);
e->m_resampling_fn(e->m_weights, &indices);
e->resample_from_indices(indices);
}
}
int compute_num_threads(int requested_threads)
{
int num_threads = requested_threads == 0 ? 1 : requested_threads;
if (num_threads < 0)
{
int hw_threads = std::thread::hardware_concurrency();
num_threads = std::max(1, hw_threads);
}
return num_threads;
}
}
// ==================================================================================
MikroPF::MikroPF() = default;
MikroPF::~MikroPF() = default;
bool MikroPF::init(
int state_dim,
int num_particles,
PriorFn prior_fn,
PredictionFn prediction_fn,
LikelihoodFn likelihood_fn,
ResamplingFn resampling_fn,
Real effective_sample_size_th,
int requested_num_threads)
{
if (state_dim < 1
|| num_particles < 1
|| !prior_fn
|| !prediction_fn
|| !likelihood_fn
|| !resampling_fn
|| effective_sample_size_th < 0
|| effective_sample_size_th > 1)
{
return false;
}
const int num_threads = compute_num_threads(requested_num_threads);
m_engine = std::make_unique<ExecutionEngine>(num_threads, num_particles, state_dim, effective_sample_size_th);
m_engine->m_prior_fn = prior_fn;
m_engine->m_prediction_fn = prediction_fn;
m_engine->m_likelihood_fn = likelihood_fn;
m_engine->m_resampling_fn = resampling_fn;
if (!m_engine->init())
{
return false;
}
m_update_cb = num_threads == 1 ? update_single_threaded : update_multi_threaded;
reset();
return true;
}
void MikroPF::update(const Real * measurement, void* optional_data)
{
m_update_cb(m_engine.get(), measurement, optional_data);
}
void MikroPF::apply(std::function<void(ParticleState* particle_state, Real* particle_weight)> callback)
{
for (int i = 0; i < m_engine->m_num_particles; ++i)
{
callback(&m_engine->m_particles[i], &m_engine->m_weights[i]);
}
}
void MikroPF::reset()
{
if (m_engine->m_num_threads == 1)
{
const Real w = Real(1) / Real(m_engine->m_num_particles);
for (int i = 0; i < m_engine->m_num_particles; ++i)
{
m_engine->m_weights[i] = w;
m_engine->m_prior_fn(&m_engine->m_particles[i]);
}
}
else
{
m_engine->execute(ExecutionEngine::Command::run_prior);
}
}
void MikroPF::systematic_resampling(const std::vector<Real>&weights, std::vector<int>*out_indices)
{
std::vector<Real> cumulative_sum = weights;
const int N = cumulative_sum.size();
for (int i = 1; i < N; ++i)
{
cumulative_sum[i] += cumulative_sum[i - 1];
}
cumulative_sum.back() = Real(1);
Real random_offset{};
{
static std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_real_distribution<Real> dist(0, 1);
random_offset = dist(generator);
}
const Real N_inv = Real(1) / N;
Real sample_pos = random_offset * N_inv;
for (int i = 0, j = 0; i < N;)
{
if (sample_pos < cumulative_sum[j])
{
(*out_indices)[i] = j;
++i;
sample_pos = (i + random_offset) * N_inv;
}
else
{
++j;
}
}
}