Ejemplo n.º 1
0
 protected abstract void OnPerformAlgorithm(TNode currentNode, TNode neighborNode, TValue neighborPosition, TValue endPosition, StopFunction <TValue> stopFunction);
Ejemplo n.º 2
0
 /// <summary>
 /// Enumerates the neighbors points for a given node.
 /// </summary>
 /// <param name="currentNode">The current node.</param>
 /// <param name="stopFunction">The stop function.</param>
 /// <returns></returns>
 protected abstract IEnumerable <TValue> OnEnumerateNeighbors(TNode currentNode, StopFunction <TValue> stopFunction);
        private static bool TryJump(Point sourcePoint, Point targetPoint, Point endPoint, StopFunction stopFunction, out Point jumpPoint)
        {
            int x = sourcePoint.X;
            int y = sourcePoint.Y;

            int deltaX = x - targetPoint.X;
            int deltaY = y - targetPoint.Y;

            jumpPoint = sourcePoint;

            if (stopFunction(sourcePoint.X, sourcePoint.Y))
            {
                return(false);
            }
            if (sourcePoint == endPoint)
            {
                return(true);
            }

            if (deltaX != 0 && deltaY != 0)
            {
                if (!stopFunction(x - deltaX, y + deltaY) && stopFunction(x - deltaX, y) ||
                    !stopFunction(x + deltaX, y - deltaY) && stopFunction(x, y - deltaY))
                {
                    return(true);
                }
            }
            else
            {
                if (deltaX != 0)
                {
                    if ((!stopFunction(x + deltaX, y + 1) && stopFunction(x, y + 1)) ||
                        !stopFunction(x + deltaX, y - 1) && stopFunction(x, y - 1))
                    {
                        return(true);
                    }
                }
                else
                {
                    if ((!stopFunction(x + 1, y + deltaY) && stopFunction(x + 1, y)) ||
                        !stopFunction(x - 1, y + deltaY) && stopFunction(x - 1, y))
                    {
                        return(true);
                    }
                }
            }

            if (deltaX != 0 && deltaY != 0)
            {
                Point dummyPoint;
                bool  leftJump   = TryJump(new Point(x + deltaX, y), sourcePoint, endPoint, stopFunction, out dummyPoint);
                bool  bottomJump = TryJump(new Point(x, y + deltaY), sourcePoint, endPoint, stopFunction, out dummyPoint);

                if (leftJump || bottomJump)
                {
                    return(true);
                }
            }

            if (!stopFunction(x + deltaX, y) || !stopFunction(x, y + deltaY))
            {
                return(TryJump(new Point(x + deltaX, y + deltaY), new Point(x, y), endPoint, stopFunction, out jumpPoint));
            }

            return(false);
        }
