c# – 优化A-Star算法

我已经实现了A *算法根据*在here的实现
但是,在移动设备上运行速度太慢.我必须等待无数个小时才能完成该功能,尽管它在桌面计算机上运行良好.我可以做些什么来优化算法?

这是实际的代码

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;
        }

解决方法:

没有看到整个代码很难给出任何提示,但我可能会给出一些提示:

你在字典上做的主要动作是找到成本最低的东西.应该针对此操作优化字典背后的数据结构.经典的数据结构将是一个堆(不是与new / delete malloc / free相关的东西,而是数据结构:http://en.wikipedia.org/wiki/Heap_%28data_structure%29)

你会发现像fibonacci-heaps这样的数据结构的子类型等等.值得尝试一下.如果没有实现A *我也会尝试使用splay-tree(在wiki上搜索会给你点击).

第二:在算法运行期间插入和删除节点吗?如果是这样,请确保自己构建一个预先分配的节点池并使用它而不是调用new / delete或malloc / free.内存分配可能非常慢.

上一篇:javascript – tv-tab-container js用于使用Closure库的工作选项卡


下一篇:JavaScript(select onchange)的网页跳转的简单实现