/ Published in: PHP
Test version. Includes mutations and crossover. I know it's lame, you don't have to point it out.
CC-Attribution-ShareAlike
Expand |
Embed | Plain Text
/* === This code is licensed under Creative Commons Attribution-ShareAlike, if anyone is interested. */ class DNA { for($i=0;$i<=$length;$i++){ $this->sequence[] = $symbols[0]; } $this->symbols = $symbols; } } public function inscribe($seq, $length) { // Clear the sequence for($i=0;$i<=$length;$i++){ $this->sequence[] = $seq; } return true; } // Clear the sequence // Danger, memory leak here } foreach($seq as $char) { // Checking allowed symbols $this->sequence[] = $char; } } return true; } } /** * @desc Mutates the sequence. Random symbols get replaced. * * @param $count Number of replaced symbols * * @param $probability Probability of replacement */ public function mutate($count = 1, $probability = 100) { // We cannot mutate 'A' to 'A' while($this->sequence[$random] == $this->symbols[$target]) { } if ($prob <= $probability) $this->sequence[$random] = $this->symbols[$target]; } } public function get_sequence() { return $this->sequence; } public function append($char) { $this->sequence[] = $char; } else { } } public function clear() { } function crossover($partner) { // Sequences must be of equal length //$partner = preg_split('//', $partner, -1, PREG_SPLIT_NO_EMPTY); $partner_genes = $partner->get_sequence(); $child->clear(); for($i=0;$i<$cut_place;$i++){ $child->append($this->sequence[$i]); } $child->append($partner_genes[$i]); } return $child; } } public function fitness() { $fit = 0; foreach($this->sequence AS $char) { if ($char == $this->symbols[0]) $fit++; } return $fit; } function __toString() { } }
Comments
Subscribe to comments
You need to login to post a comment.

$default = new DNA(100, array('A', 'D', 'N', 'E')); $default->inscribe('N', 100); $default->mutate(500, 99);