From 4507327bd5d6ef276311b527b91ddfcc6d3cd7cc Mon Sep 17 00:00:00 2001 From: Richard Feistenauer Date: Sat, 23 Feb 2019 17:37:18 +0100 Subject: [PATCH] fixed wrong evolution step used in display --- cellular_automaton/automaton.py | 11 ++++++----- cellular_automaton/cell.py | 6 +++--- cellular_automaton/cell_state.py | 2 +- cellular_automaton/display.py | 10 +++++----- cellular_automaton/factory.py | 14 ++++++++------ cellular_automaton/rule.py | 5 +++-- cellular_automaton/state.py | 2 +- examples/conways_game_of_life.py | 2 +- examples/simple_star_fall.py | 2 +- 9 files changed, 29 insertions(+), 25 deletions(-) diff --git a/cellular_automaton/automaton.py b/cellular_automaton/automaton.py index 52362a4..7b0ea0e 100644 --- a/cellular_automaton/automaton.py +++ b/cellular_automaton/automaton.py @@ -15,7 +15,8 @@ limitations under the License. """ import multiprocessing -from multiprocessing import freeze_support + +from multiprocessing.sharedctypes import RawValue from ctypes import c_int @@ -28,10 +29,10 @@ class CellularAutomatonProcessor: self.evolve() def evolve(self): + self._ca.current_evolution_step += 1 i = self._ca.current_evolution_step r = self._ca.evolution_rule.evolve_cell list(map(lambda c: c.evolve_if_ready(r, i), tuple(self._ca.cells.values()))) - self._ca.current_evolution_step += 1 def get_dimension(self): return self._ca.dimension @@ -48,14 +49,14 @@ class CellularAutomatonProcessor: class CellularAutomatonMultiProcessor(CellularAutomatonProcessor): def __init__(self, cellular_automaton, process_count: int = 2): - freeze_support() + multiprocessing.freeze_support() if process_count < 1: raise ValueError super().__init__(cellular_automaton) self.evolve_range = range(len(self._ca.cells)) - self._ca.current_evolution_step = multiprocessing.RawValue(c_int, self._ca.current_evolution_step) + self._ca.current_evolution_step = RawValue(c_int, self._ca.current_evolution_step) self.__init_processes_and_clean_cell_instances(process_count) def __init_processes_and_clean_cell_instances(self, process_count): @@ -66,8 +67,8 @@ class CellularAutomatonMultiProcessor(CellularAutomatonProcessor): self._ca.current_evolution_step)) def evolve(self): - self.pool.map(_process_routine, self.evolve_range) self._ca.current_evolution_step.value += 1 + self.pool.map(_process_routine, self.evolve_range) def get_current_evolution_step(self): return self._ca.current_evolution_step.value diff --git a/cellular_automaton/cell.py b/cellular_automaton/cell.py index 85a5d29..c610ae1 100644 --- a/cellular_automaton/cell.py +++ b/cellular_automaton/cell.py @@ -14,11 +14,11 @@ See the License for the specific language governing permissions and limitations under the License. """ -import cellular_automaton.cellular_automaton.cell_state as cs +from . import cell_state class Cell: - def __init__(self, state_class: cs.CellState, neighbors): + def __init__(self, state_class: cell_state.CellState, neighbors): self._state = state_class self._neighbor_states = neighbors @@ -37,7 +37,7 @@ class Cell: [list(n.get_state_of_last_evolution_step(evolution_step)) for n in self._neighbor_states]) self.set_new_state_and_activate(new_state, evolution_step) - def set_new_state_and_activate(self, new_state: cs.CellState, evolution_step): + def set_new_state_and_activate(self, new_state: cell_state.CellState, evolution_step): changed = self._state.set_state_of_evolution_step(new_state, evolution_step) if changed: self._state.set_active_for_next_evolution_step(evolution_step) diff --git a/cellular_automaton/cell_state.py b/cellular_automaton/cell_state.py index 7f71bc5..ad3e34c 100644 --- a/cellular_automaton/cell_state.py +++ b/cellular_automaton/cell_state.py @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. """ -from multiprocessing import RawArray, RawValue +from multiprocessing.sharedctypes import RawArray, RawValue from ctypes import c_float, c_bool diff --git a/cellular_automaton/display.py b/cellular_automaton/display.py index 6d357c3..c6a147e 100644 --- a/cellular_automaton/display.py +++ b/cellular_automaton/display.py @@ -18,11 +18,11 @@ import pygame import time import operator -import cellular_automaton.cellular_automaton.automaton as automaton +from . import automaton class _CASurface: - def __init__(self, grid_rect: pygame.Rect, cellular_automaton: automaton.CellularAutomatonProcessor, screen): + def __init__(self, grid_rect, cellular_automaton: automaton.CellularAutomatonProcessor, screen): self._cellular_automaton = cellular_automaton self.__rect = grid_rect self.__cell_size = self._calculate_cell_display_size() @@ -33,8 +33,8 @@ class _CASurface: return [self.__rect.width / grid_dimension[0], self.__rect.height / grid_dimension[1]] def redraw_cellular_automaton(self): - update_rects = list(self.__cell_redraw_dirty_rectangles()) - pygame.display.update(update_rects) + update_rectangles = list(self.__cell_redraw_dirty_rectangles()) + pygame.display.update(update_rectangles) def __cell_redraw_dirty_rectangles(self): for coordinate, cell in self._cellular_automaton.get_cells().items(): @@ -45,8 +45,8 @@ class _CASurface: cell_color = self.__get_cell_color(cell) cell_pos = self._calculate_cell_position_in_the_grid(coordinate) surface_pos = self._calculate_cell_position_on_screen(cell_pos) - yield self._draw_the_cell_to_screen(cell_color, surface_pos) cell.was_redrawn() + yield self._draw_the_cell_to_screen(cell_color, surface_pos) def __get_cell_color(self, cell): return self._cellular_automaton.get_current_rule().get_state_draw_color( diff --git a/cellular_automaton/factory.py b/cellular_automaton/factory.py index c113372..d0620e6 100644 --- a/cellular_automaton/factory.py +++ b/cellular_automaton/factory.py @@ -1,9 +1,3 @@ -from . import Neighborhood, Rule -from .automaton import CellularAutomatonProcessor, CellularAutomatonMultiProcessor -from .cell import Cell -from .state import CellularAutomatonState -from .cell_state import CellState, SynchronousCellState -from typing import Type """ Copyright 2019 Richard Feistenauer @@ -22,6 +16,14 @@ limitations under the License. import itertools +from typing import Type + +from . import Neighborhood, Rule +from .automaton import CellularAutomatonProcessor, CellularAutomatonMultiProcessor +from .cell import Cell +from .state import CellularAutomatonState +from .cell_state import CellState, SynchronousCellState + class CAFactory: @staticmethod diff --git a/cellular_automaton/rule.py b/cellular_automaton/rule.py index 9c2536c..ea41100 100644 --- a/cellular_automaton/rule.py +++ b/cellular_automaton/rule.py @@ -15,11 +15,12 @@ limitations under the License. """ import abc -import cellular_automaton.cellular_automaton.neighborhood as neighbour + +from . import neighborhood class Rule: - def __init__(self, neighborhood_: neighbour.Neighborhood): + def __init__(self, neighborhood_: neighborhood.Neighborhood): self._neighborhood = neighborhood_ def _get_neighbor_by_relative_coordinate(self, neighbours, rel_coordinate): diff --git a/cellular_automaton/state.py b/cellular_automaton/state.py index 0dc923e..d2de334 100644 --- a/cellular_automaton/state.py +++ b/cellular_automaton/state.py @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. """ -from cellular_automaton.cellular_automaton import Rule +from . import Rule class CellularAutomatonState: diff --git a/examples/conways_game_of_life.py b/examples/conways_game_of_life.py index 66fad72..d035e55 100644 --- a/examples/conways_game_of_life.py +++ b/examples/conways_game_of_life.py @@ -14,7 +14,7 @@ class TestRule(Rule): def init_state(self, cell_coordinate): rand = random.randrange(0, 16, 1) init = max(.0, float(rand - 14)) - return (init,) + return [init] def evolve_cell(self, last_cell_state, neighbors_last_states): new_cell_state = last_cell_state diff --git a/examples/simple_star_fall.py b/examples/simple_star_fall.py index 61363a5..bdcf8c2 100644 --- a/examples/simple_star_fall.py +++ b/examples/simple_star_fall.py @@ -10,7 +10,7 @@ class TestRule(Rule): def init_state(self, cell_coordinate): rand = random.randrange(0, 101, 1) init = max(.0, float(rand - 99)) - return (init,) + return [init] def evolve_cell(self, last_cell_state, neighbors_last_states): return self._get_neighbor_by_relative_coordinate(neighbors_last_states, (-1, -1))