21namespace fpmas {
namespace model {
42 template<
typename AgentType>
87 operator AgentType*()
const {
88 return static_cast<AgentType*
>(_agent->
get());
97 return static_cast<AgentType*
>(_agent->
get());
106 return *
static_cast<AgentType*
>(_agent->
get()); }
125 if(_agent ==
nullptr)
127 return static_cast<AgentType*
>(_agent->
get());
143 template<
typename AgentType,
typename Compare = std::less<AgentType>>
185 template<
typename AgentType>
225 template<
typename AgentType>
228 typedef typename std::vector<Neighbor<AgentType>>::iterator iterator;
229 typedef typename std::vector<Neighbor<AgentType>>::const_iterator const_iterator;
230 std::vector<Neighbor<AgentType>> neighbors;
241 : neighbors(neighbors) {}
249 return neighbors.begin();
257 return neighbors.begin();
266 return neighbors.end();
274 const_iterator
end()
const {
275 return neighbors.end();
284 return neighbors.size();
293 return neighbors.empty();
306 return neighbors.at(i);
307 }
catch(
const std::out_of_range&) {
317 return neighbors.at(i);
318 }
catch(
const std::out_of_range&) {
349 template<
typename Gen>
351 std::shuffle(neighbors.begin(), neighbors.end(), gen);
398 template<
typename Compare = std::less<AgentType>>
401 neighbors.begin(), neighbors.end(),
417 template<
typename Filter>
421 neighbors.begin(), neighbors.end(),
436 template<
typename Gen>
440 return neighbors[index(gen)];
458 template<
typename Gen>
462 return neighbors[index(gen)];
479 template<
typename AgentType>
482 std::vector<void(AgentType::*)()> _behaviors;
484 template<
typename T,
typename Enable=
void>
485 struct AutoAgentCast {
487 return dynamic_cast<T*
>(agent);
492 struct AutoAgentCast<T, typename std::enable_if<
493 std::is_base_of<api::model::Agent, T>::value
496 return static_cast<T*
>(agent);
551 template<
typename ...T>
553 : _behaviors({behaviors...}){
557 for(
auto behavior : this->_behaviors)
558 (AutoAgentCast<AgentType>::cast(agent)->*behavior)();
571 template<
typename AgentType,
typename ... Args>
574 void (AgentType::* behavior)(Args...);
575 std::tuple<Args...> args;
579 template<
int ...>
struct seq {};
580 template<
int N,
int ...S>
struct gens {
582 typedef typename gens<N-1, N-1, S...>::type type;
584 template<
int ...S>
struct gens<0, S...>{
typedef seq<S...> type; };
590 (
dynamic_cast<AgentType*
>(agent)->*behavior)(std::get<S>(args) ...);
602 void (AgentType::* behavior)(Args...),
604 : behavior(behavior), args(args...) {}
613 return call_fn_internal(agent,
typename gens<
sizeof...(Args)>::type());
721 template<
typename AgentInterface,
typename AgentType,
typename TypeIdBase = AgentType>
735 std::vector<api::model::GroupId> group_ids;
736 std::vector<api::model::AgentGroup*> _groups;
740 std::unordered_map<api::model::GroupId, api::model::AgentTask*> _tasks;
821 if(group_ids.size() > 0)
822 return group_ids.back();
828 std::vector<GroupId>
groupIds()
const override {
return group_ids;}
833 group_ids.push_back(
id);}
838 group_ids.push_back(
id);
845 group_ids.erase(std::remove(group_ids.begin(), group_ids.end(),
id));
852 if(_groups.size() > 0)
853 return _groups.back();
859 std::vector<const api::model::AgentGroup*>
groups()
const override {
860 return {_groups.begin(), _groups.end()};
870 std::vector<api::model::AgentGroup*>
groups()
override {
return _groups;}
876 _groups.push_back(
group);
882 _groups.push_back(
group);
889 _groups.erase(std::remove(_groups.begin(), _groups.end(),
group));
898 std::list<api::model::Agent*>::iterator pos
900 group_pos[gid] = pos;
907 return group_pos.find(gid)->second;
918 return new AgentType(*
static_cast<const AgentType*
>(
this));
926 *
static_cast<AgentType*
>(
this) = *
static_cast<const AgentType*
>(agent);
944 *
static_cast<AgentType*
>(
this) = std::move(*
static_cast<AgentType*
>(agent));
977 return _tasks.find(this->
groupId())->second;
983 return _tasks.find(this->
groupId())->second;
996 assert(_tasks.count(
id) > 0);
997 return _tasks.find(
id)->second;
1003 return _tasks.find(
id)->second;}
1015 const std::unordered_map<api::model::GroupId, api::model::AgentTask*>&
1025 virtual void act()
override {}
1041 std::vector<Neighbor<NeighborAgentType>> out;
1043 out.reserve(edges.size());
1046 if(NeighborAgentType* neighbor =
dynamic_cast<NeighborAgentType*
>(_node->
data().get())) {
1047 out.emplace_back(&_node->
data(), _edge);
1067 std::vector<Neighbor<NeighborAgentType>> out;
1069 out.reserve(edges.size());
1072 if(NeighborAgentType* neighbor =
dynamic_cast<NeighborAgentType*
>(_node->
data().get())) {
1073 out.emplace_back(&_node->
data(), _edge);
1094 std::vector<Neighbor<NeighborAgentType>> in;
1096 in.reserve(edges.size());
1099 if(NeighborAgentType* neighbor =
dynamic_cast<NeighborAgentType*
>(_node->
data().get())) {
1100 in.emplace_back(&_node->
data(), _edge);
1120 std::vector<Neighbor<NeighborAgentType>> in;
1122 in.reserve(edges.size());
1125 if(NeighborAgentType* neighbor =
dynamic_cast<NeighborAgentType*
>(_node->
data().get())) {
1126 in.emplace_back(&_node->
data(), _edge);
1139 template<
typename AgentInterface,
typename AgentType,
typename TypeIdBase>
1151 template<
typename AgentType,
typename TypeIdBase = AgentType>
1170 std::vector<api::model::Agent*> agents;
1188 agents.push_back(agent);
1210 return agents.size();
1268 std::size_t agent_count,
1316 std::size_t agent_count,
1341 template<
template<
typename>
class SyncMode>
1405 std::string file_format;
1406 std::fstream stream;
1414 ), std::ios_base::out);
1415 breakpoint.
dump(stream, model);
1442 std::string file_format,
1445 : file_format(file_format), breakpoint(breakpoint), model(model) {
1457namespace fpmas {
namespace io {
namespace json {
1555namespace fpmas {
namespace io {
namespace datapack {
1556 using api::model::AgentPtr;
1752namespace fpmas {
namespace synchro {
1798 std::set<api::model::GroupId> local_ids;
1799 for(
auto id : local_agent->groupIds())
1800 local_ids.insert(
id);
1801 std::set<api::model::GroupId> updated_ids;
1802 for(
auto id : updated_agent->groupIds())
1803 updated_ids.insert(
id);
1805 std::vector<api::model::GroupId> new_groups;
1806 for(
auto id : updated_ids)
1807 if(local_ids.count(
id) == 0)
1808 new_groups.push_back(
id);
1810 std::vector<api::model::GroupId> obsolete_groups;
1811 for(
auto id : local_ids)
1812 if(updated_ids.count(
id) == 0)
1813 obsolete_groups.push_back(
id);
1815 local_agent = std::move(updated_agent);
1821 for(
auto id : new_groups)
1822 local_agent->model()->getGroup(
id).add(local_agent.
get());
1823 for(
auto id : obsolete_groups)
1824 local_agent->model()->getGroup(
id).remove(local_agent.
get());
1840 struct adl_serializer<
fpmas::api::model::AgentPtr> {
Definition: communication.h:251
virtual int getRank() const =0
Definition: distributed_edge.h:91
Definition: distributed_graph.h:169
Definition: distributed_id.h:89
Definition: distributed_node.h:28
Definition: graph_builder.h:23
virtual const std::vector< EdgeType * > getIncomingEdges() const =0
virtual const std::vector< EdgeType * > getOutgoingEdges() const =0
Definition: breakpoint.h:26
virtual void dump(std::ostream &stream, const T &object)=0
virtual GroupId groupId() const =0
virtual api::runtime::Runtime & runtime()=0
virtual api::communication::MpiCommunicator & getMpiCommunicator()=0
virtual Date currentDate() const =0
Definition: scheduler.h:135
T * get()
Definition: ptr_wrapper.h:58
Definition: clustered_graph_builder.h:351
Definition: graph_builder.h:24
Definition: random_load_balancing.h:27
Definition: ring_graph_builder.h:55
Definition: scheduled_load_balancing.h:25
Definition: small_world_graph_builder.h:60
Definition: static_load_balancing.h:22
Definition: zoltan_load_balancing.h:419
Definition: breakpoint.h:26
Definition: datapack.h:301
std::size_t nodeCount() override
Definition: model.h:1209
AgentNodeBuilder(api::model::AgentGroup &group)
Definition: model.h:1178
void push(api::model::Agent *agent)
Definition: model.h:1187
api::model::AgentNode * buildNode(api::graph::DistributedGraph< AgentPtr > &) override
Definition: model.cpp:22
AutoBreakpoint(std::string file_format, api::io::Breakpoint< api::model::Model > &breakpoint, api::model::Model &model)
Definition: model.h:1441
const api::scheduler::Job & job() const
Definition: model.h:1453
void execute(api::model::Agent *agent) const override
Definition: model.h:612
BehaviorWithArgs(void(AgentType::*behavior)(Args...), Args... args)
Definition: model.h:601
Behavior(T... behaviors)
Definition: model.h:552
void execute(api::model::Agent *agent) const
Definition: model.h:556
CompareNeighbors(const Compare &compare)
Definition: model.h:153
bool operator()(const Neighbor< AgentType > &n1, const Neighbor< AgentType > &n2) const
Definition: model.h:159
api::model::AgentNode * buildDistantNode(api::graph::DistributedId id, int location, api::graph::DistributedGraph< AgentPtr > &graph) override
Definition: model.cpp:68
DistributedAgentNodeBuilder(const api::model::GroupList &groups, std::size_t agent_count, std::function< api::model::Agent *()> allocator, api::communication::MpiCommunicator &comm)
Definition: model.cpp:32
api::model::AgentNode * buildNode(api::graph::DistributedGraph< AgentPtr > &graph) override
Definition: model.cpp:57
Neighbor(AgentPtr *agent, AgentEdge *edge)
Definition: model.h:81
AgentType * operator->() const
Definition: model.h:96
AgentEdge * edge() const
Definition: model.h:114
AgentType & operator*() const
Definition: model.h:105
AgentType * agent() const
Definition: model.h:124
Neighbor()
Definition: model.h:62
const Neighbor< AgentType > & at(std::size_t i) const
Definition: model.h:315
Neighbor< AgentType > & operator[](std::size_t i)
Definition: model.h:329
bool empty() const
Definition: model.h:292
Neighbors & shuffle(Gen &gen)
Definition: model.h:350
const_iterator end() const
Definition: model.h:274
iterator begin()
Definition: model.h:248
Neighbor< AgentType > random()
Definition: model.h:451
const Neighbor< AgentType > random() const
Definition: model.h:468
Neighbor< AgentType > random(Gen &gen)
Definition: model.h:437
const Neighbor< AgentType > random(Gen &gen) const
Definition: model.h:459
std::size_t count() const
Definition: model.h:283
Neighbor< AgentType > & at(std::size_t i)
Definition: model.h:304
Neighbors & sort(Compare comp=Compare())
Definition: model.h:399
Neighbors & shuffle()
Definition: model.h:363
const Neighbor< AgentType > & operator[](std::size_t i) const
Definition: model.h:336
iterator end()
Definition: model.h:265
Neighbors & filter(Filter _filter)
Definition: model.h:418
Neighbors(const std::vector< Neighbor< AgentType > > &neighbors)
Definition: model.h:240
const_iterator begin() const
Definition: model.h:256
api::model::AgentTask * task(api::model::GroupId id) override
Definition: model.h:995
void copyAssign(api::model::Agent *agent) override
Definition: model.h:924
static const api::model::TypeId TYPE_ID
Definition: model.h:724
AgentBase & operator=(AgentBase &&)
Definition: model.h:813
Neighbors< NeighborAgentType > inNeighbors() const
Definition: model.h:1093
void setNode(api::model::AgentNode *node) override
Definition: model.h:958
void removeGroupId(api::model::GroupId id) override
Definition: model.h:844
const std::unordered_map< api::model::GroupId, api::model::AgentTask * > & tasks() override
Definition: model.h:1016
void setGroupPos(api::model::GroupId gid, std::list< api::model::Agent * >::iterator pos) override
Definition: model.h:896
api::model::TypeId typeId() const override
Definition: model.h:913
api::model::AgentGroup * group() override
Definition: model.h:851
api::model::Agent * copy() const override
Definition: model.h:917
GroupId groupId() const override
Definition: model.h:820
const api::model::AgentNode * node() const override
Definition: model.h:954
void addGroupId(api::model::GroupId id) override
Definition: model.h:837
AgentBase(AgentBase &&)=default
std::vector< const api::model::AgentGroup * > groups() const override
Definition: model.h:859
void setModel(api::model::Model *model) override
Definition: model.h:971
std::vector< api::model::AgentGroup * > groups() override
Definition: model.h:870
api::model::AgentNode * node() override
Definition: model.h:950
api::model::Model * model() override
Definition: model.h:963
Neighbors< NeighborAgentType > inNeighbors(api::graph::LayerId layer) const
Definition: model.h:1119
std::vector< GroupId > groupIds() const override
Definition: model.h:828
const api::model::AgentTask * task() const override
Definition: model.h:982
Neighbors< NeighborAgentType > outNeighbors(api::graph::LayerId layer) const
Definition: model.h:1066
void removeGroup(api::model::AgentGroup *group) override
Definition: model.h:888
void setTask(api::model::AgentTask *task) override
Definition: model.h:988
void setGroup(api::model::AgentGroup *group) override
Definition: model.h:875
std::list< api::model::Agent * >::iterator getGroupPos(api::model::GroupId gid) const override
Definition: model.h:906
AgentType FinalAgentType
Definition: model.h:732
api::model::AgentTask * task() override
Definition: model.h:976
const api::model::AgentTask * task(api::model::GroupId id) const override
Definition: model.h:1002
void moveAssign(api::model::Agent *agent) override
Definition: model.h:932
Neighbors< NeighborAgentType > outNeighbors() const
Definition: model.h:1040
void setTask(api::model::GroupId id, api::model::AgentTask *task) override
Definition: model.h:1008
const api::model::AgentGroup * group() const override
Definition: model.h:866
AgentBase & operator=(const AgentBase &agent)=default
void setGroupId(api::model::GroupId id) override
Definition: model.h:832
void addGroup(api::model::AgentGroup *group) override
Definition: model.h:881
AgentBase(const AgentBase &agent)=default
virtual void act() override
Definition: model.h:1025
const api::model::Model * model() const override
Definition: model.h:967
Definition: generator.h:322
Generator_t::result_type result_type
Definition: generator.h:359
Definition: distribution.h:24
Definition: scheduler.h:169
Definition: scheduler.h:78
int LayerId
Definition: edge.h:13
int GroupId
Definition: model.h:78
api::utils::PtrWrapper< api::model::Agent > WeakAgentPtr
Definition: model.h:160
std::type_index TypeId
Definition: model.h:82
api::graph::DistributedEdge< AgentPtr > AgentEdge
Definition: model.h:49
std::vector< std::reference_wrapper< AgentGroup > > GroupList
Definition: model.h:833
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
io::Breakpoint< api::model::Model > Breakpoint
Definition: model.h:1395
api::utils::Callback< AgentNode * > AgentNodeCallback
Definition: model.h:1157
bool operator<(const Neighbor< AgentType > &n1, const Neighbor< AgentType > &n2)
Definition: model.h:186
detail::DefaultModel< SyncMode > Model
Definition: model.h:1342
bool is_agent_in_group(api::model::Agent *agent, api::model::GroupId group_id)
Definition: model.cpp:5
std::string format(std::string input, int rank)
Definition: format.cpp:4
static std::size_t size(const LightObjectPack &pack, const AgentPtr &ptr)
static void to_datapack(LightObjectPack &pack, const AgentPtr &pointer)
static AgentPtr from_datapack(const LightObjectPack &pack)
static std::size_t size(const LightObjectPack &pack, const WeakAgentPtr &ptr)
static void to_datapack(LightObjectPack &pack, const WeakAgentPtr &ptr)
static WeakAgentPtr from_datapack(const LightObjectPack &pack)
Definition: datapack.h:1411
static void to_datapack(ObjectPack &pack, const AgentPtr &pointer)
static AgentPtr from_datapack(const ObjectPack &pack)
static std::size_t size(const ObjectPack &p, const AgentPtr &ptr)
static std::size_t size(const ObjectPack &pack, const WeakAgentPtr &ptr)
static WeakAgentPtr from_datapack(const ObjectPack &pack)
static void to_datapack(ObjectPack &pack, const WeakAgentPtr &ptr)
Definition: datapack.h:55
static void to_json(light_json &j, const fpmas::api::model::AgentPtr &pointer)
static fpmas::api::model::AgentPtr from_json(const light_json &j)
static fpmas::api::model::WeakAgentPtr from_json(const light_json &j)
static void to_json(light_json &j, const fpmas::api::model::WeakAgentPtr &pointer)
static random::DistributedGenerator rd
Definition: model.h:204
static void seed(random::DistributedGenerator<>::result_type seed)
Definition: model.cpp:17
static void update(api::model::AgentPtr &local_agent, api::model::AgentPtr &&updated_agent)
Definition: model.h:1789
static fpmas::api::model::AgentPtr from_json(const json &j)
static void to_json(json &j, const fpmas::api::model::AgentPtr &pointer)
static fpmas::api::model::WeakAgentPtr from_json(const json &j)
static void to_json(json &j, const fpmas::api::model::WeakAgentPtr &pointer)