Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 广度优先搜索算法太慢_Python_Breadth First Search_Path Finding - Fatal编程技术网

Python 广度优先搜索算法太慢

Python 广度优先搜索算法太慢,python,breadth-first-search,path-finding,Python,Breadth First Search,Path Finding,我试图编程一个人工智能。我用广度优先搜索为路径查找编写了这个脚本,但它似乎很慢。我不知道如何使它更快。请帮帮我。顺便说一下,如果你需要的话,我用了这个教程 导入队列 类AIRoadMap(对象): 定义初始(自我,游戏): self.game=游戏 self.road_list=[] self.initialize_road_list() def初始化道路列表(自): #此方法将所有空坐标添加到列表(self.road_列表) 对于self.game.map中的y_行: y_coord=self

我试图编程一个人工智能。我用广度优先搜索为路径查找编写了这个脚本,但它似乎很慢。我不知道如何使它更快。请帮帮我。顺便说一下,如果你需要的话,我用了这个教程

导入队列
类AIRoadMap(对象):
定义初始(自我,游戏):
self.game=游戏
self.road_list=[]
self.initialize_road_list()
def初始化道路列表(自):
#此方法将所有空坐标添加到列表(self.road_列表)
对于self.game.map中的y_行:
y_coord=self.game.map.index(y_行)
对于y_行中的x:
如果x['symbol']='.':
x_坐标=y_行索引(x)
self.road_list.append({'x':x_-coord,'y':y_-coord})
def check_valid_移动(自、x_from、y_from、移动):
不真实的x=来自
不真实的
有效=错误
对于移入移动:
如果移动==“L”:
不真实_x-=1
elif move==“R”:
不真实的x+=1
elif move==“U”:
不真实的y-=1
elif move==“D”:
不真实的y+=1
有效=错误
对于self.road_列表中的项目:
如果项目['x']==不真实的x和项目['y']==不真实的y:
有效=真
如果valid为False:
打破
返回有效
def IsPathEnd(self,x_from,y_from,to_x,to_y,移动):
不真实的x=来自
不真实的
对于移入移动:
如果移动==“L”:
不真实_x-=1
elif move==“R”:
不真实的x+=1
elif move==“U”:
不真实的y-=1
elif move==“D”:
不真实的y+=1
如果不真实的_y==to_y和不真实的_x==to_x:
返回真值
如果不真实x!=到x或不真实的y!=致:
返回错误
如果不真实x!=对x和不真实的y!=致:
返回错误
def生成路径(自、自、至、自、至):
nums=queue.queue()
nums.put(“”)
add=“”
而不是self.IsPathEnd(from_x,from_y,to_x,to_y,add):
add=nums.get()
#打印(添加)
对于j in[“L”、“R”、“U”、“D”]:
put=add+j
如果self.check\u valid\u移动(从\u x、从\u y、放置):
如果len(put)<3:
nums.put(put)
其他:
如果put[-1]=“L”和put[-2]!=“R”或put[-1]=“R”和put[-2]!=“L”或put[-1]=“U”和\
放[-2]!=“D”或put[-1]=“D”和put[-2]!=“U”:
nums.put(put)
#nums.put(put)
返回添加

方法
initialize_road_list()
将self.game.map中的所有空坐标添加到
self.road_list
,self.game.map基本上是一个网格

您的代码似乎有很多可能的改进,我现在只建议一些:

1) 替换:

2) 替换:

与:

3) 替换:

与:


好的,我按照您显示的方式修改了代码,但第三次替换基本上冻结了它。最大的改进可能来自更改
check\u valid\u moves
函数。目前,您正在遍历
项列表
(我假定这些是“可通过的方块”);但是这个列表是从网格中生成的(
self.game.map
)-检查网格中坐标x,y处的内容要比查找具有这些坐标的项目快得多(不需要循环)。Błotosmętek我像您一样更改了代码(将1替换为
any
),所以技术上它不包含循环,对吗?
import queue


class AIRoadMap(object):
    def __init__(self, game):
        self.game = game
        self.road_list = []
        self.initialize_road_list()

    def initialize_road_list(self):
        # This method adds all empty coordinates to list (self.road_list)
        for y_row in self.game.map:
            y_coord = self.game.map.index(y_row)
            for x in y_row:
                if x['symbol'] == '.':
                    x_coord = y_row.index(x)
                    self.road_list.append({'x' : x_coord, 'y' : y_coord})

    def check_valid_moves(self, x_from, y_from, moves):
        unreal_x = x_from
        unreal_y = y_from
        valid = False
        for move in moves:
            if move == "L":
                unreal_x -= 1

            elif move == "R":
                unreal_x += 1

            elif move == "U":
                unreal_y -= 1

            elif move == "D":
                unreal_y += 1
            valid = False
            for item in self.road_list:
                if item['x'] == unreal_x and item['y'] == unreal_y:
                    valid = True
            if valid is False:
                break
        return valid

    def IsPathEnd(self, x_from, y_from, to_x, to_y, moves):
        unreal_x = x_from
        unreal_y = y_from
        for move in moves:
            if move == "L":
                unreal_x -= 1

            elif move == "R":
                unreal_x += 1

            elif move == "U":
                unreal_y -= 1

            elif move == "D":
                unreal_y += 1

        if unreal_y == to_y and unreal_x == to_x:
            return True
        if unreal_x != to_x or unreal_y != to_y:
            return False
        if unreal_x != to_x and unreal_y != to_y:
            return False

    def generate_road(self, from_x, to_x, from_y, to_y):
        nums = queue.Queue()
        nums.put("")
        add = ""

        while not self.IsPathEnd(from_x, from_y, to_x, to_y, add):
            add = nums.get()
            # print(add)
            for j in ["L", "R", "U", "D"]:
                put = add + j
                if self.check_valid_moves(from_x, from_y, put):
                    if len(put) < 3:
                        nums.put(put)
                    else:
                        if put[-1] == "L" and put[-2] != "R" or put[-1] == "R" and put[-2] != "L" or put[-1] == "U" and \
                                put[-2] != "D" or put[-1] == "D" and put[-2] != "U":
                            nums.put(put)
                    #nums.put(put)

        return add
        valid = False
        for item in self.road_list:
            if item['x'] == unreal_x and item['y'] == unreal_y:
                valid = True
        if valid is False:
            break
    return valid
       if not any(item['x'] == unreal_x and item['y'] == unreal_y for item in self.road_list):
           return False
   return True
    if unreal_y == to_y and unreal_x == to_x:
        return True
    if unreal_x != to_x or unreal_y != to_y:
        return False
    if unreal_x != to_x and unreal_y != to_y:
        return False
    return unreal_y == to_y and unreal_x == to_x
if put[-1] == "L" and put[-2] != "R" or put[-1] == "R" and put[-2] != "L" or put[-1] == "U" and \
                                put[-2] != "D" or put[-1] == "D" and put[-2] != "U":
if put[-2:] in frozenset(('LR', 'RL', 'UD', 'DU')):