problem solving prof. chung-ta king department of computer science national tsing hua university...

43
Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電電電電電電電電 (Contents from Prof. I. K. Lundqvist, Prof. Nilufer Onder, Prof. Kun-Yung 140.121.196.191/pdf/ai/2007/Topic 3. Search methodologies.pdf)

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

Problem Solving

Prof. Chung-Ta KingDepartment of Computer

ScienceNational Tsing Hua University

CS1103 電機資訊工程實習

(Contents from Prof. I. K. Lundqvist, Prof. Nilufer Onder, Prof. Kun-Yung Lu,140.121.196.191/pdf/ai/2007/Topic 3. Search methodologies.pdf)

Page 2: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

2

假設我們要到澎湖玩

Page 3: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

3

由松山機場搭機前往

Page 4: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

4

如何由清大到松山機場 ?

Page 5: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

5

清大 松山機場

三重 /新竹 /豪泰客運三重 /新竹 /豪泰客運

台北捷運台北捷運

225 路公車225 路公車

清大校門清大校門 台北火車站台北火車站

台北捷運中山國中站台北捷運中山國中站 松山機場松山機場

Page 6: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

6

How Did We Derive the Solution?

Problem solving as state space search What is “state”?

States: location of you Transitions: transportation ( 客運、高鐵、台鐵、捷運、公車、計程車、走路 )

客運客運

台北捷運台北捷運

公車公車

清大校門 台北火車站

台北捷運中山國中站 松山機場

計程車計程車

Page 7: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

7

Consider a More Complex Problem

Consider the problem: Astronaut carries Grain, Goose, Fox across

river Astronaut + 1 item allowed in the boat Goose alone eats Grain Fox alone eats Goose

AstronautGrain, Goose, FoxAstronautGrain, Goose, Fox

Page 8: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

8

Problem Solving as State Space Search

Goal: Astronaut, Fox, Goose and Grain across

river Problem representation:

States: location of Astronaut, Fox, Goose and Grain at top or bottom river bank

Operators: move boat with astronaut and 1 or 0 items to other bank

Generate solution: Sequence of states: Move(goose,astronaut),

Move(astronaut), . . .

Page 9: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

9

InitialStateInitialState

GoalStateGoalState

Page 10: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

10

Page 11: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

11

Representing the Problem

The problem space consists of: A state space which is a set of states

representing the possible configurations of the world

A set of operators which can change one state into another

Initiate state and goal state Path: sequence of states produced by

the valid application of operators from an old state to a new state

The problem space can be viewed as a graph where the states are the nodes and the arcs represent the operators

Page 12: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

12

The 8-Puzzle Problem

Operator: “move blank”

1 4 3

7 6

5 8 2

1 4 3

7 6 2

5 8

Page 13: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

13

State Space of 8-Puzzle

Page 14: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

14

目前為止我們只到一個解答 但做為資工系的學生,我們不但要知道一個解答,我們更想知道一個更好的解答,甚至

最佳解 (optimal solution)

例如: sorting Bubble sort, selection sort, insertion sort,

merge sort, heapsort, quicksort, ...

Page 15: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

15

問題是 ...

何謂「最佳」 ? 時間最短 記憶體用最少 最便宜 耗電最低 最環保 最容易使用 最漂亮

還有更複雜的目標 ... 又快又便宜 功能又強又省電

Optimization goalOptimization goal

不同目標如何協調 ?不同目標如何協調 ?

Page 16: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

16

如何計算目標值 ?

時間:

其他方法: 步數、運算數

客運 (1.5 小時 )客運 (1.5 小時 )

捷運 (0.5 小時 )捷運 (0.5 小時 )

公車 (15 分鐘 )公車 (15 分鐘 )

清大校門清大校門 台北火車站台北火車站

台北捷運中山國中站台北捷運中山國中站 松山機場松山機場

Page 17: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

17

如何計算目標值 ?

成本:

其他方法: 元件成本、售價

客運 ($100)客運 ($100)

捷運 ($40)捷運 ($40)

公車 ($15)公車 ($15)

清大校門清大校門 台北火車站台北火車站

台北捷運中山國中站台北捷運中山國中站 松山機場松山機場

Page 18: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

18

如何計算目標值 ?

路上風景最好:

常用方法:專家評分、民意調查

客運 (???)客運 (???)

捷運 (???)捷運 (???)

公車 (???)公車 (???)

清大校門清大校門 台北火車站台北火車站

台北捷運中山國中站台北捷運中山國中站 松山機場松山機場

Page 19: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

19

Cost-directed Problem Solving

In many search problem, we are interested not only in reaching a goal state, but also in reaching it with the lowest cost (or the maximum profit) We can compute a cost as we apply

operators and transit from state to state

Page 20: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

20

Traveling Salesperson Problem

Page 21: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

21

State Space of TSP

(arc label = cost from root)(arc label = cost from root)

Page 22: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

22

Nearest Neighbor Path

Nearest neighbor path = AEDBCA (550)

Minimal cost path = ABCDEA (375)

Page 23: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

23

Finding Solutions

Some problems have only one solution (goal state), e.g. 8-puzzle, sorting, Tower of Hanoi; others may have more than one solution, e.g. 清大到松山機場 , traveling salesperson

To find one solution: We need to find one path from the root to

