Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
Code_Sidang_Ulang
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ari Whuandaniel Manurung
Code_Sidang_Ulang
Commits
50da4d3a
Commit
50da4d3a
authored
Jul 24, 2020
by
Ari Whuandaniel Manurung
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit4
parent
ed6e027b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
652 additions
and
0 deletions
+652
-0
ABC_getting_distance-checkpoint.ipynb
.ipynb_checkpoints/ABC_getting_distance-checkpoint.ipynb
+652
-0
git
git
+0
-0
No files found.
.ipynb_checkpoints/ABC_getting_distance-checkpoint.ipynb
0 → 100644
View file @
50da4d3a
{
"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": 39,
"metadata": {},
"outputs": [],
"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": 40,
"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": 41,
"metadata": {},
"outputs": [],
"source": [
"#Data1\n",
"#Data2\n",
"#Data3"
]
},
{
"cell_type": "code",
"execution_count": 42,
"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": 43,
"metadata": {},
"outputs": [],
"source": [
"#cost = input()\n",
"cost = 400000\n",
"Cost = int(cost)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random Data"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[13, 28, 35, 0, 7, 19, 26]"
]
},
"execution_count": 143,
"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": 45,
"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": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"720"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Fitness_value.getting_max_distance()"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[13, 28, 35, 0, 7, 19, 26]"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Data4"
]
},
{
"cell_type": "code",
"execution_count": 149,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"Maximum_distance = Fitness_value.getting_max_distance()\n",
"Maximum_cost = Fitness_value.getting_max_cost()\n",
"path = list(Data[\"ID_City\"])\n",
"def get_total_cost(path):\n",
" cost_route = []\n",
" cost = 0\n",
" for i in range(len(path)):\n",
" cost_route.append(Data5.iloc[i][4])\n",
" cost = sum(cost_route)\n",
" return cost "
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [],
"source": [
"def getting_distance(Path):\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"
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"404.6"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_distance()"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [],
"source": [
"def initialize_hive(population, data):\n",
" path = Data4\n",
" hive = [Bee(path) for i in range (0, population)]\n",
" return hive"
]
},
{
"cell_type": "code",
"execution_count": 156,
"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": 157,
"metadata": {},
"outputs": [],
"source": [
"def mutate_path(path):\n",
" # - will go out of range if last element is chosen.\n",
" path = Data4\n",
" new_path = random.sample(path,len(path))\n",
" return new_path"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[13, 26, 0, 28, 35, 7, 19]"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mutate_path(path)"
]
},
{
"cell_type": "code",
"execution_count": 159,
"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": 160,
"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": 161,
"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": 162,
"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": 163,
"metadata": {},
"outputs": [],
"source": [
"def print_details(cycle, path, distance,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": 173,
"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 = Data4\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(path)\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(path)\n",
" # temperature = Weather\n",
" print_details(cycle, best_path, best_distance,'F')\n",
" result = (cycle, best_path, best_distance,'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(path)\n",
" print_details(cycle, best_path, best_distance,'R')\n",
" result = (cycle, best_path, best_distance,'R')\n",
" if cycle % 100 == 0:\n",
" print(\"CYCLE #: {}\\n\".format(cycle))\n",
" cycle += 1"
]
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CYCLE: 1\n",
"PATH: [19, 35, 13, 7, 28, 26, 0]\n",
"DISTANCE: 404.6\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [7, 0, 35, 26, 13, 19, 28]\n",
"DISTANCE: 404.6\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [0, 26, 7, 13, 35, 19, 28]\n",
"DISTANCE: 404.6\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [0, 28, 7, 26, 35, 13, 19]\n",
"DISTANCE: 404.6\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [13, 28, 0, 26, 19, 7, 35]\n",
"DISTANCE: 404.6\n",
"BEE: F\n",
"\n",
"\n"
]
}
],
"source": [
"if __name__ == '__main__':\n",
" for i in range (0, 5):\n",
" \n",
" main()\n",
"\n",
" # main()"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [],
"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 = Data4\n",
" # Initialization\n",
" hive = initialize_hive(population, data)\n",
" assign_roles(hive, role_percent)\n",
" #cost = get_total_cost(path)\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"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[19, 0, 13, 26, 7, 28, 35]"
]
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"best_path = getting_best_path()\n",
"best_path"
]
},
{
"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
}
git
deleted
100644 → 0
View file @
ed6e027b
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment