├── A_star ├── 7.png ├── GeekLogo.ico ├── README.md ├── __pycache__ │ ├── mainWindow.cpython-39.pyc │ └── main_Ui.cpython-39.pyc ├── main.py ├── mainWindow.py ├── main_Ui.py ├── main_Ui.ui ├── map.txt └── map │ └── map.txt ├── LICENSE └── README.md /A_star/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Bsheepcoder/Search_algorithm_demonstration/5bb69383565c54c96efb7b14cbb300b1d36e33a0/A_star/7.png -------------------------------------------------------------------------------- /A_star/GeekLogo.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Bsheepcoder/Search_algorithm_demonstration/5bb69383565c54c96efb7b14cbb300b1d36e33a0/A_star/GeekLogo.ico -------------------------------------------------------------------------------- /A_star/README.md: -------------------------------------------------------------------------------- 1 | # background 2 | - 一个演示搜索类算法的地图模拟器 3 | 4 | 这个项目的最初版本源自一个B站up主 Minicking开放的代码,在其代码的基础上我进行了学习、更改和扩充 5 | ,视频链接[A*寻路算法动态演示](https://www.bilibili.com/video/BV1FJ411j7oG?share_source=copy_web&vd_source=e09e7d9860329cf23712dbc79e5ed45e) 6 | # install 7 | 目前版本为免安装版本,打开.exe即可 8 | 9 | # usage 10 | 11 | 目前使用帮助都写到了程序的页面,按照提示操作即可。 12 | 13 | **注意事项:** 14 | - **先生成地图再导入地图**,地图的文件名必须为map.txt,且将文件放在./map目录下 15 | - 在导入前**导入的地图是否与当前的地图方块数**匹配,目前还没做报错、提醒的优化 16 | 17 | # badge 18 | 19 | 现在的版本:1.1.0,使用python、PyQt5开发 20 | 21 | 技术点: 22 | 23 | - 用python的类中的__new__ 方法实现点对象的唯一存储 24 | - 用算法结合画图事件和时间事件的逻辑 25 | - 用生成器来构造算法的执行过程 26 | - 判断两点间的距离采用曼哈顿距离,相对欧拉距离速度更快 27 | - 各类搜素算法的实现 28 | 29 | # example 30 | ![Geek_YYDS](https://img-blog.csdnimg.cn/b298ebeb9d4346b7ab4d17e76c85bbd1.gif) 31 | # maintainers 32 | 新思路Geek组成员某东,负责bug的修复,功能完善和添加算法 33 | 34 | # contributing 35 | 36 | - B站up主 [Minicking](https://space.bilibili.com/105229830),贡献了最初的代码 37 | 38 | # license 39 | 40 | [新思路](许可证) © Geek 41 | 42 | # Change log 43 | 44 | ## 2022.9.5 版本更新至1.1.0 45 | 46 | - 修改了调倍速的模块和优化了倍速体验 47 | - 添加了深度受限算法DLS、迭代加深算法IDS(开始默认depth为2,每次搜索depth+1) 48 | - 添加了双向搜素BS、爬山搜素HCS、集束搜素BeamS(W = 3)、最佳优先搜素BFS、统一代价搜素UCS 49 | - 添加了A*搜索,还想添加其他的,有感兴趣的来帮忙迭代呀! 50 | - 显示搜素完成后的最短路径 -------------------------------------------------------------------------------- /A_star/__pycache__/mainWindow.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Bsheepcoder/Search_algorithm_demonstration/5bb69383565c54c96efb7b14cbb300b1d36e33a0/A_star/__pycache__/mainWindow.cpython-39.pyc -------------------------------------------------------------------------------- /A_star/__pycache__/main_Ui.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Bsheepcoder/Search_algorithm_demonstration/5bb69383565c54c96efb7b14cbb300b1d36e33a0/A_star/__pycache__/main_Ui.cpython-39.pyc -------------------------------------------------------------------------------- /A_star/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding:utf-8 -*- 3 | import sys 4 | from PyQt5.QtWidgets import * 5 | from mainWindow import MainWindow 6 | 7 | if __name__ == '__main__': 8 | app = QApplication(sys.argv) 9 | window = MainWindow() 10 | window.show() 11 | sys.exit(app.exec_()) 12 | -------------------------------------------------------------------------------- /A_star/mainWindow.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding:utf-8 -*- 3 | ############################## 4 | # *** Can we do better ? *** # 5 | ############################## 6 | import types 7 | import sys 8 | import time 9 | import json 10 | from PyQt5.QtCore import * 11 | from PyQt5.QtGui import * 12 | from PyQt5.QtWidgets import * 13 | import numpy as np 14 | from main_Ui import Ui_mainWindow 15 | 16 | 17 | # 参数设定 18 | class Config: 19 | # 地图位置,左上角位置 20 | MAP_X = 240 21 | MAP_Y = 30 22 | # 地图尺寸 23 | SIZE_W = 900 24 | SIZE_H = 750 25 | # 方块大小 26 | blockLength = 150 # 绘制画面时每一个节点方块的边长 27 | blockLengthList = [150, 75, 50, 30, 25, 15, 10, 5] # 可选方块大小,最大宽和高的最大公约数 28 | # 方块数量 29 | WIDTH = int(SIZE_W / blockLength) # 方块列数 30 | HEIGHT = int(SIZE_H / blockLength) # 方块行数 31 | 32 | def __init__(self): 33 | self.gcd(self.SIZE_H, self.SIZE_W) 34 | 35 | def gcd(self, a, b): 36 | if a >= b: 37 | num = b 38 | else: 39 | num = a 40 | for i in range(1, num): 41 | if ((a % num) == 0) and ((b % num) == 0): 42 | self.blockLengthList.append(i) 43 | 44 | 45 | # 节点类 46 | class Point: 47 | _list = [] # 储存所有的point类实例 48 | _tag = True # 标记最新创建的实例是否为_list中的已有的实例,True表示不是已有实例 49 | 50 | def __new__(cls, x, y): # 重写new方法实现对于同样的坐标只有唯一的一个实例 51 | for i in Point._list: 52 | if i.x == x and i.y == y: 53 | Point._tag = False 54 | return i 55 | # 这里是做到了单例引用,因为__new__先于__init__执行, 56 | # 并且__new__的作用是创建对象空间,重写new后return的是唯一的引用对象空间, 57 | # 则每次init初始化都会只初始化返回的唯一空间,具体参考 http://t.csdn.cn/0KtFa 58 | nt = super(Point, cls).__new__(cls) 59 | Point._list.append(nt) 60 | return nt 61 | 62 | def __init__(self, x, y): 63 | if Point._tag: 64 | self.x = x 65 | self.y = y 66 | self.father = None 67 | self.toEndCost = 0 # 如果算法需要,则表示当前点与终点距离,也就是估计的路程花费 68 | self.path = 0 # 起始点到该点的路径花费 69 | else: 70 | Point._tag = True 71 | 72 | @classmethod 73 | def clear(cls): # clear方法,每次搜索结束后,将所有点数据清除,以便进行下一次搜索的时候点数据不会冲突。 74 | Point._list = [] 75 | 76 | def __eq__(self, T): # 重写==运算以便实现point类的in运算 77 | if type(self) == type(T): 78 | return (self.x, self.y) == (T.x, T.y) 79 | else: 80 | return False 81 | 82 | def __str__(self): 83 | return '(%d,%d)[father:(%s)]' % ( 84 | self.x, self.y, str((self.father.x, self.father.y)) if self.father is not None else 'null') 85 | 86 | 87 | # 不知情搜索 88 | 89 | 90 | # 广度优先算法 breadth-first search 91 | class BFS: 92 | 93 | def __init__(self, pointStart, pointEnd, searchMap): 94 | print("构造BFS") 95 | self.start = pointStart # 储存此次搜索的开始点 96 | self.end = pointEnd # 储存此次搜索的目的点 97 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 98 | 99 | self.open = [] # 开放列表:储存即将被搜索的节点 100 | self.close = [] # 关闭列表:储存已经搜索过的节点 101 | self.result = [] # 搜索完成后保存路径 102 | self.searchCount = 0 103 | self.useTime = 0 104 | # 将起始点输入open 105 | self.open.append(self.start) 106 | 107 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 108 | 109 | x = point.x 110 | y = point.y 111 | print(x, y) 112 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 113 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 114 | # 移除不在map上的点 115 | for p in allAroundPoint[::-1]: 116 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 117 | allAroundPoint.remove(p) 118 | 119 | # 当前节点可走的下一节点队列 120 | nextPointList = [] 121 | for i in allAroundPoint: 122 | if self.map[i[1]][i[0]] == 0: 123 | nextPoint = Point(i[0], i[1]) 124 | nextPointList.append(nextPoint) 125 | return nextPointList 126 | 127 | # 将可走的节点列表依次导入open,并连接父节点 128 | def addToOpen(self, probablyPointList, fatherPoint): 129 | for p in probablyPointList: 130 | if p not in self.open: 131 | if p not in self.close: 132 | p.father = fatherPoint 133 | self.open.append(p) 134 | else: 135 | pass 136 | 137 | def getOpen(self): 138 | if len(self.open) <= 0: 139 | return None 140 | t = self.open[0] 141 | return t 142 | 143 | # 生成器 144 | def process(self): 145 | print("进入 process") 146 | while True: 147 | print("运行中") 148 | self.searchCount += 1 149 | nowPoint = self.getOpen() 150 | if nowPoint is None: 151 | print("nowPoint is None") 152 | self.result = None 153 | self.searchCount = -1 154 | break 155 | else: 156 | print("nowPoint is not None") 157 | probablyPointList = self.getAroundPoint(nowPoint) 158 | self.addToOpen(probablyPointList, nowPoint) 159 | self.open.remove(nowPoint) 160 | self.close.append(nowPoint) 161 | if self.end in self.open: 162 | r = self.end 163 | self.result.append(r) 164 | while True: 165 | r = r.father 166 | if r is None: 167 | break 168 | self.result.append(r) 169 | yield nowPoint, self.open, self.close 170 | break 171 | print(nowPoint) 172 | print("结束") 173 | yield nowPoint, self.open, self.close 174 | # self.useTime = time2 - time1 175 | 176 | 177 | # 深度优先算法 depth-first search 178 | class DFS: 179 | def __init__(self, pointStart, pointEnd, searchMap): 180 | print("构造DFS") 181 | self.start = pointStart # 储存此次搜索的开始点 182 | self.end = pointEnd # 储存此次搜索的目的点 183 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 184 | 185 | self.open = [] # 开放列表:储存即将被搜索的节点 186 | self.close = [] # 关闭列表:储存已经搜索过的节点 187 | self.result = [] # 搜索完成后保存路径 188 | self.searchCount = 0 189 | self.useTime = 0 190 | # 将起始点输入open 191 | self.open.append(self.start) 192 | 193 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 194 | 195 | x = point.x 196 | y = point.y 197 | print(x, y) 198 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 199 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 200 | # 移除不在map上的点 201 | for p in allAroundPoint[::-1]: 202 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 203 | allAroundPoint.remove(p) 204 | 205 | # 当前节点可走的下一节点队列 206 | nextPointList = [] 207 | for i in allAroundPoint: 208 | if self.map[i[1]][i[0]] == 0: 209 | nextPoint = Point(i[0], i[1]) 210 | nextPointList.append(nextPoint) 211 | return nextPointList 212 | 213 | # 将可走的节点列表依次导入open,并连接父节点 214 | def addToOpen(self, probablyPointList, fatherPoint): 215 | for p in probablyPointList: 216 | if p not in self.open: 217 | if p not in self.close: 218 | p.father = fatherPoint 219 | self.open.append(p) 220 | else: 221 | pass 222 | 223 | def getOpen(self): 224 | if len(self.open) <= 0: 225 | return None 226 | return self.open.pop() 227 | 228 | # 生成器 229 | def process(self): 230 | print("进入 process") 231 | while True: 232 | print("运行中") 233 | self.searchCount += 1 234 | nowPoint = self.getOpen() 235 | if nowPoint is None: 236 | print("nowPoint is None") 237 | self.result = None 238 | self.searchCount = -1 239 | break 240 | else: 241 | print("nowPoint is not None") 242 | probablyPointList = self.getAroundPoint(nowPoint) 243 | self.addToOpen(probablyPointList, nowPoint) 244 | self.close.append(nowPoint) 245 | if self.end in self.open: 246 | r = self.end 247 | self.result.append(r) 248 | while True: 249 | r = r.father 250 | if r is None: 251 | break 252 | self.result.append(r) 253 | yield nowPoint, self.open, self.close 254 | break 255 | print(nowPoint) 256 | print("结束") 257 | yield nowPoint, self.open, self.close 258 | # self.useTime = time2 - time1 259 | 260 | 261 | # 深度受限算法 depth-limited search 262 | class DLS: 263 | def __init__(self, pointStart, pointEnd, searchMap): 264 | print("构造DLS") 265 | self.start = pointStart # 储存此次搜索的开始点 266 | self.end = pointEnd # 储存此次搜索的目的点 267 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 268 | self.depth = 0 # 用来判断限制的深度 269 | self.wait = [] # 满足深度之后暂停等待的点 270 | self.open = [] # 开放列表:储存即将被搜索的节点 271 | self.close = [] # 关闭列表:储存已经搜索过的节点 272 | self.result = [] # 搜索完成后保存路径 273 | self.searchCount = 0 274 | self.useTime = 0 275 | # 将起始点输入open 276 | self.open.append(self.start) 277 | 278 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 279 | 280 | x = point.x 281 | y = point.y 282 | print(x, y) 283 | # 这个序列决定了根的顺序 284 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 285 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 286 | # 移除不在map上的点 287 | for p in allAroundPoint[::-1]: 288 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 289 | allAroundPoint.remove(p) 290 | 291 | # 当前节点可走的下一节点队列 292 | nextPointList = [] 293 | for i in allAroundPoint: 294 | if self.map[i[1]][i[0]] == 0: 295 | nextPoint = Point(i[0], i[1]) 296 | nextPointList.append(nextPoint) 297 | return nextPointList 298 | 299 | # 将可走的节点列表依次导入open,并连接父节点 300 | 301 | def addToOpen(self, probablyPointList, fatherPoint): 302 | for p in probablyPointList: 303 | if p not in self.open: 304 | if p not in self.close: 305 | p.father = fatherPoint 306 | self.open.append(p) 307 | else: 308 | pass 309 | 310 | def getOpen(self): 311 | if len(self.open) <= 0: 312 | return None 313 | if self.depth >= 10: 314 | self.depth = 0 315 | self.wait.append(self.close[-1]) 316 | for i in self.close: 317 | for j in self.open: 318 | if j.father is i: 319 | p = j 320 | self.open.remove(j) 321 | return p 322 | if self.wait[0] is None: 323 | return None 324 | return self.wait.pop() 325 | self.depth += 1 326 | return self.open.pop() 327 | 328 | # 生成器 329 | 330 | def process(self): 331 | print("进入 process") 332 | while True: 333 | print("运行中") 334 | self.searchCount += 1 335 | nowPoint = self.getOpen() 336 | if nowPoint is None: 337 | print("nowPoint is None") 338 | self.result = None 339 | self.searchCount = -1 340 | break 341 | else: 342 | print("nowPoint is not None") 343 | probablyPointList = self.getAroundPoint(nowPoint) 344 | self.addToOpen(probablyPointList, nowPoint) 345 | self.close.append(nowPoint) 346 | if self.end in self.open: 347 | r = self.end 348 | self.result.append(r) 349 | while True: 350 | r = r.father 351 | if r is None: 352 | break 353 | self.result.append(r) 354 | yield nowPoint, self.open, self.close 355 | break 356 | print(nowPoint) 357 | print("结束") 358 | yield nowPoint, self.open, self.close 359 | # self.useTime = time2 - time1 360 | 361 | 362 | # 迭代加深算法 iterative deepening search 363 | class IDS: 364 | def __init__(self, pointStart, pointEnd, searchMap): 365 | print("构造IDS") 366 | self.start = pointStart # 储存此次搜索的开始点 367 | self.end = pointEnd # 储存此次搜索的目的点 368 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 369 | 370 | self.depth = 0 # 用来判断限制的深度 371 | self.depthAdd = 2 # 放开深度限制的判断条件 372 | self.wait = [] # 满足深度限制之后暂停等待的点 373 | self.open = [] # 开放列表:储存即将被搜索的节点 374 | self.close = [] # 关闭列表:储存已经搜索过的节点 375 | self.result = [] # 搜索完成后保存路径 376 | self.searchCount = 0 377 | self.useTime = 0 378 | # 将起始点输入open 379 | self.open.append(self.start) 380 | 381 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 382 | 383 | x = point.x 384 | y = point.y 385 | print(x, y) 386 | # 这个序列决定了点出发方向顺序 387 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 388 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 389 | # 移除不在map上的点 390 | for p in allAroundPoint[::-1]: 391 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 392 | allAroundPoint.remove(p) 393 | 394 | # 当前节点可走的下一节点队列 395 | nextPointList = [] 396 | for i in allAroundPoint: 397 | if self.map[i[1]][i[0]] == 0: 398 | nextPoint = Point(i[0], i[1]) 399 | nextPointList.append(nextPoint) 400 | return nextPointList 401 | 402 | # 将可走的节点列表依次导入open,并连接父节点 403 | 404 | def addToOpen(self, probablyPointList, fatherPoint): 405 | for p in probablyPointList: 406 | if p not in self.open: 407 | if p not in self.close: 408 | p.father = fatherPoint 409 | self.open.append(p) 410 | else: 411 | pass 412 | 413 | def getOpen(self): 414 | if len(self.open) <= 0: 415 | return None 416 | if self.depth >= self.depthAdd: 417 | self.depth = 0 418 | self.wait.append(self.close[-1]) 419 | for i in self.close: 420 | for j in self.open: 421 | if j.father is i: 422 | p = j 423 | self.depthAdd += 1 424 | self.open.remove(j) 425 | return p 426 | if self.wait[0] is None: 427 | return None 428 | return self.wait.pop() 429 | self.depth += 1 430 | return self.open.pop() 431 | 432 | # 生成器 433 | 434 | def process(self): 435 | while True: 436 | self.searchCount += 1 437 | nowPoint = self.getOpen() 438 | if nowPoint is None: 439 | self.result = None 440 | self.searchCount = -1 441 | break 442 | else: 443 | probablyPointList = self.getAroundPoint(nowPoint) 444 | self.addToOpen(probablyPointList, nowPoint) 445 | self.close.append(nowPoint) 446 | if self.end in self.open: 447 | r = self.end 448 | self.result.append(r) 449 | while True: 450 | r = r.father 451 | if r is None: 452 | break 453 | self.result.append(r) 454 | yield nowPoint, self.open, self.close 455 | break 456 | yield nowPoint, self.open, self.close 457 | # self.useTime = time2 - time1 458 | 459 | 460 | # 双向搜索(BFS) bidirectional search 461 | class BS: 462 | 463 | def __init__(self, pointStart, pointEnd, searchMap): 464 | print("构造BS") 465 | self.start = pointStart # 储存此次搜索的开始点 466 | self.end = pointEnd # 储存此次搜索的目的点 467 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 468 | # 起点 469 | self.open_s = [] # 开放列表:储存即将被搜索的节点 470 | # 终点 471 | self.open_e = [] # 开放列表:储存即将被搜索的节点 472 | 473 | self.close = [] # 关闭列表:储存已经搜索过的节点 474 | self.result = [] # 搜索完成后保存路径 475 | self.searchCount = 0 476 | self.useTime = 0 477 | # 将起始点输入open 478 | self.open_s.append(self.start) 479 | self.open_e.append(self.end) 480 | 481 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 482 | 483 | x = point.x 484 | y = point.y 485 | print(x, y) 486 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 487 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 488 | # 移除不在map上的点 489 | for p in allAroundPoint[::-1]: 490 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 491 | allAroundPoint.remove(p) 492 | 493 | # 当前节点可走的下一节点队列 494 | nextPointList = [] 495 | for i in allAroundPoint: 496 | if self.map[i[1]][i[0]] == 0: 497 | nextPoint = Point(i[0], i[1]) 498 | nextPointList.append(nextPoint) 499 | return nextPointList 500 | 501 | # 将可走的节点列表依次导入open,并连接父节点 502 | def addToOpen(self, probablyPointList, fatherPoint, openList): 503 | for p in probablyPointList: 504 | if p not in openList: 505 | if p not in self.close: 506 | if p.father is not None: 507 | # 这里用到python动态绑定,给点p增加了一个属性使得双向搜索可以链接起来 508 | p.father2 = fatherPoint 509 | else: 510 | p.father = fatherPoint 511 | openList.append(p) 512 | else: 513 | pass 514 | 515 | def getOpen(self, openList): 516 | if len(openList) <= 0: 517 | return None 518 | t = openList[0] 519 | return t 520 | 521 | # 生成器 522 | def process(self): 523 | print("进入 process") 524 | while True: 525 | print("运行中") 526 | self.searchCount += 2 527 | nowPoint_s = self.getOpen(self.open_s) 528 | nowPoint_e = self.getOpen(self.open_e) 529 | if nowPoint_s is None or nowPoint_e is None: 530 | print("nowPoint is None") 531 | self.result = None 532 | self.searchCount = -2 533 | break 534 | else: 535 | print("nowPoint is not None") 536 | probablyPointList_s = self.getAroundPoint(nowPoint_s) 537 | probablyPointList_e = self.getAroundPoint(nowPoint_e) 538 | self.addToOpen(probablyPointList_s, nowPoint_s, self.open_s) 539 | self.addToOpen(probablyPointList_e, nowPoint_e, self.open_e) 540 | self.open_s.remove(nowPoint_s) 541 | self.open_e.remove(nowPoint_e) 542 | self.close.append(nowPoint_s) 543 | self.close.append(nowPoint_e) 544 | # 用于退出循环 545 | flag1 = False 546 | flag2 = False 547 | for ps in self.open_s: 548 | for pe in self.open_e: 549 | if ps.x == pe.x and ps.y == pe.y: 550 | r = ps 551 | self.result.append(r) 552 | while True: 553 | r = r.father 554 | if r is None: 555 | break 556 | self.result.append(r) 557 | r = ps.father2 558 | self.result.append(r) 559 | while True: 560 | r = r.father 561 | if r is None: 562 | break 563 | self.result.append(r) 564 | yield nowPoint_s, self.open_s, self.close 565 | flag1 = True 566 | break 567 | if flag1: 568 | flag2 = True 569 | break 570 | if flag2: 571 | break 572 | print(nowPoint_s) 573 | print("结束") 574 | yield nowPoint_s, self.open_s, self.close 575 | 576 | 577 | # 知情搜索 578 | 579 | # 爬山搜索 Hill Climbing search / 贪婪最佳优先搜索 greedy best-first search 580 | class HCS: 581 | def __init__(self, pointStart, pointEnd, searchMap): 582 | print("构造HCS") 583 | self.start = pointStart # 储存此次搜索的开始点 584 | self.end = pointEnd # 储存此次搜索的目的点 585 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 586 | 587 | self.MinCost = Config.WIDTH * Config.HEIGHT 588 | self.open = [] # 开放列表:储存即将被搜索的节点 589 | self.close = [] # 关闭列表:储存已经搜索过的节点 590 | self.result = [] # 搜索完成后保存路径 591 | self.searchCount = 0 592 | self.useTime = 0 593 | # 将起始点输入open 594 | self.open.append(self.start) 595 | 596 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 597 | 598 | x = point.x 599 | y = point.y 600 | print(x, y) 601 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 602 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 603 | # 移除不在map上的点 604 | for p in allAroundPoint[::-1]: 605 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 606 | allAroundPoint.remove(p) 607 | 608 | # 当前节点可走的下一节点队列 609 | nextPointList = [] 610 | for i in allAroundPoint: 611 | if self.map[i[1]][i[0]] == 0: 612 | nextPoint = Point(i[0], i[1]) 613 | nextPointList.append(nextPoint) 614 | return nextPointList 615 | 616 | # 返回传入点到终点的花费 617 | 618 | def cost(self, point): 619 | return abs(point.x - self.end.x) + abs(point.y - self.end.y) 620 | 621 | # 将可走的节点列表依次导入open,并连接父节点 622 | 623 | def addToOpen(self, probablyPointList, fatherPoint): 624 | tag = None 625 | Max = Config.WIDTH * Config.HEIGHT 626 | Flag = False 627 | for p in probablyPointList: 628 | if p not in self.open: 629 | if p not in self.close: 630 | c = self.cost(p) 631 | if c < self.MinCost: 632 | Flag = True 633 | self.MinCost = c 634 | tag = p 635 | if Flag: 636 | tag.father = fatherPoint 637 | self.open.append(tag) 638 | 639 | def getOpen(self): 640 | if len(self.open) <= 0: 641 | return None 642 | return self.open.pop() 643 | 644 | # 生成器 645 | 646 | def process(self): 647 | print("进入 process") 648 | while True: 649 | print("运行中") 650 | self.searchCount += 1 651 | nowPoint = self.getOpen() 652 | if nowPoint is None: 653 | print("nowPoint is None") 654 | self.result = None 655 | self.searchCount = -1 656 | break 657 | else: 658 | print("nowPoint is not None") 659 | probablyPointList = self.getAroundPoint(nowPoint) 660 | self.addToOpen(probablyPointList, nowPoint) 661 | self.close.append(nowPoint) 662 | if self.end in self.open: 663 | r = self.end 664 | self.result.append(r) 665 | while True: 666 | r = r.father 667 | if r is None: 668 | break 669 | self.result.append(r) 670 | yield nowPoint, self.open, self.close 671 | break 672 | print(nowPoint) 673 | print("结束") 674 | yield nowPoint, self.open, self.close 675 | # self.useTime = time2 - time1 676 | 677 | 678 | # 集束搜索 beam search W = 3 679 | class BeamS: 680 | def __init__(self, pointStart, pointEnd, searchMap): 681 | print("构造BeamS") 682 | self.start = pointStart # 储存此次搜索的开始点 683 | self.end = pointEnd # 储存此次搜索的目的点 684 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 685 | 686 | self.W = 3 # 表示集束的薄厚,也就是取几个最小的点 687 | self.MinCost = Config.WIDTH * Config.HEIGHT # 用来判断并取得最小值 688 | self.open = [] # 开放列表:储存即将被搜索的节点 689 | self.close = [] # 关闭列表:储存已经搜索过的节点 690 | self.result = [] # 搜索完成后保存路径 691 | self.searchCount = 0 692 | self.useTime = 0 693 | # 将起始点输入open 694 | self.open.append(self.start) 695 | 696 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 697 | 698 | x = point.x 699 | y = point.y 700 | print(x, y) 701 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 702 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 703 | # 移除不在map上的点 704 | for p in allAroundPoint[::-1]: 705 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 706 | allAroundPoint.remove(p) 707 | 708 | # 当前节点可走的下一节点队列 709 | nextPointList = [] 710 | for i in allAroundPoint: 711 | if self.map[i[1]][i[0]] == 0: 712 | nextPoint = Point(i[0], i[1]) 713 | nextPointList.append(nextPoint) 714 | return nextPointList 715 | 716 | # 返回传入点到终点的花费 717 | def cost(self, point): 718 | return abs(point.x - self.end.x) + abs(point.y - self.end.y) 719 | 720 | # 将可走的节点列表依次导入open,并连接父节点 721 | def addToOpen(self, probablyPointList, fatherPoint): 722 | tag = None 723 | Max = Config.WIDTH * Config.HEIGHT 724 | Flag = False 725 | for i in range(0, self.W): 726 | for p in probablyPointList: 727 | if p not in self.open: 728 | if p not in self.close: 729 | c = self.cost(p) 730 | if c < self.MinCost: 731 | Flag = True 732 | self.MinCost = c 733 | tag = p 734 | if Flag: 735 | tag.father = fatherPoint 736 | self.open.append(tag) 737 | probablyPointList.remove(tag) 738 | self.MinCost = Config.WIDTH * Config.HEIGHT 739 | Flag = False 740 | if len(probablyPointList) > 1: 741 | self.W = 1 742 | 743 | # 形式上使用了优先队列 744 | def getOpen(self): 745 | if len(self.open) <= 0: 746 | return None 747 | tag = self.open[0] 748 | m = self.open[0].toEndCost 749 | for i in self.open: 750 | if i.toEndCost < m: 751 | m = i.toEndCost 752 | tag = i 753 | self.open.remove(tag) 754 | return tag 755 | 756 | # 生成器 757 | def process(self): 758 | print("进入 process") 759 | while True: 760 | print("运行中") 761 | self.searchCount += 1 762 | nowPoint = self.getOpen() 763 | if nowPoint is None: 764 | print("nowPoint is None") 765 | self.result = None 766 | self.searchCount = -1 767 | break 768 | else: 769 | print("nowPoint is not None") 770 | probablyPointList = self.getAroundPoint(nowPoint) 771 | self.addToOpen(probablyPointList, nowPoint) 772 | self.close.append(nowPoint) 773 | if self.end in self.open: 774 | r = self.end 775 | self.result.append(r) 776 | while True: 777 | r = r.father 778 | if r is None: 779 | break 780 | self.result.append(r) 781 | yield nowPoint, self.open, self.close 782 | break 783 | print(nowPoint) 784 | print("结束") 785 | yield nowPoint, self.open, self.close 786 | # self.useTime = time2 - time1 787 | 788 | 789 | # 最佳优先搜索 best-first search+ 790 | class GBFS: 791 | def __init__(self, pointStart, pointEnd, searchMap): 792 | print("构造DFS") 793 | self.start = pointStart # 储存此次搜索的开始点 794 | self.end = pointEnd # 储存此次搜索的目的点 795 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 796 | 797 | self.MinCost = Config.WIDTH * Config.HEIGHT 798 | self.open = [] # 开放列表:储存即将被搜索的节点 799 | self.close = [] # 关闭列表:储存已经搜索过的节点 800 | self.result = [] # 搜索完成后保存路径 801 | self.searchCount = 0 802 | self.useTime = 0 803 | # 将起始点输入open 804 | self.open.append(self.start) 805 | 806 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 807 | 808 | x = point.x 809 | y = point.y 810 | print(x, y) 811 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 812 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 813 | # 移除不在map上的点 814 | for p in allAroundPoint[::-1]: 815 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 816 | allAroundPoint.remove(p) 817 | 818 | # 当前节点可走的下一节点队列 819 | nextPointList = [] 820 | for i in allAroundPoint: 821 | if self.map[i[1]][i[0]] == 0: 822 | nextPoint = Point(i[0], i[1]) 823 | nextPointList.append(nextPoint) 824 | return nextPointList 825 | 826 | # 返回传入点到终点的花费 827 | def cost(self, point): 828 | return abs(point.x - self.end.x) + abs(point.y - self.end.y) 829 | 830 | # 将可走的节点列表依次导入open,并连接父节点 831 | def addToOpen(self, probablyPointList, fatherPoint): 832 | for p in probablyPointList: 833 | if p not in self.open: 834 | if p not in self.close: 835 | p.toEndCost = self.cost(p) 836 | p.father = fatherPoint 837 | self.open.append(p) 838 | 839 | # 形式上使用了优先队列 840 | def getOpen(self): 841 | if len(self.open) <= 0: 842 | return None 843 | tag = self.open[0] 844 | m = self.open[0].toEndCost 845 | for i in self.open: 846 | if i.toEndCost < m: 847 | m = i.toEndCost 848 | tag = i 849 | self.open.remove(tag) 850 | return tag 851 | 852 | # 生成器 853 | def process(self): 854 | print("进入 process") 855 | while True: 856 | print("运行中") 857 | self.searchCount += 1 858 | nowPoint = self.getOpen() 859 | if nowPoint is None: 860 | print("nowPoint is None") 861 | self.result = None 862 | self.searchCount = -1 863 | break 864 | else: 865 | print("nowPoint is not None") 866 | probablyPointList = self.getAroundPoint(nowPoint) 867 | self.addToOpen(probablyPointList, nowPoint) 868 | self.close.append(nowPoint) 869 | if self.end in self.open: 870 | r = self.end 871 | self.result.append(r) 872 | while True: 873 | r = r.father 874 | if r is None: 875 | break 876 | self.result.append(r) 877 | yield nowPoint, self.open, self.close 878 | break 879 | print(nowPoint) 880 | print("结束") 881 | yield nowPoint, self.open, self.close 882 | # self.useTime = time2 - time1 883 | 884 | 885 | # 统一代价搜索 uniform-cost search 886 | class UCS: 887 | def __init__(self, pointStart, pointEnd, searchMap): 888 | print("构造DFS") 889 | self.start = pointStart # 储存此次搜索的开始点 890 | self.end = pointEnd # 储存此次搜索的目的点 891 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 892 | 893 | self.bestCost = Config.WIDTH * Config.HEIGHT # 最终花费值 894 | self.costList = [] 895 | self.MinCost = Config.WIDTH * Config.HEIGHT 896 | self.open = [] # 开放列表:储存即将被搜索的节点 897 | self.close = [] # 关闭列表:储存已经搜索过的节点 898 | self.result = [] # 搜索完成后保存路径 899 | self.searchCount = 0 900 | self.useTime = 0 901 | # 将起始点输入open 902 | self.open.append(self.start) 903 | 904 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 905 | 906 | x = point.x 907 | y = point.y 908 | print(x, y) 909 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 910 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 911 | # 移除不在map上的点 912 | for p in allAroundPoint[::-1]: 913 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 914 | allAroundPoint.remove(p) 915 | 916 | # 当前节点可走的下一节点队列 917 | nextPointList = [] 918 | for i in allAroundPoint: 919 | if self.map[i[1]][i[0]] == 0: 920 | nextPoint = Point(i[0], i[1]) 921 | nextPointList.append(nextPoint) 922 | return nextPointList 923 | 924 | # 返回传入点到终点的花费 925 | 926 | def cost(self, point): 927 | return abs(point.x - self.end.x) + abs(point.y - self.end.y) 928 | 929 | def path(self, point): 930 | p = point 931 | count = 0 932 | while p is not None: 933 | if p.father is not None: 934 | count += 1 935 | p = p.father 936 | return count 937 | 938 | # 将可走的节点列表依次导入open,并连接父节点 939 | 940 | def addToOpen(self, probablyPointList, fatherPoint): 941 | for p in probablyPointList: 942 | if p not in self.open: 943 | if p not in self.close: 944 | p.toEndCost = self.cost(p) 945 | p.father = fatherPoint 946 | if p.father is not None: 947 | p.path = p.father.path + 1 948 | print(p.path) 949 | self.open.append(p) 950 | 951 | # 形式上使用了优先队列 952 | 953 | def getOpen(self): 954 | if len(self.open) <= 0: 955 | return None 956 | tag = None 957 | if self.bestCost == Config.WIDTH * Config.HEIGHT: 958 | m = self.open[0].path + self.open[0].toEndCost 959 | for i in self.open: 960 | n = i.toEndCost + i.path 961 | if n <= m: 962 | m = n 963 | tag = i 964 | self.open.remove(tag) 965 | return tag 966 | 967 | else: 968 | m = self.bestCost 969 | for i in self.open: 970 | n = i.toEndCost + i.path 971 | if n <= m: 972 | tag = i 973 | self.open.remove(tag) 974 | return tag 975 | return None 976 | # 生成器 977 | 978 | def process(self): 979 | print("进入 process") 980 | while True: 981 | print("运行中") 982 | self.searchCount += 1 983 | nowPoint = self.getOpen() 984 | if nowPoint is None: 985 | if len(self.open) == 0: 986 | print("nowPoint is None") 987 | self.result = None 988 | self.searchCount = -1 989 | break 990 | else: 991 | print("nowPoint is not None") 992 | probablyPointList = self.getAroundPoint(nowPoint) 993 | self.addToOpen(probablyPointList, nowPoint) 994 | self.close.append(nowPoint) 995 | 996 | # 判断是否检测到终点 997 | if self.end in self.open: 998 | self.open.remove(self.end) 999 | if nowPoint.path + nowPoint.toEndCost < self.bestCost: 1000 | self.bestCost = nowPoint.path + nowPoint.toEndCost 1001 | self.costList.append(nowPoint) 1002 | tag = True 1003 | # 判断是否存在 path + endCost < 当前终点的 path + endCost 的点 1004 | for i in self.open: 1005 | if (i.path + i.toEndCost) <= self.bestCost: 1006 | tag = False 1007 | break 1008 | if tag: 1009 | r = self.end 1010 | self.result.append(r) 1011 | r = self.costList.pop() 1012 | self.result.append(r) 1013 | while True: 1014 | r = r.father 1015 | if r is None: 1016 | break 1017 | self.result.append(r) 1018 | yield nowPoint, self.open, self.close 1019 | break 1020 | print(nowPoint) 1021 | print("结束") 1022 | yield nowPoint, self.open, self.close 1023 | # self.useTime = time2 - time1 1024 | 1025 | 1026 | # A*搜索 f(n) = path(n) + toEndCost(n) 1027 | class A_Star: 1028 | def __init__(self, pointStart, pointEnd, searchMap): 1029 | print("构造DFS") 1030 | self.start = pointStart # 储存此次搜索的开始点 1031 | self.end = pointEnd # 储存此次搜索的目的点 1032 | self.map = searchMap # 一个二维数组,为此次搜索的地图引用 1033 | 1034 | self.bestCost = Config.WIDTH * Config.HEIGHT # 最终花费值 1035 | self.costList = [] 1036 | self.MinCost = Config.WIDTH * Config.HEIGHT 1037 | self.open = [] # 开放列表:储存即将被搜索的节点 1038 | self.close = [] # 关闭列表:储存已经搜索过的节点 1039 | self.result = [] # 搜索完成后保存路径 1040 | self.searchCount = 0 1041 | self.useTime = 0 1042 | # 将起始点输入open 1043 | self.open.append(self.start) 1044 | 1045 | def getAroundPoint(self, point): # 获取指定点周围所有可通行的点 1046 | 1047 | x = point.x 1048 | y = point.y 1049 | print(x, y) 1050 | allAroundPoint = [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 1, y - 1), 1051 | (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] 1052 | # 移除不在map上的点 1053 | for p in allAroundPoint[::-1]: 1054 | if p[0] < 0 or p[0] >= Config.WIDTH or p[1] < 0 or p[1] >= Config.HEIGHT: 1055 | allAroundPoint.remove(p) 1056 | 1057 | # 当前节点可走的下一节点队列 1058 | nextPointList = [] 1059 | for i in allAroundPoint: 1060 | if self.map[i[1]][i[0]] == 0: 1061 | nextPoint = Point(i[0], i[1]) 1062 | nextPointList.append(nextPoint) 1063 | return nextPointList 1064 | 1065 | # 返回传入点到终点的花费 1066 | 1067 | def cost(self, point): 1068 | return abs(point.x - self.end.x) + abs(point.y - self.end.y) 1069 | 1070 | def path(self, point): 1071 | p = point 1072 | count = 0 1073 | while p is not None: 1074 | if p.father is not None: 1075 | count += 1 1076 | p = p.father 1077 | return count 1078 | 1079 | # 将可走的节点列表依次导入open,并连接父节点 1080 | 1081 | def addToOpen(self, probablyPointList, fatherPoint): 1082 | for p in probablyPointList: 1083 | if p not in self.open: 1084 | if p not in self.close: 1085 | p.toEndCost = self.cost(p) 1086 | p.father = fatherPoint 1087 | if p.father is not None: 1088 | p.path = p.father.path + 1 1089 | print(p.path) 1090 | self.open.append(p) 1091 | 1092 | # 形式上使用了优先队列 1093 | def getOpen(self): 1094 | if len(self.open) <= 0: 1095 | return None 1096 | tag = None 1097 | m = self.open[0].path + self.open[0].toEndCost 1098 | for i in self.open: 1099 | n = i.toEndCost + i.path 1100 | if n <= m: 1101 | m = n 1102 | tag = i 1103 | self.open.remove(tag) 1104 | return tag 1105 | # 生成器 1106 | 1107 | def process(self): 1108 | print("进入 process") 1109 | while True: 1110 | print("运行中") 1111 | self.searchCount += 1 1112 | nowPoint = self.getOpen() 1113 | if nowPoint is None: 1114 | if len(self.open) == 0: 1115 | print("nowPoint is None") 1116 | self.result = None 1117 | self.searchCount = -1 1118 | break 1119 | else: 1120 | print("nowPoint is not None") 1121 | probablyPointList = self.getAroundPoint(nowPoint) 1122 | self.addToOpen(probablyPointList, nowPoint) 1123 | self.close.append(nowPoint) 1124 | # 判断是否检测到终点 1125 | if self.end in self.open: 1126 | r = self.end 1127 | self.result.append(r) 1128 | while True: 1129 | r = r.father 1130 | if r is None: 1131 | break 1132 | self.result.append(r) 1133 | yield nowPoint, self.open, self.close 1134 | break 1135 | print(nowPoint) 1136 | print("结束") 1137 | yield nowPoint, self.open, self.close 1138 | # self.useTime = time2 - time1 1139 | 1140 | 1141 | # 窗口类 1142 | class MainWindow(QMainWindow, Ui_mainWindow): 1143 | 1144 | def __init__(self): 1145 | print("初始化地图") 1146 | 1147 | # 初始化参数 1148 | self.Map = [] 1149 | self.startPoint = None 1150 | self.endPoint = None 1151 | self.centerTimer = None # 全局时间 1152 | self.search = None 1153 | self.yi = None 1154 | self.special = None 1155 | self.config = Config() 1156 | # 根据参数设定,初始化地图的二维list 1157 | for i in range(Config.HEIGHT): 1158 | col = [] 1159 | for j in range(Config.WIDTH): 1160 | col.append(0) 1161 | self.Map.append(col) 1162 | 1163 | # 继承QMainWindow需要调用超类的__init__() 1164 | super().__init__() 1165 | 1166 | # 导入UI 1167 | self.ui = self.setupUi(self) 1168 | self.initUI() 1169 | 1170 | # 初始化UI 1171 | def initUI(self): 1172 | 1173 | lineEdit = QLineEdit() 1174 | lineEdit.setReadOnly(True) # 设置只读 1175 | lineEdit.setAlignment(Qt.AlignCenter) # 设置文字居中 1176 | self.comboBoxSearch.setLineEdit(lineEdit) 1177 | for i in self.config.blockLengthList: 1178 | self.comboBoxBlockSize.addItem(str(i), i) 1179 | self.setFixedSize(self.width(), self.height()) 1180 | self.setWindowIcon(QIcon("./GeekLogo.ico")) 1181 | self.setObjectName("MainWindow") 1182 | self.setStyleSheet("#MainWindow{border-image:url(7.png)}") 1183 | # 控件绑定事件 1184 | self.button_CreatetMap.clicked.connect(self.event_SaveMap) 1185 | self.button_InputMap.clicked.connect(self.event_GetMap) 1186 | self.button_Run.clicked.connect(self.event_StartSearch) 1187 | self.button_ClearMap.clicked.connect(self.event_ClearMap) 1188 | self.button_EndSearch.clicked.connect(self.event_EndSearch) 1189 | self.button_RandomMap.clicked.connect(self.event_RandomMap) 1190 | self.comboBoxBlockSize.currentIndexChanged.connect(self.event_SetBlockSize) 1191 | 1192 | # 程序初始化成功... 1193 | self.ToNote('---------新思路Geek组--------\n' 1194 | '| 好奇之心,改变之力 |\n' 1195 | '| QQ群:299263121 |\n' 1196 | '初始化成功...\n' 1197 | '请设计地图或导入map.txt...') 1198 | 1199 | # 发送通知到窗口 1200 | def ToNote(self, message): 1201 | self.plainTextEdit_ShowMessage.appendPlainText(message) 1202 | 1203 | # #自定义事件 1204 | # 1205 | # 保存地图 1206 | def event_SaveMap(self): 1207 | with open('./map/map.txt', 'w') as f: 1208 | f.write(json.dumps(self.Map)) 1209 | self.ToNote("N: 生成地图成功!在该应用路径内查看") 1210 | 1211 | # 导入地图 1212 | def event_GetMap(self): 1213 | try: 1214 | with open('./map/map.txt', 'r') as f: 1215 | self.Map = json.loads(f.read()) 1216 | Config.HEIGHT = len(self.Map) 1217 | Config.WIDTH = len(self.Map[0]) 1218 | self.repaint() 1219 | except Exception as e: 1220 | print('失败', e, type(e)) 1221 | if type(e) == FileNotFoundError: 1222 | self.ToNote('Error: 地图加载失败:地图文件不存在') 1223 | elif type(e) == json.decoder.JSONDecodeError: 1224 | self.ToNote('Error: 地图加载失败,错误的地图文件') 1225 | 1226 | # 清空地图 1227 | def event_ClearMap(self): 1228 | for i in range(len(self.Map)): 1229 | for j in range(len(self.Map[i])): 1230 | self.Map[i][j] = 0 1231 | self.repaint() 1232 | self.ToNote('N: 地图已清空') 1233 | 1234 | # 随机生成地图 1235 | def event_RandomMap(self): 1236 | Map = [] 1237 | for i in range(0, Config.HEIGHT): 1238 | Map.append(np.random.randint(0, 2, Config.WIDTH).tolist()) 1239 | self.Map = Map 1240 | self.repaint() 1241 | 1242 | # 方块大小响应事件 1243 | def event_SetBlockSize(self): 1244 | Config.blockLength = int(self.comboBoxBlockSize.currentText()) 1245 | print(Config.blockLength) 1246 | # 方块数量 1247 | Config.WIDTH = int(Config.SIZE_W / Config.blockLength) # 方块列数 1248 | Config.HEIGHT = int(Config.SIZE_H / Config.blockLength) # 方块行数 1249 | self.Map = [] 1250 | # 根据参数设定,初始化地图的二维list 1251 | for i in range(Config.HEIGHT): 1252 | col = [] 1253 | for j in range(Config.WIDTH): 1254 | col.append(0) 1255 | self.Map.append(col) 1256 | self.repaint() 1257 | 1258 | # 开始搜索事件 1259 | def event_StartSearch(self): 1260 | 1261 | if self.startPoint is not None and self.endPoint is not None: 1262 | if self.centerTimer is None: 1263 | self.centerTimer = QBasicTimer() # 设置定时器 1264 | self.button_Run.setEnabled(False) 1265 | self.button_ClearMap.setEnabled(False) 1266 | self.button_InputMap.setEnabled(False) 1267 | self.button_CreatetMap.setEnabled(False) 1268 | self.button_RandomMap.setEnabled(False) 1269 | # 开始计时 1270 | self.centerTimer.start(800 / self.horizontalSlider_Speed.value(), self) 1271 | # 算法实例化 1272 | self.search = self.selectAlgorithm(self.comboBoxSearch.currentIndex()) 1273 | self.yi = self.search.process() 1274 | self.ToNote('N: 开始搜索》》》') 1275 | else: 1276 | self.ToNote('Error: 请先设置起点或终点') 1277 | 1278 | # 搜索算法选择 1279 | def selectAlgorithm(self, select): 1280 | # 简化书写,优化代码 1281 | alg = None 1282 | sx = self.startPoint[0] 1283 | sy = self.startPoint[1] 1284 | ex = self.endPoint[0] 1285 | ey = self.endPoint[1] 1286 | 1287 | if select == 0: 1288 | alg = BFS(Point(sx, sy), Point(ex, ey), self.Map) 1289 | elif select == 1: 1290 | alg = DFS(Point(sx, sy), Point(ex, ey), self.Map) 1291 | elif select == 2: 1292 | alg = DLS(Point(sx, sy), Point(ex, ey), self.Map) 1293 | elif select == 3: 1294 | alg = IDS(Point(sx, sy), Point(ex, ey), self.Map) 1295 | elif select == 4: 1296 | alg = BS(Point(sx, sy), Point(ex, ey), self.Map) 1297 | elif select == 5: 1298 | alg = HCS(Point(sx, sy), Point(ex, ey), self.Map) 1299 | elif select == 6: 1300 | alg = BeamS(Point(sx, sy), Point(ex, ey), self.Map) 1301 | elif select == 7: 1302 | alg = GBFS(Point(sx, sy), Point(ex, ey), self.Map) 1303 | elif select == 8: 1304 | alg = UCS(Point(sx, sy), Point(ex, ey), self.Map) 1305 | elif select == 9: 1306 | alg = A_Star(Point(sx, sy), Point(ex, ey), self.Map) 1307 | pass 1308 | return alg 1309 | 1310 | # 结束搜索事件 1311 | def event_EndSearch(self): 1312 | self.centerTimer.stop() 1313 | self.search = None 1314 | self.yi = None 1315 | self.special = None 1316 | Point.clear() 1317 | self.button_Run.setEnabled(True) 1318 | self.button_ClearMap.setEnabled(True) 1319 | self.button_InputMap.setEnabled(True) 1320 | self.button_CreatetMap.setEnabled(True) 1321 | self.button_RandomMap.setEnabled(True) 1322 | self.repaint() 1323 | 1324 | # #重写内置事件 1325 | # 1326 | # 鼠标点击事件 1327 | def mousePressEvent(self, event): # event是鼠标事件,包含了坐标x、y元素 1328 | # 获取鼠标点击地图中的坐标 1329 | x, y = event.x() - Config.MAP_X, event.y() - Config.MAP_Y 1330 | 1331 | # 将x和y整除Config.blockLength结果赋给x,y 1332 | x = x // Config.blockLength 1333 | y = y // Config.blockLength 1334 | 1335 | if 0 <= x <= 19 and 0 <= y <= 19: 1336 | print(f"鼠标点击坐标[{x},{y}]") 1337 | 1338 | # 判读鼠标响应事件范围 1339 | if 0 <= x < Config.WIDTH and 0 <= y < Config.HEIGHT: 1340 | # 鼠标左键点击 1341 | if Qt.LeftButton == event.button(): 1342 | # 判断当前点是否是起点或者终点 1343 | if (x, y) != self.startPoint and (x, y) != self.endPoint: 1344 | # 地图块1置为0,0置为1 1345 | if self.Map[y][x] == 0: 1346 | self.ToNote('N: 坐标(%d,%d),设置为墙' % (x, y)) 1347 | else: 1348 | self.ToNote('N: 坐标(%d,%d),墙移除' % (x, y)) 1349 | self.Map[y][x] = (1 if self.Map[y][x] == 0 else 0) 1350 | 1351 | if Qt.RightButton == event.button(): 1352 | if self.Map[y][x] == 0: 1353 | if self.startPoint is None: 1354 | self.startPoint = (x, y) 1355 | self.ToNote('N: 添加起点:(%d,%d)' % (x, y)) 1356 | elif self.endPoint is None and self.startPoint != (x, y): 1357 | self.endPoint = (x, y) 1358 | self.ToNote('N: 添加终点:(%d,%d)' % (x, y)) 1359 | else: 1360 | self.ToNote('N: 清除起点终点') 1361 | self.endPoint = None 1362 | self.startPoint = None 1363 | # 重绘 1364 | self.repaint() 1365 | 1366 | # 绘画事件 1367 | def paintEvent(self, event): 1368 | qp = QPainter() 1369 | qp.begin(self) 1370 | self.drawSearch(qp) 1371 | qp.end() 1372 | 1373 | # 绘制搜索路径 1374 | def drawSearch(self, qp): 1375 | pen = QPen(QColor(8, 44, 137), 2, Qt.SolidLine) # 8, 44, 137 1376 | qp.setPen(pen) 1377 | # 画地图 1378 | self.drawMap(qp) 1379 | 1380 | # 画当前点 1381 | if self.search is not None: 1382 | if self.special is not None: 1383 | qp.setBrush(QColor(24, 90, 54)) # 绿色 24, 90, 54 1384 | qp.drawRect(Config.MAP_X + self.special[0].x * Config.blockLength, 1385 | Config.MAP_Y + self.special[0].y * Config.blockLength, 1386 | Config.blockLength, Config.blockLength) 1387 | for p in self.special[2]: # 24, 92, 55 1388 | qp.setBrush(QColor(24, 92, 55)) # 深绿色 1389 | qp.drawRect(Config.MAP_X + p.x * Config.blockLength, 1390 | Config.MAP_Y + p.y * Config.blockLength, 1391 | Config.blockLength, Config.blockLength) 1392 | for i in self.search.result: # 28, 214, 108 1393 | qp.setBrush(QColor(28, 214, 108)) # 绿 1394 | qp.drawRect(Config.MAP_X + i.x * Config.blockLength, 1395 | Config.MAP_Y + i.y * Config.blockLength, 1396 | Config.blockLength, Config.blockLength) 1397 | 1398 | for i in range(len(self.Map)): 1399 | for j in range(len(self.Map[i])): 1400 | # 配置方格颜色 1401 | if (j, i) == self.startPoint: 1402 | qp.setBrush(QColor(243, 139, 0)) # 橘色起点 1403 | qp.drawRect(Config.MAP_X + j * Config.blockLength, 1404 | Config.MAP_Y + i * Config.blockLength, 1405 | Config.blockLength, Config.blockLength) 1406 | elif (j, i) == self.endPoint: 1407 | qp.setBrush(QColor(225, 218, 48)) # 黄色终点 1408 | qp.drawRect(Config.MAP_X + j * Config.blockLength, 1409 | Config.MAP_Y + i * Config.blockLength, 1410 | Config.blockLength, Config.blockLength) 1411 | else: 1412 | pass 1413 | 1414 | # 绘制地图 1415 | def drawMap(self, qp): 1416 | # 根据Map画方格 1417 | for i in range(len(self.Map)): 1418 | for j in range(len(self.Map[i])): 1419 | if self.Map[i][j] == 0: 1420 | qp.setBrush(QColor(4, 11, 4)) 1421 | else: # 24, 90, 189 1422 | qp.setBrush(QColor(24, 90, 189)) # 255, 214, 98 1423 | # 画方格 1424 | qp.drawRect(Config.MAP_X + j * Config.blockLength, 1425 | Config.MAP_Y + i * Config.blockLength, 1426 | Config.blockLength, Config.blockLength) 1427 | 1428 | # 时间事件,隔一段时间执行一次 1429 | def timerEvent(self, e): 1430 | print("") 1431 | try: 1432 | data = next(self.yi) 1433 | except Exception as e: 1434 | self.ToNote('N: 搜索结束 !') 1435 | if self.search.result is None: 1436 | self.ToNote('N: 未找到可行路径!') 1437 | else: 1438 | self.ToNote('N: 总计搜索节点数:%d' % self.search.searchCount) 1439 | self.ToNote('N: 最终路径长度:%d' % len(self.search.result)) 1440 | self.centerTimer.stop() 1441 | self.search = None 1442 | self.yi = None 1443 | self.special = None 1444 | Point.clear() 1445 | self.button_Run.setEnabled(True) 1446 | self.button_ClearMap.setEnabled(True) 1447 | self.button_InputMap.setEnabled(True) 1448 | self.button_CreatetMap.setEnabled(True) 1449 | self.button_RandomMap.setEnabled(True) 1450 | else: 1451 | self.special = data 1452 | self.repaint() 1453 | -------------------------------------------------------------------------------- /A_star/main_Ui.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Form implementation generated from reading ui file 'main_Ui.ui' 4 | # 5 | # Created by: PyQt5 UI code generator 5.15.4 6 | # 7 | # WARNING: Any manual changes made to this file will be lost when pyuic5 is 8 | # run again. Do not edit this file unless you know what you are doing. 9 | 10 | 11 | from PyQt5 import QtCore, QtGui, QtWidgets 12 | 13 | 14 | class Ui_mainWindow(object): 15 | def setupUi(self, mainWindow): 16 | mainWindow.setObjectName("mainWindow") 17 | mainWindow.resize(1150, 790) 18 | self.centralwidget = QtWidgets.QWidget(mainWindow) 19 | self.centralwidget.setObjectName("centralwidget") 20 | self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget) 21 | self.groupBox_3.setGeometry(QtCore.QRect(10, 10, 221, 181)) 22 | font = QtGui.QFont() 23 | font.setFamily("微软雅黑") 24 | font.setPointSize(12) 25 | self.groupBox_3.setFont(font) 26 | self.groupBox_3.setObjectName("groupBox_3") 27 | self.textBrowser = QtWidgets.QTextBrowser(self.groupBox_3) 28 | self.textBrowser.setGeometry(QtCore.QRect(10, 20, 201, 151)) 29 | self.textBrowser.setObjectName("textBrowser") 30 | self.groupBox = QtWidgets.QGroupBox(self.centralwidget) 31 | self.groupBox.setGeometry(QtCore.QRect(10, 190, 221, 211)) 32 | font = QtGui.QFont() 33 | font.setFamily("微软雅黑") 34 | font.setPointSize(12) 35 | self.groupBox.setFont(font) 36 | self.groupBox.setObjectName("groupBox") 37 | self.button_Run = QtWidgets.QPushButton(self.groupBox) 38 | self.button_Run.setGeometry(QtCore.QRect(50, 30, 111, 31)) 39 | font = QtGui.QFont() 40 | font.setPointSize(10) 41 | self.button_Run.setFont(font) 42 | self.button_Run.setObjectName("button_Run") 43 | self.comboBoxSearch = QtWidgets.QComboBox(self.groupBox) 44 | self.comboBoxSearch.setGeometry(QtCore.QRect(100, 120, 91, 31)) 45 | sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) 46 | sizePolicy.setHorizontalStretch(0) 47 | sizePolicy.setVerticalStretch(0) 48 | sizePolicy.setHeightForWidth(self.comboBoxSearch.sizePolicy().hasHeightForWidth()) 49 | self.comboBoxSearch.setSizePolicy(sizePolicy) 50 | font = QtGui.QFont() 51 | font.setPointSize(12) 52 | font.setKerning(True) 53 | self.comboBoxSearch.setFont(font) 54 | self.comboBoxSearch.setObjectName("comboBoxSearch") 55 | self.comboBoxSearch.addItem("") 56 | self.comboBoxSearch.addItem("") 57 | self.comboBoxSearch.addItem("") 58 | self.comboBoxSearch.addItem("") 59 | self.comboBoxSearch.addItem("") 60 | self.comboBoxSearch.addItem("") 61 | self.comboBoxSearch.addItem("") 62 | self.comboBoxSearch.addItem("") 63 | self.comboBoxSearch.addItem("") 64 | self.comboBoxSearch.addItem("") 65 | self.label = QtWidgets.QLabel(self.groupBox) 66 | self.label.setGeometry(QtCore.QRect(20, 120, 81, 31)) 67 | font = QtGui.QFont() 68 | font.setFamily("微软雅黑") 69 | font.setPointSize(12) 70 | self.label.setFont(font) 71 | self.label.setObjectName("label") 72 | self.button_EndSearch = QtWidgets.QPushButton(self.groupBox) 73 | self.button_EndSearch.setGeometry(QtCore.QRect(50, 70, 111, 31)) 74 | font = QtGui.QFont() 75 | font.setPointSize(10) 76 | self.button_EndSearch.setFont(font) 77 | self.button_EndSearch.setObjectName("button_EndSearch") 78 | self.label_2 = QtWidgets.QLabel(self.groupBox) 79 | self.label_2.setGeometry(QtCore.QRect(20, 170, 71, 31)) 80 | font = QtGui.QFont() 81 | font.setFamily("微软雅黑") 82 | font.setPointSize(12) 83 | self.label_2.setFont(font) 84 | self.label_2.setObjectName("label_2") 85 | self.horizontalSlider_Speed = QtWidgets.QSlider(self.groupBox) 86 | self.horizontalSlider_Speed.setGeometry(QtCore.QRect(100, 180, 111, 21)) 87 | self.horizontalSlider_Speed.setMinimum(1) 88 | self.horizontalSlider_Speed.setMaximum(100) 89 | self.horizontalSlider_Speed.setPageStep(1) 90 | self.horizontalSlider_Speed.setOrientation(QtCore.Qt.Horizontal) 91 | self.horizontalSlider_Speed.setObjectName("horizontalSlider_Speed") 92 | self.label_5 = QtWidgets.QLabel(self.groupBox) 93 | self.label_5.setGeometry(QtCore.QRect(100, 160, 21, 21)) 94 | self.label_5.setObjectName("label_5") 95 | self.label_6 = QtWidgets.QLabel(self.groupBox) 96 | self.label_6.setGeometry(QtCore.QRect(190, 160, 21, 21)) 97 | self.label_6.setObjectName("label_6") 98 | self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget) 99 | self.groupBox_2.setGeometry(QtCore.QRect(10, 400, 221, 161)) 100 | font = QtGui.QFont() 101 | font.setFamily("微软雅黑") 102 | font.setPointSize(10) 103 | font.setBold(False) 104 | font.setWeight(50) 105 | self.groupBox_2.setFont(font) 106 | self.groupBox_2.setObjectName("groupBox_2") 107 | self.button_CreatetMap = QtWidgets.QPushButton(self.groupBox_2) 108 | self.button_CreatetMap.setGeometry(QtCore.QRect(10, 30, 91, 31)) 109 | self.button_CreatetMap.setObjectName("button_CreatetMap") 110 | self.button_InputMap = QtWidgets.QPushButton(self.groupBox_2) 111 | self.button_InputMap.setGeometry(QtCore.QRect(120, 30, 91, 31)) 112 | self.button_InputMap.setObjectName("button_InputMap") 113 | self.button_ClearMap = QtWidgets.QPushButton(self.groupBox_2) 114 | self.button_ClearMap.setGeometry(QtCore.QRect(10, 70, 91, 31)) 115 | self.button_ClearMap.setObjectName("button_ClearMap") 116 | self.label_3 = QtWidgets.QLabel(self.groupBox_2) 117 | self.label_3.setGeometry(QtCore.QRect(20, 120, 71, 31)) 118 | font = QtGui.QFont() 119 | font.setFamily("微软雅黑") 120 | font.setPointSize(12) 121 | self.label_3.setFont(font) 122 | self.label_3.setObjectName("label_3") 123 | self.comboBoxBlockSize = QtWidgets.QComboBox(self.groupBox_2) 124 | self.comboBoxBlockSize.setGeometry(QtCore.QRect(100, 120, 61, 31)) 125 | self.comboBoxBlockSize.setObjectName("comboBoxBlockSize") 126 | self.label_4 = QtWidgets.QLabel(self.groupBox_2) 127 | self.label_4.setGeometry(QtCore.QRect(170, 120, 21, 31)) 128 | font = QtGui.QFont() 129 | font.setFamily("微软雅黑") 130 | font.setPointSize(12) 131 | self.label_4.setFont(font) 132 | self.label_4.setObjectName("label_4") 133 | self.button_RandomMap = QtWidgets.QPushButton(self.groupBox_2) 134 | self.button_RandomMap.setGeometry(QtCore.QRect(120, 72, 91, 31)) 135 | self.button_RandomMap.setObjectName("button_RandomMap") 136 | self.groupBox_4 = QtWidgets.QGroupBox(self.centralwidget) 137 | self.groupBox_4.setGeometry(QtCore.QRect(0, 560, 231, 191)) 138 | font = QtGui.QFont() 139 | font.setFamily("微软雅黑") 140 | font.setPointSize(10) 141 | self.groupBox_4.setFont(font) 142 | self.groupBox_4.setObjectName("groupBox_4") 143 | self.plainTextEdit_ShowMessage = QtWidgets.QPlainTextEdit(self.groupBox_4) 144 | self.plainTextEdit_ShowMessage.setGeometry(QtCore.QRect(10, 20, 211, 161)) 145 | self.plainTextEdit_ShowMessage.setObjectName("plainTextEdit_ShowMessage") 146 | mainWindow.setCentralWidget(self.centralwidget) 147 | self.menubar = QtWidgets.QMenuBar(mainWindow) 148 | self.menubar.setGeometry(QtCore.QRect(0, 0, 1150, 23)) 149 | self.menubar.setObjectName("menubar") 150 | self.menuasd = QtWidgets.QMenu(self.menubar) 151 | self.menuasd.setObjectName("menuasd") 152 | self.menu = QtWidgets.QMenu(self.menubar) 153 | self.menu.setObjectName("menu") 154 | mainWindow.setMenuBar(self.menubar) 155 | self.statusbar = QtWidgets.QStatusBar(mainWindow) 156 | self.statusbar.setObjectName("statusbar") 157 | mainWindow.setStatusBar(self.statusbar) 158 | self.action11 = QtWidgets.QAction(mainWindow) 159 | self.action11.setObjectName("action11") 160 | self.action111 = QtWidgets.QAction(mainWindow) 161 | self.action111.setObjectName("action111") 162 | self.action299263121 = QtWidgets.QAction(mainWindow) 163 | self.action299263121.setObjectName("action299263121") 164 | self.menuasd.addAction(self.action11) 165 | self.menu.addAction(self.action111) 166 | self.menu.addAction(self.action299263121) 167 | self.menubar.addAction(self.menuasd.menuAction()) 168 | self.menubar.addAction(self.menu.menuAction()) 169 | 170 | self.retranslateUi(mainWindow) 171 | QtCore.QMetaObject.connectSlotsByName(mainWindow) 172 | 173 | def retranslateUi(self, mainWindow): 174 | _translate = QtCore.QCoreApplication.translate 175 | mainWindow.setWindowTitle(_translate("mainWindow", "搜索算法演示1.1.0 Geek")) 176 | self.groupBox_3.setTitle(_translate("mainWindow", "使用帮助")) 177 | self.textBrowser.setHtml(_translate("mainWindow", "\n" 178 | "\n" 181 | "

