Navigation

2017年2月14日火曜日

探索アルゴリムズまとめ

edX ColumbiaXのAI (CSMM.101x)で説明されているAIで使用される探索アルゴリムズのまとめです。

b: 一つの分岐における最大の分岐数
d: 探索木における解がある深さ
m: 探索木における最大の深さ

Uninformed Search (=網羅的探索 brute-force search, 盲目的探索 blind search)
BFS: Breadth-first Search 横型探索法、幅優先探索法
空間計算量:O(b^d)
時間計算量:O(b^d)
  完全性:○(分岐bが有限の場合。無限の場合は完全ではない)
  最適性:○(グラフの重みが全て同じであれば、最適である)

アルゴリズム
function BFS (initialState, goalTest)
    returns SUCCESS or FAILURE:

    frontier = Queue.new(initialState)
    explored = Set.new()

    while not frontier.isEmpty()

        state = frontier.dequeue()
        explored.add(state)

        if goalTest(state):
            return SUCCESS(state)

        for neighbour in state.neighbours():
            if neighbour not in frontier U explored:
                frontier.enqueue(neighbour)
    return FAILURE

DFS: Depth-first Search 縦型探索法、深さ優先探索法
空間計算量:O(bm)
時間計算量:O(b^m)
  完全性:○(深さが有限の場合)
  最適性:✖️

アルゴリズム
function DFS (initialState, goalTest)
    returns SUCCESS or FAILURE:

    frontier = Stack.new(initialState)
    explored = Set.new()

    while not frontier.isEmpty()

        state = frontier.pop()
        explored.add(state)

        if goalTest(state):
            return SUCCESS(state)

        for neighbour in state.neighbours():
            if neighbour not in frontier U explored:
                frontier.push(neighbour)
    return FAILURE

IDS: Iterative Deepening Depth-first Search 反復深化深さ優先探索
空間計算量:O(bd): bは分岐係数、dは深さ
時間計算量:O(b^d)
  完全性:○
  最適性:○

説明:IDSを知識あり探索にしたものがIDA*である。これは、ダイクストラ法を知識あり探索にしたものがA*であることに対応する。

DA: Dijkstra Algorithm ダイクストラ法
空間計算量:
時間計算量:
  完全性:
  最適性:

UCS: Uniform-Cost Search 均一コスト探索法
空間計算量:O(b^(c/e)): Cは最適解のコスト、eは1アクションあたりの最低コスト
時間計算量:O(b^(c/e))
  完全性:○(コストが有限の場合)
  最適性:○

説明:ステップコストが全て同じであるときは幅優先探索と同じアルゴリズムになる。

アルゴリズム
function UCS (initialState, goalTest)
    returns SUCCESS or FAILURE: /*Cost f(n) = g(n) */

    frontier = Heap.new(initialState)
    explored = Set.new()

    while not frontier.isEmpty()

        state = frontier.deleteMin()
        explored.add(state)

        if goalTest(state):
            return SUCCESS(state)

        for neighbour in state.neighbours():
            if neighbour not in frontier U explored:
                frontier.insert(neighbour)
            else if neighbour in frontier:
                 frontier.decreaseKey(neighbour)
    return FAILURE

Informed Search (=発見的探索法 heuristic search)
Greedy Best-first Search 最良優先探索法

空間計算量:
時間計算量:
  完全性:
  最適性:

説明:
(1)方向性をもった探索なので、縦型探索や横型探索よりも早く解が求められる。
(2)未探索接点を全て保存しておく必要があるので使用メモリが膨大となる。
(3)適当な発見的関数が存在しない場合には使えない

アルゴリズム
function GBFS (initialState, goalTest)
    returns SUCCESS or FAILURE: /*Cost f(n) = h(n) */

    frontier = Heap.new(initialState)
    explored = Set.new()

    while not frontier.isEmpty()

        state = frontier.deleteMin()
        explored.add(state)

        if goalTest(state):
            return SUCCESS(state)

        for neighbour in state.neighbours():
            if neighbour not in frontier U explored:
                frontier.insert(neighbour)
            else if neighbour in frontier:
                 frontier.decreaseKey(neighbour)
    return FAILURE

A* Search エイスター探索法
空間計算量:
時間計算量:
  完全性:
  最適性:○(発見的関数h(n)が許容的(admissibleな場合))

説明:A*アルゴリズムは、ダイクストラ法を推定値つきの場合に一般化したもので、hが常に0である場合はもとのダイクストラ法に一致する。
(1)最適経路が必ず得られる
(2)条件を満たす発見的関数が思いつかない場合には使えない
(3)方向性をもった探索なので、縦型探索や横型探索よりも早く解が求められる
(4)最良優先探索と同様の理由で使用メモリが膨大になる

アルゴリズム
function GBFS (initialState, goalTest)
    returns SUCCESS or FAILURE: /*Cost f(n) = 
g(n) + h(n) */

    frontier = Heap.new(initialState)
    explored = Set.new()

    while not frontier.isEmpty()

        state = frontier.deleteMin()
        explored.add(state)

        if goalTest(state):
            return SUCCESS(state)

        for neighbour in state.neighbours():
            if neighbour not in frontier U explored:
                frontier.insert(neighbour)
            else if neighbour in frontier:
                 frontier.decreaseKey(neighbour)
    return FAILURE

Local Search
Hill Climbing Search(Greedy Local Search): 山登り法
空間計算量:
時間計算量:
  完全性:
  最適性:✖️

説明:
(1)現在の状態だけを保存しておけばよいので、最良優先探索法に比べ使用メモリならびに探索の手間が少ない
(2)バックトラックが得られるとは限らない
(3)最適解が得られるとは限らない

