Mychael has asked for the wisdom of the Perl Monks concerning the following question:

I need to create something that will simulate evolution... sort of. I was wondering if this was something I could use Perl for?

The general idea would be to have a population of different kinds of "creatures" with different, variable "genes." Each kind of creature would be both preditor and prey to different types, based on their genes. They would also have to reproduce with each other, and possibly have mutations occur in the genes. (At which point they would possibly become preditor/prey to a different creature.) Like in real-world evolution, the mutations couldn't be completely random, and hopefully would help the creatures to survive -- but other creatures will also be evolving.

Anyway, I'm sure this sort of thing will be rather difficult, but before I start learning Perl, I just wanted to know if something like this would be possible with Perl. And, if so, where a good starting point would be in trying to learn how to accomplish this.


Thanks.

Replies are listed 'Best First'.
Re: Using Perl to simulate evolution.
by blokhead (Monsignor) on Mar 15, 2006 at 22:42 UTC
Re: Using Perl to simulate evolution.
by wazoox (Prior) on Mar 15, 2006 at 21:51 UTC
Re: Using Perl to simulate evolution.
by friedo (Prior) on Mar 15, 2006 at 21:28 UTC
    It's certainly something Perl can do, and simulations of this sort often lend themselves to object-oriented programming fairly well. But if you don't already know Perl, learning Perl's OOP system can be somewhat daunting. I recommend getting a good book like merlyn's Learning Perl and doing the exercises therein. (And perhaps starting with a somewhat less ambitious project to get your feet wet.)

    Once you're comfortable with basic procedural perl, check out the manpages for perlboot and perltoot and perlobj. And also see TheDamian's excellent book Object Oriented Perl.

      Thanks for the fast response!

      I will definitely look into those sources. I picked up O'Reilly's "Learning Perl" and have started flipping through it. I'll check out those that you've suggested too.

      It's just good to know that it is possible.


      Thanks again.
        "Learning Perl Objects, References and Modules" (aka the wooly llama) is also a good next step after you finish "Learning Perl"- I keep re-reading it (it's fairly short but very dense- or maybe that's me. ;) )

        Does EVERYONE get a hankering to do this at some point? If not, what might it be indicative of? Hope you have fun in your quest- what a great reason to learn (perl)!

Re: Using Perl to simulate evolution.
by TedPride (Priest) on Mar 15, 2006 at 22:41 UTC
    You need to distinguish between microevolution, which is variation among existing genetic data, and macroevolution, which is creation of new genetic data. So-called beneficial mutations always inolve a -loss- of genetic data and are not beneficial in a global sense, just for certain specific conditions. Case in point: Sickle-cell anemia and malaria.

    It will probably be most realistic to start with several types of animal, each able to vary a small amount each generation according to size, speed, (and as a result, food intake), ferocity, gestation period, color, etc. within certain maximum limits. Then once this core is worked out, along with the interactions between the various types, you can add mutations that help or hinder according to certain conditions. If, for instance, your animals were susceptible to malaria, those with sickle-cell anemia (even though it weakens the animal overall) would tend to live longer and would eventually become most of the population. Remove the malaria factor (perhaps the frogs reproduce and kill the mosquitos?) and sickle-cell anemia will quickly die out. Or if birds tend to eat white moths and not black, black moths will quickly take over (incidently, the whole pepper moths thing was a hoax - the moths pictured in those photos were planted on the trees).

    Basically, you need to reduce your animals to a list of certain characteristics and corresponding limits. Then define each mutation in terms of these characteristics. A missing leg would cut down on speed and body mass; that missing speed might make the difference between being caught by a faster animal and getting away, or the missing mass might mean the difference between starvation and survival for the animal that eats it.

    Check out Spore, the Maxis computer game:
    http://www.spore.com

    This is going to be a very pretty version of what you're trying to do.

Re: Using Perl to simulate evolution.
by zentara (Cardinal) on Mar 16, 2006 at 00:34 UTC
    I have to throw in a recommendation for the Tk::Canvas or Tk::Zinc, when you get to your "visualization" stage. You can assign tags to your 'blobs" and detect when they overlap. When you detect an "overlap event" you can have callbacks which define how the 2 lifeforms will interact. If you make your "blobs" as objects, you can design them to move randomly or according to some drive they have( like gravitate toward heat, etc). It sounds like a fun project that could grow into a large long term model, with all sorts of object-types..... viruses, single-cells, proemes, and all that good stuff.

    I'm not really a human, but I play one on earth. flash japh
Re: Using Perl to simulate evolution.
by eric256 (Parson) on Mar 16, 2006 at 15:21 UTC

    Two points. Yes it can be done in perl, it can be done in any programming language. I've done it in QBasic and in VisualBasic (screen saver fun).

    Second point, "Like in real-world evolution, the mutations couldn't be completely random, and hopefully would help the creatures to survive -- but other creatures will also be evolving." errr what? Real-world mutations *are* random and arn't driven towards helping creatures survive. Sometimes mutations help and sometimes they give an individual enough of a benefit that they will have higher survival rates and so that mutation will survive. Realy the point is you want your mutations random because you don't know what will make things survive better. In the paste when playing with this problem I found that picking a set of attributes (like 20 or so) and storing them in a string (the dna if you will), then randomly mutating and crossing genes from two individuals can work. You have to be pretty carefull about how you store and retrieve the data or eventualy you get a mess.

    One other thing I experimented with was having characters in the gene that mean different things. I.e. some of them related to the drawing of the creature and some the color, others the speed, turn rate etc. Then to figure out what attributes the creature has your read there dna. I'm not sure thats clear so here is an example. Lets say that a = speed++, b = speed--, c = turn rate ++, d = turn rate --. Then we take a creature whos DNA is "aaabbabcd", they have a speed of (4 - 2) and a turn rate of (0). In this way you can mutate the dna but you are always gauranteed to get the data you need, some creatures like this one will die pretty fast because they can't turn, etc. This could be expanded with more complex multi letter genes in the DNA but you get the idea. Just a few ideas, good luck and share what you come up with!


    ___________
    Eric Hodges
Re: Using Perl to simulate evolution.
by Scott7477 (Chaplain) on Mar 16, 2006 at 04:49 UTC
    Thinking about your idea here led me to consider how one would feed randomness into your model, as of course
    one of the key points of evolutionary theory is "random mutation".

    My understanding is that it is not possible to get true randomness in a program using a seed value and the "rand()"
    type functions that can be found in programming languages.

    Connecting some type of device such as a Geiger counter to a PC and sampling the output of such a device is certainly
    one way to get truly random numbers. I am wondering if anyone else has used something else such as stock price changes, as according to finance theory stock prices change in a "random walk". Putting code together to grab stock prices has obviously
    been done before, but I wonder if anyone has done so for the purpose of extracting random values.

    Update 04-04-06: A readable and informative document titled "Cryptographic Random Numbers" by Carl Ellison can be found here.
      Personally, I think "randomness" is like Anthropomorphism. (giving human attributes to the "gods" to make our understanding easier). In many contexts, randomness is something we have attributed to Nature, in order to use our statistical models. That doesn't mean that Nature works at random, but it appears that way to us, and our puny mathematical models.

      So I think you can get a fairly good random motion model, by taking rand(360), to determine which way something should move. Of course, this gets to the crux of the model. What factors will modify that rand(360) value? Water flow? Chemical concentrations in certain areas, etc,etc. This sort of thing will probably need "fudge factor constants" at first, so that the model will reflect what is actually observed. But as understanding increases, those fudge-factors will become computable variables. Or......

      use God qw('hidden hand'); # ;-)


      I'm not really a human, but I play one on earth. flash japh
        Your points are reasonable. The first one leads to a deeper discussion than I'd want to be in on this site:)...
Re: Using Perl to simulate evolution.
by swampyankee (Parson) on Mar 17, 2006 at 22:47 UTC

    Of course, if you are only interested in experimenting with it, there are several alife programs out there, such as avida (google it).

    emc

    " When in doubt, use brute force." — Ken Thompson