Fix Sim1Lay
This commit is contained in:
parent
0711f071a5
commit
b935701f9b
@ -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
|
||||||
|
@ -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,28 +474,34 @@ 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)
|
||||||
|
self._track()
|
||||||
|
if self.__draw_engine is not None:
|
||||||
|
time_ca_end = time.time()
|
||||||
|
self._redraw_dirty_cells()
|
||||||
|
time_ds_end = time.time()
|
||||||
|
self.print_process_info(evolve_duration=(time_ca_end - time_ca_start),
|
||||||
|
draw_duration=(time_ds_end - time_ca_end),
|
||||||
|
evolution_step=self._cellular_automaton.evolution_step,
|
||||||
|
runlendig=len(str(last_evolution_step)),
|
||||||
|
)
|
||||||
|
self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second)
|
||||||
|
self.__active = self.__draw_engine.is_active()
|
||||||
|
self._save()
|
||||||
|
|
||||||
|
finally:
|
||||||
if self.__draw_engine is not None:
|
if self.__draw_engine is not None:
|
||||||
time_ca_end = time.time()
|
try:
|
||||||
self._redraw_dirty_cells()
|
self.__draw_engine._pygame.quit()
|
||||||
time_ds_end = time.time()
|
except:
|
||||||
self.print_process_info(evolve_duration=(time_ca_end - time_ca_start),
|
print('Failed to quit pygame')
|
||||||
draw_duration=(time_ds_end - time_ca_end),
|
|
||||||
evolution_step=self._cellular_automaton.evolution_step,
|
|
||||||
runlendig=len(str(last_evolution_step)))
|
|
||||||
self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second)
|
|
||||||
self.__active = self.__draw_engine.is_active()
|
|
||||||
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':
|
||||||
self._append_all()
|
self._append_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}')
|
||||||
|
Loading…
Reference in New Issue
Block a user