Commit ded360ac by Yolanda

ABC update

parent 65d056df
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 11,
"metadata": {},
"outputs": [
{
......@@ -33,7 +33,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
......@@ -57,13 +57,14 @@
},
{
"cell_type": "code",
"execution_count": 81,
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"jumlah_tempat_wisata = 7\n",
"cost = 400000\n",
"Cost = int(cost)\n",
"tgl_perjalanan = 30\n",
"#min_cost = 399333"
]
},
......@@ -72,6 +73,13 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 14,
"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",
......@@ -88,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
......@@ -112,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
......@@ -125,7 +133,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
......@@ -136,13 +144,13 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"x_train = []\n",
"y_train = []\n",
"n_future = len(date_generated) # next 5 days temperature forecast\n",
"n_future = 30 # next 5 days temperature forecast\n",
"n_past = 30 # Past 30 days \n",
"for i in range(0,len(training_set_scaled)-n_past-n_future+1):\n",
" x_train.append(training_set_scaled[i : i + n_past , 0]) \n",
......@@ -153,7 +161,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 71,
"metadata": {},
"outputs": [
{
......@@ -161,54 +169,54 @@
"output_type": "stream",
"text": [
"Epoch 1/20\n",
"91/91 [==============================] - 3s 36ms/step - loss: 0.1736 - acc: 0.0110\n",
"62/62 [==============================] - 3s 55ms/step - loss: 0.2066 - acc: 0.0806\n",
"Epoch 2/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0574 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.1949 - acc: 0.0645\n",
"Epoch 3/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0410 - acc: 0.0000e+00\n",
"62/62 [==============================] - 0s 3ms/step - loss: 0.1787 - acc: 0.1129\n",
"Epoch 4/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0481 - acc: 0.0220\n",
"62/62 [==============================] - 0s 3ms/step - loss: 0.1603 - acc: 0.0645\n",
"Epoch 5/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0250 - acc: 0.0000e+00\n",
"62/62 [==============================] - 0s 3ms/step - loss: 0.1375 - acc: 0.0968\n",
"Epoch 6/20\n",
"91/91 [==============================] - 0s 4ms/step - loss: 0.0372 - acc: 0.0110\n",
"62/62 [==============================] - 0s 3ms/step - loss: 0.1191 - acc: 0.0806\n",
"Epoch 7/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0314 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.1010 - acc: 0.0806\n",
"Epoch 8/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0264 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.0917 - acc: 0.1129\n",
"Epoch 9/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0244 - acc: 0.0000e+00\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.0840 - acc: 0.0484\n",
"Epoch 10/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0278 - acc: 0.0000e+00\n",
"62/62 [==============================] - 0s 5ms/step - loss: 0.0738 - acc: 0.0323\n",
"Epoch 11/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0228 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.0688 - acc: 0.0484\n",
"Epoch 12/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0263 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.0613 - acc: 0.0645\n",
"Epoch 13/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0264 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.0574 - acc: 0.0484\n",
"Epoch 14/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0250 - acc: 0.0110\n",
"62/62 [==============================] - 0s 3ms/step - loss: 0.0530 - acc: 0.0806\n",
"Epoch 15/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0230 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.0514 - acc: 0.0645\n",
"Epoch 16/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0269 - acc: 0.0110\n",
"62/62 [==============================] - 0s 5ms/step - loss: 0.0494 - acc: 0.0161\n",
"Epoch 17/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0247 - acc: 0.0110\n",
"62/62 [==============================] - 0s 3ms/step - loss: 0.0448 - acc: 0.0968\n",
"Epoch 18/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0263 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.0437 - acc: 0.0645\n",
"Epoch 19/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0246 - acc: 0.0110\n",
"62/62 [==============================] - 0s 4ms/step - loss: 0.0432 - acc: 0.0645\n",
"Epoch 20/20\n",
"91/91 [==============================] - 0s 3ms/step - loss: 0.0250 - acc: 0.0110\n"
"62/62 [==============================] - 0s 3ms/step - loss: 0.0417 - acc: 0.0161\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.callbacks.History at 0x1e05e61bef0>"
"<keras.callbacks.callbacks.History at 0x25c695acac8>"
]
},
"execution_count": 9,
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
......@@ -230,7 +238,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
......@@ -247,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
......@@ -258,28 +266,77 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[27.967451],\n",
" [27.607952],\n",
" [28.059902],\n",
" [27.81603 ],\n",
" [27.799353],\n",
" [27.736164],\n",
" [27.613163],\n",
" [26.997648],\n",
" [27.680696],\n",
" [27.724241],\n",
" [26.857187],\n",
" [27.729334],\n",
" [27.896103],\n",
" [27.735334],\n",
" [27.649883],\n",
" [27.663065],\n",
" [27.571318],\n",
" [27.641148],\n",
" [27.764853],\n",
" [27.439972],\n",
" [27.94146 ],\n",
" [27.124084],\n",
" [27.573236],\n",
" [27.68184 ],\n",
" [27.275497],\n",
" [27.024868],\n",
" [27.208172],\n",
" [27.361816],\n",
" [27.742584],\n",
" [27.756859]], dtype=float32)"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predicted_temperature"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"def get_weather ():\n",
" for i in range(len(predicted_temperature)):\n",
" weat= predicted_temperature[0][0]\n",
" weat= predicted_temperature[tgl_perjalanan-1][0]\n",
" return weat"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"27.432116"
"27.756859"
]
},
"execution_count": 13,
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
......@@ -291,8 +348,10 @@
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"execution_count": 24,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
......@@ -321,12 +380,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Particle Swarm Optimization (PSO) Algorithm"
"# PSO"
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
......@@ -340,12 +399,13 @@
"map = [];\n",
"particles = []\n",
"Maximum_distance= Fitness_value.getting_max_distance()\n",
"CITY_COUNT = 7"
"Maximum_cost = Fitness_value.getting_max_cost()\n",
"CITY_COUNT = jumlah_tempat_wisata"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
......@@ -363,6 +423,10 @@
" return self.mpBest_distance\n",
" def set_pBest_distance(self, value):\n",
" self.mpBest_distance = value\n",
" def get_pBest_cost(self):\n",
" return self.mpBest_cost\n",
" def set_pBest_cost(self, value):\n",
" self.mpBest_cost = value\n",
" def get_velocity(self):\n",
" return self.mVelocity\n",
" def set_velocity(self, velocityScore):\n",
......@@ -371,7 +435,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
......@@ -395,14 +459,23 @@
" 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",
" particles[index].set_pBest_distance(distance) \n",
" return\n",
"def get_total_cost(index):\n",
" particles[index].set_pBest_cost(0)\n",
" cost_route = []\n",
" cost = 0\n",
" for i in range(CITY_COUNT):\n",
" id_= particles[index].get_data(i)\n",
" cost_route.append(Dataz.iloc[id_][2])\n",
" cost = sum(cost_route)\n",
" particles[index].set_pBest_cost(cost) \n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
......@@ -416,11 +489,12 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"def randomly_arrange(index = 0):\n",
" \n",
" cityA = random.randrange(0, CITY_COUNT)\n",
" cityB = 0\n",
" done = False\n",
......@@ -437,7 +511,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
......@@ -450,12 +524,13 @@
" 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",
" get_total_cost(len(particles) - 1)\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
......@@ -470,7 +545,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
......@@ -480,42 +555,54 @@
" 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",
" while (numbers[right].get_pBest_distance() >= pivot.get_pBest_distance() and\n",
" numbers[right].get_pBest_cost() >= pivot.get_pBest_cost()) and (left < right):\n",
" right -= 1\n",
"\n",
" if left != right:\n",
" numbers[left] = numbers[right]\n",
" left += 1 \n",
" while (numbers[left].get_pBest_distance() <= pivot.get_pBest_distance()) and (left < right):\n",
" while (numbers[left].get_pBest_cost() <= pivot.get_pBest_cost()) and (left < 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",
" right -= 1\n",
" \n",
" numbers[left] = pivot\n",
" pivot = left\n",
" left = I_hold\n",
" right = r_hold\n",
" \n",
" return pivot\n"
" return pivot"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"def get_velocity():\n",
" worstResults_distance = 0.0\n",
" vValue_distance = 0.0 \n",
" #worstResults_weather = 0.0\n",
" worstResults_cost = 0.0\n",
" vValue_distance = 0.0\n",
" #vValue_weather = 0.0\n",
" vValue_cost = 0.0 \n",
" # After sorting, worst will be last in list.\n",
" worstResults_distance = particles[PARTICLE_COUNT - 1].get_pBest_distance() \n",
" worstResults_distance = particles[PARTICLE_COUNT - 1].get_pBest_distance()\n",
" worstResults_cost = particles[PARTICLE_COUNT - 1].get_pBest_cost()\n",
" #worstResults_weather = particles[PARTICLE_COUNT - 1].get_pBest_weather() \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",
" vValue_distance = (Acceleration_constant * particles[i].get_pBest_distance()) / worstResults_distance\n",
" #vValue_weather = (Acceleration_constant * particles[i].get_pBest_weather()) / worstResults_weather\n",
" vValue_cost = (Acceleration_constant* particles[i].get_pBest_cost()) / worstResults_cost \n",
" if ((vValue_distance > Acceleration_constant) and \n",
" (vValue_cost > Acceleration_constant)): \n",
" particles[i].set_velocity(Acceleration_constant)\n",
" elif (vValue_distance < 0.0):\n",
" elif ((vValue_distance < 0.0) and \n",
" (vValue_cost < 0.0)):\n",
" particles[i].set_velocity(0.0)\n",
" else:\n",
" particles[i].set_velocity(vValue_distance) \n",
......@@ -524,7 +611,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
......@@ -534,31 +621,26 @@
" targetB = 0\n",
" indexA = 0\n",
" indexB = 0\n",
" tempIndex = 0\n",
" \n",
" tempIndex = 0 \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",
" targetB = particles[source].get_data(i + 1) \n",
" break \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",
" indexA = j \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",
" tempIndex = indexA + 1 \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",
......@@ -568,7 +650,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
......@@ -583,18 +665,18 @@
" for j in range(changes):\n",
" # 50/50 chance.\n",
" if random.random() > 0.5:\n",
" randomly_arrange(i)\n",
" \n",
" randomly_arrange(i) \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",
" get_total_distance(i)\n",
" get_total_cost(i)\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
......@@ -603,17 +685,18 @@
" 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 i in range(5):\n",
" \n",
" sys.stdout.write(\"Route: \")\n",
" \n",
" for j in range(CITY_COUNT):\n",
" sys.stdout.write(str(particles[i].get_data(j)) + \", \")\n",
" get_total_distance(i) \n",
" get_total_distance(i)\n",
" get_total_cost(i) \n",
" sys.stdout.write(\"Distance: \" + str(particles[i].get_pBest_distance()) + \"\\n\") \n",
" if (particles[i].get_pBest_distance() <= Maximum_distance):\n",
" if (particles[i].get_pBest_distance() <= Maximum_distance) and (particles[i].get_pBest_cost() <= Maximum_cost) and (Weather>=20 and Weather<=28):\n",
" if (Weather >= 20) and (Weather <=28):\n",
" done = True \n",
" quicksort(particles, 0, len(particles) - 1)\n",
" # list has to sorted in order for get_velocity() to work.\n",
......@@ -622,49 +705,53 @@
" sys.stdout.write(\"epoch number: \" + str(epoch) + \"\\n\") \n",
" epoch += 1 \n",
" else:\n",
" done = True \n",
" done = True\n",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"def print_best_solution():\n",
" if (particles[0].get_pBest_distance() <= Maximum_distance):\n",
" if ((particles[0].get_pBest_distance() <= Maximum_distance) and \n",
" (particles[0].get_pBest_cost() <= Maximum_cost)):\n",
" sys.stdout.write(\"Target reached.\\n\")\n",
" else:\n",
" sys.stdout.write(\"Target not reached.\\n\") \n",
" sys.stdout.write(\"Target not reached.\\n\")\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",
" sys.stdout.write(str(particles[0].get_data(j)) + \", \")\n",
" \n",
" sys.stdout.write(\"Distance: \" + str(particles[0].get_pBest_distance()) + \" , \" \"Cost : \" + str(particles[0].get_pBest_cost()) + \" , \" \"Weather : \" + str(Weather) +\"\\n\")\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Route: 5, 1, 2, 6, 0, 3, 4, Distance: 336.40000000000003\n",
"Route: 0, 2, 6, 3, 1, 4, 5, Distance: 166.60000000000002\n",
"Route: 2, 5, 6, 3, 0, 1, 4, Distance: 248.10000000000002\n",
"Route: 0, 1, 5, 3, 6, 2, 4, Distance: 256.5\n",
"Route: 2, 0, 5, 6, 1, 3, 4, Distance: 220.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",
"Route: 1, 6, 0, 5, 3, 2, 4, Distance: 305.3\n",
"Route: 1, 6, 5, 0, 2, 3, 4, Distance: 236.39999999999998\n",
"Route: 6, 4, 5, 1, 2, 0, 3, Distance: 251.50000000000003\n",
"Route: 3, 6, 0, 5, 2, 1, 4, Distance: 306.20000000000005\n",
"Route: 5, 2, 4, 3, 6, 0, 1, Distance: 247.3\n",
"Changes for particle 1: 1\n",
"Changes for particle 2: 1\n",
"Changes for particle 3: 1\n",
"Changes for particle 4: 1\n",
"epoch number: 0\n",
"Target reached.\n",
"Best Route: 4, 2, 5, 0, 3, 1, 6, Distance: 153.10000000000002\n"
"Best Route: 1, 5, 2, 4, 0, 3, 6, Distance: 157.3 , Cost : 45000 , Weather : 27.510067\n"
]
}
],
......@@ -677,16 +764,16 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 2, 5, 0, 3, 1, 6]"
"[1, 5, 2, 4, 0, 3, 6]"
]
},
"execution_count": 29,
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
......@@ -705,7 +792,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
......@@ -721,19 +808,19 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Water Park Tambunan\n",
"Pakkodian\n",
"BUKIT travel Gibeon\n",
"Bukit Pahoda\n",
"Pakkodian\n",
"Water Park Tambunan\n",
"Pantai BUL BUL\n",
"Taman Eden 100 Tobasa\n",
"BUKIT travel Gibeon\n",
"Bukit Senyum\n"
]
}
......@@ -743,22 +830,15 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ant Colony Optimization (ACO) Algorithm"
"# ACO"
]
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
......@@ -775,7 +855,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
......@@ -798,7 +878,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
......@@ -820,7 +900,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
......@@ -838,7 +918,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
......@@ -862,7 +942,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
......@@ -880,7 +960,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
......@@ -898,7 +978,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
......@@ -910,31 +990,31 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"itinerary 1 = [0, 4, 31, 23, 22, 20, 24, 0]\n",
"36.230000000000004 , 25000 , 27.432116\n",
"Route : [0, 22, 5, 4, 34, 16, 21, 0]\n",
"118.8 , 20000 , 27.510067\n",
"\n",
"\n",
"itinerary 2 = [5, 22, 4, 14, 35, 20, 31, 5]\n",
"26.85 , 35000 , 27.432116\n",
"Route : [4, 31, 23, 22, 12, 19, 29, 4]\n",
"189.13 , 30000 , 27.510067\n",
"\n",
"\n",
"itinerary 3 = [2, 4, 23, 31, 25, 9, 32, 2]\n",
"135.03 , 15000 , 27.432116\n",
"Route : [6, 8, 24, 35, 14, 2, 15, 6]\n",
"158.35 , 25000 , 27.510067\n",
"\n",
"\n",
"itinerary 4 = [0, 31, 23, 34, 16, 24, 27, 0]\n",
"94.93 , 0 , 27.432116\n",
"Route : [0, 35, 14, 20, 23, 31, 4, 0]\n",
"22.08 , 35000 , 27.510067\n",
"\n",
"\n",
"itinerary 5 = [0, 22, 2, 31, 23, 20, 7, 0]\n",
"57.129999999999995 , 10000 , 27.432116\n",
"Route : [5, 23, 31, 33, 15, 35, 14, 5]\n",
"67.18 , 12000 , 27.510067\n",
"\n",
"\n"
]
......@@ -951,6 +1031,7 @@
" 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",
......@@ -975,18 +1056,21 @@
"\n",
" tau = update_tau(individu, tau, tabulists, L, evaporation_coeff, Q,n_kota)\n",
" \n",
" \n",
" print (\"itinerary\",run+1,\"=\",route)\n",
" print (\"Route : \",route)\n",
" print (min_dist,\",\",min_cost,\",\",Weather)\n",
" \n",
" \n",
" global_distance.append(shortest_distance)\n",
" print(\"\\n\")\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": 74,
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
......@@ -1001,7 +1085,7 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 52,
"metadata": {},
"outputs": [
{
......@@ -1009,12 +1093,12 @@
"output_type": "stream",
"text": [
"Bukit Pahoda\n",
"Situmurun Waterfall\n",
"Balerong Onan Balige\n",
"Monumen Raja Sonakmalela\n",
"Pakkodian\n",
"Dolok Tolong\n",
"Pantai Pasifik Porsea\n",
"Bukit Tarabunga\n",
"Makam Raja Sisingamangaraja XII\n",
"Museum T. B. Silalahi Center\n",
"Bukit Pahoda\n"
]
}
......@@ -1027,7 +1111,536 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Artificial Bee Colony (ABC) Algorithm"
"# ABC"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"class Bee:\n",
" def __init__(self, node_set):\n",
" self.role = ''\n",
" self.path = list(node_set) # stores all nodes in each bee, will randomize foragers\n",
" self.distance = 0\n",
" self.temperature = 0\n",
" self.cycle = 0 \n",
" self.cost = 0"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"Maximum_distance = Fitness_value.getting_max_distance()\n",
"Maximum_cost = Fitness_value.getting_max_cost()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"path = [5, 12, 23, 31, 16, 34, 24, 5]"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"def getting_distance(Data):\n",
" distance_route = []\n",
" last_distance = 0\n",
" distance = 0\n",
" for i in range(0,len(Data)-1):\n",
" source = Data[i]\n",
" target = Data[i+1]\n",
" distance_route.append(Datax.iloc[source][target])\n",
" for i in range(0,len(Data)-1):\n",
" source = Data[len(Data)-1]\n",
" target = Data[len(Data)-len(Data)]\n",
" last_distance = Datax.iloc[source][target] \n",
" distance = sum(distance_route)+last_distance\n",
" return distance\n",
"cities = list(Dataz['ID_City'])\n",
"cities = random.sample(range(len(cities)), 7)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"def get_total_cost(Dataz, cities):\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 cities[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": 57,
"metadata": {},
"outputs": [],
"source": [
"def initialize_hive(population, data):\n",
" path = cities\n",
" hive = [Bee(path) for i in range (0, population)]\n",
" return hive"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"def assign_roles(hive, role_percentiles):\n",
" forager_percent = 0.5\n",
" onlooker_percent = 0.5\n",
" role_percent = [onlooker_percent, forager_percent]\n",
" scout_percent = 0.2\n",
" population = len(hive)\n",
" onlooker_count = math.floor(population * role_percentiles[0])\n",
" forager_count = math.floor(population * role_percentiles[1])\n",
" for i in range(0, onlooker_count):\n",
" hive[i].role = 'O'\n",
" for i in range(onlooker_count, (onlooker_count + forager_count)):\n",
" hive[i].role = 'F'\n",
" random.shuffle(hive[i].path)\n",
" hive[i].distance = getting_distance(hive[i].path)\n",
" return hive"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"def mutate_path(path):\n",
" # - will go out of range if last element is chosen.\n",
" path = cities\n",
" new_path = random.sample(path,len(path))\n",
" return new_path"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"def forage(bee,limit):\n",
" new_path = mutate_path(bee.path)\n",
" new_distance = getting_distance(new_path)\n",
" if new_distance < bee.distance:\n",
" bee.path = new_path\n",
" bee.distance = new_distance\n",
" bee.cycle = 0 # reset cycle so bee can continue to make progress\n",
" else:\n",
" bee.cycle += 1\n",
" if bee.cycle >= limit: # if bee is not making progress\n",
" bee.role = 'S'\n",
" return bee.distance, list(bee.path)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"def scout(bee):\n",
" new_path = list(bee.path)\n",
" random.shuffle(new_path)\n",
" bee.path = new_path\n",
" bee.distance = getting_distance(bee.path)\n",
" bee.temperature = Weather\n",
" bee.role = 'F'\n",
" bee.cycle = 0"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"def waggle(hive, best_distance,forager_limit, scout_count):\n",
" best_path = []\n",
" results = []\n",
" for i in range(0, len(hive)):\n",
" if hive[i].role == 'F':\n",
" distance, path = forage(hive[i], forager_limit)\n",
" if distance < best_distance:\n",
" best_distance = distance\n",
" best_path = list(hive[i].path)\n",
" results.append((i, distance))\n",
"\n",
" elif hive[i].role == 'S':\n",
" scout(hive[i])\n",
" # after processing all bees, set worst performers to scout\n",
" results.sort(reverse = True, key=lambda tup: tup[1])\n",
" scouts = [ tup[0] for tup in results[0:int(scout_count)] ]\n",
" for new_scout in scouts:\n",
" hive[new_scout].role = 'S'\n",
" return best_distance, best_path"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"def recruit(hive, best_distance, best_path):\n",
" for i in range(0, len(hive)):\n",
" if hive[i].role == 'O':\n",
" new_path = mutate_path(best_path)\n",
" new_distance = getting_distance(new_path)\n",
" if new_distance < best_distance:\n",
" best_distance = new_distance\n",
" best_path = new_path\n",
" return best_distance, best_path"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"def print_details(cycle, path, distance, cost,temperature, bee):\n",
" \"\"\"\n",
" Prints cycle details to console.\n",
" \"\"\"\n",
" print(\"CYCLE: {}\".format(cycle))\n",
" print(\"PATH: {}\".format(path))\n",
" print(\"DISTANCE: {}\".format(distance))\n",
" print(\"COST: {}\".format(cost))\n",
" print(\"TEMPERATURE: {}\".format(temperature))\n",
" print(\"BEE: {}\".format(bee))\n",
" print(\"\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"def main():\n",
" # Control parameters\n",
" population =40\n",
" forager_percent = 0.5\n",
" onlooker_percent = 0.4\n",
" role_percent = [onlooker_percent, forager_percent]\n",
" scout_percent = 0.01\n",
" scout_count = math.ceil(population * scout_percent)\n",
" forager_limit = 500\n",
" cycle_limit = 100\n",
" cycle = 1\n",
" temperature = Weather\n",
" # Data source\n",
" # data = read_data_from_csv(\"data/data_10.csv\")\n",
" # data = read_data_from_csv(\"data/data_11.csv\")\n",
" data = cities\n",
" # Global vars\n",
" best_distance = sys.maxsize\n",
" best_path = []\n",
" result = ()\n",
" # Initialization\n",
" hive = initialize_hive(population, data)\n",
" assign_roles(hive, role_percent)\n",
" cost = get_total_cost(Dataz,cities)\n",
" while cycle < cycle_limit:\n",
" waggle_distance,waggle_path = waggle(hive, best_distance,forager_limit,scout_count)\n",
" if (waggle_distance < best_distance) and (waggle_distance <= Maximum_distance):\n",
" best_distance = waggle_distance\n",
" best_path = list(waggle_path)\n",
" cost = get_total_cost(Dataz,cities)\n",
" temperature = Weather\n",
" print_details(cycle, best_path, best_distance,cost,temperature,'F')\n",
" result = (cycle, best_path, best_distance,cost,temperature,'F')\n",
" recruit_distance,recruit_path = recruit(hive, best_distance,best_path)\n",
" if (recruit_distance < best_distance) and (recruit_distance <= Maximum_distance):\n",
" best_path = list(recruit_path)\n",
" best_distance = recruit_distance \n",
" cost = get_total_cost(Dataz,cities)\n",
" print_details(cycle, best_path, best_distance,cost,temperature,'R')\n",
" result = (cycle, best_path, best_distance,cost,temperature,'R')\n",
" if cycle % 100 == 0:\n",
" print(\"CYCLE #: {}\\n\".format(cycle))\n",
" cycle += 1"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CYCLE: 1\n",
"PATH: [16, 20, 7, 13, 21, 14, 35]\n",
"DISTANCE: 180.95000000000002\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 6\n",
"PATH: [35, 14, 20, 16, 7, 13, 21]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [20, 16, 14, 35, 13, 21, 7]\n",
"DISTANCE: 181.04999999999998\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [16, 13, 21, 7, 20, 14, 35]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [35, 14, 20, 16, 7, 21, 13]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [7, 20, 35, 14, 16, 13, 21]\n",
"DISTANCE: 181.35\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [35, 16, 20, 21, 13, 7, 14]\n",
"DISTANCE: 181.05\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 3\n",
"PATH: [21, 20, 16, 14, 35, 7, 13]\n",
"DISTANCE: 181.04999999999998\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 5\n",
"PATH: [21, 35, 14, 20, 16, 7, 13]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [16, 7, 13, 21, 20, 35, 14]\n",
"DISTANCE: 181.35\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [35, 14, 16, 20, 13, 21, 7]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [7, 16, 20, 14, 35, 13, 21]\n",
"DISTANCE: 181.35\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [21, 13, 14, 35, 16, 20, 7]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [7, 21, 13, 14, 35, 16, 20]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [21, 35, 14, 20, 16, 7, 13]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [20, 16, 14, 35, 13, 21, 7]\n",
"DISTANCE: 181.04999999999998\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [20, 16, 7, 13, 21, 35, 14]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [35, 20, 16, 7, 13, 21, 14]\n",
"DISTANCE: 181.35\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 3\n",
"PATH: [16, 7, 13, 21, 35, 14, 20]\n",
"DISTANCE: 180.95\n",
"COST: 25000\n",
"TEMPERATURE: 27.510066986083984\n",
"BEE: F\n",
"\n",
"\n"
]
}
],
"source": [
"if __name__ == '__main__':\n",
" for i in range (0, 10):\n",
" \n",
" main()\n",
"\n",
" # main()"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[14, 21, 13, 7, 20, 16, 35]"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def getting_best_path():\n",
" # Control parameters\n",
" population = 40\n",
" forager_percent = 0.5\n",
" onlooker_percent = 0.4\n",
" role_percent = [onlooker_percent, forager_percent]\n",
" scout_percent = 0.01\n",
" scout_count = math.ceil(population * scout_percent)\n",
" forager_limit = 500\n",
" cycle_limit = 100\n",
" cycle = 1\n",
" best_distance = sys.maxsize\n",
" best_path = []\n",
" result = ()\n",
" data = cities\n",
" # Initialization\n",
" hive = initialize_hive(population, data)\n",
" assign_roles(hive, role_percent)\n",
" cost = get_total_cost(Dataz,cities)\n",
" waggle_distance,waggle_path = waggle(hive, best_distance,forager_limit,scout_count)\n",
" if (waggle_distance < best_distance) and (waggle_distance <= Maximum_distance):\n",
" best_distance = waggle_distance\n",
" best_path = list(waggle_path)\n",
" recruit_distance,recruit_path = recruit(hive, best_distance,best_path)\n",
" if (recruit_distance < best_distance) and (recruit_distance <= Maximum_distance):\n",
" best_path = list(recruit_path)\n",
" best_distance = recruit_distance \n",
" return best_path\n",
"best_path = getting_best_path()\n",
"best_path"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Museum T. B. Silalahi Center\n",
"Siregar Aek Nalas\n",
"The Kaldera\n",
"Air Terjun Pandumaan\n",
"Lumban Silintong\n",
"Pantai Meat\n",
"Makam Raja Sisingamangaraja XII\n"
]
}
],
"source": [
"def decoding_ABC():\n",
" name = 0\n",
" for i in range(len(best_path)):\n",
" name = best_path[i]\n",
" print(Dataz.iloc[name][1])\n",
" i+=1\n",
" return \n",
"decoding_ABC()\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