2""" @brief This class defines a 2D square grid on which agents can move.
5#============================================================================#
6# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles
30 """ Defines what's in one location on the ground
32 def __init__(self, Content=None, VoidCell=None):
39 def Content(self, Future=False):
return self.Future
if Future
else self.
Present
46 if Content
is None: Content = self.
VoidCell
48 if Future
and Content != self.
Future:
58 """ tells whether a cell is active
66 def __str__(self):
return str(self.
Content())
69 """ A 2-D square toric grid
71 def __init__(self, Width=100, Height=0, CellType=LandCell):
96 def Modify(self, P, NewContent, check=False, Future=False):
97 """ Changes content at a location
103 if NewContent
is not None:
104 if not self.
Admissible(NewContent)
or not Cell.free():
107 if Cell.free():
return False
108 Cell.setContent(NewContent, Future=Future)
109 if Cell.activated(Future=Future):
120 try:
return self.
Ground[x][y]
122 raise Exception(
'Bad coordinates for Cell (%d, %d)' % (x,y))
131 return (2*Radius+1) ** 2 - 5
134 """ returns neighbouring cells
138 for distx
in range(-Radius, Radius+1):
139 for disty
in range(-Radius, Radius+1):
140 if distx == 0
and disty == 0:
continue
141 if abs(distx) + abs(disty) == 2 * Radius:
continue
149 """ computes the shorter segment between two points on the tore
153 return (min(vx,wx), min(vy,wy))
156 """ Makes statistics about local content
157 Returns a dictionary by Content.
158 The center position is omitted
163 else: LocalStatistics = dict()
164 for NPos
in Neighbourhood:
166 if self.
Content(NPos)
in LocalStatistics: LocalStatistics[self.
Content(NPos)] += 1
167 else: LocalStatistics[self.
Content(NPos)] = 1
168 return LocalStatistics
171 """ scans ground and builds lists of cells depending on Content
175 for (Pos, Cell)
in self.
travel():
182 """ updates the delayed effect of cells that have been modified
187 for Pos
in CurrentlyActiveCells:
188 if self.
Cell(Pos).Update():
193 """ Active cells produce their effect
199 """ Cell located at position 'Pos' has been modified and now produces its effect, possibly on neighbouring cells
204 """ picks an element of the grid with 'Content' in it
210 if self.
Content(Pos) == Content:
216 Row = random.randint(0,self.
Height-1)
217 Col = random.randint(0,self.
Width-1)
219 if self.
Content((Col,Row)) == Content:
return (Col, Row)
220 else:
return (Col, Row)
224 """ Iteratively returns Cells of the grid
227 for Col
in range(self.
Width):
228 for Row
in range(self.
Height):
229 try:
yield ((Col,Row), self.
Ground[Col][Row])
237 """ Same as LandCell, plus a third dimension
239 def __init__(self, Altitude=0, Content=None, VoidCell=None):
240 LandCell.__init__(self, Content=Content, VoidCell=VoidCell)
244 """ Same as Landscape, but stores a third dimension in cells
246 def __init__(self, Altitudes=[], AltitudeFile='', CellType=LandCell_3D):
247 Height = len(Altitudes)
250 Rows = open(AltitudeFile).readlines()
252 Altitudes = [list(map(int,Row.split()))
for Row
in Rows]
253 Height = len(Altitudes)
254 Width = len(Altitudes[0])
255 print(
'Ground = %d x %d' % (Width, Height))
256 Landscape.__init__(self, Width=Width, Height=Height, CellType=CellType)
257 for ((Col,Row), Cell)
in self.
travel():
259 Cell.Altitude = Altitudes[Height-1-Row][Col]
267if __name__ ==
"__main__":
272__author__ =
'Dessalles'
Same as LandCell, plus a third dimension.
def __init__(self, Altitude=0, Content=None, VoidCell=None)
Defines what's in one location on the ground.
def Content(self, Future=False)
def setContent(self, Content=None, Future=False)
def activated(self, Future=False)
tells whether a cell is active
def __init__(self, Content=None, VoidCell=None)
Same as Landscape, but stores a third dimension in cells.
def __init__(self, Altitudes=[], AltitudeFile='', CellType=LandCell_3D)
def segment(self, P0, P1)
def travel(self)
Iteratively returns Cells of the grid.
def ToricConversion(self, P)
def statistics(self)
scans ground and builds lists of cells depending on Content
def setAdmissible(self, ContentType)
def Admissible(self, Content)
def activation(self)
Active cells produce their effect.
def update(self)
updates the delayed effect of cells that have been modified
def neighbours(self, P, Radius=1)
returns neighbouring cells
def Content(self, P, Future=False)
def __init__(self, Width=100, Height=0, CellType=LandCell)
def Modify(self, P, NewContent, check=False, Future=False)
Changes content at a location.
def randomPosition(self, Content=None, check=False)
picks an element of the grid with 'Content' in it
def activate(self, Pos)
Cell located at position 'Pos' has been modified and now produces its effect, possibly on neighbourin...
def neighbourhoodLength(self, Radius=1)
def InspectNeighbourhood(self, Pos, Radius)
Makes statistics about local content Returns a dictionary by Content.