清大 松山機場 - 清華大學資訊工程系

CS1103 電機資訊工程實習
Problem Solving
Prof. Chung-Ta King
Department of Computer Science
National Tsing Hua University
(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)
假設我們要到澎湖玩
1
由松山機場搭機前往
2
如何由清大到松山機場?
3
清大  松山機場
清大校門
三重/新竹/豪泰客運
台北火車站
台北捷運
台北捷運中山國中站
225路公車
松山機場
4
How Did We Derive the Solution?


Problem solving as state space search
What is “state”?


States: location of you
Transitions: transportation (客運、高鐵、台鐵、捷
運、公車、計程車、走路)
清大校門
客運
台北火車站
計程車
台北捷運
公車
台北捷運中山國中站
松山機場
5
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
Astronaut
Grain, Goose, Fox
6
Problem Solving as State Space
Search

Goal:


Problem representation:



Astronaut, Fox, Goose and Grain across river
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), . . .
7
Initial
State
Goal
State
8
9
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
10
The 8-Puzzle Problem
1
4
7
5

8
3
1
4
3
6
7
6
2
2
5
8
Operator: “move blank”
11
State Space of 8-Puzzle
12
目前為止我們只到一個解答

但做為資工系的學生,我們不但要知道一個解
答,我們更想知道一個更好的解答,甚至
最佳解 (optimal solution)

例如:sorting

Bubble sort, selection sort, insertion sort, merge
sort, heapsort, quicksort, ...
13
問題是...

何謂「最佳」?








時間最短
記憶體用最少
最便宜
耗電最低
最環保
最容易使用
最漂亮
Optimization goal
還有更複雜的目標 ...


又快又便宜
功能又強又省電
不同目標如何協調?
14
如何計算目標值?

時間:
清大校門
客運 (1.5小時)
台北火車站
捷運 (0.5小時)
公車 (15分鐘)
台北捷運中山國中站

松山機場
其他方法:

步數、運算數
15
如何計算目標值?

成本:
清大校門
客運 ($100)
台北火車站
捷運 ($40)
台北捷運中山國中站

公車 ($15)
松山機場
其他方法:

元件成本、售價
16
如何計算目標值?

路上風景最好:
清大校門
客運 (???)
台北火車站
捷運 (???)
台北捷運中山國中站

公車 (???)
松山機場
常用方法:專家評分、民意調查
17
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
18
Traveling Salesperson Problem
19
State Space of TSP
(arc label = cost from root)
20
Nearest Neighbor Path
Nearest neighbor path = AEDBCA (550)
Minimal cost path = ABCDEA (375) 21
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?
22
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
23
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)
24
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
25
Breadth-first Search
26
Depth-first Search
Depth bound = 5
27
“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
28
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
29
Tic-tac-toe

# of states in an exhaustive search is 9!
30
Tic-tac-toe

A heuristic is moving to the board in which X
has the most winning lines
31
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
32
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
33
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
34
Heuristic Evaluation Function


The goal is to use the limited information
available in a single state descriptor to make
intelligent choices
For 8-puzzle, heuristic
may be:



# tiles out of place
Sum of all the distances
by which the tiles are out
of pace
2 x # direct tile reversals
35
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
36
Iterative Improvement

Start with one solution and make
modifications to improve its quality
37
Find Initial Solution

Traverse the state space and find one
solution
清大校門
客運 (1.5小時)
台北火車站
捷運 (0.5小時)
公車 (15分鐘)
台北捷運中山國中站
松山機場
38
Refinement

Modify the solution to make it better
重慶北路交流道
客運 (1.2小時)
清大校門
客運 (1.5小時)
公車 (45分鐘)
台北火車站
捷運 (0.5小時)
公車 (15分鐘)
台北捷運中山國中站
松山機場
39
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,
…
40
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
41
Quiz



假設我們要以最小的成本設計一台掌上型DVD
播放機。我們能夠調整的參數包含CPU的種類
、記憶體的大小
請列出本設計的state space
如何評估某一組參數 (path)
可以達成目標?
42