示例#1
0
/**
    For each segment, the first marker and the second marker of the same segment will have the same lines. We check the line used. If it does not exist in second segment's second marker, then we know that the first marker is a junction point and needs to have a transfer.
    
    @param $path[]<Segment> the list of path segments
    @param $type<String> the type of connection (s2s,dir)
    
    @deprecated
*/
function consolidatePath2($path, $type)
{
    $cpath = array();
    /*
        What are the conditions that determine a transfer?
        1. If the connection type is different (express -> local)
        2. If the train in the next segment is different and the original one is not available
        3. if the connection type is transfer
    */
    // debugging
    $debug = true;
    $path_array = $path->path;
    $count = count($path_array);
    // this is a direction path and there is only one or two paths.
    // that means it either goes from loc->station->loc or just loc->loc
    if ($type == "dir" && $count <= 2) {
        return "walk";
    }
    //set up the first segment
    $segment = $path_array[0];
    if ($type == "s2s") {
        $i = 1;
    } else {
        $cpath[] = $segment;
        $segment = $path_array[1];
        $i = 2;
    }
    $marker1 = $segment->m1;
    $current_lines = $segment->m1->getOverlapLines($segment->m2);
    $connection = $segment->connection;
    $total_time = $segment->t;
    $segment2 = $path_array[$i + 1];
    // loop through the path, starting at the next segment.
    for (; $i < $count; $i++) {
        // grab the segment
        $segment2 = $path_array[$i];
        if ($debug) {
            echo "@" . $segment2->m1->name . "<br/>";
            echo "lines = ";
            foreach ($current_lines as $line) {
                echo $line->name . " ";
            }
            echo "<br/>";
        }
        if ($connection == "transfer") {
            // we know this segment is a transfer, so they will have to get off the train
            echo "transfer<br/>";
            // end the segment
            $new_segment = new Segment($marker1, $segment2->m1, $current_lines, $total_time, $connection);
            if ($debug) {
                echo "segment = " . $new_segment->toString() . "<br/>";
            }
            $cpath[] = $new_segment;
            // add the transfer segment
            // we need to get the transfer lines
            $lines = $segment2->m1->getOverlapLines($segment2->m2);
            $lines = getCorrectLines($lines, $segment2->connection);
            // create new segment
            $new_segment = new Segment($segment2->m1, $segment2->m2, $lines, $segment2->t, $segment2->connection);
            $cpath[] = $new_segment;
            // increment if we need to
            if ($i < $count) {
                // increment
                $i++;
                $segment = $path_array[$i];
                $marker1 = $segment->m1;
                $current_lines = $segment->m1->getOverlapLines($segment->m2);
                $connection = $segment->connection;
                $total_time = $segment->t;
                if ($i < $count) {
                    // more segments available
                    continue;
                }
                // we end here with this segment
                $new_segment = new Segment($segment->m1, $segment->m2, $current_lines, $total_time, $connection);
                $cpath[] = $new_segment;
                return $cpath;
            }
        } else {
            if ($connection != $segment2->connection) {
                // the connection types are different, but trains can shift from express to local and vice versa.
                if ($debug) {
                    echo "old conn: " . $connection . " | new conn: " . $segment2->connection . "<br/>";
                }
                // do we have the same train available?
                if (sameTrain($current_lines, $segment2)) {
                    // it's the same train, so we just update our lines and continue
                    $current_lines = updateLines($segment, $current_lines);
                    $total_time += $segment2->t;
                    $connection = $segment2->connection;
                    continue;
                }
                // different trains, so we need to transfer
                // end the old segment and add to the path
                $new_segment = new Segment($marker1, $segment2->m1, $current_lines, $total_time, $connection);
                if ($debug) {
                    echo "new_segment = " . $new_segment->toString() . "<br/>";
                }
                $cpath[] = $new_segment;
                // reset our variables for the next segment
                $segment = $segment2;
                $marker1 = $segment2->m1;
                $current_lines = $segment2->m1->getOverlapLines($segment2->m2);
                $connection = $segment2->connection;
                $total_time = $segment2->t;
                continue;
            } else {
                if (!sameTrain($current_lines, $segment2)) {
                    // different trains, so we need to transfer
                    if ($debug) {
                        echo "old lines: ";
                        foreach ($current_lines as $line) {
                            echo $line->name . " ";
                        }
                        echo "<br/>";
                        echo "new line: " . $segment2->lines[0]->name . "<br/>";
                    }
                    // end the old segment and add to the path
                    $new_segment = new Segment($marker1, $segment2->m1, $current_lines, $total_time, $connection);
                    if ($debug) {
                        echo "new_segment = " . $new_segment->toString() . "<br/>";
                    }
                    $cpath[] = $new_segment;
                    // reset our variables for the next segment
                    $segment = $segment2;
                    $marker1 = $segment2->m1;
                    $current_lines = $segment2->m1->getOverlapLines($segment2->m2);
                    $connection = $segment2->connection;
                    $total_time = $segment2->t;
                    continue;
                }
            }
        }
        // otherwise, we keep going and merge the segments
        $current_lines = linesIntersect($current_lines, $segment2->m1->getOverlapLines($segment2->m2));
        if ($debug) {
            echo "# current_lines after intersect: " . count($current_lines) . "<br/>";
            foreach ($current_lines as $line) {
                echo $line->name . " ";
            }
            echo "<br/>";
        }
        $current_lines = getCorrectLines($current_lines, $connection);
        if ($debug) {
            echo "# current_lines after correction: " . count($current_lines) . "<br/>";
            foreach ($current_lines as $line) {
                echo $line->name . " ";
            }
            echo "<br/>";
        }
        $connection = $segment2->connection;
        $total_time = $segment2->t;
    }
    // for
    // add the last segment
    $new_segment = new Segment($marker1, $segment2->m2, $current_lines, $total_time, $connection);
    $cpath[] = $new_segment;
    return $cpath;
}
示例#2
0
/**
    For each segment, the first marker and the second marker of the same segment will have the same lines. We check the line used. If it does not exist in second segment's second marker, then we know that the first marker is a junction point and needs to have a transfer.
    
    @param $path[]<Segment> the list of path segments
    @param $type<String> the type of connection (s2s,dir)
*/
function consolidatePath($path, $type)
{
    $cpath = array();
    $debug = false;
    $path_array = $path->path;
    $count = count($path_array);
    if ($debug) {
        echo "// algorithm2.php:consolidatePath()<br/>";
    }
    // this is a direction path and there is only one or two paths.
    // that means it either goes from loc->station->loc or just loc->loc
    if (($type == "dir" || $type == "dir_alt") && $count <= 2) {
        if ($debug) {
            echo "// end algorithm2.php:consolidatePath()<br/>";
        }
        return "walk";
    }
    //set up the first segment
    $segment = $path_array[0];
    if ($type == "s2s") {
        $i = 1;
    } else {
        $cpath[] = $segment;
        $segment = $path_array[1];
        $i = 2;
    }
    $marker1 = $segment->m1;
    $current_lines = $segment->m1->getOverlapLines($segment->m2);
    $connection = $segment->connection;
    $total_time = $segment->t;
    $segment2 = $path_array[$i];
    // loop through the path, starting at the next segment.
    for (; $i < $count; $i++) {
        // grab the segment
        $segment2 = $path_array[$i];
        if ($debug) {
            echo "[{$i}/{$count}]@" . $segment2->m1->name . "<br/>";
            echo "lines = ";
            foreach ($current_lines as $line) {
                echo $line->name . " ";
            }
            echo "<br/>";
        }
        if ($connection == "transfer") {
            // we know this segment is a transfer, so they will have to get off the train
            if ($debug) {
                echo "transfer<br/>";
            }
            $lines = $marker1->getOverlapLines($segment2->m1);
            $lines = getCorrectLines($lines, "transfer");
            if ($debug) {
                echo "lines: ";
                foreach ($lines as $line) {
                    echo $line->name . " ";
                }
                echo "<br/>";
            }
            // create the transfer segment
            $new_segment = new Segment($marker1, $segment2->m1, $lines, $total_time, $connection);
            if ($debug) {
                echo "adding segment = " . $new_segment->toString() . "<br/>";
            }
            $cpath[] = $new_segment;
            $segment = $path_array[$i];
            if ($debug) {
                echo "new segment = " . $segment->toString() . "<br/>";
            }
            $marker1 = $segment->m1;
            $current_lines = $marker1->getOverlapLines($segment->m2);
            $connection = $segment->connection;
            $total_time = $segment->t;
            if ($i < $count) {
                // more segments available
                if ($debug) {
                    echo "more segments {$i}/{$count}<br/>";
                }
                continue;
            }
            // we end here with this segment
            if ($debug) {
                echo "ending after transfer<br/>";
            }
            //$current_lines = getCorrectLines($current_lines, $connection);
            //$new_segment = new Segment($segment->m1, $segment->m2, $current_lines, $total_time, $connection);
            //$cpath[] = $new_segment;
            return $cpath;
        }
        // not a transfer
        $segment_lines = array();
        // find out which lines fit the segment
        foreach ($segment2->m1->lines as $line) {
            if ($debug) {
                echo "looking @ line: " . $line->name . "<br/>";
            }
            // check the connections and find which one to go to
            foreach ($line->connections as $line_connection) {
                if ($debug) {
                    echo "looking @ connection: " . $line_connection->id . "<br/>";
                }
                if ($line_connection->id == $segment2->m2->id) {
                    $line_name = $line->name;
                    if ($debug) {
                        echo "adding line {$line_name}<br/>";
                    }
                    $segment_lines["{$line_name}"] = $line;
                    break;
                }
            }
        }
        if ($debug) {
            echo "lines that fit the segment: ";
            foreach ($segment_lines as $line) {
                echo $line->name . " ";
            }
            echo "<br/>";
        }
        // we found all the lines that go to the marker that is next
        // now compare them to the current lines we have
        $new_lines = linesIntersect($current_lines, $segment_lines);
        if ($debug) {
            echo "lines that intersect the old: ";
            foreach ($new_lines as $line) {
                echo $line->name . " ";
            }
            echo "<br/>";
        }
        if (count($new_lines) == 0) {
            // no overlapping lines
            // end the old segment and add to the path
            $new_segment = new Segment($marker1, $segment2->m1, $current_lines, $total_time, $connection);
            if ($debug) {
                echo "adding segment = " . $new_segment->toString() . "<br/>";
            }
            $cpath[] = $new_segment;
            // reset our variables for the next segment
            $segment = $segment2;
            $marker1 = $segment2->m1;
            $current_lines = $segment2->m1->getOverlapLines($segment2->m2);
            $connection = $segment2->connection;
            $total_time = $segment2->t;
            continue;
        }
        // we move forward
        if ($debug) {
            echo "# current_lines: " . count($new_lines) . "<br/>";
            foreach ($new_lines as $line) {
                echo $line->name . " ";
            }
            echo "<br/>";
        }
        $current_lines = $new_lines;
        $connection = $segment2->connection;
        $total_time += $segment2->t;
    }
    // for
    // add the last segment
    if ($marker1->id != $segment2->m2->id) {
        $current_lines = getCorrectLines($current_lines, $connection);
        $new_segment = new Segment($marker1, $segment2->m2, $current_lines, $total_time, $connection);
        $cpath[] = $new_segment;
    }
    return $cpath;
}
示例#3
0
/**
    The location is the user inputted address or station object. However, it may not
    be the best place to start. There are several different scenarios that have
    obvious path choices. If it is a station and is the last station of the line, the
    algorithm doesn't need to determine options until it hits a station with multiple
    lines. If it isn't the last station, it may be one where either there is only one
    line or has multiple lines that have the same destination stations. If it is an 
    address and the stations are all on the same line or group of lines, then the
    station closest to the destination should be used.
*/
function findStartStations($location, $destination)
{
    $paths = array();
    $debug = true;
    outputDebug("findStartStations (" . $location->toString() . ", " . $destination->toString() . ")", $debug);
    $originalBox = new Box($location->point, $destination->point);
    outputDebug("original box = " . $originalBox->getArea(), $debug);
    if ($location instanceof Station2) {
        $visitedStations = array();
        foreach ($location->getLines() as $line) {
            foreach ($line->getConnections() as $connection) {
                // ignore already visited stations
                $cid = $connection->id;
                outputdebug("Looking at next connection " . $cid, $debug);
                if (in_array($cid, $visitedStations)) {
                    continue;
                }
                $nextStation = retrieveStation($cid);
                $visitedStations[] = $cid;
                // create a new path with this connection
                $segment = new Segment($location, $nextStation, $location->getOverlapLines($nextStation), $connection->duration, $connection->type);
                $visited = array();
                if ($location->isJunction()) {
                    $visited[] = $location;
                }
                if ($nextStation->isJunction()) {
                    outputDebug("nextStation is a junction.", $debug);
                    $visited[] = $nextStation;
                    outputDebug("segment = " . $segment->toString(), $debug);
                    $path = new Path($segment, $visited, $destination);
                    if ($originalBox->getArea() > $path->getBox()->getArea()) {
                        $metaPath = new MetaPath("start", $path);
                        outputDebug("creating path: " . $metaPath->toString(), $debug);
                        $paths = addToArray($paths, $metaPath);
                    } else {
                        outputDebug("path (" . $path->getBox()->getArea() . ") is greater than originalBox (" . $originalBox->getArea() . ")", $debug);
                    }
                    continue;
                }
                outputDebug("nextStation is NOT a junction.", $debug);
                // here we know that the nextStation is not a junction.
                $path = new Path($segment, $visited, $destination);
                if ($originalBox->getArea() < $path->getBox()->getArea()) {
                    outputDebug("path (" . $path->getBox()->getArea() . ") is greater than originalBox (" . $originalBox->getArea() . ")", $debug);
                    continue;
                }
                // extend the path until we find a junction point
                $metaPath = expandPath($path);
                if ($metaPath != null) {
                    outputDebug("creating path: " . $metaPath->toString(), $debug);
                    $paths = addToArray($paths, $metaPath);
                }
            }
        }
    }
    return $paths;
}