Lispによるothello

Download Report

Transcript Lispによるothello

Lispによるothello
Lispとは
ゲーム理論
minimaxアルゴリズム
αβアルゴリズム
ソースコードの一部
Lispとは






記号処理を中心にしたプログラム言語。
リスト形式のデータ処理に向いている。
人工知能(AI)研究などの分野で使われている。
1962年に米MIT(マサチューセッツ工科大学)のJ.マッカーシーが考案し、
その後様々な研究機関が独自の拡張を行っていたが、1984年に
Common Lispとして標準化された。
Prologと並ぶ人工知能分野の基本言語で、「リスト」と呼ばれるデータ操
作命令の並びの形式を処理するのが特徴。
Lispのプログラミングは、既に定義されているいくつかの関数を組み合わ
せて新しい関数を作る形で記述される。また、呼び出すべき関数を実行
時に決定でき、変数や関数の型宣言が不要リスト処理や記号処理用の
機能が組み込まれている。つまり関数型言語である。
ゲーム理論とは



ゲーム理論はAIの最も古いアリアのひとつです。ゲームを
難しくしているのは限られた時間の中で問題をとかなけれ
ばならないことです。
例えばチェス。
チェスでは平均分岐数が約35個ある。各プレーヤーは大
体50ステップずつ行うので、トータルで35100もの分岐が考
えられる。
これだけの分岐をすべてチェックするのは一生かかっても
無理かもしれない。
このようにゲーム理論は現実的な問題を解決する上で有力
なものである。
Minimaxアルゴリズム

ミニマックス法は非常に単純な考え方で、相
手がこちらの一番不利になる手を打ってくる
として、自分が打てる最良の手を探すアルゴ
リズムです。つまり、相手は評価をMINにす
る手、自分はその中で評価をMAXにする手
を選ぼうというものです。
αβアルゴリズム



この方法はミニ・マックス法と基本的に同じ考え方
で、相手は常に一番有利な手を打って来ると考え
ますが、その上でこれ以上は考えても自分にとって
不利な手しか出てこないような枝については、省略
して行くという方法です。
盤面の評価を行いながら木を掘り進めることで、余
計な枝について評価や探索を行うことを防ぎます。
資源(メモリ、CPU)の有効利用につながります。
Othelloのソースコード①















(defun minimax (player board ply eval-fn)
(if (= ply 0)
(funcall eval-fn player board)
(let ((moves (legal-moves player board)))
(if (null moves)
(if (any-legal-move? (opponent player) board)
(- (minimax (opponent player) board (- ply 1) eval-fn))
(final-value player board))
(let ((best-move nil) (best-val nil))
(dolist (move moves)
(let* ((board2 (make-move move player (copy-board board)))
(val (- (minimax (opponent player) board2 (- ply 1) eval-fn))))
(when (or (null best-val) (> val best-val))
(setf best-val val)
(setf best-move move))))
(values best-val best-move))))))
Othelloのソースコード②


















(defun alpha-beta (player board achievable cutoff ply eval-fn)
(if (= ply 0)
(funcall eval-fn player board)
(let ((moves (legal-moves player board)))
(if (any-legal-move? (opponent player) board)
(- (alpha-beta (opponent player) board (- cutoff)
(- achievable) (- ply 1) eval-fn))
(final-value player board))
(let ((best-move (first moves)))
(loop for move in moves do
(let* ((board2 (make-move move player (copy-board board)))
(val (- (alpha-beta (opponent player) board2 (- cutoff)
(- achievable) (- ply 1) eval-fn))))
(when (> val achievable)
(setf achievable val)
(setf best-move move)))
until (>= achievable cutoff))
(values achievable best-move)))))
Othelloの始め方

(othello
(minimax-searcher 3 #'count-difference)
(alpha-beta-searcher 3 #'weighted-squares))
とタイプして、実行するとゲームが始まります。
Minimax vs αβ
結果は?
おまけ(さらにOthelloをJessで)
JessとはExpertSystem構築ツールである
CLIPSをJAVA環境で使用できるようにした
もの。
 文法はLISPになかなか似てる。
 人工知能プログラミング + JAVA
 LISPで書いたコードを100%JESSに
translateすることは難しい。(多分無理。)
=>足りないところはJAVAで。

おまけ(続き・・・)
イメージ
データ
JAVA
(GUI担当)
JESS
(アルゴリズム担当)
データ
JavaとJessのコード



public int[][] minimax(int player, int[][] board, int ply){
int eval = 0;
int[][] position = new int[1][1];
・・・・・・

return position;

}
Minimaxメソッド
引数:player(現在のplayer)、board(現在のboard)、ply(検索木の深さ)
Return:position(minimaxによって決められた手)
JavaとJessのコード①



public int[] minimax(int player, int[][] board, int ply){
int eval = 0;
int[] position = {0};
if(ply == 0){
eval = weightedSquares(player, board);
}
else{
minimax2(player, board, ply);
}






return position;


}
JavaとJessのコード②


private int[] minimax2(int player, int[][] board, int ply){
int[][] possibleStone;

possibleStone = parent.checkAll2(parent.turn);

try{
r.executeCommand("(bind ?moves (create$ ))");
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
if(possibleStone[i][j] == 1)
r.executeCommand("(bind ?moves (create$ ?moves "+ (i*10 + j) +"))");
}
}
if(r.executeCommand("(eq ?moves (create$ ))").equals("TRUE")){
r.executeCommand("" +
"" +
"");
}
else minimax3(player, board, ply);
}
catch(JessException je){
System.err.println(je);
}
return new int[1];



















}