From 05987af47ecd14d3b2885d645a4c4600c0b551bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ismael=20Bartolom=C3=A9?= Date: Sat, 30 May 2015 19:20:19 +0200 Subject: [PATCH 1/2] Primer pomodoro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Funcion que calcula la lista de vecinos 2. Funcion que suma los vecinos vivos 3. Funcion que construye un nuevo mundo a partir de la lista de casillas que resultarán validas según las reglas --- GoL/gol.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ GoL/golTest.py | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 GoL/gol.py create mode 100644 GoL/golTest.py diff --git a/GoL/gol.py b/GoL/gol.py new file mode 100644 index 0000000..b4a2b07 --- /dev/null +++ b/GoL/gol.py @@ -0,0 +1,58 @@ +import gol_utils as g + +MAX = 3 + +world = g.create_world(alive=([(0,1),(1,1),(2,1)])) + +def casillas_vecinas( pos, MAX): + """Retorna Lista de casillas vecinas validas""" + + xpos, ypos = pos + + posiciones = [ + (x,y) + for x in range(xpos-1, xpos+2) if x >= 0 and x = 0 and y Date: Sat, 30 May 2015 22:10:59 +0200 Subject: [PATCH 2/2] =?UTF-8?q?pomodoro2=20dimension=20variable=20y=20visu?= =?UTF-8?q?alizaci=C3=B3n=20dinamica?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit vida.py para generar ejemplos multislider --- GoL/gol.py | 16 +++++---- GoL/golTest.py | 35 ++++++++++++++++++ GoL/gol_utils.py | 6 ++-- GoL/vida.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 GoL/vida.py diff --git a/GoL/gol.py b/GoL/gol.py index b4a2b07..a4e27be 100644 --- a/GoL/gol.py +++ b/GoL/gol.py @@ -1,6 +1,6 @@ import gol_utils as g -MAX = 3 + world = g.create_world(alive=([(0,1),(1,1),(2,1)])) @@ -19,26 +19,30 @@ def casillas_vecinas( pos, MAX): #print casillas_vecinas((0,0),MAX) -def numero_vecinos( world, pos ): +def numero_vecinos( world, pos): """Retorna el numero de vecions vivos de una casilla""" xpos, ypos = pos - valores = [ world[p[0],p[1]] for p in casillas_vecinas(pos,MAX) ] + valores = [ world[p[0],p[1]] for p in casillas_vecinas(pos,world.shape[0]) ] return sum(valores) def evolve(world): """Recorre el mundo creando una nuevo""" + + dim = world.shape[0] nuevo_mundo = g.create_world( [ (x,y) - for x in range(0,MAX) - for y in range(0,MAX) + for x in range(0,dim) + for y in range(0,dim) if ( (world[x,y] and numero_vecinos(world,(x,y))==2) or numero_vecinos(world,(x,y))==3) - ] + ], + dim + ) return nuevo_mundo diff --git a/GoL/golTest.py b/GoL/golTest.py index 0162c51..9da4e87 100644 --- a/GoL/golTest.py +++ b/GoL/golTest.py @@ -37,6 +37,41 @@ def test_flipflop(self): self.assertTrue((gol.evolve(w1) != w2).sum()==0) self.assertTrue((gol.evolve(w2) != w1).sum() ==0) + def test_de_cuatro_estable(self): + + w1 = g.create_world( + alive=([(1,1),(1,2),(2,1),(2,2)]), + MAX=4 + ) + + w2 = gol.evolve(w1) + self.assertTrue((w2 != w1).sum()==0) + + def test_beacon_de_6(self): + + w1 = g.create_world( + alive=( + [ + (1,1),(1,2),(2,1),(2,2), + (3,3),(3,4),(4,3),(4,4) + ]), + MAX=6 + ) + + w2 = gol.evolve(w1) + + w2_ref = g.create_world( + alive=( + [ + (1,1),(1,2),(2,1), + (3,4),(4,3),(4,4) + ]), + MAX=6 + ) + self.assertTrue((w2 != w2_ref).sum()==0) + w3 = gol.evolve(w2) + self.assertTrue((w3 != w1).sum()==0) + if __name__ == "__main__": unittest.main() \ No newline at end of file diff --git a/GoL/gol_utils.py b/GoL/gol_utils.py index 78dae01..75375f0 100644 --- a/GoL/gol_utils.py +++ b/GoL/gol_utils.py @@ -1,8 +1,8 @@ import numpy as np import matplotlib.pyplot as plt -def create_world(alive = None): - world = np.zeros((3,3), dtype=int) +def create_world(alive = None, MAX=3): + world = np.zeros((MAX,MAX), dtype=int) if alive: for position in alive: world[position[0], position[1]] = 1 @@ -10,3 +10,5 @@ def create_world(alive = None): def draw_world(world): plt.imshow(world, cmap=plt.cm.Greys, interpolation='nearest') + + diff --git a/GoL/vida.py b/GoL/vida.py new file mode 100644 index 0000000..25f72c6 --- /dev/null +++ b/GoL/vida.py @@ -0,0 +1,94 @@ +import gol +import gol_utils +import numpy as np +import random +import matplotlib.pyplot as plt +import matplotlib.animation as anim + +# beacon +def get_beacon(): + w = gol_utils.create_world( + alive=( + [ + (1,1),(1,2),(2,1),(2,2), + (3,3),(3,4),(4,3),(4,4) + ]), + MAX=6 + ) + +# Para crear una matriz random +def ger_random(): + return np.random.randint(0,2,(50,50)) + +def slider(posx,posy,flip=0,invertx=0,inverty=0,MAX=50): + res = [(2, 0), (3, 0), (0, 1), (1, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (1, 3), (2, 3)] + + masx = max( x for x,y in res) + masy = max( y for x,y in res) + + if flip: + res = [(y,-x+masx) for x,y in res] + + def invx(v): + if invertx: + return masx -v + else: + return v + def invy(v): + if inverty: + return masy -v + else: + return v + + res = [ ( + invx(x)+posx, + invy(y)+posy + ) for x,y in res] + + + return [ (x,y) for x,y in res if x in range(0,MAX) and y in range(0,MAX) ] + +def multi_slider(num): + + w = [] + for a in range(num): + w += slider( + random.randint(0,50), + random.randint(0,50), + random.randint(0,1), + random.randint(0,1), + random.randint(0,1), + ) + return gol_utils.create_world(w,50) + + +def simple_slider(): + return gol_utils.create_world( + alive=( + [ + (5,3),(6,3), + (3,4),(4,4), (6,4),(7,4), + (3,5),(4,5),(5,5),(6,5), + (4,6),(5,6), + ]), + MAX=30 + ) + +w = multi_slider(20) + + + + + +fig = plt.figure() + +def update(i): + global w + plt.clf() + gol_utils.draw_world(w) + fig.canvas.draw() + w = gol.evolve(w) + + +a = anim.FuncAnimation(fig, update, frames=3000, repeat=False) +plt.show()