SlideShare a Scribd company logo
1 of 9
Download to read offline
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
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 */
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
* 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){
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;
}
}
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;
}
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){
}
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) {
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);
}
}

More Related Content

What's hot

Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Functions in python
Functions in pythonFunctions in python
Functions in pythonIlian Iliev
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Rick Copeland
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in pythonKarin Lagesen
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlowBayu Aldi Yansyah
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Andreas Dewes
 
Erlang/OTP for Rubyists
Erlang/OTP for RubyistsErlang/OTP for Rubyists
Erlang/OTP for RubyistsSean Cribbs
 
Learning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysisLearning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysisAndreas Dewes
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad Fabernovel
 
Intro to Functions Python
Intro to Functions PythonIntro to Functions Python
Intro to Functions Pythonprimeteacher32
 
Advanced Python, Part 1
Advanced Python, Part 1Advanced Python, Part 1
Advanced Python, Part 1Zaar Hai
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리용 최
 

What's hot (20)

Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Chtp414
Chtp414Chtp414
Chtp414
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlow
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
 
C# - What's next
C# - What's nextC# - What's next
C# - What's next
 
Erlang/OTP for Rubyists
Erlang/OTP for RubyistsErlang/OTP for Rubyists
Erlang/OTP for Rubyists
 
Learning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysisLearning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysis
 
Introduction to Go
Introduction to GoIntroduction to Go
Introduction to Go
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Python dictionaries
Python dictionariesPython dictionaries
Python dictionaries
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad
 
Intro to Functions Python
Intro to Functions PythonIntro to Functions Python
Intro to Functions Python
 
Python course Day 1
Python course Day 1Python course Day 1
Python course Day 1
 
Advanced Python, Part 1
Advanced Python, Part 1Advanced Python, Part 1
Advanced Python, Part 1
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
IT6801-Service Oriented Architecture-Unit-2-notes
IT6801-Service Oriented Architecture-Unit-2-notesIT6801-Service Oriented Architecture-Unit-2-notes
IT6801-Service Oriented Architecture-Unit-2-notes
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리
 

Viewers also liked

Laporan ai modul 2-if b 2014-14102055-deprilana ego prakasa
Laporan ai modul 2-if b 2014-14102055-deprilana ego prakasaLaporan ai modul 2-if b 2014-14102055-deprilana ego prakasa
Laporan ai modul 2-if b 2014-14102055-deprilana ego prakasaDeprilana Ego Prakasa
 
Laporan ai modul 3-if b 2014-14102055-deprilana ego prakasa
Laporan ai modul 3-if b 2014-14102055-deprilana ego prakasaLaporan ai modul 3-if b 2014-14102055-deprilana ego prakasa
Laporan ai modul 3-if b 2014-14102055-deprilana ego prakasaDeprilana Ego Prakasa
 
Modul2 deprilana ego-prakasa-14102055_if-b
Modul2 deprilana ego-prakasa-14102055_if-bModul2 deprilana ego-prakasa-14102055_if-b
Modul2 deprilana ego-prakasa-14102055_if-bDeprilana Ego Prakasa
 
14102055 deprilana ego prakasa-modul-4
14102055 deprilana ego prakasa-modul-414102055 deprilana ego prakasa-modul-4
14102055 deprilana ego prakasa-modul-4Deprilana Ego Prakasa
 
A new hybrid approach for solving travelling salesman problem using ordered c...
A new hybrid approach for solving travelling salesman problem using ordered c...A new hybrid approach for solving travelling salesman problem using ordered c...
A new hybrid approach for solving travelling salesman problem using ordered c...eSAT Journals
 
Robots As Cooperative Partners
Robots As Cooperative PartnersRobots As Cooperative Partners
Robots As Cooperative PartnersBurhanus Shidqy
 
The travelling salesman problem
The travelling salesman problemThe travelling salesman problem
The travelling salesman problemNayef Al Furaih
 
Jurnal kecerdasan buatan
Jurnal kecerdasan buatanJurnal kecerdasan buatan
Jurnal kecerdasan buatanNasri Nasri
 

Viewers also liked (20)

Laporan ai modul 2-if b 2014-14102055-deprilana ego prakasa
Laporan ai modul 2-if b 2014-14102055-deprilana ego prakasaLaporan ai modul 2-if b 2014-14102055-deprilana ego prakasa
Laporan ai modul 2-if b 2014-14102055-deprilana ego prakasa
 
Laporan ai modul 3-if b 2014-14102055-deprilana ego prakasa
Laporan ai modul 3-if b 2014-14102055-deprilana ego prakasaLaporan ai modul 3-if b 2014-14102055-deprilana ego prakasa
Laporan ai modul 3-if b 2014-14102055-deprilana ego prakasa
 
