neuropercolation/examples/basic_2d_ca.py

42 lines
1.5 KiB
Python
Raw Normal View History

2018-12-01 14:14:22 +00:00
#!/usr/bin/env python3
2019-02-10 10:00:57 +00:00
import random
2019-02-17 10:11:27 +00:00
from cellular_automaton import *
2019-02-03 17:26:31 +00:00
2018-12-01 14:14:22 +00:00
2018-12-02 16:45:08 +00:00
class TestRule(Rule):
@staticmethod
2019-02-16 17:05:26 +00:00
def evolve_cell(last_cell_state, neighbors_last_states):
try:
2019-02-16 17:05:26 +00:00
return neighbors_last_states[0]
except IndexError:
return last_cell_state
2018-12-01 19:37:18 +00:00
2019-02-16 17:05:26 +00:00
# class MyState(SynchronousCellState):
class MyState(CellState):
random_seed = random.seed(1000)
2019-01-31 12:38:48 +00:00
def __init__(self):
rand = random.randrange(0, 101, 1)
init = max(.0, float(rand - 99))
super().__init__((init,), draw_first_state=init > 0)
2018-12-09 10:20:16 +00:00
2019-02-16 17:05:26 +00:00
def get_state_draw_color(self, evolution_step):
state = self.get_state_of_evolution_step(evolution_step)[0]
return [255 if state else 0, 0, 0]
2019-02-03 17:26:31 +00:00
2018-12-01 14:14:22 +00:00
if __name__ == "__main__":
2019-02-09 18:06:18 +00:00
# best single is 400/400 with 0,2 ca speed and 0,09 redraw / multi is 300/300 with 0.083
2019-02-03 17:26:31 +00:00
neighborhood = MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)
2019-02-16 17:05:26 +00:00
ca = CAFactory.make_cellular_automaton(dimension=[100, 100],
neighborhood=neighborhood,
rule=TestRule(),
state_class=MyState)
# ca_processor = CellularAutomatonMultiProcessor(cellular_automaton=ca, process_count=4)
ca_processor = CellularAutomatonProcessor(cellular_automaton=ca)
2019-02-10 10:00:57 +00:00
ca_window = PyGameFor2D(window_size=[1000, 800], cellular_automaton=ca)
2019-02-16 17:05:26 +00:00
ca_window.main_loop(cellular_automaton_processor=ca_processor, evolution_steps_per_draw=1)