Add save_states option

This commit is contained in:
timofej 2023-08-19 21:33:25 +02:00
parent 88d4d2c9ea
commit c37f63f7bf

View File

@ -99,7 +99,8 @@ class Simulate1Layer:
def run(self, def run(self,
evolutions_per_second=0, evolutions_per_second=0,
evolutions_per_draw=1, evolutions_per_draw=1,
last_evolution_step=0,): last_evolution_step=0,
save_states=True):
""" """
Evolves and draws the CellularAutomaton Evolves and draws the CellularAutomaton
:param evolutions_per_second: 0 = as fast as possible | > 0 to slow down the CellularAutomaton :param evolutions_per_second: 0 = as fast as possible | > 0 to slow down the CellularAutomaton
@ -107,12 +108,18 @@ class Simulate1Layer:
:param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop :param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop
Warning: is blocking until finished Warning: is blocking until finished
""" """
self._append_state() if save_states:
self._append_state()
else:
self._append_activation()
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
while self._not_at_the_end(last_evolution_step): while 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._append_state() if save_states:
self._append_state()
else:
self._append_activation()
if self.__draw_engine is not None: if self.__draw_engine is not None:
time_ca_end = time.time() time_ca_end = time.time()
self._redraw_dirty_cells() self._redraw_dirty_cells()
@ -127,7 +134,9 @@ class Simulate1Layer:
self.__draw_engine._pygame.quit() self.__draw_engine._pygame.quit()
except: except:
print('Failed to quit pygame') print('Failed to quit pygame')
self._save_state_list() if save_states:
self._save_state_list()
self._save_activation_list()
def _append_state(self): def _append_state(self):
automaton_state = [[0 for n in range(self.__dimension)] for m in range(self.__dimension)] automaton_state = [[0 for n in range(self.__dimension)] for m in range(self.__dimension)]
@ -144,12 +153,26 @@ class Simulate1Layer:
self.__state_list.append(automaton_state) self.__state_list.append(automaton_state)
self.__activation_list.append(activation) self.__activation_list.append(activation)
def _append_activation(self):
activation = 0
for coord, cell in self._cellular_automaton._current_state.items():
x,y = coord
activation += int(cell.state[0])
activation = round(1-2*activation/self.__dimension**2, 6)
self.__activation_list.append(activation)
def _save_state_list(self): def _save_state_list(self):
if not os.path.exists(self.__path): if not os.path.exists(self.__path):
os.makedirs(self.__path) os.makedirs(self.__path)
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_states.txt", 'w', encoding='utf-8') as f: 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) json.dump(self.__state_list, f, indent=1)
def _save_activation_list(self):
if not os.path.exists(self.__path):
os.makedirs(self.__path)
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f: 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) json.dump(self.__activation_list, f, indent=1)
@ -232,7 +255,8 @@ class Simulate2Layers:
def run(self, def run(self,
evolutions_per_second=0, evolutions_per_second=0,
evolutions_per_draw=1, evolutions_per_draw=1,
last_evolution_step=0,): last_evolution_step=0,
save_states=True):
""" """
Evolves and draws the CellularAutomaton Evolves and draws the CellularAutomaton
:param evolutions_per_second: 0 = as fast as possible | > 0 to slow down the CellularAutomaton :param evolutions_per_second: 0 = as fast as possible | > 0 to slow down the CellularAutomaton
@ -240,12 +264,18 @@ class Simulate2Layers:
:param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop :param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop
Warning: is blocking until finished Warning: is blocking until finished
""" """
self._append_state() if save_states:
self._append_state()
else:
self._append_activation()
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
while self._not_at_the_end(last_evolution_step): while 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._append_state() if save_states:
self._append_state()
else:
self._append_activation()
if self.__draw_engine is not None: if self.__draw_engine is not None:
time_ca_end = time.time() time_ca_end = time.time()
self._redraw_dirty_cells() self._redraw_dirty_cells()
@ -255,12 +285,16 @@ class Simulate2Layers:
evolution_step=self._cellular_automaton.evolution_step, evolution_step=self._cellular_automaton.evolution_step,
runlendig=len(str(last_evolution_step))) runlendig=len(str(last_evolution_step)))
self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second)
if self.__draw_engine is not None: if self.__draw_engine is not None:
try: try:
self.__draw_engine._pygame.quit() self.__draw_engine._pygame.quit()
except: except:
print('Failed to quit pygame') print('Failed to quit pygame')
self._save_state_list()
if save_states:
self._save_state_list()
self._save_activation_list()
def _append_state(self): def _append_state(self):
automaton_state = [[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)] automaton_state = [[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)]
@ -269,13 +303,21 @@ class Simulate2Layers:
x,y,l = coord x,y,l = coord
automaton_state[l][y][x] = int(cell.state[0]) automaton_state[l][y][x] = int(cell.state[0])
activation[l] += 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(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(rows) for rows in layers) for layers in automaton_state]
automaton_state = '-'.join(str(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 _append_activation(self):
activation = [0]*2
for coord, cell in self._cellular_automaton._current_state.items():
x,y,l = coord
activation[l] += int(cell.state[0])
activation = [round(1-2*act/self.__dimension**2, 6) for act in activation] activation = [round(1-2*act/self.__dimension**2, 6) for act in activation]
self.__state_list.append(automaton_state)
self.__activation_list.append(activation) self.__activation_list.append(activation)
def _save_state_list(self): def _save_state_list(self):
@ -284,6 +326,11 @@ class Simulate2Layers:
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_states.txt", 'w', encoding='utf-8') as f: 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) json.dump(self.__state_list, f, indent=1)
def _save_activation_list(self):
if not os.path.exists(self.__path):
os.makedirs(self.__path)
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f: 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) json.dump(self.__activation_list, f, indent=1)
@ -371,7 +418,8 @@ class Simulate4Layers:
def run(self, def run(self,
evolutions_per_second=0, evolutions_per_second=0,
evolutions_per_draw=1, evolutions_per_draw=1,
last_evolution_step=0,): last_evolution_step=0,
save_states=True):
""" """
Evolves and draws the CellularAutomaton Evolves and draws the CellularAutomaton
:param evolutions_per_second: 0 = as fast as possible | > 0 to slow down the CellularAutomaton :param evolutions_per_second: 0 = as fast as possible | > 0 to slow down the CellularAutomaton
@ -380,11 +428,17 @@ class Simulate4Layers:
Warning: is blocking until finished Warning: is blocking until finished
""" """
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
self._append_state() if save_states:
self._append_state()
else:
self._append_activation()
while self._not_at_the_end(last_evolution_step): while 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._append_state() if save_states:
self._append_state()
else:
self._append_activation()
if self.__draw_engine is not None: if self.__draw_engine is not None:
time_ca_end = time.time() time_ca_end = time.time()
self._redraw_dirty_cells() self._redraw_dirty_cells()
@ -399,8 +453,10 @@ class Simulate4Layers:
self.__draw_engine._pygame.quit() self.__draw_engine._pygame.quit()
except: except:
print('Failed to quit pygame') print('Failed to quit pygame')
self._save_state_list() if save_states:
self._save_state_list()
self._save_activation_list()
def _append_state(self): 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)] 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]] activation = [[0,0],[0,0]]
@ -418,12 +474,26 @@ class Simulate4Layers:
self.__state_list.append(automaton_state) self.__state_list.append(automaton_state)
self.__activation_list.append(activation) self.__activation_list.append(activation)
def _append_activation(self):
activation = [[0,0],[0,0]]
for coord, cell in self._cellular_automaton._current_state.items():
x,y,l,o = coord
activation[o][l] += int(cell.state[0])
activation = [[round(1-2*act/self.__dimension**2, 6) for act in layer] for layer in activation]
self.__activation_list.append(activation)
def _save_state_list(self): def _save_state_list(self):
if not os.path.exists(self.__path): if not os.path.exists(self.__path):
os.makedirs(self.__path) os.makedirs(self.__path)
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_states.txt", 'w', encoding='utf-8') as f: 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) json.dump(self.__state_list, f, indent=1)
def _save_activation_list(self):
if not os.path.exists(self.__path):
os.makedirs(self.__path)
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f: 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) json.dump(self.__activation_list, f, indent=1)