1.鼠标左键,设定地图中的墙,双击撤销;

\n" 182 | "

2.鼠标右键,设定起始点、终止点;再次右键,则清除起止点

\n" 183 | "

3.请注意导入的map地图必须命名为map.txt,注意导入的地图和设置的方块大小匹配,地图生成在./map中

\n" 184 | "

好奇之心,改变之力

")) 185 | self.groupBox.setTitle(_translate("mainWindow", "搜索控制")) 186 | self.button_Run.setText(_translate("mainWindow", "开始搜索")) 187 | self.comboBoxSearch.setItemText(0, _translate("mainWindow", "BFS")) 188 | self.comboBoxSearch.setItemText(1, _translate("mainWindow", "DFS")) 189 | self.comboBoxSearch.setItemText(2, _translate("mainWindow", "DLS")) 190 | self.comboBoxSearch.setItemText(3, _translate("mainWindow", "IDS")) 191 | self.comboBoxSearch.setItemText(4, _translate("mainWindow", "BS")) 192 | self.comboBoxSearch.setItemText(5, _translate("mainWindow", "HCS")) 193 | self.comboBoxSearch.setItemText(6, _translate("mainWindow", "BeamS")) 194 | self.comboBoxSearch.setItemText(7, _translate("mainWindow", "GBFS")) 195 | self.comboBoxSearch.setItemText(8, _translate("mainWindow", "UCS")) 196 | self.comboBoxSearch.setItemText(9, _translate("mainWindow", "A*")) 197 | self.label.setText(_translate("mainWindow", "搜索算法:")) 198 | self.button_EndSearch.setText(_translate("mainWindow", "结束搜索")) 199 | self.label_2.setText(_translate("mainWindow", "搜索倍速:")) 200 | self.label_5.setText(_translate("mainWindow", "慢")) 201 | self.label_6.setText(_translate("mainWindow", "快")) 202 | self.groupBox_2.setTitle(_translate("mainWindow", "地图控制")) 203 | self.button_CreatetMap.setText(_translate("mainWindow", "生成地图")) 204 | self.button_InputMap.setText(_translate("mainWindow", "导入地图")) 205 | self.button_ClearMap.setText(_translate("mainWindow", "清空地图")) 206 | self.label_3.setText(_translate("mainWindow", "方块大小:")) 207 | self.label_4.setText(_translate("mainWindow", "px")) 208 | self.button_RandomMap.setText(_translate("mainWindow", "地图随机生成")) 209 | self.groupBox_4.setTitle(_translate("mainWindow", "Note")) 210 | self.menuasd.setTitle(_translate("mainWindow", "文件")) 211 | self.menu.setTitle(_translate("mainWindow", "帮助")) 212 | self.action11.setText(_translate("mainWindow", "Geek is Love")) 213 | self.action111.setText(_translate("mainWindow", "Geek - QQ")) 214 | self.action299263121.setText(_translate("mainWindow", "299263121")) 215 | -------------------------------------------------------------------------------- /A_star/main_Ui.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | mainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 1150 10 | 790 11 | 12 | 13 | 14 | 搜索算法演示1.1.0 Geek 15 | 16 | 17 | 18 | 19 | 20 | 10 21 | 10 22 | 221 23 | 181 24 | 25 | 26 | 27 | 28 | 微软雅黑 29 | 12 30 | 31 | 32 | 33 | 使用帮助 34 | 35 | 36 | 37 | 38 | 10 39 | 20 40 | 201 41 | 151 42 | 43 | 44 | 45 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> 46 | <html><head><meta name="qrichtext" content="1" /><style type="text/css"> 47 | p, li { white-space: pre-wrap; } 48 | </style></head><body style=" font-family:'微软雅黑'; font-size:12pt; font-weight:400; font-style:normal;"> 49 | <p align="justify" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'SimSun'; font-size:10pt; font-weight:600;">1.</span><span style=" font-family:'SimSun'; font-size:10pt;">鼠标左键,设定地图中的墙,双击撤销;</span></p> 50 | <p align="justify" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'SimSun'; font-size:10pt; font-weight:600;">2.</span><span style=" font-family:'SimSun'; font-size:10pt;">鼠标右键,设定起始点、终止点;再次右键,则清除起止点</span></p> 51 | <p align="justify" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'SimSun'; font-size:10pt; font-weight:600;">3.</span><span style=" font-family:'SimSun'; font-size:10pt;">请注意导入的map地图必须命名为map.txt,注意导入的地图和设置的方块大小匹配,地图生成在./map中</span></p> 52 | <p align="center" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'SimSun'; font-size:9pt; font-weight:600;">好奇之心,改变之力</span></p></body></html> 53 | 54 | 55 | 56 | 57 | 58 | 59 | 10 60 | 190 61 | 221 62 | 211 63 | 64 | 65 | 66 | 67 | 微软雅黑 68 | 12 69 | 70 | 71 | 72 | 搜索控制 73 | 74 | 75 | 76 | 77 | 50 78 | 30 79 | 111 80 | 31 81 | 82 | 83 | 84 | 85 | 10 86 | 87 | 88 | 89 | 开始搜索 90 | 91 | 92 | 93 | 94 | 95 | 100 96 | 120 97 | 91 98 | 31 99 | 100 | 101 | 102 | 103 | 0 104 | 0 105 | 106 | 107 | 108 | 109 | 12 110 | true 111 | 112 | 113 | 114 | 115 | BFS 116 | 117 | 118 | 119 | 120 | DFS 121 | 122 | 123 | 124 | 125 | DLS 126 | 127 | 128 | 129 | 130 | IDS 131 | 132 | 133 | 134 | 135 | BS 136 | 137 | 138 | 139 | 140 | HCS 141 | 142 | 143 | 144 | 145 | BeamS 146 | 147 | 148 | 149 | 150 | GBFS 151 | 152 | 153 | 154 | 155 | UCS 156 | 157 | 158 | 159 | 160 | A* 161 | 162 | 163 | 164 | 165 | 166 | 167 | 20 168 | 120 169 | 81 170 | 31 171 | 172 | 173 | 174 | 175 | 微软雅黑 176 | 12 177 | 178 | 179 | 180 | 搜索算法: 181 | 182 | 183 | 184 | 185 | 186 | 50 187 | 70 188 | 111 189 | 31 190 | 191 | 192 | 193 | 194 | 10 195 | 196 | 197 | 198 | 结束搜索 199 | 200 | 201 | 202 | 203 | 204 | 20 205 | 170 206 | 71 207 | 31 208 | 209 | 210 | 211 | 212 | 微软雅黑 213 | 12 214 | 215 | 216 | 217 | 搜索倍速: 218 | 219 | 220 | 221 | 222 | 223 | 100 224 | 180 225 | 111 226 | 21 227 | 228 | 229 | 230 | 1 231 | 232 | 233 | 100 234 | 235 | 236 | 1 237 | 238 | 239 | Qt::Horizontal 240 | 241 | 242 | 243 | 244 | 245 | 100 246 | 160 247 | 21 248 | 21 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 190 259 | 160 260 | 21 261 | 21 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 10 273 | 400 274 | 221 275 | 161 276 | 277 | 278 | 279 | 280 | 微软雅黑 281 | 10 282 | 50 283 | false 284 | 285 | 286 | 287 | 地图控制 288 | 289 | 290 | 291 | 292 | 10 293 | 30 294 | 91 295 | 31 296 | 297 | 298 | 299 | 生成地图 300 | 301 | 302 | 303 | 304 | 305 | 120 306 | 30 307 | 91 308 | 31 309 | 310 | 311 | 312 | 导入地图 313 | 314 | 315 | 316 | 317 | 318 | 10 319 | 70 320 | 91 321 | 31 322 | 323 | 324 | 325 | 清空地图 326 | 327 | 328 | 329 | 330 | 331 | 20 332 | 120 333 | 71 334 | 31 335 | 336 | 337 | 338 | 339 | 微软雅黑 340 | 12 341 | 342 | 343 | 344 | 方块大小: 345 | 346 | 347 | 348 | 349 | 350 | 100 351 | 120 352 | 61 353 | 31 354 | 355 | 356 | 357 | 358 | 359 | 360 | 170 361 | 120 362 | 21 363 | 31 364 | 365 | 366 | 367 | 368 | 微软雅黑 369 | 12 370 | 371 | 372 | 373 | px 374 | 375 | 376 | 377 | 378 | 379 | 120 380 | 72 381 | 91 382 | 31 383 | 384 | 385 | 386 | 地图随机生成 387 | 388 | 389 | 390 | 391 | 392 | 393 | 0 394 | 560 395 | 231 396 | 191 397 | 398 | 399 | 400 | 401 | 微软雅黑 402 | 10 403 | 404 | 405 | 406 | Note 407 | 408 | 409 | 410 | 411 | 10 412 | 20 413 | 211 414 | 161 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 0 424 | 0 425 | 1150 426 | 23 427 | 428 | 429 | 430 | 431 | 文件 432 | 433 | 434 | 435 | 436 | 437 | 帮助 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | Geek is Love 449 | 450 | 451 | 452 | 453 | Geek - QQ 454 | 455 | 456 | 457 | 458 | 299263121 459 | 460 | 461 | 462 | 463 | 464 | 465 | -------------------------------------------------------------------------------- /A_star/map.txt: -------------------------------------------------------------------------------- 1 | [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] -------------------------------------------------------------------------------- /A_star/map/map.txt: -------------------------------------------------------------------------------- 1 | [[0, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 1], [0, 1, 0, 1, 0, 0], [0, 1, 0, 1, 0, 0], [1, 1, 1, 0, 1, 0]] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Bsheepcoder 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # background 2 | - 一个演示搜索类算法的地图模拟器 3 | 4 | 这个项目的最初版本源自一个B站up主 Minicking开放的代码,在其代码的基础上我进行了学习、更改和扩充 5 | ,视频链接[A*寻路算法动态演示](https://www.bilibili.com/video/BV1FJ411j7oG?share_source=copy_web&vd_source=e09e7d9860329cf23712dbc79e5ed45e) 6 | 7 | # usage 8 | 9 | 目前使用帮助都写到了程序的页面,按照提示操作即可。 10 | 11 | **注意事项:** 12 | - **先生成地图再导入地图**,地图的文件名必须为map.txt,且将文件放在./map目录下 13 | - 在导入前**导入的地图是否与当前的地图方块数**匹配,目前还没做报错、提醒的优化 14 | 15 | # badge 16 | 17 | 现在的版本:1.1.0,使用python、PyQt5开发 18 | 19 | 技术点: 20 | 21 | - 用python的类中的__new__ 方法实现点对象的唯一存储 22 | - 用算法结合画图事件和时间事件的逻辑 23 | - 用生成器来构造算法的执行过程 24 | - 判断两点间的距离采用曼哈顿距离,相对欧拉距离速度更快 25 | - 各类搜素算法的实现 26 | 27 | # example 28 | ![Geek_YYDS](https://img-blog.csdnimg.cn/b298ebeb9d4346b7ab4d17e76c85bbd1.gif) 29 | # maintainers 30 | 新思路Geek组成员某东,负责bug的修复,功能完善和添加算法 31 | 32 | # contributing 33 | 34 | - B站up主 [Minicking](https://space.bilibili.com/105229830),贡献了最初的代码 35 | 36 | # license 37 | 38 | [新思路](许可证) © Geek 39 | 40 | # Change log 41 | 42 | ## 2022.9.5 版本更新至1.1.0 43 | 44 | - 修改了调倍速的模块和优化了倍速体验 45 | - 添加了深度受限算法DLS、迭代加深算法IDS(开始默认depth为2,每次搜索depth+1) 46 | - 添加了双向搜素BS、爬山搜素HCS、集束搜素BeamS(W = 3)、最佳优先搜素BFS、统一代价搜素UCS 47 | - 添加了A*搜索,还想添加其他的,有感兴趣的来帮忙迭代呀! 48 | - 显示搜素完成后的最短路径 49 | --------------------------------------------------------------------------------