Modul2 deprilana ego-prakasa-14102055_if-b
Modul2 deprilana ego-prakasa-14102055_if-bModul2 deprilana ego-prakasa-14102055_if-b
Modul2 deprilana ego-prakasa-14102055_if-b
 
14102055
1410205514102055
14102055
 
14102055 deprilana ego prakasa-modul-4
14102055 deprilana ego prakasa-modul-414102055 deprilana ego prakasa-modul-4
14102055 deprilana ego prakasa-modul-4
 
Tugas 1 Kecerdasan buatan
Tugas 1 Kecerdasan buatanTugas 1 Kecerdasan buatan
Tugas 1 Kecerdasan buatan
 
A new hybrid approach for solving travelling salesman problem using ordered c...
A new hybrid approach for solving travelling salesman problem using ordered c...A new hybrid approach for solving travelling salesman problem using ordered c...
A new hybrid approach for solving travelling salesman problem using ordered c...
 
Resume praktikum 5__linked_list
Resume praktikum 5__linked_listResume praktikum 5__linked_list
Resume praktikum 5__linked_list
 
Laporan praktikum jarkom_3
Laporan praktikum jarkom_3Laporan praktikum jarkom_3
Laporan praktikum jarkom_3
 
Laporan praktikum jarkom_2
Laporan praktikum jarkom_2Laporan praktikum jarkom_2
Laporan praktikum jarkom_2
 
Laporan praktikum jarkom_4
Laporan praktikum jarkom_4Laporan praktikum jarkom_4
Laporan praktikum jarkom_4
 
Laporan praktikum multimedia_4-4
Laporan praktikum multimedia_4-4Laporan praktikum multimedia_4-4
Laporan praktikum multimedia_4-4
 
Laporan praktikum multimedia 5 5
Laporan praktikum multimedia 5 5Laporan praktikum multimedia 5 5
Laporan praktikum multimedia 5 5
 
Template pkm-kc-2015
Template pkm-kc-2015Template pkm-kc-2015
Template pkm-kc-2015
 
Laporan praktikum multimedia_3-3
Laporan praktikum multimedia_3-3Laporan praktikum multimedia_3-3
Laporan praktikum multimedia_3-3
 
14 graph2
14 graph214 graph2
14 graph2
 
Teori bahasa dan otomata 5
Teori bahasa dan otomata 5Teori bahasa dan otomata 5
Teori bahasa dan otomata 5
 
Robots As Cooperative Partners
Robots As Cooperative PartnersRobots As Cooperative Partners
Robots As Cooperative Partners
 
The travelling salesman problem
The travelling salesman problemThe travelling salesman problem
The travelling salesman problem
 
Jurnal kecerdasan buatan
Jurnal kecerdasan buatanJurnal kecerdasan buatan
Jurnal kecerdasan buatan
 

