当前位置: 首页 > news >正文

做网站的科技公司百度云资源搜索入口

做网站的科技公司,百度云资源搜索入口,定制手机微网站,app定制公司本实验寻路算法均基于网格实现,整体称呼为Grid,单个瓦片称之为Tile 考虑程序处理的简洁性,所有算法使用同一种Tile,且权值点,A*所需的记录数值也全部放在Tile中记录 前排贴上代码仓库链接: GitHub - Sir…

本实验寻路算法均基于网格实现,整体称呼为Grid,单个瓦片称之为Tile

考虑程序处理的简洁性,所有算法使用同一种Tile,且权值点,A*所需的记录数值也全部放在Tile中记录

前排贴上代码仓库链接:

GitHub - Sirokus/Unity-Pathfinding-Project: 内置了BFS,DFS,GBFS,Dijkstra,A*有权,A*无权的寻路函数,以及一个能够控制大小,障碍物,起始点和终点的网格系统,仅供个人了解原理使用

数据结构

首先是Tile的数据结构

已剪除不必要的代码保证逻辑清晰

public class Tile : MonoBehaviour
{public bool walkable;public bool visited;public Tile pre;public float cost = 1;//A*所需public float G;public float H;public float F => G + H;public void setWalkable(bool canWalk){walkable = canWalk;}public int GetManhattanDistance(Tile tile){Vector2Int aCoord = Grid.getCoordByTile(this);Vector2Int bCoord = Grid.getCoordByTile(tile);return Mathf.Abs(aCoord.x - bCoord.x) + Mathf.Abs(aCoord.y - bCoord.y);}
}

接着是Grid

鉴于Grid中集中了大量业务代码,此处只贴出部分有用的函数内容

Grid中存储着所有的格子,并提供格子和索引之间的转换,以下是具体方式

public static Vector2Int getCoordByTile(Tile tile)
{return new Vector2Int((int)tile.transform.localPosition.x, (int)tile.transform.localPosition.y);
}
public static Tile getTileByCoord(Vector2Int coord)
{if (coord.x < 0 || coord.y < 0 || coord.x >= Grid.ins.gridSize.x || coord.y >= Grid.ins.gridSize.y)return null;return Grid.ins.tiles[coord.x][coord.y];
}

接着是关键的寻路代码

BFS寻路算法

以下是BFS代码(为了能够运行时观看寻路过程,因此所有寻路代码皆使用协程实现)

BFS的基本思想是从出发点开始,向四周所有可以行走的节点进行访问,并在碰到目标值后停止

BFS是不考虑权值的

IEnumerator BfsPathFindingTask()
{Queue<Tile> queue = new Queue<Tile>();                              //遍历队列Dictionary<Tile, Tile> came_from = new Dictionary<Tile, Tile>();    //前驱队列queue.Enqueue(start);came_from[start] = null;while(queue.Count > 0){//访问当前TileTile cur = queue.Dequeue();Vector2Int curCoord = getCoordByTile(cur);//依次访问其四个邻居,若可行走且未被访问过则入队foreach(var d in dir){//获取邻居坐标Vector2Int tmp = curCoord + d;//按坐标获取邻居Tile neighbor = getTileByCoord(tmp);//确保邻居可访问且没有前驱(没有访问过)if (neighbor && neighbor.walkable && !came_from.ContainsKey(neighbor)){if(neighbor != end){neighbor.setColor(Color.black, VisitedColorBlendLerp);}//入队该邻居,标记已访问,记录其父tilequeue.Enqueue(neighbor);came_from[neighbor] = cur;//终止条件if(CheckFindPathComplete(neighbor, came_from))yield break;}}if(ShowProcess)yield return new WaitForSeconds(0.001f / (0.001f * speedMultiple));}
}

DFS寻路算法

我写的这个就是纯搞笑的算法,它会向一个方向一直探测转向直到走无可走再从下一个相邻点进行行走

也就是说这个不是一个启发式算法,目标肯定是可以找到的,但中间要走多少个弯就只有天知道了

(不过review一下发现写的还挺长的)

