|
22 | 22 | from mesa.experimental.devs import ABMSimulator
|
23 | 23 |
|
24 | 24 |
|
25 |
| -def is_trapped_in_wall( |
26 |
| - cell, wall_coord, width, height |
27 |
| -): # true if cell is trapped of walls |
28 |
| - north = (cell.coordinate[0] - 1, cell.coordinate[1]) |
29 |
| - south = (cell.coordinate[0] + 1, cell.coordinate[1]) |
30 |
| - east = (cell.coordinate[0], cell.coordinate[1] + 1) |
31 |
| - west = (cell.coordinate[0], cell.coordinate[1] - 1) |
32 |
| - |
33 |
| - coord = (cell.coordinate[0], cell.coordinate[1]) |
34 |
| - |
35 |
| - # 'corner' cases (pun intended) |
36 |
| - if coord == (0, 0): # top left corner |
37 |
| - return {east, south}.issubset(wall_coord) |
38 |
| - if coord == (height - 1, 0): # bottom left corner |
39 |
| - return {north, east}.issubset(wall_coord) |
40 |
| - if coord == (0, width - 1): # top right corner |
41 |
| - return {west, south}.issubset(wall_coord) |
42 |
| - if coord == (height - 1, width - 1): # bottom right corner |
43 |
| - return {north, west}.issubset(wall_coord) |
44 |
| - if coord[0] == 0: # for cells at top row |
45 |
| - return {south, west, east}.issubset(wall_coord) |
46 |
| - if coord[1] == 0: # for cells at leftmost column |
47 |
| - return {north, south, east}.issubset(wall_coord) |
48 |
| - if coord[0] == height - 1: # for cells at the bottom row |
49 |
| - return {north, east, west}.issubset(wall_coord) |
50 |
| - if coord[1] == width - 1: # for cells at rightmost column |
51 |
| - return {north, south, west}.issubset(wall_coord) |
52 |
| - |
53 |
| - return {north, south, west, east}.issubset(wall_coord) |
54 |
| - |
55 |
| - |
56 | 25 | class WolfSheep(Model):
|
57 | 26 | """Wolf-Sheep Predation Model.
|
58 | 27 |
|
@@ -140,17 +109,25 @@ def __init__(
|
140 | 109 |
|
141 | 110 | self.grid.add_property_layer(PropertyLayer.from_data("wall", wall_arr))
|
142 | 111 |
|
143 |
| - possible_cells = [] |
144 |
| - for cell in self.grid.all_cells.cells: |
145 |
| - if ( |
146 |
| - ( |
147 |
| - cell.coordinate[0], |
148 |
| - cell.coordinate[1], |
149 |
| - ) |
150 |
| - not in wall_coord |
151 |
| - and not is_trapped_in_wall(cell, wall_coord, width, height) |
152 |
| - ): # so we don't create an animal at wall cells. and make sure the animal is not trapped in walls |
153 |
| - possible_cells.append(cell) |
| 112 | + def is_wall(row, col): |
| 113 | + return ( |
| 114 | + True |
| 115 | + if row < 0 or col < 0 or row >= height or col >= width # corner case |
| 116 | + else wall_arr[row][col] |
| 117 | + ) |
| 118 | + |
| 119 | + def is_trapped_in_walls(row, col): |
| 120 | + return ( |
| 121 | + is_wall(row + 1, col) |
| 122 | + and is_wall(row - 1, col) |
| 123 | + and is_wall(row, col + 1) |
| 124 | + and is_wall(row, col - 1) |
| 125 | + ) |
| 126 | + |
| 127 | + possible_cells = self.grid.all_cells.select( |
| 128 | + lambda cell: not wall_arr[cell.coordinate[0]][cell.coordinate[1]] |
| 129 | + and not is_trapped_in_walls(cell.coordinate[0], cell.coordinate[1]) |
| 130 | + ).cells # so we don't create an animal at wall cells. and make sure the animal is not trapped in walls |
154 | 131 |
|
155 | 132 | # Create sheep:
|
156 | 133 | Sheep.create_agents(
|
|
0 commit comments