commitv6

parent 5bb9deb4
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Kode Program TA 14"
]
},
{
"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",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"# Library \n",
"import pandas as pd\n",
"from numpy import * \n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import math\n",
"import csv\n",
"import random\n",
"import time\n",
"import sys\n",
"import datetime\n",
"import timeit\n",
"from sklearn.neighbors import DistanceMetric\n",
"from math import radians,cos,sin\n",
"from haversine import haversine, Unit\n",
"from scipy.spatial import distance\n",
"from sklearn.preprocessing import MinMaxScaler\n",
"from keras.models import Sequential\n",
"from keras.layers import Bidirectional, GlobalMaxPool1D\n",
"from keras.layers import LSTM"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#Load dataset sebelum integrasi\n",
"Data1 = pd.read_csv('./tri/Data Toba Samosir - Sheet3.csv')\n",
"Data1.drop(Data1.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"Data2 = pd.read_csv('./tri/Data Toba Samosir - Sheet1.csv')\n",
"Data2.drop(Data2.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"Data3 = pd.read_csv('./tri/List_city.csv')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"#Data1\n",
"#Data2\n",
"#Data3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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": 5,
"metadata": {},
"outputs": [],
"source": [
"#cost = input()\n",
"cost = 400000\n",
"Cost = int(cost)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random Data"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[32, 2, 9, 10, 11, 4, 26]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id_city = list(Data3['ID_City'])\n",
"Data4 = random.sample(range(len(id_city)), 7)\n",
"Data4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fitness Calculation"
]
},
{
"cell_type": "code",
"execution_count": 16,
"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": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"720"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Fitness_value.getting_max_distance()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"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 = 200\n",
"map = [];\n",
"particles = []\n",
"Maximum_distance = Fitness_value.getting_max_distance()\n",
"CITY_COUNT = len(Data4)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"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": 28,
"metadata": {},
"outputs": [],
"source": [
"Data4 = [32, 2, 9, 10, 11, 4, 26]"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"# define city class\n",
"class City:\n",
" def __init__(self):\n",
" self.mX = 0\n",
" self.mY = 0\n",
"# calculating distance of city\n",
"def get_distance():\n",
" distance_route = []\n",
" last_distance = 0\n",
" distance = 0\n",
" for i in range(0,len(Data4)-1):\n",
" source = Data4[i]\n",
" target = Data4[i+1]\n",
" distance_route.append(Data2.iloc[source][target])\n",
" for i in range(0,len(Data4)-1):\n",
" source = Data4[len(Data4)-1]\n",
" target = Data4[len(Data4)-len(Data4)]\n",
" last_distance = Data2.iloc[source][target] \n",
" distance = sum(distance_route)+last_distance\n",
" return distance\n",
"def get_total_distance(index):\n",
" particles[index].set_pBest_distance(0)\n",
" for i in range(CITY_COUNT):\n",
" if i == CITY_COUNT - 1:\n",
" particles[index].set_pBest_distance(particles[index].get_pBest_distance() + get_distance(particles[index].get_data(CITY_COUNT - 1), particles[index].get_data(0))) # Complete trip.\n",
" else:\n",
" particles[index].set_pBest_distance(particles[index].get_pBest_distance() + get_distance(particles[index].get_data(i), particles[index].get_data(i + 1)))\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 51,
"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": 40,
"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",
" \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": 52,
"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()\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"def quicksort(array, left, right):\n",
" pivot = quicksort_partition(array, left, right)\n",
" \n",
" if left < pivot:\n",
" quicksort(array, left, pivot - 1)\n",
" \n",
" if right > pivot:\n",
" quicksort(array, pivot + 1, right) \n",
" return array"
]
},
{
"cell_type": "code",
"execution_count": 54,
"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",
" \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",
" \n",
" numbers[left] = pivot\n",
" pivot = left\n",
" left = I_hold\n",
" right = r_hold\n",
" \n",
" return pivot"
]
},
{
"cell_type": "code",
"execution_count": 55,
"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",
" \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": 56,
"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, len(Data4)) # 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(len(arr)):\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",
" \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",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 61,
"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",
" \n",
" # Update pBest value.\n",
" get_total_distance()\n",
" \n",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"def PSO_algorithm():\n",
" epoch = 0\n",
" done = False\n",
" \n",
" initialize_particles()\n",
" \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",
" \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()\n",
" \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",
" \n",
" sys.stdout.write(\"epoch number: \" + str(epoch) + \"\\n\")\n",
" \n",
" epoch += 1 \n",
" else:\n",
" done = True\n",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 69,
"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",
" \n",
" sys.stdout.write(\"Best Route: \")\n",
" for j in range(CITY_COUNT):\n",
" sys.stdout.write(str(particles[0].get_data(j)) + \", \")\n",
" \n",
" sys.stdout.write(\"Distance: \" + str(particles[0].get_pBest_distance()) +\"\\n\")\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Route: 0, 1, 3, 2, 4, 5, 6, Distance: 0.9\n",
"Route: 2, 0, 6, 3, 5, 4, 1, Distance: 0.9\n",
"Route: 1, 6, 4, 5, 0, 2, 3, Distance: 0.9\n",
"Route: 3, 4, 5, 2, 6, 1, 0, Distance: 0.9\n",
"Route: 3, 1, 2, 0, 5, 4, 6, Distance: 0.9\n",
"Changes for particle 1: 2\n"
]
},
{
"ename": "NameError",
"evalue": "name 'arr' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-70-1a83413392a8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0minitialize_map\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mPSO_algorithm\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mprint_best_solution\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-68-7469a4923133>\u001b[0m in \u001b[0;36mPSO_algorithm\u001b[1;34m()\u001b[0m\n\u001b[0;32m 24\u001b[0m \u001b[1;31m# list has to sorted in order for get_velocity() to work.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 25\u001b[0m \u001b[0mget_velocity\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 26\u001b[1;33m \u001b[0mupdate_particles\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 27\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 28\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"epoch number: \"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mepoch\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m\"\\n\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-61-58efe8e1ae0d>\u001b[0m in \u001b[0;36mupdate_particles\u001b[1;34m()\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;31m# Push it closer to it's best neighbor.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[0mcopy_from_particle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;31m# Update pBest value.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-56-3e6b95d5d0b4>\u001b[0m in \u001b[0;36mcopy_from_particle\u001b[1;34m(source, destination)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mcopy_from_particle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msource\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdestination\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;31m# push destination's data points closer to source's data points.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mtargetA\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# source's city to target.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mtargetB\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mindexA\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'arr' is not defined"
]
}
],
"source": [
" if __name__ == '__main__':\n",
" initialize_map()\n",
" PSO_algorithm()\n",
" print_best_solution() "
]
},
{
"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": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#read data\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"Data1 = pd.read_csv('./tri/Data Toba Samosir - Sheet2.csv')\n",
"Data1.drop(Data1.filter(regex=\"Unname\"),axis=1, inplace=True)\n",
"Data2 = pd.read_csv('./tri/Data Toba Samosir - Sheet1.csv')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" <th>3</th>\n",
" <th>4</th>\n",
" <th>5</th>\n",
" <th>6</th>\n",
" <th>7</th>\n",
" <th>8</th>\n",
" <th>9</th>\n",
" <th>10</th>\n",
" <th>11</th>\n",
" <th>12</th>\n",
" <th>13</th>\n",
" <th>14</th>\n",
" <th>15</th>\n",
" <th>16</th>\n",
" <th>17</th>\n",
" <th>18</th>\n",
" <th>19</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.0</td>\n",
" <td>50.9</td>\n",
" <td>12.8</td>\n",
" <td>44.7</td>\n",
" <td>5.9</td>\n",
" <td>71.0</td>\n",
" <td>60.6</td>\n",
" <td>12.4</td>\n",
" <td>63.5</td>\n",
" <td>50.5</td>\n",
" <td>52.7</td>\n",
" <td>43.1</td>\n",
" <td>51.9</td>\n",
" <td>56.3</td>\n",
" <td>6.0</td>\n",
" <td>11.8</td>\n",
" <td>14.6</td>\n",
" <td>60.7</td>\n",
" <td>9.0</td>\n",
" <td>42.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>50.9</td>\n",
" <td>0.0</td>\n",
" <td>59.2</td>\n",
" <td>6.2</td>\n",
" <td>47.3</td>\n",
" <td>53.5</td>\n",
" <td>12.0</td>\n",
" <td>51.4</td>\n",
" <td>17.6</td>\n",
" <td>18.6</td>\n",
" <td>20.7</td>\n",
" <td>50.3</td>\n",
" <td>59.0</td>\n",
" <td>7.7</td>\n",
" <td>52.4</td>\n",
" <td>58.2</td>\n",
" <td>61.0</td>\n",
" <td>67.9</td>\n",
" <td>55.5</td>\n",
" <td>8.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>12.8</td>\n",
" <td>59.2</td>\n",
" <td>0.0</td>\n",
" <td>53.0</td>\n",
" <td>14.2</td>\n",
" <td>11.9</td>\n",
" <td>68.9</td>\n",
" <td>20.7</td>\n",
" <td>71.8</td>\n",
" <td>58.8</td>\n",
" <td>61.0</td>\n",
" <td>51.4</td>\n",
" <td>60.2</td>\n",
" <td>64.6</td>\n",
" <td>8.0</td>\n",
" <td>4.1</td>\n",
" <td>4.8</td>\n",
" <td>69.0</td>\n",
" <td>15.4</td>\n",
" <td>50.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>44.7</td>\n",
" <td>6.2</td>\n",
" <td>53.0</td>\n",
" <td>0.0</td>\n",
" <td>41.1</td>\n",
" <td>47.2</td>\n",
" <td>15.9</td>\n",
" <td>45.5</td>\n",
" <td>18.8</td>\n",
" <td>12.3</td>\n",
" <td>14.5</td>\n",
" <td>44.0</td>\n",
" <td>52.8</td>\n",
" <td>11.6</td>\n",
" <td>46.2</td>\n",
" <td>52.0</td>\n",
" <td>54.8</td>\n",
" <td>61.7</td>\n",
" <td>49.2</td>\n",
" <td>2.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.9</td>\n",
" <td>47.3</td>\n",
" <td>14.2</td>\n",
" <td>41.1</td>\n",
" <td>0.0</td>\n",
" <td>8.4</td>\n",
" <td>57.0</td>\n",
" <td>13.8</td>\n",
" <td>59.8</td>\n",
" <td>46.9</td>\n",
" <td>49.0</td>\n",
" <td>39.4</td>\n",
" <td>48.2</td>\n",
" <td>52.7</td>\n",
" <td>7.4</td>\n",
" <td>13.1</td>\n",
" <td>16.0</td>\n",
" <td>57.0</td>\n",
" <td>10.4</td>\n",
" <td>38.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>7.1</td>\n",
" <td>53.5</td>\n",
" <td>11.1</td>\n",
" <td>47.2</td>\n",
" <td>8.4</td>\n",
" <td>0.0</td>\n",
" <td>63.2</td>\n",
" <td>14.9</td>\n",
" <td>66.0</td>\n",
" <td>53.1</td>\n",
" <td>55.3</td>\n",
" <td>45.6</td>\n",
" <td>54.4</td>\n",
" <td>58.9</td>\n",
" <td>3.5</td>\n",
" <td>10.9</td>\n",
" <td>13.0</td>\n",
" <td>63.3</td>\n",
" <td>9.6</td>\n",
" <td>45.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>60.6</td>\n",
" <td>12.0</td>\n",
" <td>68.9</td>\n",
" <td>15.9</td>\n",
" <td>57.0</td>\n",
" <td>63.1</td>\n",
" <td>0.0</td>\n",
" <td>61.4</td>\n",
" <td>5.5</td>\n",
" <td>28.2</td>\n",
" <td>30.4</td>\n",
" <td>59.9</td>\n",
" <td>68.7</td>\n",
" <td>10.8</td>\n",
" <td>62.1</td>\n",
" <td>67.9</td>\n",
" <td>70.7</td>\n",
" <td>77.6</td>\n",
" <td>65.1</td>\n",
" <td>18.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>12.4</td>\n",
" <td>51.7</td>\n",
" <td>20.7</td>\n",
" <td>45.5</td>\n",
" <td>11.3</td>\n",
" <td>14.9</td>\n",
" <td>61.4</td>\n",
" <td>0.0</td>\n",
" <td>64.2</td>\n",
" <td>51.3</td>\n",
" <td>53.5</td>\n",
" <td>43.8</td>\n",
" <td>52.6</td>\n",
" <td>57.1</td>\n",
" <td>13.9</td>\n",
" <td>19.6</td>\n",
" <td>22.9</td>\n",
" <td>61.5</td>\n",
" <td>15.0</td>\n",
" <td>43.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>62.5</td>\n",
" <td>16.6</td>\n",
" <td>71.8</td>\n",
" <td>17.8</td>\n",
" <td>58.8</td>\n",
" <td>66.0</td>\n",
" <td>5.5</td>\n",
" <td>63.3</td>\n",
" <td>0.0</td>\n",
" <td>31.1</td>\n",
" <td>33.2</td>\n",
" <td>61.8</td>\n",
" <td>70.6</td>\n",
" <td>23.0</td>\n",
" <td>64.0</td>\n",
" <td>69.7</td>\n",
" <td>73.6</td>\n",
" <td>80.4</td>\n",
" <td>67.0</td>\n",
" <td>21.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>50.5</td>\n",
" <td>18.6</td>\n",
" <td>58.8</td>\n",
" <td>12.3</td>\n",
" <td>46.9</td>\n",
" <td>53.1</td>\n",
" <td>28.2</td>\n",
" <td>51.3</td>\n",
" <td>31.1</td>\n",
" <td>0.0</td>\n",
" <td>5.7</td>\n",
" <td>49.9</td>\n",
" <td>58.6</td>\n",
" <td>23.9</td>\n",
" <td>52.0</td>\n",
" <td>57.8</td>\n",
" <td>60.7</td>\n",
" <td>67.5</td>\n",
" <td>55.1</td>\n",
" <td>10.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>52.7</td>\n",
" <td>20.7</td>\n",
" <td>61.0</td>\n",
" <td>14.5</td>\n",
" <td>49.0</td>\n",
" <td>55.2</td>\n",
" <td>30.4</td>\n",
" <td>53.5</td>\n",
" <td>33.2</td>\n",
" <td>5.7</td>\n",
" <td>0.0</td>\n",
" <td>52.0</td>\n",
" <td>60.8</td>\n",
" <td>26.1</td>\n",
" <td>54.2</td>\n",
" <td>59.9</td>\n",
" <td>62.8</td>\n",
" <td>69.6</td>\n",
" <td>57.2</td>\n",
" <td>12.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>43.1</td>\n",
" <td>50.3</td>\n",
" <td>51.4</td>\n",
" <td>44.0</td>\n",
" <td>39.4</td>\n",
" <td>45.6</td>\n",
" <td>59.9</td>\n",
" <td>43.8</td>\n",
" <td>62.8</td>\n",
" <td>49.9</td>\n",
" <td>52.0</td>\n",
" <td>0.0</td>\n",
" <td>13.6</td>\n",
" <td>55.6</td>\n",
" <td>44.6</td>\n",
" <td>50.3</td>\n",
" <td>53.2</td>\n",
" <td>22.4</td>\n",
" <td>47.6</td>\n",
" <td>41.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>51.9</td>\n",
" <td>59.0</td>\n",
" <td>60.2</td>\n",
" <td>52.8</td>\n",
" <td>48.2</td>\n",
" <td>54.4</td>\n",
" <td>68.7</td>\n",
" <td>52.8</td>\n",
" <td>71.6</td>\n",
" <td>58.6</td>\n",
" <td>60.8</td>\n",
" <td>13.6</td>\n",
" <td>0.0</td>\n",
" <td>64.4</td>\n",
" <td>53.4</td>\n",
" <td>59.1</td>\n",
" <td>62.0</td>\n",
" <td>11.6</td>\n",
" <td>56.4</td>\n",
" <td>50.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>56.3</td>\n",
" <td>7.7</td>\n",
" <td>64.6</td>\n",
" <td>11.6</td>\n",
" <td>52.7</td>\n",
" <td>58.8</td>\n",
" <td>10.8</td>\n",
" <td>57.1</td>\n",
" <td>23.0</td>\n",
" <td>23.9</td>\n",
" <td>26.1</td>\n",
" <td>55.6</td>\n",
" <td>64.4</td>\n",
" <td>0.0</td>\n",
" <td>57.8</td>\n",
" <td>63.6</td>\n",
" <td>66.4</td>\n",
" <td>73.3</td>\n",
" <td>60.8</td>\n",
" <td>14.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>6.0</td>\n",
" <td>52.5</td>\n",
" <td>8.0</td>\n",
" <td>46.2</td>\n",
" <td>7.4</td>\n",
" <td>3.3</td>\n",
" <td>62.1</td>\n",
" <td>13.9</td>\n",
" <td>65.0</td>\n",
" <td>52.0</td>\n",
" <td>54.1</td>\n",
" <td>44.6</td>\n",
" <td>53.4</td>\n",
" <td>57.8</td>\n",
" <td>0.0</td>\n",
" <td>7.0</td>\n",
" <td>9.8</td>\n",
" <td>62.1</td>\n",
" <td>8.6</td>\n",
" <td>43.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>11.8</td>\n",
" <td>58.2</td>\n",
" <td>4.1</td>\n",
" <td>52.0</td>\n",
" <td>13.2</td>\n",
" <td>10.9</td>\n",
" <td>67.9</td>\n",
" <td>19.6</td>\n",
" <td>70.7</td>\n",
" <td>57.8</td>\n",
" <td>59.9</td>\n",
" <td>50.3</td>\n",
" <td>59.1</td>\n",
" <td>63.6</td>\n",
" <td>7.0</td>\n",
" <td>0.0</td>\n",
" <td>5.9</td>\n",
" <td>67.9</td>\n",
" <td>14.4</td>\n",
" <td>49.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>14.6</td>\n",
" <td>61.0</td>\n",
" <td>4.7</td>\n",
" <td>54.8</td>\n",
" <td>16.0</td>\n",
" <td>13.7</td>\n",
" <td>70.7</td>\n",
" <td>22.5</td>\n",
" <td>73.6</td>\n",
" <td>60.6</td>\n",
" <td>62.8</td>\n",
" <td>53.2</td>\n",
" <td>62.0</td>\n",
" <td>66.4</td>\n",
" <td>9.9</td>\n",
" <td>5.9</td>\n",
" <td>0.0</td>\n",
" <td>70.8</td>\n",
" <td>17.2</td>\n",
" <td>52.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>60.7</td>\n",
" <td>67.9</td>\n",
" <td>74.2</td>\n",
" <td>61.7</td>\n",
" <td>57.0</td>\n",
" <td>68.5</td>\n",
" <td>82.8</td>\n",
" <td>61.5</td>\n",
" <td>80.4</td>\n",
" <td>72.7</td>\n",
" <td>74.9</td>\n",
" <td>22.4</td>\n",
" <td>11.6</td>\n",
" <td>78.5</td>\n",
" <td>62.2</td>\n",
" <td>67.9</td>\n",
" <td>76.1</td>\n",
" <td>0.0</td>\n",
" <td>65.2</td>\n",
" <td>64.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>9.0</td>\n",
" <td>55.5</td>\n",
" <td>15.4</td>\n",
" <td>49.2</td>\n",
" <td>10.4</td>\n",
" <td>9.6</td>\n",
" <td>65.1</td>\n",
" <td>15.0</td>\n",
" <td>68.0</td>\n",
" <td>55.1</td>\n",
" <td>57.2</td>\n",
" <td>47.6</td>\n",
" <td>56.4</td>\n",
" <td>60.8</td>\n",
" <td>8.6</td>\n",
" <td>14.3</td>\n",
" <td>17.2</td>\n",
" <td>65.2</td>\n",
" <td>0.0</td>\n",
" <td>46.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>42.4</td>\n",
" <td>8.7</td>\n",
" <td>50.7</td>\n",
" <td>2.4</td>\n",
" <td>38.8</td>\n",
" <td>44.9</td>\n",
" <td>18.3</td>\n",
" <td>43.2</td>\n",
" <td>21.2</td>\n",
" <td>10.0</td>\n",
" <td>12.2</td>\n",
" <td>41.7</td>\n",
" <td>50.5</td>\n",
" <td>14.0</td>\n",
" <td>43.9</td>\n",
" <td>49.6</td>\n",
" <td>52.5</td>\n",
" <td>59.3</td>\n",
" <td>46.9</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1 2 3 4 5 6 7 8 9 10 11 \\\n",
"0 0.0 50.9 12.8 44.7 5.9 71.0 60.6 12.4 63.5 50.5 52.7 43.1 \n",
"1 50.9 0.0 59.2 6.2 47.3 53.5 12.0 51.4 17.6 18.6 20.7 50.3 \n",
"2 12.8 59.2 0.0 53.0 14.2 11.9 68.9 20.7 71.8 58.8 61.0 51.4 \n",
"3 44.7 6.2 53.0 0.0 41.1 47.2 15.9 45.5 18.8 12.3 14.5 44.0 \n",
"4 5.9 47.3 14.2 41.1 0.0 8.4 57.0 13.8 59.8 46.9 49.0 39.4 \n",
"5 7.1 53.5 11.1 47.2 8.4 0.0 63.2 14.9 66.0 53.1 55.3 45.6 \n",
"6 60.6 12.0 68.9 15.9 57.0 63.1 0.0 61.4 5.5 28.2 30.4 59.9 \n",
"7 12.4 51.7 20.7 45.5 11.3 14.9 61.4 0.0 64.2 51.3 53.5 43.8 \n",
"8 62.5 16.6 71.8 17.8 58.8 66.0 5.5 63.3 0.0 31.1 33.2 61.8 \n",
"9 50.5 18.6 58.8 12.3 46.9 53.1 28.2 51.3 31.1 0.0 5.7 49.9 \n",
"10 52.7 20.7 61.0 14.5 49.0 55.2 30.4 53.5 33.2 5.7 0.0 52.0 \n",
"11 43.1 50.3 51.4 44.0 39.4 45.6 59.9 43.8 62.8 49.9 52.0 0.0 \n",
"12 51.9 59.0 60.2 52.8 48.2 54.4 68.7 52.8 71.6 58.6 60.8 13.6 \n",
"13 56.3 7.7 64.6 11.6 52.7 58.8 10.8 57.1 23.0 23.9 26.1 55.6 \n",
"14 6.0 52.5 8.0 46.2 7.4 3.3 62.1 13.9 65.0 52.0 54.1 44.6 \n",
"15 11.8 58.2 4.1 52.0 13.2 10.9 67.9 19.6 70.7 57.8 59.9 50.3 \n",
"16 14.6 61.0 4.7 54.8 16.0 13.7 70.7 22.5 73.6 60.6 62.8 53.2 \n",
"17 60.7 67.9 74.2 61.7 57.0 68.5 82.8 61.5 80.4 72.7 74.9 22.4 \n",
"18 9.0 55.5 15.4 49.2 10.4 9.6 65.1 15.0 68.0 55.1 57.2 47.6 \n",
"19 42.4 8.7 50.7 2.4 38.8 44.9 18.3 43.2 21.2 10.0 12.2 41.7 \n",
"\n",
" 12 13 14 15 16 17 18 19 \n",
"0 51.9 56.3 6.0 11.8 14.6 60.7 9.0 42.4 \n",
"1 59.0 7.7 52.4 58.2 61.0 67.9 55.5 8.7 \n",
"2 60.2 64.6 8.0 4.1 4.8 69.0 15.4 50.7 \n",
"3 52.8 11.6 46.2 52.0 54.8 61.7 49.2 2.4 \n",
"4 48.2 52.7 7.4 13.1 16.0 57.0 10.4 38.8 \n",
"5 54.4 58.9 3.5 10.9 13.0 63.3 9.6 45.0 \n",
"6 68.7 10.8 62.1 67.9 70.7 77.6 65.1 18.3 \n",
"7 52.6 57.1 13.9 19.6 22.9 61.5 15.0 43.2 \n",
"8 70.6 23.0 64.0 69.7 73.6 80.4 67.0 21.2 \n",
"9 58.6 23.9 52.0 57.8 60.7 67.5 55.1 10.0 \n",
"10 60.8 26.1 54.2 59.9 62.8 69.6 57.2 12.2 \n",
"11 13.6 55.6 44.6 50.3 53.2 22.4 47.6 41.7 \n",
"12 0.0 64.4 53.4 59.1 62.0 11.6 56.4 50.5 \n",
"13 64.4 0.0 57.8 63.6 66.4 73.3 60.8 14.0 \n",
"14 53.4 57.8 0.0 7.0 9.8 62.1 8.6 43.9 \n",
"15 59.1 63.6 7.0 0.0 5.9 67.9 14.4 49.6 \n",
"16 62.0 66.4 9.9 5.9 0.0 70.8 17.2 52.5 \n",
"17 11.6 78.5 62.2 67.9 76.1 0.0 65.2 64.6 \n",
"18 56.4 60.8 8.6 14.3 17.2 65.2 0.0 46.9 \n",
"19 50.5 14.0 43.9 49.6 52.5 59.3 46.9 0.0 "
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Data1"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"path = [2,3,4,5,1]"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"def calculate_distance():\n",
" distance_route = []\n",
" last_distance = 0\n",
" distance = 0\n",
" for i in range(0,4):\n",
" source = path[i]\n",
" target = path[i+1]\n",
" distance_route.append(Data1.iloc[source][target])\n",
" for i in range(0,4):\n",
" source = path[len(path)-1]\n",
" target = path[len(path)-len(path)]\n",
" last_distance = Data1.iloc[source][target] \n",
" distance = sum(distance_route)+last_distance\n",
" return distance"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"215.2"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_distance()"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [],
"source": [
"def get_last_distance():\n",
" last_distance = 0\n",
" for i in range(0,4):\n",
" source = path[len(path)-1]\n",
" target = path[len(path)-len(path)]\n",
" last_distance = Data1.iloc[source][target]\n",
" return last_distance "
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"59.2"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_last_distance()"
]
},
{
"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
}
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