示例#1
0
        /// <summary>
        /// currentNode-ból járművel elérhető csomópontok és azok költsége (incompleteNodes-ból válogatva)
        /// </summary>
        /// <param name="currentNode"></param>
        /// <param name="incompleteNodes"></param>
        /// <param name="when"></param>
        /// <returns></returns>
        protected List <CompleteNode> GetRouteCandidates(CompleteNode currentNode, List <Node> incompleteNodes, DateTime when, int?limitOfRoutes = 5)
        {
            var candidates = new List <CompleteNode>();
            var routes     = GetRoutes(currentNode.stopId);

            if (routes == null)
            {
                return(candidates);
            }

            foreach (var routeId in routes)
            {
                bool isKnownRouteId = currentNode.usedRouteIds.Contains(routeId);
                if (!(isKnownRouteId && (currentNode.usedRouteIds.Peek() != routeId)))
                {
                    // A járatok nem újrafelhasználhatóak!

                    foreach (var stop in GetEndpoints(currentNode.stopId, routeId))
                    {
                        if (!incompleteNodes.Exists(ic => ic.stopId == stop.DbId))
                        {
                            // Csak akkor foglalkozzunk vele, ha még nem dolgoztuk fel
                            continue;
                        }

                        var edge     = new RouteEdge(stop.DbId, routeId, when, currentNode.departureTime);
                        var edgeCost = edge.GetCost();

                        if (edgeCost != null)
                        {
                            var usedEdges = new Stack <Edge>(currentNode.usedEdges.Reverse());

                            #region Check for hidden transfers
                            {
                                // Ha ugyanannál a megállónál száll át...
                                if ((usedEdges.Count > 0) && (usedEdges.Peek() is RouteEdge))
                                {
                                    if (((RouteEdge)usedEdges.Peek()).RouteId != edge.RouteId)
                                    {
                                        var stp = GetStop(currentNode.stopId);
                                        usedEdges.Push(new TransferEdge(stp.DbId, currentNode.departureTime, stp, stp));
                                        edgeCost += usedEdges.Peek().GetCost();
                                    }
                                }
                            }
                            #endregion

                            usedEdges.Push(edge);

                            var usedRouteIds = new Stack <int>(currentNode.usedRouteIds.Reverse());
                            if (!isKnownRouteId)
                            {
                                usedRouteIds.Push(routeId);
                            }

                            if ((limitOfRoutes == null) || (usedRouteIds.Count <= limitOfRoutes))
                            {
                                candidates.Add(new CompleteNode
                                {
                                    stopId        = stop.DbId,
                                    viaNode       = currentNode,
                                    departureTime = currentNode.departureTime.Add(TimeSpan.FromMinutes((int)edgeCost)),
                                    usedEdges     = usedEdges,
                                    usedRouteIds  = new Stack <int>(usedRouteIds.Reverse())
                                });
                            }
                        }
                    }
                }
            }

            return(candidates);
        }
示例#2
0
        /// <summary>
        /// currentNode-ból járművel elérhető csomópontok és azok költsége (incompleteNodes-ból válogatva)
        /// </summary>
        /// <param name="currentNode"></param>
        /// <param name="incompleteNodes"></param>
        /// <param name="when"></param>
        /// <returns></returns>
        protected List<CompleteNode> GetRouteCandidates(CompleteNode currentNode, List<Node> incompleteNodes, DateTime when, int? limitOfRoutes = 5)
        {
            var candidates = new List<CompleteNode>();
            var routes = GetRoutes(currentNode.stopId);

            if (routes == null)
            {
                return candidates;
            }

            foreach (var routeId in routes)
            {
                bool isKnownRouteId = currentNode.usedRouteIds.Contains(routeId);
                if (!(isKnownRouteId && (currentNode.usedRouteIds.Peek() != routeId)))
                {
                    // A járatok nem újrafelhasználhatóak!

                    foreach (var stop in GetEndpoints(currentNode.stopId, routeId))
                    {
                        if (!incompleteNodes.Exists(ic => ic.stopId == stop.DbId))
                        {
                            // Csak akkor foglalkozzunk vele, ha még nem dolgoztuk fel
                            continue;
                        }

                        var edge = new RouteEdge(stop.DbId, routeId, when, currentNode.departureTime);
                        var edgeCost = edge.GetCost();

                        if (edgeCost != null)
                        {
                            var usedEdges = new Stack<Edge>(currentNode.usedEdges.Reverse());

                            #region Check for hidden transfers
                            {
                                // Ha ugyanannál a megállónál száll át...
                                if ((usedEdges.Count > 0) && (usedEdges.Peek() is RouteEdge))
                                {
                                    if (((RouteEdge)usedEdges.Peek()).RouteId != edge.RouteId)
                                    {
                                        var stp = GetStop(currentNode.stopId);
                                        usedEdges.Push(new TransferEdge(stp.DbId, currentNode.departureTime, stp, stp));
                                        edgeCost += usedEdges.Peek().GetCost();
                                    }
                                }
                            }
                            #endregion

                            usedEdges.Push(edge);

                            var usedRouteIds = new Stack<int>(currentNode.usedRouteIds.Reverse());
                            if (!isKnownRouteId)
                            {
                                usedRouteIds.Push(routeId);
                            }

                            if ((limitOfRoutes == null) || (usedRouteIds.Count <= limitOfRoutes))
                            {
                                candidates.Add(new CompleteNode
                                {
                                    stopId = stop.DbId,
                                    viaNode = currentNode,
                                    departureTime = currentNode.departureTime.Add(TimeSpan.FromMinutes((int)edgeCost)),
                                    usedEdges = usedEdges,
                                    usedRouteIds = new Stack<int>(usedRouteIds.Reverse())
                                });
                            }
                        }
                    }
                }
            }

            return candidates;
        }