14template<
typename CellType>
23 fpmas::model::Neighbors<CellType>& mobility_field)
const = 0;
32template<
typename CellType>
35 fpmas::model::Neighbors<CellType>& mobility_field)
const override;
43template<
typename CellType>
46 fpmas::model::Neighbors<CellType>& mobility_field)
const override;
49template<
typename CellType>
51 fpmas::model::Neighbors<CellType> &mobility_field)
const {
52 std::vector<float> utilities;
53 std::vector<CellType*> cells;
55 bool null_utilities =
true;
56 for(
auto cell : mobility_field) {
57 fpmas::model::ReadGuard read(cell);
58 if(cell->getUtility() > 0)
59 null_utilities =
false;
60 utilities.push_back(cell->getUtility());
61 cells.push_back(cell);
65 fpmas::random::UniformIntDistribution<std::size_t> rd_cell(0, cells.size()-1);
66 rd_index = rd_cell(fpmas::model::RandomNeighbors::rd);
68 fpmas::random::DiscreteDistribution<std::size_t> rd_cell(utilities);
69 rd_index = rd_cell(fpmas::model::RandomNeighbors::rd);
71 return cells[rd_index];
74template<
typename CellType>
76 fpmas::model::Neighbors<CellType> &mobility_field)
const {
78 mobility_field.shuffle();
80 std::vector<std::pair<CellType*, float>> cells;
81 for(
auto cell : mobility_field) {
82 fpmas::model::ReadGuard read(cell);
83 cells.push_back({cell, cell->getUtility()});
86 return std::max_element(cells.begin(), cells.end(),
88 const std::pair<CellType*, float>& a1,
89 const std::pair<CellType*, float>& a2
91 return a1.second < a2.second;
127 std::deque<DistributedId> _contacts;
148 virtual const fpmas::api::model::AgentNode*
agentNode()
const = 0;
180template<
typename AgentBase,
typename PerceptionRange>
183 PerceptionRange range;
189 void add_to_contacts(fpmas::api::model::Agent* agent);
244 const fpmas::api::model::AgentNode*
agentNode()
const override {
245 return this->AgentBase::node();
249template<
typename AgentBase,
typename PerceptionRange>
251 if(contacts().size() == max_contacts) {
252 for(
auto edge : this->node()->getOutgoingEdges(CONTACT)) {
254 if(edge->getTargetNode()->getId() == contacts().front()) {
255 this->model()->graph().unlink(edge);
261 contacts().pop_front();
266 contacts().push_back(agent->node()->getId());
269template<
typename AgentBase,
typename PerceptionRange>
272 auto perceptions = this->perceptions();
275 perceptions.shuffle();
277 auto current_perception = perceptions.begin();
280 while(current_perception != perceptions.end()) {
281 if(!is_in_contacts(current_perception->agent()->node()->getId())) {
282 add_to_contacts(*current_perception);
285 current_perception = perceptions.end();
287 current_perception++;
292template<
typename AgentBase,
typename PerceptionRange>
295 this->
template outNeighbors<MetaAgent<AgentBase, PerceptionRange>>(CONTACT);
296 if(contacts.count() >= 2) {
298 std::size_t random_index =
299 fpmas::random::UniformIntDistribution<std::size_t>(0, contacts.count()-1)(
300 fpmas::model::RandomNeighbors::rd
302 std::swap(contacts[0], contacts[random_index]);
308 fpmas::model::ReadGuard read(contacts[0]);
312 i < contacts.count() &&
313 contacts[0]->is_in_contacts(contacts[i]->node()->getId()))
316 if(i < contacts.count())
320 this->model()->link(contacts[0], contacts[i], NEW_CONTACT);
324template<
typename AgentBase,
typename PerceptionRange>
327 this->
template outNeighbors<MetaAgent<AgentBase, PerceptionRange>>(NEW_CONTACT);
328 for(
auto new_contact : new_contacts) {
330 add_to_contacts(new_contact);
333 this->model()->unlink(new_contact.edge());
337template<
typename AgentBase,
typename PerceptionRange>
339 auto mobility_field = this->mobilityField();
340 typename AgentBase::Cell* selected_cell;
341 switch(move_policy) {
352 this->moveTo(selected_cell);
361template<
typename AgentType>
366 static void to_json(nlohmann::json& j,
const AgentType* agent);
370 static AgentType*
from_json(
const nlohmann::json& j);
375 static std::size_t
size(
const fpmas::io::datapack::ObjectPack& o,
const AgentType* agent);
380 fpmas::io::datapack::ObjectPack& o,
const AgentType* agent);
384 static AgentType*
from_datapack(
const fpmas::io::datapack::ObjectPack& o);
387template<
typename AgentType>
389 j = agent->contacts();
392template<
typename AgentType>
394 return new AgentType(j.get<std::deque<DistributedId>>());
397template<
typename AgentType>
399 const fpmas::io::datapack::ObjectPack &o,
const AgentType *agent) {
400 return o.size(agent->contacts());
403template<
typename AgentType>
405 fpmas::io::datapack::ObjectPack& o,
const AgentType* agent) {
406 o.put(agent->contacts());
409template<
typename AgentType>
411 const fpmas::io::datapack::ObjectPack &o) {
412 return new AgentType(o.get<std::deque<DistributedId>>());
420 GridAgent<MetaGridAgent, MetaGridCell>,
421 MooreRange<MooreGrid<MetaGridCell>>>,
425 GridAgent<MetaGridAgent, MetaGridCell>,
434 SpatialAgent<MetaGraphAgent, MetaGraphCell>, GraphRange<MetaGraphCell>
438 SpatialAgent<MetaGraphAgent, MetaGraphCell>, GraphRange<MetaGraphCell>
MovePolicy
Definition: config.h:99
CellType * selectCell(fpmas::model::Neighbors< CellType > &mobility_field) const override
Definition: agent.h:75
virtual CellType * selectCell(fpmas::model::Neighbors< CellType > &mobility_field) const =0
CellType * selectCell(fpmas::model::Neighbors< CellType > &mobility_field) const override
Definition: agent.h:50