It's good that you're assigning symbols for these values, but you should consider taking it a step further and using.
Among other things, it'll make it easier for you to switch to system Python if you need it. Usually you should just #!/usr/bin/env pythonĪnd make sure that your environment uses the correct Python. Plt.plot(rabbitnums, 'b-', label="rabbits",) Interval=500, repeat=False, save_count=10, blit=True) Im_ani = animation.FuncAnimation(fig, animate, init_func=anim_init, frames=steps, If hitting the bounds of the grid, "bounde back", step to the opposite direction insetad.ĭirection '.format(rabbitnums, foxnums)) Each step consumes 1 energy if no energy left, die. If self.species = RABBIT and other.species = FOX:Įlif self.species = FOX and other.species = RABBIT: If they're from the same species, ignore each other. Tracks the animal's position, energy, species (rabbit/fox) and state (live/dead).ĭef _init_(self, x0, y0, init_energy, species): # chance of a new fox/rabbit being spawned at a gridpoint on a step
even adding a new species is fairly simple.įrom matplotlib import animation as animationįrom progressbar import progressbar as prbar # (use pip/conda install progressbar2, or rewrite line 116.)
Rabbit and a (newborn/not) fox occupies the same point, the fox eatsĪnd, of course, there's a lot of possibility to play around: change parameters like energy or spawning probability, initial values, rules of interaction (recharge energy when feeding?) or spawning (spawn only if two of the same species on the same place? after feeding).
If an animal runs out of energy, it dies. Each animal starts with a given amount of energy, and each timestep they lose 1 energy.If a rabbit and a fox steps on the same spot, the fox eats the rabbit, so the rabbit dies.In each step, each of them moves 1 step to a neighbour grid (no diagonal movements).Initially an exact number of them is spawned on random distinct locations.There are two species competing on a rectangular grid: rabbits and foxes.
I'd like to hear your opinion about this: how could the concept and the code structure be improved, and how good is my implementation? Rules in the model: I made a random-walk predator-prey simulation that focuses on individual animals instead of the (maybe) more common array-based approach.