public override AlgorithmResults RunAlgorithm()
        {
            startTime = DateTime.Now;

            start.H = H(start, start, goal);
            start.G = 0;
            start.F = start.G + start.H;


            start.Parent = start;
            fringe.Push(start, start.G + H(start, start, goal));

            while (!fringe.IsEmpty)
            {
                Cell s = fringe.Pop();
                isInFringe[s.X, s.Y] = false;

                if (s.Equals(goal))
                {
                    elapsedTime = DateTime.Now.Subtract(startTime);
                    return(getResults());
                }

                closedArray[s.X, s.Y] = true;
                nodesExpanded++;

                Cell[] sprimes = AlgorithmFormulas.Successors(s, cellGrid);
                for (int i = 0; i < 8; i++)
                {
                    if (sprimes[i] == null)
                    {
                        continue;
                    }

                    if (!closedArray[sprimes[i].X, sprimes[i].Y])
                    {
                        if (!isInFringe[sprimes[i].X, sprimes[i].Y])
                        {
                            sprimes[i].G      = decimal.MaxValue;
                            sprimes[i].Parent = null;
                        }
                        UpdateVertex(s, sprimes[i]);
                    }
                }
            }

            elapsedTime = DateTime.Now.Subtract(startTime);
            return(new AlgorithmResults {
                Success = false
            });
        }
Ejemplo n.º 2
0
        public void ExpandState(Cell s, int i)
        {
            Cell[] sprimes = AlgorithmFormulas.Successors(s, cellGrid);
            for (int k = 0; k < sprimes.Length; k++)
            {
                if (sprimes[k] == null)
                {
                    continue;
                }

                Cell    sprime = sprimes[k];
                decimal cost   = cellData[i][s.X, s.Y].G + AlgorithmFormulas.C(s, sprime);

                if (!itTracker[i][sprime.X, sprime.Y])
                {
                    cellData[i][sprime.X, sprime.Y].G      = decimal.MaxValue;
                    cellData[i][sprime.X, sprime.Y].Parent = null;

                    itTracker[i][sprime.X, sprime.Y] = true;
                }
                if (cellData[i][sprime.X, sprime.Y].G > cost)
                {
                    cellData[i][sprime.X, sprime.Y].G      = cost;
                    cellData[i][sprime.X, sprime.Y].Parent = vectorOf(s);

                    if (!closedList[i][sprime.X, sprime.Y])
                    {
                        if (isInFringe[i][sprime.X, sprime.Y])
                        {
                            fringe[i].Remove(sprime);
                        }
                        fringe[i].Push(sprime, Key(vectorOf(sprime), i));
                        isInFringe[i][sprime.X, sprime.Y] = true;
                    }
                }
            }
        }