Anda di halaman 1dari 51

Kecerdasan Buatan/

Artificial Intelligence

Informed Searching Part 1


Rekyan Regasari Mardi Putri, ST, MT
Lailil Muflikhah, S.Kom, M.Sc
Imam Cholissodin, S.Si., M.Kom
M. Ali Fauzi, S.Kom, M.Kom
Pokok Bahasan
1. Best-first search
2. Min-Max
3. Alpha Betha Pruning
4. Greedy best-first search
5. A* search
6. Heuristics
7. Local search algorithms
8. Hill-climbing search
9. Simulated annealing
10. Local beam search
11. Genetic algorithms
12. Latihan Individu + Tugas Kelompok
Best-First Search
 Prinsip best-first search : Lakukan node expansion
terhadap node di fringe yang nilai f(n)-nya paling kecil.
 Ide dasar : f(n) adalah sebuah evaluation function →
fungsi yang menyatakan perkiraan seberapa “bagus”
sebuah node.
 Kenapa perkiraan? Kalau tidak, bukan search namanya!
 Implementasi: fringe adalah sebuah priority queue di
mana node disortir berdasarkan f(n).
 Contoh :
o Uniform-cost search
o Greedy (best-first) search
o A* search
Heuristic Function
 Kunci keberhasilan best-first search terletak di heuristic
function.
 Heuristic adalah :
o rule of thumb
o “kiat-kiat sukses”, “tips-tips keberhasilan”
o informasi tambahan bagi si agent (agar lebih sukses)
→ informed search
 Heuristic function h(n) adalah fungsi yang menyatakan
estimasi cost dari n ke goal state.
 Ada banyak kemungkinan heuristic function untuk
sebuah masalah.
Heuristic Function
 Contoh : (Romania with step costs in km)

 Sebuah heuristic function untuk agent turis Rumania :


hSLD(n) = jarak straight-line distance dari n ke Bucharest.
Greedy Best-First Search
 Prinsip greedy best-first search : Lakukan node
expansion terhadap node di fringe yang nilai h(n)-nya
paling kecil.
 Greedy best-first search selalu memilih node yang
kelihatannya paling dekat ke goal.
Greedy Best-First Search
 Prinsip greedy best-first search : Lakukan node
expansion terhadap node di fringe yang nilai h(n)-nya
paling kecil.
 Greedy best-first search selalu memilih node yang
kelihatannya paling dekat ke goal.
Greedy Best-First Search
 Prinsip greedy best-first search : Lakukan node
expansion terhadap node di fringe yang nilai h(n)-nya
paling kecil.
 Greedy best-first search selalu memilih node yang
kelihatannya paling dekat ke goal.
Greedy Best-First Search
 Prinsip greedy best-first search : Lakukan node
expansion terhadap node di fringe yang nilai h(n)-nya
paling kecil.
 Greedy best-first search selalu memilih node yang
kelihatannya paling dekat ke goal.
Greedy Best-First Search
 Properties of greedy best-first search :
o Complete? Ya, jika state space terbatas dan pengulangan state-
nya ditangani. (Lihat Neamt → Oradea)
o Time complexity? Secara teoritis, O(bm), tetapi heuristic
function yang baik akan lebih mempercepat.
o Space complexity? O(bm) → semua node disimpan di memory
o Optimal? Tidak.
A* Search
 Prinsip A* search : Hindari node yang berada di path yang “mahal”.
 Evaluation function f(n) = g(n) + h(n) :
o g(n) = Path cost ke n
o h(n) = Estimasi path cost dari n ke goal
o f(n) = Estimasi total cost melalui n
 Contoh penelusuran A* search :
A* Search
 Prinsip A* search : Hindari node yang berada di path yang “mahal”.
 Evaluation function f(n) = g(n) + h(n) :
o g(n) = Path cost ke n
o h(n) = Estimasi path cost dari n ke goal
o f(n) = Estimasi total cost melalui n
 Contoh penelusuran A* search :
A* Search
 Prinsip A* search : Hindari node yang berada di path yang “mahal”.
 Evaluation function f(n) = g(n) + h(n) :
o g(n) = Path cost ke n
o h(n) = Estimasi path cost dari n ke goal
o f(n) = Estimasi total cost melalui n
 Contoh penelusuran A* search :
A* Search
 Prinsip A* search : Hindari node yang berada di path yang “mahal”.
 Evaluation function f(n) = g(n) + h(n) :
o g(n) = Path cost ke n
o h(n) = Estimasi path cost dari n ke goal
o f(n) = Estimasi total cost melalui n
 Contoh penelusuran A* search :
