德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2...

56
德德德德德德德德德德德 1 Problem & Solving Unit 5

Transcript of 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2...

Page 1: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 1

Problem & Solving

Unit 5

Page 2: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 2

You Will Know

• Problem and solving

• Production system

• Search

• Game Theory

Page 3: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 3

Problem and Solving

• In general– Problem : what goals do you want to reach?– Solving: how do you reach the goals?

• We want to build problem-solving agents for our AI problems

• Problem formulation– the process of deciding what actions and states

to consider, given a goal

Page 4: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 4

Problem Formulation

• Four components in a problem– Initial state the agent starts in it.– Actions: possible actions available to the agent– Goal test: determine whether a given state is the

goal– Path cost: assign a numeric cost to each path

• Optimal solution– The lowest path cost among all solutions

Page 5: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 5

Example: Vacuum CleanerGoal:

clean the housein state 7 or 8

Initial:

Actions:

Solution path:

in state 5

{left, right, suck}

{right, suck}

Page 6: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 6

Page 7: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 7

Example: Vacuum Cleaner• Problem formulation

– States: possible world states

– Initial state: any state can be designated as the initial state

– Successor function (or actions): (left, right, suck)

– Goal test: This check whether all the square are clean

– Path cost: Each step costs 1, so the path cost is the number of steps in the path

822 2

Page 8: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 8

Example: 8-puzzleGoal:

goal state

Initial:

Actions:

Path cost:The start state

black move {left, right, up, down}

unit cost

Page 9: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 9

Example: 8-puzzle• Problem formulation

– States: the location of each of the eight tiles and the black in one of the nine square

– Initial state: any state can be designated as the initial state

– Successor function (or actions): (left, right, up, down)

– Goal test: This check whether the state matches the goal

– Path cost: Each step costs 1, so the path cost is the number of steps in the path

Page 10: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 10

Example: DiagnosisGoal:

disease state

Initial:

Actions:

Path cost:symptom

test, diagnose

test cost

patient symptoms(cough, fever)

disease state(pneumonia)

diagnose

test (x-ray)

Page 11: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 11

N-Queen Problem Goal:

No two queens attacking

State:

Actions:

Path cost:Placement of n queens

Place a queen on square (i, j)

zero cost

Page 12: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 12

N-Queen Problem

• Problem formulation (please fill it)– States:– Initial state:– Successor function (or actions):– Goal test:– Path cost:

Page 13: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 13

Production System

• One type of problem-solving systems

• Three components in production system– Global database– Set of rules– Control system

• Data driven and data adaptive

• Take an example: chess

Page 14: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 14

Global Database

• Information of the problem– Environment description– Facts– Parameters of rule inferences

• In practice, global database means designing data structure for environment

• What’s the global database for chess?

Page 15: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 15

Set of Rules

• Rule– Cover the ability and possibility of the agent– Rule inference can produce new facts

• The general form of a ruleConditions => Actions

If Condition then Action A

else Action B

Facts => Results

What’s the rule for chess?

Page 16: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 16

Control System

• Inference engine for rules– Select proper rules for observing facts– Enable rules– Disable rules– Goal: looking for an optimal path for solutions

What’s the control system for chess?

Page 17: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 17

Search

• Why do we need search– Many many possible solutions for a problem– An optimal solution: minimum cost

• It is often impossible to test all possible solutions in difficult problems– Question: Who is the tallest in the world?– We need smarter strategies for search– How smart? What’s smart?

Page 18: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 18

Search

• Complexity– The cost of the search approach– In most cases, we use “complexity” to measure

the cost of an algorithm– O(n), O(n2), O(n log n), …– Two kinds of complexity

• Time complexity

• Space complexity

Page 19: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 19

Complexity Example

• Given a data sequence with size 1000000 items, does it contain the number “1205”?– sequential search:

• It may be success at the first time if you are so lucky

• O(n): 1000000 times at the worst case

– binary search:• It may be success at the first time if you are so lucky

• O(n log n): 1000 times at the worst case

• But the data sequence need to be sorted

Page 20: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 20

Complexity

• Consider the example at the previous page– If the data size is 100 items?– If the data size is 10000 items?– If the data size is 100000000 items?

• Scalability issue– The performance, either time or space, of an

algorithm depends on the size of the data

Page 21: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 21

Complexity• The search problem is

– P problem• There exists a polynomial-time algorithm to solve

the search problem• This is not very difficult

– NP-Complete• No polynomial-time solution can be found• That means that we can solve the problem if the data

size is not small• We need to find a non-optimal solution• e.g., 0-1 knapsack

Page 22: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 22

Search Space

Page 23: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 23

Quality of A Search Algorithm• Complete

– Is it a correct solution?

• Optimality– Is it high quality?

• Time complexity– How long to find a solution?

• Space complexity– How much memory is required to find a solution?

Page 24: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 24

4-Queen Problem

X

X X

(12, 24, 31, 43)

(13, 21, 34, 42)

Page 25: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 25

4-Queen Problem• Action

– Rij: put a chess at (i, j), where

– total: 16 different actions– Tree structure: 44=256 nodes

41 ,41 ji

Page 26: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 26

Depth First Search (DFS)

• DFS Algorithm1. 以某一頂點 v 為起點2. 在所有與 v 相鄰且未被走過的其他頂點中,選擇一個頂點 w 為新的起

點3. 從 w 開始再度遞迴的執行深度優先搜尋法4. 當所有相鄰頂點都拜訪過,回溯到上一層繼續深度優先搜尋其他位被

拜訪過的頂點procedure DFS(v) {

visited(v) = 1; // 紀錄 v 已拜訪過

for each vertex w adjacent to v

//loop for 所有 v 的相鄰頂點 w

if visited(w)==0

then DFS(v);

}

Page 27: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 27

