2/13/2012

Jurusan Teknik Informatika – Universitas Widyatama Overview

• Deskripsi • Heuristic Search Heuristic Search – Generate-and Test – – Best-First Search Pertemuan : 3 – Greedy Best-First-Search – A* Dosen Pembina : – Iterative Deeping A* Sriyani Violina • Latihan Praktikum III Danang Junaedi • Tugas Rumah III Susetyo Bagas Baskoro

IF-UTAMA 1 IF-UTAMA 2

Deskripsi Limitations of uninformed search

• Pertemuan ini mempelajari bagaimana memecahkan • 8-puzzle suatu masalah dengan teknik searching. – Avg. solution cost is about 22 steps • Metode searching yang dibahas pada pertemuan ini – branching factor ~ 3 adalah heuristic search atau informed search yang – Exhaustive search to depth 22: terdiri dari: • 3.1 x 10 10 states – Generate-and Test – E.g., d=12, IDS expands 3.6 million states on average – Hill Climbing – Best-First Search [24 puzzle has 10 24 states (much worse)] – Greedy Best-First-Search – A* – Iterative Deeping A*

IF-UTAMA 3 IF-UTAMA 4

IF-UTAMA 1 2/13/2012

Recall search… Recall tree search…

This “strategy” is what differentiates different search algorithms

IF-UTAMA 5 IF-UTAMA 6

Informed methods add Heuristic domain-specific information

Merriam-Webster's Online Dictionary • Add domain-specific information to select the best Heuristic (pron. \hy u-’ris-tik\): adj. [from Greek heuriskein to discover.] involving or serving as an aid to learning, discovery, or problem-solving by experimental and path along which to continue searching especially trial-and-error methods The Free On-line Dictionary of Computing (15Feb98) • Define a heuristic function h(n) that estimates the heuristic 1. A rule of thumb, simplification or educated guess that reduces or limits the search for solutions in domains that are difficult and poorly “goodness” of a node n. understood. Unlike algorithms, heuristics do not guarantee feasible solutions and – Specifically, h(n) = estimated cost (or distance) of minimal are often used with no theoretical guarantee. 2. approximation algorithm. cost path from n to a goal state . From WordNet (r) 1.6 • The heuristic function is an estimate of how close we heuristic adj 1: (computer science) relating to or using a heuristic rule 2: of or relating to a general formulation that serves to guide investigation [ant: algorithmic] n : a are to a goal, based on domain-specific information commonsense rule (or set of rules) intended to increase the probability of solving some problem [syn: heuristic rule, heuristic program] that is computable from the current state description.

IF-UTAMA 7 IF-UTAMA 8

IF-UTAMA 2 2/13/2012

Heuristic function Heuristic functions for 8-puzzle

• Heuristic: • 8-puzzle – Definition: “using rules of thumb to find answers” – Avg. solution cost is about 22 steps – branching factor ~ 3 • Heuristic function h(n) – Exhaustive search to depth 22: – Estimate of (optimal) cost from n to goal • 3.1 x 10 10 states. – h(n) = 0 if n is a goal node – A good heuristic function can reduce the search process. – Example: straight line distance from n to Bucharest • Note that this is not the true state-space distance • Two commonly used heuristics • It is an estimate – actual state-space distance can be higher – h1 = the number of misplaced tiles • h (s)=8 – Provides problem-specific knowledge to the search algorithm 1 – h2 = the sum of the distances of the tiles from their goal positions (Manhattan distance).

• h2(s)=3+1+2+2+2+3+3+2=18

IF-UTAMA 9 IF-UTAMA 10

Heuristics Weak vs. strong methods

• All domain knowledge used in the search is encoded in the heuristic function h() . • We use the term weak methods to refer to methods that are • Heuristic search is an example of a “ weak method ” because of extremely general and not tailored to a specific situation. the limited way that domain-specific information is used to • Examples of weak methods include solve the problem. – Means-ends analysis is a strategy in which we try to represent the current • Examples: situation and where we want to end up and then look for ways to shrink the differences between the two. – Missionaries and Cannibals: Number of people on starting river bank – Space splitting is a strategy in which we try to list the possible solutions to – 8-puzzle: Number of tiles out of place a problem and then try to rule out classes of these possibilities. – 8-puzzle: Sum of distances each tile is from its goal position – Subgoaling means to split a large problem into several smaller ones that • In general: can be solved one at a time. – h(n) ≥ 0 for all nodes n • Called “weak” methods because they do not take advantage of – h(n) = 0 implies that n is a goal node more powerful domain-specific heuristics – h(n) = ∞ implies that n is a dead-end that can never lead to a goal

