[英]How can I create a tree structure in Python?
我正在嘗試使AI能夠玩看起來像跳棋的游戲,其邏輯幾乎相同。 無論如何,我正在尋找使用蒙特卡洛樹搜索方法的方法,但是我不知道如何實現樹形結構。 如果我沒記錯的話,我的樹的根應該是初始狀態或板,而節點應該是所有可能的游戲。 我知道我必須創建一個函數來計算每個節點的權重並選擇最佳播放。 我的問題是,正如我之前所說,我不知道如何在python中實現所述樹。
到目前為止,我已經有了董事會和兩個職能部門,可返回您可以采取的法律行動清單。 木板是用10x10多維數組創建的,為了找到可能的移動,我有兩個功能可以接收要移動的工件的X和Y坐標,並檢查所有可用選項。 我有2個移動功能的原因是,一個功能用於基本運動,即當您旁邊的空間相鄰時,另一個功能檢查“跳”,即當您旁邊的空間被占用但該空間就在它旁邊是免費的。
我將在此處添加我的代碼,以防萬一它使你們更容易理解我正在嘗試做的事情。
import numpy as np
matrix = [[1,1,1,1,1,0,0,0,0,0], [1,1,1,1,0,0,0,0,0,0], [1,1,1,0,0,0,0,0,0,0], [1,1,0,0,0,0,0,0,0,0], [1,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,2], [0,0,0,0,0,0,0,0,2,2], [0,0,0,0,0,0,0,2,2,2], [0,0,0,0,0,0,2,2,2,2], [0,0,0,0,0,2,2,2,2,2]]
#new_matrix = np.fliplr(np.flipud(matrix))
#new_matrix = new_matrix.tolist()
print "\n".join(" ".join(str(el) for el in row) for row in matrix)
#print "\n"
#print "\n".join(" ".join(str(el) for el in row) for row in new_matrix)
def basicMove(x,y):
listMoves = []
if x > 0 and matrix[x-1][y] == 0: #left
listMoves.append([x-1,y])
if x < 9 and matrix[x+1][y] == 0: #right
listMoves.append([x+1,y])
if y < 9: #up
if matrix[x][y+1] == 0:
listMoves.append([x,y+1])
if x>0 and matrix[x-1][y+1] == 0: #up left
listMoves.append([x-1,y+1])
if x < 9 and matrix[x+1][y+1] == 0: #up right
listMoves.append([x+1,y+1])
if y > 0: #down
if matrix[x][y-1] == 0:
listMoves.append([x,y-1])
if x > 0 and matrix[x-1][y-1] == 0: #down left
listMoves.append([x-1,y-1])
if x<9 and matrix[x+1][y-1] == 0: #down right
listMoves.append([x+1,y-1])
return listMoves
def hopper(x,y):
listHops = []
listHops.append(basicMove(x,y)) #Call the basic move function inside the hop function
if x > 1 and matrix[x-1][y] != 0 and matrix[x-2][y] == 0: #left
listHops.append([x-2,y])
if x < 8 and matrix[x+1][y] != 0 and matrix[x+2][y] == 0: #right
listHops.append([x+2,y])
if y > 1:
if matrix[x][y-1] != 0 and matrix[x][y-2] == 0: #down
listHops.append([x,y-2])
if x>1 and matrix[x-1][y-1] != 0 and matrix[x-2][y-2] == 0: #down left
listHops.append([x-2,y-2])
if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #down right
listHops.append([x+2,y-2])
if y < 8:
if matrix[x][y+1] != 0 and matrix[x][y+2] == 0: #up
listHops.append([x,y+2])
if x > 1 and matrix[x-1][y+1] != 0 and matrix[x-2][y+2] == 0: #up left
listHops.append([x-2,y+2])
if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y+2] == 0: #up right
listHops.append([x+2,y+2])
return listHops
hopper(2,1) #Testing the function
最后一個問題,使用面向對象編程對我來說會變得更容易/更有效嗎? 我一直在檢查一些為游戲實現MCTS的人的例子,例如在Tic tac toe和Reversi上使用Python,他們似乎都使用OOP。 感謝您的幫助。
首先,是的。 樹的根將是板的初始狀態。
但是,在蒙特卡洛樹搜索中,您不需要函數來計算權重(或評估函數)。 在這里,類似的任務通過稱為“模擬”的函數完成,該函數從給定狀態(或節點)隨機進行游戲,直到結束(直到達到結果,即獲勝/平局/松散),然后返回該結果(+ 1/0 / -1)。 MCTS算法使用多次仿真,以粗略估計所考慮的移動的好壞。 然后,它通過對該移動進行更多的模擬來探索(或擴展)最佳移動,從而獲得更清晰的估計。 具體地,選擇具有通過該移動的隨機播放的累積結果中的最高值的移動以進一步擴展。 算法還跟蹤探索的深度(因此,它不僅繼續挖掘一個動作並留下更好的動作),因此它探索后悔最少的節點(O(logn),最佳)。
而且,關於使用面向對象的編程,如果您擅長的話,它可以提供幫助,但是也可以在沒有它的情況下完成(嘗試將節點作為列表使用,並使用其子列表存儲要存儲在每個節點中的功能。 )
資源:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.