PHP, execution too long

software development

#1

hi! i was wondering if i can change the max execution time somehow, it times up after 30 secs, or if im making an obvious gross error in my code! (first time tyring php) heres the code thanks!!!

<?php class aTools{ function hapPer($i,$ns,$nl){ $r=($i*100.0)/($ns*$nl);return (int)$r; } function qa($i1){ $g=array();$g=array_pad($g,$i1,0);return $g; } function qa2($i1,$i2){ $g=array();$g=array_pad($g,$i1,$this->qa($i2)); } function contains($i1,$i2,$i3){ for($i=0;$iqa($b); for($i=0;$i<$b;$i++)$g[$i]=$i1[$i];return $g; } function ca2($i1){ $b=count($i1);$b2=count($i1[0]);$g=array();$g=$this->qa2($b,$b2); for($i=0;$i<$b;$i++)$g[$i]=$this->ca1($i1[$i]);return $g; } function randomSwitch($i1,$i2){ $i=mt_rand(0,(count($i1)-1)); while($i==$i2)$i=mt_rand(0,(count($i1)-1)); return swap($i1,$i2,$i); } function swap($i1,$i1,$i2){ $tmp=$i1[$i2];$ret=$this->qa(count($i1)); for($i=0;$iqa(2,count($i1));$k1=0;$k2=0; $crossover=mt_rand(0,(count($i1)-1)); for($i=0;$i<count($i1)&&$i<=$crossover;$i++){ $ret[0][$i]=$i1[$i];$ret[1][$i]=$i2[$i]; } if($crossover!=count($i1)-1)for($i=$crossover+1;$i<count($i1);$i++){ for($j=$k1;$jcontains($ret[0],$crossover,$i2[$j])){ $ret[0][$i]=$i2[$j];$j=9000;$k1++; } else $k1++; } for($j=$k2;$jcontains($ret[1],$crossover,$i1[$j])){ $ret[1][i]=$i1[$j];$j=9000;$k2++; } else $k2++; } } return $ret; } function aContains($i1,$i2,$i3){ for($i=0;$i<count($i1)&&$i<=$i2;$i++)if($i1[$i]==$i3)return true; return false; } function getHappiness($i1,$i2){ $v=0;for($i=0;$iqa(count($i1)); for($i=0;$i<count($i1);$i++)$ret[$i]=$dc[$i1[$i]]; return $ret; } function f01(){ return rand(0,1000)/1000; } } $cycles=50;$seeds=1;$popsize=20;$crossover=50;$mutation=4; //settings $t=new aTools(); echo 'PHP Genetic Test'; echo 'Testing gentic algorithm, prefrences hard coded.

'; $nos=6;$nol=3;$nsil=array(2,2,2); $sp=array(array(3,2,1,1,2,3),array(2,3,2,2,3,1),array(1,1,3,3,1,2)); echo 'Student Prefrences'; echo ''; for($i=0;$i<$nos;$i++){ echo ''; } echo ''; for($i=0;$i<$nol;$i++){ echo ''; for($j=0;$j<$nos;$j++){ echo''; } echo ''; } echo '
Student
'.($i+1).'
Location
'.($i+1).''.$sp[$i][$j].'
'; for($i=0;$i<$nol;$i++)for($j=0;$j<$nos;$j++){ $sp[$i][$j]=$nol-$sp[$i][$j]; } $pop=$t->qa($nol); for($i=0;$i<$nol;$i++)for($j=0;$j<$nos;$j++) $pop[$i]+=$sp[$i][$j]; echo '

Location Popularity'; echo ''; for($i=0;$i<$nol;$i++)echo ''; echo ''; for($i=0;$i<$nol;$i++)echo ''; echo '
Location
'.($i+1).'
'.$t->hapPer($pop[$i],$nos,$nol).'%
'; $numberOfSpots=0;for($i=0;$iqa($nos);$bst=$t->qa($nos);$ch1=$t->qa($nos);$ch2=$t->qa($nos); $dc=$t->qa($nos);$tsil=$t->qa($nol);$tsil=$t->ca1(nosil); for($i=0;$i<$nos;$i++){ $dun=false;$k=0; while(($k<$nol)&&!$dun){$k++;if($tsil[$k]!=0){ $tsil[$k]--;$dun=true; }} $dc[i]=$k-1; } $population=$t->qa2($popsize,$nos);$fnd=false;$ans=0; for($i=0;$iaContains($population[$i],($j-1),$ans))$fnd=true; } $population[$i][$j]=$ans;$fnd=false; } $fit=$t->qa($popsize);$bestHap=0;$np1=0;$np2=0;$newPop=$t->qa2($popsize,$nos); $ch1=$t->qa($nos);$ch2=$t->qa($nos); for($i=0;$i<$popsize;$i++){ $dk=$t->decode($population[$i],$dc); $fitness[$i]=$t->getHappiness($dk,$sp); if($fitness[$i]>$bestHap){ for($k=0;$k<$nos;$k++)$best[$k]=$dc[$population[$i][$k]]; $bestHap=$fitness[$i]; } } if($bestHap==$nos*($nol-1))$cycles=-1; for($bbb=0;$bbb<$seeds;$bbb++)for($i=0;$i<$cycles;$i++){ $newPop=$t->qa2($popsize,$nos); for($k=0;$k<$popsize/2;$k++){ while(!$fnd){ $np1=rand(0,$popsize); $dk1=$t->decode($population[$np1],$dc); if($t->f01()<($t->getHappiness($dk1,$sp) /($nos*($nol-1))))$fnd=true; } $fnd=false; while(!$fnd){ $np2=rand(0,$popsize); $dk2=$t->decode($population[$np2],$dc); if($t->f01()<($t->getHappiness($dk2,$sp) /($nos*($nol-1))))$fnd=true; } $fnd=false; if($t->f01()<($cross/100)){ $cr=$t->cross($population[$np1],$population[$np2]); $ch2=$cr[0];$ch1=$cr[1]; } else{ $ch1=$population[$np2];$ch2=$population[$np1]; } for($j=0;$j<$nos;$j++)if($t->r01()<($mutation/100)) $ch1=$t->randomSwitch($ch1,$j); for($j=0;$j<$nos;$j++)if($t->r01()<($mutation/100)) $ch2=$t->randomSwitch($ch2,$j); $newPop[2*$k]=$ch1;$newPop[2*$k+1]=$ch2; } $population=$newPop; for($g=0;$g<$popsize;$g++){ $dk=$t->decode($population[$g],$dc); $fitness[$g]=$t->getHappiness($dk,$sp); if($fitness[$g]>$bestHap){ for($k=0;$k<$nos;$k++)$best[$k]=$dc[$population[$g][$k]]; $bestHap=$fitness[$g]; } } if($bestHap==$nos*($nol-1))$cycles=-1; } echo 'Best Solution Found
Happiness Percent'.$t->hapPer($bestHap,$nos,$nol); echo ''; for($i=0;$i<$nos;$i++) echo ''.$best[$i].''; echo'
Student
'.($i+1).' '.(($sp[$best[$i]][$i]*100.0)/$nl).'%
'; ?>

#2

0.o

You don’t by chance have a Perl background?


yerba# rm -rf /etc
yerba#


#3

java actually, which may be surprising based on the way i code! what is really odd is the same thing in java finished really quickly, probably based on the fact that it was compiled? but i cant use java with dreamhost so thats why i translated it


#4

Java? Wow, looks nothing like that. 0.o

Anyway, um, as far as the problem. Can’t really say. Your code is not very readable. lol

You can, however, use Java on the DH machines. They just don’t have JSP capabilities. If javac isn’t already on the server, you can easily install it yourself within your home directory. They do allow that (I’ve done it myself).


yerba# rm -rf /etc
yerba#