[Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4
1. 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
2. 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 */
3. 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
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){
8. 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) {
9. 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);
}
}