This should be re-represented with another dimension, either density or time. Your Rabbit vs fox population graph is cool, but it quickly becomes muddied when the system converges. List lookup is O(1) so this will be fast, and you won't have to compare all animal coordinates, which seems to be O(n^2). Consider maintaining a nested grid list tracking where all of your animals are. This will get slow as you scale up to larger and larger grid sizes. Scaling # interact if two animals are at the same coordinates You should move the global-scoped code starting with animals = into functions, as well. This can be deleted: if direction = STAY: Though the type for that species would change if you make it an Enum. PEP484 type hints can help, here for instance: def _init_(self, x0: int, y0: int, init_energy: float, species: int): You can then factor out a lot of common code. Rather than these variables # initial number of rabbits and foxesīeing tracked individually, consider making a Species class with two instances, each having the above attributes. 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). in each step, there's a small chance for each point that a newborn rabbit or fox spawns there, with fresh energy. 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).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |