46 #include "repast_hpc/SharedContext.h"
47 #include "repast_hpc/Properties.h"
48 #include "repast_hpc/Random.h"
49 #include "repast_hpc/DataSet.h"
50 #include "repast_hpc/RepastProcess.h"
53 #include "RelogoAgent.h"
54 #include "RelogoDiscreteSpaceAdder.h"
55 #include "RelogoContinuousSpaceAdder.h"
56 #include "RelogoLink.h"
57 #include "WorldDefinition.h"
60 #include "agent_set_functions.h"
87 bool operator()(
const std::type_info* one,
const std::type_info* two)
const {
88 return one->before(*two) != 0;
98 template<
typename TargetType>
99 struct Caster:
public std::unary_function<boost::shared_ptr<RelogoAgent>, TargetType*> {
101 TargetType* operator()(boost::shared_ptr<RelogoAgent> ptr)
const {
102 return static_cast<TargetType*
> (ptr.get());
132 virtual void go() = 0;
212 bool spacePtToGridPt(std::vector<double>& spacePt, std::vector<int>& gridPt);
221 template<
typename AgentType>
238 template<
typename AgentType,
typename FactoryFunctor>
250 template<
typename AgentType>
264 template<
typename AgentType>
281 template<
typename AgentType,
typename FactoryFunctor>
282 int create(
size_t count, FactoryFunctor agentCreator);
296 template<
typename AgentType>
322 template<
typename AgentType>
332 template<
typename AgentType>
343 template<
typename AgentType>
356 template<
typename AgentType>
402 template<
typename LinkCreator>
424 template<
typename AgentType>
437 template<
typename AgentType>
451 template<
typename PatchType>
452 PatchType*
patchAt(
int x,
int y);
498 template<
typename PatchType>
508 template<
typename PatchType>
520 template<
typename TurtleType>
534 template<
typename TurtleType>
547 template<
typename AgentType>
568 template<
typename TurtleContent,
typename Prov
ider,
typename Updater,
typename AgentCreator>
569 void synchronizeTurtleStatus(Provider& provider, Updater& updater, AgentCreator& creator, RepastProcess::EXCHANGE_PATTERN exchangePattern = RepastProcess::POLL);
587 template<
typename TurtleContent,
typename Prov
ider,
typename Updater>
590 template<
typename TurtleContent,
typename Prov
ider,
typename Updater,
typename AgentCreator>
591 void synchronize(Provider& provider, Updater& updater, AgentCreator& creator, RepastProcess::EXCHANGE_PATTERN exchangePattern = RepastProcess::POLL
592 #ifdef SHARE_AGENTS_BY_SET
593 ,
bool declareNoAgentsKeptOnAnyProcess =
false
606 std::vector<repast::DataSet*> dataSets;
608 NetworkType* findNetwork(
const std::string& name);
611 const static int NO_TYPE_ID;
614 typedef std::map<const std::type_info*, std::pair<int, int>,
TypeInfoCmp> TypeMap;
615 typedef boost::filter_iterator<IsNotType<RelogoAgent> , Context<RelogoAgent>::const_iterator>
616 const_not_type_iterator;
617 typedef TypeMap::iterator TypeMapIterator;
622 static int nextTypeId;
624 template<
typename Agent>
634 template<
typename AgentType>
639 template<
typename AgentType,
typename FactoryFunctor>
641 const std::type_info* info = &(
typeid(AgentType));
644 TypeMapIterator iter = typeMap.find(info);
645 if (iter == typeMap.end()) {
646 agentTypeId = nextTypeId;
649 agentTypeId = iter->second.first;
650 id = iter->second.second;
653 AgentType* agent = agentCreator(
repast::AgentId(
id, _rank, agentTypeId),
this);
654 agent->hatchCopy(parent);
655 context.addAgent(agent);
656 agent->setxy(parent->
xCor(), parent->
yCor());
657 typeMap[info] = std::make_pair(agentTypeId,
id + 1);
661 template<
typename AgentType>
666 template<
typename AgentType>
667 int Observer::getTypeId() {
668 const std::type_info* info = &(
typeid(AgentType));
669 TypeMapIterator iter = typeMap.find(info);
670 if (iter == typeMap.end())
673 return iter->second.first;
676 template<
typename AgentType,
typename FactoryFunctor>
678 const std::type_info* info = &(
typeid(AgentType));
681 TypeMapIterator iter = typeMap.find(info);
682 if (iter == typeMap.end()) {
683 agentTypeId = nextTypeId;
686 agentTypeId = iter->second.first;
687 id = iter->second.second;
691 for (
size_t i = 0; i < count; i++) {
695 AgentType* agent = agentCreator(agentid,
this);
696 agent->heading((
float) gen.
next());
697 context.addAgent(agent);
699 typeMap[info] = std::make_pair(agentTypeId,
id + count);
703 template<
typename AgentType>
705 return static_cast<AgentType*
> (context.getAgent(
id));
708 template<
typename Agent>
715 template<
typename AgentType>
717 int typeId = getTypeId<AgentType> ();
718 if (typeId != NO_TYPE_ID) {
719 for (SharedContext<RelogoAgent>::const_local_iterator iter = context.localBegin(); iter != context.localEnd(); ++iter) {
720 AgentId id = iter->get()->getId();
721 if (
id.agentType() == typeId) {
722 agentSet.
add(
static_cast<AgentType*
> (iter->get()));
728 template<
typename AgentType>
735 template<
typename AgentType>
737 int typeId = getTypeId<AgentType> ();
738 if (typeId != NO_TYPE_ID) {
739 std::vector<RelogoAgent*> out;
741 filterVecToSet(out, set, typeId);
745 template<
typename LinkCreator>
748 boost::shared_ptr<RelogoLink>
link(creator(source, target));
753 template<
typename AgentType>
755 int typeId = getTypeId<AgentType> ();
756 if (typeId != NO_TYPE_ID) {
758 std::vector<RelogoAgent*> agents;
760 filterVecToSet(agents, out, typeId);
765 template<
typename AgentType>
767 int typeId = getTypeId<AgentType> ();
768 if (typeId != NO_TYPE_ID) {
770 std::vector<RelogoAgent*> agents;
772 filterVecToSet(agents, out, typeId);
776 template<
typename PatchType>
783 template<
typename PatchType>
785 boost::transform_iterator<Caster<PatchType> , Context<RelogoAgent>::const_bytype_iterator> begin(
786 context.byTypeBegin(PATCH_TYPE_ID));
787 boost::transform_iterator<Caster<PatchType> , Context<RelogoAgent>::const_bytype_iterator> end(context.byTypeEnd(
792 template<
typename PatchType>
794 return static_cast<PatchType*
> (
patchAt(x, y));
797 template<
typename TurtleType>
799 int typeId = getTypeId<TurtleType> ();
800 if (typeId != NO_TYPE_ID) {
801 std::vector<RelogoAgent*> in;
803 filterVecToSet(in, out, typeId);
807 template<
typename TurtleType>
809 int typeId = getTypeId<TurtleType> ();
810 if (typeId != NO_TYPE_ID) {
811 std::vector<RelogoAgent*> in;
812 for (AgentSet<RelogoAgent>::const_as_iterator iter = agentSet.
begin(); iter != agentSet.
end(); ++iter) {
816 filterVecToSetNoDuplicates(in, out, typeId);
820 template<
typename AgentType>
823 int typeId = getTypeId<AgentType> ();
824 if (typeId != NO_TYPE_ID) {
825 double radiusSq = radius * radius;
826 std::vector<RelogoAgent*> vec;
828 for (
size_t i = 0; i < inSet.
size(); i++) {
831 outSet.
add(
static_cast<AgentType*
> (
const_cast<RelogoAgent*
> (agent)));
837 template<
typename TurtleContent,
typename Prov
ider,
typename Updater>
842 template<
typename TurtleContent,
typename Prov
ider,
typename Updater,
typename AgentCreator>
847 template<
typename TurtleContent,
typename Prov
ider,
typename Updater,
typename AgentCreator>
848 void Observer::synchronize(Provider& provider, Updater& updater, AgentCreator& creator, RepastProcess::EXCHANGE_PATTERN exchangePattern
849 #ifdef SHARE_AGENTS_BY_SET
850 ,
bool declareNoAgentsKeptOnAnyProcess
854 context.getProjection(SPACE_NAME)->balance();
856 synchronizeTurtleStatus<TurtleContent>(provider, updater, creator, exchangePattern);
858 #ifdef SHARE_AGENTS_BY_SET
869 template<
typename AgentType>
873 return static_cast<AgentType*
> (agent);
double yCor() const
Gets the y coordinate of the agent's location.
Definition: RelogoAgent.cpp:53
DoubleUniformGenerator createUniDoubleGenerator(double from, double to)
Creates a generator that produces doubles in the range [from, to).
Definition: Random.cpp:90
int maxPycor() const
Gets the maximum y coordinate of the patches managed by this Observer.
Definition: Observer.cpp:108
boost::shared_ptr< RelogoLink > link(RelogoAgent *source, RelogoAgent *target, const std::string &networkName)
Gets the link, if any, between the source and target agents in the named network.
Definition: Observer.cpp:271
virtual void successors(V *vertex, std::vector< V * > &out)
Gets the sucessors of the specified vertex and puts them in out.
Definition: Graph.h:384
virtual double getDistanceSq(const Point< GPType > &pt1, const Point< GPType > &pt2) const =0
Gets the square of the distance between the two grid points.
void _setup(Properties &props)
Performs internal Relogo initialization.
Definition: Observer.cpp:127
void successors(RelogoAgent *agent, const std::string &networkName, AgentSet< AgentType > &out)
Gets the network successors of the specified agent in the specified network and puts the result into ...
Definition: Observer.h:766
double randomYcor()
Gets a random y coodinate of the turtles managed by this Observer.
Definition: Observer.cpp:123
void inRadius(const Point< double > ¢er, AgentSet< RelogoAgent > &inSet, double radius, AgentSet< AgentType > &outSet)
Puts all the agents in the inSet that are of the specified type and within the specified radius from ...
Definition: Observer.h:821
void createLink(RelogoAgent *source, RelogoAgent *target, const std::string &networkName)
Creates a link between the source and target agents in the named network.
Definition: Observer.cpp:261
AgentType * hatch(RelogoAgent *parent)
Hatchs an agent of the specified type.
Definition: Observer.h:635
Base agent for Relogo.
Definition: RelogoAgent.h:60
static Random * instance()
Gets the singleton instance of this Random.
Definition: Random.cpp:80
const RelogoSpaceType * space()
Gets the space managed by this Observer.
Definition: Observer.cpp:158
void predecessors(RelogoAgent *agent, const std::string &networkName, AgentSet< AgentType > &out)
Gets the network predecessors of the specified agent in the specified network and puts the result int...
Definition: Observer.h:754
Adapts the templated boost::variate_generator to the NumberGenerator interface.
Definition: Random.h:97
Agent identity information.
Definition: AgentId.h:60
void dataSetClose()
Non API method for closing all the datasets at the end of a sim runs.
Definition: Observer.cpp:81
int minPycor() const
Gets the minimum y coordinate of the patches managed by this Observer.
Definition: Observer.cpp:101
virtual void predecessors(V *vertex, std::vector< V * > &out)
Gets the predecessors of the specified vertex and puts them in out.
Definition: Graph.h:390
Compare two elements of type std::type_info using 'before'.
Definition: Observer.h:86
void addEdge(boost::shared_ptr< E > edge)
Add an edge to this SharedNetwork.
Definition: SharedNetwork.h:231
Specialized indexable collection class for agents.
Definition: AgentSet.h:82
AgentType * who(const AgentId &id)
Gets the agent with the specified id.
Definition: Observer.h:870
int rank() const
Gets the process rank of this Observer.
Definition: Observer.h:364
int currentRank() const
Gets the current process rank of this AgentId.
Definition: AgentId.h:135
virtual repast::AgentId & getId()
Gets the id of this RelogoAgent.
Definition: RelogoAgent.h:93
int randomPxcor()
Gets a random x coodinate of the patches managed by this Observer.
Definition: Observer.cpp:112
virtual void setup(Properties &props)
Classes that extend this should include model initialization here.
Definition: Observer.h:139
void synchronizeTurtleStatus(Provider &provider, Updater &updater, AgentCreator &creator, RepastProcess::EXCHANGE_PATTERN exchangePattern=RepastProcess::POLL)
Synchronizes the status (moved or died) of all turtles across processes.
Definition: Observer.h:843
void removeAgent(const AgentId &id)
Removes the specified turtle from the world.
Definition: Observer.cpp:265
int randomPycor()
Gets a random y coodinate of the patches managed by this Observer.
Definition: Observer.cpp:116
const RelogoGridType * grid()
Gets the grid managed by this Observer.
Definition: Observer.cpp:154
as_iterator end()
Gets an iterator to the end of this AgentSet.
Definition: AgentSet.h:228
Context implementation specialized for the parallel distributed simulation.
Definition: SharedContext.h:115
void addDataSet(repast::DataSet *dataSet)
Adds a dataset to this Observer.
Definition: Observer.cpp:88
virtual int pxCor() const =0
Gets the patch x coordinate of the agent's location.
virtual void go()=0
Called every tick of the simulation.
AgentSet< Turtle > turtles()
Gets all the turtles in this world and return them in the AgentSet.
Definition: Observer.cpp:238
int agentType() const
Gets the agent type component of this AgentId.
Definition: AgentId.h:124
virtual void getObjectsAt(const Point< GPType > &pt, std::vector< T * > &out) const =0
Gets all the objects found at the specified point.
AgentSet< PatchType > patches()
Gets an agent set of the all the patches.
Definition: Observer.h:777
Basic structure for specifying grid dimenions.
Definition: GridDimensions.h:58
virtual int pyCor() const =0
Gets the patch y coordinate of the agent's location.
Creates a the relogo world given some parameters.
Definition: WorldCreator.h:65
PatchType * patchAt(int x, int y)
Gets the patch at the specified coordinates.
Definition: Observer.h:793
Point< double > location() const
Gets the location of this RelogoAgent.
Definition: RelogoAgent.h:111
double next()
Gets the "next" number from this Number Generator.
Definition: Random.h:113
void add(T *agent)
Adds an agent to this AgentSet.
Definition: AgentSet.h:406
AgentSet< AgentType > get()
Gets all of the agents of the templated type and returns them in an AgentSet.
int create(size_t count)
Create count number of agents of the specified type.
Definition: Observer.h:662
void synchronizeTurtleStates(Provider &provider, Updater &updater)
Synchronizes the state of any Turtles that are shared across processes.
Definition: Observer.h:838
as_iterator begin()
Gets an iterator to the begining of this AgentSet.
Definition: AgentSet.h:210
void synchronizeProjectionInfo(SharedContext< T > &context, Provider &provider, Updater &updater, AgentCreator &creator, EXCHANGE_PATTERN exchangePattern=POLL, bool declareNoAgentsKeptOnAnyProcess=false)
Synchronizes the Projection information for shared projections.
Definition: RepastProcess.h:679
void synchronizeAgentStates(Provider &provider, Updater &updater, std::string setName=REQUEST_AGENTS_ALL)
Synchronizes the state values of shared agents.
Definition: RepastProcess.h:614
static RepastProcess * instance()
Gets this RepastProcess.
Definition: RepastProcess.cpp:126
double xCor() const
Gets the x coordinate of the agent's location.
Definition: RelogoAgent.cpp:49
AgentSet< AgentType > turtlesAt(int x, int y)
Gets all of the agents of the templated type at the specified patch location.
Definition: Observer.h:729
size_t size() const
Gets the size of this AgentSet.
Definition: AgentSet.h:192
void addAll(input_iterator begin, input_iterator end)
Adds all the agents from the start iterator through the end to this AgentSet.
Definition: AgentSet.h:396
Unary function used in the transform_iterator that allows context iterators to return the agent maps ...
Definition: Observer.h:99
operator() that creates an agent of type Agent.
Definition: creators.h:61
Map type object that contains key, value(string) properties.
Definition: Properties.h:77
int maxPxcor() const
Gets the maximum x coordinate of the patches managed by this Observer.
Definition: Observer.cpp:105
Implementation of a logo Observer.
Definition: Observer.h:110
Patch * patchAtOffset(Point< double > location, double heading, double distance)
Gets the patch at the heading/distance offset from the specified location or 0 if the resulting locat...
Definition: Observer.cpp:300
Network implementation that can be shared across processes.
Definition: SharedNetwork.h:91
void synchronizeAgentStatus(SharedContext< T > &context, Provider &provider, Updater &updater, AgentCreator &creator, EXCHANGE_PATTERN exchangePattern=POLL)
Synchronizes the status (moved or died) of all agents across processes.
Definition: RepastProcess.h:892
A logo patch.
Definition: Patch.h:58
double randomXcor()
Gets a random x coodinate of the turtles managed by this Observer.
Definition: Observer.cpp:120
Relogo Turtle implementation.
Definition: Turtle.h:59
Interface for recording and writing data.
Definition: DataSet.h:49
void turtlesOn(AgentSet< RelogoAgent > &agentSet, AgentSet< TurtleType > &out)
Gets all the turtles that are on any patches contained in the agentSet or on the patches where any tu...
Definition: Observer.h:808
int minPxcor() const
Gets the minimum x coordinate of the patches managed by this Observer.
Definition: Observer.cpp:98