Wikipedia 구현에 따른 A * 알고리즘을 here 에 구현했습니다. 그러나 모바일 장치에서 실행하기에는 너무 느립니다. 데스크톱 컴퓨터에서 제대로 실행되지만 기능을 끝내기 위해 끝없는 시간을 기다려야합니다. 알고리즘을 최적화하기 위해 할 수있는 일이 있습니까?A-Star 알고리즘 최적화
여기에 A * 좋은 휴리스틱 알고리즘,하지만 당신은 아마 너무 최적화를 필요로하는 실제 코드를
public DriveRoute findroute(routenode from, routenode to)
{
Dictionary<int, routenode> openlist = new Dictionary<int, routenode>();
openlist.Add(from.nodeid, from);
Dictionary<int, routenode> closedlist = new Dictionary<int, routenode>();
Dictionary<int, double> gscores = new Dictionary<int, double>();
gscores.Add(from.nodeid, 0);
Dictionary<int, double> hscores = new Dictionary<int, double>();
hscores.Add(from.nodeid, distanceForRouting(from.latlon, to.latlon));
Dictionary<int, double> fscores = new Dictionary<int, double>();
fscores.Add(from.nodeid, gscores[from.nodeid] + hscores[from.nodeid]);
Dictionary<int, routenode> came_from = new Dictionary<int, routenode>();
while (openlist.Values.Count > 0)
{
routenode x = getLowestFscore(openlist, fscores);
if (x.latlon.Equals(to.latlon))
{
return rebuildPathWay(came_from, to);
}
openlist.Remove(x.nodeid);
closedlist.Add(x.nodeid, x);
foreach (routearc arc in x.outgoingarcs)
{
if (closedlist.Keys.Contains(arc.endnode))
continue;
double tentative_g_score = gscores[x.nodeid] + arc.time;
bool tentative_is_better = false;
if (!openlist.Keys.Contains(arc.endnode))
{
openlist.Add(arc.endnode, map.routenodes[arc.endnode]);
tentative_is_better = true;
}
else if (tentative_g_score < gscores[arc.endnode])
{
tentative_is_better = true;
}
if (tentative_is_better)
{
if (came_from.ContainsKey(arc.endnode))
came_from[arc.endnode] = x;
else
came_from.Add(arc.endnode, x);
gscores[arc.endnode] = tentative_g_score;
hscores[arc.endnode] = distanceForRouting(arc.endlatlon, to.latlon);
fscores[arc.endnode] = gscores[arc.endnode] + hscores[arc.endnode];
}
}
}
return null;
}
프로파일을 만들었습니까? 병목 현상이 무엇입니까? – Oded
Dictionary 클래스처럼 보이고 getLowestFscore()는 병 목입니다. getLowestFscore는 for 루프가 FScore가 가장 낮은 루프 일뿐입니다. 무엇으로 대체해야합니까? – VOX
모바일 장치가 처리 할 데이터가 너무 많습니다. 얼마나 많은 노드가 있습니까? 어쩌면 사전에 사용하고있는 데이터를 최소한으로 나누어야합니다. – MicSim