MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / run

Method run

cellular_automata/wa_tor.py:431–478  ·  view source on GitHub ↗

Emulate time passing by looping `iteration_count` times >>> wt = WaTor(WIDTH, HEIGHT) >>> wt.run(iteration_count=PREDATOR_INITIAL_ENERGY_VALUE - 1) >>> len(list(filter(lambda entity: entity.prey is False, ... wt.get_entities()))) >= PREDATOR_INITIAL_COUNT

(self, *, iteration_count: int)

Source from the content-addressed store, hash-verified

429 entity.energy_value -= 1
430
431 def run(self, *, iteration_count: int) -> None:
432 """
433 Emulate time passing by looping `iteration_count` times
434
435 >>> wt = WaTor(WIDTH, HEIGHT)
436 >>> wt.run(iteration_count=PREDATOR_INITIAL_ENERGY_VALUE - 1)
437 >>> len(list(filter(lambda entity: entity.prey is False,
438 ... wt.get_entities()))) >= PREDATOR_INITIAL_COUNT
439 True
440 """
441 for iter_num in range(iteration_count):
442 # Generate list of all entities in order to randomly
443 # pop an entity at a time to simulate true randomness
444 # This removes the systematic approach of iterating
445 # through each entity width by height
446 all_entities = self.get_entities()
447
448 for __ in range(len(all_entities)):
449 entity = all_entities.pop(randint(0, len(all_entities) - 1))
450 if entity.alive is False:
451 continue
452
453 directions: list[Literal["N", "E", "S", "W"]] = ["N", "E", "S", "W"]
454 shuffle(directions) # Randomly shuffle directions
455
456 if entity.prey:
457 self.perform_prey_actions(entity, directions)
458 else:
459 # Create list of surrounding prey
460 surrounding_prey = self.get_surrounding_prey(entity)
461 surrounding_prey_coords = None
462
463 if surrounding_prey:
464 # Again, randomly shuffle directions
465 shuffle(surrounding_prey)
466 surrounding_prey_coords = surrounding_prey[0].coords
467
468 self.perform_predator_actions(
469 entity, surrounding_prey_coords, directions
470 )
471
472 # Balance out the predators and prey
473 self.balance_predators_and_prey()
474
475 if self.time_passed is not None:
476 # Call time_passed function for Wa-Tor planet
477 # visualisation in a terminal or a graph.
478 self.time_passed(self, iter_num)
479
480
481def visualise(wt: WaTor, iter_number: int, *, colour: bool = True) -> None:

Callers 2

wa_tor.pyFile · 0.80
tf_k_means_clusterFunction · 0.80

Calls 6

get_entitiesMethod · 0.95
perform_prey_actionsMethod · 0.95
get_surrounding_preyMethod · 0.95
popMethod · 0.45

Tested by

no test coverage detected