アルゴリズム
function HILL-CLIMBING(initialState)
    returns State that is a local maximum

   
    initialize current with initialState

    loop do
         neighbour = a highest - valued successor of current

         if neighbour.value <= current.value:
              return current.state
         current = neighbour

2017年2月13日月曜日

CSMM.101x: Week 4: Adversarial Search and Games

edX ColumbiaXのAI (CSMM.101x) Week4のまとめです。

日本語資料では、下記が参考になりそうです。

ミニマックス法とアルファベータ法
http://www.geocities.jp/m_hiroi/light/pyalgo24.html

Suggest Readings
下記が参照記事としてあげられています。
WIRED: Inside Libratus, the Poker AI That Out-Bluffed the Best Humans: http://www.wired.com/2017/02/libratus/
4.1 Adversarial Search and Games
Adversarial Search(∽game)が今回のテーマです。Adversarial Searchは適切な訳が見つけられませんでした。
ゲームの特徴は我々がコントロールできない相手が存在することで、最適なソリューションは一連の行動ではなく、strategy(policy)となる。

ゲームの種類は、プレイヤーの手が全て見えているかどうか、また運の要素があるかどうかの二軸で、四次元に分けられる。

Perfect Information
Deterministic: Chess, Checkers, Go, Othello
Chance (stochastic): Backgammon, Monopoly

Imperfect Information
Deterministic: Battleships, Blind Tictactoe
Chance (stochastic): Bridge, Poker, Scrabble Nuclear War

AIで主に扱うのは、ゼロサムゲームと呼ばれる、Chess, Checkers, Go, OthelloなどのPerfect Informationが与えられ、Deterministicなゲームである。

Zero-sum Gamesの特徴
* Adversarial: Pure Competition
* Agents have different values on the outcomes
* One agent maximizes one single value, while the other minimizes it

4.2 Minimax algorithm
ミニマックス法の説明が行なわれている。
以下が特徴としてあげられる。
1. DFSである
2. 最適な解は探索木のどの深さでもありうる。
3. 解は最適であり、有限な探索木であれば完全性が保証される
4. 時間計算量は O(b^m), 空間計算量はO(bm)
が、現実的な場面では時間が限られており、全てのリーフの探索はできない。
対応方法のひとつとして「枝刈り」がある。

4.3 Alpha-Beta Pruning (アルファ・ベータ枝刈り)
代表的な方法としてα-β 枝刈りが紹介されている。
Strategy: ミニマックス法と同じく、DFSである。
αは、Maxのなりうる最大値であり、その時点でのMaxの最下限の数値である
βは、Minのなりうる最小値であり、その時点でのMinの最上限の数値である

4.4 Stochastic games
サイコロをなげる、カードをシャッフルするなどランダムな要素のあるゲーム
探索木にChanceノードというノードを追加し、ランダムな要素を組み込む
例:バックギャモン(バックギャモン、日本では一般的なゲームではないので、私としてはわかりにくい例です)

Week 4 Quiz: Adversarial Search and Games
前半は好調でしたが、後半間違いを重ねてしまい、得点はギリギリPassの60%でした。
ミニマックス法とα-β枝刈り法の設問は基本的なものが多かったものの、Utility/Eval functionに関するものや、Expectiminimaxに関する質問は該当の箇所の講義をしっかりと視聴していなかったため、得点できませんでした。

Week 4 Project: Adversarial Search and Games
Week 2と同様にPythonでのコーディングの課題です。やはり分量が多いので、どこかで時間を作ってWeek 2とまとめて取り組んだ方が良さそうですね。

Week 4 Discussion Questions
チェッカーというゲームのChinook(チノーク)というAIについて議論するのが課題になっていますが、グレードの対象外ですので、スキップしました。

2017年2月12日日曜日

CSMM.101x: Week 3: Heuristic Search

edX ColumbiaXのAI (CSMM.101x) Week3のまとめです。

今回取り上げられているヒューリスティック検索や前回の講義で取り上げられている、幅優先探索等を日本語で理解するためには、下記サイトを参照致しました。

ヒューリスティック探索
幅優先探索と反復深化
欲張り法 (greedy strategy)

3.1 Heuristic and Greedy Search Algorithm
前回説明されたUninformed Searchに対し、解に近づいているかどうかがわかるInformed Searchが今回の講義のテーマです。

最良優先探索(Greedy best-first search)、Aスター探索、反復深化Aスター探索がInformed Searchの例としてあげられています。

3.2 A* Search and Optimality
Aスター探索となぜ、それが最適な解を提示し得るかの説明がされています。

3.3 Search Algorithms Recap
ここでは、今まで説明された、BFS, DFS, IDS, UCS, A*, IDA*, Greedy best first search, A* Searchの各種アルゴリズムの特徴が説明されています。
ここまでの講義の最重要点ですので、別途まとめてポスト予定です。

3.4 Local Search (局所探索法)
局所探索法としてHill Climbing 山登り法の説明がされています。

Week 3 Quiz: Heuristic Search
ヒューリスティック関数の性質に関する質問で1問間違えたものの、他はすべて正解ができました。ヒューリスティック関数が許容的(admissible)かどうか、特定条件下で各アルゴリズムのcompletnessはどうか、といった設問が多かったです。

Week 3 Discussion Questions
ダイクストラ法と均一コスト法の比較を行っている下記論文が紹介されています。こちらも別途ポスト予定の探索アルゴリズムまとめで言及する予定です。

Position Paper: Dijkstra’s Algorithm versus Uniform Cost Search or a Case Against Dijkstra’s Algorithm
http://www.aaai.org/ocs/index.php/SOCS/SOCS11/paper/viewFile/4017/4357