Пример #1
0
/**
    We don't care about closed paths. These are the conditions for a failed path:
    1. If the path dead-ends.
    2. If the path attempts to loop back on itself.
    3. If the path exceeds the time of an already completed path.
*/
function algorithm($start, $end)
{
    $debug = true;
    outputDebug("→ algorithm.algorithm()", $debug);
    outputDebug("start = " . $start->toString(), $debug);
    outputDebug("end = " . $end->toString(), $debug);
    $completedPaths = array();
    $starting = array();
    $finishing = array();
    $startPaths = array();
    $endPaths = array();
    $starts = findStartStations($start, $end);
    foreach ($starts as $metaPathArray) {
        foreach ($metaPathArray as $metaPath) {
            if ($metaPath->getStatus() == "complete") {
                $completedPaths = addPath($completedPaths, $metaPath->path);
                continue;
            }
            $currentStation = $metaPath->path->getCurrentStation();
            $startPaths = createPaths2($currentStation, $end, $startPaths);
            $array = $starting[$currentStation->getID()];
            if ($array == null) {
                $array = array();
            }
            $duration = $metaPath->path->duration;
            $array["{$duration}"] = $metaPath->path;
            $starting[$currentStation->getID()] = $array;
        }
    }
    $ends = findStartStations($end, $start);
    foreach ($ends as $metaPathArray) {
        foreach ($metaPathArray as $metaPath) {
            if ($metaPath->getStatus() == "complete") {
                $completedPaths = addPath($completedPaths, $metaPath->path);
                continue;
            }
            $currentStation = $metaPath->path->getCurrentStation();
            $endPaths = createPaths2($currentStation, $start, $endPaths);
            //$starting[$currentStation->getID()] = $metaPath->path;
            $array = $finishing[$currentStation->getID()];
            if ($array == null) {
                $array = array();
            }
            $duration = $metaPath->path->duration;
            $array["{$duration}"] = $metaPath->path;
            $finishing[$currentStation->getID()] = $array;
        }
    }
    // determine start type and, if necessary, create the first segment
    /*if ($start instanceof Station2) {
          outputDebug("*** creating startPaths ***", $debug);
          $startPaths = createPaths($start, $end);
          outputDebug("*** creating startPaths : DONE ***", $debug);
      }
      
      // determine end type and, if necessary, create the end segment
      if ($end instanceof Station2) {
          outputDebug("*** creating endPaths ***", $debug);
          $endPaths = createPaths($end, $start);
          outputDebug("*** creating endPaths : DONE ***", $debug);
      }*/
    $finished = array();
    outputDebug("start search.", $debug);
    $completedPaths = algorithmLoop3($startPaths, $endPaths, $completedPaths);
    outputDebug("total number of paths found = " . count($completedPaths), $debug);
    foreach ($completedPaths as $path) {
        outputDebug("completedPath is " . $path->toString(), $debug);
        $station = $path->getFirstStation();
        $startArray = $starting[$station->getID()];
        ksort($startArray);
        $startPath = array_shift($startArray);
        if ($startPath != null) {
            outputDebug("adding startPath: " . $startPath->toString(), $debug);
            $path = new Path($startPath, $path);
        }
        $station = $path->getCurrentStation();
        $finishArray = $finishing[$station->getID()];
        ksort($finishArray);
        $finishPath = array_shift($finishArray);
        if ($finishPath != null) {
            outputDebug("adding finishPath: " . $finishPath->toString(), $debug);
            $path = mergePaths($path, $finishPath);
        }
        outputDebug("real path is " . $path->toString(), $debug);
        $finished[] = $path;
    }
    return $finished;
    //return algorithmLoop2($starts, $ends);
}
Пример #2
0
/**
    We don't care about closed paths. These are the conditions for a failed path:
    1. If the path dead-ends.
    2. If the path attempts to loop back on itself.
    3. If the path exceeds the time of an already completed path.
*/
function algorithm($start, $end)
{
    $debug = true;
    outputDebug("→ algorithm.algorithm()", $debug);
    outputDebug("start = " . $start->toString(), $debug);
    outputDebug("end = " . $end->toString(), $debug);
    $starts = findStartStations($start, $end);
    $ends = findStartStations($end, $start);
    // determine start type and, if necessary, create the first segment
    /*
     if ($start instanceof Station2) {
        outputDebug("*** creating startPaths ***", $debug);
        $startPaths = createPaths($start, $end);
        outputDebug("*** creating startPaths : DONE ***", $debug);
    }
    
    // determine end type and, if necessary, create the end segment
    if ($end instanceof Station2) {
        outputDebug("*** creating endPaths ***", $debug);
        $endPaths = createPaths($end, $start);
        outputDebug("*** creating endPaths : DONE ***", $debug);
    }
    */
    //return algorithmLoop($startPaths, $endPaths);
    return algorithmLoop2($starts, $ends);
}