neuropercolation/examples/simple_star_fall.py
Richard Feistenauer 9b527a044f Reworked CellularAutomaton to improve API and speed
+ Added CI
+ Restructured Project
+ Improved API Improved creation speed by factor of \~2
+ Improved execution speed by factor of \~15

- Removed multi processor since it doesn't work with the new setup and was not fast enough to matter.
2020-10-20 10:14:05 +00:00

58 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""
Copyright 2019 Richard Feistenauer
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# pylint: disable=wrong-import-position
# pylint: disable=missing-function-docstring
# pylint: disable=no-self-use
import random
import contextlib
import sys
import os
from typing import Sequence
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from cellular_automaton import CellularAutomaton, MooreNeighborhood, CAWindow, EdgeRule
class StarFallAutomaton(CellularAutomaton):
""" Represents an automaton dropping colorful stars """
def __init__(self):
super().__init__(dimension=[100, 100],
neighborhood=MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS))
def init_cell_state(self, __) -> Sequence:
rand = random.randrange(0, 101, 1)
init = max(.0, float(rand - 99))
return [init * random.randint(0, 3)]
def evolve_rule(self, __, neighbors_last_states: Sequence) -> Sequence:
return self._neighborhood.get_neighbor_by_relative_coordinate(neighbors_last_states, (-1, -1))
def state_to_color(current_state: Sequence) -> Sequence:
return 255 if current_state[0] == 1 else 0, \
255 if current_state[0] == 2 else 0, \
255 if current_state[0] == 3 else 0
if __name__ == "__main__":
with contextlib.suppress(KeyboardInterrupt):
CAWindow(cellular_automaton=StarFallAutomaton(), state_to_color_cb=state_to_color).run()