Similar to [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4

please navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdfplease navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdfaioils
 
Assignment-6 (2).docx
Assignment-6 (2).docxAssignment-6 (2).docx
Assignment-6 (2).docxkantrajnee88
 
Program To change this license header, choose License Heade.pdf
Program  To change this license header, choose License Heade.pdfProgram  To change this license header, choose License Heade.pdf
Program To change this license header, choose License Heade.pdfannaelctronics
 
golang_refcard.pdf
golang_refcard.pdfgolang_refcard.pdf
golang_refcard.pdfSpam92
 
Gradle - small introduction
Gradle - small introductionGradle - small introduction
Gradle - small introductionIgor Popov
 
Python import mechanism
Python import mechanismPython import mechanism
Python import mechanismYuki Nishiwaki
 
Drupal vs WordPress
Drupal vs WordPressDrupal vs WordPress
Drupal vs WordPressWalter Ebert
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing ScenarioTara Hardin
 
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017Codemotion
 
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongVu Huy
 
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongGrokking VN
 
F# Eye for the C# guy - Øredev 2013
F# Eye for the C# guy - Øredev 2013F# Eye for the C# guy - Øredev 2013
F# Eye for the C# guy - Øredev 2013Phillip Trelford
 
To change this license header, choose License Headers in P.docx
To change this license header, choose License Headers in P.docxTo change this license header, choose License Headers in P.docx
To change this license header, choose License Headers in P.docxadkinspaige22
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmersMark Whitaker
 
Tested on linux,ubuntu#include stdlib.h #include systypes..pdf
Tested on linux,ubuntu#include stdlib.h #include systypes..pdfTested on linux,ubuntu#include stdlib.h #include systypes..pdf
Tested on linux,ubuntu#include stdlib.h #include systypes..pdfamaresh6333
 
Tested on ubuntu,Linux#include stdio.h #include string.h.pdf
Tested on ubuntu,Linux#include stdio.h #include string.h.pdfTested on ubuntu,Linux#include stdio.h #include string.h.pdf
Tested on ubuntu,Linux#include stdio.h #include string.h.pdfaquacare2008
 

Similar to [Laporan ai kelas b] 14102055 deprilana ego prakasa_modul 4 (20)

please navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdfplease navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
 
Assignment-6 (2).docx
Assignment-6 (2).docxAssignment-6 (2).docx
Assignment-6 (2).docx
 
Npc14
Npc14Npc14
Npc14
 
Program To change this license header, choose License Heade.pdf
Program  To change this license header, choose License Heade.pdfProgram  To change this license header, choose License Heade.pdf
Program To change this license header, choose License Heade.pdf
 
golang_refcard.pdf
golang_refcard.pdfgolang_refcard.pdf
golang_refcard.pdf
 
Gradle - small introduction
Gradle - small introductionGradle - small introduction
Gradle - small introduction
 
Python import mechanism
Python import mechanismPython import mechanism
Python import mechanism
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
Drupal vs WordPress
Drupal vs WordPressDrupal vs WordPress
Drupal vs WordPress
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
 
Briefly Rust
Briefly RustBriefly Rust
Briefly Rust
 
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
 
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen Luong
 
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
 
F# Eye for the C# guy - Øredev 2013
F# Eye for the C# guy - Øredev 2013F# Eye for the C# guy - Øredev 2013
F# Eye for the C# guy - Øredev 2013
 
To change this license header, choose License Headers in P.docx
To change this license header, choose License Headers in P.docxTo change this license header, choose License Headers in P.docx
To change this license header, choose License Headers in P.docx
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Tested on linux,ubuntu#include stdlib.h #include systypes..pdf
Tested on linux,ubuntu#include stdlib.h #include systypes..pdfTested on linux,ubuntu#include stdlib.h #include systypes..pdf
Tested on linux,ubuntu#include stdlib.h #include systypes..pdf
 
Tested on ubuntu,Linux#include stdio.h #include string.h.pdf
Tested on ubuntu,Linux#include stdio.h #include string.h.pdfTested on ubuntu,Linux#include stdio.h #include string.h.pdf
Tested on ubuntu,Linux#include stdio.h #include string.h.pdf
 

More from Deprilana Ego Prakasa

More from Deprilana Ego Prakasa (14)

Dokumentasi rpl
Dokumentasi rplDokumentasi rpl
Dokumentasi rpl
 
Proposal multimedia-bab-i-ii-iii
Proposal multimedia-bab-i-ii-iiiProposal multimedia-bab-i-ii-iii
Proposal multimedia-bab-i-ii-iii
 
Tugas dsm kelompok11
Tugas dsm kelompok11Tugas dsm kelompok11
Tugas dsm kelompok11
 
Modul5 deprilana ego-prakasa-14102055_IF-B
Modul5 deprilana ego-prakasa-14102055_IF-BModul5 deprilana ego-prakasa-14102055_IF-B
Modul5 deprilana ego-prakasa-14102055_IF-B
 
Modul4 deprilana ego-prakasa-14102055_IF-B
Modul4 deprilana ego-prakasa-14102055_IF-BModul4 deprilana ego-prakasa-14102055_IF-B
Modul4 deprilana ego-prakasa-14102055_IF-B
 
Modul3 deprilana ego-prakasa-14102055_IF-b
Modul3 deprilana ego-prakasa-14102055_IF-bModul3 deprilana ego-prakasa-14102055_IF-b
Modul3 deprilana ego-prakasa-14102055_IF-b
 
Laporan praktikum jarkom
Laporan praktikum jarkomLaporan praktikum jarkom
Laporan praktikum jarkom
 
Resume praktikum 7__queue
Resume praktikum 7__queueResume praktikum 7__queue
Resume praktikum 7__queue
 
Resume praktikum 4__structure
Resume praktikum 4__structureResume praktikum 4__structure
Resume praktikum 4__structure
 
Resume praktikum 3__pointer
Resume praktikum 3__pointerResume praktikum 3__pointer
Resume praktikum 3__pointer
 
VIDEO EDITING_14102055
VIDEO EDITING_14102055VIDEO EDITING_14102055
VIDEO EDITING_14102055
 
Jurnal Article &lt;search>
Jurnal Article &lt;search>Jurnal Article &lt;search>
Jurnal Article &lt;search>
 
Resume praktikum 6 stack
Resume praktikum 6 stackResume praktikum 6 stack
Resume praktikum 6 stack
 
UAS Pemodelan basis data_Deprilana Ego Prakasa_14102055
UAS Pemodelan basis data_Deprilana Ego Prakasa_14102055UAS Pemodelan basis data_Deprilana Ego Prakasa_14102055
UAS Pemodelan basis data_Deprilana Ego Prakasa_14102055
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

[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){
  • 5. 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;
  • 6. } } 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; }
  • 7. 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){ }
  • 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); } }