IF-UTAMA 11 IF-UTAMA 12

IF-UTAMA 3 2/13/2012

When to use ? Advantages

• The input data are limited or inexact. • Simple to understand. • Optimization model can not be used as for complex • Easier to implement and explain. reality. • Save formulation time. • Reliable and exact algorithm is not available. • Save computer programming and storage requirements. • Save computational time and thus real time in decision making. • It is possible to improve the efficiency of the • Often produce multiple acceptable solution. optimization process. • Usually it is possible to state a theoretical or empirical measure of • Symbolic rather than numerical processing is involved. the solution quality. • Quick decision must be made . • Incorporate intellegence to guide the search. • Computerization is not feasible. • It is possible to apply efficient heuristics to models that could be solved with mathematical programming.

IF-UTAMA 13 IF-UTAMA 14

Disadvantages Generate-and-Test

• An optimal solution can not be guaranteed. • It is a depth first search procedure since complete solutions must be generated before they can be tested. • There may be too many exceptions to the rules. • In its most systematic form, it is simply an exhaustive search of the problem space. • Sequential decision choices may fail to anticipate • Operate by generating solutions randomly. the future consequences of each choice. • Also called as British Museum algorithm • The interdependencies of one part pf a system • If a sufficient number of monkeys were placed in front of a set of typewriters, and left alone long enough, then they would can sometime have a profound influence on the eventually produce all the works of shakespeare. whole system. • Dendral: which infers the struture of organic compounds using NMR spectrogram. It uses plan-generate-test.

IF-UTAMA 15 IF-UTAMA 16

IF-UTAMA 4 2/13/2012

Generate-and-Test Algorithm Hill Climbing

1. Generate a possible solution. For some problems, this • Is a variant of generate-and test in which feedback from the test procedure is used to help the generator decide means generating a particular point in the problem which direction to move in search space. space. For others it means generating a path from a • The test function is augmented with a heuristic function start state that provides an estimate of how close a given state is to 2. Test to see if this is actually a solution by comparing the goal state. the chosen point or the endpoint of the chosen path to • Computation of heuristic function can be done with the set of acceptable goal states. negligible amount of computation. • Hill climbing is often used when a good heuristic 3. If a solution has been found, quit, Otherwise return to function is available for evaluating states but when no step 1. other useful knowledge is available.

IF-UTAMA 17 IF-UTAMA 18

Hill climbing Hill climbing algorithm

• Example: using the straight-line distance: 1. QUEUE <<----path only containing the root;

SS 2. WHILE QUEUE is not empty • Perform depth-first, AND goal is not reached AA 10.4 DD 8.98.9 BUT : DODO remove the first path from the QUEUE ;; • instead of left-to-right create new paths (to all children); 6.96.9 selection, AA 10.4 EE reject the new paths with loops; • FIRST select the child sort new paths (HEURISTIC) ;; with the best heuristic add the new paths to front of QUEUE ;; 6.76.7BB FF 3.03.0 value GG 3. IFIF goal reached THEN success; ELSE failure; IF-UTAMA 19 IF-UTAMA 20

IF-UTAMA 5 2/13/2012

Simple Hill Climbing Algorithm Simple Hill Climbing

1. Evaluate the initial state. If it is also goal state, then return it and • The key difference between Simple Hill climbing quit. Otherwise continue with the initial state as the current state. and Generate-and-test is the use of evaluation 2. Loop until a solution is found or until there are no new function as a way to inject task specific operators left to be applied in the current state: a. Select an operator that has not yet been applied to the current state and knowledge into the control process. apply it to produce a new state • Is on state better than another ? For this algorithm b. Evaluate the new state i. If it is the goal state, then return it and quit. to work, precise definition of better must be ii. If it is not a goal state but it is better than the current state, then make it the current state. provided. iii. If it is not better than the current state, then continue in the loop.

IF-UTAMA 21 IF-UTAMA 22

Algorithm: Steepest -Ascent Hill Steepest-Ascent Hill Climbing Climbing