A* Search
 Prinsip A* search : Hindari node yang berada di path yang “mahal”.
 Evaluation function f(n) = g(n) + h(n) :
o g(n) = Path cost ke n
o h(n) = Estimasi path cost dari n ke goal
o f(n) = Estimasi total cost melalui n
 Contoh penelusuran A* search :
A* Search
 Prinsip A* search : Hindari node yang berada di path yang “mahal”.
 Evaluation function f(n) = g(n) + h(n) :
o g(n) = Path cost ke n
o h(n) = Estimasi path cost dari n ke goal
o f(n) = Estimasi total cost melalui n
 Contoh penelusuran A* search :
Admissible Heuristics
 A* search menggunakan heuristic yang admissible 0 ≤ h(n) ≤
h*(n), di mana h*(n) adalah cost dari n yang sebenarnya.
 Bahasa mudahnya : nilai sebuah heuristic function tidak pernah
melebihi cost ke goal yang sebenarnya. Contoh : hSLD(n).
 A* search adalah optimal. Bukti optimalitas A* (1) :
o Andaikan G2 adalah goal suboptimal di dalam fringe. Ambil n
sebuah fringe node pada path menuju G, goal optimal, sbb :

• f(G2) = g(G2), karena h(G2) = 0


• g(G2) > g(G), karena G2 tidak optimal
• g(G) ≥ f(n), karena h admissible

o Karena f(G2) > f(n), algoritma A* search tidak pernah akan


memilih G2 untuk di-expand. Teorema terbukti!
Admissible Heuristics
 Consistency sebuah heuristic :
o Sebuah heuristic dikatakan consistent jika :
h(n) ≤ c(n, a, n’) + h(n’)
o Jika h konsisten, maka :
f(n’) = g(n’) + h(n’)
= g(n) + c(n, a, n’) + h(n’)
≥ g(n) + h(n)
≥ f(n)
o Pada sembarang path, nilai f(n) tidak pernah turun
(nondecreasing), atau monotonic.
o Theorem : If h(n) is consistent, A* using GRAPH-
SEARCH is optimal
Admissible Heuristics
 Bukti optimalitas A*(2)
o Node expansion A* berdasarkan urutan nilai f .
o Bayangkan penelusuran state space yang dilakukan
A* menambahkan f-contour.

o Bandingkan dengan “lapisan” yang ditelusuri breadth-


first dan uniform-cost.
A* Search
Properties of A* :
o Complete? Ya, kecuali jumlah node di mana f ≤ f(G)
tak terbatas.
o Time complexity? Eksponensial dalam (error h x
jumlah step solusi).
o Space complexity? O(bm) → semua node disimpan
di memory.
o Optimal? Ya.
 A* meng-expand semua node di mana f(n) < C*
 A* (mungkin) meng-expand beberapa node di mana f(n) = C*
 A* tidak pernah meng-expand node di mana f(n) > C*
Merancang Heuristic
 Contoh Admissible Heuristic
o h(n) untuk 8-puzzle
• h1(n) : jumlah angka yang salah posisi.
• h2(n) : jumlah jarak semua angka dari posisi yang benar (base
Manhattan Distance)
(0,0) (0,1) (0,2)
d
(1,0) (1,1) (1,2)
Dman x, y    x j  y j
(2,0) (2,1) (2,2) j 1
Posisi

• Diketahui posisi tile 1 di Start State (2,2) dan di Goal State (0,0)
• Hitung Dman(1) = |2-0|+|2-0| = 2 + 2 = 4

o h1(s) = 6
o h2(s) = Dman(1) + Dman(2)+ Dman(3) + Dman(4)+ Dman(5) + Dman(6)+ Dman(7)
+ Dman(8)= 4+0+3+3+1+0+2+1=14
Merancang Heuristic
Latihan Admissible Heuristic
o Perhatikan 8-puzzle berikut :
d
Dman x, y    x j  y j
j 1

Tentukan h1(s) dan h2(s) !


Merancang Heuristic
Membandingkan dua heuristic
o h1 dan h2 sama-sama admissible. Mana yang lebih
baik? Bandingkan jumlah node yang di-expand:
D IDS A*(h1) A*(h2)
12 3,473,941 539 113
24 54,000,000,000 39,135 1,641

o Jika h2(n) ≥ h1(n) untuk semua n (dan keduanya


admissible), dikatakan bahwa h2 men-dominate h1
dan lebih baik untuk search.
o Semakin besar nilai h(n), semakin dekat ke h*(n),
semakin banyak node yang tidak di-expand (di-
prune), semakin efisien search-nya!
A* Search
f(N) = h(N) = jumlah angka yang salah posisi

3 3 4
5 3

4
2
4 2 1
3 3
0
4

5 4
A* Search
f(N) = g(N) + h(N) dengan h(N) = jumlah angka
yang salah posisi
3+3
1+5 2+3

3+4
5+2
0+4 3+2 4+1
1+3 2+3
5+0
3+4

1+5 2+4
A* Search
f(N) = h(N) =  jarak semua angka dari posisi
yang benar (base Manhattan Distance)

6 5

2
5 2 1
4 3
0
4

6 5
Admissible Heuristics
 Consistency sebuah heuristic :
o Sebuah heuristic dikatakan consistent jika :
h(n) ≤ c(n, a, n’) + h(n’)
o Jika h konsisten, maka :
f(n’) = g(n’) + h(n’)
= g(n) + c(n, a, n’) + h(n’)
≥ g(n) + h(n)
≥ f(n)
o Pada sembarang path, nilai f(n) tidak pernah turun
(nondecreasing), atau monotonic.
o Theorem : If h(n) is consistent, A* using GRAPH-
SEARCH is optimal
Admissible Heuristics
 Bukti optimalitas A*(2)
o Node expansion A* berdasarkan urutan nilai f .
o Bayangkan penelusuran state space yang dilakukan
A* menambahkan f-contour.

o Bandingkan dengan “lapisan” yang ditelusuri breadth-


first dan uniform-cost.
Hill-Climbing Search
 NILAI sebuah node → h(n) (heuristic function)
 Bayangkan seorang penderita amnesia mendaki gunung dengan
kabut tebal (thick fog).
o State: posisi koordinat (X,Y)
o h(n): ketinggian pendaki
 Konsep penting: state space sebagai landscape.
 Disebut juga greedy local search
 Tergantung pilihan initial state, hill-climbing bisa terperangkap
dalam local maximum.
Hill-Climbing Search
 Local maximum: tidak ada tetangga yang lebih baik, tetapi bukan
solusi optimal.
 Plateau (dataran): semua tetangga sama baiknya.
 Contoh Hill-Climbing Search :
Algoritma Minimax
 Definisi algoritma ini rekursif, dengan base case pada
terminal state
 Untuk menghitung MINIMAX VALUE pada initial state,
harus depth-first search seluruh game tree!
 Complete? Ya, kalau game tree-nya finite
 Optimal? Ya, asumsi lawan musuh optimal juga. (Kalau
tidak? “Lebih optimal”!)
 Time complexity? O(bm)
 Space complexity? O(bm) (atau O(m) dgn.
backtracking)
 Contoh dalam catur: b ≈ 35, m ≈ 100 → pencarian
strategi optimal berdasarkan Minimax tidak feasible!
Algoritma Minimax
 Biasanya dalam suatu permainan ada batasan waktu
 Andaikan ada agent bermain catur yang diberi 100 detik
untuk “berpikir” tiap langkah.
 Mis. sistem bisa memproses 104 node/detik → 106
node/langkah.
 Kita bisa melakukan aproksimasi sbb.:
o Cutoff: batasi depth yang diproses (≈ IDS).
o Evaluation function: prediksi dari nilai utility function
(tidak perlu sampai ke terminal state).
Algoritma Minimax
 Biasanya, evaluation function berupa kombinasi linier
dari fitur-fitur sebuah state: n

 Eval (s) = w1f1(s) + w2f2(s) + . . . + wnfn (s) = 


i 1
wi f i ( s )
 Mis. untuk catur:
 w1 = 1, f1 = jumlah pion putih - jumlah pion hitam
 w2 = 3, f2 = jumlah gajah putih - jumlah gajah hitam
Algoritma Minimax
 Contoh penerapan algoritma Min Max. Diberikan sebuah situasi
permainan seperti di bawah ini :

• X (max player) sedang dalam giliran untuk melanjutkan permainan.


Berikan semua situasi berikutnya yang mungkin untuk X

• Pilihlah jalur yang tepat sesuai dengan algoritma minmax, jika


diketahui fungsi utilitas untuk situasi menang untuk X = +10, kalah =
-10, dan draw = 0.
Algoritma Minimax
 Penyelesaian pada Tic-Tac-Toe (Algoritma Min Max) :
Game Playing - Minimax
 Misal, Terdiri dari :
