fpmas 1.6
moore_grid.h
Go to the documentation of this file.
1#ifndef FPMAS_MOORE_GRID_H
2#define FPMAS_MOORE_GRID_H
3
8#include "grid_builder.h"
9
10namespace fpmas { namespace model {
11
18 public:
25 const DiscretePoint& p1,
26 const DiscretePoint& p2) const {
27 return std::max(std::abs(p2.x - p1.x), std::abs(p2.y - p1.y));
28 }
29 };
30
46 template<typename CellType = model::GridCell>
47 class MooreGridBuilder : public detail::GridBuilder<CellType> {
48 private:
49
63 void buildLocalGrid(
65 GridDimensions local_dimensions,
67 ) const override;
68
81 void linkFrontiers(
83 GridDimensions local_dimensions,
85 std::vector<CellType*>& frontier
86 ) const override;
87 public:
89 };
90
91 template<typename CellType>
94 GridDimensions local_dimensions,
96 ) const {
97 DiscreteCoordinate local_width = local_dimensions.width();
98 DiscreteCoordinate local_height = local_dimensions.height();
99
100 // VonNeumann links (horizontal direction)
101 for(DiscreteCoordinate j = 0; j < local_width; j++) {
102 for(DiscreteCoordinate i = 0; i < local_height-1; i++) {
103 model.link(cells[i][j], cells[i+1][j], api::model::CELL_SUCCESSOR);
104 }
105 for(DiscreteCoordinate i = local_height-1; i > 0; i--) {
106 model.link(cells[i][j], cells[i-1][j], api::model::CELL_SUCCESSOR);
107 }
108 }
109
110 // VonNeumann links (vertical direction)
111 for(DiscreteCoordinate i = 0; i < local_height; i++) {
112 for(DiscreteCoordinate j = 0; j < local_width-1; j++) {
113 model.link(cells[i][j], cells[i][j+1], api::model::CELL_SUCCESSOR);
114 }
115 for(DiscreteCoordinate j = local_width-1; j > 0; j--) {
116 model.link(cells[i][j], cells[i][j-1], api::model::CELL_SUCCESSOR);
117 }
118 }
119
120 // Links NE corners
121 for(DiscreteCoordinate j = 0; j < local_width-1; j++) {
122 for(DiscreteCoordinate i = 0; i < local_height-1; i++) {
123 model.link(cells[i][j], cells[i+1][j+1], api::model::CELL_SUCCESSOR);
124 }
125 }
126
127 // Links NW corners
128 for(DiscreteCoordinate j = 1; j < local_width; j++) {
129 for(DiscreteCoordinate i = 0; i < local_height-1; i++) {
130 model.link(cells[i][j], cells[i+1][j-1], api::model::CELL_SUCCESSOR);
131 }
132 }
133
134 // Links SE corners
135 for(DiscreteCoordinate j = 0; j < local_width-1; j++) {
136 for(DiscreteCoordinate i = 1; i < local_height; i++) {
137 model.link(cells[i][j], cells[i-1][j+1], api::model::CELL_SUCCESSOR);
138 }
139 }
140
141 // Links SW corners
142 for(DiscreteCoordinate j = 1; j < local_width; j++) {
143 for(DiscreteCoordinate i = 1; i < local_height; i++) {
144 model.link(cells[i][j], cells[i-1][j-1], api::model::CELL_SUCCESSOR);
145 }
146 }
147 }
148
149 template<typename CellType>
150 void MooreGridBuilder<CellType>::linkFrontiers(
151 api::model::SpatialModel<CellType>& model,
152 GridDimensions local_dimensions,
153 typename detail::GridBuilder<CellType>::CellMatrix& local_cells,
154 std::vector<CellType*>& frontier
155 ) const {
156 for(auto cell : frontier) {
157 if(cell->location().y == local_dimensions.getExtent().y) {
158 // top
159
160 // X coordinate of the cell below (index in the local_cells
161 // matrix)
162 DiscreteCoordinate mid_x = cell->location().x - local_dimensions.getOrigin().x;
163 if(mid_x >= 0 && mid_x < local_dimensions.width())
164 // For corners, the mid_x value is not in the local
165 // matrix
166 model.link(
167 local_cells[local_dimensions.height()-1][mid_x],
168 cell,
169 SpatialModelLayers::CELL_SUCCESSOR
170 );
171
172 // X coordinate the cell below left (index in the local_cells
173 // matrix)
174 DiscreteCoordinate left_x = mid_x-1;
175 if(left_x >= 0)
176 model.link(
177 local_cells[local_dimensions.height()-1][left_x],
178 cell,
179 SpatialModelLayers::CELL_SUCCESSOR
180 );
181
182 // X coordinate of the cell below right (index in the
183 // local_cells matrix)
184 DiscreteCoordinate right_x = mid_x+1;
185 if(right_x < local_dimensions.width())
186 model.link(
187 local_cells[local_dimensions.height()-1][right_x],
188 cell,
189 SpatialModelLayers::CELL_SUCCESSOR
190 );
191 }
192 else if(cell->location().y == local_dimensions.getOrigin().y-1) {
193 // bottom
194
195 // X coordinate of the cell above (index in the local_cells
196 // matrix)
197 DiscreteCoordinate mid_x = cell->location().x - local_dimensions.getOrigin().x;
198 if(mid_x >= 0 && mid_x < local_dimensions.width())
199 model.link(
200 local_cells[0][mid_x],
201 cell,
202 SpatialModelLayers::CELL_SUCCESSOR
203 );
204
205 // X coordinate the cell above left (index in the local_cells
206 // matrix)
207 DiscreteCoordinate left_x = mid_x-1;
208 if(left_x >= 0)
209 model.link(
210 local_cells[0][left_x],
211 cell,
212 SpatialModelLayers::CELL_SUCCESSOR
213 );
214
215 // X coordinate of the cell above right (index in the
216 // local_cells matrix)
217 DiscreteCoordinate right_x = mid_x+1;
218 if(right_x < local_dimensions.width())
219 model.link(
220 local_cells[0][right_x],
221 cell,
222 SpatialModelLayers::CELL_SUCCESSOR
223 );
224 }
225 else if(cell->location().x == local_dimensions.getOrigin().x-1) {
226 // left
227
228 // Y coordinate of the cell left (index in the local_cells
229 // matrix)
230 DiscreteCoordinate left_y = cell->location().y - local_dimensions.getOrigin().y;
231 if(left_y >= 0 && left_y < local_dimensions.height())
232 model.link(
233 local_cells[left_y][0],
234 cell,
235 SpatialModelLayers::CELL_SUCCESSOR
236 );
237
238 // Y coordinate the cell below left (index in the local_cells
239 // matrix)
240 DiscreteCoordinate bottom_y = left_y-1;
241 if(bottom_y >= 0)
242 model.link(
243 local_cells[bottom_y][0],
244 cell,
245 SpatialModelLayers::CELL_SUCCESSOR
246 );
247
248 // y coordinate of the cell above left (index in the
249 // local_cells matrix)
250 DiscreteCoordinate top_y = left_y+1;
251 if(top_y < local_dimensions.height())
252 model.link(
253 local_cells[top_y][0],
254 cell,
255 SpatialModelLayers::CELL_SUCCESSOR
256 );
257 }
258 else if(cell->location().x == local_dimensions.getExtent().x) {
259 // right
260
261 // Y coordinate of the cell left (index in the local_cells
262 // matrix)
263 DiscreteCoordinate right_y = cell->location().y - local_dimensions.getOrigin().y;
264 if(right_y >= 0 && right_y < local_dimensions.height())
265 model.link(
266 local_cells[right_y][local_dimensions.width()-1],
267 cell,
268 SpatialModelLayers::CELL_SUCCESSOR
269 );
270
271 // Y coordinate the cell below right (index in the local_cells
272 // matrix)
273 DiscreteCoordinate bottom_y = right_y-1;
274 if(bottom_y >= 0)
275 model.link(
276 local_cells[bottom_y][local_dimensions.width()-1],
277 cell,
278 SpatialModelLayers::CELL_SUCCESSOR
279 );
280
281 // y coordinate of the cell above left (index in the
282 // local_cells matrix)
283 DiscreteCoordinate top_y = right_y+1;
284 if(top_y < local_dimensions.height())
285 model.link(
286 local_cells[top_y][local_dimensions.width()-1],
287 cell,
288 SpatialModelLayers::CELL_SUCCESSOR
289 );
290 }
291 }
292 }
297 template<typename CellType = model::GridCell>
299
300}}
301#endif
virtual AgentEdge * link(Agent *src_agent, Agent *tgt_agent, api::graph::LayerId layer)=0
Definition: moore_grid.h:17
DiscreteCoordinate operator()(const DiscretePoint &p1, const DiscretePoint &p2) const
Definition: moore_grid.h:24
Definition: grid_load_balancing.h:30
DiscreteCoordinate height() const
Definition: grid_load_balancing.h:79
DiscreteCoordinate width() const
Definition: grid_load_balancing.h:72
Definition: moore_grid.h:47
Definition: grid_builder.h:29
std::vector< std::vector< CellType * > > CellMatrix
Definition: grid_builder.h:37
long DiscreteCoordinate
Definition: grid.h:15
Definition: fpmas.cpp:3
DiscreteCoordinate x
Definition: grid.h:25
DiscreteCoordinate y
Definition: grid.h:29
Definition: grid.h:507