曼哈顿和错误的启发式方法

时间:2019-03-06 18:51:48

标签: python a-star

抱歉,我是python新手。我设法实现了算法,但是
我对Python的了解无法做几件事。所以我在这里
我的算法需要做三件事
1-需要添加放错位置的瓷砖启发功能
2-需要添加“曼哈顿距离”启发式函数
3-我需要处理val = [0,3,2,5,8,7,1,6,4]而不是start1 =“ 012634785”一个字符串。我怎样才能做到这一点?将字符串转换为队列。

最后,我试图用A *搜索写出8个拼图
初始状态= [0,3,2,5,8,7,1,6,4]像这样。
最终状态= [0,1,2,3,4,5,6,7,8]
请帮我学习。
我知道有很多要问的地方,但我真的很感谢你们。

from queue import PriorityQueue
class State(object):
    def __init__ (self, value, parent, start = 0, goal = 0):
        self.children = []
        self.parent = parent
        self.value = value
        self.dist = 0
        if parent:
            self.path = parent.path[:]
            self.path.append(value)
            self.start = parent.start
            self.goal = parent.goal
        else:
            self.path = [value]
            self.start = start
            self.goal = goal

    def GetDist(self):
        pass
    def CreateChildren(self):
        pass

class State_String(State):
    def __init__ (self, value, parent, start = 0, goal = 0):
        super(State_String, self).__init__(value, parent, start, goal)
        self.dist = self.GetDist()
    def GetDist(self):
        if self.value == self.goal:
            return 0
        dist = 0
        for i in range(len(self.goal)):
            letter = self.goal[i]
            dist += abs(i - self.value.index(letter))
        return dist
    def CreateChildren(self):
        if not self.children:
            for i in list(range(len(self.goal)- 1)):
                val = self.value
                val = val[:i] + val[i + 1] + val[i] + val[i + 2:]
                child = State_String(val, self)
                self.children.append(child)

class AStar_Solver:
    def __init__(self, start, goal):
        self.path = []
        self.visitedQueue = []
        self.priorityQueue = PriorityQueue()
        self.start = start
        self.goal = goal

    def Solve(self):
        startState = State_String(self.start, 0, self.start, self.goal)
        count = 0
        self.priorityQueue.put((0, count, startState))
        while (not self.path and self.priorityQueue.qsize()):
            closestChild = self.priorityQueue.get()[2]
            closestChild.CreateChildren()
            self.visitedQueue.append(closestChild.value)
            for child in closestChild.children:
                if child.value not in self.visitedQueue:
                    count += 1
                    if not child.dist:
                        self.path = child.path
                        break
                    self.priorityQueue.put((child.dist, count, child))
        if not self.path:
            print("Goal of ", self.goal, " is not possible")
        return self.path

if __name__ == "__main__":
    start1 = "012634785"
    goal1 = "012345678"
    a = AStar_Solver (start1, goal1)
    a.Solve()
    for i in list(range(len(a.path))):
        print(a.path[i])

1 个答案:

答案 0 :(得分:2)

如果我得到您的要求,则需要输入“ 013749520348”并将其转换为整数列表。为此:

# initial value
string = "012634785"

# converted to integers
list_nums = [int(i) for i in string]

# sort list
list_nums.sort()

# returns
[0, 1, 2, 3, 4, 5, 6, 7, 8]

如果您想获得其他答案,例如启发式函数或已发布的任何代码,则需要编写一个新问题,以解决您遇到的特定问题和所获得的特定结果试图实现。