IEnumerator DfsPathFindingTask(Tile tile, System.Action isDone)
{if(end.visited || !tile){isDone();yield break;}tile.visited = true;Vector2Int cur = getCoordByTile(tile);Tile neighbor = null;float minDis = float.MaxValue;foreach(var d in dir){Vector2Int tmp = cur + d;Tile tmpTile = getTileByCoord(tmp);if(tmpTile && tmpTile.walkable && !tmpTile.visited){float dis = Vector2.Distance(tmp, endCoord);if(dis < minDis){neighbor = tmpTile;minDis = dis;}}}  if(neighbor){if(neighbor != end){neighbor.setColor(Color.black, VisitedColorBlendLerp);}neighbor.visited = true;neighbor.pre = tile;if(neighbor == end){float costCount = neighbor.cost;neighbor = neighbor.pre;List<Tile> path = new List<Tile>();while(neighbor != start){costCount += neighbor.cost;path.Add(neighbor);neighbor = neighbor.pre;}costCount += start.cost;Debug.Log(costCount);path.Reverse();foreach(Tile t in path){t.setColor(Color.green, 0.5f);yield return new WaitForSeconds(0.02f);}yield break;}if(ShowProcess)yield return new WaitForSeconds(0.01f / (0.01f * speedMultiple));bool isdone = false;coroutines.Add(StartCoroutine(DfsPathFindingTask(neighbor, () => { isdone = true; })));yield return new WaitUntil(() => isdone);}else{bool isdone = false;coroutines.Add(StartCoroutine(DfsPathFindingTask(tile.pre, () => { isdone = true; })));yield return new WaitUntil(() => isdone);}isDone();
}

GBFS寻路算法

GBFS是在BFS基础上进行改进的算法,G代表Greedy(贪心),这是一个启发式算法,这意味着其知道终点的位置,并且会一直向理论最靠近终点的位置靠近

