public Node[] SolveFirst(Vector2 vqinit, Vector2 vqend)
        {
            QuadTreeArea currentArea = new QuadTreeArea(Math.Min(vqinit.X, vqend.X),
                                                Math.Max(vqinit.Y, vqend.Y),
                                                Math.Max(vqinit.X, vqend.X),
                                                Math.Min(vqinit.Y, vqend.Y));

            Node qend = new Node { Position = vqend};
            Node qinit = new Node { Position = vqinit };
            qinit.Heuristic = (vqend - vqinit).Module();

            currentArea.Add(qinit);
            ProblemContext.onNewNode(qinit);

            int CurrentIteration = 0;
            while (CurrentIteration < ProblemContext.MaxGenerations)
            {
                Node bestNode = currentArea.GetBestNodeFromMostUndenseArea();
                Node newNode = bestNode.TryExpand(qend);

                if (currentArea.Parent != null)
                    currentArea = currentArea.Antecesor;

                if (newNode == qend)
                {
                    return newNode.Predecesors.ToArray();
                }
                CurrentIteration++;
            }

            return null;
        }
Ejemplo n.º 2
0
        public Node[] SolveFirst(Vector2 vqinit, Vector2 vqend)
        {
            QuadTreeArea currentArea = new QuadTreeArea(Math.Min(vqinit.X, vqend.X),
                                                        Math.Max(vqinit.Y, vqend.Y),
                                                        Math.Max(vqinit.X, vqend.X),
                                                        Math.Min(vqinit.Y, vqend.Y));


            Node qend = new Node {
                Position = vqend
            };
            Node qinit = new Node {
                Position = vqinit
            };

            qinit.Heuristic = (vqend - vqinit).Module();

            currentArea.Add(qinit);
            ProblemContext.onNewNode(qinit);

            int CurrentIteration = 0;

            while (CurrentIteration < ProblemContext.MaxGenerations)
            {
                Node bestNode = currentArea.GetBestNodeFromMostUndenseArea();
                Node newNode  = bestNode.TryExpand(qend);

                if (currentArea.Parent != null)
                {
                    currentArea = currentArea.Antecesor;
                }

                if (newNode == qend)
                {
                    return(newNode.Predecesors.ToArray());
                }
                CurrentIteration++;
            }

            return(null);
        }
Ejemplo n.º 3
0
        public void Add(Node node)
        {
            if (this.Cover(node))
            {
                if (IsFinalNode)
                {
                    if (Nodes.Count == ProblemContext.NodesPerArea && Area >= ProblemContext.MinAreaSize)
                    {
                        //SUBDIVISION
                        subdivide();
                        Nodes.Add(node);

                        foreach (Node n in Nodes)
                        {
                            QuadTreeArea selectedArea = GetCoveringChildArea(n);
                            selectedArea.Add(n);
                        }

                        this.Nodes.Clear();
                        this.Nodes = null;
                    }
                    else
                    {
                        //NORMAL ADDITION
                        //BASE CASE

                        Nodes.Add(node);
                        node.Area = this;
                        if (this.BestNode == null || node.PathQualityEstimation < this.BestNode.PathQualityEstimation)
                        {
                            this.BestNode = node;
                        }
                    }
                }
                else
                {
                    //RECURSIVE DOWN CASE
                    GetCoveringChildArea(node).Add(node);
                }
            }
            else
            {
                if (this.Parent == null)
                {
                    bool north = node.Position.Y > this.Top;
                    bool west  = node.Position.X < this.Left;
                    bool south = node.Position.Y < this.Bottom;
                    bool east  = node.Position.X > this.Right;

                    double width  = this.Width;
                    double height = this.Height;

                    if (south || east)
                    {
                        Parent       = new QuadTreeArea(this.Left, this.Top, this.Right + width, this.Bottom - height);
                        Parent.NWest = this;
                    }
                    else if (north || west)
                    {
                        Parent       = new QuadTreeArea(this.Left - width, this.Top + height, this.Right, this.Bottom);
                        Parent.SEast = this;
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }

                    this.Name = (north ? "N" : "S") + (east ? "E" : "W");
                    Parent.subdivide();
                }

                this.Parent.Add(node);
            }
        }