• This is a variation of simple hill climbing which 1. Evaluate the initial state. If it is also a goal state, then return it and quit. Otherwise, continue with the initial state as the current considers all the moves from the current state and state. selects the best one as the next state. 2. Loop until a solution is found or until a complete iteration produces no change to current state: • Also known as Gradient search a. Let SUCC be a state such that any possible successor of the current state will be better than SUCC b. For each operator that applies to the current state do: i. Apply the operator and generate a new state ii. Evaluate the new state. If is is a goal state, then return it and quit. If not, compare it to SUCC. If it is better, then set SUCC to this state. If it is not better, leave SUCC alone. c. If the SUCC is better than the current state, then set current state to SYCC,

IF-UTAMA 23 IF-UTAMA 24

IF-UTAMA 6 2/13/2012

Hill climbing example Best First Search 2 8 3 1 2 3 start 1 6 4 h = -4 goal 8 4 h = 0 • Combines the advantages of bith DFS and BFS into a 7 5 7 6 5 single method. • DFS is good because it allows a solution to be found -5 -5 -2 without all competing branches having to be expanded. 2 8 3 1 2 3 • BFS is good because it does not get branches on dead 1 4 h = -3 8 4 h = -1 end paths. 7 6 5 7 6 5 • One way of combining the tow is to follow a single path at a time, but switch paths whenever some competing -3 -4 path looks more promising than the current one does. 2 3 2 3 1 8 4 1 8 4 h = -2 7 6 5 7 6 5 h = -3 -4 h(n) = -(number of tiles out of place)

IF-UTAMA 25 IF-UTAMA 26

BFS Algorithm: BFS

• At each step of the BFS search process, we select the most 1. Start with OPEN containing just the initial state promising of the nodes we have generated so far. • This is done by applying an appropriate heuristic function to each 2. Until a goal is found or there are no nodes left on of them. OPEN do: • We then expand the chosen node by using the rules to generate its a. Pick the best node on OPEN successors b. Generate its successors • Similar to Steepest ascent hill climbing with two exceptions: c. For each successor do: – In hill climbing, one move is selected and all the others are rejected, never to be reconsidered. This produces the straightline behaviour that is i. If it has not been generated before, evaluate it, add it to OPEN, and characteristic of hill climbing. record its parent. – In BFS, one move is selected, but the others are kept around so that they ii. If it has been generated before, change the parent if this new path is can be revisited later if the selected path becomes less promising. Further, better than the previous one. In that case, update the cost of getting to the best available state is selected in the BFS, even if that state has a value this node and to any successors that this node may already have. that is lower than the value of the state that was just explored. This contrasts with hill climbing, which will stop if there are no successor states with better values than the current state.

IF-UTAMA 27 IF-UTAMA 28

IF-UTAMA 7 2/13/2012

BFS : simple explanation BFS Ilustration

Step 1 Step 2 Step 3 A • It proceeds in steps, expanding one node at each step, until it A A generates a node that corresponds to a goal state. B C D • At each step, it picks the most promising of the nodes that have so 3 5 1 B C D far been generated but not expanded. 3 5 1 • It generates the successors of the chosen node, applies the Step 4 Step 5 E 4 F heuristic function to them, and adds them to the list of open nodes, A A 6 after checking to see if any of them have been generated before.

• By doing this check, we can guarantee that each node only B 3 C D B 3 C 5D 1 5 1 appears once in the graph, although many nodes may point to it as a successor. E F G H E 4 F G 6H 5 4 6 5 6 6

A 2 A 1

IF-UTAMA 29 IF-UTAMA 30

Greedy best-first search Romania with step costs in km

• Special case of best-first search – Uses h(n) = heuristic function as its evaluation function – Expand the node that appears closest to goal

IF-UTAMA 31 IF-UTAMA 32

IF-UTAMA 8 2/13/2012

Greedy best-first search example Greedy best-first search example

IF-UTAMA 33 IF-UTAMA 34

Greedy best-first search example Greedy best-first search example

IF-UTAMA 35 IF-UTAMA 36

IF-UTAMA 9 2/13/2012

Optimal Path Properties of greedy best-first search

• Complete? – Not unless it keeps track of all states visited • Otherwise can get stuck in loops (just like DFS) • Optimal? – No – we just saw a counter-example • Time? – O(b m), can generate all nodes at depth m before finding solution – m = maximum depth of search space • Space? – O(b m) – again, worst case, can generate all nodes at depth m before finding solution

IF-UTAMA 37 IF-UTAMA 38

A* Search A* Algorithm

