我试图将JPS集成到我的A *算法中,但是性能下降了。阅读GitHub中的文档和示例,目前感到困惑,为什么工作缓慢。检查或添加到某些列表时应纠正什么?也许我不了解这种算法的全部含义。
这是我修改了Astar的功能:
public List<PathFinderNode> FindPath(Point start, Point end, List<Point> ResultPoints)
{
PathFinderNode parentNode;
bool found = false;
int gridX = mGrid[0].Count - 1;//horizontal size of grid
int gridY = mGrid.Count - 1; //vertical size of grid
mStopped = false;
mOpen.Clear();
mClose.Clear();
parentNode.G = 0;
parentNode.H = mHEstimate; //Equals 2
parentNode.F = parentNode.G + parentNode.H;
parentNode.X = start.X;
parentNode.Y = start.Y;
parentNode.PX = parentNode.X;
parentNode.PY = parentNode.Y;
mOpen.Push(parentNode); //Add a ParentNode First
while (mOpen.Count > 0)
{
parentNode = mOpen.Pop();
if (parentNode.X == end.X && parentNode.Y == end.Y)
{
mClose.Add(parentNode);
found = true;
break;
}
if (mClose.Count > mSearchLimit)
{
mStopped = true;
return null;
}
//Checking Neibours
for (int i = 0; i < 4; i++)
{
PathFinderNode newNode;
newNode.X = parentNode.X + direction[i, 0];
newNode.Y = parentNode.Y + direction[i, 1];
var tmpXY = jump(newNode.X, newNode.Y, parentNode.X, parentNode.Y, start,end); //doing a jump action
if (tmpXY[0] != -1) //if jump returns more than -1
{
int newG;
newNode.X = tmpXY[0];
newNode.Y = tmpXY[1];
newG = parentNode.G + (-1) * mGrid[newNode.Y][newNode.X];
if (newG == parentNode.G)
{
continue;
}
int foundInOpenIndex = -1;
for (int j = 0; j < mOpen.Count; j++) //openList check
{
if (mOpen[j].X == newNode.X && mOpen[j].Y == newNode.Y)
{
foundInOpenIndex = j;
break;
}
}
if (foundInOpenIndex != -1 && mOpen[foundInOpenIndex].G <= newG)
continue;
int foundInCloseIndex = -1;
for (int j = 0; j < mClose.Count; j++) //ClosedListCheck
{
if (mClose[j].X == newNode.X && mClose[j].Y == newNode.Y)
{
foundInCloseIndex = j;
continue;
}
}
if (foundInCloseIndex != -1 && mClose[foundInCloseIndex].G <= newG)
continue;
newNode.PX = parentNode.X;
newNode.PY = parentNode.Y;
newNode.G = newG;
newNode.H = mHEstimate * (Math.Abs(newNode.X - end.X) + Math.Abs(newNode.Y - end.Y)); //Manhattan
newNode.F = newNode.G + newNode.H;
mOpen.Push(newNode);
}
}
mClose.Add(parentNode);
}
if (found)
{
PathFinderNode fNode = mClose[mClose.Count - 1];
for (int i = mClose.Count - 1; i >= 0; i--)
{
if (fNode.PX == mClose[i].X && fNode.PY == mClose[i].Y || i == mClose.Count - 1)
{
fNode = mClose[i];
ResultPoints.Add(new Point(fNode.X * 10, fNode.Y * 10));
}
else
mClose.RemoveAt(i);
}
mStopped = true;
return mClose;
}
mStopped = true;
return null;
}