easyAI
1.0.0
EasyAi(這裡的完整文檔)是一個純淨的Python人工智能框架,用於兩人抽象遊戲,例如TIC TAC TOE,CONNECT 4,RECRVERSI等。它可以輕鬆定義遊戲的機制,並與計算機進行遊戲或解決遊戲。在引擎蓋下,AI是一種Negamax算法,如Wikipedia所述,帶有α-Beta修剪和換位表。
如果已安裝了pip ,請在終端中輸入
sudo pip安裝easyai
否則,請下載源代碼(例如在github上),將所有內容解壓縮到一個文件夾中,並在此文件夾中,在一個終端中,類型
sudo python setup.py安裝
此外,您需要安裝Numpy才能運行一些示例。
讓我們定義遊戲規則,並開始與AI的比賽:
from easyAI import TwoPlayerGame , Human_Player , AI_Player , Negamax
class GameOfBones ( TwoPlayerGame ):
""" In turn, the players remove one, two or three bones from a
pile of bones. The player who removes the last bone loses. """
def __init__ ( self , players = None ):
self . players = players
self . pile = 20 # start with 20 bones in the pile
self . current_player = 1 # player 1 starts
def possible_moves ( self ): return [ '1' , '2' , '3' ]
def make_move ( self , move ): self . pile -= int ( move ) # remove bones.
def win ( self ): return self . pile <= 0 # opponent took the last bone ?
def is_over ( self ): return self . win () # Game stops when someone wins.
def show ( self ): print ( "%d bones left in the pile" % self . pile )
def scoring ( self ): return 100 if game . win () else 0 # For the AI
# Start a match (and store the history of moves when it ends)
ai = Negamax ( 13 ) # The AI will think 13 moves in advance
game = GameOfBones ( [ Human_Player (), AI_Player ( ai ) ] )
history = game . play ()結果:
堆中有20個骨頭 玩家1您玩什麼? 3 移動#1:播放器1播放3: 17個骨頭留在堆中 移動#2:播放器2播放1: 一堆留下16個骨頭 玩家1您玩什麼?
現在讓我們解決遊戲:
from easyAI import solve_with_iterative_deepening
r , d , m = solve_with_iterative_deepening (
game = GameOfBones (),
ai_depths = range ( 2 , 20 ),
win_score = 100
)我們獲得r=1 ,這意味著,如果兩個球員都表現出色,那麼第一個玩的球員總是可以獲勝(-1將始終輸), d=10 ,這意味著勝利將以十分勝(即每位播放器5個動作)或更少, m='3' ,這表明第一名球員的第一步應該是'3' 。
這些計算可以使用一個換位表加速,該表將存儲所遇到的情況,並且每種情況的最佳動作:
tt = TranspositionTable ()
GameOfBones . ttentry = lambda game : game . pile # key for the table
r , d , m = solve_with_iterative_deepening (
game = GameOfBones (),
ai_depths = range ( 2 , 20 ),
win_score = 100 ,
tt = tt
)運行這些線後,變量tt包含一個換位表,該表存儲可能的情況(在此,樁的可能大小)和最佳移動以進行。使用tt ,您可以完美地發揮而無需思考:
game = GameOfBones ( [ AI_Player ( tt ), Human_Player () ] )
game . play () # you will always lose this game :) Easyai是最初由Zulko撰寫並根據MIT許可發布的開源軟件。歡迎捐款!一些想法:用於不完整的信息遊戲的AI算法,更好的遊戲解決策略,(有效)使用數據庫存儲移動,使用並行化的AI算法。
對於故障排除和錯誤報告,目前最好的是在Github上詢問。
每次MR合併到主人中時,都會發生自動發布: