示例#1
0
        /// <summary>
        /// Expands the current coverage area with all neighbors of the specified <see
        /// cref="Graph"/> node that can be reached by the current <see cref="Agent"/>.</summary>
        /// <param name="node">
        /// The <see cref="Graph"/> node whose neighbors to examine.</param>
        /// <param name="cost">
        /// The total path cost to reach <paramref name="node"/>, measured as the sum of all <see
        /// cref="IGraphAgent{T}.GetStepCost"/> results for each movement step between neighboring
        /// nodes.</param>
        /// <remarks><para>
        /// <b>ExpandArea</b> recursively computes all possible movement paths for the current <see
        /// cref="Agent"/>, adding all valid nodes in any affordable path to the <see cref="Nodes"/>
        /// collection.
        /// </para><para>
        /// <b>ExpandArea</b> never revisits nodes that were already reached by a better path. The
        /// source node specified in the <see cref="FindReachable"/> call is never added to the
        /// <b>Nodes</b> collection.</para></remarks>

        private void ExpandArea(T node, double cost)
        {
            // get valid neighbors of current node
            IList <T> neighbors = Graph.GetNeighbors(node);

            // recurse into all valid neighbors
            for (int i = 0; i < neighbors.Count; i++)
            {
                T      neighbor = neighbors[i];
                double minCost;

                // skip nodes with better path
                _pathCosts.TryGetValue(neighbor, out minCost);
                if (minCost != 0 && minCost <= cost)
                {
                    continue;
                }

                // skip unreachable nodes
                if (!_agent.CanMakeStep(node, neighbor))
                {
                    continue;
                }

                // get cost for next movement step
                double stepCost = _agent.GetStepCost(node, neighbor);
                Debug.Assert(stepCost > 0);

                // skip unaffordable nodes
                if (!_agent.RelaxedRange && cost + stepCost > _maxCost)
                {
                    continue;
                }

                // skip nodes with better path
                if (minCost != 0 && minCost <= cost + stepCost)
                {
                    continue;
                }

                // add newly reached neighbor if possible
                if (minCost == 0 && _agent.CanOccupy(neighbor))
                {
                    _nodes.Add(neighbor);
                }

                // store new minimum path cost
                _pathCosts[neighbor] = cost + stepCost;

                // visit neighbors if still affordable
                if (cost + stepCost < _maxCost)
                {
                    ExpandArea(neighbor, cost + stepCost);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Adds all valid neighbors as children to the specified parent node.</summary>
        /// <param name="parent">
        /// The <see cref="PathNode{T}"/> whose neighbors to examine.</param>

        private void CreateChildren(PathNode <T> parent)
        {
            T source = parent.Node;

            // compute direct neighbors of parent node
            IList <T> neighbors = Graph.GetNeighbors(source);

            // link all children that can be reached
            for (int i = 0; i < neighbors.Count; i++)
            {
                T neighbor = neighbors[i];

                if (_agent.CanMakeStep(source, neighbor))
                {
                    LinkChild(parent, neighbor);
                }
            }
        }