Dies ist eine alte Version des Dokuments!
Was wir geschafft haben:
- man kann das Programm anhalten und dann wieder fortführen - man kann Zellzustände per Mausklick/Mausbewegung verändern - der zeitkritischste Schritt ist nun um 4 Größenordnungen schneller, durch boolsche abfragen
Umgeschmissene Hürden entlang des heutigen Marathons:
- Einlesen in Events (in Matplotlib) z.B. mit Leertaste stoppen - Einsamkeit ohne Mazal und Leroy und die einhergehende Teaminstabilität - allen Fortschritt der heutigen Session zu handlen, das übermotivierte Team zu koordinieren und jeden an allem Fortschritt teilhaben lassen
Ziele/Ideen fürs nächste Mal:
- Nebenplots mit statistiken zu Alter/Anzahl der Zellen o.Ä. - Code ein wenig durchkämmen (mancherorts wurde heute schlampig gecodet)
Aktuelle GOL Datei:
# -*- coding: utf-8 -*- """ Created on Thu Jan 30 15:06:55 2020 @author: HP """ import random import numpy as np import matplotlib.pyplot as plt from PIL import Image from math import floor, ceil import time # Größe der Matrix # Die Matrix wird zufällig mit Nullen und Einsen gefüllt def Tripleeinheitsmatrix(zeilen,spalten): tem = np.zeros ((zeilen,spalten)) for i in range(0, zeilen): for j in range(0, spalten): if i==j: tem[(i,j)] = 1 else: tem[(i,j)] = 0 for i in range(0, zeilen): for j in range(0, spalten): if i==(j+1): tem[(i,j)] = 1 for i in range(0, zeilen): for j in range(0, spalten): if j==(i+1): tem[(i,j)] = 1 tem[0,tem.shape[1]-1] = 1 tem[tem.shape[1]-1,0] = 1 return tem def BerechneAnzahlNachbarn(zustand): z = np.dot(tem, zustand) k = np.dot(z, tem) anzahlNachbarn = k - zustand return anzahlNachbarn def BerechneNeuenZustand(zustand, anzahlNachbarn): #Ursprungszelle Tod + genau 3 lebende Nachbarn = lebend #Ursprungszelle Lebend + genau 1 lebenden Nachbarn = tot #Ursprungszelle Lebend + 2 o. 3 Nachbarn = lebend #Ursprungszelle lebend + 4 oder mehr Nachbarn = tot #for i in range(0, zeilen): # for j in range(0, spalten): # if zustand[(i,j)] == 1 and (anzahlNachbarn[(i,j)] in range(2) or anzahlNachbarn[(i,j)] in range(4,9)): # zustand[(i,j)]=0 # elif zustand[(i,j)] == 0 and anzahlNachbarn[(i,j)]==3: # zustand[(i,j)]=1 zustand = (anzahlNachbarn == 3) + ((anzahlNachbarn == 2) * zustand) return zustand def Schritt(): global zustand, cmap start_time = time.time() anzahlNachbarn = BerechneAnzahlNachbarn(zustand) print('Berechne Nachbarn:', time.time() - start_time) start_time = time.time() zustand = BerechneNeuenZustand(zustand, anzahlNachbarn) print('Berechne neuen Zustand:', time.time() - start_time) cmap = cmap * zustand + zustand return zustand def Wiederhole(t): for i in range(t): Schritt() def MachQuadrat(pixel): breite = pixel.shape[0] hoehe = pixel.shape[1] size = max(breite, hoehe) links = floor((size - breite) / 2) rechts = ceil((size - breite) / 2) oben = floor((size - hoehe) / 2) unten = ceil((size - hoehe) / 2) pad_width = ((links, rechts), (oben, unten)) pixel = np.pad(pixel, pad_width, mode='constant', constant_values=0) return pixel #t= int(input('Gib die Zeitschritte ein: ')) def Initialisiere(): global zustand, tem, zeilen, spalten, cmap modus = int(input('Gib den Modus 1, 2, 3 oder 4 ein: ')) if modus==1 or modus==2: spalten = zeilen = int(input('Gib die Anzahl der Zeilen/Spalten ein: ')) #zustand = np.zeros if modus==1: zustand = np.round(np.random.random((zeilen,spalten))).astype(int) elif modus==2: zustand=np.zeros((zeilen,spalten)) for i in range(0,zeilen): for j in range (0,spalten): if j%2==0: zustand[(i,j)]=0 else: zustand[(i,j)]=1 if zustand[(int(zeilen/2),int(spalten/2))]==0: zustand[(int(zeilen/2),int(spalten/2))]=1 zustand[(int((zeilen/2)+1),int(spalten/2))]=1 else: zustand[(int(zeilen/2),int(spalten/2))]=0 zustand[(int((zeilen/2)+1),int(spalten/2))]=0 elif modus==3: DateiName = input('was willste oeffnen? ') Datei = open(DateiName) DateiAlsListe = [] for line in Datei: DateiAlsListe.append([int(c) for c in list(line.rstrip())]) DateiAlsMatrix = np.array(DateiAlsListe) zustand = MachQuadrat(DateiAlsMatrix) zeilen = spalten = zustand.shape[0] elif modus==4: dateiname = input('Gib den Bildpfad ein: ') bild = Image.open(dateiname) bild = bild.convert('1') bild.thumbnail((500,500)) pixel = np.array(bild) zustand = MachQuadrat(pixel) zeilen = spalten = zustand.shape[0] tem = Tripleeinheitsmatrix(zeilen, spalten) cmap = zustand #Wiederholungen(t)
Ausführungsdatei:
# -*- coding: utf-8 -*- """ Created on Thu Jan 30 15:07:26 2020 @author: HP """ import GameOfLifeaktuell as gol import numpy as np from matplotlib import pyplot as plt from matplotlib import animation colormaps = ['Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r'] def animate(e): im.set_array(np.ones(gol.cmap.shape) - np.power(0.5, gol.cmap)) #im.set_array( np.power(0.5, gol.cmap)) #im.set_array(gol.cmap * 0.1) #print(gol.cmap) gol.Schritt() return im, gol.Initialisiere() #fig = plt.figure(figsize=(1920,1080), dpi=1, frameon=False) fig = plt.figure() anim_running = True #fig position ändern def onPress(event): global anim_running if event.key==' ': if anim_running: anim.event_source.stop() anim_running = False else: anim.event_source.start() anim_running = True zustand_neu = None cmap_neu = None def onClick(event): global zustand_neu, cmap_neu if event.button == 1: anim.event_source.stop() zustand_neu = np.copy(gol.zustand) cmap_neu = np.copy(gol.cmap) if event.xdata != None and event.ydata != None: pos_x = int(np.round(event.xdata)) pos_y = int(np.round(event.ydata)) if pos_x >= 0 and pos_y >= 0 and pos_x < gol.zustand.shape[1] and pos_y < gol.zustand.shape[0]: zustand_neu[pos_y, pos_x] = 1 - gol.zustand[pos_y, pos_x] cmap_neu = gol.cmap * zustand_neu + zustand_neu im.set_array(np.ones(cmap_neu.shape) - np.power(0.5, cmap_neu)) fig.canvas.draw() def onMove(event): global zustand_neu, cmap_neu if event.button == 1 and event.xdata != None and event.ydata != None: pos_x = int(np.round(event.xdata)) pos_y = int(np.round(event.ydata)) if pos_x >= 0 and pos_y >= 0 and pos_x < gol.zustand.shape[1] and pos_y < gol.zustand.shape[0]: zustand_neu[pos_y, pos_x] = 1 - gol.zustand[pos_y, pos_x] cmap_neu = gol.cmap * zustand_neu + zustand_neu im.set_array(np.ones(cmap_neu.shape) - np.power(0.5, cmap_neu)) fig.canvas.draw() def onClickEnd(event): global zustand_neu, cmap_neu if event.button == 1: gol.zustand = zustand_neu gol.cmap = cmap_neu im.set_array(np.ones(cmap_neu.shape) - np.power(0.5, cmap_neu)) fig.canvas.draw() if anim_running: anim.event_source.start() plt.axis('off') #figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k') #plt.figure(figsize=(1,1)) #im = plt.imshow(gol.zustand, animated=True, cmap=plt.get_cmap('YlGn')) #im = plt.imshow(gol.zustand, animated=True, cmap=plt.get_cmap('inferno')) im = plt.imshow(gol.zustand, animated=True, cmap=plt.get_cmap(colormaps[int(np.random.random()*len(colormaps))])) fig.canvas.mpl_connect('key_press_event', onPress) fig.canvas.mpl_connect('button_press_event', onClick) fig.canvas.mpl_connect('button_release_event', onClickEnd) fig.canvas.mpl_connect('motion_notify_event', onMove) anim = animation.FuncAnimation(fig, animate, interval=200) #blit ist boese # # fuer mint gruen cmap=YlGn plt.show()