o 2 players: MAX (computer) and MIN (opponent)
o Sifat : deterministic, perfect information. Select a depth-bound
(say: 2) and evaluation function
- Construct the tree up till
MAX Select the depth-bound
3 this move
- Compute the evaluation
function for the leaves

MIN
2 1 3 - Propagate the evaluation
function upwards:
- Taking minima in MIN
- Taking maxima in MAX
MAX
2 5 3 1 4 4 3
Minimax Example
MAX 1 A
Select this move

MIN 1 B -3 C

MAX 4 D 1 E 2 F -3 G

4 -5 -5 1 -7 2 -3 -8

= terminal position = agent = opponent


Algoritma Alpha-beta Pruning
 Secara umum digunakan untuk optimasi Algoritma Mini-
max.
 Instead of :
o First creating the entire tree (up to depth-level)
o Then doing all propagation
o Interleave the generation of the tree and the propagation of
values.
 Point :
o Some of the obtained values in the tree will provide information
that other (non-generated) parts are redundant and do not need
to be generated.
Algoritma Alpha-beta Pruning
 Principles:
o Generate the tree depth-first, left-to-right (propagate final values
of nodes as initial estimates for their parent node).
MAX 2 - The MIN-value (1) is already
smaller than the MAX-value of
the parent (2)

- The MIN-value can only


decrease further,
MIN 2 =2
1
- The MAX-value is only allowed
to increase,

- No point in computing further


below this node
MAX
2 5 1
Algoritma Alpha-beta Pruning
 If an ALPHA-value is larger or equal than the Beta-value of a
descendant node : stop generation of the children of the descendant

MAX 2 Alpha-value

MIN 2 =2 Beta-value
1

MAX
2 5 1
Algoritma Alpha-beta Pruning
 If an Beta-value is smaller or equal than the Alpha-value of a
descendant node: stop generation of the children of the descendant

MAX 2

MIN 2 =2 Beta-value
1

MAX Alpha-value
2 5 3
Algoritma Alpha-beta Pruning
MAX A

MIN ≤6 B C

MAX 6 D ≥8 E

H I J K
6 5 8
= agent = opponent
Algoritma Alpha-beta Pruning
MAX ≥6 A

MIN 6 B ≤2 C

MAX 6 D ≥8 E 2 F G

H I J K L M
6 5 8 2 1
= agent = opponent
Algoritma Alpha-beta Pruning
MAX ≥6 A

MIN 6 B 2 C

MAX 6 D ≥8 E 2 F G

H I J K L M
6 5 8 2 1
= agent = opponent
Algoritma Alpha-beta Pruning
MAX 6 A

MIN 6 B 2 C beta
cutoff

MAX 6 D ≥8 E 2 F G
alpha
cutoff

H I J K L M
6 5 8 2 1
= agent = opponent
Mini-Max with  at work:
 4 16  5 31 = 5 39
MAX
8 6  5 23
= 4 15 = 5 30  3 38
MIN

 2 10  1 18
 1 33
8 2  4 12  3 20  3 25  2 35
=8 5 9 8 = 4 14 = 5 22  9 27  6 29 = 3 37
MAX

8 7 3 9 1 6 2 4 1 1 3 5 3 9 2 6 5 2 1 2 3 9 7 2 8 6 4
1 3 4 7 9 11 13 17 19 21 24 26 28 32 34 36
11 static evaluations saved !!
Example of a perfectly ordered tree
21 MAX

21 12 3 MIN

21 24 27 12 15 18 3 6 9
MAX

21 20 19 24 23 22 27 26 25 12 11 10 15 14 13 18 17 16 3 2 1 6 5 4 9 8 7
Algoritma Alpha-beta Pruning
 Kesimpulan :
o alpha-beta algorithm does the same calculation as
minimax, and is more efficient since it prunes
irrelevant branches.
o usually, the complete game tree is not expanded,
search is cut off at some point and an evaluation
function calculated to estimate the utility of a state.
o So far, for a possibly good and efficient search:
• select good search method/technique
• provide info/heuristic if possible
• apply prune irrelevant branches
Ringkasan
 Best-first search :
o Uniform-cost search : f(n) = g(n)
o Greedy best-first search : f(n) = h(n)
o A* search : f(n) = g(n) + h(n)
 Dengan heuristic yang admissible dan consistent , A* pasti complete dan
optimal.
 Heuristic dapat diperoleh dari variasi masalah yang dipermudah, atau
submasalah.
 Search di mana environment-nya tidak observable atau non-
deterministic masih bisa diatasi.
 Permasalahan utama : menghindari local maximum secara stochastic.
Selesai

Anda mungkin juga menyukai