Depth First Search (DFS)

)(

...10

d

d

bO

bbb

Complete

Optimal

Time

Space

The solution will be found if it exists

Find the shallowest goal

)(

...10

d

d

bO

bbb

b: degree, or # of childrend: depth of search

Page 28: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 28

Breadth First Search (BFS)

• BFS Algorithm1. 以某一頂點 v 為起點

2. 依序拜訪 v 的所有相鄰頂點

3. 當一層的相鄰頂點拜訪完畢,才繼續拜訪下一層的頂點

BFS 是 level-by-level 的,所以使用 queue 的概念; DFS 是使用遞迴,所以是 stack

DFS 與 WFS 的結果都跟使用之資料結構有關,採用資料結構不同,即使起點相同,結果也不一定會一樣

Page 29: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 29

Breadth First Search (BFS)Complete

Optimal

Time

Space

May not find the best solution if ∞ depth

May not optimal if ∞ depth

)( mbO

)(mbO

b: degree, or # of childrenm: max depth

Page 30: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 30

DFS vs. BFS

DFSv1, v2, v4, v8, v5, v6, v3, v7

v1

v2

v3

v4

v5

v6

v7

v8

BFSv1, v2, v3, v4, v5, v6, v7, v8

Page 31: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 31

Exercise

• Start from A– Travel by DFS– Travel by BFS

Page 32: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 32

Which Strategy to Use

• Depends on your problem.

• If there are infinite paths– DFS is bad

• If goal is at know depth– DFS is good almost

• If there exist large branching factor– BFS is bad

• Consider data structure in your program.

Page 33: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 33

Improvement• Either in DFS or BFS

– These are basic approaches for graph traversal– Cost is very high if the graph is huge– It is necessary to cost down

• Set constraints on search– Take some limited criteria, not optimal solution– Example: looking for a pair of shoes at shopping

• Optimal: looking for the cheapest one• Constraint: looking for the one less than NT$1000

– Depth-limited Depth-first Search

Page 34: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 34

Depth-limited Depth-first Searchconstraint: depth d 4≦

Complete: No, unless at depth d≦Optimal: No

Time: O(bd)Space: O(bd)

5

6

7 8

9

Page 35: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 35

Game Theory• Game

– A set of game rules– Players’ decision to win the game

• 單人遊戲的問題– 找出最佳的結果– 可用一般的搜索技術進行求解– 8-puzzle; n-queen; 接龍

• 雙人遊戲– 對方同時也想取得最佳的結果– 可用與或樹 ( 或圖 ) 來描述,因而搜索就是尋找最

佳解樹 ( 或圖 ) 的問題– Chess; Chinese chess; Go; Bridge

Page 36: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 36

X

X

X

O OOO O OO O

X

X

X

XX

O O

O

X

O

X

O

X

O

X

O

X

O

X

O

X

O O

O O O

O X

O

O

XO

OX

O

O

XO

O

XO

O

XO X X X

X X X

O

O

O OO O

O O O

Page 37: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 37

Playing

• You will play a best step – Also, your opponent will do best– Past: you and your opponent knew all– Future: you and your opponent can guess

• Find a path from start to a goal– Search all possible paths: infeasible– Maximize your step (score), and minimize your op

ponent’s step (score).– MiniMax Algorithm

Page 38: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 38

Score Function

• We need to define a score function to measure the “value” of a step p– f(p)=(number of possible O lines - number of p

ossible X lines)– Example: (you are “O”)

O

f = 8-4 = 4

O

X

f = 6-4 = 2

O

X

f = 5-4 = 1

Page 39: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 39

X

X

X

O OOO O OO O

X

X

X

XX

O

O

O OO O

O O Of=8-5=3 f=3 f=3 f=3f=4f=2 f=2 f=2 f=2

f=2f=2f=2 f=2f=1 f=1 f=1 f=1

O O

O

X

O

X

O

X

O

X

O

X

O

X

O

X

O O

O O Of=4 f=4 f=3 f=3 f=3f=4 f=4

O X

O

O

XO

OX

O

O

XO

O

XO

O

XO X X X

X X Xf=2 f=2 f=1 f=1 f=2 f=1

MAX

MIN

MAX

MIN

Page 40: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 40

MinMax

• What’s min?

• What’s max?

• Top-down or bottom-up

Page 41: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 41

X

X

X

O OOO O OO O

X

X

X

XX

O

O

O OO O

O O Of=8-5=3 f=3 f=3 f=3f=4f=2 f=2 f=2 f=2

f=2f=2f=2 f=2f=1 f=1 f=1 f=1

O O

O

X

O

X

O

X

O

X

O

X

O

X

O

X

O O

O O Of=3 f=2 f=2 f=2 f=2f=3 f=3

O X

O

O

XO

OX

O

O

XO

O

XO

O

XO X X X

X X Xf=2 f=1 f=2 f=1 f=1 f=1

MAX

MIN

MAX

MIN

Page 42: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 42

極小極大搜索程序

Page 43: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 43

Page 44: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 44

Page 45: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 45

Properties of MiniMax

Page 46: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 46

Resource Limits

Solutions:

1. cutoff test, eg: depth limit2. evaluation function

or estimated desirability of position

• Example: Chess( 西洋棋 )– a reasonable game: O(35100) nodes– suppose we have 10 seconds/move– If explore 109 nodes/second1010 nodes per movenot enough

Page 47: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 47

Cutoff Search• Depth-limit in Minimax search

– e.g. depth < 6

– means the player can think less than 6 steps

• Does it work in practice?

– m=4 => the computer can think less than 4 steps

• In chess– m=4, human novice

– m=8, typical pc, human master

– m=12, deep blue, kasparov

4 ,35 106 mbbm

Page 48: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 48

α-β Pruning

Page 49: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 49

α-β Pruning: Example

Page 50: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 50

α-β Pruning: Example

Page 51: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 51

α-β Pruning: Example

Page 52: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 52

Properties of α-β

• Pruning doesn’t affect the final result

• Good move ordering can improve effectiveness of pruning

• With “perfect ordering”– Time complexity: O(bm/2) – Double depth for search– Easily reach depth 8 => human master

Page 53: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 53

Why is it called α-β

• α: the best value to Max– If v is worse than α, Max will avoid it– i.e. prune that branch

• β: the best value to Min– or the worst value to MAX

• α for Max, and β for Min

Page 54: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 54

α-β Algorithm

Basic MiniMax + tracking of α-β + pruning

Page 55: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 55

Now

Page 56: 德明科技大學資訊科技系 1 Problem & Solving Unit 5. 德明科技大學資訊科技系 2 You Will Know Problem and solving Production system Search Game Theory.

德明科技大學資訊科技系 56

Non-determined Game• You cannot exactly know which step you can pl

ay– Backgammon: dice rolls determine legal moves– Coin-flipping– 暗棋

expected values