14namespace fpmas {
namespace model {
15 using api::model::DiscretePoint;
49 template<
typename Gr
idCellType,
typename Derived = Gr
idCellType>
51 public CellBase<api::model::GridCell, GridCellType, GridCellBase<GridCellType, Derived>> {
52 friend nlohmann::adl_serializer<fpmas::api::utils::PtrWrapper<GridCellBase<GridCellType, Derived>>>;
95 void seed(random::FPMAS_AGENT_RNG::result_type
seed)
override {
119 template<
typename AgentType,
typename Gr
idCellType = model::Gr
idCell,
typename Derived = AgentType>
121 public SpatialAgentBase<api::model::GridAgent<GridCellType>, AgentType, GridCellType, GridAgent<AgentType, GridCellType, Derived>> {
122 friend nlohmann::adl_serializer<api::utils::PtrWrapper<GridAgent<AgentType, GridCellType, Derived>>>;
124 static_assert(std::is_base_of<api::model::GridCell, GridCellType>::value,
125 "The specified GridCellType must extend api::model::GridCell.");
136 void moveTo(GridCellType* cell)
override;
152 void seed(std::FPMAS_AGENT_RNG::result_type
seed)
override {
157 template<
typename AgentType,
typename Gr
idCellType,
typename Derived>
159 this->updateLocation(cell);
160 location_point = cell->location();
163 template<
typename AgentType,
typename Gr
idCellType,
typename Derived>
166 auto mobility_field = this->
template outNeighbors<GridCellType>(SpatialModelLayers::MOVE);
167 auto it = mobility_field.begin();
168 while(!found && it != mobility_field.end()) {
169 if((*it)->location() == point) {
187 template<
typename Gr
idCellType = Gr
idCell>
199 return new GridCellType(location);
257 template<
typename CellType = Gr
idCell>
261 std::map<DiscretePoint, std::size_t> item_counts;
264 std::map<GridAgentIndex, api::model::GridAgent<CellType>*> agent_index;
394 const std::vector<T>& items,
397 typename std::vector<T>::const_reference
402 template<
typename CellType>
409 this->build(model, groups, [&factory] () {
return factory.
build();}, agent_mapping);
412 template<
typename CellType>
419 for(
auto cell : model.
cells()) {
420 std::size_t count = agent_mapping.
countAt(cell);
423 item_counts[cell->location()]=count;
433 std::map<DiscretePoint, std::size_t> local_counts;
436 std::vector<api::model::GridAgent<CellType>*> agents;
438 this->build_agents(model, groups, [&agents, &factory] {
439 auto agent = factory();
448 for(std::size_t i = 0; i < agents.size(); i++) {
449 std::size_t& offset = local_counts[agents[i]->locationPoint()];
450 GridAgentIndex index(&item_counts, agents[i]->locationPoint(), offset);
451 agent_index.insert({index, agents[i]});
461 agents[i]->seed(agents[i]->locationCell()->rd()());
465 template<
typename CellType>
472 for(
auto index : indexes) {
473 auto it = agent_index.find(index);
474 if(it != agent_index.end())
475 init_function(it->second);
479 template<
typename CellType>
482 const std::vector<T> &items,
485 typename std::vector<T>::const_reference
486 )> init_function)
const {
488 auto map_index = agent_index.
begin();
490 while(map_index != agent_index.end()) {
491 if(map_index->first == grid_index) {
506 template<
typename BuilderType,
typename DistanceType,
typename _CellType = model::Gr
idCell>
547 template<
typename DistanceType,
typename PerimeterType>
622 template<
typename Gr
idConfig,
typename Gr
idRangeConfig>
677 return range_distance(location_cell->location(), cell->location()) <= size;
698 return grid_distance({0, 0}, perimeter(*
this));
702 template<
typename Gr
idConfig,
typename RangeConfig>
704 template<
typename Gr
idConfig,
typename RangeConfig>
705 const typename RangeConfig::Distance GridRange<GridConfig, RangeConfig>::range_distance;
706 template<
typename Gr
idConfig,
typename RangeConfig>
707 const typename RangeConfig::Perimeter GridRange<GridConfig, RangeConfig>::perimeter;
713 template<
typename>
class SyncMode,
715 typename EndCondition = DynamicEndCondition<CellType>>
728 struct adl_serializer<
fpmas::api::model::DiscretePoint> {
736 j = {point.
x, point.
y};
755 template<
typename Gr
idCellType,
typename Derived>
756 struct adl_serializer<
fpmas::api::utils::PtrWrapper<fpmas::model::GridCellBase<GridCellType, Derived>>> {
780 const_cast<Derived*
>(
static_cast<const Derived*
>(ptr.
get())));
782 j[1] = ptr->_location;
813 return derived_ptr.
get();
820 template<
typename AgentType,
typename CellType,
typename Derived>
821 struct adl_serializer<
fpmas::api::utils::PtrWrapper<fpmas::model::GridAgent<AgentType, CellType, Derived>>> {
845 const_cast<Derived*
>(
static_cast<const Derived*
>(ptr.
get())));
847 j[1] = ptr->location_point;
878 return derived_ptr.
get();
884namespace fpmas {
namespace io {
namespace json {
896 template<
typename Gr
idCellType,
typename Derived>
918 const_cast<Derived*
>(
static_cast<const Derived*
>(cell.
get()))
939 return derived_ptr.
get();
954 template<
typename AgentType,
typename CellType,
typename Derived>
977 const_cast<Derived*
>(
static_cast<const Derived*
>(agent.
get()))
998 return derived_ptr.
get();
1004namespace fpmas {
namespace io {
namespace datapack {
1022 template<
typename Gr
idCellType,
typename Derived>
1035 const_cast<Derived*
>(
static_cast<const Derived*
>(ptr.
get())));
1050 const_cast<Derived*
>(
static_cast<const Derived*
>(ptr.
get())));
1053 pack.
put(ptr->_location);
1084 return derived_ptr.
get();
1106 template<
typename AgentType,
typename CellType,
typename Derived>
1119 const_cast<Derived*
>(
static_cast<const Derived*
>(ptr.
get()))))
1134 const_cast<Derived*
>(
static_cast<const Derived*
>(ptr.
get())));
1137 pack.
put(ptr->location_point);
1166 derived_ptr->location_point =
1169 return derived_ptr.
get();
1192 template<
typename Gr
idCellType,
typename Derived>
1206 const_cast<Derived*
>(
static_cast<const Derived*
>(ptr.
get()))
1226 const_cast<Derived*
>(
static_cast<const Derived*
>(cell.
get()))
1247 return derived_ptr.
get();
1271 template<
typename AgentType,
typename CellType,
typename Derived>
1285 const_cast<Derived*
>(
static_cast<const Derived*
>(ptr.
get()))
1305 const_cast<Derived*
>(
static_cast<const Derived*
>(agent.
get()))
1326 return derived_ptr.
get();
1343 static std::size_t size(
const ObjectPack& p);
1356 static void to_datapack(
1374 struct hash<
fpmas::model::DiscretePoint> {
#define FPMAS_AGENT_RNG
Definition: model.h:32
virtual api::communication::MpiCommunicator & getMpiCommunicator()=0
Definition: exceptions.h:32
Definition: spatial_model.h:121
Definition: spatial_model.h:607
virtual SpatialAgent< CellType > * build()=0
virtual std::size_t countAt(CellType *cell)=0
Definition: spatial_model.h:234
virtual std::vector< CellType * > cells()=0
Definition: ptr_wrapper.h:21
T * get()
Definition: ptr_wrapper.h:58
Definition: datapack.h:301
std::size_t size() const
Definition: datapack.h:367
void put(const T &item)
Definition: datapack.h:447
T get() const
Definition: datapack.h:459
Definition: spatial_model.h:215
void initSequence(const std::vector< T > &items, std::function< void(api::model::GridAgent< CellType > *, typename std::vector< T >::const_reference)> init_function) const
Definition: grid.h:481
void build(api::model::SpatialModel< CellType > &model, api::model::GroupList groups, api::model::SpatialAgentFactory< CellType > &factory, api::model::SpatialAgentMapping< api::model::GridCell > &agent_mapping) override
Definition: grid.h:403
void initSample(std::size_t n, std::function< void(api::model::GridAgent< CellType > *)> init_function) const
Definition: grid.h:466
void moveTo(GridCellType *cell) override
Definition: grid.h:158
void seed(std::FPMAS_AGENT_RNG::result_type seed) override
Definition: grid.h:152
random::FPMAS_AGENT_RNG & rd() override
Definition: grid.h:148
DiscretePoint locationPoint() const override
Definition: grid.h:146
GridCellBase()
Definition: grid.h:68
random::FPMAS_AGENT_RNG & rd() override
Definition: grid.h:88
void seed(random::FPMAS_AGENT_RNG::result_type seed) override
Definition: grid.h:95
DiscretePoint location() const override
Definition: grid.h:81
GridCellBase< GridCellType, Derived > JsonBase
Definition: grid.h:63
GridCellBase(DiscretePoint location)
Definition: grid.h:75
GridCellType * build(DiscretePoint location) override
Definition: grid.h:198
std::size_t radius(typename GridConfig::CellType *) const override
Definition: grid.h:697
DiscreteCoordinate getSize() const
Definition: grid.h:644
GridRange(DiscreteCoordinate size)
Definition: grid.h:636
bool contains(typename GridConfig::CellType *location_cell, typename GridConfig::CellType *cell) const override
Definition: grid.h:671
void setSize(DiscreteCoordinate size)
Definition: grid.h:657
Definition: spatial_model.h:756
Definition: spatial_model.h:1087
Definition: spatial_model.h:674
Definition: generator.h:113
UniformRandomBitGenerator< Generator_t >::result_type result_type
Definition: generator.h:120
static Index end(const std::map< DiscretePoint, std::size_t > *item_counts)
Definition: random.h:479
static Index begin(const std::map< DiscretePoint, std::size_t > *item_counts)
Definition: random.h:471
#define FPMAS_DEFAULT_DATAPACK(AGENT)
Definition: datapack_serializer.h:154
#define FPMAS_DEFAULT_JSON(AGENT)
Definition: json_serializer.h:127
std::vector< std::reference_wrapper< AgentGroup > > GroupList
Definition: model.h:833
long DiscreteCoordinate
Definition: grid.h:15
T all_reduce(api::communication::TypedMpi< T > &mpi, const T &data, BinaryOp binary_op=BinaryOp())
Definition: communication.h:634
nlohmann::basic_json< std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, light_serializer > light_json
Definition: json.h:14
random::Index< DiscretePoint > GridAgentIndex
Definition: grid.h:223
std::vector< Index_t > sample_indexes(Index_t begin, Index_t end, std::size_t n, Generator_t &gen)
Definition: random.h:292
void seed(unsigned long seed)
Definition: fpmas.cpp:23
DiscreteCoordinate x
Definition: grid.h:25
DiscreteCoordinate y
Definition: grid.h:29
Definition: communication.h:585
static std::size_t size(const LightObjectPack &p, const Ptr &ptr)
Definition: grid.h:1283
PtrWrapper< fpmas::model::GridAgent< AgentType, CellType, Derived > > Ptr
Definition: grid.h:1276
static void to_datapack(LightObjectPack &pack, const Ptr &agent)
Definition: grid.h:1301
static Ptr from_datapack(const LightObjectPack &pack)
Definition: grid.h:1321
static std::size_t size(const LightObjectPack &p, const Ptr &ptr)
Definition: grid.h:1204
static void to_datapack(LightObjectPack &pack, const Ptr &cell)
Definition: grid.h:1222
static Ptr from_datapack(const LightObjectPack &pack)
Definition: grid.h:1242
PtrWrapper< fpmas::model::GridCellBase< GridCellType, Derived > > Ptr
Definition: grid.h:1197
Definition: datapack.h:1411
static void to_datapack(LightObjectPack &pack, const T &item)
Definition: datapack.h:1438
static T from_datapack(const LightObjectPack &pack)
Definition: datapack.h:1451
static std::size_t size(const ObjectPack &p, const Ptr &ptr)
Definition: grid.h:1117
static Ptr from_datapack(const ObjectPack &pack)
Definition: grid.h:1159
static void to_datapack(ObjectPack &pack, const Ptr &ptr)
Definition: grid.h:1131
PtrWrapper< fpmas::model::GridAgent< AgentType, CellType, Derived > > Ptr
Definition: grid.h:1111
static Ptr from_datapack(const ObjectPack &pack)
Definition: grid.h:1075
static std::size_t size(const ObjectPack &p, const Ptr &ptr)
Definition: grid.h:1033
PtrWrapper< fpmas::model::GridCellBase< GridCellType, Derived > > Ptr
Definition: grid.h:1027
static void to_datapack(ObjectPack &pack, const Ptr &ptr)
Definition: grid.h:1047
Definition: datapack.h:55
PtrWrapper< fpmas::model::GridAgent< AgentType, CellType, Derived > > Ptr
Definition: grid.h:959
static Ptr from_json(const light_json &j)
Definition: grid.h:993
static void to_json(light_json &j, const Ptr &agent)
Definition: grid.h:973
static void to_json(light_json &j, const Ptr &cell)
Definition: grid.h:914
PtrWrapper< fpmas::model::GridCellBase< GridCellType, Derived > > Ptr
Definition: grid.h:901
static Ptr from_json(const light_json &j)
Definition: grid.h:934
static void from_json(const light_json &j, T &data)
Definition: json.h:123
static void to_json(light_json &j, const T &data)
Definition: json.h:107
_CellType CellType
Definition: grid.h:541
DistanceType Distance
Definition: grid.h:533
BuilderType Builder
Definition: grid.h:512
DistanceType Distance
Definition: grid.h:573
PerimeterType Perimeter
Definition: grid.h:594
std::size_t operator()(const DiscretePoint &p) const
Definition: grid.cpp:5
static random::mt19937_64 rd
Definition: grid.h:238
Definition: functional.h:94
static void to_json(nlohmann::json &j, const fpmas::api::model::DiscretePoint &point)
Definition: grid.h:735
static void from_json(const nlohmann::json &j, fpmas::api::model::DiscretePoint &point)
Definition: grid.h:746
fpmas::api::utils::PtrWrapper< fpmas::model::GridAgent< AgentType, CellType, Derived > > Ptr
Definition: grid.h:825
static Ptr from_json(const nlohmann::json &j)
Definition: grid.h:869
static void to_json(nlohmann::json &j, const Ptr &ptr)
Definition: grid.h:842
static Ptr from_json(const nlohmann::json &j)
Definition: grid.h:804
fpmas::api::utils::PtrWrapper< fpmas::model::GridCellBase< GridCellType, Derived > > Ptr
Definition: grid.h:760
static void to_json(nlohmann::json &j, const Ptr &ptr)
Definition: grid.h:777
static const fpmas::model::PointHash hasher
Definition: grid.h:1378