{"id":38,"date":"2008-12-11T01:20:19","date_gmt":"2008-12-11T09:20:19","guid":{"rendered":"http:\/\/sethjust.wordpress.com\/?p=38"},"modified":"2010-11-11T15:23:45","modified_gmt":"2010-11-11T23:23:45","slug":"genetic-algorithms-in-perl","status":"publish","type":"post","link":"https:\/\/sethjust.com\/2008\/12\/11\/genetic-algorithms-in-perl\/","title":{"rendered":"Genetic Algorithms in Perl"},"content":{"rendered":"
Inspired by recent<\/a> genetic<\/a> algorithms floating around, I decided to try my hand at implementing one in perl. I’d thought for a long time that it would be quite difficult, but really it’s quite easy. My biggest hangup was dealing with data structures, but once I did that, it turns out that all you really need is a few functions:<\/p>\n I ended up implementing a very simple algorithm, but it’s fairly fast and very generic \u00e2\u20ac\u201c it can be easily adapted to just about any task. Sadly, I have no fascinating application just yet, but if I stumble across one, I’ll be sure to post about it.<\/p>\n After the jump, I’ll put up some of the code I used and a link to the script, all for your viewing pleasure. The heart of the algorithm is the mutate and breed functions:<\/p>\n Between the comments and the code, it should be pretty clear what's going on in here, although some of the data structures are kinda hard to get from this section. As I explain in the code<\/a>:<\/p>\n A randomized population is created. An array holds references to the specified number of \"individuals\". Each individual is an array of alleles (scalar values) that are chosen by evaluating a specified string. These are currently positive real numbers.<\/p><\/blockquote>\n These references (or references to the population array) are passed as arguments to the functions each iteration.<\/p>\n\n
\n<\/p>\nsub mutate{ # Mutates each allele of an individual by a random, weighted amount,\r\n # controlled by certain parameters. It re-initializes if the allele\r\n # is zero, and takes abs()\r\n my $ind = shift;\r\n\r\n for (@$ind) {\r\n $_ = eval($init_string) unless ($_);\r\n $_ += (rand(2) - 1)*($mut_weight*($_)+$mut_offset);\r\n $_ = -$_ unless ($_>0);\r\n }\r\n\r\n return 0;\r\n}\r\n\r\nsub breed{ # Breeds each individual with the top 20 percent of the population randomly\r\n # by averaging each allele of the individual with the corresponding allele\r\n # of a random individual in the top quintile. Note that this means that\r\n # each individual breeds with, at most (and ideally), as many individuals\r\n # as it has alleles, which breaks down the parent \/ child model slightly.\r\n my $indvs = shift;\r\n my @list;\r\n ($minimize) || (@list = sort {fitness($b) fitness($a)} @$indvs); # Sort asc. vs. desc.\r\n ($minimize) && (@list = sort {fitness($a) fitness($b)} @$indvs);\r\n\r\n @list = @list[1..(int(scalar(@list)\/5)+1)];\r\n\r\n for (@$indvs) { # Iterate through individuals\r\n my $ind_ref = $_;\r\n my $i = 0;\r\n for (@$ind_ref) { # Iterate through alleles\r\n $_ = ((@list[int(rand(length(@list)-1))])->[$i] + $_)\/2; # Average given allele with\r\n # a random, fit,\r\n # corresponding allele\r\n $_ = (rand(1)<$reset_prob?eval($init_string):$_); # Re-init the allele some\r\n # percent of the time\r\n $i++;\r\n }\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n