A. DEFINISI MASALAH
Buatlah program dengan menggunakan algoritma Uniform-cost Search dengan me-
modifikasi program BFS pada halaman sebelumnya (Uniform-cost Search mirip
dengan BFS, tetapi mempertimbangakan nilai cost, atau dalam kasus ini adalah nilai
jarak antar node) pada pencarian rute berikut ini:
Input:
a. Kota asal
b. Kota Tujuan
Output:
a. Langkah-langkah pencarian rute
b. Rute yang merupakan solusi dan jarak yang ditempuh
Kasus khusus yang wajib diujikan:
a. Kota A ke kota D
b. Kota H ke kota N
c. Kota H ke kota U
d. Kota O ke H
e. Kota I ke A
B. JAWAB
Node.java
1 package bab3;
2 public class Node{
3 public String Kota;
4 int pathCost;
5 public Node parent;
6 public Edge[] adjacencies;
7 public Node[] child;
8 public int[] cost;
9 public Node(String Kota, int child) {
10 this.Kota = Kota;
11 this.child = new Node[child];
12 this.cost = new int[child];
13 this.adjacencies = new Edge[child];
14 }
15 public void adjacencies(Object... values) {
16 int length = values.length / 2;
17 for (int i = 0; i < length; i++) {
18 this.child[i] = (Node) values[i];
19 }
20 for (int i = length, j = 0; i <
21 values.length; i++, j++) {
22 this.cost[j] = (int) values[i];
23 }
24 for (int i = 0; i < child.length; i++) {
25 this.adjacencies[i] = new
26 Edge(this.child[i],
27 this.cost[i]);
28 }
29 }
30 @Override
31 public String toString() {
32 return this.Kota;
33 }
34 }
35 class Edge {
36 public final double cost;
37 public final Node side;
38 public Edge(Node targetNode, int value) {
39 this.cost = value;
40 this.side = targetNode;
41 }
42 }
Main.java
1 package bab3;
2 public class Main {
3 public static void main(String[] args) {
4 Node A = new Node("A", 4);
5 Node B = new Node("B", 2);
6 Node C = new Node("C", 2);
7 Node D = new Node("D", 3);
8 Node E = new Node("E", 5);
9 Node F = new Node("F", 2);
10 Node G = new Node("G", 2);
11 Node H = new Node("H", 1);
12 Node I = new Node("I", 3);
13 Node J = new Node("J", 2);
14 Node K = new Node("K", 2);
15 Node L = new Node("L", 1);
16 Node M = new Node("M", 1);
17 Node N = new Node("N", 3);
18 Node O = new Node("O", 1);
19 Node P = new Node("P", 1);
20 Node Q = new Node("Q", 2);
21 Node R = new Node("R", 2);
22 Node S = new Node("S", 2);
23 Node T = new Node("T", 3);
24 Node U = new Node("U", 2);
25 Node V = new Node("V", 3);
26 Node X = new Node("X", 3);
27 A.adjacencies(B, I, X, V, 145, 148, 55, 167);
28 B.adjacencies(A, C, 145, 98);
29 C.adjacencies(B, D, 98, 212);
30 D.adjacencies(C, E, K, 212, 102, 102);
31 E.adjacencies(D, L, F, M, N, 102, 95, 152, 73,
97);
32 F.adjacencies(E, G, 152, 83);
33 G.adjacencies(F, H, 83, 75);
34 H.adjacencies(G, 75);
35 I.adjacencies(A, J, Q, 148, 75, 25);
36 J.adjacencies(I, K, 75, 95);
37 K.adjacencies(D, J, 102, 95);
38 L.adjacencies(E, 95);
39 M.adjacencies(E, 73);
40 N.adjacencies(E, O, P, 97, 25, 65);
41 O.adjacencies(N, 25); //o
42 P.adjacencies(N, 65); //p
43 Q.adjacencies( I, X, 25, 30); //q
44 R.adjacencies( S, X, 93, 57); //r
45 S.adjacencies( R, T, 93, 112); //s
46 T.adjacencies( S, U, V, 112, 75, 25); //t
47 U.adjacencies( T, V, 75, 85); //u
48 V.adjacencies( A, T, U, 167, 25, 85); //v
49 X.adjacencies( A, Q, R, 55, 30, 57); //x
50
51 UCS bfs = new UCS(A, D);
52 bfs.compute();
53 bfs.ShortestPath();
54 bfs.SmallestCost();
55 }
56 }
UCS.java
1 package bab3;
2 import java.util.*;
3 public class UCS {
4 Node NodeAwal;
5 Node NodeAkhir;
6 List<Node> finalPath;
7 int finalCost;
8 public UCS(Node start, Node goalNode) {
9 this.NodeAwal = start;
10 this.NodeAkhir = goalNode;
11 }
12 public void compute() {
13 System.out.println("Asal : " +
this.NodeAwal );
14 System.out.println("Tujuan : " +
this.NodeAkhir);
15 System.out.println("");
16 System.out.println("Langkah - Langkah");
17 NodeAwal.pathCost = 0;
18 PriorityQueue<Node> queue = new
19 PriorityQueue<>(30,
20 (Node first, Node second) -> {
21 if (first.pathCost >
second.pathCost)
22 {
23 return 1;
24 } else if (first.pathCost
<second.pathCost) {
25 return -1;
26 } else {
27 return 0;
28 }
29 });
30 queue.add(NodeAwal);
31 Set<Node> explored = new HashSet<>();
32 boolean found = false;
33 do {
34 Node current = queue.poll();
35 explored.add(current);
36 if
37 (current.Kota.equals(NodeAkhir.Kota)) {
38 found = true;
39 }
40 for (Edge temp : current.adjacencies) {
41 Node child = temp.side;
42 double cost = temp.cost;
43 child.pathCost = current.pathCost +
(int)cost;
44 if (!explored.contains(child) &&
45 !queue.contains(child)) {
46 child.parent = current;
47 queue.add(child);
48 System.out.print(child + " ");
49 System.out.println(queue);
50 }
51 else if ((queue.contains(child)) &&
(child.pathCost >current.pathCost)) {
52 child.parent = current;
53 queue.remove(child);
54 queue.add(child);
55 }
56 }
57 } while (!queue.isEmpty());
58 System.out.println();
59 }
60 public void SmallestCost() {
61 this.finalCost = 0;
62 for (int i = 0; i < finalPath.size() - 1;
i++) {
63 int
index=Arrays.asList(finalPath.get(i).child).contains(
64 finalPath.get(i).parent) ?
65
66 Arrays.asList(finalPath.get(i).child).indexOf(finalPa
67 th.get(i).parent) : -1;
68 System.out.println(finalPath.get(i)+"--
>"+finalPath.get(i).cost[index]);
69 this.finalCost +=
70 finalPath.get(i).cost[index];
71 }
72 System.out.println("Path Cost terkecil : " +
this.finalCost + "\n");
73 }
74 public void ShortestPath() {
75 List<Node> path = new ArrayList<>();
76 for (Node node = NodeAkhir; node != null;
node = node.parent) {
77 path.add(node);
78 }
79 this.finalPath = new ArrayList<>(path);
80 Collections.reverse(path);
81 System.out.println("Rute yang dilalui: "
+path );
82 }
83 }
Node.java
1 Deklarasi package
3–8 Deklarasi variabel yang dibutuhkan
9 – 14 Deklarasi konstruktor untuk mengisi nilai variabel
15 – 34 Deklarasi method adjacencies untuk mendeklarasikan penghubung antar node
35 – 41 Deklarasi Clas Edge untuk mengisi nilai edge dan target Node
UCS.java
1 Deklarasi package
2 Import semua library java
3 Deklarasi kelas UCS
4-7 Deklarasi variabel yang dibututhkan UCS
8- 11 Deklarasi konstruktor untuk mengisi nilai variabel
12 - 59 Deklarasi method compute untuk menentukan langkah – langkah pencarian
rute dari node awal hingga node akhir dengan menggunakan priority queue
60 - 73 Deklarasi method SmallesCost untuk menampilkan Path Cost terkecil
74 – 82 Deklarasi method shortestPath untuk menampilkan rute terpendek yang dilalui
Main.java
1 Deklarasi package
3 Deklarasi method main
4 - 26 Mendeklarasikan Objek Node beserta mengisi nilai kota dan child
27 - 48 Memberikan nilai adjacencies tiap Node
50 Deklarasi objek UCS beserta nilai yang mau dicari
51 Menentukan langkah-langkah pencarian objek UCS
52 Menampilkan Path Cost terkecil objek UCS
53 Menampilkan rute terpendek yang akan dilalui objek UCS
SCREENSHOT
Kota A ke kota D
Kota H ke kota N
Kota H ke kota U
Kota O ke H
Kota I ke A
C. KESIMPULAN
1. Jelaskan bentuk permasalahan yang dapat diselesaikan dengan uninform
search!
Uninformed Search Algoritm merupakan salah satu mekanisme searching
yang ada kecerdasan buatan. Algoritma ini hanya berfokus memberikan
informasi tentang algoritma itu sendiri tanpa memberikan informasi apapun
tentang permasalahan yang ada. algoritma ini melakukan generate dari
successor serta membedakan goal state dari non gol State. ketika
permasalahan tidak memiliki informasi apapun tentang permasalah yang ada
etapi hanya berfokus memberikan informasi tentang algorima tersebut.