-
Notifications
You must be signed in to change notification settings - Fork 0
/
rummy_simulation.cpp
executable file
·113 lines (91 loc) · 3.29 KB
/
rummy_simulation.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
#include "rummy_simulation.hpp"
#include <tools/random/random_generator.hpp>
#include <numeric>
#ifdef USE_TBB
#include <tbb/tbb.h>
#include <oneapi/tbb/parallel_for.h>
#endif
namespace simu {
namespace simulation {
RummySimulation::RummySimulation(defaults::RummySimuParams params)
: Simulation(params), _params(params)
{
reinit();
}
void RummySimulation::reinit()
{
_iteration = 0;
_is_kicking = false;
if (_fish.size() != _params.num_fish) {
_fish.clear();
_fish.resize(static_cast<size_t>(_params.num_fish));
for (size_t i = 0; i < _fish.size(); ++i) {
_fish[i] = std::make_shared<RummyIndividual>(i);
}
}
else {
for (size_t i = 0; i < _fish.size(); ++i) {
_fish[i]->reinit();
}
}
// log the initial position
_update_stats(std::make_shared<RummySimulation>(*this));
}
void RummySimulation::spin_once()
{
_t_next_kick = std::numeric_limits<float>::max();
for (size_t i = 0; i < _fish.size(); ++i) {
if ((_fish[i]->t0() + _fish[i]->kick_duration()) < _t_next_kick) {
_t_next_kick = _fish[i]->t0() + _fish[i]->kick_duration();
_next_kicker_idx = i;
}
}
_is_kicking = false;
// no fish kicking yet
if ((_iteration + 1) * _sim_settings.timestep <= _t_next_kick) {
for (size_t i = 0; i < _fish.size(); ++i) {
_fish[i]->coast(std::make_shared<RummySimulation>(*this));
}
}
// compute new kick
else {
_is_kicking = true;
for (size_t i = 0; i < _fish.size(); ++i) {
_fish[i]->prepare_kick(std::make_shared<RummySimulation>(*this));
}
_fish[_next_kicker_idx]->kick(std::make_shared<RummySimulation>(*this));
}
// update statistics
_update_stats(std::make_shared<RummySimulation>(*this));
_update_descriptors(std::make_shared<RummySimulation>(*this));
if (!_is_kicking) {
// if ((_iteration % 10000) == 0) {
// std::cout << "Iteration: " << _iteration << std::endl;
// }
Simulation::spin_once();
}
}
std::vector<RummyIndividualPtr> RummySimulation::fish() const { return _fish; }
std::vector<RummyIndividualPtr>& RummySimulation::fish() { return _fish; }
float RummySimulation::t_next_kick() const
{
return _t_next_kick;
}
const defaults::RummySimuParams RummySimulation::params() const
{
return _params;
}
defaults::RummySimuParams& RummySimulation::params()
{
return _params;
}
int RummySimulation::next_kicker_idx() const
{
return _next_kicker_idx;
}
bool RummySimulation::is_kicking() const
{
return _is_kicking;
}
} // namespace simulation
} // namespace simu