fixed wrong evolution step used in display
This commit is contained in:
parent
924a851c43
commit
4507327bd5
@ -15,7 +15,8 @@ limitations under the License.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
from multiprocessing import freeze_support
|
|
||||||
|
from multiprocessing.sharedctypes import RawValue
|
||||||
from ctypes import c_int
|
from ctypes import c_int
|
||||||
|
|
||||||
|
|
||||||
@ -28,10 +29,10 @@ class CellularAutomatonProcessor:
|
|||||||
self.evolve()
|
self.evolve()
|
||||||
|
|
||||||
def evolve(self):
|
def evolve(self):
|
||||||
|
self._ca.current_evolution_step += 1
|
||||||
i = self._ca.current_evolution_step
|
i = self._ca.current_evolution_step
|
||||||
r = self._ca.evolution_rule.evolve_cell
|
r = self._ca.evolution_rule.evolve_cell
|
||||||
list(map(lambda c: c.evolve_if_ready(r, i), tuple(self._ca.cells.values())))
|
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):
|
def get_dimension(self):
|
||||||
return self._ca.dimension
|
return self._ca.dimension
|
||||||
@ -48,14 +49,14 @@ class CellularAutomatonProcessor:
|
|||||||
|
|
||||||
class CellularAutomatonMultiProcessor(CellularAutomatonProcessor):
|
class CellularAutomatonMultiProcessor(CellularAutomatonProcessor):
|
||||||
def __init__(self, cellular_automaton, process_count: int = 2):
|
def __init__(self, cellular_automaton, process_count: int = 2):
|
||||||
freeze_support()
|
multiprocessing.freeze_support()
|
||||||
if process_count < 1:
|
if process_count < 1:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
super().__init__(cellular_automaton)
|
super().__init__(cellular_automaton)
|
||||||
|
|
||||||
self.evolve_range = range(len(self._ca.cells))
|
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)
|
self.__init_processes_and_clean_cell_instances(process_count)
|
||||||
|
|
||||||
def __init_processes_and_clean_cell_instances(self, process_count):
|
def __init_processes_and_clean_cell_instances(self, process_count):
|
||||||
@ -66,8 +67,8 @@ class CellularAutomatonMultiProcessor(CellularAutomatonProcessor):
|
|||||||
self._ca.current_evolution_step))
|
self._ca.current_evolution_step))
|
||||||
|
|
||||||
def evolve(self):
|
def evolve(self):
|
||||||
self.pool.map(_process_routine, self.evolve_range)
|
|
||||||
self._ca.current_evolution_step.value += 1
|
self._ca.current_evolution_step.value += 1
|
||||||
|
self.pool.map(_process_routine, self.evolve_range)
|
||||||
|
|
||||||
def get_current_evolution_step(self):
|
def get_current_evolution_step(self):
|
||||||
return self._ca.current_evolution_step.value
|
return self._ca.current_evolution_step.value
|
||||||
|
@ -14,11 +14,11 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import cellular_automaton.cellular_automaton.cell_state as cs
|
from . import cell_state
|
||||||
|
|
||||||
|
|
||||||
class Cell:
|
class Cell:
|
||||||
def __init__(self, state_class: cs.CellState, neighbors):
|
def __init__(self, state_class: cell_state.CellState, neighbors):
|
||||||
self._state = state_class
|
self._state = state_class
|
||||||
self._neighbor_states = neighbors
|
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])
|
[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)
|
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)
|
changed = self._state.set_state_of_evolution_step(new_state, evolution_step)
|
||||||
if changed:
|
if changed:
|
||||||
self._state.set_active_for_next_evolution_step(evolution_step)
|
self._state.set_active_for_next_evolution_step(evolution_step)
|
||||||
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from multiprocessing import RawArray, RawValue
|
from multiprocessing.sharedctypes import RawArray, RawValue
|
||||||
from ctypes import c_float, c_bool
|
from ctypes import c_float, c_bool
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,11 +18,11 @@ import pygame
|
|||||||
import time
|
import time
|
||||||
import operator
|
import operator
|
||||||
|
|
||||||
import cellular_automaton.cellular_automaton.automaton as automaton
|
from . import automaton
|
||||||
|
|
||||||
|
|
||||||
class _CASurface:
|
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._cellular_automaton = cellular_automaton
|
||||||
self.__rect = grid_rect
|
self.__rect = grid_rect
|
||||||
self.__cell_size = self._calculate_cell_display_size()
|
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]]
|
return [self.__rect.width / grid_dimension[0], self.__rect.height / grid_dimension[1]]
|
||||||
|
|
||||||
def redraw_cellular_automaton(self):
|
def redraw_cellular_automaton(self):
|
||||||
update_rects = list(self.__cell_redraw_dirty_rectangles())
|
update_rectangles = list(self.__cell_redraw_dirty_rectangles())
|
||||||
pygame.display.update(update_rects)
|
pygame.display.update(update_rectangles)
|
||||||
|
|
||||||
def __cell_redraw_dirty_rectangles(self):
|
def __cell_redraw_dirty_rectangles(self):
|
||||||
for coordinate, cell in self._cellular_automaton.get_cells().items():
|
for coordinate, cell in self._cellular_automaton.get_cells().items():
|
||||||
@ -45,8 +45,8 @@ class _CASurface:
|
|||||||
cell_color = self.__get_cell_color(cell)
|
cell_color = self.__get_cell_color(cell)
|
||||||
cell_pos = self._calculate_cell_position_in_the_grid(coordinate)
|
cell_pos = self._calculate_cell_position_in_the_grid(coordinate)
|
||||||
surface_pos = self._calculate_cell_position_on_screen(cell_pos)
|
surface_pos = self._calculate_cell_position_on_screen(cell_pos)
|
||||||
yield self._draw_the_cell_to_screen(cell_color, surface_pos)
|
|
||||||
cell.was_redrawn()
|
cell.was_redrawn()
|
||||||
|
yield self._draw_the_cell_to_screen(cell_color, surface_pos)
|
||||||
|
|
||||||
def __get_cell_color(self, cell):
|
def __get_cell_color(self, cell):
|
||||||
return self._cellular_automaton.get_current_rule().get_state_draw_color(
|
return self._cellular_automaton.get_current_rule().get_state_draw_color(
|
||||||
|
@ -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
|
Copyright 2019 Richard Feistenauer
|
||||||
|
|
||||||
@ -22,6 +16,14 @@ limitations under the License.
|
|||||||
|
|
||||||
import itertools
|
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:
|
class CAFactory:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -15,11 +15,12 @@ limitations under the License.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
import cellular_automaton.cellular_automaton.neighborhood as neighbour
|
|
||||||
|
from . import neighborhood
|
||||||
|
|
||||||
|
|
||||||
class Rule:
|
class Rule:
|
||||||
def __init__(self, neighborhood_: neighbour.Neighborhood):
|
def __init__(self, neighborhood_: neighborhood.Neighborhood):
|
||||||
self._neighborhood = neighborhood_
|
self._neighborhood = neighborhood_
|
||||||
|
|
||||||
def _get_neighbor_by_relative_coordinate(self, neighbours, rel_coordinate):
|
def _get_neighbor_by_relative_coordinate(self, neighbours, rel_coordinate):
|
||||||
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from cellular_automaton.cellular_automaton import Rule
|
from . import Rule
|
||||||
|
|
||||||
|
|
||||||
class CellularAutomatonState:
|
class CellularAutomatonState:
|
||||||
|
@ -14,7 +14,7 @@ class TestRule(Rule):
|
|||||||
def init_state(self, cell_coordinate):
|
def init_state(self, cell_coordinate):
|
||||||
rand = random.randrange(0, 16, 1)
|
rand = random.randrange(0, 16, 1)
|
||||||
init = max(.0, float(rand - 14))
|
init = max(.0, float(rand - 14))
|
||||||
return (init,)
|
return [init]
|
||||||
|
|
||||||
def evolve_cell(self, last_cell_state, neighbors_last_states):
|
def evolve_cell(self, last_cell_state, neighbors_last_states):
|
||||||
new_cell_state = last_cell_state
|
new_cell_state = last_cell_state
|
||||||
|
@ -10,7 +10,7 @@ class TestRule(Rule):
|
|||||||
def init_state(self, cell_coordinate):
|
def init_state(self, cell_coordinate):
|
||||||
rand = random.randrange(0, 101, 1)
|
rand = random.randrange(0, 101, 1)
|
||||||
init = max(.0, float(rand - 99))
|
init = max(.0, float(rand - 99))
|
||||||
return (init,)
|
return [init]
|
||||||
|
|
||||||
def evolve_cell(self, last_cell_state, neighbors_last_states):
|
def evolve_cell(self, last_cell_state, neighbors_last_states):
|
||||||
return self._get_neighbor_by_relative_coordinate(neighbors_last_states, (-1, -1))
|
return self._get_neighbor_by_relative_coordinate(neighbors_last_states, (-1, -1))
|
||||||
|
Loading…
Reference in New Issue
Block a user