the leaf in the state space To find the optimal solution:

We need to traverse all the paths in the state space

What if the state space is huge?

Page 24: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

24

Search Algorithms

Effective search algorithm must: Cause motion or traversal of the state space Do so in a controlled or systematic manner

The method that never using the information about the problem to help direct the search is called brute-force, uninformed, or blind search

Search algorithms which use information about the problem, such as the cost or distance to the goal state, are called heuristic, informed, or directed search

Page 25: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

25

Search Algorithms

An algorithm is optimal if it will find the best solution from among several possible solutions

A strategy is complete if it guarantees that it will find a solution if one exists.

Complexity of an algorithm: Time complexity (how long to find a

solution) Space complexity (how much memory it

requires)

Page 26: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

26

Search Algorithms

The search problem can be classified to two classes: P and NP

The classes P consists of all problems for which algorithms with polynomial time behavior have been found

The class NP is the set of problems for which algorithms with exponential behavior have been found

If an optimization of the problem cannot be solved in polynomial time, it is called NP-hard

Page 27: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

27

Breadth-first Search

Page 28: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

28

Depth-first Search

Depth bound = 5Depth bound = 5

Page 29: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

29

“ Blind Search”

BFS and DFS are blind in the sense that they have no knowledge about the problem at all other than the problem space

Such techniques are also called brute-force search, uninformed search, or weak methods

Worst case scenarios are equally bad (exponential)

Obviously, we can’t expect too much from these, but they provide Worst-case scenarios

Page 30: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

30

Heuristic State-space Search

A heuristic algorithm consists of two parts: The heuristic measure: a heuristic evaluation

function measures the “goodness” of a node An algorithm that uses the heuristic measure

to search the state space Heuristics are rules for choosing the

branches in a state space that are most likely to lead to an acceptable problem solution A heuristic is only an informed guess of the

next step to be taken in solving the problem A heuristic is often based on experience or

intuition and can lead to a suboptimal solution

Page 31: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

31

Tic-tac-toe

# of states in an exhaustive search is 9!

Page 32: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

32

Tic-tac-toe

A heuristic is moving to the board in which X has the most winning lines

Page 33: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

33

Hill-climbing Search

Hill-climbing expands the current state and selects the best child for further expansion Neither its siblings nor its parent are

retained (without backtracking) Search halts when it reaches a state

that is better than any of its children Major problems:

May get stuck at a local optimal

Page 34: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

34

Hill-climbing Search

Possible solutions: Keep a list of plausible move and backtrack

when a dead-end is met Make a big jump or move the same

direction several times Try different directions (applying two or

more rules) before test Hill climbing is basically a “local “

heuristics

Page 35: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

35

Best-First Search

Use heuristic function to choose a best move out of several alternatives

Keep exploring the best path (depth-first search) until it turns less promising than a previous path

Return to explore the previous path that has become most promising

Page 36: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

36

Heuristic Evaluation Function

The goal is to use the limited information available in a single state descriptor to make intelligent choices

For 8-puzzle, heuristicmay be: # tiles out of place Sum of all the distances

by which the tiles are outof pace

2 x # direct tile reversals

Page 37: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

37

Heuristic Evaluation Function

The evaluation function may be the sum of two components: f(n)=g(n)+h(n) g(n) measures the actual length of the path

from state n to the start state h(n) is a heuristic estimate of the distance

from state n to a goal

A* Algorithm

Page 38: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

38

Iterative Improvement

Start with one solution and make modifications to improve its quality

Page 39: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

39

Find Initial Solution

Traverse the state space and find one solution

客運 (1.5 小時 )客運 (1.5 小時 )

捷運 (0.5 小時 )捷運 (0.5 小時 )

公車 (15 分鐘 )公車 (15 分鐘 )

清大校門清大校門 台北火車站台北火車站

台北捷運中山國中站台北捷運中山國中站 松山機場松山機場

Page 40: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

40

Refinement

Modify the solution to make it better

客運 (1.5 小時 )客運 (1.5 小時 )

捷運 (0.5 小時 )捷運 (0.5 小時 )

公車 (15 分鐘 )公車 (15 分鐘 )

清大校門清大校門 台北火車站台北火車站

台北捷運中山國中站台北捷運中山國中站 松山機場松山機場

重慶北路交流道重慶北路交流道客運 (1.2 小時 )客運 (1.2 小時 )

公車 (45 分鐘 )公車 (45 分鐘 )

Page 41: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

41

Example Search Problems

Puzzles: missionaries and cannibals, 8-puzzle, n-queens, Tower of Hanoi, …

2-player games: chess, checkers, Chinese Go, …

Proving theorems in logic and geometry Path finding “Industrial” problems: VLSI layout

design, assembling a complex object “AI problems”: speech recognition,

planning, …

Page 42: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

42

Importance of Problem Space

The choice of a problem space makes a big difference Finding a good abstraction is half of the

problem Intelligence is needed to figure out what

problem space to use

Page 43: Problem Solving Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Prof. I. K. Lundqvist,

43

Quiz

假設我們要以最小的成本設計一台掌上型DVD 播放機。我們能夠調整的參數包含 CPU的種類、記憶體的大小

請列出本設計的 state space 如何評估某一組參數 (path)可以達成目標 ?