发生500错误 - 在PHP中执行长循环

时间:2015-03-18 17:18:39

标签: php html sql

发生500错误 - 使用PHP执行长循环。

我目前正在制定一个调度系统来安排医院病房的护士,我正在使用遗传算法来实现这一点。

所以我随机分配每个护士到班次。 然后弄清楚它们适合这种转变。 然后我杀掉任何不符合我的健康水平的分配。

然后我随机分配一个新的时间表。 评估健身状况 杀掉任何不符合我健康水平的分配。

合并两个时间表,保持适合度分配

我循环生成随机时间表,访问其适应性并合并时间表

这可以正常循环30到100次

一旦我超过100分,它有时会失败 - 发生了500错误 当完成脚本需要2:30分钟时,总会发生这种情况

所以我在某个时候做出了假设,我的服务器需要花费太长时间?

我添加了<?php set_time_limit(3600);

这是我文件的顶部,不在构造函数或类中。它在正确的地方吗?
它还会在2分半钟后超时,

这是我的代码,循环是200循环 仍然需要折射我的代码所以不要过于评判

<?php
set_time_limit(3600);

 * Description of scheduler
 *
 * @author Dela
 */

include ("randomTtAllocation.php");
include ("fittness.php");
class scheduler {

private $randomTimetable;
private $timetable;
private $weight;
private $newTimetable ;
private $newWeight;

    function __construct($labs,$students) {

        echo "helloworld";

        // create random timetable and print it 
         $r = new randomTtAllocation();
         $this->randomTimetable = $r->__randomAllocation($labs, $students); 
         //echo "<br>" . " ............initial Time Table............." . "<br>" ;

         echo "<br>";
          echo "<br>";
           echo "<br>";


         $this->__printTt($this->randomTimetable, $this->randomTimetable);

         // work out fittness for the timetable, return fit results and print 
         $fit = new fittness( $this->randomTimetable, $labs, $students , $r->__getNumOfSessions());



         $this->newWeight = $fit->__getnewWeight();
         $this->newTimetable = $fit->__getnewTimetable();;


         //echo "<br>" . " ............newTimetable.........newWeight...." . "<br>" ;
         //$this->__printTt($this->newTimetable, $this->newWeight );

         // sort 
         $this->__sortTtByWeight();
         $this->timetable = $this->newTimetable;
         $this->weight =  $this->newWeight;


      for ($i = 0; $i < 200; $i++) {
         // create second time table 
         $this->randomTimetable = $r->__randomAllocation($labs, $students); 

        // echo "<br>" . " ............initial Time Table............." . "<br>" ;
        // $this->__printTt($this->randomTimetable, $this->randomTimetable);

         // work out fittness for the timetable, return fit results and print 
         $fit = new fittness( $this->randomTimetable, $labs, $students , $r->__getNumOfSessions());

         $this->newWeight = $fit->__getnewWeight();
         $this->newTimetable = $fit->__getnewTimetable();
         //echo "<br>" . " ............tempTimetable.........tempWeight...." . "<br>" ;
         //$this->__printTt($this->newTimetable, $this->timetable );
         $this->__sortTtByWeight();

         // merge timetables 
         echo "<br>" . " ......old...........new." . "<br>" ;
         $this->__printTt($this->timetable,$this->newTimetable );
         $this->__mergeTimetables($this->newTimetable, $this->newWeight );      

         //echo "<br>" . " ........... mergedTimetable.........newWeight...." . "<br>" ;
        //$this->__printTt($this->timetable,$this->weight );



         // fittness of new timetable
         $fit = new fittness( $this->timetable, $labs, $students , $r->__getNumOfSessions());
          echo "<br>" . " ......merged.......kulled" . "<br>" ;
         $this->__printTt($this->timetable,$fit->__getnewTimetable() );

         $this->weight = $fit->__getnewWeight();
         $this->timetable = $fit->__getnewTimetable();

           $c[$i] = $this->__countSlotsAllocated();

         echo "<br> ". $i;
     }

  print_r($c);

    }
     // sorts the re allocated time table by weight
     function __sortTtByWeight() {
                // for each slot
            foreach($this->newTimetable as $l => $i_value) {
            $size = 0;
            // see how many sessions they are taking
            while ($this->newTimetable[$l][$size] != "null") {
                if ($this->newTimetable[$l][$size] == "0") {
                    break;
                }
                $size++;
            }
            for ($i = 1; $i < $size; $i++) {
                $key = $this->newWeight[$l][$i];
                $key1 = $this->newTimetable[$l][$i];

                $k = $i - 1;
                while ($k >= 0 && $this->newWeight[$l][$k] < $key) {
                    $this->newWeight[$l][$k + 1] = $this->newWeight[$l][$k];
                    $this->newTimetable[$l][$k + 1] = $this->newTimetable[$l][$k];
                    $k--;
                }
                $this->newTimetable[$l][$k + 1] = $key1;
                $this->newWeight[$l][$k + 1] = $key;
            }
        }    
     }  
       function __countSlotsAllocated() {
           $count = 0;
           foreach($this->timetable as $i => $x_value) {
               $j = 0;
             while (($this->timetable[$i][$j] != "null") && ($this->timetable[$i][$j] != "0")){
                $count++;
                $j++;
             }
           }
           echo "count " . $count;
           return $count;
       }

     function __mergeTimetables($tempTimeTable,$tempWeight) {

        // for each session
         foreach($this->newTimetable as $i => $i_value) {
            $j = 0;
            $k = 0;
            // while there are still students

            while (($tempTimeTable[$i][$j] != "null") && ($tempTimeTable[$i][$j] != "0")) {
                            //echo $tempTimeTable[$i][$j];
                // System.out.println(timeTable[i][k]);
                // see if there is a free gap
                while ($this->timetable[$i][$k] != "null") {
                    // if student is already taking that session
                    if ($tempTimeTable[$i][$j] == $this->timetable[$i][$k]) {
                        break;
                    }
                    if ($this->timetable[$i][$k] == "0") {
                        $this->timetable[$i][$k] = $tempTimeTable[$i][$j];
                        $this->weight[$i][$k] = $tempWeight[$i][$j];
                        break;
                    }
                    $k++;
                }
                if ($this->timetable[$i][$k] == "null") {
                    if ($tempWeight[$i][$j] < $this->weight[$i][0]) {
                        $this->timetable[$i][0] = $tempTimeTable[$i][$j];
                        $this->weight[$i][0] = $tempWeight[$i][$j];

                    }
                }

                $j++;
            }
        }
     }
      function __returnTimetable() {
          return $this->timetable;
      }


      function __printTt($timetable, $weight) {

           foreach($timetable as $x => $x_value) {
             for ($i = 0; $i < 5; $i++) {
                echo $timetable[$x][$i] . " ";
             }
             echo " . . . . .";
             for ($i = 0; $i < 5; $i++) {
                echo $weight[$x][$i] . " ";
             }

             echo "<br>";
             }
         }
}

1 个答案:

答案 0 :(得分:0)

通常你不应该在http请求中完成所有这些工作。相反,您应该启动后台任务并让网页显示作业的进度。

php配置有一个时间限制,你可以调整,但用户不希望这么长时间。