Commitv5

parent 50da4d3a
...@@ -8,41 +8,6 @@ ...@@ -8,41 +8,6 @@
] ]
}, },
{ {
"cell_type": "raw",
"metadata": {},
"source": [
"Daftar isi\n",
"\n",
"1. Data Preprocessing\n",
"1.1 Data Cleaning\n",
"1.2 Data Integration\n",
"1.3 Data Transformation\n",
"\n",
"Adapun data yang di proses antara lain:\n",
" Kabupaten Dairi (kab1)\n",
" Kabupaten_Humbang_Hasundutan (kab2)\n",
" Kabupaten_karo (kab3)\n",
" Kabupaten_Samosir (kab4)\n",
" Kabupaten_Simalungun (kab5)\n",
" Kabupaten_Tapanuli_Utara (kab6)\n",
" Kabupaten_Toba_Samosir (kab7)\n",
" \n",
"2. Random Data\n",
"3. Encoding\n",
"4. Fitness Calculation\n",
"5. Prediksi Suhu\n",
"\n",
"PSO Implementation\n",
" Decoding PSO\n",
"ACO Implementation\n",
" Decoding ACO\n",
"ACO Implementation\n",
" Decoding ABC \n",
"\n",
"Evaluasi menggunakan VIKOR"
]
},
{
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": {}, "metadata": {},
...@@ -71,7 +36,7 @@ ...@@ -71,7 +36,7 @@
"#Load dataset sebelum integrasi\n", "#Load dataset sebelum integrasi\n",
"Data1 = pd.read_csv('./tri/Data Toba Samosir - Sheet3.csv')\n", "Data1 = pd.read_csv('./tri/Data Toba Samosir - Sheet3.csv')\n",
"Data1.drop(Data1.filter(regex=\"Unname\"),axis=1, inplace=True)\n", "Data1.drop(Data1.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"Data2 = pd.read_csv('./tri/Data Toba Samosir - Sheet1.csv')\n", "Data2 = pd.read_csv('./triData Toba Samosir - Sheet1.csv')\n",
"Data2.drop(Data2.filter(regex=\"Unname\"),axis=1, inplace=True)\n", "Data2.drop(Data2.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"Data3 = pd.read_csv('./tri/List_city.csv')" "Data3 = pd.read_csv('./tri/List_city.csv')"
] ]
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Analisis Masalah\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Source Code, "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import math\n",
"import random\n",
"import time\n",
"import pandas as pd\n",
"import csv\n",
"import sys\n",
"import datetime\n",
"import timeit"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"start = datetime.datetime.strptime(\"21-07-2020\", \"%d-%m-%Y\")\n",
"end = datetime.datetime.strptime(\"22-07-2020\", \"%d-%m-%Y\")\n",
"date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]\n",
"#print(len(date_generated))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"cost = 400000\n",
"Cost = int(cost)\n",
"min_cost = 399333"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"Weather = 26.897694"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"class Fitness_value:\n",
" def getting_max_distance():\n",
" max_distance = 0 \n",
" max_distance += len(date_generated) * 720\n",
" return max_distance\n",
" def getting_max_cost():\n",
" max_cost = 0\n",
" max_cost +=Cost\n",
" return max_cost"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"#Load dataset sebelum integrasi\n",
"#Datax = pd.read_csv('./Yolanda/tri/Data Toba Samosir_Sheet3.csv')\n",
"#Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"Datax = pd.read_csv('./Data Toba Samosir_Sheet3.csv')\n",
"Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"Dataz = pd.read_csv('./List_city.csv')\n",
"Dat = Datax.to_numpy()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0. , 50.9, 12.8, ..., 22.5, 14.4, 5.7],\n",
" [50.9, 0. , 59.2, ..., 32.9, 60.8, 52.1],\n",
" [12.8, 59.2, 0. , ..., 31.8, 4.5, 7.7],\n",
" ...,\n",
" [23.5, 32.9, 31.8, ..., 0. , 33.4, 24.7],\n",
" [14.4, 60.8, 4.5, ..., 33.4, 0. , 9.2],\n",
" [ 5.7, 52.1, 7.7, ..., 24.7, 9.2, 0. ]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dat"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"#id_city = list(Dataz['ID_City'])\n",
"#path = random.sample(range(len(id_city)), 7)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"#Datax = pd.read_csv('./Yolanda/tri/Data Toba Samosir_Sheet1.csv')\n",
"#Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def generate_start_nodes (individu,n_kota):\n",
" \"\"\"\n",
" :param individu:(int) jumlah semut\n",
" :param n_kota :(int) jumlah kota\n",
" :return : tabulist yang masih berisi start (dan end) node untuk semua individu semut\n",
" \"\"\"\n",
" start_nodes= []\n",
" for i in range (individu): \n",
" start_nodes.append([0 for i in range(n_kota+1)])\n",
" start_nodes[-1][0] = random.randint(0,(n_kota-1))\n",
" start_nodes[-1][-1] = start_nodes[-1][0]\n",
" return start_nodes"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def evaluate_best_city (kota_sekarang, matriks_jarak ,tau, tabulist, alpha, beta):\n",
" \"\"\"\n",
" Mengevaluasi kota terbaik yang memungkinkan berdasarkan probabilitas dan tabulist\n",
" :param kota_sekarang: (int 0..15) \n",
" :param tabulist : (int[0..15]) merupakan kota yang sudah pernah dikunjungi oleh semut\n",
" :param tau : (list) intensitas pheromon pada \n",
" :param matriks_jarak: (matriks[16][16]) matriks yang berisi jarak antar kota\n",
" :return : kota dengan probabilitas tertinggi\n",
" \"\"\"\n",
" probability = [0] * np.size(matriks_jarak[1])\n",
" allowed = []\n",
" allowed[:] = [x for x in range(np.size(matriks_jarak[1])) if x not in tabulist]\n",
" p = 0\n",
" for i in allowed:\n",
" visibility = 1/matriks_jarak[kota_sekarang][i]\n",
" p += (tau[kota_sekarang][i]**alpha)*(visibility**beta)\n",
" for i in allowed:\n",
" visibility = 1/matriks_jarak[kota_sekarang][i]\n",
" probability[i] = (tau[kota_sekarang][i]**alpha)*(visibility**beta)/p\n",
" tmp = max(probability)\n",
" return probability.index(tmp)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def update_tau(individu, tau, tabulists, L, evaporation_coeff, Q,n_kota):\n",
" \"\"\"\n",
" :param evaporation: koefisien evaporation\n",
" :param tau : matriks tau seukuran kota x kota\n",
" :param L : matriks/array[1x16] yang mengandung informasi jarak tour yang\n",
" dihasilkan oleh satu semut\n",
" :param delta_tau : matriks seukuran kota x kota\n",
" :return : tau yang baru\n",
" \"\"\"\n",
" for i in range (individu):\n",
" for j in range (n_kota):\n",
" delta_tau = Q/L[i]\n",
" tau[tabulists[i][j],tabulists[i][j+1]] = evaporation_coeff * tau[tabulists[i][j],tabulists[i][j+1]] + delta_tau\n",
" return (tau)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def hitung_jarak_1_rute(single_solution,matriks_jarak):\n",
" \"\"\"\n",
" :param single_solution: Sebuah tabulist dari seekor semut (1 solusi) [0..16]\n",
" :param matriks_jarak : matriks jarak antar node\n",
" :return : Total jarak yang dihasilkan solusi tersebut\n",
" \"\"\"\n",
" jarak = 0\n",
" for i in range (len(single_solution)-1):\n",
" jarak += matriks_jarak[single_solution[i]][single_solution[i+1]] \n",
" return (jarak)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def compute_shortest_distances(tabulists, matriks_jarak):\n",
" \"\"\"\n",
" :param tabulists: Tabulist sejumlah individu x banyak kota\n",
" :return : tuple (jarak terkecil yang di temukan (float)\n",
" rute dengan jarak terkecil (list),\n",
" list jarak (L)(list)\n",
" rata2 jarak yang ditemukan (float))\n",
" :>>>(min_dist,route,L,rerata)\n",
" \"\"\"\n",
" distances = []\n",
" distances = [0] * len(tabulists)\n",
" for i in range(0,len(tabulists)):\n",
" distances[i] = hitung_jarak_1_rute(tabulists[i],matriks_jarak)\n",
" L = distances\n",
" L = L- 0.97*min(L)\n",
" return (min(distances) , tabulists[distances.index(min(distances))] , L, np.mean(distances))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"#tabulists"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"Maximum_distance = Fitness_value.getting_max_distance()\n",
"Maximum_cost = Fitness_value.getting_max_cost()\n",
"TARGET_weather = Weather\n",
"CITY_COUNT = len(Datax)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"400000"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Maximum_cost"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"running = 1\n",
"global_distance = []\n",
"\n",
"for run in range (running):\n",
" iterasi = 300\n",
" alpha = 2 #jejak pheromon\n",
" beta = 2#visibility\n",
" individu = 40\n",
" n_kota = 7\n",
" evaporation_coeff = 0.1\n",
" Q = 1\n",
" nodelist = []\n",
"\n",
" matriks_jarak = Dat\n",
" tau = np.ones(np.shape(matriks_jarak))*0.5 #init. intensitas pheromon/jejak pada busur\n",
" delta_tau = np.zeros((n_kota,n_kota))\n",
"\n",
" shortest_distance = math.inf #infinit\n",
"\n",
" shortest_route = []\n",
" average = []\n",
" for i in range (iterasi):\n",
" tabulists = generate_start_nodes(individu,n_kota)\n",
" for j in range (1,n_kota):\n",
" for k in range (individu):\n",
" tabulists[k][j] = evaluate_best_city(tabulists[k][j-1],matriks_jarak,tau, tabulists[k],alpha,beta)\n",
" min_dist,route,L,rerata = compute_shortest_distances(tabulists,matriks_jarak)\n",
" average.append(rerata)\n",
"\n",
" if ((shortest_distance > min_dist) and (shortest_distance > Maximum_distance) and (min_cost <=Maximum_cost) and ((Weather >=18) and (Weather <=28))):\n",
" shortest_distance = min_dist\n",
" shortest_route = route\n",
"\n",
" tau = update_tau(individu, tau, tabulists, L, evaporation_coeff, Q,n_kota)\n",
"\n",
" print (\"Route : \",route)\n",
" print (shortest_distance,rerata,\",\",min_cost,\",\",Weather)\n",
" global_distance.append(shortest_distance)\n",
" print(\"\\n\")\n",
"\n",
"for shortest in global_distance:\n",
" print (shortest) \n",
"\n",
"\n",
"#print (\"Rata-rata rute terpendek yang dihasilkan: \",np.average(global_distance))\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 15, 24, 20, 31, 23, 22, 2]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"route"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def decoding_ACO():\n",
" name = 0\n",
" for i in range(len(route)):\n",
" name = route[i]\n",
" print(Dataz.iloc[name][1])\n",
" i+=1\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pakkodian\n",
"Bukit Tarabunga\n",
"Dolok Tolong\n",
"Lumban Silintong\n",
"Monumen Raja Sonakmalela\n",
"Balerong Onan Balige\n",
"Pantai Janji Maria\n",
"Pakkodian\n"
]
}
],
"source": [
"decoding_ACO()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Analisis Masalah\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Source Code, "
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import math\n",
"import random\n",
"import time\n",
"import pandas as pd\n",
"import csv\n",
"import sys\n",
"import datetime\n",
"import timeit"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
"#Load dataset sebelum integrasi\n",
"#Datax = pd.read_csv('./Yolanda/tri/Data Toba Samosir_Sheet3.csv')\n",
"#Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"#Datax = pd.read_csv('./Data Toba Samosir_Sheet3.csv')\n",
"Datax = pd.read_csv('./Data_Dianalisis.csv')\n",
"Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"#Dataz = pd.read_csv('./List_city.csv')\n",
"Dataz = pd.read_csv('./List_city_Data_Dianalisis.csv')\n",
"Dat = Datax.to_numpy()"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"start = datetime.datetime.strptime(\"21-07-2020\", \"%d-%m-%Y\")\n",
"end = datetime.datetime.strptime(\"22-07-2020\", \"%d-%m-%Y\")\n",
"date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]\n",
"#print(len(date_generated))"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
"#Dataz"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"def get_cost(Dataz,route):\n",
" Cost = 0 \n",
" id_city = Dataz['Biaya'].to_numpy()\n",
" cost_route = []\n",
" for i in range(len(id_city)): \n",
" for x in range(7):\n",
" if route[x] == id_city[i]:\n",
" cost_route.append(((Dataz.iloc[i][2])))\n",
" Cost = sum(cost_route) \n",
" return(Cost)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_cost(Dataz,route)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"cost = 400000\n",
"Cost = int(cost)\n",
"min_cost = 399333\n",
"Weather = 26.897694"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"class Fitness_value:\n",
" def getting_max_distance():\n",
" max_distance = 0 \n",
" max_distance += len(date_generated) * 720\n",
" return max_distance\n",
" def getting_max_cost():\n",
" max_cost = 0\n",
" max_cost +=Cost\n",
" return max_cost"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PSO"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"# Initialization of PSO parameters,and velocity\n",
"#Number of Agents\n",
"PARTICLE_COUNT = 40;\n",
"#Acceleration Constant\n",
"Acceleration_constant = 2; # Maximum velocity change allowed. Range: 0 >= V_MAX < CITY_COUNT\n",
"#Iterasi\n",
"MAX_EPOCHS = 2\n",
"map = [];\n",
"particles = []\n",
"Maximum_distance= Fitness_value.getting_max_distance()\n",
"CITY_COUNT = 7"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"# define particle function\n",
"class Particle:\n",
" def __init__(self):\n",
" self.mData = [0] * CITY_COUNT\n",
" self.mpBest_distance = 0.9\n",
" self.mVelocity = 0.0\n",
" def get_data(self, index): \n",
" return self.mData[index]\n",
" def set_data(self, index, value):\n",
" self.mData[index] = value\n",
" def get_pBest_distance(self):\n",
" return self.mpBest_distance\n",
" def set_pBest_distance(self, value):\n",
" self.mpBest_distance = value\n",
" def get_velocity(self):\n",
" return self.mVelocity\n",
" def set_velocity(self, velocityScore):\n",
" self.mVelocity = velocityScore "
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"class City:\n",
" def __init__(self):\n",
" self.mX = 0\n",
" self.mY = 0 \n",
"def get_total_distance(index):\n",
" particles[index].set_pBest_distance(0)\n",
" distance_route = []\n",
" distance = 0\n",
" for i in range(CITY_COUNT):\n",
" if i == CITY_COUNT - 1:\n",
" source = particles[index].get_data(CITY_COUNT -1)\n",
" target = particles[index].get_data(0)\n",
" distance_route.append(Datax.iloc[source][target])\n",
" distance = sum(distance_route)\n",
" particles[index].set_pBest_distance(distance) # Complete trip.\n",
" else:\n",
" source = particles[index].get_data(i)\n",
" target = particles[index].get_data(i + 1)\n",
" distance_route.append(Datax.iloc[source][target])\n",
" distance = sum(distance_route)\n",
" particles[index].set_pBest_distance(distance)\n",
" \n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [],
"source": [
"# mapping city \n",
"def initialize_map():\n",
" for i in range(CITY_COUNT):\n",
" newCity = City()\n",
" map.append(newCity)\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"def randomly_arrange(index = 0):\n",
" cityA = random.randrange(0, CITY_COUNT)\n",
" cityB = 0\n",
" done = False\n",
" while not done:\n",
" cityB = random.randrange(0, CITY_COUNT)\n",
" if cityB != cityA:\n",
" done = \tTrue\n",
" # swap cityA and cityB.\n",
" temp = particles[index].get_data(cityA)\n",
" particles[index].set_data(cityA, particles[index].get_data(cityB))\n",
" particles[index].set_data(cityB, temp)\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"def initialize_particles():\n",
" for i in range(PARTICLE_COUNT):\n",
" newParticle = Particle() \n",
" for j in range(CITY_COUNT):\n",
" newParticle.set_data(j, j) \n",
" particles.append(newParticle) \n",
" for j in range(10): # just any number of times to randomize them.\n",
" randomly_arrange(len(particles) - 1) \n",
" get_total_distance(len(particles) - 1)\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"def quicksort(array, left, right):\n",
" pivot = quicksort_partition(array, left, right) \n",
" if left < pivot:\n",
" quicksort(array, left, pivot - 1) \n",
" if right > pivot:\n",
" quicksort(array, pivot + 1, right) \n",
" return array"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"def quicksort_partition(numbers, left, right):\n",
" # The comparison is on each particle's pBest value.\n",
" I_hold = left\n",
" r_hold = right\n",
" pivot = numbers[left] \n",
" while left < right:\n",
" while (numbers[right].get_pBest_distance() >= pivot.get_pBest_distance()) and (left < right):\n",
" right -= 1\n",
"\n",
" if left != right:\n",
" numbers[left] = numbers[right]\n",
" left += 1\n",
" \n",
" while (numbers[left].get_pBest_distance() <= pivot.get_pBest_distance()) and (left < right):\n",
" left += 1 \n",
" if left != right:\n",
" numbers[right] = numbers[left]\n",
" right -= 1 \n",
" numbers[left] = pivot\n",
" pivot = left\n",
" left = I_hold\n",
" right = r_hold\n",
" \n",
" return pivot\n"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"def get_velocity():\n",
" worstResults_distance = 0.0\n",
" vValue_distance = 0.0 \n",
" # After sorting, worst will be last in list.\n",
" worstResults_distance = particles[PARTICLE_COUNT - 1].get_pBest_distance() \n",
" for i in range(PARTICLE_COUNT):\n",
" vValue_distance = (Acceleration_constant * particles[i].get_pBest_distance()) / worstResults_distance \n",
" if (vValue_distance > Acceleration_constant): \n",
" particles[i].set_velocity(Acceleration_constant)\n",
" elif (vValue_distance < 0.0):\n",
" particles[i].set_velocity(0.0)\n",
" else:\n",
" particles[i].set_velocity(vValue_distance) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"def copy_from_particle(source, destination):\n",
" # push destination's data points closer to source's data points.\n",
" targetA = random.randrange(0, CITY_COUNT) # source's city to target.\n",
" targetB = 0\n",
" indexA = 0\n",
" indexB = 0\n",
" tempIndex = 0\n",
" \n",
" # targetB will be source's neighbor immediately succeeding targetA (circular).\n",
" for i in range(CITY_COUNT):\n",
" if particles[source].get_data(i) == targetA:\n",
" if i == CITY_COUNT - 1:\n",
" targetB = particles[source].get_data(0) # if end of array, take from beginning.\n",
" else:\n",
" targetB = particles[source].get_data(i + 1)\n",
" \n",
" break\n",
" \n",
" # Move targetB next to targetA by switching values.\n",
" for j in range(CITY_COUNT):\n",
" if particles[destination].get_data(j) == targetA:\n",
" indexA = j\n",
" \n",
" if particles[destination].get_data(j) == targetB:\n",
" indexB = j \n",
" # get temp index succeeding indexA.\n",
" if indexA == CITY_COUNT - 1:\n",
" tempIndex = 0\n",
" else:\n",
" tempIndex = indexA + 1\n",
" \n",
" # Switch indexB value with tempIndex value.\n",
" temp = particles[destination].get_data(tempIndex)\n",
" particles[destination].set_data(tempIndex, particles[destination].get_data(indexB))\n",
" particles[destination].set_data(indexB, temp) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
" # updating particles city\n",
"def update_particles():\n",
" # Best was previously sorted to index 0, so start from the second best.\n",
" for i in range(5):\n",
" if i > 0:\n",
" # The higher the velocity score, the more changes it will need.\n",
" changes = math.floor(math.fabs(particles[i].get_velocity()))\n",
" sys.stdout.write(\"Changes for particle \" + str(i) + \": \" + str(changes) + \"\\n\")\n",
" for j in range(changes):\n",
" # 50/50 chance.\n",
" if random.random() > 0.5:\n",
" randomly_arrange(i)\n",
" \n",
" # Push it closer to it's best neighbor.\n",
" copy_from_particle(i - 1, i) \n",
" # Update pBest value.\n",
" get_total_distance(i) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"def PSO_algorithm():\n",
" epoch = 0\n",
" done = False \n",
" initialize_particles() \n",
" while not done:\n",
" # Two conditions can end this loop:\n",
" # if the maximum number of epochs allowed has been reached, or,\n",
" # if the Target value has been found.\n",
" if epoch < MAX_EPOCHS:\n",
" for i in range(5): \n",
" sys.stdout.write(\"Route: \") \n",
" for j in range(CITY_COUNT):\n",
" sys.stdout.write(str(particles[i].get_data(j)) + \", \")\n",
" get_total_distance(i) \n",
" sys.stdout.write(\"Distance: \" + str(particles[i].get_pBest_distance()) + \"\\n\") \n",
" if (particles[i].get_pBest_distance() <= Maximum_distance):\n",
" done = True \n",
" quicksort(particles, 0, len(particles) - 1)\n",
" # list has to sorted in order for get_velocity() to work.\n",
" get_velocity() \n",
" update_particles() \n",
" sys.stdout.write(\"epoch number: \" + str(epoch) + \"\\n\") \n",
" epoch += 1 \n",
" else:\n",
" done = True \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
"def print_best_solution():\n",
" if (particles[0].get_pBest_distance() <= Maximum_distance):\n",
" sys.stdout.write(\"Target reached.\\n\")\n",
" else:\n",
" sys.stdout.write(\"Target not reached.\\n\") \n",
" sys.stdout.write(\"Best Route: \")\n",
" for j in range(CITY_COUNT):\n",
" sys.stdout.write(str(particles[0].get_data(j)) + \", \") \n",
" sys.stdout.write(\"Distance: \" + str(particles[0].get_pBest_distance()) +\"\\n\")\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Route: 0, 2, 3, 5, 1, 4, 6, Distance: 331.40000000000003\n",
"Route: 1, 0, 5, 4, 6, 2, 3, Distance: 315.40000000000003\n",
"Route: 2, 4, 1, 0, 5, 6, 3, Distance: 315.5\n",
"Route: 4, 1, 2, 0, 6, 3, 5, Distance: 251.4\n",
"Route: 4, 5, 0, 6, 2, 1, 3, Distance: 251.49999999999997\n",
"Changes for particle 1: 0\n",
"Changes for particle 2: 0\n",
"Changes for particle 3: 0\n",
"Changes for particle 4: 0\n",
"epoch number: 0\n",
"Target reached.\n",
"Best Route: 2, 0, 4, 5, 6, 1, 3, Distance: 161.5\n"
]
}
],
"source": [
" if __name__ == '__main__':\n",
" initialize_map()\n",
" PSO_algorithm()\n",
" print_best_solution() "
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 0, 4, 5, 6, 1, 3]"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Decode PSO\n",
"def checking_city():\n",
" if (particles[0].get_pBest_distance() <= Maximum_distance):\n",
" Route = []\n",
" for j in range(CITY_COUNT):\n",
" Route.append(particles[0].get_data(j))\n",
" return Route\n",
"Route= checking_city()\n",
"Route"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"def decoding(Route):\n",
" id_ = 0\n",
" name = []\n",
" for i in range(len(Route)):\n",
" id_= Route[i]\n",
" print(Dataz.iloc[id_][1])\n",
" i+=1\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BUKIT travel Gibeon\n",
"Taman Eden 100 Tobasa\n",
"Pantai BUL BUL\n",
"Bukit Senyum\n",
"Water Park Tambunan\n",
"Bukit Pahoda\n",
"Pakkodian\n"
]
}
],
"source": [
"decoding(Route)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ACO"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def generate_start_nodes (individu,n_kota):\n",
" \"\"\"\n",
" :param individu:(int) jumlah semut\n",
" :param n_kota :(int) jumlah kota\n",
" :return : tabulist yang masih berisi start (dan end) node untuk semua individu semut\n",
" \"\"\"\n",
" start_nodes= []\n",
" for i in range (individu): \n",
" \n",
" start_nodes.append([0 for i in range(n_kota+1)])\n",
" start_nodes[-1][0] = random.randint(0,(n_kota-1))\n",
" start_nodes[-1][-1] = start_nodes[-1][0]\n",
" return start_nodes"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"def evaluate_best_city (kota_sekarang, matriks_jarak ,tau, tabulist, alpha, beta):\n",
" \"\"\"\n",
" Mengevaluasi kota terbaik yang memungkinkan berdasarkan probabilitas dan tabulist\n",
" :param kota_sekarang: (int 0..15) \n",
" :param tabulist : (int[0..15]) merupakan kota yang sudah pernah dikunjungi oleh semut\n",
" :param tau : (list) intensitas pheromon pada \n",
" :param matriks_jarak: (matriks[16][16]) matriks yang berisi jarak antar kota\n",
" :return : kota dengan probabilitas tertinggi\n",
" \"\"\"\n",
" probability = [0] * np.size(matriks_jarak[1])\n",
" allowed = []\n",
" allowed[:] = [x for x in range(np.size(matriks_jarak[1])) if x not in tabulist]\n",
" p = 0\n",
" for i in allowed:\n",
" visibility = 1/matriks_jarak[kota_sekarang][i]\n",
" p += (tau[kota_sekarang][i]**alpha)*(visibility**beta)\n",
" for i in allowed:\n",
" visibility = 1/matriks_jarak[kota_sekarang][i]\n",
" probability[i] = (tau[kota_sekarang][i]**alpha)*(visibility**beta)/p\n",
" tmp = max(probability)\n",
" return probability.index(tmp)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def update_tau(individu, tau, tabulists, L, evaporation_coeff, Q,n_kota):\n",
" \"\"\"\n",
" :param evaporation: koefisien evaporation\n",
" :param tau : matriks tau seukuran kota x kota\n",
" :param L : matriks/array[1x16] yang mengandung informasi jarak tour yang\n",
" dihasilkan oleh satu semut\n",
" :param delta_tau : matriks seukuran kota x kota\n",
" :return : tau yang baru\n",
" \"\"\"\n",
" for i in range (individu):\n",
" for j in range (n_kota):\n",
" delta_tau = Q/L[i]\n",
" tau[tabulists[i][j],tabulists[i][j+1]] = evaporation_coeff * tau[tabulists[i][j],tabulists[i][j+1]] + delta_tau\n",
" return (tau)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"def hitung_jarak_1_rute(single_solution,matriks_jarak):\n",
" \"\"\"\n",
" :param single_solution: Sebuah tabulist dari seekor semut (1 solusi) [0..16]\n",
" :param matriks_jarak : matriks jarak antar node\n",
" :return : Total jarak yang dihasilkan solusi tersebut\n",
" \"\"\"\n",
" jarak = 0\n",
" for i in range (len(single_solution)-1):\n",
" jarak += matriks_jarak[single_solution[i]][single_solution[i+1]] \n",
" return (jarak)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"def compute_shortest_distances(tabulists, matriks_jarak):\n",
" \"\"\"\n",
" :param tabulists: Tabulist sejumlah individu x banyak kota\n",
" :return : tuple (jarak terkecil yang di temukan (float)\n",
" rute dengan jarak terkecil (list),\n",
" list jarak (L)(list)\n",
" rata2 jarak yang ditemukan (float))\n",
" :>>>(min_dist,route,L,rerata)\n",
" \"\"\"\n",
" distances = []\n",
" distances = [0] * len(tabulists)\n",
" for i in range(0,len(tabulists)):\n",
" distances[i] = hitung_jarak_1_rute(tabulists[i],matriks_jarak)\n",
" L = distances\n",
" L = L- 0.97*min(L)\n",
" return (min(distances) , tabulists[distances.index(min(distances))] , L, np.mean(distances))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"#tabulists"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"Maximum_distance = Fitness_value.getting_max_distance()\n",
"Maximum_cost = Fitness_value.getting_max_cost()\n",
"TARGET_weather = Weather\n",
"CITY_COUNT = len(Datax)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"400000"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Maximum_cost"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Route : [0, 6, 7, 4, 5, 2, 3, 0]\n",
"149.60000000000002 , 399333 , 26.897694\n",
"\n",
"\n",
"Route : [0, 6, 7, 4, 5, 2, 3, 0]\n",
"149.60000000000002 , 399333 , 26.897694\n",
"\n",
"\n",
"Route : [0, 6, 7, 4, 5, 2, 3, 0]\n",
"149.60000000000002 , 399333 , 26.897694\n",
"\n",
"\n",
"Route : [0, 6, 7, 4, 5, 2, 3, 0]\n",
"149.60000000000002 , 399333 , 26.897694\n",
"\n",
"\n",
"Route : [0, 6, 7, 5, 2, 4, 3, 0]\n",
"163.5 , 399333 , 26.897694\n",
"\n",
"\n"
]
}
],
"source": [
"running = 5\n",
"global_distance = []\n",
"\n",
"for run in range (running):\n",
" iterasi = 50\n",
" alpha = 2 #jejak pheromon\n",
" beta = 2#visibility\n",
" individu = 40\n",
" n_kota = 7\n",
" evaporation_coeff = 0.1\n",
" Q = 1\n",
" nodelist = []\n",
"\n",
" matriks_jarak = Dat\n",
" tau = np.ones(np.shape(matriks_jarak))*0.5 #init. intensitas pheromon/jejak pada busur\n",
" delta_tau = np.zeros((n_kota,n_kota))\n",
"\n",
" shortest_distance = math.inf #infinit\n",
"\n",
" shortest_route = []\n",
" average = []\n",
" for i in range (iterasi):\n",
" tabulists = generate_start_nodes(individu,n_kota)\n",
" for j in range (1,n_kota):\n",
" for k in range (individu):\n",
" tabulists[k][j] = evaluate_best_city(tabulists[k][j-1],matriks_jarak,tau, tabulists[k],alpha,beta)\n",
" min_dist,route,L,rerata = compute_shortest_distances(tabulists,matriks_jarak)\n",
" average.append(rerata)\n",
"\n",
" if ((shortest_distance > min_dist) and (shortest_distance > Maximum_distance) and (min_cost <=Maximum_cost) and ((Weather >=18) and (Weather <=28))):\n",
" shortest_distance = min_dist\n",
" shortest_route = route\n",
"\n",
" tau = update_tau(individu, tau, tabulists, L, evaporation_coeff, Q,n_kota)\n",
"\n",
" print (\"Route : \",route)\n",
" print (min_dist,\",\",min_cost,\",\",Weather)\n",
" global_distance.append(shortest_distance)\n",
" print(\"\\n\")\n",
"\n",
"#for shortest in global_distance:\n",
"# print (shortest) \n",
"\n",
"\n",
"#print (\"Rata-rata rute terpendek yang dihasilkan: \",np.average(global_distance))\n"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 6, 7, 5, 2, 4, 3, 0]"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"route"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"#route = []"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"def decoding_ACO():\n",
" name = 0\n",
" for i in range(len(route)):\n",
" name = route[i]\n",
" print(Dataz.iloc[name][1])\n",
" i+=1\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pantai BUL BUL\n",
"Bukit Senyum\n",
"Air Terjun Pandumaan\n",
"Bukit Pahoda\n",
"Pakkodian\n",
"Water Park Tambunan\n",
"Taman Eden 100 Tobasa\n",
"Pantai BUL BUL\n"
]
}
],
"source": [
"decoding_ACO()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
,Pantai BUL BUL,BUKIT travel Gibeon,Pakkodian,Taman Eden 100 Tobasa,Water Park Tambunan,Bukit Pahoda,Bukit Senyum,Air Terjun Pandumaan,Long Beach,PANTAI AGADON,Situmurun Waterfall,Simanimbo Waterfall,Air Terjun Sigura Gura,The Kaldera,Museum T. B. Silalahi Center,Bukit Tarabunga,Pantai Meat,Air Terjun Sialogo,Air Terjun Siboruon,Gunung Pangulubao,Lumban Silintong,Siregar Aek Nalas,Pantai Janji Maria,Balerong Onan Balige,Dolok Tolong,Pantai Pasir Putih Parparean,Dolok Surungan,Air Terjun Sampuran,Air Terjun Morena,Tornagodang,Hatulian Beach,Monumen Raja Sonakmalela,Lumban Binanga Beach,Pantai Pasifik Porsea,Desa Adat Ragi Hotang,Makam Raja Sisingamangaraja XII
Pantai BUL BUL,0,50.9,12.8,44.7,5.9,71,60.6,12.4,63.5,50.5,52.7,43.1,51.9,56.3,6,11.8,14.6,60.7,9,42.4,6.8,36.3,4.4,3.4,9.8,21,74.2,37.1,64.5,36.1,11.6,3.3,10.7,22.5,14.4,5.7
BUKIT travel Gibeon,50.9,0,59.2,6.2,47.3,53.5,12,51.4,17.6,18.6,20.7,50.3,59,7.7,52.4,58.2,61,67.9,55.5,8.7,53.2,45.7,47.9,49.8,56.2,32.2,102,83.5,71.7,63.9,46.1,49.7,45.2,32.9,60.8,52.1
Pakkodian,12.8,59.2,0,53,14.2,11.9,68.9,20.7,71.8,58.8,61,51.4,60.2,64.6,8,4.1,4.8,69,15.4,50.7,8.7,44.6,12.7,9.4,4.5,29.3,82.5,30.4,72.8,44.4,19.9,9.5,18.9,31.8,4.5,7.7
Taman Eden 100 Tobasa,44.7,6.2,53,0,41.1,47.2,15.9,45.5,18.8,12.3,14.5,44,52.8,11.6,46.2,52,54.8,61.7,49.2,2.4,47,39.5,41.7,43.6,50,26,95.8,77.2,65.5,57.7,39.9,43.5,39,26.7,54.5,45.9
Water Park Tambunan,5.9,47.3,14.2,41.1,0,8.4,57,13.8,59.8,46.9,49,39.4,48.2,52.7,7.4,13.1,16,57,10.4,38.8,8.1,32.7,2.9,4.8,11.2,17.4,70.5,38.4,60.9,32.4,7.9,4.7,7,19.9,15.7,7.1
Bukit Pahoda,7.1,53.5,11.1,47.2,8.4,0,63.2,14.9,66,53.1,55.3,45.6,54.4,58.9,3.5,10.9,13,63.3,9.6,45,2.8,38.9,7,3.7,8.9,23.6,76.7,36.2,67.1,38.6,14.1,3.8,13.2,26.1,13.5,4
Bukit Senyum,60.6,12,68.9,15.9,57,63.1,0,61.4,5.5,28.2,30.4,59.9,68.7,10.8,62.1,67.9,70.7,77.6,65.1,18.3,62.9,55.4,57.6,59.5,65.9,41.9,112,93.1,81.4,73.6,55.8,59.4,54.9,42.6,70.4,61.8
Air Terjun Pandumaan,12.4,51.7,20.7,45.5,11.3,14.9,61.4,0,64.2,51.3,53.5,43.8,52.6,57.1,13.9,19.6,22.9,61.5,15,43.2,14.6,37.1,11.9,11.3,17.7,21.8,74.9,44.9,65.3,36.8,12.8,11.2,11.9,24.3,22.2,13.6
Long Beach,62.5,16.6,71.8,17.8,58.8,66,5.5,63.3,0,31.1,33.2,61.8,70.6,23,64,69.7,73.6,80.4,67,21.2,64.7,58.2,59.5,62.4,67.8,43.8,114,95,83.3,75.5,57.7,61.3,56.8,44.5,72.3,64.6
PANTAI AGADON,50.5,18.6,58.8,12.3,46.9,53.1,28.2,51.3,31.1,0,5.7,49.9,58.6,23.9,52,57.8,60.7,67.5,55.1,10,52.8,23.4,47.5,49.4,55.8,31.9,102,83.1,71.3,63.5,45.7,49.3,44.8,19.8,60.4,51.7
Situmurun Waterfall,52.7,20.7,61,14.5,49,55.2,30.4,53.5,33.2,5.7,0,52,60.8,26.1,54.2,59.9,62.8,69.6,57.2,12.2,54.9,47.5,49.7,51.6,58,34,104,85.2,73.5,65.7,47.9,51.5,47,34.7,62.5,53.8
Simanimbo Waterfall,43.1,50.3,51.4,44,39.4,45.6,59.9,43.8,62.8,49.9,52,0,13.6,55.6,44.6,50.3,53.2,22.4,47.6,41.7,45.6,37.9,40.1,42,48.4,24.4,94.2,75.6,26.2,56.1,38.2,41.8,37.3,25.1,52.9,44.2
Air Terjun Sigura Gura,51.9,59,60.2,52.8,48.2,54.4,68.7,52.8,71.6,58.6,60.8,13.6,0,64.4,53.4,59.1,62,11.6,56.4,50.5,54.1,46.6,48.9,50.8,57.2,33.2,103,84.4,15.5,64.9,47,50.6,46.1,33.8,61.7,53
The Kaldera,56.3,7.7,64.6,11.6,52.7,58.8,10.8,57.1,23,23.9,26.1,55.6,64.4,0,57.8,63.6,66.4,73.3,60.8,14,58.6,51.1,53.3,55.2,61.6,37.6,107,88.8,77.1,69.3,51.5,55.1,50.6,38.3,66.1,57.5
Museum T. B. Silalahi Center,6,52.5,8,46.2,7.4,3.3,62.1,13.9,65,52,54.1,44.6,53.4,57.8,0,7,9.8,62.1,8.6,43.9,1.9,37.8,5.9,2.6,5,22.6,75.7,32.3,66,37.6,13.1,2.8,12.2,25.1,9.6,0.35
Bukit Tarabunga,11.8,58.2,4.1,52,13.2,10.9,67.9,19.6,70.7,57.8,59.9,50.3,59.1,63.6,7,0,5.9,67.9,14.4,49.6,7.6,43.6,11.7,8.4,3.5,28.3,81.4,29.3,71.8,43.3,18.8,8.5,17.9,30.8,5.6,6.6
Pantai Meat,14.6,61,4.7,54.8,16,13.7,70.7,22.5,73.6,60.6,62.8,53.2,62,66.4,9.9,5.9,0,70.8,17.2,52.5,10.5,46.4,14.5,11.2,6.4,31.2,84.3,32.1,74.6,46.2,21.7,11.4,20.8,33.7,0.6,9.5
Air Terjun Sialogo,60.7,67.9,74.2,61.7,57,68.5,82.8,61.5,80.4,72.7,74.9,22.4,11.6,78.5,62.2,67.9,76.1,0,65.2,64.6,62.9,60.7,57.7,64.8,66,42,112,93.2,9.7,73.7,55.9,59.5,55,42.7,70.5,67.1
Air Terjun Siboruon,9,55.5,15.4,49.2,10.4,9.6,65.1,15,68,55.1,57.2,47.6,56.4,60.8,8.6,14.3,17.2,65.2,0,46.9,9.3,40.8,8.9,6,12.4,25.6,78.7,39.6,69,40.6,16.1,5.9,15.2,28.1,16.9,8.3
Gunung Pangulubao,42.4,8.7,50.7,2.4,38.8,44.9,18.3,43.2,21.2,10,12.2,41.7,50.5,14,43.9,49.6,52.5,59.3,46.9,0,44.6,37.2,39.4,41,47.7,23.7,93.5,74.9,63.2,55.4,37.6,41.2,36.7,24.4,52.2,43.6
Lumban Silintong,6.7,53.1,8.7,46.9,8.1,2.8,62.9,14.6,65.7,52.8,54.9,45.3,47.9,58.5,1.9,7.6,10.5,62.9,9.3,44.6,0,38.6,6.6,3.4,5.7,23.3,76.4,32.9,66.7,38.3,13.8,3.4,12.9,25.7,10.2,2
Siregar Aek Nalas,36.3,45.7,44.6,39.5,32.7,38.9,55.4,37.1,58.2,23.4,47.5,37.9,40.4,51.1,37.8,43.6,46.4,55.5,40.8,37.2,38.5,0,33.3,35.2,41.6,17.6,87.4,68.9,59.3,49.3,32.5,35.1,30.6,14.7,46.2,37.5
Pantai Janji Maria,4.4,47.9,12.7,41.7,2.9,7,57.6,11.9,60.5,47.5,49.5,40.1,42.7,53.3,5.9,11.7,14.5,57.7,8.9,39.4,6.7,33.3,0,3.3,9.7,18,71.2,37,61.5,33.1,8.6,3.2,7.6,20.5,14.3,5.6
Balerong Onan Balige,3.4,49.8,9.4,43.6,4.8,3.6,59.5,11.3,62.4,49.4,51.6,42,44.6,55.2,2.6,8.4,11.2,59.6,6,41.3,3.4,35.2,3.3,0,6.4,19.9,73.1,33.6,63.4,35,10.5,0.13,9.5,22.4,5.6,2.3
Dolok Tolong,9.8,56.2,4.1,50,11.2,8.9,65.9,17.7,68.8,55.8,58,48.4,51,61.6,5,3.5,5.9,66,12.4,47.7,5.7,41.6,9.7,6.4,0,26.4,79.5,27.4,69.8,41.4,16.9,6.6,16,28.9,5.6,4.7
Pantai Pasir Putih Parparean,21,32.2,29.3,26,17.4,23.6,41.9,21.8,44.8,31.9,34,24.4,27,37.6,22.6,28.3,31.2,42,25.6,23.7,23.3,17.6,18,19.9,26.3,0,72.1,53.6,45.8,34,16.2,19.8,15.3,4.9,5.6,22.2
Dolok Surungan,74.2,102,82.5,95.8,70.5,76.7,112,74.9,115,102,104,94.2,96.8,107,75.7,81.4,84.3,112,78.7,93.5,76.4,87.4,71.2,73.1,79.5,72.1,0,107,118,36.5,69.3,72.9,68.4,74.6,84,75.3
Air Terjun Sampuran,51.5,83.5,59.8,78.1,47.8,36.2,93.1,52.3,96.8,83.1,60.4,76.4,78.2,89.7,53,29.3,33,93.2,40.5,74.9,33.8,68.9,48.5,34.5,28.2,32.8,103,0,15.1,69.4,46.7,34.6,45.7,56.9,61.3,31.9
Air Terjun Morena,64.5,71.7,72.8,65.5,60.9,67.1,81.4,65.3,84.2,71.3,73.5,26.2,18.9,77.1,66,71.8,74.6,9.7,69,63.2,66.8,59.3,61.5,63.4,69.8,45.8,116,97.1,0,77.5,59.7,63.3,58.8,46.5,74.3,65.7
Tornagodang,36.1,63.9,44.3,57.7,32.4,38.6,73.6,36.8,76.5,63.5,65.7,56.1,58.7,69.3,37.6,43.3,46.2,73.7,40.6,55.4,38.3,49.3,33.1,35,41.4,34,36.5,68.6,77.5,0,31.2,34.8,30.3,36.5,45.9,37.2
Hatulian Beach,11.9,46.1,20.2,39.9,8.3,14.1,55.8,12.7,58.7,45.7,47.9,38.3,40.8,51.5,13.4,18.8,22,55.9,16.4,37.6,14.2,31.5,8.9,10.8,17.2,16.2,69.4,44.4,59.7,31.3,0,10.7,0.9,18.7,21.7,12.7
Monumen Raja Sonakmalela,3.3,49.7,9.5,43.5,4.7,3.8,59.4,11.2,62.2,49.3,51.5,41.8,44.4,55.1,2.8,8.5,11.3,59.5,5.9,41.2,3.5,35.1,3.2,0.13,6.5,19.8,72.9,33.8,63.3,34.8,10.3,0,9.4,22.3,11.1,2.4
Lumban Binanga Beach,11.2,45.2,19.4,39.1,7.5,13.2,54.9,11.9,57.9,44.8,47.1,37.5,39.9,50.7,12.7,17.9,21.3,55,15.7,36.7,13.4,30.6,8.1,10,16.5,15.5,68.6,43.7,59,30.5,0.9,9.9,0,18,21,11.8
Pantai Pasifik Porsea,23.5,32.9,31.8,26.7,19.9,26.1,42.6,24.3,45.5,19.8,34.7,25.1,27.6,38.3,25.1,30.8,33.6,42.7,28.1,24.4,25.8,14.7,20.5,22.4,28.8,4.9,74.6,56.1,46.5,36.5,18.7,22.3,17.8,0,33.4,24.7
Desa Adat Ragi Hotang,14.4,60.8,4.5,54.6,15.7,13.5,70.4,22.2,73.3,60.4,62.5,52.9,55.5,66.1,9.6,5.6,0.6,70.5,16.9,52.2,10.2,46.2,14.3,10.9,6.1,30.9,84,31.9,74.4,45.9,21.4,11.1,20.5,33.4,0,9.2
Makam Raja Sisingamangaraja XII,5.7,52.1,7.7,45.9,7.1,3.6,61.8,13.6,64.6,51.7,53.8,44.2,46.8,57.5,0.35,6.6,9.5,61.8,8.3,43.6,2,37.5,5.6,2.3,4.7,22.2,75.3,31.9,65.7,37.2,12.7,2.4,11.8,24.7,9.2,0
\ No newline at end of file
,Water Park Tambunan,Bukit Senyum,Air Terjun Pandumaan,Air Terjun Sigura Gura,Air Terjun Siboruon,Balerong Onan Balige,Makam Raja Sisingamangaraja XII
Water Park Tambunan,0,57,13.8,48.2,10.4,4.8,7.1
Bukit Senyum,57,0,61.4,68.7,65.1,59.5,61.8
Air Terjun Pandumaan,11.3,61.4,0,52.6,15,11.3,13.6
Air Terjun Sigura Gura,48.2,68.7,52.8,0,56.4,50.8,53
Air Terjun Siboruon,10.4,65.1,15,56.4,0,6,8.3
Balerong Onan Balige,4.8,59.5,11.3,44.6,6,0,2.3
Makam Raja Sisingamangaraja XII,7.1,61.8,13.6,46.8,8.3,2.3,0
ID_City,City ID_City,City,Biaya
0,Pantai BUL BUL 0,Pantai BUL BUL,10000
1,BUKIT travel Gibeon 1,BUKIT travel Gibeon,10001
2,Pakkodian 2,Pakkodian,10002
3,Taman Eden 100 Tobasa 3,Taman Eden 100 Tobasa,10003
4,Water Park Tambunan 4,Water Park Tambunan,10004
5,Bukit Pahoda 5,Bukit Pahoda,10005
6,Bukit Senyum 6,Bukit Senyum,10006
7,Air Terjun Pandumaan 7,Air Terjun Pandumaan,10007
8,Long Beach 8,Long Beach,10008
9,PANTAI AGADON 9,PANTAI AGADON,10009
10,Situmurun Waterfall 10,Situmurun Waterfall,10010
11,Simanimbo Waterfall 11,Simanimbo Waterfall,10011
12,Air Terjun Sigura Gura 12,Air Terjun Sigura Gura,10012
13,The Kaldera 13,The Kaldera,10013
14,Museum T. B. Silalahi Center 14,Museum T. B. Silalahi Center,10014
15,Bukit Tarabunga 15,Bukit Tarabunga,10015
16,Pantai Meat 16,Pantai Meat,10016
17,Air Terjun Sialogo 17,Air Terjun Sialogo,10017
18,Air Terjun Siboruon 18,Air Terjun Siboruon,10018
19,Gunung Pangulubao 19,Gunung Pangulubao,10019
20,Lumban Silintong 20,Lumban Silintong,10020
21,Siregar Aek Nalas 21,Siregar Aek Nalas,10021
22,Pantai Janji Maria 22,Pantai Janji Maria,10022
23,Balerong Onan Balige 23,Balerong Onan Balige,10023
24,Dolok Tolong 24,Dolok Tolong,10024
25,Pantai Pasir Putih Parparean 25,Pantai Pasir Putih Parparean,10025
26,Dolok Surungan 26,Dolok Surungan,10026
27,Air Terjun Sampuran 27,Air Terjun Sampuran,10027
28,Air Terjun Morena 28,Air Terjun Morena,10028
29,Tornagodang 29,Tornagodang,10029
30,Hatulian Beach 30,Hatulian Beach,10030
31,Monumen Raja Sonakmalela 31,Monumen Raja Sonakmalela,10031
32,Lumban Binanga Beach 32,Lumban Binanga Beach,10032
33,Pantai Pasifik Porsea 33,Pantai Pasifik Porsea,10033
34,Desa Adat Ragi Hotang 34,Desa Adat Ragi Hotang,10034
35,Makam Raja Sisingamangaraja XII 35,Makam Raja Sisingamangaraja XII,10035
ID_City,City,Biaya
4,Water Park Tambunan,10000
6,Bukit Senyum,10000
7,Air Terjun Pandumaan,10000
12,Air Terjun Sigura Gura,10000
18,Air Terjun Siboruon,10000
23,Balerong Onan Balige,10000
35,Makam Raja Sisingamangaraja XII,10000
{ {
"cells": [ "cells": [
{ {
"cell_type": "markdown",
"metadata": {},
"source": [
"# Analisis Masalah\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Source Code, "
]
},
{
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": {}, "metadata": {},
...@@ -38,10 +24,15 @@ ...@@ -38,10 +24,15 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"start = datetime.datetime.strptime(\"21-07-2020\", \"%d-%m-%Y\")\n", "#Load dataset sebelum integrasi\n",
"end = datetime.datetime.strptime(\"22-07-2020\", \"%d-%m-%Y\")\n", "#Datax = pd.read_csv('./Yolanda/tri/Data Toba Samosir_Sheet3.csv')\n",
"date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]\n", "#Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"#print(len(date_generated))" "Datax = pd.read_csv('./Data Toba Samosir_Sheet1.csv')\n",
"#Datax = pd.read_csv('./Data_Dianalisis.csv')\n",
"Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"Dataz = pd.read_csv('./List_city.csv')\n",
"#Dataz = pd.read_csv('./List_city_Data_Dianalisis.csv')\n",
"Dat = Datax.to_numpy()"
] ]
}, },
{ {
...@@ -50,9 +41,10 @@ ...@@ -50,9 +41,10 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"cost = 400000\n", "start = datetime.datetime.strptime(\"21-07-2020\", \"%d-%m-%Y\")\n",
"Cost = int(cost)\n", "end = datetime.datetime.strptime(\"22-07-2020\", \"%d-%m-%Y\")\n",
"min_cost = 399333" "date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]\n",
"#print(len(date_generated))"
] ]
}, },
{ {
...@@ -61,6 +53,9 @@ ...@@ -61,6 +53,9 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"cost = 400000\n",
"Cost = int(cost)\n",
"#min_cost = 399333\n",
"Weather = 26.897694" "Weather = 26.897694"
] ]
}, },
...@@ -82,69 +77,447 @@ ...@@ -82,69 +77,447 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {},
"source": [
"# PSO"
]
},
{
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"#Load dataset sebelum integrasi\n", "# Initialization of PSO parameters,and velocity\n",
"#Datax = pd.read_csv('./Yolanda/tri/Data Toba Samosir_Sheet3.csv')\n", "#Number of Agents\n",
"#Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)\n", "PARTICLE_COUNT = 40;\n",
"Datax = pd.read_csv('./Data Toba Samosir_Sheet3.csv')\n", "#Acceleration Constant\n",
"Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)\n", "Acceleration_constant = 2; # Maximum velocity change allowed. Range: 0 >= V_MAX < CITY_COUNT\n",
"Dataz = pd.read_csv('./List_city.csv')\n", "#Iterasi\n",
"Dat = Datax.to_numpy()" "MAX_EPOCHS = 2\n",
"map = [];\n",
"particles = []\n",
"Maximum_distance= Fitness_value.getting_max_distance()\n",
"CITY_COUNT = 7"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [],
"source": [
"# define particle function\n",
"class Particle:\n",
" def __init__(self):\n",
" self.mData = [0] * CITY_COUNT\n",
" self.mpBest_distance = 0.9\n",
" self.mVelocity = 0.0\n",
" def get_data(self, index): \n",
" return self.mData[index]\n",
" def set_data(self, index, value):\n",
" self.mData[index] = value\n",
" def get_pBest_distance(self):\n",
" return self.mpBest_distance\n",
" def set_pBest_distance(self, value):\n",
" self.mpBest_distance = value\n",
" def get_velocity(self):\n",
" return self.mVelocity\n",
" def set_velocity(self, velocityScore):\n",
" self.mVelocity = velocityScore "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"class City:\n",
" def __init__(self):\n",
" self.mX = 0\n",
" self.mY = 0 \n",
"def get_total_distance(index):\n",
" particles[index].set_pBest_distance(0)\n",
" distance_route = []\n",
" distance = 0\n",
" for i in range(CITY_COUNT):\n",
" if i == CITY_COUNT - 1:\n",
" source = particles[index].get_data(CITY_COUNT -1)\n",
" target = particles[index].get_data(0)\n",
" distance_route.append(Datax.iloc[source][target])\n",
" distance = sum(distance_route)\n",
" particles[index].set_pBest_distance(distance) # Complete trip.\n",
" else:\n",
" source = particles[index].get_data(i)\n",
" target = particles[index].get_data(i + 1)\n",
" distance_route.append(Datax.iloc[source][target])\n",
" distance = sum(distance_route)\n",
" particles[index].set_pBest_distance(distance)\n",
" \n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# mapping city \n",
"def initialize_map():\n",
" for i in range(CITY_COUNT):\n",
" newCity = City()\n",
" map.append(newCity)\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def randomly_arrange(index = 0):\n",
" cityA = random.randrange(0, CITY_COUNT)\n",
" cityB = 0\n",
" done = False\n",
" while not done:\n",
" cityB = random.randrange(0, CITY_COUNT)\n",
" if cityB != cityA:\n",
" done = \tTrue\n",
" # swap cityA and cityB.\n",
" temp = particles[index].get_data(cityA)\n",
" particles[index].set_data(cityA, particles[index].get_data(cityB))\n",
" particles[index].set_data(cityB, temp)\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def initialize_particles():\n",
" for i in range(PARTICLE_COUNT):\n",
" newParticle = Particle() \n",
" for j in range(CITY_COUNT):\n",
" newParticle.set_data(j, j) \n",
" particles.append(newParticle) \n",
" for j in range(10): # just any number of times to randomize them.\n",
" randomly_arrange(len(particles) - 1) \n",
" get_total_distance(len(particles) - 1)\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def quicksort(array, left, right):\n",
" pivot = quicksort_partition(array, left, right) \n",
" if left < pivot:\n",
" quicksort(array, left, pivot - 1) \n",
" if right > pivot:\n",
" quicksort(array, pivot + 1, right) \n",
" return array"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def quicksort_partition(numbers, left, right):\n",
" # The comparison is on each particle's pBest value.\n",
" I_hold = left\n",
" r_hold = right\n",
" pivot = numbers[left] \n",
" while left < right:\n",
" while (numbers[right].get_pBest_distance() >= pivot.get_pBest_distance()) and (left < right):\n",
" right -= 1\n",
"\n",
" if left != right:\n",
" numbers[left] = numbers[right]\n",
" left += 1\n",
" \n",
" while (numbers[left].get_pBest_distance() <= pivot.get_pBest_distance()) and (left < right):\n",
" left += 1 \n",
" if left != right:\n",
" numbers[right] = numbers[left]\n",
" right -= 1 \n",
" numbers[left] = pivot\n",
" pivot = left\n",
" left = I_hold\n",
" right = r_hold\n",
" \n",
" return pivot\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def get_velocity():\n",
" worstResults_distance = 0.0\n",
" vValue_distance = 0.0 \n",
" # After sorting, worst will be last in list.\n",
" worstResults_distance = particles[PARTICLE_COUNT - 1].get_pBest_distance() \n",
" for i in range(PARTICLE_COUNT):\n",
" vValue_distance = (Acceleration_constant * particles[i].get_pBest_distance()) / worstResults_distance \n",
" if (vValue_distance > Acceleration_constant): \n",
" particles[i].set_velocity(Acceleration_constant)\n",
" elif (vValue_distance < 0.0):\n",
" particles[i].set_velocity(0.0)\n",
" else:\n",
" particles[i].set_velocity(vValue_distance) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def copy_from_particle(source, destination):\n",
" # push destination's data points closer to source's data points.\n",
" targetA = random.randrange(0, CITY_COUNT) # source's city to target.\n",
" targetB = 0\n",
" indexA = 0\n",
" indexB = 0\n",
" tempIndex = 0\n",
" \n",
" # targetB will be source's neighbor immediately succeeding targetA (circular).\n",
" for i in range(CITY_COUNT):\n",
" if particles[source].get_data(i) == targetA:\n",
" if i == CITY_COUNT - 1:\n",
" targetB = particles[source].get_data(0) # if end of array, take from beginning.\n",
" else:\n",
" targetB = particles[source].get_data(i + 1)\n",
" \n",
" break\n",
" \n",
" # Move targetB next to targetA by switching values.\n",
" for j in range(CITY_COUNT):\n",
" if particles[destination].get_data(j) == targetA:\n",
" indexA = j\n",
" \n",
" if particles[destination].get_data(j) == targetB:\n",
" indexB = j \n",
" # get temp index succeeding indexA.\n",
" if indexA == CITY_COUNT - 1:\n",
" tempIndex = 0\n",
" else:\n",
" tempIndex = indexA + 1\n",
" \n",
" # Switch indexB value with tempIndex value.\n",
" temp = particles[destination].get_data(tempIndex)\n",
" particles[destination].set_data(tempIndex, particles[destination].get_data(indexB))\n",
" particles[destination].set_data(indexB, temp) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
" # updating particles city\n",
"def update_particles():\n",
" # Best was previously sorted to index 0, so start from the second best.\n",
" for i in range(5):\n",
" if i > 0:\n",
" # The higher the velocity score, the more changes it will need.\n",
" changes = math.floor(math.fabs(particles[i].get_velocity()))\n",
" sys.stdout.write(\"Changes for particle \" + str(i) + \": \" + str(changes) + \"\\n\")\n",
" for j in range(changes):\n",
" # 50/50 chance.\n",
" if random.random() > 0.5:\n",
" randomly_arrange(i)\n",
" \n",
" # Push it closer to it's best neighbor.\n",
" copy_from_particle(i - 1, i) \n",
" # Update pBest value.\n",
" get_total_distance(i) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def PSO_algorithm():\n",
" epoch = 0\n",
" done = False \n",
" initialize_particles() \n",
" while not done:\n",
" # Two conditions can end this loop:\n",
" # if the maximum number of epochs allowed has been reached, or,\n",
" # if the Target value has been found.\n",
" if epoch < MAX_EPOCHS:\n",
" for i in range(5): \n",
" sys.stdout.write(\"Route: \") \n",
" for j in range(CITY_COUNT):\n",
" sys.stdout.write(str(particles[i].get_data(j)) + \", \")\n",
" get_total_distance(i) \n",
" sys.stdout.write(\"Distance: \" + str(particles[i].get_pBest_distance()) + \"\\n\") \n",
" if (particles[i].get_pBest_distance() <= Maximum_distance):\n",
" done = True \n",
" quicksort(particles, 0, len(particles) - 1)\n",
" # list has to sorted in order for get_velocity() to work.\n",
" get_velocity() \n",
" update_particles() \n",
" sys.stdout.write(\"epoch number: \" + str(epoch) + \"\\n\") \n",
" epoch += 1 \n",
" else:\n",
" done = True \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def print_best_solution():\n",
" if (particles[0].get_pBest_distance() <= Maximum_distance):\n",
" sys.stdout.write(\"Target reached.\\n\")\n",
" else:\n",
" sys.stdout.write(\"Target not reached.\\n\") \n",
" sys.stdout.write(\"Best Route: \")\n",
" for j in range(CITY_COUNT):\n",
" sys.stdout.write(str(particles[0].get_data(j)) + \", \") \n",
" sys.stdout.write(\"Distance: \" + str(particles[0].get_pBest_distance()) +\"\\n\")\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Route: 5, 1, 6, 4, 0, 3, 2, Distance: 238.00000000000003\n",
"Route: 1, 3, 6, 4, 5, 2, 0, Distance: 162.29999999999998\n",
"Route: 0, 2, 4, 1, 5, 3, 6, Distance: 251.5\n",
"Route: 1, 3, 5, 2, 0, 6, 4, Distance: 242.2\n",
"Route: 5, 4, 6, 1, 2, 3, 0, Distance: 305.3\n",
"Changes for particle 1: 0\n",
"Changes for particle 2: 0\n",
"Changes for particle 3: 0\n",
"Changes for particle 4: 0\n",
"epoch number: 0\n",
"Target reached.\n",
"Best Route: 4, 3, 6, 1, 5, 0, 2, Distance: 156.6\n"
]
}
],
"source": [
" if __name__ == '__main__':\n",
" initialize_map()\n",
" PSO_algorithm()\n",
" print_best_solution() "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"array([[ 0. , 50.9, 12.8, ..., 22.5, 14.4, 5.7],\n", "[4, 3, 6, 1, 5, 0, 2]"
" [50.9, 0. , 59.2, ..., 32.9, 60.8, 52.1],\n",
" [12.8, 59.2, 0. , ..., 31.8, 4.5, 7.7],\n",
" ...,\n",
" [23.5, 32.9, 31.8, ..., 0. , 33.4, 24.7],\n",
" [14.4, 60.8, 4.5, ..., 33.4, 0. , 9.2],\n",
" [ 5.7, 52.1, 7.7, ..., 24.7, 9.2, 0. ]])"
] ]
}, },
"execution_count": 7, "execution_count": 20,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"Dat" "#Decode PSO\n",
"def checking_city():\n",
" if (particles[0].get_pBest_distance() <= Maximum_distance):\n",
" Route = []\n",
" for j in range(CITY_COUNT):\n",
" Route.append(particles[0].get_data(j))\n",
" return Route\n",
"Route= checking_city()\n",
"Route"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 21,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"#id_city = list(Dataz['ID_City'])\n", "def decoding(Route):\n",
"#path = random.sample(range(len(id_city)), 7)" " id_ = 0\n",
" name = []\n",
" for i in range(len(Route)):\n",
" id_= Route[i]\n",
" print(Dataz.iloc[id_][1])\n",
" i+=1\n",
" return "
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Water Park Tambunan\n",
"Taman Eden 100 Tobasa\n",
"Bukit Senyum\n",
"BUKIT travel Gibeon\n",
"Bukit Pahoda\n",
"Pantai BUL BUL\n",
"Pakkodian\n"
]
}
],
"source": [
"decoding(Route)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [ "source": [
"#Datax = pd.read_csv('./Yolanda/tri/Data Toba Samosir_Sheet1.csv')\n", "# ACO"
"#Datax.drop(Datax.filter(regex=\"Unname\"),axis=1, inplace=True)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 23,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -156,6 +529,7 @@ ...@@ -156,6 +529,7 @@
" \"\"\"\n", " \"\"\"\n",
" start_nodes= []\n", " start_nodes= []\n",
" for i in range (individu): \n", " for i in range (individu): \n",
" \n",
" start_nodes.append([0 for i in range(n_kota+1)])\n", " start_nodes.append([0 for i in range(n_kota+1)])\n",
" start_nodes[-1][0] = random.randint(0,(n_kota-1))\n", " start_nodes[-1][0] = random.randint(0,(n_kota-1))\n",
" start_nodes[-1][-1] = start_nodes[-1][0]\n", " start_nodes[-1][-1] = start_nodes[-1][0]\n",
...@@ -164,7 +538,7 @@ ...@@ -164,7 +538,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 24,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -193,7 +567,7 @@ ...@@ -193,7 +567,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 25,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -215,7 +589,7 @@ ...@@ -215,7 +589,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 26,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -233,7 +607,7 @@ ...@@ -233,7 +607,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 27,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -257,74 +631,79 @@ ...@@ -257,74 +631,79 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 28,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"#tabulists" "def get_cost(Dataz,route):\n",
" Cost = 0 \n",
" id_city = Dataz['ID_City'].to_numpy()\n",
" cost_route = []\n",
" for i in range(len(id_city)): \n",
" for x in range(7):\n",
" if route[x] == id_city[i]:\n",
" cost_route.append(((Dataz.iloc[i][2])))\n",
" Cost = sum(cost_route) \n",
" return(Cost)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 29,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"Maximum_distance = Fitness_value.getting_max_distance()\n", "def get_cost(Dataz,route):\n",
"Maximum_cost = Fitness_value.getting_max_cost()\n", " Cost = 0 \n",
"TARGET_weather = Weather\n", " id_city = Dataz['ID_City'].to_numpy()\n",
"CITY_COUNT = len(Datax)" " cost_route = []\n",
" for i in range(len(id_city)): \n",
" for x in range(7):\n",
" if route[x] == id_city[i]:\n",
" cost_route.append(((Dataz.iloc[i][2])))\n",
" Cost = sum(cost_route) \n",
" return(Cost)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 30,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"400000"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"Maximum_cost" "Maximum_distance = Fitness_value.getting_max_distance()\n",
"Maximum_cost = Fitness_value.getting_max_cost()\n",
"TARGET_weather = Weather\n",
"CITY_COUNT = len(Datax)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": 31,
"metadata": { "metadata": {},
"scrolled": true
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Route : [4, 23, 31, 14, 32, 4]\n", "Route : [2, 5, 35, 14, 29, 18, 21, 2]\n",
"27.43 , 399333 , 26.897694\n", "179.85 , 70124 , 26.897694\n",
"\n", "\n",
"\n", "\n",
"Route : [4, 31, 23, 32, 15, 4]\n", "Route : [0, 31, 23, 19, 3, 1, 13, 0]\n",
"45.42999999999999 , 399333 , 26.897694\n", "117.33 , 70090 , 26.897694\n",
"\n", "\n",
"\n", "\n",
"Route : [2, 35, 14, 20, 16, 2]\n", "Route : [5, 20, 14, 35, 31, 23, 4, 5]\n",
"25.150000000000002 , 399333 , 26.897694\n", "20.78 , 70132 , 26.897694\n",
"\n", "\n",
"\n", "\n",
"Route : [2, 35, 23, 31, 15, 2]\n", "Route : [4, 20, 23, 31, 14, 35, 24, 4]\n",
"22.730000000000004 , 399333 , 26.897694\n", "30.68 , 70151 , 26.897694\n",
"\n", "\n",
"\n", "\n",
"Route : [2, 16, 24, 18, 7, 2]\n", "Route : [0, 35, 14, 23, 31, 4, 32, 0]\n",
"59.3 , 399333 , 26.897694\n", "31.68 , 70139 , 26.897694\n",
"\n", "\n",
"\n" "\n"
] ]
...@@ -333,13 +712,12 @@ ...@@ -333,13 +712,12 @@
"source": [ "source": [
"running = 5\n", "running = 5\n",
"global_distance = []\n", "global_distance = []\n",
"\n",
"for run in range (running):\n", "for run in range (running):\n",
" iterasi = 50\n", " iterasi = 50\n",
" alpha = 2 #jejak pheromon\n", " alpha = 2 #jejak pheromon\n",
" beta = 2#visibility\n", " beta = 2#visibility\n",
" individu = 40\n", " individu = 40\n",
" n_kota = 5\n", " n_kota = 7\n",
" evaporation_coeff = 0.1\n", " evaporation_coeff = 0.1\n",
" Q = 1\n", " Q = 1\n",
" nodelist = []\n", " nodelist = []\n",
...@@ -359,13 +737,14 @@ ...@@ -359,13 +737,14 @@
" tabulists[k][j] = evaluate_best_city(tabulists[k][j-1],matriks_jarak,tau, tabulists[k],alpha,beta)\n", " tabulists[k][j] = evaluate_best_city(tabulists[k][j-1],matriks_jarak,tau, tabulists[k],alpha,beta)\n",
" min_dist,route,L,rerata = compute_shortest_distances(tabulists,matriks_jarak)\n", " min_dist,route,L,rerata = compute_shortest_distances(tabulists,matriks_jarak)\n",
" average.append(rerata)\n", " average.append(rerata)\n",
" min_cost = get_cost(Dataz,route) \n",
"\n", "\n",
" if ((shortest_distance > min_dist) and (shortest_distance > Maximum_distance) and (min_cost <=Maximum_cost) and ((Weather >=18) and (Weather <=28))):\n", " if ((shortest_distance > min_dist) and (shortest_distance > Maximum_distance) and (min_cost <=Maximum_cost) and ((Weather >=18) and (Weather <=28))):\n",
" shortest_distance = min_dist\n", " shortest_distance = min_dist\n",
" shortest_route = route\n", " shortest_route = route\n",
"\n", "\n",
" tau = update_tau(individu, tau, tabulists, L, evaporation_coeff, Q,n_kota)\n", " tau = update_tau(individu, tau, tabulists, L, evaporation_coeff, Q,n_kota)\n",
"\n", " \n",
" print (\"Route : \",route)\n", " print (\"Route : \",route)\n",
" print (min_dist,\",\",min_cost,\",\",Weather)\n", " print (min_dist,\",\",min_cost,\",\",Weather)\n",
" global_distance.append(shortest_distance)\n", " global_distance.append(shortest_distance)\n",
...@@ -380,36 +759,7 @@ ...@@ -380,36 +759,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 16, 24, 18, 7, 2]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"route"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#route = []"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -424,19 +774,21 @@ ...@@ -424,19 +774,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 37, "execution_count": 39,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Pakkodian\n", "Pantai BUL BUL\n",
"Pantai Meat\n", "Makam Raja Sisingamangaraja XII\n",
"Dolok Tolong\n", "Museum T. B. Silalahi Center\n",
"Air Terjun Siboruon\n", "Balerong Onan Balige\n",
"Air Terjun Pandumaan\n", "Monumen Raja Sonakmalela\n",
"Pakkodian\n" "Water Park Tambunan\n",
"Lumban Binanga Beach\n",
"Pantai BUL BUL\n"
] ]
} }
], ],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment