Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Genetic algorithms

by dash2 (Hermit)
on May 23, 2002 at 09:30 UTC ( #168713=perlquestion: print w/ replies, xml ) Need Help??
dash2 has asked for the wisdom of the Perl Monks concerning the following question:

Another insane project. This is just a gleam in my eye. I thought my other program was going to be my last one! Damn.

Iterations of the Prisoner's Dilemma prove interesting things about what a "successful" strategy is. (See Axelrod, The Evolution of Co-operation). I would love to build a genetic algorithm to play the prisoner's dilemma, and see what evolves. (I know it has been done before, but I like DIY.)

It's all easy apart from the genetic algorithm bit. My scribbled notes suggest this - NB I am an amateur and don't have any academic background in the field:

Use 1 for cooperation, 0 for defection. The DNA is a string of elements which operate successively to come up with a final 1 or 0.

Start with 1. (Or 0 - does it matter?)

DNA letter meanings:

Ax - "and" with x
Ox - "or" with x
Sx - save in register, start again with x
"x" can be any of:
D - defect. 0.
C - cooperate. 1.
number - opponent's strategy in game played number rounds previously
number - own strategy in game played number rounds previously. This can be distinguished by e.g. using odd for opponent, even for self; or adding 100 to self. R - retrieve result from register

I know this wastes space and it should all be bit vectors or whatever, but ignore that. My questions:

Could it be simpler? Do I need a "register"?

Should it be more complicated? Will adding complications allow for more powerful solutions?

Related to the "register" issue: does a simple linear program like this allow for different priorities? To put it another way, can you express "(1 and 0) or 1" as well as "1 and (0 or 1)" without the brackets?

How "atomic" should individual DNA elements be? E.g. should an element mean "and" and be coupled with the next element (disadvantage - some mutations might "not work" at all) or should an element mean "and with such and such" (seems less flexible, easier to write though).

A gene pool of 100 players all playing each other means 100*99 games (maths?) in a single generation. How can I make this bearably fast?

I'd be very interested to hear comments from people with a CS background.

dave hj~

NB: I found this page describing Axelrod's work with this. He uses simple brute force rather than a "programming language" approach. The downside is each player can only remember the last 3 games, and this is built in. Clever though.

Comment on Genetic algorithms
Re: Genetic algorithms
by Matts (Deacon) on May 23, 2002 at 10:54 UTC
    I'm no CS expert (I got a 2-2, and only just scraped that ;-) but we use GA's in the evolution of the scores for SpamAssassin. My recommendation for speed is to look into the GAlib at It's a C++ library, but that's what you have to use for speed - and GA's tend to be quite slow.

    Alternatively, also check out MyBeasties - a Perl library for doing GA's. I'm sure going through the source code to that module will tell you everything you need to know.

    Sorry this doesn't answer your question more directly - I'm still waiting for the caffeine to sink in - I might be able to make more sense of it later ;-)

Re: Genetic algorithms
by lhoward (Vicar) on May 23, 2002 at 12:05 UTC
    I did a good bit of work with GA's a few years ago. Choosing the right representation is key. I've seen problems where one representation of the solution lead nowhere, and a different representation lead to good solutions quickly.

    I recomend that you plan on trying several different representations while you're experimenting with your project. The one your proposed above seems as good as any to start out with.

Re: Genetic algorithms (OT!)
by educated_foo (Vicar) on May 23, 2002 at 15:29 UTC
    Probably the cleverest "solution" I've heard of was one that won by stepping outside the game. There was a prisoners' dilemma competition a few years ago -- I can't find a URL -- in which teams would submit programs to compete in a tournament, with each round consisting of many games against the same opponent. Whoever had the best record at the end won. The obvious strategy in the absence of any other information is to always inform. But if you know a bit more about your opponent...

    The winners submitted many solutions that cooperated. Basically, all their programs would start out with a certain sequence of moves identifying themselves either as "winner" or "loser". The "winner" program would, after the signalling period, always inform. The "loser" would, if it detected the "winner" signal, always refuse to confess. Otherwise, it would always inform. Thus their single designated "winner" was able to rack up a huge number of points playing against their other entries, while only losing a small number of points during the initial signalling. Great stuff, IMHO.


Re: Genetic algorithms
by mojotoad (Monsignor) on May 23, 2002 at 17:25 UTC
Re: Genetic algorithms
by nop (Hermit) on May 24, 2002 at 02:41 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://168713]
Approved by virtualsue
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-07-30 21:24 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (241 votes), past polls