def process(self): """ Accept Start and Goal Puzzle state""" #start = self.puzzle start = self.puzzle goal = self.goal() start = Node(start, 0, 0) start.fval = self.f(start, goal) """ Put the start node in the open list""" self.open.append(start) print("\n\n") #a = 0 path = [] cur = self.open[0] while(True): #a += 1 #print(a) cur = self.open[0] if(self.h(cur.data, goal) == 0): break for i in cur.generate_child(): i.parent = cur i.fval = self.f(i, goal) if self.stateInStates(i, self.closed): continue if not self.stateInStates(i, self.open): self.open.append(i) if self.stateInStates(i, self.open) and i.fval < self.GetStateFromStates(i, self.open).fval: self.open.remove(self.GetStateFromStates(i, self.open)) self.open.append(i) self.closed.append(cur) del self.open[0] """ sort the opne list based on f value """ self.open.sort(key=lambda x: x.fval, reverse=False) # for i in self.open: # print(i.data, i.fval, i.level) while(cur != start): path.append(cur) cur = cur.parent path.append(start) path.reverse() startRow, startColumn = FuntionTools.FindNumberFormMatrix(path[0].data, 0) print("where zero at beginning?") print(startRow, startColumn) print("steps = %d" % (len(path) - 1)) # 算走法 movePath = [] for step in range(1, len(path)): startRow, startColumn, moveStep = FuntionTools.GetMove(startRow, startColumn, path[step].data) movePath.append(moveStep) print("A* step: %d" % step) for row in path[step].data: print(row) return path, movePath
def ClickSaveButton(self): self.data.SaveData(self.puzzlePath, self.movePath, self.step + self.storeStep, self.totalStep) self.data.SetPuzzle(self.puzzle) # self.data.SetPuzzle(self.bestSearch.GetNowPuzzleState(self.step)) # self.data.SetPuzzle(self.aStar.GetStateFromStates(self.step)) print("test") print(self.data.GetPuzzle()) print(type(self.data.GetPuzzle())) print("Click save data") FuntionTools.writeJson("data.json", self.data)
def ClickImageButtion(self, button): # buttonPuzzle = [] # for i in self.buttonList: # btnl = [] # for j in i: # btnl.append(j.name()) # buttonPuzzle.append(btnl) size = 100 posx, posy = button.pos().x(), button.pos().y() btnRow, btnCol = posy // size, posx // size print("{%d, %d}" % (btnRow, btnCol)) if FuntionTools.IsAround((self.nullBtnIndexRow, self.nullBtnIndexCol), (btnRow, btnCol)): #self.Move() movedir = FuntionTools.GetPlayerMove((self.nullBtnIndexRow, self.nullBtnIndexCol), (btnRow, btnCol)) self.Move(movedir) self.IsUserPlayed = True
def AStar_ComputePath(self): self.aStar = AS.Puzzle(self.puzzle) self.nullBtnIndexRow, self.nullBtnIndexCol = FuntionTools.FindNumberFormMatrix(self.puzzle, 0) self.puzzlePath = self.aStar.GetPath() self.totalStep = self.aStar.GetTotalStep() self.movePath = self.aStar.GetMovePath() self.storeStep += self.step self.step = 0 self.IsUserPlayed = False
def AI_ComputePath(self): self.nullBtnIndexRow, self.nullBtnIndexCol = FuntionTools.FindNumberFormMatrix(self.puzzle, 0) self.puzzlePath = PA.NPuzzle(self.nullBtnIndexCol, self.nullBtnIndexRow, self.puzzle) self.bestSearch = PA.test_best_first_search(self.puzzlePath) self.movePath = self.bestSearch.GetMovePath() self.totalStep = self.bestSearch.GetTotalStep() self.storeStep += self.step self.step = 0 self.IsUserPlayed = False
def ClickLoadButton(self): message = "Goto3" dataDict = FuntionTools.readJson("data.json") self.data.SetData(dataDict) self.data.SetPuzzle(dataDict["puzzle"]) self.data.SetButtonCount(dataDict["rowButtonCount"]) #self.data.SetPixmapList(dataDict["pixmapList"]) self.data.SetImagePath(dataDict["imagePath"]) if self.data.GetImagePath(): self.imgCtrl.LoadImage(self.data.GetImagePath()) self.imgCtrl.SetImageList(self.data.GetButtonCount()) print("Click load") self.data.dataSignal.Shoot(message)
def best_first_search(start, max_times): """利用最佳優先搜尋來尋找目標狀態。回傳目標狀態以及路徑""" def creat_item(puzzle): """產生裝在堆疊中用的item,以長度為2的tuple儲存, 前者為評估函數,後者為推盤狀態。 """ return calc_diffrence(puzzle), puzzle # 儲存展開後狀態的上個狀態,用來回溯完成路徑 previous_states = dict() # 儲存已經拜訪過的狀態 visiteds = set() # 產生算法中所使用的堆疊,並把開始狀態裝入堆疊 heap = list() heapq.heappush(heap, creat_item(start)) times = 0 while heap and not heap[0][1].is_done() and times < max_times: # 如果堆疊還有元素可以展開,且最優先之狀態不等於目標狀態\ # 從堆疊中拿出最優先的狀態,並展開 diff, top_priority = heapq.heappop(heap) for state in top_priority.nextstates: if state not in visiteds: # 如果該狀態沒有被搜尋過,加入到堆疊中,並記錄其上一個狀態 heapq.heappush(heap, creat_item(state)) previous_states.setdefault(state, top_priority) visiteds.add(top_priority) times += 1 final = heap[0][1] if heap else None # 回溯路徑 path = list() movePath = list() current = final while current != start: path.append(current) current = previous_states[current] path.append(start) path.reverse() startRow, startColumn = FuntionTools.FindNumberFormMatrix( path[0].matrix, 0) print("rref") print(startRow, startColumn) # print("print step") # for step in path: # print(step) #算走法 for step in range(1, len(path)): startRow, startColumn, moveStep = FuntionTools.GetMove( startRow, startColumn, path[step].matrix) movePath.append(moveStep) # for state in movePath: # print(state) return final, path, movePath