Topic: PHP Help
I'm in MSc IT and I'm doing project on timetable scheduling using genetic algorithm as my final year project. I have developed the code for it but the problem is timetable is not getting displayed. Please help. Urgent
this is the action.php file in which genetic algorithm is there which should generate the timetable but this code only generates days and subjects in the timetable is not generated.I have also attached the html file first run the tableform.htm and give inputs as no of hours of lecture:6,max lecture per week:8,max lec per day:3 and num of generation:2
<?php
//Declaration
$TotalRooms=2;
$Generation=0;
//$lec = array();
//$Rooms = array();
list() = $Rooms ;
$LengthOfPool=$TotalRooms*4;
$DaysInWeek=6;
$count=0;
$lec1=array();
list()=$lec1;
$get_noofhoursperday=$_POST['noofhoursperday'];
$get_maxlectperweek=$_POST['maxlectperweek'];
$get_maxlectperday=$_POST['maxlectperday'];
$get_genno=$_POST['genno'];
echo "NO Of Hours Per Day : ".$get_noofhoursperday."<br>";
echo "Max Lec Per Week : ".$get_maxlectperweek."<br>";
echo "Max Lec PerDay : ".$get_maxlectperday."<br>";
echo " Number Of Gen : ".$get_genno."<br>";
//$p=preg_match('','');
//$p=re.compile(r'\n');
//$p=re.split(r'\n','');
//$p= eval('r',\n');
//file opening
$subject = "subjects.txt";
$f = fopen($subject, 'r');
while (!feof($f))
{
//echo "asdsd<br>";
//$sub=$p.preg_replace( '','','');
//$sub=$p.preg_replace('','','');
//$sub=p.sub('',a);
$sub = fgets($f);
$lec1[$count]=$sub;
//$TotalLectures=count($lec1);
$count=$count+1;
//exit();
}
/* echo "<pre>";
pr3int_r($lec1);
echo "</pre>"; */
Prints(1,2);
function Prints ($Room1,$Room2)
{
$R=array();
array_push($R,$Room1);
//$R.($Room1);
array_push($R,$Room2);
// $R.($Room2);
foreach( $R as $eachroom )
{
echo (" ");
echo (" ");
$i= 9;
echo " ";
echo "<br>";
echo ("Div No : ".$eachroom)."<br>";
echo ("---------------------------------------------------------------------------------------------------------------------------------------");
echo ("<br />");
echo (" Time '\t\t\t\t\t\t\t\t\t' Mon '\t\t\t\t\t' Tue '\t\t\t\t\t' Wed '\t\t\t\t\t\t' Thu '\t\t\t\t\t' Fri '\t\t\t\t\t' Sat '\t\t\t\t\t' ");
echo ("<br />");
echo ("--------------------------------------------------------------------------------------------------------------------------------------");
for($h=1;$h<=$get_noofhoursperday;$h++)
{
if ($i==12)
{
echo ("Lunch Break");
echo ("<br />");
echo(" ");
$i=1;
//echo ($i,$i+1);
echo "$i $i+1 ";
}
$i=$i+1;
for($d=1; $d<=$DaysInWeek;$d++)
{
if ($eachroom[$d][$h]==0)
echo(" ");
for($l=0;$l<=$lec1;$l++)
{
if ($l[1]==$eachroom[$d][$h])
echo ($l[0]);
}
}
}
}
}
function CreatePool($Rooms,$LengthOfPool)
{
for ($no=1;$no<=$LengthOfPool;$no++)
{
// Original $room=[[0]* $get_noofhoursperday for($i=1; $i<=$DaysInWeek;$i++)];
for($i=1; $i<=$DaysInWeek;$i++)
$room[]= ($get_noofhoursperday*$i);
$a=array();
foreach($a as $b)
$room=$get_noofhoursperday*$b;
$Rooms.append($room);
foreach($room as $Rooms)
{
for($d=1;$d<=$DaysInweek;$d++)
{
for($h=1;$h<= $get_noofhoursperday;$h++)
$room[$d][$h]=rand(1,$TotalLectures);
}
}
}
return $Rooms;
}
function BreedNew($Rooms)
{
// $room=[[0]*$HoursInDay for( $i=1;$i<=$DaysInWeek;$i++)];
for( $i=1;$i<=$DaysInWeek;$i++)
$room[]= ($HoursInDay*$i);
for($d=1;$d<=$DaysInWeek;$d++)
{
for( $h=1; $h<= $get_noofhoursperday;$h++)
{
$room[$d][$h]=rand(1,$TotalLectures);
$Rooms.append($room);
}
}
}
function FitnessOfRoom($Rooms)
{
global $Generation;
$Generation=$Generation+1;
echo( "Generation no = " +strval($Generation));
foreach($room as $Rooms)
{
$RoomFit=100;
for($d=1;$d<=$DaysInWeek;$d++)
{
for($h=1;$h<=$get_noofhoursperday;$h++)
{
if ($room[$d].count($room[$d][$h])>$get_maxlectperday)
{
$RoomFit=$RoomFit-5;
break;
}
if($RoomFit!=100)
{
$Rooms.remove($room);
BreedNew($Rooms);
}
}
}
}
return $Rooms;
}
function CrossOver($Roooms)
{
global $Generation;
$Generation=$Generation+1;
//echo ("Generation no = " +str($Generation));
echo ("Generation no = " +strval($Generation));
$CRate=(int)(count($Rooms)/2);
// in php count is used $CRate=(int)(count($Rooms)/2);
$Cpoint=(int)($DaysInWeek/2 );
$MRate=10;
for($i=0;$i<=$CRate;$i++)
{
$a=rand(0,count($Rooms)-1);
$b=rand(0,count($Rooms)-1);
while( $a==$b)
{
$b=rand(0,count($Rooms)-1);
$father=$Rooms[$a];
$mother=$Rooms[$b];
$child=array();
$Rooms.remove($father);
$Rooms.remove($mother);
$child.extend($father[$Cpoint]);
$child.extend($mother[$Cpoint]);
$Rooms.append($child);
while(count($Rooms<$LengthOfPool))
{
BreedNew($Rooms);
if(rand(1,200)>$MRate)
Mutate();
}
}
}
}
function Mutate()
{
global $Rooms;
$randroom=rand(0,count($Rooms)-1);
$randday=rand(0,$DaysInWeek-1);
$randhour=rand(0, $get_noofhoursperday-1);
$Rooms[$randroom][$randday][$randhour]=rand(1,$TotalLectures);
}
function LecturesPerDayAndWeek($Room1,$Room2)
{
$R=array();
$R.append($Room1);
$R.append($Room2);
for($i=1;$i<=count($R);$i++)
{
for($val=1;$val<=$TotalLectures+1;$i++)
{
$WeekOccured=0;
for($d=1;$d<=$DaysInWeek;$d++)
{
$occurred=0;
for($h=1; $h<=$HoursInDay;$h++)
{
if ($Rooms[$i][$d][$h]==$val)
{
$occurred=$occurred+1;
$WeekOccured=$WeekOccured+1;
if( $occurred >$get_maxlectperday)
return true;
}
if ($WeekOccured >$get_maxlectperweek)
return true;
}
}
}
}
return false;
}
function ClashOfRooms($Rooms)
{
for($i=1; $i<=count($Rooms)-1;$i++)
{
$p=$i+1;
while ($p!=count($Rooms))
{
$clash=false;
for($d=1;$d<=$DaysInWeek;$i++)
{
for($h=1;$h<=$HoursInDay;$h++)
{
if ($Rooms[$i][$d][$h]==$Rooms[$p][$d][$h])
$clash=true;
break;
if ($clash==true)
break;
if ($clash==false)
{
if (LecturesPerDayAndWeek($Rooms[$i][$p])==0)
echo $Rooms[$i],$Rooms[$p];
return false;
}
}
$p=$p+1;
CrossOver($Rooms);
}
}
}
return true;
}
function Fitness($Rooms)
{
$Rooms=FitnessOfRoom($Rooms);
if(ClashOfRooms($Rooms)==0)
return false;
else
return true;
$Rooms=CreatePool($Rooms,$LengthOfPool);
$RET=true;
while($RET==true)
{
if(Fitness($Rooms)==0)
$RET=false;
echo(" ");
echo(" ");
}
}
?>
Last edited by jyotisi (June 10, 2011 01:46)