Compare commits

...

14 Commits

Author SHA1 Message Date
pre-commit-ci[bot]
318e087397 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-07-30 19:07:45 +00:00
CaedenPH
e6f3995219 refactor(wa-tor): Use double for loop 2023-07-30 20:07:17 +01:00
CaedenPH
5ba7243e12 refactor(display): Rename to display_visually to visualise 2023-07-30 20:04:16 +01:00
Caeden Perelli-Harris
8670299598
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:40:24 +01:00
Caeden Perelli-Harris
7dc67884d1
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:38:09 +01:00
Caeden Perelli-Harris
adcc4ff659
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:37:40 +01:00
Caeden Perelli-Harris
bf422b1f32
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:37:11 +01:00
Caeden Perelli-Harris
a87d6720d5
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:35:39 +01:00
Caeden Perelli-Harris
9d8afeb9a1
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:35:18 +01:00
Caeden Perelli-Harris
d277dba404
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:34:00 +01:00
Caeden Perelli-Harris
e659bd100d
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:32:58 +01:00
Caeden Perelli-Harris
a9fb2ed3fa
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:31:40 +01:00
Caeden Perelli-Harris
8554b71947
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:30:41 +01:00
Caeden Perelli-Harris
9f23c25f96
Update cellular_automata/wa_tor.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-07-30 19:29:49 +01:00

View File

@ -15,7 +15,7 @@ to result in one of the three possible results.
from collections.abc import Callable from collections.abc import Callable
from random import randint, shuffle from random import randint, shuffle
from time import sleep from time import sleep
from typing import Any, Literal from typing import Literal
WIDTH = 50 # Width of the Wa-Tor planet WIDTH = 50 # Width of the Wa-Tor planet
HEIGHT = 50 # Height of the Wa-Tor planet HEIGHT = 50 # Height of the Wa-Tor planet
@ -54,7 +54,7 @@ class Entity:
self.coords = coords self.coords = coords
self.remaining_reproduction_time = ( self.remaining_reproduction_time = (
PREY_REPRODUCTION_TIME if prey is True else PREDATOR_REPRODUCTION_TIME PREY_REPRODUCTION_TIME if prey else PREDATOR_REPRODUCTION_TIME
) )
self.energy_value = None if prey is True else PREDATOR_INITIAL_ENERGY_VALUE self.energy_value = None if prey is True else PREDATOR_INITIAL_ENERGY_VALUE
self.alive = True self.alive = True
@ -124,9 +124,9 @@ class WaTor:
# Populate planet with predators and prey randomly # Populate planet with predators and prey randomly
for _ in range(PREY_INITIAL_COUNT): for _ in range(PREY_INITIAL_COUNT):
self.add_entity(True) self.add_entity(prey=True)
for _ in range(PREDATOR_INITIAL_COUNT): for _ in range(PREDATOR_INITIAL_COUNT):
self.add_entity(False) self.add_entity(prey=False)
self.set_planet(self.planet) self.set_planet(self.planet)
def set_planet(self, planet: list[list[Entity | None]]) -> None: def set_planet(self, planet: list[list[Entity | None]]) -> None:
@ -167,8 +167,8 @@ class WaTor:
while True: while True:
row, col = randint(0, self.height - 1), randint(0, self.width - 1) row, col = randint(0, self.height - 1), randint(0, self.width - 1)
if self.planet[row][col] is None: if self.planet[row][col] is None:
break
self.planet[row][col] = Entity(prey=prey, coords=(row, col)) self.planet[row][col] = Entity(prey=prey, coords=(row, col))
return
def get_entities(self) -> list[Entity]: def get_entities(self) -> list[Entity]:
""" """
@ -178,11 +178,7 @@ class WaTor:
>>> len(wt.get_entities()) == PREDATOR_INITIAL_COUNT + PREY_INITIAL_COUNT >>> len(wt.get_entities()) == PREDATOR_INITIAL_COUNT + PREY_INITIAL_COUNT
True True
""" """
start: Any = [] return [entity for column in self.planet for entity in column if entity]
return sum(
[[entity for entity in column if entity] for column in self.planet],
start=start,
)
def balance_predators_and_prey(self) -> None: def balance_predators_and_prey(self) -> None:
""" """
@ -203,18 +199,17 @@ class WaTor:
shuffle(entities) shuffle(entities)
if len(entities) >= MAX_ENTITIES - MAX_ENTITIES / 10: if len(entities) >= MAX_ENTITIES - MAX_ENTITIES / 10:
prey = list(filter(lambda entity: entity.prey is True, entities)) prey = [entity for entity in entities if entity.prey]
predators = list(filter(lambda entity: entity.prey is True, entities)) predators = [entity for entity in entities if not entity.prey]
prey_count, predator_count = len(prey), len(predators) prey_count, predator_count = len(prey), len(predators)
if prey_count > predator_count: entities_to_purge = (
for entity in prey[:DELETE_UNBALANCED_ENTITIES]: prey[:DELETE_UNBALANCED_ENTITIES]
# Purge the first n entities of the prey if prey_count > predator_count
self.planet[entity.coords[0]][entity.coords[1]] = None else predators[:DELETE_UNBALANCED_ENTITIES]
else: )
for entity in predators[:DELETE_UNBALANCED_ENTITIES]: for entity in entities_to_purge:
# Purge the first n entities of the predators
self.planet[entity.coords[0]][entity.coords[1]] = None self.planet[entity.coords[0]][entity.coords[1]] = None
def get_surrounding_prey(self, entity: Entity) -> list[Entity]: def get_surrounding_prey(self, entity: Entity) -> list[Entity]:
@ -335,8 +330,7 @@ class WaTor:
Entity(prey=False, coords=(0, 1), remaining_reproduction_time=20, Entity(prey=False, coords=(0, 1), remaining_reproduction_time=20,
energy_value=15)]] energy_value=15)]]
""" """
coords = entity.coords row, col = coords = entity.coords
row, col = coords
for direction in direction_orders: for direction in direction_orders:
# If the direction is North and the northern square # If the direction is North and the northern square
@ -515,7 +509,7 @@ class WaTor:
self.time_passed(self, iter_num) self.time_passed(self, iter_num)
def display_visually(wt: WaTor, iter_number: int, *, colour: bool = True) -> None: def visualise(wt: WaTor, iter_number: int, *, colour: bool = True) -> None:
""" """
Visually displays the Wa-Tor planet using Visually displays the Wa-Tor planet using
an ascii code in terminal to clear and re-print an ascii code in terminal to clear and re-print
@ -533,27 +527,27 @@ def display_visually(wt: WaTor, iter_number: int, *, colour: bool = True) -> Non
... [Entity(False, coords=(1, 0)), None, Entity(False, coords=(1, 2))], ... [Entity(False, coords=(1, 0)), None, Entity(False, coords=(1, 2))],
... [None, Entity(True, coords=(2, 1)), None] ... [None, Entity(True, coords=(2, 1)), None]
... ]) ... ])
>>> display_visually(wt, 0, colour=False) # doctest: +NORMALIZE_WHITESPACE >>> visualise(wt, 0, colour=False) # doctest: +NORMALIZE_WHITESPACE
# x . # x .
x . x x . x
. # . . # .
<BLANKLINE> <BLANKLINE>
Iteration: 0 | Prey count: 2 | Predator count: 3 | Iteration: 0 | Prey count: 2 | Predator count: 3 |
""" """
if colour is True: if colour:
__import__("os").system("") __import__("os").system("")
print("\x1b[0;0H\x1b[2J\x1b[?25l") print("\x1b[0;0H\x1b[2J\x1b[?25l")
reprint = "\x1b[0;0H" if colour is True else "" reprint = "\x1b[0;0H" if colour else ""
ansii_colour_end = "\x1b[0m " if colour is True else " " ansi_colour_end = "\x1b[0m " if colour else " "
planet = wt.planet planet = wt.planet
output = "" output = ""
# Iterate over every entity in the planet # Iterate over every entity in the planet
for i in range(len(planet)): for row in planet:
for j in range(len(planet[0])): for entity in row:
if (entity := planet[i][j]) is None: if entity is None:
output += " . " output += " . "
else: else:
if colour is True: if colour is True:
@ -562,12 +556,12 @@ def display_visually(wt: WaTor, iter_number: int, *, colour: bool = True) -> Non
if entity.prey if entity.prey
else "\x1b[38;2;255;255;15m" else "\x1b[38;2;255;255;15m"
) )
output += f" {'#' if entity.prey else 'x'}{ansii_colour_end}" output += f" {'#' if entity.prey else 'x'}{ansi_colour_end}"
output += "\n" output += "\n"
entities = wt.get_entities() entities = wt.get_entities()
prey_count = len(list(filter(lambda entity: entity.prey is True, entities))) prey_count = sum(entity.prey for entity in entities)
print( print(
f"{output}\n Iteration: {iter_number} | Prey count: {prey_count} | " f"{output}\n Iteration: {iter_number} | Prey count: {prey_count} | "
@ -583,5 +577,5 @@ if __name__ == "__main__":
doctest.testmod() doctest.testmod()
wt = WaTor(WIDTH, HEIGHT) wt = WaTor(WIDTH, HEIGHT)
wt.time_passed = display_visually wt.time_passed = visualise
wt.run(iteration_count=100_000) wt.run(iteration_count=100_000)