Skip to content
Snippets Groups Projects
Commit 3b272b80 authored by phwitte's avatar phwitte
Browse files

Update Life.py

parent ff7076ce
Branches
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ matplotlib.use('TkAgg') ...@@ -15,7 +15,7 @@ matplotlib.use('TkAgg')
import matplotlib.pyplot as pyplot import matplotlib.pyplot as pyplot
def circleArray(r1, r2, weight, oval=0, ovalscaling= 2, invers=False, xshift = 0, yshift= 0): def circleArray(r1, r2, weight, oval=0, ovalscaling= 2, invers=False, xshift = 1, yshift= 1):
width =2*r2+1 width =2*r2+1
height=2*r2+1 height=2*r2+1
...@@ -61,7 +61,7 @@ def circleArray(r1, r2, weight, oval=0, ovalscaling= 2, invers=False, xshift = 0 ...@@ -61,7 +61,7 @@ def circleArray(r1, r2, weight, oval=0, ovalscaling= 2, invers=False, xshift = 0
return map_ return map_
def squareArray(r1, r2, weight, rect=1, rectscaling= 2): def squareArray(r1, r2, weight, rect=0, rectscaling= 2):
width = 2*r2+1 width = 2*r2+1
height=2*r2+1 height=2*r2+1
...@@ -93,7 +93,7 @@ class Life(object): ...@@ -93,7 +93,7 @@ class Life(object):
n: the number of rows and columns n: the number of rows and columns
""" """
def __init__(self, n, mode='wrap', random = True, p=0.001, radius1 = 3, radius2 = 6, weight=-0.5, doubleStates = False, inversion = False): def __init__(self, n, mode='wrap', random = False, p=0.001, radius1 = 3, radius2 = 6, weight=-0.25, doubleStates = True, inversion = False):
"""Attributes: """Attributes:
n: number of rows and columns n: number of rows and columns
mode: how border conditions are handled mode: how border conditions are handled
...@@ -141,18 +141,31 @@ class Life(object): ...@@ -141,18 +141,31 @@ class Life(object):
"""Executes one time step.""" """Executes one time step."""
con = scipy.ndimage.filters.convolve(self.array, con = scipy.ndimage.filters.convolve(self.array,
self.weights, self.weights,
mode=self.mode) mode=self.mode, cval=0.0 )
#boolean = (con==3) | (con==12) | (con==13) #boolean = (con==3) | (con==12) | (con==13)
sideBorders = True
topbotBorders = True
if (not self.doubleStates): if (not self.doubleStates):
boolean = (con >= 1) boolean = (con >= 1)
for y in range(self.n):
for x in range(self.n):
if sideBorders and ((x < self.radius2) or (x > self.n-self.radius2)):
boolean[y][x] = True
if topbotBorders and ((y < self.radius2) or (y > self.n-self.radius2)):
boolean[y][x] = True
self.array = numpy.int8(boolean) self.array = numpy.int8(boolean)
else: else:
print ("doublestates")
values = [[ 0 for x in range(self.n)] for y in range(self.n)] values = [[ 0 for x in range(self.n)] for y in range(self.n)]
for y in range(self.n): for y in range(self.n):
for x in range(self.n): for x in range(self.n):
if con[y][x] >= 4*self.radius2: if sideBorders and ((x < self.radius2) or (x > self.n-self.radius2)):
values[y][x] = 0
if topbotBorders and ((y < self.radius2) or (y > self.n-self.radius2)):
values[y][x] = 0
elif con[y][x] >= 2*self.radius2:
values[y][x] = 2 values[y][x] = 2
elif con[y][x] >= 1: elif con[y][x] >= 1:
values[y][x] = 1 values[y][x] = 1
...@@ -185,19 +198,21 @@ class LifeViewer(object): ...@@ -185,19 +198,21 @@ class LifeViewer(object):
self.pcolor = pyplot.pcolor(a, cmap=self.cmap) self.pcolor = pyplot.pcolor(a, cmap=self.cmap)
self.fig.canvas.draw() self.fig.canvas.draw()
def animate(self, steps=10): def animate(self, steps=1):
"""Creates the GUI and then invokes animate_callback. """Creates the GUI and then invokes animate_callback.
Generates an animation with the given number of steps. Generates an animation with the given number of steps.
""" """
self.steps = steps self.steps = steps
self.fig.canvas.manager.window.after(1000, self.animate_callback) self.fig.canvas.manager.window.after(1000, self.animate_callback)
self.fig.savefig('/home/phill/Cell_Automata/test_0/pic.png')
pyplot.show() pyplot.show()
def animate_callback(self): def animate_callback(self):
"""Runs the animation.""" """Runs the animation."""
for i in range(self.steps): for i in range(self.steps):
self.life.step() self.life.step()
self.fig.savefig('/home/phill/Cell_Automata/test_0/pic' + str(i)+ '.png')
self.update() self.update()
print ('end') print ('end')
...@@ -209,7 +224,7 @@ def main(script, n=20, radius1 = 5, radius2 = 10,p= 0.1, weight=-0.5, doubleStat ...@@ -209,7 +224,7 @@ def main(script, n=20, radius1 = 5, radius2 = 10,p= 0.1, weight=-0.5, doubleStat
life = Life(n, random=True, radius1 = radius1, radius2 = radius2, p = p, weight = weight, doubleStates=doubleStates, inversion=inversion) life = Life(n, random=True, radius1 = radius1, radius2 = radius2, p = p, weight = weight, doubleStates=doubleStates, inversion=inversion)
#life.add_glider(10, 10) #life.add_glider(10, 10)
viewer = LifeViewer(life) viewer = LifeViewer(life)
viewer.animate(steps=100) viewer.animate(steps=10)
""" df """ """ df """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment