Пример #1
0
        private void SetAverageSpeedAndTotalDensity(ICrossroad crossroad, ILocation location)
        {
            ZeroCrossroadTrafficData(crossroad);

            foreach (var trafficFlow in _trafficFlows)
            {
                if (trafficFlow.Path.Contains(location))
                {
                    for (var pathIndex = 0; pathIndex < trafficFlow.Path.Count; pathIndex++)
                    {
                        var pathElementLocation = trafficFlow.Path[pathIndex];
                        var pathElement         = _map.GetElement(pathElementLocation.Row,
                                                                  pathElementLocation.Column);

                        if (pathElement is ICrossroad)
                        {
                            if (crossroad == pathElement || location.Equals(pathElementLocation))
                            {
                                AddCrossroadTrafficData(trafficFlow.Path[pathIndex - 1], pathElementLocation,
                                                        trafficFlow.Path[pathIndex + 1], crossroad, trafficFlow);
                                break;
                            }

                            if (!IsPathOpen(trafficFlow.Path[pathIndex - 1], pathElementLocation,
                                            trafficFlow.Path[pathIndex + 1], (ICrossroad)pathElement))
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public static bool DoEquals(this ILocation location, object other)
        {
            if (other == null)
            {
                return(false);
            }
            ILocation other1 = other as ILocation;

            if (other1 != null)
            {
                return(location.Equals(other1));
            }
            return(false);
        }
Пример #3
0
        //returns if the path is found; variable path contains the path
        public bool findPath()
        {
            while (open.Count > 0)
            {
                // Find the best node (lowest f). After sorting it
                // will be the last element in the array, and we
                // remove it from OPEN and also update its open flag.
                //open = open.sortOn('f', Array.DESCENDING | Array.NUMERIC);
                open = SortStack <CalcNode>(open);

                CalcNode best = open.Pop();
                best.Open = 0;

                // If we find the goal, we're done.
                if (goal.Equals(best.Node))
                {
                    reconstructPath();
                    return(true);
                }

                // Add the neighbors of this node to OPEN
                ILocation[] next = graph.GetNodeNeighbors(best.Node);
                for (int j = 0; j != next.GetLength(0); j++)
                {
                    float c = cost(best.Node, next[j], m_Unit);
                    if (float.IsInfinity(c))
                    {
                        continue;                  // cannot pass
                    }
                    // Every node needs to be in VISITED; be sure it's there.
                    CalcNode e;
                    if (!visited.TryGetValue(next[j].nodeToString(), out e))
                    {
                        e        = new CalcNode();
                        e.Node   = next[j];
                        e.Open   = 0;
                        e.Closed = 0;
                        e.Parent = null;
                        e.g      = float.PositiveInfinity;
                        e.h      = 0;
                        e.f      = 0;
                        visited.Add(next[j].nodeToString(), e);
                    }
                    //if the movement-range of the unit is already used up we cannot reach target
                    if ((this.m_Unit.GetRange() - (best.g + c)) < 0f)
                    {
                        continue;
                    }
                    // We'll consider this node if the new cost (g) is
                    // better than the old cost. The old cost starts
                    // at Infinity, so it's always better the first
                    // time we see this node.
                    if (best.g + c < e.g)
                    {
                        if (e.Open <= 0)
                        {
                            e.Open = 1;
                            open.Push(e);
                        }
                        e.g      = best.g + c;
                        e.h      = manhattanHeuristic(e.Node, goal, graph, m_Unit);
                        e.f      = (alpha * e.g + (1 - alpha) * e.h) / Math.Max(alpha, 1 - alpha);
                        e.Parent = best;
                    }
                }
            }
            return(false);
        }