-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.h
168 lines (136 loc) · 4.18 KB
/
agent.h
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
#ifndef PP_AGENT_H
#define PP_AGENT_H
#include "grass.h"
#include "fpmas/model/spatial/moore.h"
#include "config.h"
class PreyPredatorAgentBase {
protected:
static fpmas::random::UniformRealDistribution<> random_real;
int move_cost;
float reproduction_rate;
MooreRange<GridType> mobility_range;
MooreRange<GridType> perception_range;
int energy;
bool alive;
public:
PreyPredatorAgentBase(
int move_cost, float reproduction_rate,
int mobility_range_size, int perception_range_size,
int energy, bool alive) :
move_cost(move_cost), reproduction_rate(reproduction_rate),
mobility_range(mobility_range_size),
perception_range(perception_range_size),
energy(energy), alive(alive) {
}
int getEnergy() const {return energy;}
void kill() {this->alive = false;}
bool isAlive() const {return alive;}
void update(int current_rank, int energy);
virtual void move() = 0;
virtual void reproduce() = 0;
virtual void die() = 0;
virtual void eat() = 0;
};
template<typename AgentType>
class PreyPredatorAgent : public PreyPredatorAgentBase, public GridAgent<AgentType, Grass> {
public:
FPMAS_MOBILITY_RANGE(mobility_range);
FPMAS_PERCEPTION_RANGE(perception_range);
PreyPredatorAgent() : PreyPredatorAgent(AgentType::init_energy, true) {
}
PreyPredatorAgent(int energy, bool alive) :
PreyPredatorAgentBase(
AgentType::move_cost, AgentType::reproduction_rate,
AgentType::mobility_range_size, AgentType::perception_range_size,
energy, alive
) {
}
Neighbors<Grass> reachableCells() const {
return this->mobilityField();
}
AgentType* buildChild(int energy) const {
return new AgentType(energy, true);
}
void move() override {
this->energy -= move_cost;
Grass* new_location = this->reachableCells().random();
this->moveTo(new_location);
#if PP_LOG
std::cout
<< this->model()->runtime().currentDate() << ": "
<< this->node()->getId() << " moves to " << new_location->location()
<< " " << new_location->node()->getId()
<< std::endl;
#endif
}
void reproduce() override {
if(this->random_real(rd) <= reproduction_rate) {
this->energy /= 2;
AgentType* child = this->buildChild(this->energy);
for(auto group : this->groups())
group->add(child);
child->initLocation(this->locationCell());
#if PP_LOG
std::cout
<< this->model()->runtime().currentDate() << ": "
<< "Agent " << this->node()->getId()
<< " reproduces at " << this->locationCell()->location() << ". "
<< "Child: " << child->node()->getId() << std::endl;
#endif
}
}
void die() override {
if(this->energy <= 0)
kill();
if(!isAlive()) {
#if PP_LOG
std::cout
<< this->model()->runtime().currentDate() << ": "
<< this->node()->getId() << " dies." << std::endl;
#endif
auto groups = this->groups();
for(auto group : groups)
group->remove(this);
}
}
static void to_json(nlohmann::json& j, const AgentType* agent) {
j[0] = agent->getEnergy();
j[1] = agent->isAlive();
}
static AgentType* from_json(const nlohmann::json& j) {
return new AgentType(j[0].get<int>(), j[1].get<bool>());
}
static std::size_t size(const fpmas::io::datapack::ObjectPack& p, const AgentType* agent) {
return p.size<int>() + p.size<bool>();
}
static void to_datapack(fpmas::io::datapack::ObjectPack& o, const AgentType* agent) {
o.put(agent->getEnergy());
o.put(agent->isAlive());
}
static AgentType* from_datapack(const fpmas::io::datapack::ObjectPack& o) {
return new AgentType(o.get<int>(), o.get<bool>());
}
};
class Prey : public PreyPredatorAgent<Prey> {
public:
static int init_energy;
static int energy_gain;
static int move_cost;
static float reproduction_rate;
static int mobility_range_size;
static const int perception_range_size;
using PreyPredatorAgent<Prey>::PreyPredatorAgent;
void eat() override;
};
class Predator : public PreyPredatorAgent<Predator> {
public:
static int init_energy;
static int energy_gain;
static int move_cost;
static float reproduction_rate;
static int mobility_range_size;
static int perception_range_size;
using PreyPredatorAgent<Predator>::PreyPredatorAgent;
void eat() override;
};
#endif