Easyai(こちらの完全なドキュメント)は、Tic Tac Toe、Connect 4、Reversiなどの2人のプレイヤーの抽象ゲームの純粋なPython人工知能フレームワークです。ゲームのメカニズムを簡単に定義し、コンピューターに対抗したり、ゲームを解決したりできます。ボンネットの下では、AIはウィキペディアで説明されているように、アルファベータプルーニングと転置テーブルを備えたネガマックスアルゴリズムです。
pipがインストールされている場合は、これを端末に入力してください
Sudo PipインストールEasyai
それ以外の場合は、ソースコード(たとえばgithubで)をダウンロードし、すべてを1つのフォルダーに解凍し、このフォルダーに端末のタイプで解凍します
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 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がマスターに統合されるたびに、自動リリースが発生します。