Example #1
0
        public Node TryExpand(Node qend)
        {
            bool   found    = false;
            Node   n        = new Node();
            bool   solution = false;
            Random r        = new Random(Environment.TickCount);


            if (candidateDisplacement == null)
            {
                candidateDisplacementGenerator = new randomExpansionIterator {
                    q = this.Position, qend = qend.Position, maxVectorModule = 50
                };
                candidateDisplacement = candidateDisplacementGenerator
                                        .GetVectors()
                                        .GetEnumerator();
            }



            int fails = 0;

            candidateDisplacement.MoveNext();


            while (!found && fails < ProblemContext.MaxAttemptsPerNodeExpansion)
            {
                Vector2 targetPoint;
                Vector2?collisionPoint;

                if (r.Next() % 3 == 0)
                {
                    targetPoint = qend.Position;
                }
                else
                {
                    QuadTreeArea CandidateArea;
                    do
                    {
                        targetPoint   = this.Position + candidateDisplacement.Current;
                        CandidateArea = this.Area.FindArea(targetPoint);
                        this.candidateDisplacement.MoveNext();
                    }while (CandidateArea != null && CandidateArea.Density > this.Area.Density);
                }



                ProblemContext.onCandidate(this, candidateDisplacement.Current);
                if (!ProblemContext.checkObstacle(targetPoint.X, targetPoint.Y))
                {
                    collisionPoint = ProblemContext.LocalPlanner(Position, targetPoint);

                    //no collision
                    if (collisionPoint == null)
                    {
                        Node parentForNewChild = this;

                        //OK
                        n.Position  = targetPoint;
                        n.Heuristic = 0;
                        n.Cost      = parentForNewChild.Cost + targetPoint.Module();
                        n.Parent    = parentForNewChild;
                        parentForNewChild.ChildrenCount++;



                        parentForNewChild.Area.Add(n);
                        ProblemContext.onNewNode(n);

                        found = true;

                        if (qend.Position.X == n.Position.X && qend.Position.Y == n.Position.Y)
                        {
                            solution = true;
                        }
                    }
                }
                if (!found)
                {
                    fails++;
                    //candidateDisplacementGenerator.maxVectorModule /= 2.0;
                }
            }

            if (solution)
            {
                qend.Parent = n;
                return(qend);
            }
            else
            {
                return(n);
            }
        }
        public Node TryExpand(Node qend)
        {
            bool found = false;
            Node n = new Node();
            bool solution = false;
            Random r=new Random(Environment.TickCount);

            if (candidateDisplacement == null)
            {
                candidateDisplacementGenerator= new randomExpansionIterator { q = this.Position, qend = qend.Position, maxVectorModule = 50 };
                candidateDisplacement = candidateDisplacementGenerator
                            .GetVectors()
                            .GetEnumerator();
            }

            int fails = 0;
            candidateDisplacement.MoveNext();

            while (!found && fails < ProblemContext.MaxAttemptsPerNodeExpansion)
            {
                Vector2 targetPoint;
                Vector2? collisionPoint;

                if (r.Next() % 3 == 0)
                    targetPoint = qend.Position;
                else
                {
                    QuadTreeArea CandidateArea;
                    do{
                        targetPoint = this.Position + candidateDisplacement.Current;
                        CandidateArea = this.Area.FindArea(targetPoint);
                        this.candidateDisplacement.MoveNext();
                    }
                    while (CandidateArea != null && CandidateArea.Density > this.Area.Density);
                }

                ProblemContext.onCandidate(this, candidateDisplacement.Current);
                if (!ProblemContext.checkObstacle(targetPoint.X , targetPoint.Y))
                {
                   collisionPoint= ProblemContext.LocalPlanner(Position,targetPoint );

                    //no collision
                    if (collisionPoint == null)
                    {
                        Node parentForNewChild = this;

                        //OK
                        n.Position = targetPoint;
                        n.Heuristic = 0;
                        n.Cost = parentForNewChild.Cost + targetPoint.Module();
                        n.Parent = parentForNewChild;
                        parentForNewChild.ChildrenCount++;

                        parentForNewChild.Area.Add(n);
                        ProblemContext.onNewNode(n);

                        found = true;

                        if (qend.Position.X == n.Position.X && qend.Position.Y == n.Position.Y)
                            solution = true;

                    }
                }
                if (!found)
                {
                    fails++;
                    //candidateDisplacementGenerator.maxVectorModule /= 2.0;
                }
            }

            if (solution)
            {
                qend.Parent = n;
                return qend;
            }
            else
                return n;
        }