Ejemplo n.º 4
0
 public void set_train_stop_function(StopFunction train_stop_function)
 {
     fanndoublePINVOKE.neural_net_set_train_stop_function(swigCPtr, (int)train_stop_function);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnEnumerateNeighbors"/> for more details.
        /// </summary>
        protected override IEnumerable <WorldmapCluster> OnEnumerateNeighbors(WorldmapNode currentNode, StopFunction <WorldmapCluster> stopFunction)
        {
            List <WorldmapCluster> result = new List <WorldmapCluster>();

            var currentCluster      = new Cluster(currentNode.Value.Info);
            var currentClusterExits = currentCluster.GetExits();

            foreach (var exit in currentClusterExits)
            {
                if (exit.Kind != akf.Kind.Cluster)
                {
                    continue;
                }

                var exitCluster = _world.GetCluster(exit.Destination.Internal);

                if (exitCluster != null)
                {
                    result.Add(exitCluster);
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnEnumerateNeighbors"/> for more details.
        /// </summary>
        protected override IEnumerable <ClusterDescriptor> OnEnumerateNeighbors(WorldmapNode currentNode, StopFunction <ClusterDescriptor> stopFunction)
        {
            List <ClusterDescriptor> result = new List <ClusterDescriptor>();

            var currentCluster      = (ClusterDescriptor)currentNode.Value;
            var currentClusterExits = currentCluster.GetExits();

            foreach (var exit in currentClusterExits)
            {
                if (exit.GetKind() != ClusterExitKind.Cluster)
                {
                    continue;
                }

                ClusterDescriptor cluster = exit.GetDestination();
                if (cluster != null)
                {
                    result.Add(cluster);
                }
            }

            return(result);
        }
Ejemplo n.º 7
0
        private static DirectionType RotateUntil(Point startPoint, DirectionType direction, StopFunction stopFunction, bool leftSide, bool untilFree)
        {
            bool condition;

            do // rotates until the conditions are fullfilled (determined by untilFree)
            {
                direction = DirectionHelper.Rotate(direction, leftSide, false);
                Point nextLeftPoint = DirectionHelper.GetNextStep(startPoint, direction);
                condition = untilFree ? !stopFunction(nextLeftPoint.X, nextLeftPoint.Y) : stopFunction(nextLeftPoint.X, nextLeftPoint.Y);
            }while (!condition);

            return(direction);
        }
Ejemplo n.º 8
0
 public Task <string> StopRequestAsync(StopFunction stopFunction)
 {
     return(ContractHandler.SendRequestAsync(stopFunction));
 }
Ejemplo n.º 9
0
        /// <summary>
        /// See <see cref="BasePathfinder.OnTryFindPath"/> for more details.
        /// </summary>
        protected override bool OnTryFindPath(Point startPoint, Point endPoint,
                                              StopFunction stopFunction,
                                              out IReadOnlyCollection <Point> path,
                                              out IReadOnlyCollection <Point> pivotPoints,
                                              bool optimize = true)
        {
            // prepares main parameters
            bool         result    = false;
            List <Point> pointList = new List <Point>();

            // detects all the path segments that are passable
            IReadOnlyList <PathSegment> segments = LineRasterizer.ScanPathSegments(startPoint, endPoint, stopFunction);
            int  segmentIndex = 0;
            bool running      = true;

            while (running)
            {
                // gets this segment
                PathSegment segment        = segments[segmentIndex];
                Point       collisionPoint = segment.LastPoint;

                // adds already found points to a final path
                pointList.Add(segment.FirstPoint);
                pointList.Add(segment.LastPoint);

                // we have arrived at destination, we're done here
                if (collisionPoint == endPoint)
                {
                    result = true;
                    break;
                }

                // cirumvents the obstacle from both sides
                EvasionObstacleInfo obstacleInfo;

                // tries to circumvent the obstacle from both sides (left/right) to determine which on is shorter
                if (TryScanObstacle(collisionPoint, segment.Direction, stopFunction, segments, segmentIndex, out obstacleInfo))
                {
                    // adds better route points to our result structures, advances to the latest segment
                    segmentIndex += obstacleInfo.SegmentIndex + 1;

                    // determines left/right side paths step counts
                    int leftPathStepCount  = obstacleInfo.LeftStepCount;
                    int rightPathStepCount = obstacleInfo.TotalStepCount - leftPathStepCount;

                    // determines short path
                    IEnumerable <Point> shorterPath = leftPathStepCount < rightPathStepCount?
                                                      obstacleInfo.PivotPoints.Take(obstacleInfo.LefPointCount) :
                                                          obstacleInfo.PivotPoints.Skip(obstacleInfo.LefPointCount).Reverse();

                    // adds this path to overall path
                    pointList.AddRange(shorterPath);
                }
                else // path not found
                {
                    running = false;
                }
            }

            // returns the found path
            pivotPoints = pointList;
            path        = pointList;
            return(result);
        }
Ejemplo n.º 10
0
        private static bool TryScanObstacle(
            Point startPoint,                         // starting point (one before hitting the obstacle)
            DirectionType direction,                  // direction of hitting the obstacle
            StopFunction stopFunction,
            IEnumerable <PathSegment> segments,       // path segments detected from start to end
            int segmentIndex,                         // starting segment in which the obstacle was hit
            out EvasionObstacleInfo obstacleInfo)     // useful informations about obstacle, and optimal path around
        {
            // initializes the result structures
            bool         result          = false;
            List <Point> cornerPointList = new List <Point>();

            obstacleInfo = default(EvasionObstacleInfo);

            // detects all the starting points from relevant segments (to be tested for potential evading path end)
            IList <Point> finishPoints = segments.
                                         Skip(segmentIndex + 1).
                                         Select(segment => segment.FirstPoint).
                                         ToList();

            // initalizes the parameters
            int   oldSegmentIndex = segmentIndex;
            Point position        = startPoint;
            int   totalStepCount  = 0;

            // expected direction in which start point should be re-entered (this check is essential)
            DirectionType entryDirection = RotateUntil(startPoint, direction, stopFunction, false, true);

            entryDirection = DirectionHelper.Reverse(entryDirection);

            // rotates until the direction is no longer in a collision course (in a given hand side)
            direction = RotateUntil(startPoint, direction, stopFunction, true, true);

            do
            {
                // retrieves next point in a actual direction
                Point nextPoint = DirectionHelper.GetNextStep(position, direction);
                totalStepCount++;

                // we've ended up in the start position, that means we can terminate this scan
                if (nextPoint == startPoint && direction == entryDirection)
                {
                    break;
                }

                // detects whether this point coincides with any finish point
                int finishPointIndex = finishPoints.IndexOf(nextPoint);

                // we've ended up on ther other side, path was found, continue if we can find better finish point (in a higher segment)
                if (finishPointIndex >= 0 && finishPointIndex + oldSegmentIndex >= segmentIndex)
                {
                    obstacleInfo = new EvasionObstacleInfo(cornerPointList, totalStepCount, finishPointIndex);
                    result       = true;
                }

                // rotates (starting at opposite direction) from the wall until it finds a passable spot
                DirectionType previousDirection = direction;
                direction = RotateUntil(nextPoint, DirectionHelper.Reverse(direction), stopFunction, true, true);
                if (direction != previousDirection)
                {
                    cornerPointList.Add(nextPoint);
                }

                // advances to next point
                position = nextPoint;
            }while (true);

            // returns the points, and operation result
            if (result)
            {
                obstacleInfo.SetTotalStepCount(totalStepCount);
            }

            return(result);
        }
Ejemplo n.º 11
0
 public void set_train_stop_function(StopFunction train_stop_function)
 {
     fanndoublePINVOKE.neural_net_set_train_stop_function(swigCPtr, (int)train_stop_function);
 }
Ejemplo n.º 12
0
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnPerformAlgorithm"/> for more details.
        /// </summary>
        protected override void OnPerformAlgorithm(DijkstraNode currentNode, DijkstraNode neighborNode, Vector2 neighborPosition, Vector2 endPosition, StopFunction <Vector2> stopFunction)
        {
            Int32 neighborScore = currentNode.Score + GetNeighborDistance(currentNode.Value, neighborPosition);

            if (neighborNode == null)
            {
                Map.OpenNode(neighborPosition, currentNode, neighborScore);
            }
            else if (neighborScore < neighborNode.Score)
            {
                neighborNode.Update(neighborScore, currentNode);
            }
        }
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnEnumerateNeighbors"/> for more details.
        /// </summary>
        protected override IEnumerable <Point> OnEnumerateNeighbors(AStarNode currentNode, StopFunction stopFunction)
        {
            List <Point> result = new List <Point>();

            if (currentNode.Origin != null)
            {
                int x  = currentNode.Point.X;
                int y  = currentNode.Point.Y;
                int px = currentNode.Origin.Point.X;
                int py = currentNode.Origin.Point.Y;

                // get the normalized direction of travel
                int deltaX = (x - px) / Math.Max(Math.Abs(x - px), 1);
                int deltaY = (y - py) / Math.Max(Math.Abs(y - py), 1);

                // search diagonally
                if (deltaX != 0 && deltaY != 0)
                {
                    if (!stopFunction(x, y + deltaY))
                    {
                        result.Add(new Point(x, y + deltaY));
                    }

                    if (!stopFunction(x + deltaX, y))
                    {
                        result.Add(new Point(x + deltaX, y));
                    }

                    if (!stopFunction(x, y + deltaY) || !stopFunction(x + deltaX, y))
                    {
                        result.Add(new Point(x + deltaX, y + deltaY));
                    }

                    if (stopFunction(x - deltaX, y) && !stopFunction(x, y + deltaY))
                    {
                        result.Add(new Point(x - deltaX, y + deltaY));
                    }

                    if (stopFunction(x, y - deltaY) && !stopFunction(x + deltaX, y))
                    {
                        result.Add(new Point(x + deltaX, y - deltaY));
                    }
                }
                else // search horizontally/vertically
                {
                    if (deltaX == 0)
                    {
                        if (!stopFunction(x, y + deltaY))
                        {
                            result.Add(new Point(x, y + deltaY));

                            if (stopFunction(x + 1, y) && !stopFunction(x + 1, y + deltaY))
                            {
                                result.Add(new Point(x + 1, y + deltaY));
                            }

                            if (stopFunction(x - 1, y) && !stopFunction(x - 1, y + deltaY))
                            {
                                result.Add(new Point(x - 1, y + deltaY));
                            }
                        }
                    }
                    else
                    {
                        if (!stopFunction(x + deltaX, y))
                        {
                            result.Add(new Point(x + deltaX, y));

                            if (stopFunction(x, y + 1) && !stopFunction(x + deltaX, y + 1))
                            {
                                result.Add(new Point(x + deltaX, y + 1));
                            }
                            if (stopFunction(x, y - 1) && !stopFunction(x + deltaX, y - 1))
                            {
                                result.Add(new Point(x + deltaX, y - 1));
                            }
                        }
                    }
                }
            }
            else
            {
                result.AddRange(base.OnEnumerateNeighbors(currentNode, stopFunction));
            }

            return(result);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// See <see cref="IPathfinder.TryFindPath"/> for more details.
        /// </summary>
        protected virtual Boolean OnTryFindPath(TValue startValue, TValue endValue,
                                                StopFunction <TValue> stopFunction,
                                                out List <TValue> path,
                                                out List <TValue> pivotPoints,
                                                Boolean optimize = true)
        {
            // prepares main parameters
            Boolean result = false;

            pivotPoints = null;
            path        = null;

            // clears the map
            Map.Clear();

            // creates start/finish nodes
            TNode endNode = EndNode = Map.CreateEmptyNode(endValue);

            // prepares first node
            Map.OpenFirstNode(startValue, endValue);

            var depth = 0;

            while (Map.OpenCount > 0)
            {
                TNode currentNode = Map.CloseTopNode();

                // if current node is obstacle, skip it
                if (stopFunction(currentNode.Value))
                {
                    continue;
                }

                if (depth++ > (600 * 600))
                {
                    return(false);
                }

                // if we've detected end node, reconstruct the path back to the start
                if (currentNode.Equals(endNode))
                {
                    path   = ReconstructPath(endValue);
                    result = true;
                    break;
                }

                // processes all the neighbor points
                foreach (TValue neighborPoint in OnEnumerateNeighbors(currentNode, stopFunction))
                {
                    // if this neighbor is obstacle skip it, it is not viable node
                    if (stopFunction(neighborPoint))
                    {
                        continue;
                    }

                    // determines the node if possible, whether it is closed, and calculates its score
                    TNode neighborNode = default(TNode);

                    if (Map.Nodes.TryGetValue(neighborPoint, out neighborNode))
                    {
                        Boolean inClosedSet = neighborNode.IsClosed;

                        // if this node was already processed, skip it
                        if (inClosedSet)
                        {
                            continue;
                        }
                    }

                    // performs the implementation specific variant of graph search algorithm
                    OnPerformAlgorithm(currentNode, neighborNode, neighborPoint, endValue, stopFunction);
                }
            }

            return(result);
        }
 /// <summary>
 /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnPerformAlgorithm"/> for more details.
 /// </summary>
 protected override void OnPerformAlgorithm(SimpleNode currentNode, SimpleNode neighborNode, Point neighborPoint, Point endPoint, StopFunction stopFunction)
 {
     if (neighborNode == null)
     {
         Map.OpenNode(neighborPoint, currentNode);
     }
 }
Ejemplo n.º 16
0
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnPerformAlgorithm"/> for more details.
        /// </summary>
        protected override void OnPerformAlgorithm(DijkstraNode currentNode, DijkstraNode neighborNode, Point neighborPoint, Point endPoint, StopFunction stopFunction)
        {
            int neighborScore = HeuristicHelper.FastEuclideanDistance(neighborPoint, endPoint);

            if (neighborNode == null)
            {
                Map.OpenNode(neighborPoint, currentNode, neighborScore);
            }
            else if (neighborScore < neighborNode.Score)
            {
                neighborNode.Update(neighborScore, currentNode);
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Enumerates the neighbors points for a given node.
 /// </summary>
 /// <param name="currentNode">The current node.</param>
 /// <param name="stopFunction">The stop function.</param>
 /// <returns></returns>
 protected override IEnumerable <Vector2> OnEnumerateNeighbors(TNode currentNode, StopFunction <Vector2> stopFunction)
 {
     return(Directions.
            // creates next step in this direction from current position
            Select(direction => DirectionHelper.GetNextStep(currentNode.Value, direction)));
 }
Ejemplo n.º 18
0
 public Task <TransactionReceipt> StopRequestAndWaitForReceiptAsync(StopFunction stopFunction, CancellationTokenSource cancellationToken = null)
 {
     return(ContractHandler.SendRequestAndWaitForReceiptAsync(stopFunction, cancellationToken));
 }
Ejemplo n.º 19
0
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnEnumerateNeighbors"/> for more details.
        /// </summary>
        protected override IEnumerable <Vector2> OnEnumerateNeighbors(AStarNode currentNode, StopFunction <Vector2> stopFunction)
        {
            List <Vector2> result = new List <Vector2>();

            bool enumerateNeightbors = true;

            if (enumerateNeightbors)
            {
                result.AddRange(base.OnEnumerateNeighbors(currentNode, stopFunction));
            }

            return(result);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnPerformAlgorithm"/> for more details.
        /// </summary>
        protected override void OnPerformAlgorithm(AStarNode currentNode, AStarNode neighborNode, Vector2 neighborPosition, Vector2 endPosition, StopFunction <Vector2> stopFunction)
        {
            Int32 neighborScore = currentNode.Score + GetNeighborDistance(currentNode.Value, neighborPosition);

            // opens node at this position
            if (neighborNode == null)
            {
                Map.OpenNode(neighborPosition, currentNode, neighborScore, neighborScore + HeuristicHelper.FastEuclideanDistance(neighborPosition, endPosition));
            }
            else if (neighborScore < neighborNode.Score)
            {
                neighborNode.Update(neighborScore, neighborScore + HeuristicHelper.FastEuclideanDistance(neighborPosition, endPosition), currentNode);
            }
        }
Ejemplo n.º 21
0
        public StopFunction get_train_stop_function()
        {
            StopFunction ret = (StopFunction)fanndoublePINVOKE.neural_net_get_train_stop_function(swigCPtr);

            return(ret);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnPerformAlgorithm"/> for more details.
        /// </summary>
        protected override void OnPerformAlgorithm(WorldmapNode currentNode, WorldmapNode neighborNode, ClusterDescriptor neighborPosition, ClusterDescriptor endPosition, StopFunction <ClusterDescriptor> stopFunction)
        {
            Int32 neighborScore = currentNode.Score + GetScore(currentNode.Value, neighborPosition);

            // opens node at this position
            if (neighborNode == null)
            {
                Map.OpenNode(neighborPosition, currentNode, neighborScore, neighborScore);
            }
            else if (neighborScore < neighborNode.Score)
            {
                neighborNode.Update(neighborScore, neighborScore, currentNode);
            }
        }
        /// <summary>
        /// See <see cref="BaseGraphSearchPathfinder{TNode,TMap}.OnPerformAlgorithm"/> for more details.
        /// </summary>
        protected override void OnPerformAlgorithm(AStarNode currentNode, AStarNode neighborNode, Point neighborPoint, Point endPoint, StopFunction stopFunction)
        {
            Point jumpPoint;

            if (TryJump(neighborPoint, currentNode.Point, endPoint, stopFunction, out jumpPoint))
            {
                AStarNode jumpNode = Map[jumpPoint.X, jumpPoint.Y];

                int distance  = HeuristicHelper.FastEuclideanDistance(currentNode.Point, jumpPoint);
                int jumpScore = currentNode.Score + distance;

                if (jumpNode == null)
                {
                    Map.OpenNode(jumpPoint, currentNode, jumpScore, jumpScore + HeuristicHelper.FastEuclideanDistance(jumpPoint, endPoint));
                }
                else if (jumpScore < jumpNode.Score)
                {
                    if (jumpNode.IsClosed)
                    {
                        return;
                    }

                    jumpNode.Update(jumpScore, jumpScore + HeuristicHelper.FastEuclideanDistance(jumpPoint, endPoint), currentNode);
                }
            }
        }