2) && (substr($s,0,1)!="#")) { $label=explode(" ",$s); if (strtolower($label[0])=="target") { $target_string=substr($s,strlen($label[0])+1); $target=explode(",",$target_string); $CHROMOSOME_LENGTH=count($target); } if (strtolower($label[0])=="weights") { $weight_string=$label[1]; $weights=explode(",",$weight_string); } if (strtolower($label[0])=="mutation") { $MUTATION_CHANCE=$label[1]; } if (strtolower($label[0])=="comment") { $comment_string=substr($s,strlen($label[0])+1); } if (strtolower($label[0])=="verbose") { if (!$verbose) $verbose=$label[1]; } if (strtolower($label[0])=="iterations") { $iterations=$label[1]; } if (strtolower($label[0])=="trials") { $trials=$label[1]; } if (strtolower($label[0])=="actions") { $action_string=$label[1]; $actions=explode(",",$action_string); for ($x=0;$xselect a different file.

"; exit(); } if (!$trials) $trials=1; $NUMBER_OF_SURVIVORS=round($POPULATION_SIZE/2); array_sort_by_column($individu,'fitness'); } // end function read_chromosomes // ------------------------------------------------ function check_fitness($s) { global $target; global $CHROMOSOME_LENGTH; global $actions; global $weights; // $t=0; $genome=explode(",",$s); for ($x=0;$x<$CHROMOSOME_LENGTH;$x++) { $OK=0; if ($actions[$x]=="=") if ($genome[$x]==$target[$x]) $OK=1; if ($actions[$x]=="<") if ($genome[$x]<$target[$x]) $OK=1; if ($actions[$x]==">") if ($genome[$x]>$target[$x]) $OK=1; if ($actions[$x]=="<=") if ($genome[$x]<=$target[$x]) $OK=1; if ($actions[$x]==">=") if ($genome[$x]>=$target[$x]) $OK=1; if ($actions[$x]=="<>") if ($genome[$x]<>$target[$x]) $OK=1; if (substr($actions[$x],0,1)=="%") { $percent=(int)substr($actions[$x],1); $tolerance=($target[$x] / 100) * $percent; if (($target[$x]-$genome[$x]) < $tolerance) $OK=1; if ($debug) { echo $target[$x]." - ".$genome[$x].$end_line; echo "tolerance ".$tolerance.$end_line; echo "percent ".$percent.$end_line; echo "result ".$OK.$end_line; exit(); } } if ($actions[$x]=="x") $OK=1; // and now check the strength of this gen // where the weight is the probability that it adds its outcome to // the final fitness $treshold=intval(100/$weights[$x]); if ($weights[$x]==0) echo "zerodiv :".$x.$end_line; $r=rand(0,100); if ($r>$treshold) if ($OK==1) $OK=0; $t=$t+$OK; } $fitness=$CHROMOSOME_LENGTH-$t; // echo "gen ".$s."; score ".$fitness." ".$CHROMOSOME_LENGTH.$end_line; return $fitness; } //----------------------------------------------- function crossover(&$a,&$b) { // choose random crossover point in individu[chromosome] global $CHROMOSOME_LENGTH; $r1=explode(",",$a); $r2=explode(",",$b); $r=rand(0,$CHROMOSOME_LENGTH); $split_r1_1=array_slice($r1,0,$r); $split_r1_2=array_slice($r1,$r); $split_r2_1=array_slice($r2,0,$r); $split_r2_2=array_slice($r2,$r); $r1=array_merge($split_r1_1,$split_r2_2); $r2=array_merge($split_r2_1,$split_r1_2); $a=implode(",",$r1); $b=implode(",",$r2); } // ------------------------------------------------ function mutation(&$s) { global $CHROMOSOME_LENGTH; global $actions; // echo "voor mutatie ".$s.$end_line; $genes=explode(",",$s); $gene_to_mutate=rand(0,$CHROMOSOME_LENGTH); if ($actions[$gene_to_mutate]=="x") return 0; $new_gene_value=rand(0,9); $genes[$gene_to_mutate]=$new_gene_value; $s=implode(",",$genes); // echo "na mutatie ".$s.$end_line; return 1; } // ------------------------------------------------ function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) { $sort_col = array(); foreach ($arr as $key=> $row) { $sort_col[$key] = $row[$col]; } array_multisort($sort_col, $dir, $arr); } // ------------------------------------------------ $max_genes=100; $end_line="\n"; if ($argv[1]) $filename=$argv[1]; else { $filename=$_GET["filename"]; $verbose=$_GET["verbose"]; $end_line="
"; $apache=1; } read_chromosomes($individu); echo "paai_GA, a demonstration of a simple genetic algorithm. "; echo "Version 0.9.".$end_line; echo "GNU copyright 2013.

"; echo "filename: ".$filename.$end_line; echo "Comment: ".$comment_string."

"; echo "Date: ".date(DATE_RFC822).$end_line; echo "target_string: ".$target_string.$end_line; echo "action_string: ".$action_string.$end_line; echo "Active gens: ".$active_gens.$end_line; echo "weight_string: ".$weight_string.$end_line; echo "POPULATION_SIZE: ". $POPULATION_SIZE.$end_line; echo "CHROMOSOME_LENGTH: ".$CHROMOSOME_LENGTH.$end_line; echo "MUTATION_CHANCE:".$MUTATION_CHANCE.$end_line; echo "NUMBER_OF_SURVIVORS: ".$NUMBER_OF_SURVIVORS.$end_line; echo "iterations: ".$iterations.$end_line; echo "trials: ".$trials.$end_line; // $verbose=1; echo "verbose: ".$verbose.$end_line; // exit(); $missers=0; if ($verbose==1) for ($i=0;$i<$POPULATION_SIZE;$i++) { echo "----------- original population sorted on fitness -----------
\n\n"; echo $i." - ".$individu[$i]['chromosome'] . " FITNESS: " . $individu[$i]['fitness'] ."
\n\n"; } echo "-------------------------------------------------------------
\n\n"; // for every new generation // sorteer op fitness for ($t=1;$t<=$trials;$t++) { echo $end_line."starting trial ".$t.$end_line; flush(); $iteration=0; $no_success=1; $number_mutations=0; while ($no_success) { $iteration++; array_sort_by_column($individu,'fitness'); if ($verbose==1) { // echo "iteration ".$iteration."; top dog ".$individu[0]['chromosome'].". FITNESS ".$individu[1]['fitness']; echo "iteration ".$iteration."; FITNESS ".$individu[1]['fitness']; if ($individu[0]['chromosome']==$individu[1]['chromosome']) echo " first two equal. "; if ($mutated) echo " mutated.".$end_line; echo $end_line; } $mutated=0; // select survivors (even number) for ($x=0;$x<=$NUMBER_OF_SURVIVORS;$x++) { $s1=$individu[$x]['chromosome']; if (rand(0,$MUTATION_CHANCE)==1) { $m=mutation($s1); if ($m==1) { $mutated=1; $number_mutations++; } } $s2=$individu[$x+1]['chromosome']; crossover($s1,$s2); $r1=check_fitness($s1); $r2=check_fitness($s2); // bij succes if (($r1==0) || ($r2==0)) { $succes_sum+=$iteration; echo "Success in iteration ".$iteration." with ".$number_mutations." mutations"; if ($mutated) echo "; after mutation.".$end_line; else echo $end_line; // for ($i=0;$i<10;$i++) echo $i." ".$individu[$i]['chromosome']." ".$individu[$i]['fitness'].$end_line; $no_success=0; break; } $individu[$x+1+$NUMBER_OF_SURVIVORS]['chromosome']=$s1; $individu[$x+2+$NUMBER_OF_SURVIVORS]['chromosome']=$s2; $individu[$x+1+$NUMBER_OF_SURVIVORS]['fitness']=$r1; $individu[$x+2+$NUMBER_OF_SURVIVORS]['fitness']=$r2; // $mutated=0; } if ($iteration==$iterations) { $no_success=0; echo "Geen succes".$end_line; $missers++; $succes_sum+=$iteration; // so the maximum number of iterations are added in case of failure } flush(); } // end of all trials } // write results echo $trials." ".$missers." ".$succes_sum.$end_line; //$average_trials=round($succes_sum/($trials-$missers)); $average_trials=round($succes_sum/$trials); echo $end_line."Final average number of generations in ".$trials." runs: ".$average_trials.$end_line; $bname=basename ( $filename,".csv"); $logfile=$bname.".log"; $f=fopen($logfile,"a"); fwrite($f,$filename." ".$comment_string."; ".date(DATE_RFC822)."; active gens ".$active_gens."; mutation_chance ".$MUTATION_CHANCE."; "); fwrite($f,"average number of generations in ".$trials." runs: ".$average_trials."; failures: ".$missers."\n"); fclose($f); flush(); ?>