这也代表如果其和目标中间有个墙角,其也会先深入墙角再沿着墙走出来找到目标(因为其不考虑已付出代价)

        IEnumerator GBfsPathFindingTask(){SortedDictionary<float, LinkedList<Tile>> queue = new SortedDictionary<float, LinkedList<Tile>>(){{0, new LinkedList<Tile>()}};Dictionary<Tile, Tile> came_from = new Dictionary<Tile, Tile>();queue[0].AddLast(start);came_from[start] = null;Vector2Int endCoord = getCoordByTile(end);while(queue.Count > 0){//访问当前TileTile cur = queue.First().Value.First();//当前Tile出队if(queue.First().Value.Count > 1)queue.First().Value.RemoveFirst();elsequeue.Remove(queue.First().Key);//获取当前Tile坐标Vector2Int curCoord = getCoordByTile(cur);//依次访问其四个邻居,若可行走且未被访问过则入队foreach(var d in dir){//计算邻居坐标Vector2Int tmp = curCoord + d;//按坐标获取邻居Tile neighbor = getTileByCoord(tmp);//确保邻居可访问if (neighbor && neighbor.walkable && !came_from.ContainsKey(neighbor)){//计算优先级float priority = Vector2Int.Distance(tmp, endCoord);//入队if(!queue.ContainsKey(priority))queue.Add(priority, new LinkedList<Tile>());queue[priority].AddLast(neighbor);//设置前驱came_from[neighbor] = cur;//终止条件if(CheckFindPathComplete(neighbor, came_from))yield break;}}if(ShowProcess)yield return new WaitForSeconds(0.01f / (0.01f * speedMultiple));}}

Dijkstra寻路算法

该算法不是一个启发式算法,但该算法能够根据不同权值找到最优路径(一定最优),其几乎相当于一个广度优先的有权版本,其会评估路上每一个的已付出代价,并选择付出代价最小的节点进行扩展,当找到一个已访问结点的更优路径时,还会修改其前驱

IEnumerator DijkstraPathFindingTask()
{SortedDictionary<float, LinkedList<Tile>> queue = new SortedDictionary<float, LinkedList<Tile>>(){{0, new LinkedList<Tile>()}};Dictionary<Tile, Tile> came_from = new Dictionary<Tile, Tile>();Dictionary<Tile, float> cost_so_far = new Dictionary<Tile, float>();queue[0].AddLast(start);came_from[start] = null;cost_so_far[start] = 0;while(queue.Count > 0){//访问当前TileTile cur = queue.First().Value.First();//当前Tile出队if(queue.First().Value.Count > 1)queue.First().Value.RemoveFirst();elsequeue.Remove(queue.First().Key);//获取当前Tile坐标Vector2Int curCoord = getCoordByTile(cur);//依次访问其四个邻居,若可行走且未被访问过则入队foreach(var d in dir){//计算邻居坐标Vector2Int tmp = curCoord + d;//按坐标获取邻居Tile neighbor = getTileByCoord(tmp);if(!neighbor)continue;//计算costfloat new_cost = cost_so_far[cur] + neighbor.cost;//可行走,且第一次走或者此为更优路线if (neighbor.walkable && (!cost_so_far.ContainsKey(neighbor) || new_cost < cost_so_far[neighbor])){if(neighbor != end){neighbor.setColor(Color.black, VisitedColorBlendLerp);}//入队if(!queue.ContainsKey(new_cost))queue.Add(new_cost, new LinkedList<Tile>());queue[new_cost].AddLast(neighbor);//设置前驱came_from[neighbor] = cur;//更新costcost_so_far[neighbor] = new_cost;//终止条件if(CheckFindPathComplete(neighbor, came_from))yield break;}}if(ShowProcess)yield return new WaitForSeconds(0.001f / (0.001f * speedMultiple));}
}

A*寻路算法

A*寻路算法是非常著名的寻路算法,其相当于一个混合算法,其存在一个启发函数

即 F = G + H + C

G = 已经付出的代价(类似Dijkstra)

H = 预估到达目标代价(类似GBFS)

C = 用户自定义算法(可用于实现一些A*寻路的特殊偏好,如减少拐点等)

A*的H计算可以计算欧式距离或者曼哈顿距离,通常使用后者,因为计算机计算起来比需要平方根的欧氏距离更快,方便大量计算

Tips:我在算法中,还额外使用了向量的叉乘,使得寻路时会更倾向于扩展朝向目标的点而非扩展更远的点

IEnumerator AStarPathFindingTask()
{SortedDictionary<float, LinkedList<Tile>> openQueue = new SortedDictionary<float, LinkedList<Tile>>(){{0, new LinkedList<Tile>()}};Dictionary<Tile, Tile> preDic = new Dictionary<Tile, Tile>();Dictionary<Tile, float> costDic = new Dictionary<Tile, float>();//用start初始化容器openQueue[0].AddLast(start);preDic[start] = null;costDic[start] = 0;Vector2Int endCoord = getCoordByTile(end);bool wait;while(openQueue.Count > 0){wait = false;//访问当前TileTile cur = openQueue.First().Value.First();//当前Tile出队if(openQueue.First().Value.Count > 1)openQueue.First().Value.RemoveFirst();elseopenQueue.Remove(openQueue.First().Key);//获取当前Tile坐标Vector2Int curCoord = getCoordByTile(cur);//依次访问其四个邻居,若可行走且未被访问过则入队foreach(var d in dir){//计算邻居坐标Vector2Int tmp = curCoord + d;//按坐标获取邻居Tile neighbor = getTileByCoord(tmp);if(!neighbor)continue;//计算邻居的Costfloat new_cost = costDic[cur] + neighbor.cost;//可行走,且第一次走或者此为更优路线if (neighbor.walkable && (!costDic.ContainsKey(neighbor) || new_cost < costDic[neighbor])){if(neighbor != end){neighbor.setColor(Color.black, VisitedColorBlendLerp);}//更新cost(G)costDic[neighbor] = new_cost;//F = G+H,switch中主要是不同的H的计算方式switch(AStarType){case 0:new_cost += Vector2Int.Distance(tmp, endCoord);break;case 1:float dx = Mathf.Abs(tmp.x - endCoord.x);float dy = Mathf.Abs(tmp.y - endCoord.y);new_cost += dx + dy + (Mathf.Sqrt(2) - 2) * Mathf.Min(dx, dy);break;case 2:float dx1 = Mathf.Abs(tmp.x - endCoord.x);float dy1 = Mathf.Abs(tmp.y - endCoord.y);float dx2 = Mathf.Abs(startCoord.x - endCoord.x);float dy2 = Mathf.Abs(startCoord.y - endCoord.y);float cross = dx1 * dy2 - dx2 * dy1;new_cost += neighbor.GetManhattanDistance(end) + (cross < 0 ? (cross + 1) * -2 : cross) * AstarCrossMultiple;break;}//入队if(!openQueue.ContainsKey(new_cost))openQueue.Add(new_cost, new LinkedList<Tile>());openQueue[new_cost].AddLast(neighbor);//记录前驱preDic[neighbor] = cur;//终止条件if(CheckFindPathComplete(neighbor, preDic))yield break;wait = true;}}if(wait && ShowProcess)yield return new WaitForSeconds(0.001f / (0.001f * speedMultiple));}
}


文章转载自:
http://satisfying.spkw.cn
http://bijection.spkw.cn
http://effusion.spkw.cn
http://pastelist.spkw.cn
http://philistinism.spkw.cn
http://wonderworking.spkw.cn
http://neoplasty.spkw.cn
http://cispadane.spkw.cn
http://dickensian.spkw.cn
http://veronal.spkw.cn
http://belong.spkw.cn
http://clause.spkw.cn
http://screenwash.spkw.cn
http://sleepful.spkw.cn
http://geomorphology.spkw.cn
http://lowlife.spkw.cn
http://ordo.spkw.cn
http://accessorily.spkw.cn
http://longhair.spkw.cn
http://disennoble.spkw.cn
http://hatchery.spkw.cn
http://somesthetic.spkw.cn
http://manhole.spkw.cn
http://nanchang.spkw.cn
http://differentiable.spkw.cn
http://precompiler.spkw.cn
http://diaphragmatic.spkw.cn
http://incantation.spkw.cn
http://dialogist.spkw.cn
http://sincere.spkw.cn
http://textural.spkw.cn
http://badger.spkw.cn
http://enshield.spkw.cn
http://tripersonal.spkw.cn
http://presentment.spkw.cn
http://colorplate.spkw.cn
http://deflective.spkw.cn
http://romany.spkw.cn
http://chino.spkw.cn
http://humped.spkw.cn
http://roburite.spkw.cn
http://conveyer.spkw.cn
http://diminishable.spkw.cn
http://unpierceable.spkw.cn
http://payable.spkw.cn
http://lilliput.spkw.cn
http://leachable.spkw.cn
http://one.spkw.cn
http://tsuris.spkw.cn
http://dovelet.spkw.cn
http://pedosphere.spkw.cn
http://excessively.spkw.cn
http://rebop.spkw.cn
http://exploit.spkw.cn
http://weser.spkw.cn
http://parylene.spkw.cn
http://quinquecentennial.spkw.cn
http://collegiality.spkw.cn
http://akyab.spkw.cn
http://karachai.spkw.cn
http://suppliance.spkw.cn
http://suitable.spkw.cn
http://tipple.spkw.cn
http://hypercomplex.spkw.cn
http://squamulose.spkw.cn
http://compositor.spkw.cn
http://fusspot.spkw.cn
http://mopoke.spkw.cn
http://leucocytosis.spkw.cn
http://reascend.spkw.cn
http://ploughhead.spkw.cn
http://transshape.spkw.cn
http://miserly.spkw.cn
http://photodissociation.spkw.cn
http://cerated.spkw.cn
http://chunderous.spkw.cn
http://opportunism.spkw.cn
http://tabasheer.spkw.cn
http://deepie.spkw.cn
http://ungual.spkw.cn
http://folivore.spkw.cn
http://unvaried.spkw.cn
http://clavecinist.spkw.cn
http://edgy.spkw.cn
http://aeroplane.spkw.cn
http://missileman.spkw.cn
http://texture.spkw.cn
http://ots.spkw.cn
http://bypass.spkw.cn
http://unaccommodated.spkw.cn
http://cankered.spkw.cn
http://rozzer.spkw.cn
http://humous.spkw.cn
http://immaterial.spkw.cn
http://sluttery.spkw.cn
http://inquest.spkw.cn
http://hurdler.spkw.cn
http://centigram.spkw.cn
http://cull.spkw.cn
http://catecheticel.spkw.cn
http://www.15wanjia.com/news/81531.html

相关文章:

  • 外贸网站用什么语言百度信息
  • 自助游戏充值网站怎么做seo关键词优化排名推广
  • 企业做app好还是网站好腾讯企点账户中心
  • 做美食直播哪个网站好关于手机的软文营销
  • wordpress绑定外部域名贵港网站seo
  • 做好网站如何发布怎么开通百度推广账号
  • 电商网站设计方案大全seo优化外包
  • 郑州 (网站建设百度竞价和优化的区别
  • 眉山网站建设公司专业搜索引擎seo公司
  • 郑州做音响网站的公司免费投放广告的平台
  • 网站设计与建设难吗什么是营销型网站?
  • .net wap网站模板如何自己创建网址
  • 法律垂直问答网站怎样做百度统计
  • 叮当app制作平台下载石家庄百度搜索引擎优化
  • 中国新闻网今日最新消息抖音搜索seo排名优化
  • 广州网站建设 易企建站在百度做广告多少钱
  • 创意礼品做的比较好的网站网络推广和seo
  • 做网站怎么电话约客户sem和seo的区别
  • 网站开发难吗百度seo网站
  • 网站设计时图片怎么做百度小说风云排行榜
  • 做网站排名需要多少钱广东广州网点快速网站建设
  • 长春网站z制作如何在网络上推广产品
  • 新闻网站如何做原创内容青岛官网seo
  • 网站建设管理工作经验介绍中国网站排名网
  • 装修网站源码百度网盘下载慢怎么解决
  • 美妆网站制作教程长沙网络优化产品
  • 网站建设维护的方案怎样在网上做推广
  • 创建官方网站新东方留学机构官网
  • 专业网站设计联系电话免费友情链接网
  • 国内 上市网站建设公司模板建站网页