Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.44 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import java.util.Stack;
  4.  
  5. public class AntColony {
  6.  
  7. private static Random random = new Random();
  8.  
  9. public static void main(String[] args) {
  10. // LABEL VERTEX
  11. String[][] label = new String[8][2];
  12. label[0][0] = "A";
  13. label[0][1] = "City Market Palopo";
  14. label[1][0] = "B";
  15. label[1][1] = "Toko Baru";
  16. label[2][0] = "C";
  17. label[2][1] = "Pasar Andi Tadda Palopo";
  18. label[3][0] = "D";
  19. label[3][1] = "Opsal Plaza";
  20. label[4][0] = "E";
  21. label[4][1] = "Toko Matahari";
  22. label[5][0] = "F";
  23. label[5][1] = "Pusat Niaga Palopo";
  24. label[6][0] = "G";
  25. label[6][1] = "Mega Plaza";
  26. label[7][0] = "H";
  27. label[7][1] = "Mangga Dua";
  28.  
  29. // MATRIKS ADJACENCY
  30. double[][] adjacency = {
  31. { 0, 0.5, 1.6, 1.3, 1.7, 1.7, 1.7, 2.3 },
  32. { 0.5, 0, 1.1, 0.8, 1.2, 1.2, 1.2, 2.2 },
  33. { 1.6, 1.1, 0, 0.85, 1.2, 1.2, 1.2, 2.3 },
  34. { 1.3, 0.8, 0.85, 0, 0.55, 0.55, 0.4, 1.4 },
  35. { 2.4, 1.9, 1.1, 0.55, 0, 0.11, 0.35, 1.6 },
  36. { 2.4, 1.9, 1.1, 0.55, 0.11, 0, 0.35, 1.6 },
  37. { 1.7, 1.2, 1.2, 0.4, 0.35, 0.35, 0, 1.2 },
  38. { 2.3, 2.2, 2.3, 1.4, 1.5, 1.6, 1.2, 0 } };
  39.  
  40. // INISIALISASI PARAMETER
  41. double tou_awal = 0.01;// ---->menyatakan intensitas awal pheromone (jejak semut) disetiap vertex
  42. int n = adjacency.length;// -->jumlah vertex
  43. double Q = 1;// -------------->konstanta siklus semut
  44. double alpha = 0.5;// ---------->Konstanta pengendali intensitas jejak semut
  45. double beta = 0.5;// ----------->Konstanta pengendali visibilitas
  46. double rho = 0.5;// ---------->Tetapan penguapan jejak semut (penguapan pheromone)
  47. int m = 1000;// ---------------->Banyaknya Semut
  48. int NC_MAX = 1000;// ---------->Banyaknya Siklus Semut
  49. double probBacktracking = 0.7;//>Peluang Backtracking
  50.  
  51. /**
  52. * PERSIAPAN MEMULAI SIKLUS SEMUT
  53. */
  54.  
  55. // VISIBILITAS ANTAR VERTEX (invers jarak dinotasikan dg simbol eta)
  56. double[][] eta = new double[n][n];
  57. for (int i = 0; i < eta.length; i++) {
  58. for (int j = 0; j < eta[i].length; j++) {
  59. if (adjacency[i][j] > 0) {
  60. eta[i][j] = 1.0 / adjacency[i][j];
  61. } else {
  62. eta[i][j] = 0;
  63. }
  64. }
  65. }
  66.  
  67. // INISIALISASI MATRIX TOU. mula-mulai nilainya sama untuk setiap edge
  68. // yaitu sebesar tou_awal
  69. double[][] tou = new double[n][n];
  70. for (int i = 0; i < tou.length; i++) {
  71. for (int j = 0; j < tou[i].length; j++) {
  72. if (adjacency[i][j] > 0) {
  73. tou[i][j] = tou_awal;
  74. } else {
  75. tou[i][j] = 0;
  76. }
  77. }
  78. }
  79.  
  80. //MEMPERSIAPKAN arrayList MST untuk menyimpan Spanning Tree dengan total bobot paling minimum yang didapatkan oleh semut
  81. //NILAI MST INI AKAN DIEVALUASI MELALUI OPERASI ELITISM DISETIAP AKHIR SIKLUS
  82. ArrayList<Edge>mst = null;
  83. double bestValue = Double.MAX_VALUE;
  84.  
  85. /**
  86. * MEMULAI SIKLUS SEMUT
  87. */
  88. for (int nc = 1; nc <= NC_MAX; nc++) {// melakukan looping siklus sebanyak NC_MAX
  89. //System.out.println("SIKLUS-" + nc);
  90. double[][] deltaTou = new double[n][n];
  91. double[] Lk = new double[m];
  92.  
  93. //PERSIAPKAN VARIABEL UNTUK MENYIMPAN NILAI DARI SEMUT TERBAIK
  94. ArrayList<Edge>mstSemutTerbaik = null;
  95. double valueSemutTerbaik = Double.MAX_VALUE;
  96.  
  97. // menebar semut buatan (artificial ants) ke vertex-vertex secara
  98. // random
  99. for (int semut = 0; semut < m; semut++) {
  100. //System.out.println("-->Semut-" + (1 + semut));
  101.  
  102. // menyediakan object stack untuk menyimpan vertex yang telah
  103. // dikunjungi
  104. // serta agar semut dapat bergerak mundur
  105. Stack stackVertex = new Stack();
  106.  
  107. // memilih vertex pertama untuk dikunjungi secara acak
  108. int vo = randomBetweenInteger(0, (n - 1));
  109. stackVertex.push(vo);// menambahkan vo ke stack
  110.  
  111. boolean[] block = new boolean[n];// membuat array block untuk menandai vertex yang sudah pernah dikunjungi
  112. block[vo] = true;
  113.  
  114.  
  115.  
  116. //INISIALISASI EDGE UNTUK MENGKONSTRUKSI MST
  117. ArrayList<Edge>edges = new ArrayList<Edge>();//
  118. //Lk
  119. double L_semut = 0.0;
  120.  
  121. int nVisited = 1;// baru ada satu vertex yang dikunjungi yaitu vertex awal (vo)
  122. while (!stackVertex.isEmpty() && nVisited < n) {
  123.  
  124. //MEMBANGKITKAN BILANGAN ACAK pBack UNTUK MENENTUKAN GERAKAN MAJU ATAU MUNDUR
  125. double pBack = randomBetweenDouble(0.0, 1.0);
  126.  
  127. if(pBack<probBacktracking&&stackVertex.size()>1&&stackVertex.size()<(n-1)){
  128. //MUNDUR
  129. stackVertex.pop();
  130. }else{
  131. //MAJU
  132. int vertexAsal = (int) stackVertex.peek();
  133.  
  134. // mencari kandidat Vertex tujuan dari semua vertex yang
  135. // terhubung ke vertexAsal dan belum terblock
  136. ArrayList<Double> kandidatVertexTujuan = new ArrayList<Double>();
  137. for (int j = 0; j < n; j++) {
  138. if (adjacency[vertexAsal][j] > 0 && block[j] == false) {
  139. kandidatVertexTujuan.add((double) j);
  140. }
  141. }
  142.  
  143. if (kandidatVertexTujuan.isEmpty()) {// jika kandidatVertexTujuan KOSONG/EMPTY
  144. // jika tidak ditemukan kandidatVertexTujuan maka semut
  145. // akan bergerak mundur
  146. // secara struktur data gerakan mundur ini dilakukan
  147. // melalui operasi pop() pada stack
  148. stackVertex.pop();
  149. } else {
  150. // jika ditemukan kandidatVertexTujuan
  151. // maka hitung probabilitas ke masing-masing vertex
  152. // kandidat menggunakan
  153. // formula probabilitas semut
  154. // pembilang = tou_ij_pangkat_alpha +
  155. // eta_ij_pangkat_beta
  156. ArrayList<Double> pembilang = new ArrayList<Double>();
  157. double penyebut = 0;
  158. int i = vertexAsal;
  159. for (int k = 0; k < kandidatVertexTujuan.size(); k++) {
  160. int j = (int) Math.floor(kandidatVertexTujuan.get(k));
  161. double tou_ij_pangkat_alpha = Math.pow(tou[i][j], alpha);
  162. double eta_ij_pangkat_beta = Math.pow(eta[i][j], beta);
  163. double tou_ij_pangkat_alpha_plus_eta_ij_pangkat_beta = tou_ij_pangkat_alpha + eta_ij_pangkat_beta;
  164. pembilang.add(tou_ij_pangkat_alpha_plus_eta_ij_pangkat_beta);
  165. penyebut += tou_ij_pangkat_alpha_plus_eta_ij_pangkat_beta;
  166. }
  167.  
  168. // improve menggunakan roda roulette
  169. ArrayList<Double> probabilitasKomulatif = new ArrayList<Double>();
  170. double MAX_SUM_PROB_KOMULATIF = 0;
  171.  
  172. // hitung probabilistik ke setiap vertex kandidat
  173. ArrayList<Double> p_ij = new ArrayList<Double>();
  174. for (int k = 0; k < pembilang.size(); k++) {
  175. double peluang = pembilang.get(k) / penyebut;
  176. p_ij.add(peluang);
  177. MAX_SUM_PROB_KOMULATIF += peluang;
  178. probabilitasKomulatif.add(MAX_SUM_PROB_KOMULATIF);
  179. }
  180.  
  181. // bangkitkan sebuah bilangan acak r antara
  182. // [0..MAX_SUM_PROB_KOMULATIF] untuk memilih vertex
  183. // tujuan
  184. double r = randomBetweenDouble(0, MAX_SUM_PROB_KOMULATIF);
  185. int vertexTujuan = -1;//(int) Math.floor(kandidatVertexTujuan.get(kandidatVertexTujuan.size() - 1));// -1;
  186. for (int k = 0; k < probabilitasKomulatif.size(); k++) {
  187. if (r <= probabilitasKomulatif.get(k)) {
  188. vertexTujuan = (int) Math.floor(kandidatVertexTujuan.get(k));
  189. break;
  190. }
  191. }
  192.  
  193. // semut menemuan vertex baru
  194. stackVertex.push(vertexTujuan);
  195. block[vertexTujuan] = true;
  196. double bobot = adjacency[vertexAsal][vertexTujuan];
  197. L_semut += bobot;
  198. edges.add(new Edge(vertexAsal,vertexTujuan,bobot));
  199. nVisited++;
  200. //System.out.print(vertexAsal + " - " + vertexTujuan + "; ");
  201. }
  202. }
  203. }
  204. //SEMUT TELAH BERHASIL MEMBENTUK SATU SOLUSI MST
  205. //System.out.println("L Semut: "+L_semut);
  206. Lk[semut]=L_semut;
  207. //LAKUKAN UPDATE DELTA TOU OLEH SEMUT
  208. if(!edges.isEmpty()){
  209. for(Edge edge:edges){
  210. deltaTou[edge.vertexAsal][edge.vertexTujuan] += (Q/L_semut);
  211. }
  212. }
  213.  
  214. //EVALUASI NILAI SEMUT TERBAIK
  215. if(L_semut<valueSemutTerbaik && edges!=null){
  216. valueSemutTerbaik = L_semut;
  217. mstSemutTerbaik = edges;
  218. }
  219.  
  220. }//end of for semut (masing-masing semut mencari solusi MST)
  221.  
  222. //UPDATE TOU SEBELUM LANJUT KE SIKLUS BERIKUTNYA
  223. for (int i = 0; i < tou.length; i++) {
  224. for (int j = 0; j < tou[i].length; j++) {
  225. if (adjacency[i][j] > 0 && deltaTou[i][j]>0) {
  226. tou[i][j] = rho * tou[i][j]+deltaTou[i][j];
  227. } else {
  228. tou[i][j] = 0;
  229. }
  230. }
  231. }
  232.  
  233. //OPERASI ELITISME
  234. //EVALUASI NILAI SEMUT TERBAIK
  235. if(valueSemutTerbaik<bestValue && mstSemutTerbaik!=null){
  236. bestValue = valueSemutTerbaik;
  237. mst = mstSemutTerbaik;
  238. }
  239.  
  240. }//end of siklus semut
  241.  
  242. //PRINT BEST VALUE
  243. System.out.println();
  244. System.out.println("PROSES SIKLUS ANT COLONY SELESAI...");
  245. System.out.println("BEST VALUE: "+bestValue);
  246. if(mst!=null){
  247. System.out.print("MST: ");
  248. for(Edge edge:mst){
  249. int i = edge.vertexAsal;
  250. int j = edge.vertexTujuan;
  251. //System.out.print(i+"-"+j+"; ");
  252. System.out.print(label[i][1]+" - "+label[j][1]+"; ");
  253. }
  254. }
  255.  
  256. }
  257.  
  258. private static int randomBetweenInteger(int min, int max) {
  259. if (min >= max) {
  260. throw new IllegalArgumentException("nilai variabel max harus lebih besar dari nilai variabel min");
  261. }
  262. return random.nextInt((max - min) + 1) + min;
  263. }
  264.  
  265. private static double randomBetweenDouble(double min, double max) {
  266. if (min >= max) {
  267. throw new IllegalArgumentException("nilai variabel max harus lebih besar dari nilai variabel min");
  268. }
  269. return (min + random.nextDouble() * (max - min));
  270. }
  271.  
  272. }
  273.  
  274. class Edge{
  275. int vertexAsal;
  276. int vertexTujuan;
  277. double bobot;
  278.  
  279. public Edge(int vertexAsal, int vertexTujuan) {
  280. super();
  281. this.vertexAsal = vertexAsal;
  282. this.vertexTujuan = vertexTujuan;
  283. }
  284.  
  285. public Edge(int vertexAsal, int vertexTujuan, double bobot) {
  286. super();
  287. this.vertexAsal = vertexAsal;
  288. this.vertexTujuan = vertexTujuan;
  289. this.bobot = bobot;
  290. }
  291.  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement