RepastHPC  2.3.1
WorldCreator.h
1 
2 /*
3  * Repast for High Performance Computing (Repast HPC)
4  *
5  * Copyright (c) 2010 Argonne National Laboratory
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with
9  * or without modification, are permitted provided that the following
10  * conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the Argonne National Laboratory nor the names of its
20  * contributors may be used to endorse or promote products derived from
21  * this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  *
36  * WorldCreator.h
37  *
38  * Created on: Aug 5, 2010
39  * Author: nick
40  */
41 
42 #ifndef WORLDCREATOR_H_
43 #define WORLDCREATOR_H_
44 
45 #include <vector>
46 
47 #include <boost/mpi/communicator.hpp>
48 
49 #include "repast_hpc/SharedSpaces.h"
50 
51 #include "WorldDefinition.h"
52 #include "RelogoDiscreteSpaceAdder.h"
53 #include "RelogoContinuousSpaceAdder.h"
54 #include "creators.h"
55 #include "relogo.h"
56 #include "grid_types.h"
57 
58 namespace repast {
59 
60 namespace relogo {
61 
65 class WorldCreator {
66 
67  boost::mpi::communicator* comm;
68 
69 public:
70  WorldCreator(boost::mpi::communicator* communicator):comm(communicator) {}
71  virtual ~WorldCreator() {}
72 
87  template<typename ObsType, typename PatchType, typename PatchCreator>
88  ObsType* createWorld(const WorldDefinition& worldDef, const std::vector<int>& pConfig, PatchCreator& patchCreator);
89 
100  template<typename ObsType, typename PatchType>
101  ObsType* createWorld(const WorldDefinition& worldDef, const std::vector<int>& pConfig);
102 
103 private:
104 
105  repast::Projection<RelogoAgent>* createDiscreteSpace(const WorldDefinition&, const std::vector<int>&) const;
106  repast::Projection<RelogoAgent>* createContinuousSpace(const WorldDefinition&, const std::vector<int>&) const;
107 };
108 
109 template<typename ObsType, typename PatchType>
110 ObsType* WorldCreator::createWorld(const WorldDefinition& worldDef, const std::vector<int>& pConfig) {
112  return createWorld<ObsType, PatchType> (worldDef, pConfig, pCreator);
113 }
114 
115 template<typename ObsType, typename PatchType, typename PatchCreator>
116 ObsType* WorldCreator::createWorld(const WorldDefinition& worldDef, const std::vector<int>& pConfig,
117  PatchCreator& pCreator) {
118  ObsType* observer = new ObsType();
119 
120  observer->context.addProjection(createDiscreteSpace(worldDef, pConfig));
121 
122  Projection<RelogoAgent>* spaceProj = createContinuousSpace(worldDef, pConfig);
123  observer->context.addProjection(spaceProj);
124  std::string projName = spaceProj->name();
125  std::vector<std::string>::iterator iter = observer->context.getAgentsToPushProjOrder.begin();
126  while(iter != observer->context.getAgentsToPushProjOrder.end()){
127  if(*iter == projName) iter = observer->context.getAgentsToPushProjOrder.erase(iter);
128  else iter++;
129  }
130 
131 
132  RelogoGridType* grid = static_cast<RelogoGridType*> (observer->context.getProjection(GRID_NAME));
133  RelogoSpaceType* space = static_cast<RelogoSpaceType*> (observer->context.getProjection(SPACE_NAME));
134  observer->localBounds = grid->dimensions();
135 
136  int id = 0;
137 
138  GridDimensions dims = grid->dimensions();
139  for (int x = dims.origin(0), n = dims.origin(0) + dims.extents(0); x < n; ++x) {
140  for (int y = dims.origin(1), k = dims.origin(1) + dims.extents(1); y < k; ++y) {
141  repast::AgentId agentId(id, observer->rank(), PATCH_TYPE_ID);
142  agentId.currentRank(observer->_rank);
143  id++;
144  PatchType* patch = pCreator(agentId, observer);
145  observer->context.addAgent(patch);
146  patch->_location = Point<double>(x, y);
147  grid->moveTo(patch->getId(), Point<int> (x, y));
148  space->moveTo(patch->getId(), Point<double> (x, y));
149  }
150  }
151 
152  for (WorldDefinition::proj_iter iter = worldDef.networks_begin(); iter != worldDef.networks_end(); ++iter) {
153  observer->context.addProjection(*iter);
154  }
155 
156  return observer;
157 }
158 
159 }
160 
161 }
162 
163 #endif /* WORLDCREATOR_H_ */
repast::GridDimensions::extents
const Point< double > & extents() const
Gets the extents along each dimension.
Definition: GridDimensions.h:90
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::GridDimensions::origin
const Point< double > & origin() const
Gets the origin.
Definition: GridDimensions.h:83
repast::relogo::WorldDefinition::networks_end
proj_iter networks_end() const
Gets the end of an iterator over the network Projections defined in this WorldDefinition.
Definition: WorldDefinition.h:127
repast::AgentId::currentRank
int currentRank() const
Gets the current process rank of this AgentId.
Definition: AgentId.h:135
repast::Projection< RelogoAgent >
repast::Grid< RelogoAgent, int >
repast::relogo::WorldDefinition::networks_begin
proj_iter networks_begin() const
Gets the start of an iterator over the network Projections defined in this WorldDefinition.
Definition: WorldDefinition.h:118
repast::GridDimensions
Basic structure for specifying grid dimenions.
Definition: GridDimensions.h:58
repast::relogo::WorldCreator
Creates a the relogo world given some parameters.
Definition: WorldCreator.h:65
repast::relogo::WorldDefinition
Defines a Relogo world.
Definition: WorldDefinition.h:61
repast::relogo::WorldCreator::createWorld
ObsType * createWorld(const WorldDefinition &worldDef, const std::vector< int > &pConfig, PatchCreator &patchCreator)
Creates the Relogo world using the specified parameters and returns an Observer of ObsType.
Definition: WorldCreator.h:116
repast::relogo::DefaultAgentCreator
operator() that creates an agent of type Agent.
Definition: creators.h:61
repast::Point< double >
repast::Projection::name
const std::string name() const
Gets the name of this projection.
Definition: Projection.h:164
repast::Grid::moveTo
virtual bool moveTo(const AgentId &id, const Point< GPType > &pt)=0
Moves the specified agent to the specified point.
repast::relogo::WorldDefinition::proj_iter
std::vector< Projection< RelogoAgent > * >::const_iterator proj_iter
An iterator over pointers to Projection<RelogoAgent>.
Definition: WorldDefinition.h:76
repast::Grid::dimensions
virtual const GridDimensions dimensions() const =0
Gets the dimensions of this Grid.