Evolutionary computation in Perl | ||
---|---|---|
<<< Previous | Doing Evolutionary Algorithms with Algorithm::Evolutionary | Next >>> |
It is about time we go into more complex stuff, and, since many sub-algorithms are already programmed into Algorithm::Evolutionary, we will use it. The not so-simple genetic algorithm is composed of a main loop that does, in sequence,
selection of the group of individuals that will be the parents of the next generation
application of genetic operators to those elements
insertion of those parents in the population
elimination of a part of the population, and
checking for termination conditions
#!perl use strict; use warnings; use Algorithm::Evolutionary::Experiment; use Algorithm::Evolutionary::Op::Creator; use Algorithm::Evolutionary::Op::Bitflip; use Algorithm::Evolutionary::Op::Crossover; use Algorithm::Evolutionary::Op::RouletteWheel; use Algorithm::Evolutionary::Op::GeneralGeneration; use Algorithm::Evolutionary::Op::DeltaTerm; use Algorithm::Evolutionary::Op::FullAlgorithm; my $numberOfBits = shift || 32; my $popSize = 100; my $fitness = sub { my $indi = shift; my $total = grep( $_ == 1, split(//,$indi->Chrom() )); return $total; }; (1) my $creator = (2) new Algorithm::Evolutionary::Op::Creator( $popSize, 'BitString', { length => $numberOfBits }); my $selector = new Algorithm::Evolutionary::Op::RouletteWheel $popSize; my $mutation = new Algorithm::Evolutionary::Op::Bitflip; my $crossover = new Algorithm::Evolutionary::Op::Crossover; my $replacementRate = 0.4; #Replacement rate my $generation = new Algorithm::Evolutionary::Op::GeneralGeneration( $fitness, $selector, (2) [$mutation, $crossover], $replacementRate ); (3) my $terminator = new Algorithm::Evolutionary::Op::DeltaTerm $numberOfBits, 0; my $algorithm = new Algorithm::Evolutionary::Op::FullAlgorithm $generation, $terminator;(3) my $experiment = new Algorithm::Evolutionary::Experiment $creator, $algorithm;(4) print<<EOC; <?xml version="1.0" standalone="no"?> <experiment> EOC print $experiment->asXML(); (4) $experiment->go(); print $experiment->asXML(), "</experiment>"; |
The good thing about having this XML output is that you can process it very easily, for instance, to pretty-print final population, using this XSLT stylesheet to obtain this web page. The XML document can be used for post-algorithmic analysis, for interchange with other evolutionary algorithms, possibly written in other languages, or even for external data representation for parallel and distributed algorithms. For instance, the output of the algorithm can be converted to a combined HTML/SVG (Scalable Vector Graphics) document, which can be used for presentation straight away. It could also be imagined a "literate evolutionary computation" application that would mix the output of an evolutionary algorithm, with the description of the classes obtained via pod2xml, to create an XSLT stylesheet that would process output and create a document with output along explanation. This is left as an exercise to the reader.
XML is cool. 'Nuff said. |
<<< Previous | Home | Next >>> |
Canonical GA with Algorithm::Evolutionary | Up | Extending Algorithm::Evolutionary |