Example #1
0
        /// <summary>
        /// Tries to calculate an earliest arrival route from stop1 to stop2.
        /// </summary>
        public Result <Route> TryEarliestArrival(DateTime departureTime, uint stop1, uint stop2, Func <uint, bool> useAgency)
        {
            var tripEnumerator = _transitDb.GetTripsEnumerator();
            var transfersDb    = _transitDb.GetTransfersDb(_transferProfile);
            var profileSearch  = new ProfileSearch(_transitDb, departureTime, transfersDb, (t, day) =>
            {
                if (tripEnumerator.MoveTo(t))
                {
                    if (useAgency(tripEnumerator.AgencyId))
                    {
                        return(true);
                    }
                }
                return(false);
            });

            profileSearch.SetSourceStop(stop1, (uint)(departureTime - departureTime.Date).TotalSeconds);
            profileSearch.SetTargetStop(stop2, 0);
            profileSearch.Run();
            if (!profileSearch.HasSucceeded)
            {
                return(new Result <Route>(profileSearch.ErrorMessage, (message) =>
                {
                    return new RouteNotFoundException(message);
                }));
            }

            // generate route.
            var routeBuilder = new ProfileSearchRouteBuilder(profileSearch);

            routeBuilder.Run();
            if (!routeBuilder.HasSucceeded)
            {
                return(new Result <Route>(routeBuilder.ErrorMessage, (message) =>
                {
                    return new RouteBuildFailedException(message);
                }));
            }
            return(new Result <Route>(routeBuilder.Route));
        }
Example #2
0
        public void TestOneHopUnsuccessful()
        {
            // build dummy db.
            var db = new TransitDb();

            db.AddStop(0, 0, 0);
            db.AddStop(1, 1, 1);
            db.AddTrip(0, 0, 0);
            db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60);
            db.SortConnections(DefaultSorting.DepartureTime, null);

            // run algorithm.
            var algorithm = new ProfileSearch(db, new DateTime(2017, 05, 10, 08, 30, 00),
                                              (profileId, day) => true);

            algorithm.SetSourceStop(0, 08 * 3600 + 30 * 60);
            algorithm.SetTargetStop(1, 0);
            algorithm.Run();

            // test results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsFalse(algorithm.HasSucceeded);
        }
Example #3
0
        public void TestOneHopWithWalkingAfter()
        {
            // build dummy db.
            var db = new TransitDb();

            db.AddStop(0, 0, db.StopAttributes.Add(new Attribute("name", "stop1")));
            db.AddStop(1, 1, db.StopAttributes.Add(new Attribute("name", "stop2")));
            db.AddTrip(0, 0, db.TripAttributes.Add(new Attribute("name", "trip1")));
            db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60);
            db.SortConnections(DefaultSorting.DepartureTime, null);

            // run algorithm.
            var departureTime = new System.DateTime(2017, 05, 10, 07, 30, 00);
            var algorithm     = new ProfileSearch(db, departureTime,
                                                  (profileId, day) => true);

            algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60); // a 15 min walk.
            algorithm.SetTargetStop(1, 15 * 60);
            algorithm.Run();

            // build route.
            var routeBuilder = new ProfileSearchRouteBuilder(algorithm, false);

            routeBuilder.Run();
            var route = routeBuilder.Route;

            Assert.IsNotNull(route);

            Assert.IsNotNull(route.Shape);
            Assert.AreEqual(3, route.Shape.Length);
            Assert.AreEqual(0, route.Shape[0].Latitude);
            Assert.AreEqual(0, route.Shape[0].Longitude);
            Assert.AreEqual(0, route.Shape[1].Latitude);
            Assert.AreEqual(0, route.Shape[1].Longitude);
            Assert.AreEqual(1, route.Shape[2].Latitude);
            Assert.AreEqual(1, route.Shape[2].Longitude);

            Assert.IsNotNull(route.Stops);
            Assert.AreEqual(3, route.Stops.Length);
            var stop = route.Stops[0];

            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop1"));
            Assert.AreEqual(0, stop.Shape);
            Assert.AreEqual(0, stop.Coordinate.Latitude);
            Assert.AreEqual(0, stop.Coordinate.Longitude);
            stop = route.Stops[1];
            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop1"));
            Assert.AreEqual(1, stop.Shape);
            Assert.AreEqual(0, stop.Coordinate.Latitude);
            Assert.AreEqual(0, stop.Coordinate.Longitude);
            stop = route.Stops[2];
            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop2"));
            Assert.AreEqual(2, stop.Shape);
            Assert.AreEqual(1, stop.Coordinate.Latitude);
            Assert.AreEqual(1, stop.Coordinate.Longitude);

            Assert.IsNotNull(route.ShapeMeta);
            Assert.AreEqual(3, route.ShapeMeta.Length);

            var meta = route.ShapeMeta[0];

            Assert.AreEqual(0, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((07 * 3600) + (30 * 60)).ToInvariantString()));
            Assert.AreEqual(0, meta.Time);

            meta = route.ShapeMeta[1];
            Assert.AreEqual(1, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((08 * 3600) + (00 * 60)).ToInvariantString()));
            Assert.AreEqual(30 * 60, meta.Time);

            meta = route.ShapeMeta[2];
            Assert.AreEqual(2, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((08 * 3600) + (10 * 60)).ToInvariantString()));
            Assert.AreEqual(meta.Time, 40 * 60);
        }
Example #4
0
        public void TestTwoHopsOneTransferCloseStopsSuccessful()
        {
            // build dummy db.
            var db = new TransitDb();

            db.AddStop(0, 0, db.StopAttributes.Add(new Attribute("name", "stop1")));
            db.AddStop(1, 1, db.StopAttributes.Add(new Attribute("name", "stop2")));
            db.AddStop(2, 2, db.StopAttributes.Add(new Attribute("name", "stop3")));
            db.AddStop(3, 3, db.StopAttributes.Add(new Attribute("name", "stop4")));
            db.AddTrip(0, 0, db.TripAttributes.Add(new Attribute("name", "trip1")));
            db.AddTrip(0, 0, db.TripAttributes.Add(new Attribute("name", "trip2")));
            db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60);
            db.AddConnection(2, 3, 1, 8 * 3600 + 15 * 60, 8 * 3600 + 25 * 60);

            db.SortConnections(DefaultSorting.DepartureTime, null);

            // build dummy transfers db.
            var transfersDb = new TransfersDb(1024);

            transfersDb.AddTransfer(1, 2, 100);

            // run algorithm.
            var departureTime = new System.DateTime(2017, 05, 10, 07, 30, 00);
            var algorithm     = new ProfileSearch(db, departureTime, transfersDb,
                                                  (profileId, day) => true);

            algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60);
            algorithm.SetTargetStop(3, 0);
            algorithm.Run();

            // build route.
            var routeBuilder = new ProfileSearchRouteBuilder(algorithm, false);

            routeBuilder.Run();
            var route = routeBuilder.Route;

            Assert.IsNotNull(route);

            Assert.IsNotNull(route.Shape);
            Assert.AreEqual(6, route.Shape.Length);
            Assert.AreEqual(0, route.Shape[0].Latitude);
            Assert.AreEqual(0, route.Shape[0].Longitude);
            Assert.AreEqual(0, route.Shape[1].Latitude);
            Assert.AreEqual(0, route.Shape[1].Longitude);
            Assert.AreEqual(1, route.Shape[2].Latitude);
            Assert.AreEqual(1, route.Shape[2].Longitude);
            Assert.AreEqual(2, route.Shape[3].Latitude);
            Assert.AreEqual(2, route.Shape[3].Longitude);
            Assert.AreEqual(2, route.Shape[4].Latitude);
            Assert.AreEqual(2, route.Shape[4].Longitude);
            Assert.AreEqual(3, route.Shape[5].Latitude);
            Assert.AreEqual(3, route.Shape[5].Longitude);

            Assert.IsNotNull(route.Stops);
            Assert.AreEqual(6, route.Stops.Length);
            var stop = route.Stops[0];

            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop1"));
            Assert.AreEqual(0, stop.Shape);
            Assert.AreEqual(0, stop.Coordinate.Latitude);
            Assert.AreEqual(0, stop.Coordinate.Longitude);
            stop = route.Stops[1];
            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop1"));
            Assert.AreEqual(1, stop.Shape);
            Assert.AreEqual(0, stop.Coordinate.Latitude);
            Assert.AreEqual(0, stop.Coordinate.Longitude);
            stop = route.Stops[2];
            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop2"));
            Assert.AreEqual(2, stop.Shape);
            Assert.AreEqual(1, stop.Coordinate.Latitude);
            Assert.AreEqual(1, stop.Coordinate.Longitude);
            stop = route.Stops[3];
            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop3"));
            Assert.AreEqual(3, stop.Shape);
            Assert.AreEqual(2, stop.Coordinate.Latitude);
            Assert.AreEqual(2, stop.Coordinate.Longitude);
            stop = route.Stops[4];
            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop3"));
            Assert.AreEqual(4, stop.Shape);
            Assert.AreEqual(2, stop.Coordinate.Latitude);
            Assert.AreEqual(2, stop.Coordinate.Longitude);
            stop = route.Stops[5];
            Assert.IsNotNull(stop.Attributes);
            Assert.AreEqual(1, stop.Attributes.Count);
            Assert.IsTrue(stop.Attributes.Contains("name", "stop4"));
            Assert.AreEqual(5, stop.Shape);
            Assert.AreEqual(3, stop.Coordinate.Latitude);
            Assert.AreEqual(3, stop.Coordinate.Longitude);

            Assert.IsNotNull(route.ShapeMeta);
            Assert.AreEqual(6, route.ShapeMeta.Length);

            var meta = route.ShapeMeta[0];

            Assert.AreEqual(0, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((07 * 3600) + (30 * 60)).ToInvariantString()));
            Assert.AreEqual(0, meta.Time);

            meta = route.ShapeMeta[1];
            Assert.AreEqual(1, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((08 * 3600) + (00 * 60)).ToInvariantString()));
            Assert.AreEqual(meta.Time, 30 * 60);

            meta = route.ShapeMeta[2];
            Assert.AreEqual(2, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((08 * 3600) + (10 * 60)).ToInvariantString()));
            Assert.AreEqual(meta.Time, 40 * 60);

            meta = route.ShapeMeta[3];
            Assert.AreEqual(3, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((08 * 3600) + (10 * 60) + 100).ToInvariantString()));
            Assert.AreEqual(meta.Time, 40 * 60 + 100);

            meta = route.ShapeMeta[4];
            Assert.AreEqual(4, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((08 * 3600) + (15 * 60)).ToInvariantString()));
            Assert.AreEqual(meta.Time, 45 * 60);

            meta = route.ShapeMeta[5];
            Assert.AreEqual(5, meta.Shape);
            Assert.IsNotNull(meta.Attributes);
            Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey,
                                                   ((08 * 3600) + (25 * 60)).ToInvariantString()));
            Assert.AreEqual(meta.Time, 55 * 60);
        }
Example #5
0
        /// <summary>
        /// Tries to calculate an earliest arrival route from stop1 to stop2.
        /// </summary>
        public override Result <Route> TryEarliestArrival(DateTime departureTime,
                                                          RouterPoint sourcePoint, Profile sourceProfile, RouterPoint targetPoint, Profile targetProfile,
                                                          EarliestArrivalSettings settings)
        {
            // get the get factor function.
            var sourceGetFactor = _router.GetDefaultGetFactor(sourceProfile);
            var targetGetFactor = _router.GetDefaultGetFactor(targetProfile);

            // create the profile search.
            var tripEnumerator = _db.TransitDb.GetTripsEnumerator();
            var transfersDb    = _db.TransitDb.GetTransfersDb(_transferProfile);
            var profileSearch  = new ProfileSearch(_db.TransitDb, departureTime, transfersDb, _db.TransitDb.GetIsTripPossibleFunc());

            // search for sources.
            var departureTimeSeconds = (uint)(departureTime - departureTime.Date).TotalSeconds;
            var sourceSearch         = new ClosestStopsSearch(_db, sourceProfile, sourceGetFactor, sourcePoint,
                                                              settings.MaxSecondsSource, false);

            sourceSearch.StopFound = (s, t) =>
            {
                profileSearch.SetSourceStop(s, departureTimeSeconds + (uint)t);
                return(false);
            };

            // search for targets.
            var targetSearch = new ClosestStopsSearch(_db, targetProfile, targetGetFactor, targetPoint,
                                                      settings.MaxSecondsTarget, true);

            targetSearch.StopFound = (s, t) =>
            {
                profileSearch.SetTargetStop(s, (uint)t);
                return(false);
            };

            // create bidirectional helper if possible.
            SearchHelper helper = null;
            BidirectionalSearchHelper bidirectionalHelper = null;

            if (sourceProfile.Name == targetProfile.Name)
            { // profiles are the same.
                bidirectionalHelper = new BidirectionalSearchHelper(
                    sourceSearch.Search, targetSearch.Search);
                targetSearch.WasEdgeFound = bidirectionalHelper.TargetWasFound;
            }
            else
            { // profiles are different but the source can still reach the destination.
                helper = new SearchHelper(_router.Db, sourceSearch.Search, sourceProfile, targetPoint);
                sourceSearch.WasEdgeFound = helper.SourceWasFound;
            }

            // run source search.
            sourceSearch.Run();
            if (!sourceSearch.HasRun ||
                !sourceSearch.HasSucceeded)
            {
                return(new Result <Route>("Searching for source stops failed."));
            }

            // run target search.
            targetSearch.Run();
            if (!targetSearch.HasRun ||
                !targetSearch.HasSucceeded)
            {
                return(new Result <Route>("Searching for target stops failed."));
            }

            // run actual profile search.
            profileSearch.Run();
            if (!profileSearch.HasRun ||
                !profileSearch.HasSucceeded)
            {
                return(new Result <Route>("No route found."));
            }

            // build routes.
            var profileSearchRouteBuilder = new ProfileSearchRouteBuilder(profileSearch);

            profileSearchRouteBuilder.Run();
            if (!profileSearchRouteBuilder.HasRun ||
                !profileSearchRouteBuilder.HasSucceeded)
            {
                return(new Result <Route>(string.Format("Route could not be built: {0}.", profileSearchRouteBuilder.ErrorMessage)));
            }

            var sourceWeight  = sourceSearch.GetWeight(profileSearchRouteBuilder.Stops[0]);
            var targetWeight  = targetSearch.GetWeight(profileSearchRouteBuilder.Stops[profileSearchRouteBuilder.Stops.Count - 1]);
            var transitWeight = sourceWeight + profileSearchRouteBuilder.Duration + targetWeight;

            if (bidirectionalHelper != null &&
                bidirectionalHelper.HasSucceeded)
            { // there is a direct route to the target.
                if (transitWeight > bidirectionalHelper.BestWeight)
                {
                    if (!this.PreferTransit || bidirectionalHelper.BestWeight < this.PreferTransitThreshold)
                    { // transit it not preferred or belof threshold.
                        var path = bidirectionalHelper.GetPath();
                        return(new Result <Route>(
                                   CompleteRouteBuilder.Build(_router.Db, sourceProfile, sourcePoint, targetPoint, path)));
                    }
                }
            }
            else if (helper != null &&
                     helper.HasSucceeded)
            { // there is a direct route to the target.
                if (transitWeight > helper.BestWeight)
                {
                    if (!this.PreferTransit || bidirectionalHelper.BestWeight < this.PreferTransitThreshold)
                    { // transit it not preferred or belof threshold.
                        var path = helper.GetPath();
                        return(new Result <Route>(
                                   CompleteRouteBuilder.Build(_router.Db, sourceProfile, sourcePoint, targetPoint, path)));
                    }
                }
            }

            // build source/target routes.
            var sourceRoute = sourceSearch.GetRoute(profileSearchRouteBuilder.Stops[0]);
            var targetRoute = targetSearch.GetRoute(profileSearchRouteBuilder.Stops[profileSearchRouteBuilder.Stops.Count - 1]);

            // concatenate it all.
            var route = sourceRoute.Concatenate(profileSearchRouteBuilder.Route);

            route = route.Concatenate(targetRoute);

            return(new Result <Route>(route));
        }
Example #6
0
        public void TestOneHopScheduled()
        {
            // build dummy db.
            var db       = new TransitDb();
            var schedule = db.AddSchedule();

            db.AddScheduleEntry(schedule, new DateTime(2017, 01, 01), new DateTime(2018, 01, 01),
                                DayOfWeek.Monday);
            db.AddStop(0, 0, 0);
            db.AddStop(1, 1, 1);
            db.AddTrip(schedule, 0, 0);
            db.AddConnection(0, 1, 0, 3600, 3600 + 40 * 60);
            db.SortConnections(DefaultSorting.DepartureTime, null);

            // run algorithm.
            var departureTime = new DateTime(2017, 05, 08, 00, 50, 00);
            var algorithm     = new ProfileSearch(db, departureTime,
                                                  db.GetIsTripPossibleFunc());

            algorithm.SetSourceStop(0, 50 * 60);
            algorithm.SetTargetStop(1, 0);
            algorithm.Run();

            // test results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            // check arrival profile(s).
            var arrivalStops = algorithm.ArrivalStops;

            Assert.AreEqual(3, arrivalStops.Count);
            Assert.AreEqual(1, arrivalStops[2]);
            var arrivalProfiles = algorithm.ArrivalProfiles;

            Assert.AreEqual(3, arrivalProfiles.Count);
            Assert.AreEqual(3600 * 01 + 40 * 60, arrivalProfiles[2].Seconds);
            Assert.AreEqual(50 * 60, algorithm.Duration(2));
            Assert.AreEqual(new DateTime(2017, 05, 08, 01, 40, 00), algorithm.ArrivalTime(2));

            // check stop profiles.
            var connections = db.GetConnectionsEnumerator(DefaultSorting.DepartureTime);
            var profiles    = algorithm.GetStopProfiles(1);
            var profile     = profiles[0];

            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(0, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 50 * 60, profile.Seconds);
            connections.MoveTo(profile.PreviousConnectionId);
            Assert.AreEqual(0, connections.TripId);
            Assert.AreEqual(0, connections.DepartureStop);
            profiles = algorithm.GetStopProfiles(0);
            profile  = profiles[0];
            Assert.AreEqual(Itinero.Transit.Constants.NoConnectionId, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds, profile.Seconds);

            var tripStatus = algorithm.GetTripStatus(0);

            Assert.AreEqual(2, tripStatus.Transfers);
            Assert.AreEqual(0, tripStatus.StopId);
            Assert.AreEqual(3600, tripStatus.DepartureTime);

            // run algorithm.
            departureTime = new DateTime(2017, 05, 10, 00, 50, 00);
            algorithm     = new ProfileSearch(db, departureTime,
                                              db.GetIsTripPossibleFunc());
            algorithm.SetSourceStop(0, 50 * 60);
            algorithm.SetTargetStop(1, 0);
            algorithm.Run();

            Assert.IsTrue(algorithm.HasRun);
            Assert.IsFalse(algorithm.HasSucceeded);
        }
Example #7
0
        public void TestTwoHopsOneTransferCloseStopsSuccessfulSkippedPseudo()
        {
            // build dummy db.
            var db = new TransitDb();

            db.AddStop(0, 0, 0);
            db.AddStop(1, 1, 1);
            db.AddStop(2, 2, 2);
            db.AddStop(3, 3, 3);
            db.AddTrip(0, 0, 0);
            db.AddTrip(0, 0, 0);
            db.AddTrip(0, 0, 0);
            db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60);
            db.AddConnection(1, 2, 0, 8 * 3600 + 10 * 60, 8 * 3600 + 15 * 60);
            db.AddConnection(2, 3, 0, 8 * 3600 + 15 * 60, 8 * 3600 + 25 * 60);

            db.SortConnections(DefaultSorting.DepartureTime, null);

            // build dummy transfers db.
            var transfersDb = new TransfersDb(1024);

            transfersDb.AddTransfer(1, 2, 60); // this leads to a transfer time faster than the actual connection.

            // run algorithm.
            var departureTime = new DateTime(2017, 05, 10, 07, 30, 00);
            var algorithm     = new ProfileSearch(db, departureTime, transfersDb,
                                                  (profileId, day) => true);

            algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60);
            algorithm.SetTargetStop(3, 0);
            algorithm.Run();

            // test results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            // check arrival profile(s).
            var arrivalStops = algorithm.ArrivalStops;

            Assert.AreEqual(3, arrivalStops.Count);
            Assert.AreEqual(3, arrivalStops[2]);
            var arrivalProfiles = algorithm.ArrivalProfiles;

            Assert.AreEqual(3, arrivalProfiles.Count);
            Assert.AreEqual(08 * 3600 + 25 * 60, arrivalProfiles[2].Seconds);
            Assert.AreEqual(55 * 60, algorithm.Duration(2));
            Assert.AreEqual(new DateTime(2017, 05, 10, 08, 25, 00), algorithm.ArrivalTime(2));

            // check stop profiles.
            var connections = db.GetConnectionsEnumerator(DefaultSorting.DepartureTime);
            var profiles    = algorithm.GetStopProfiles(3);

            Assert.AreEqual(3, profiles.Count);
            var profile = profiles[0];

            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(2, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 55 * 60, profile.Seconds);

            profiles = algorithm.GetStopProfiles(2);
            Assert.AreEqual(4, profiles.Count);
            profile = profiles[0];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(1, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 45 * 60, profile.Seconds);
            profile = profiles[3];
            Assert.IsTrue(profile.IsTransfer);
            Assert.AreEqual(1, profile.PreviousStopId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 40 * 60 + 60, profile.Seconds);

            profiles = algorithm.GetStopProfiles(1);
            Assert.AreEqual(3, profiles.Count);
            profile = profiles[0];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(0, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 40 * 60, profile.Seconds);

            profiles = algorithm.GetStopProfiles(0);
            Assert.AreEqual(1, profiles.Count);
            profile = profiles[0];
            Assert.AreEqual(Itinero.Transit.Constants.NoConnectionId, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds, profile.Seconds);

            // build dummy transfers db.
            transfersDb = new TransfersDb(1024);
            transfersDb.AddTransfer(1, 2, 6 * 60); // this leads to a transfer time slower than the actual connection.

            // run algorithm.
            departureTime = new DateTime(2017, 05, 10, 07, 30, 00);
            algorithm     = new ProfileSearch(db, departureTime, transfersDb,
                                              (profileId, day) => true);
            algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60);
            algorithm.SetTargetStop(3, 0);
            algorithm.Run();

            // test results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            // check arrival profile(s).
            arrivalStops = algorithm.ArrivalStops;
            Assert.AreEqual(3, arrivalStops.Count);
            Assert.AreEqual(3, arrivalStops[2]);
            arrivalProfiles = algorithm.ArrivalProfiles;
            Assert.AreEqual(3, arrivalProfiles.Count);
            Assert.AreEqual(08 * 3600 + 25 * 60, arrivalProfiles[2].Seconds);
            Assert.AreEqual(55 * 60, algorithm.Duration(2));
            Assert.AreEqual(new DateTime(2017, 05, 10, 08, 25, 00), algorithm.ArrivalTime(2));

            // check stop profiles.
            connections = db.GetConnectionsEnumerator(DefaultSorting.DepartureTime);
            profiles    = algorithm.GetStopProfiles(3);
            Assert.AreEqual(3, profiles.Count);
            profile = profiles[0];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(2, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 55 * 60, profile.Seconds);

            profiles = algorithm.GetStopProfiles(2);
            Assert.AreEqual(3, profiles.Count);
            profile = profiles[0];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(1, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 45 * 60, profile.Seconds);

            profiles = algorithm.GetStopProfiles(1);
            Assert.AreEqual(3, profiles.Count);
            profile = profiles[0];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(0, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 40 * 60, profile.Seconds);

            profiles = algorithm.GetStopProfiles(0);
            Assert.AreEqual(1, profiles.Count);
            profile = profiles[0];
            Assert.AreEqual(Itinero.Transit.Constants.NoConnectionId, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds, profile.Seconds);

            var tripStatus = algorithm.GetTripStatus(0);

            Assert.AreEqual(2, tripStatus.Transfers);
            Assert.AreEqual(0, tripStatus.StopId);
            Assert.AreEqual(08 * 3600 + 00 * 60, tripStatus.DepartureTime);
        }
