neuropercolation/cellular_automaton/rule.py

49 lines
1.8 KiB
Python
Raw Normal View History

2019-02-23 15:20:48 +00:00
"""
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.
"""
import abc
import cellular_automaton.cellular_automaton.neighborhood as neighbour
2018-12-01 14:14:22 +00:00
2018-12-01 19:37:18 +00:00
class Rule:
2019-02-23 15:20:48 +00:00
def __init__(self, neighborhood_: neighbour.Neighborhood):
self._neighborhood = neighborhood_
2018-12-01 19:37:18 +00:00
2019-02-23 15:20:48 +00:00
def _get_neighbor_by_relative_coordinate(self, neighbours, rel_coordinate):
return neighbours[self._neighborhood.get_neighbor_id_from_rel(rel_coordinate)]
@abc.abstractmethod
def evolve_cell(self, last_cell_state, neighbors_last_states):
2018-12-02 16:45:08 +00:00
""" Calculates and sets new state of 'cell'.
2019-01-31 12:38:48 +00:00
:param last_cell_state: The cells current state to calculate new state for.
2019-02-16 17:05:26 +00:00
:param neighbors_last_states: The cells neighbors current states.
2019-02-17 10:11:27 +00:00
:return: New state.
2019-02-16 17:05:26 +00:00
A cells evolution will only be called if it or at least one of its neighbors has changed last evolution_step cycle.
2018-12-02 16:45:08 +00:00
"""
2019-02-17 10:11:27 +00:00
return last_cell_state
2019-02-23 15:20:48 +00:00
@abc.abstractmethod
def init_state(self, cell_coordinate):
""" Set the initial state for the cell with the given coordinate.
:param cell_coordinate: Cells coordinate.
:return: Iterable that represents the state
"""
return [0]
@abc.abstractmethod
def get_state_draw_color(self, current_state):
return [0, 0, 0]