Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: GP problem with tree structure using hash

by kvale (Monsignor)
on Oct 12, 2004 at 06:22 UTC ( #398379=note: print w/replies, xml ) Need Help??

in reply to GP problem with tree structure using hash

For a tree represented as a recursive hash, the crossover and mutation operators are fairly simple.

It looks like you have only binomial operators, so a binary tree. To arrive at a random node, start at the root, $tree, and pick a random key, "left" or right". Then ,e.g., $tree->{left} becomes the new root and pick another random key, etc. Continue walking the hash until you get to a terminal, which you can recognize as a non-reference. Alter that terminal to get a one-point mutation.

For a crossover, each node on the tree is a ref, so you can just exchange references:

$temp = $tree->{right}{left}; $tree->{right}{left} = $tree->{right}{right}; $tree->{right}{right} = temp;
For Boolean expression chromosomes, you will want to make sure that mutations and crossovers generate a syntatctically valid expression (I haven't checked your code for this).

That is the general idea. The implementation is left as an exercise :)


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://398379]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-02-06 12:10 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (34 votes). Check out past polls.