• Expand node based on estimate of total path cost • BFS is a simplification of A* Algorithm • Presented by Hart et al through node • Algorithm uses: – f’: Heuristic function that estimates the merits of each node we generate. • Evaluation function f(n) = g(n) + h(n) This is sum of two components, g and h’ and f’ represents an estimate of the cost of getting from the initial state to a goal state along with the path – g(n) = cost so far to reach n that generated the current node. – g : The function g is a measure of the cost of getting from initial state to the – h(n) = estimated cost from n to goal current node. – h’ : The function h’ is an estimate of the additional cost of getting from the – f(n) = estimated total cost of path through n to goal current node to a goal state. – OPEN • Efficiency of search will depend on quality of – CLOSED heuristic h(n)

IF-UTAMA 39 IF-UTAMA 40

IF-UTAMA 10 2/13/2012

A* Algorithm A* Algorithm ( contd)

1. Start with OPEN containing only initial node. Set that node’s g For each of the SUCCESSOR, do the following: value to 0, its h’ value to whatever it is, and its f’ value to h’+0 a. Set SUCCESSOR to point back to BESTNODE. These or h’. Set CLOSED to empty list. backwards links will make it possible to recover the path once a 2. Until a goal node is found, repeat the following procedure: If solution is found. there are no nodes on OPEN, report failure. Otherwise picj the b. Compute g(SUCCESSOR) = g(BESTNODE) + the cost of node on OPEN with the lowest f’ value. Call it BESTNODE. getting from BESTNODE to SUCCESSOR Remove it from OPEN. Place it in CLOSED. See if the c. See if SUCCESSOR is the same as any node on OPEN. If so BESTNODE is a goal state. If so exit and report a solution. call the node OLD. Otherwise, generate the successors of BESTNODE but do not d. If SUCCESSOR was not on OPEN, see if it is on CLOSED. If so, call the node on CLOSED OLD and add OLD to the list of set the BESTNODE to point to them yet. BESTNODE’s successors. e. If SUCCESSOR was not already on either OPEN or CLOSED, then put it on OPEN and add it to the list of BESTNODE’s successors. Compute f’(SUCCESSOR) = g(SUCCESSOR) + h’(SUCCESSOR)

IF-UTAMA 41 IF-UTAMA 42

A* search example A* search example

IF-UTAMA 43 IF-UTAMA 44

IF-UTAMA 11 2/13/2012

A* search example A* search example

IF-UTAMA 45 IF-UTAMA 46

A* search example A* search example

IF-UTAMA 47 IF-UTAMA 48

IF-UTAMA 12 2/13/2012

Improving A* - memory-bounded heuristic Properties of A* search

• Complete? • Iterative-deepening A* (IDA*) – Using f-cost(g+h) rather than the depth – Yes (unless there are infinitely many nodes with f ≤ f(G) ) – Cutoff value is the smallest f-cost of any node that exceeded the cutoff on • Optimal? the previous iteration; keep these nodes only – Yes – Space complexity O(bd) – Also optimally efficient: • Recursive best-first search (RBFS) • No other optimal algorithm will expand fewer nodes, for a given heuristic – Best-first search using only linear space (Fig 4.5) • Time? – It replaces the f-value of each node along the path with the best f-value of its children (Fig 4.6) – Exponential in worst case – Space complexity O(bd) with excessive node regeneration • Space? • Simplified memory bounded A* (SMA*) – Exponential in worst case – IDA* and RBFS use too little memory – excessive node regeneration – Expanding the best leaf until memory is full – Dropping the worst leaf node (highest f-value) by backing up to its parent

IF-UTAMA 49 IF-UTAMA 50

Iterative Deepening A* (IDA*) 8-Puzzle

f(N) = g(N) + h(N)  Idea: Reduce memory requirement of A* with h(N) = number of misplaced tiles by applying cutoff on values of f  Consistent heuristic function h  Algorithm IDA*: 1. Initialize cutoff to f(initial-node) 4 2. Repeat: Cutoff=4 a. Perform depth-first search by expanding all nodes N such that f(N) ≤ cutoff b. Reset cutoff to smallest value f of non-expanded 6 (leaf) nodes

IF-UTAMA 51 IF-UTAMA 52

IF-UTAMA 13 2/13/2012

8-Puzzle 8-Puzzle

f(N) = g(N) + h(N) f(N) = g(N) + h(N) with h(N) = number of misplaced tiles with h(N) = number of misplaced tiles

