August 16, 2011

Predator-Prey Simulation

Since I started programming, I have thought it would be interesting to attempt to approximate life within a computer program.  I started this project on my own, but finished it, collected data from it, and wrote a paper on if for my final CS class project.  

What my final program does is create a virtual Petri dish with two types of cells in it: predator cells and prey cells.  As the cells move around in their virtual environment, the predator cells try to chase down the prey cells, and the prey try to escape the predators.  If the predators do not eat for a long time, they die of starvation.  As the cells move in the simulation, the also replicate.  All of the cells, both predator and prey, have two individual attributes: sight radius and movement speed, which are passed on from parent cells to child cells.  However, as the cells replicate, there is a small chance that the child cells "mutate."  In other words, one or both of their attributes has a slightly different value than the parent cell's attributes.  These random mutations allow for natural selection to occur within the simulation, provided it runs for long enough.  The most effective predators will survive the longest, and therefore produce the most offspring, so their desirable attributes proliferate.  The same applies to prey cells that can escape most easily.  As the simulation runs, it outputs the average attributes of all of each type of cell to a text file at regular intervals.  If the simulation is run for a long period of time, these values can be evaluated, and one can see if natural selection occurred or not, on average.



In the simulation's graphics, red squares represent predator cells.  Other colors represent the prey.  The color of they prey is determined by the cells movement speed, in order to help visualize the mutations that occur.  Green prey are the slowest, and magenta prey are the fastest.  Along the bottom of the window, the average speed of the prey (the black dots) and the predators (the red dots)  are graphed.  Vertical black lines indicate the start of a new simulation.  This happens when all of one type of cell become extinct.  Alternatively, the graph can be used to track the populations of the predators and prey.  For faster data collection, the graphics can be disabled, which increases the speed at which the program runs by about ten times.


The python 3 code can be found on Instructables, here:


http://www.instructables.com/files/orig/FVL/IYEL/GREPYCQF/FVLIYELGREPYCQF.py


The graphics can be enabled or disabled by commenting out the line "canvas.update()" in the function "run()."


The program, especially when run with the graphics, is highly CPU dependent, and the speed will decrease significantly as the simulation is populated.  To change the results of the simulation, the cells starting attributes (including rate of reproduction and rate of mutation along with the two others descibed) can be modified, as can the starting populations and the map size.

2 comments:

  1. Hey!
    Very interesting program! But how do I execute this skript?

    I typed in apple's terminal: "python ./FVLIYELGREPYCQF.py" but I got an error:
    "No module named tkinter".

    ReplyDelete
    Replies
    1. go to terminal and type 'pip install tkinter', you don't have the package installed

      Delete