Add activation dump, background simulation and Simulate1Layer example

This commit is contained in:
timofej 2023-08-18 19:42:06 +02:00
parent 7d5f579a34
commit 88d4d2c9ea
2 changed files with 93 additions and 49 deletions

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 18 19:05:04 2023
@author: astral
"""
import numpy as np
from neuropercolation import Simulate1Layer
eps_space = np.linspace(0.005,0.5,100)
for dim in range(4,10):
for eps in eps_space:
eps = round(eps,3)
Simulate1Layer(dim,
eps,
res=2,
path=f'/cloud/Public/_data/neuropercolation/1lay/dim={dim:02}/',
draw=None
).run(evolutions_per_second=0,
last_evolution_step=100000)
print(f'Done eps={eps:.3f} at dim={dim}')

View File

@ -94,6 +94,7 @@ class Simulate1Layer:
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
self.__path = path
self.__state_list = []
self.__activation_list = []
def run(self,
evolutions_per_second=0,
@ -108,33 +109,40 @@ class Simulate1Layer:
"""
self._append_state()
with contextlib.suppress(KeyboardInterrupt):
while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step):
while self._not_at_the_end(last_evolution_step):
time_ca_start = time.time()
self._cellular_automaton.evolve(evolutions_per_draw)
self._append_state()
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)
try:
self.__draw_engine._pygame.quit()
except:
print('Failed to quit pygame')
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)
if self.__draw_engine is not None:
try:
self.__draw_engine._pygame.quit()
except:
print('Failed to quit pygame')
self._save_state_list()
def _append_state(self):
automaton_state = [[0 for n in range(self.__dimension)] for m in range(self.__dimension)]
activation = 0
for coord, cell in self._cellular_automaton._current_state.items():
x,y = coord
automaton_state[y][x] = int(cell.state[0])
activation += int(cell.state[0])
automaton_state = [''.join(str(cells) for cells in rows) for rows in automaton_state]
automaton_state = '.'.join(str(rows) for rows in automaton_state)
activation = round(1-2*activation/self.__dimension**2, 6)
self.__state_list.append(automaton_state)
self.__activation_list.append(activation)
def _save_state_list(self):
if not os.path.exists(self.__path):
@ -142,6 +150,8 @@ class Simulate1Layer:
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_states.txt", 'w', encoding='utf-8') as f:
json.dump(self.__state_list, f, indent=1)
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f:
json.dump(self.__activation_list, f, indent=1)
def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover
if evolutions_per_second > 0:
@ -186,9 +196,6 @@ class Simulate1Layer:
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))}')
def _is_not_user_terminated(self):
return self.__draw_engine.is_active()
class Simulate2Layers:
def __init__(self,
dim,
@ -220,6 +227,7 @@ class Simulate2Layers:
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
self.__path = path
self.__state_list = []
self.__activation_list = []
def run(self,
evolutions_per_second=0,
@ -234,34 +242,41 @@ class Simulate2Layers:
"""
self._append_state()
with contextlib.suppress(KeyboardInterrupt):
while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step):
while self._not_at_the_end(last_evolution_step):
time_ca_start = time.time()
self._cellular_automaton.evolve(evolutions_per_draw)
self._append_state()
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)
try:
self.__draw_engine._pygame.quit()
except:
print('Failed to quit pygame')
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)
if self.__draw_engine is not None:
try:
self.__draw_engine._pygame.quit()
except:
print('Failed to quit pygame')
self._save_state_list()
def _append_state(self):
automaton_state = [[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)]
activation = [0]*2
for coord, cell in self._cellular_automaton._current_state.items():
x,y,l = coord
automaton_state[l][y][x] = int(cell.state[0])
activation[l] += int(cell.state[0])
automaton_state = [[''.join(str(bits) for bits in rows) for rows in layers] for layers in automaton_state]
automaton_state = ['.'.join(str(rows) for rows in layers) for layers in automaton_state]
automaton_state = '-'.join(str(layers) for layers in automaton_state)
activation = [round(1-2*act/self.__dimension**2, 6) for act in activation]
self.__state_list.append(automaton_state)
self.__activation_list.append(activation)
def _save_state_list(self):
if not os.path.exists(self.__path):
@ -269,6 +284,8 @@ class Simulate2Layers:
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_states.txt", 'w', encoding='utf-8') as f:
json.dump(self.__state_list, f, indent=1)
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f:
json.dump(self.__activation_list, f, indent=1)
def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover
if evolutions_per_second > 0:
@ -317,9 +334,6 @@ class Simulate2Layers:
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))}')
def _is_not_user_terminated(self):
return self.__draw_engine.is_active()
class Simulate4Layers:
def __init__(self,
dim,
@ -352,6 +366,7 @@ class Simulate4Layers:
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
self.__path = path
self.__state_list = []
self.__activation_list = []
def run(self,
evolutions_per_second=0,
@ -366,35 +381,42 @@ class Simulate4Layers:
"""
with contextlib.suppress(KeyboardInterrupt):
self._append_state()
while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step):
while self._not_at_the_end(last_evolution_step):
time_ca_start = time.time()
self._cellular_automaton.evolve(evolutions_per_draw)
self._append_state()
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)
try:
self.__draw_engine._pygame.quit()
except:
print('Failed to quit pygame')
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)
if self.__draw_engine is not None:
try:
self.__draw_engine._pygame.quit()
except:
print('Failed to quit pygame')
self._save_state_list()
def _append_state(self):
automaton_state = [[[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)] for k in range(2)]
activation = [[0,0],[0,0]]
for coord, cell in self._cellular_automaton._current_state.items():
x,y,l,o = coord
automaton_state[o][l][y][x] = int(cell.state[0])
activation[o][l] += int(cell.state[0])
automaton_state = [[[''.join(str(bits) for bits in rows) for rows in layers] for layers in oscillators] for oscillators in automaton_state]
automaton_state = [['.'.join(str(rows) for rows in layers) for layers in oscillators] for oscillators in automaton_state]
automaton_state = ['-'.join(str(layers) for layers in oscillators) for oscillators in automaton_state]
automaton_state = '='.join(str(oscillators) for oscillators in automaton_state)
activation = [[round(1-2*act/self.__dimension**2, 6) for act in layer] for layer in activation]
self.__state_list.append(automaton_state)
self.__activation_list.append(activation)
def _save_state_list(self):
if not os.path.exists(self.__path):
@ -402,6 +424,8 @@ class Simulate4Layers:
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_states.txt", 'w', encoding='utf-8') as f:
json.dump(self.__state_list, f, indent=1)
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f:
json.dump(self.__activation_list, f, indent=1)
def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover
if evolutions_per_second > 0:
@ -450,6 +474,3 @@ class Simulate4Layers:
def print_process_info(self, evolve_duration, draw_duration, evolution_step, runlendig):
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))}')
def _is_not_user_terminated(self):
return self.__draw_engine.is_active()