Example #8
0
        public void TestTwoHopsOneTransferVersusOneHopSuccessful()
        {
            // build dummy db.
            var db = new TransitDb();

            db.AddStop(0, 0, 0);
            db.AddStop(1, 1, 1);
            db.AddStop(2, 2, 2);
            db.AddTrip(0, 0, 0);
            db.AddTrip(0, 0, 0);
            db.AddTrip(0, 0, 0);
            db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60);
            db.AddConnection(1, 2, 1, 8 * 3600 + 15 * 60, 8 * 3600 + 25 * 60);
            db.AddConnection(0, 2, 2, 8 * 3600 + 16 * 60, 8 * 3600 + 25 * 60);

            db.SortConnections(DefaultSorting.DepartureTime, null);

            // run algorithm.
            var departureTime = new DateTime(2017, 05, 10, 07, 30, 00);
            var algorithm     = new ProfileSearch(db, departureTime,
                                                  (profileId, day) => true);

            algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60);
            algorithm.SetTargetStop(2, 0);
            algorithm.Run();

            // test results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            // check arrival profile(s).
            var arrivalStops = algorithm.ArrivalStops;

            Assert.AreEqual(3, arrivalStops.Count);
            Assert.AreEqual(2, arrivalStops[2]);
            var arrivalProfiles = algorithm.ArrivalProfiles;

            Assert.AreEqual(3, arrivalProfiles.Count);
            Assert.AreEqual(08 * 3600 + 25 * 60, arrivalProfiles[2].Seconds);
            Assert.AreEqual(55 * 60, algorithm.Duration(2));
            Assert.AreEqual(new DateTime(2017, 05, 10, 08, 25, 00), algorithm.ArrivalTime(2));

            // check stop profiles.
            var connections   = db.GetConnectionsEnumerator(DefaultSorting.DepartureTime);
            var precedingStop = Itinero.Transit.Constants.NoStopId;
            var transfers     = Itinero.Transit.Constants.NoTransfers;

            // get profiles at stop 2.
            var profiles = algorithm.GetStopProfiles(2);
            var profile  = profiles[0];

            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(2, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 55 * 60, profile.Seconds);
            connections.MoveTo(profile.PreviousConnectionId);
            Assert.AreEqual(0, connections.DepartureStop);
            Assert.AreEqual(2, connections.TripId);

            // get previous profile and check this is stop 0.
            profile  = algorithm.GetPreceding(profiles, 2, out precedingStop, out transfers);
            profiles = algorithm.GetStopProfiles(0);
            profile  = profiles[0];
            Assert.AreEqual(Itinero.Transit.Constants.NoConnectionId, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds, profile.Seconds);

            // check the profiles at stop 1.
            profiles = algorithm.GetStopProfiles(1);
            profile  = profiles[0];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[1];
            Assert.IsTrue(profile.IsEmpty);
            profile = profiles[2];
            Assert.AreEqual(0, profile.PreviousConnectionId);
            Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 40 * 60, profile.Seconds);
            connections.MoveTo(profile.PreviousConnectionId);
            Assert.AreEqual(0, connections.DepartureStop);
            Assert.AreEqual(0, connections.TripId);

            var tripStatus = algorithm.GetTripStatus(0);

            Assert.AreEqual(2, tripStatus.Transfers);
            Assert.AreEqual(0, tripStatus.StopId);
            Assert.AreEqual(08 * 3600 + 00 * 60, tripStatus.DepartureTime);

            tripStatus = algorithm.GetTripStatus(1);
            Assert.AreEqual(4, tripStatus.Transfers);
            Assert.AreEqual(1, tripStatus.StopId);
            Assert.AreEqual(08 * 3600 + 15 * 60, tripStatus.DepartureTime);

            tripStatus = algorithm.GetTripStatus(2);
            Assert.AreEqual(2, tripStatus.Transfers);
            Assert.AreEqual(0, tripStatus.StopId);
            Assert.AreEqual(08 * 3600 + 16 * 60, tripStatus.DepartureTime);
        }