LISTING PROGRAM Kode Program Algoritma Brute-Force: public class Bruteforce { List semuaNode; Node nodeTujuan, nodeAsal; public float jarakMinimum; public List hasil; public TimeSpan runningTime; public Bruteforce(List semuaNode) { this.semuaNode = semuaNode; this.jarakMinimum = float.PositiveInfinity; this.hasil = new List(); } private void rekursif_bruteforce(List sampel, List populasi){ if(sampel.Count > 0){ float jarakTemp = nodeAsal.cariJarakKe(sampel[0].getId()); for(int i = 0; i < sampel.Count - 1; i++){ jarakTemp += sampel[i].cariJarakKe(sampel[i+1].getId()); } if(jarakTemp == float.PositiveInfinity){ return; }
Universitas Sumatera Utara
if(sampel[sampel.Count-1].getId() == nodeTujuan.getId() && jarakTemp < jarakMinimum){ jarakMinimum = jarakTemp; hasil = new List(sampel); hasil.Insert(0, nodeAsal); } } List temp = new List(sampel); foreach(Node node in populasi){ if(sampel.IndexOf(node) == -1){ sampel = new List(temp);
sampel.Add(node); rekursif_bruteforce(sampel, populasi); } } } public bool cariRuteTerpendek(Node asal, Node tujuan){ var stopwatch = System.Diagnostics.Stopwatch.StartNew(); this.nodeTujuan = tujuan; this.nodeAsal = asal; List populasi = new List(semuaNode); populasi.Remove(asal); rekursif_bruteforce(new List(), populasi); stopwatch.Stop(); runningTime = stopwatch.Elapsed;
Kode Program Algoritma A*: public class AStar { class Elemen { public Node node; public Elemen dari; public float g, h; public Elemen(Node node, Node nodeTujuan){ this.node = node; this.g = 0; this.h = hitungJarakHeuristik(node, nodeTujuan); this.dari = null; } public float getF(){ return g + h; } float deg2rad(float deg){ return (float) (deg * (Math.PI / 180)); } float hitungJarakHeuristik(Node node_a, Node node_b){ var R = 6371; var dLat = deg2rad(node_b.getLat()-node_a.getLat()); var dLon = deg2rad(node_b.getLng()-node_a.getLng()); var a = Math.Sin(dLat/2) * Math.Sin(dLat/2) + Math.Cos(deg2rad(node_a.getLat())) * Math.Cos(deg2rad(node_b.getLat
Universitas Sumatera Utara
())) * Math.Sin(dLon/2) * Math.Sin(dLon/2) ; var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a)); var d = R * c; return (float) d; } } public List hasil; public TimeSpan runningTime; public float totalJarak; public AStar() { hasil = new List(); runningTime = new TimeSpan(); totalJarak = float.PositiveInfinity; } private Elemen cariElemenDenganNilaiTerkecil(List<Elemen> open){ float nilaiFTerkecil = float.PositiveInfinity; Elemen result = null; foreach(Elemen elemen in open){ if(elemen.getF() < nilaiFTerkecil){ nilaiFTerkecil = elemen.getF(); result = elemen; } } return result; } public bool cariRuteTerpendek(Node asal, Node tujuan){ var stopwatch = System.Diagnostics.Stopwatch.StartNew(); List<Elemen> open = new List<Elemen>(); List<Elemen> closed = new List<Elemen>(); Elemen elemen_mulai = new Elemen(asal, tujuan); open.Add(elemen_mulai);