Posted By

kurokikaze on 01/28/09


Tagged

Algorithms DNA genetic


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
adkatrit


DNA framework


 / 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

  1. /* ===
  2.  
  3. This code is licensed under Creative Commons Attribution-ShareAlike, if anyone is interested.
  4.  
  5. */
  6.  
  7. class DNA {
  8. private $sequence = array();
  9. private $symbols = array();
  10.  
  11. public function __construct($length = 10, $symbols=array('G','T','C', 'A')) {
  12. if (!empty($symbols) && IntVal($length)>0) {
  13. for($i=0;$i<=$length;$i++){
  14. $this->sequence[] = $symbols[0];
  15. }
  16. $this->symbols = $symbols;
  17. $this->length = count($this->sequence);
  18. }
  19. }
  20.  
  21. public function inscribe($seq, $length) {
  22. if(strlen($seq) == 1 && isset($length) && in_array($seq, $this->symbols)) {
  23.  
  24. // Clear the sequence
  25. $this->sequence = array();
  26.  
  27. for($i=0;$i<=$length;$i++){
  28. $this->sequence[] = $seq;
  29. }
  30. return true;
  31. }
  32.  
  33. if(is_string($seq) || is_array($seq)) {
  34.  
  35. // Clear the sequence
  36. $this->sequence = array();
  37.  
  38. if (is_string($seq)) {
  39. // Danger, memory leak here
  40. $seq = preg_split('//', $seq, -1, PREG_SPLIT_NO_EMPTY);
  41. }
  42.  
  43. foreach($seq as $char) {
  44.  
  45. // Checking allowed symbols
  46. if (in_array($char, $this->symbols)) {
  47. $this->sequence[] = $char;
  48. }
  49.  
  50. }
  51.  
  52. return true;
  53. }
  54. }
  55.  
  56. /**
  57.   * @desc Mutates the sequence. Random symbols get replaced.
  58.   *
  59.   * @param $count Number of replaced symbols
  60.   *
  61.   * @param $probability Probability of replacement
  62.   */
  63. public function mutate($count = 1, $probability = 100) {
  64. for($i=0;$i<=IntVal($count);$i++){
  65. $prob = rand(0,100);
  66. $random = rand(0, count($this->sequence) - 1);
  67. $target = rand(0, count($this->symbols) - 1);
  68.  
  69. // We cannot mutate 'A' to 'A'
  70. while($this->sequence[$random] == $this->symbols[$target]) {
  71. $target = rand(0, count($this->symbols) - 1);
  72. }
  73.  
  74. if ($prob <= $probability)
  75. $this->sequence[$random] = $this->symbols[$target];
  76. }
  77.  
  78. }
  79.  
  80.  
  81. public function get_sequence() {
  82. return $this->sequence;
  83. }
  84.  
  85. public function append($char) {
  86. if(in_array($char, $this->symbols)) {
  87. $this->sequence[] = $char;
  88. } else {
  89. echo $char . ' not in set: ' . implode($this->symbols) . '<br/>';
  90. }
  91. }
  92.  
  93. public function clear() {
  94. $this->sequence = array();
  95. }
  96.  
  97. function crossover($partner) {
  98. // Sequences must be of equal length
  99. if (count($this->sequence)) {
  100.  
  101. //$partner = preg_split('//', $partner, -1, PREG_SPLIT_NO_EMPTY);
  102. $partner_genes = $partner->get_sequence();
  103. $child = new DNA(count($this->sequence), $this->symbols);
  104. $child->clear();
  105.  
  106. $cut_place = rand(0, count($this->sequence) - 1);
  107.  
  108. for($i=0;$i<$cut_place;$i++){
  109. $child->append($this->sequence[$i]);
  110. }
  111.  
  112. for($i=$cut_place;$i<=(count($this->sequence) - 1);$i++){
  113. $child->append($partner_genes[$i]);
  114. }
  115.  
  116. return $child;
  117. }
  118. }
  119.  
  120. public function fitness() {
  121. $fit = 0;
  122. foreach($this->sequence AS $char) {
  123. if ($char == $this->symbols[0]) $fit++;
  124. }
  125. return $fit;
  126. }
  127.  
  128. function __toString() {
  129. return implode($this->sequence);
  130. }
  131.  
  132.  
  133.  
  134. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: kurokikaze on January 28, 2009

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

You need to login to post a comment.