4 4 Cutoff=4 4 Cutoff=4 4 5

6 6 6 6

IF-UTAMA 53 IF-UTAMA 54

8-Puzzle 8-Puzzle

f(N) = g(N) + h(N) f(N) = g(N) + h(N) with h(N) = number of misplaced tiles with h(N) = number of misplaced tiles

5 6 5

4 4 Cutoff=4 4 5 Cutoff=4 4 5

6 6 6 6

IF-UTAMA 55 IF-UTAMA 56

IF-UTAMA 14 2/13/2012

8-Puzzle 8-Puzzle

f(N) = g(N) + h(N) f(N) = g(N) + h(N) with h(N) = number of misplaced tiles with h(N) = number of misplaced tiles

4 4 Cutoff=5 Cutoff=5 4

6 6 6

IF-UTAMA 57 IF-UTAMA 58

8-Puzzle 8-Puzzle

f(N) = g(N) + h(N) f(N) = g(N) + h(N) with h(N) = number of misplaced tiles with h(N) = number of misplaced tiles

4 4 Cutoff=5 4 5 Cutoff=5 4 5 7

6 6 6 6

IF-UTAMA 59 IF-UTAMA 60

IF-UTAMA 15 2/13/2012

8-Puzzle 8-Puzzle

f(N) = g(N) + h(N) f(N) = g(N) + h(N) with h(N) = number of misplaced tiles with h(N) = number of misplaced tiles

4 5 4 5 5 Cutoff=5 4 5 Cutoff=5 4 5 7 7

6 6 6 6

IF-UTAMA 61 IF-UTAMA 62

8-Puzzle Advantages/Drawbacks of IDA* f(N) = g(N) + h(N) with h(N) = number of misplaced tiles  Advantages: • Still complete and optimal • Requires less memory than A* • Avoid the overhead to sort the fringe  Drawbacks: 4 5 5 • Can’t avoid revisiting states not on the Cutoff=5 4 5 5 current path 7 • Essentially a DFS

6 6 • Available memory is poorly used ( memory-bounded search, see R&N p. 101-104)

IF-UTAMA 63 IF-UTAMA 64

IF-UTAMA 16 2/13/2012

Latihan Praktikum II (4) Tugas Rumah III

1. Berdasarkan kasus yang anda peroleh dari dosen 1. Berdasarkan Latihan Praktikum III, cari atau buat pembina, gambarkan ilustrasi pencarian solusi dengan implementasi kasus tersebut! menggunakan metode Heuristic Search yang telah 2. Analisis programnya, kemudian buat penjelasan dibahas di kelas, jelaskan secara singkat! perbaris program dan penjelasan program secara 2. Analisis ilustrasi tersebut, mana yang memberikan keseluruhan serta hasil running -nya! Kirim dokumen solusi terbaik? Jelaskan alasan anda! hasil analisis program via e-mail!

Deadline: 21 Februari 2012 jam 24:00

IF-UTAMA 65 IF-UTAMA 66

Referensi

1. Suyanto.2007.”Artificial Intelligence” .Informatika. Bandung 2. -.2009. “Heuristic (Informed) Search[online]”. http://www.cse.fau.edu/~xqzhu/courses/cap4630_09spring/lectures/He uristic_Search_Part2.ppt.Tanggal Akses : 12 Februari 2011 3. Tim Finin et.al..2011.“Informed Search [online]”, http://www.cs.swarthmore.edu/~eeaton/teaching/cs63/slides/InformedS earchPart1.ppt, Tanggal Akses : 12 Februari 2011 4. Surma Mukhopadhyay.-.”Heuristic Search[online]”.url:http://student.bus.olemiss.edu/files/Conlon/Others/ BUS669/Notes/Chapter4/HEURISTIC%20%20%20SEARCH.ppt.Tan ggal Akses: 12 Februari 2011 5. Dr. Suthikshn Kumar.-.” Heuristic Search Techniques[online]”.url:http://www.cs.rpi.edu/~beevek/files/ai- heuristic-search.pdf.Tanggal Akses: 12 Februari 2011 6. -.2007.” Informed/Heuristic Search[online]”.url:http://www.ics.uci.edu/~smyth/courses/cs271/topic 3_heuristicsearch.ppt.Tanggal Akses: 12 Februari 2011 7. Dan sumber-sumber lain yang terkait

IF-UTAMA 67

IF-UTAMA 17