[Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

9
Sekolah Tinggi Teknologi Telematika Telkom Laporan Praktikum Kecerdasan Buatan Modul 4 – [Greedy Best First Search] Dosen Pengampu: Muhammad Zidny Naf’an, Lc., S.Kom., M.Kom. Nama Mahasiswa : Deprilana Ego Prakasa NIM : 14102055 Kelas : IF B 2014

Transcript of [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

Page 1: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

Sekolah Tinggi Teknologi Telematika Telkom

Laporan Praktikum Kecerdasan Buatan

Modul 4 – [Greedy Best First Search]

Dosen Pengampu: Muhammad Zidny Naf’an, Lc., S.Kom., M.Kom.

Nama Mahasiswa : Deprilana Ego Prakasa

NIM : 14102055

Kelas : IF B 2014

Page 2: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

Modul4.java Modul4.java /*

Node.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package modul4;

/** * * @author samsung * */

import java.util.ArrayList; import java.util.HashMap; import java.util.List;

public class Node {

private double h; private booleanwasVisited; private String label;

HashMap<Node, Double>adjNode;

/** * @return the h */

public double getH() { return h;

}

/** * @param h the h to set */ public void setH(double h)

{ this.h = h; }

/** * @return the wasVisited */

Page 3: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

public booleanisWasVisited()

{ return wasVisited; }

/** * @paramwasVisited the wasVisited to set */ public void setWasVisited(booleanwasVisited)

{ this.wasVisited = wasVisited; }

/** * @return the label */

public String getLabel() { return label;

}

/** * @param label the label to set */ public void setLabel(String label) {

this.label = label; }

/** * @return the adjNode */

public HashMap<Node, Double>getAdjNode() { return adjNode;

}

/** * @paramadjNode the adjNode to set */ public void setAdjNode(HashMap<Node, Double>adjNode)

{ this.adjNode = adjNode; }

}

GreedyBestFirstSearch.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates

Page 4: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

* and open the template in the editor. */ package modul4;

/** * * @author samsung * */

import java.util.ArrayList; import java.util.HashMap; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.IOError; import java.util.Map;

public class GreedyBestFirstSearch { public ArrayList<Node>alNodes; public void loadDataset(String pathname){

String line; File file = new File(pathname); try {

BufferedReaderbr = new BufferedReader (new FileReader(file));

//initial Node while ((line = br.readLine())!=null){

Node node = new Node(); String[] split1 = line.split("%"); //get Node

String[] splitLabel = split1[0].split("_"); if(splitLabel.length> 1){

node.setLabel(splitLabel[0]); node.setH(Double.parseDouble(splitLabel[1]));

} else {

node.setLabel(split1[0]); node.setH(0);

} alNodes.add(node);

} br.close();

} catch (FileNotFoundException e){ e.printStackTrace();

} catch (IOException e){

Page 5: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

e.printStackTrace();

} } public void setAdjacentNode(String pathname){

String line; File file = new File(pathname); try {

BufferedReaderbr = new BufferedReader(new FileReader(file)); Node node = new Node();

//Initial Adjacent Node while((line = br.readLine())!=null){

if(!line.equalsIgnoreCase("G")){ String label = "";

String[] split1 = line.split("%"); //get node String[] splitLabel = split1[0].split("_"); if(splitLabel.length> 1){

label = splitLabel[0]; } else {

label = split1[0]; }

intidxRoot = getIndexOfNodes(label); HashMap<Node, Double>adjNode = new HashMap<Node, Double>();

String[] split2 = split1[1].split("#"); //get adjacent if(split2.length>1){

for(String s: split2){ String[] split3 = s.split("_");//get distance

beetwen node and adjacent intidx = getIndexOfNodes(split3[0]);

if(idx!=-1){ Node tNode =

alNodes.get(idx); adjNode.put(tNode,Double.parseDouble(split3[1])); }

} } else{

String[] split3 = split1[1].split("_"); //get distance beetwen node and adjacent intidx = getIndexOfNodes(split3[0]);

if(idx!=1){ Node tNode = alNodes.get(idx);

//tNode.setHeuristicValue(Double.ParseDouble(split3[1]))); adjNode.put(tNode, Double.parseDouble(split3[1]));

} }

alNodes.get(idxRoot).adjNode = adjNode;

Page 6: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

}

} br.close();

} catch(FileNotFoundException e){ e.printStackTrace();

} catch (IOException e){ e.printStackTrace();

} } public ArrayList<Node>greedyBFS(){

ArrayList<Node>alGreedyPath = new ArrayList<Node>(); ArrayList<Node>alClosed = new ArrayList<Node>(); ArrayList<Node>alOpen = new ArrayList<Node>();

alOpen.add(alNodes.get(0)); alGreedyPath.add(alNodes.get(0));

Node goalNode = alNodes.get(alNodes.size()-1);

while(!alOpen.isEmpty()){ Node currentBestNode = getBestNode(alOpen, "greedy");

if(currentBestNode.getLabel().equalsIgnoreCase(goalNode.getLabel())){ alGreedyPath.add(goalNode);

return alGreedyPath; }

intidxBestNode = getIndexOfNodes(currentBestNode.getLabel());

alOpen.remove(idxBestNode); alClosed.add(currentBestNode); alGreedyPath.add(currentBestNode);

//successor of current Best Node ArrayList<Node>alAdjNode = new ArrayList<Node>();

for (Map.Entry<Node, Double> entry : currentBestNode.adjNode.entrySet())

{ if(!isInOpenList(alOpen,entry.getKey())){

alOpen.add(entry.getKey()); }

}

} alGreedyPath.add(goalNode);

return alGreedyPath;

}

Page 7: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

private intgetIndexOfNodes(String label) {

for(inti=0; i<alNodes.size(); i++){ if(alNodes.get(i).getLabel().equalsIgnoreCase(label)){

return i; }

} return -1;

}

private booleanisInOpenList(ArrayList<Node>alOpen, Node node){ for(Node n: alOpen){

if(n.getLabel().equalsIgnoreCase(node.getLabel())) return true;

} return false;

}

private Node getBestNode(ArrayList<Node>alOpen, String greedy) { Node tNode = new Node(); double minDistance = Integer.MAX_VALUE;

for(Node node: alOpen){ if(minDistance>node.getH()){

minDistance = node.getH(); tNode = node;

} }

return tNode; }

private void display(ArrayList<Node>alNodes){

}

Page 8: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

Jawaban Soal Latihan

6. Untuk mencari posisi suatu NODE pada OPEN, yaitu membuat method getIndexOfNodes!

GreedyBestFirstSearch.java

private int getIndexOfNodes(String label) {

for(int i=0; i<alNodes.size(); i++){

if(alNodes.get(i).getLabel().equalsIgnoreCase(label)){

return i;

}

}

return -1;

}

7. Untuk mengecek apakah Node sudah ada di OPEN, buatlah method menggunakan isInOpenList :

GreedyBestFirstSearch.java

private boolean isInOpenList(ArrayList<Node> alOpen, Node node){

for(Node n: alOpen){

if(n.getLabel().equalsIgnoreCase(node.getLabel()))

return true;

}

return false;

}

8. Untuk Mencari Node terbaik (jarak perkiraan terkecil) pada list

GreedyBestFirstSearch.java

private Node getBestNode(ArrayList<Node> alOpen, String greedy) {

Page 9: [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

Node tNode = new Node();

double minDistance = Integer.MAX_VALUE;

for(Node node: alOpen){

if(minDistance > node.getH()){

minDistance = node.getH();

tNode = node;

}

}

return tNode;

}

9. Method display untuk mencetak urutan hasil lintasan yang disimpan dalam alGreedyPath

GreedyBestFirstSearch.java

private void display(ArrayList<node> alNodes){

for(node node: alNodes){

System.out.println(node.adjNode);

}

}