Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- $length=10;
- function set_routes($num_elements){
- $elements_length=array();
- $count=$num_elements;
- for ($i=1; $i <$num_elements ; $i++) {
- for ($j=$i+1; $j <$count+1; $j++) {
- $elements_length[$i][$j]=rand(1,42);
- }
- }
- return $elements_length;
- }
- $route=set_routes($length);
- function chromosome($length){
- $chromosome=array();
- for ($i=0; $i <$length ; $i++) {
- $chromosome[]=$i+1;
- }
- for ($i=$length-1; $i >-1 ; $i--) {
- $j=rand(0,$length-1);
- $buf=$chromosome[$i];
- $chromosome[$i]=$chromosome[$j];
- $chromosome[$j]=$buf;
- }
- return $chromosome;
- }
- function chr_len($chr){
- $route=0;
- for ($i=0; $i < count($chr)-1 ; $i++) {
- if ($chr[$i]<$chr[$i+1]){
- $route+=$GLOBALS['route'][$chr[$i]][$chr[$i+1]];
- }else{
- $route+=$GLOBALS['route'][$chr[$i+1]][$chr[$i]];
- }
- }
- return $route;
- }
- function crossingover($chr1,$chr2,$length){
- $count=round($length/2);
- $count=$count+round($count/2);
- $child1=array();
- $child2=array();
- for ($i=0; $i <$count ; $i++) {
- $child1[]=$chr1[$i];
- $child2[]=$chr2[$i];
- }
- for ($i=$count; $i <$length ; $i++) {
- $child1[]=$chr2[$i];
- $child2[]=$chr1[$i];
- }
- if (chr_len($child1)<chr_len($child2)){
- return $child1;
- }else{
- return $child2;
- }
- }
- crossingover(chromosome($length),chromosome($length),$length);
- $population=array();
- for ($i=0; $i <70000 ; $i++) {
- $population[]=chromosome($length);
- }
- do{
- $child=crossingover($population[rand(0,count($population)-1)],$population[rand(0,count($population)-1)],$length);
- }while (count(array_unique($child)<$length)) ;
- print_r($child);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement