Fix Sim1Lay

This commit is contained in:
timofej 2023-09-03 22:53:43 +02:00
parent 0711f071a5
commit b935701f9b
2 changed files with 34 additions and 28 deletions

View File

@ -84,8 +84,9 @@ class CellularAutomatonCreator(abc.ABC):
class Neurolattice(CellularAutomatonCreator, abc.ABC): class Neurolattice(CellularAutomatonCreator, abc.ABC):
""" Cellular automaton with the evolution rules of conways game of life """ """ Cellular automaton with the evolution rules of conways game of life """
def __init__(self, dim, eps, *args, **kwargs): def __init__(self, dim, eps, init, *args, **kwargs):
super().__init__(dimension=[dim, dim], super().__init__(dimension=[dim, dim],
init=init,
neighborhood=VonNeumannNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)) neighborhood=VonNeumannNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS))
self._evolution_step = 0 self._evolution_step = 0
self.epsilon = eps self.epsilon = eps

View File

@ -24,6 +24,7 @@ import operator
import collections import collections
import contextlib import contextlib
from typing import Sequence from typing import Sequence
import math as m #just for last_sync
from . import Neurolattice, Neuropercolation, NeuropercolationCoupled from . import Neurolattice, Neuropercolation, NeuropercolationCoupled
@ -102,7 +103,6 @@ class Simulate1Layer:
self.__path = path if path[-1]=='/' else path+'/' self.__path = path if path[-1]=='/' else path+'/'
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.__active = True
self.run(evolutions_per_second=fps, last_evolution_step=steps) self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
@ -119,7 +119,7 @@ class Simulate1Layer:
self._track() self._track()
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
while self.__active() and self._not_at_the_end(last_evolution_step): while self.__active and self._not_at_the_end(last_evolution_step):
time_ca_start = time.time() time_ca_start = time.time()
self._cellular_automaton.evolve(evolutions_per_draw) self._cellular_automaton.evolve(evolutions_per_draw)
self._track() self._track()
@ -275,7 +275,6 @@ class Simulate2Layers:
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.__channel_list = [] self.__channel_list = []
self.__active = True
self.run(evolutions_per_second=fps, last_evolution_step=steps) self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
@ -459,9 +458,9 @@ class Simulate4Layers:
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.__channel_list = [] self.__channel_list = []
self.__active = True
self.run(evolutions_per_second=fps, last_evolution_step=steps) self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
evolutions_per_second=0, evolutions_per_second=0,
evolutions_per_draw=1, evolutions_per_draw=1,
@ -475,27 +474,33 @@ class Simulate4Layers:
""" """
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
self._track() self._track()
while self.__active and self._not_at_the_end(last_evolution_step): try:
time_ca_start = time.time() while self.__active and self._not_at_the_end(last_evolution_step):
self._cellular_automaton.evolve(evolutions_per_draw) time_ca_start = time.time()
self._track() self._cellular_automaton.evolve(evolutions_per_draw)
if self.__draw_engine is not None: self._track()
time_ca_end = time.time() if self.__draw_engine is not None:
self._redraw_dirty_cells() time_ca_end = time.time()
time_ds_end = time.time() self._redraw_dirty_cells()
self.print_process_info(evolve_duration=(time_ca_end - time_ca_start), time_ds_end = time.time()
draw_duration=(time_ds_end - time_ca_end), self.print_process_info(evolve_duration=(time_ca_end - time_ca_start),
evolution_step=self._cellular_automaton.evolution_step, draw_duration=(time_ds_end - time_ca_end),
runlendig=len(str(last_evolution_step))) evolution_step=self._cellular_automaton.evolution_step,
self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) runlendig=len(str(last_evolution_step)),
self.__active = self.__draw_engine.is_active() )
if self.__draw_engine is not None: self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second)
try: self.__active = self.__draw_engine.is_active()
self.__draw_engine._pygame.quit() self._save()
except:
print('Failed to quit pygame') finally:
if self.__draw_engine is not None:
try:
self.__draw_engine._pygame.quit()
except:
print('Failed to quit pygame')
self._save()
def _track(self): def _track(self):
if self.__save == 'all': if self.__save == 'all':
@ -601,6 +606,6 @@ class Simulate4Layers:
def __draw_cell_surface(self, surface_pos, cell_color): def __draw_cell_surface(self, surface_pos, cell_color):
return self.__draw_engine.fill_surface_with_color((surface_pos, self.__cell_size), cell_color) return self.__draw_engine.fill_surface_with_color((surface_pos, self.__cell_size), cell_color)
def print_process_info(self, evolve_duration, draw_duration, evolution_step, runlendig): def print_process_info(self, evolve_duration, draw_duration, evolution_step, runlendig, extra=''):
self.__draw_engine.fill_surface_with_color(((0, 0), (self.__rect.width, 30))) self.__draw_engine.fill_surface_with_color(((0, 0), (self.__rect.width, 30)))
self.__draw_engine.write_text((0, 5), f'Step: {evolution_step:>{runlendig}} FPS: {int(1/(evolve_duration+draw_duration))}') self.__draw_engine.write_text((0, 5), f'Step: {evolution_step:>{runlendig}} FPS: {int(1/(evolve_duration+draw_duration))} {extra}')