Commit 5c56bae1 by ratna kasmala

Upload New File

parent b67e6851
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"import csv\n",
"import math\n",
"import random\n",
"import sys\n",
"from scipy.spatial import distance\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\n",
"from keras.layers import Dropout, Dense\n",
"from math import sqrt\n"
]
},
{
"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": 3,
"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>ID_City</th>\n",
" <th>City</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>Pantai BUL BUL</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>BUKIT travel Gibeon</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>Pakkodian</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>Taman Eden 100 Tobasa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4</td>\n",
" <td>Water Park Tambunan</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" ID_City City\n",
"0 0 Pantai BUL BUL\n",
"1 1 BUKIT travel Gibeon\n",
"2 2 Pakkodian\n",
"3 3 Taman Eden 100 Tobasa\n",
"4 4 Water Park Tambunan"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Data3.head()"
]
},
{
"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": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"209.0"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"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(Data2.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 = Data2.iloc[source][target] \n",
" distance = sum(distance_route)+last_distance\n",
" return distance\n",
"\n",
"\n",
"id_city = list(Data3['ID_City'])\n",
"Data4 = random.sample(range(len(id_city)), 7)\n",
"getting_distance(Data4)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[30, 0, 16, 32, 20, 29, 1]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Data4"
]
},
{
"cell_type": "code",
"execution_count": 30,
"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": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"720"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Fitness_value.getting_max_distance()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"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": 33,
"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": 34,
"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": 35,
"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": 36,
"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": 37,
"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": 38,
"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": 39,
"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": 40,
"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": 41,
"metadata": {},
"outputs": [],
"source": [
"Maximum_distance = Fitness_value.getting_max_distance()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"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": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CYCLE: 1\n",
"PATH: [20, 1, 32, 30, 29, 0, 16]\n",
"DISTANCE: 191.70000000000002\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [30, 16, 20, 0, 29, 1, 32]\n",
"DISTANCE: 185.30000000000004\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [16, 20, 0, 1, 29, 32, 30]\n",
"DISTANCE: 185.20000000000002\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [30, 1, 29, 0, 20, 16, 32]\n",
"DISTANCE: 185.10000000000002\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 3\n",
"PATH: [30, 32, 1, 29, 20, 16, 0]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 15\n",
"PATH: [32, 30, 29, 1, 16, 20, 0]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [29, 20, 16, 0, 30, 32, 1]\n",
"DISTANCE: 185.0\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 3\n",
"PATH: [30, 1, 29, 16, 20, 0, 32]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [1, 29, 20, 16, 0, 32, 30]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [32, 30, 0, 20, 16, 1, 29]\n",
"DISTANCE: 185.3\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [20, 0, 32, 30, 29, 1, 16]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [30, 29, 1, 16, 20, 0, 32]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [20, 16, 0, 1, 29, 30, 32]\n",
"DISTANCE: 185.4\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [0, 20, 16, 32, 30, 1, 29]\n",
"DISTANCE: 185.1\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 5\n",
"PATH: [20, 0, 32, 30, 29, 1, 16]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 6\n",
"PATH: [0, 30, 32, 1, 29, 20, 16]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [1, 0, 20, 16, 30, 32, 29]\n",
"DISTANCE: 185.2\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [30, 32, 1, 29, 0, 20, 16]\n",
"DISTANCE: 185.1\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 3\n",
"PATH: [29, 1, 16, 20, 0, 32, 30]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 12\n",
"PATH: [32, 30, 29, 1, 16, 20, 0]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [29, 1, 16, 20, 0, 32, 30]\n",
"DISTANCE: 185.0\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [0, 30, 32, 1, 29, 20, 16]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [1, 16, 20, 0, 32, 30, 29]\n",
"DISTANCE: 185.0\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [0, 30, 32, 1, 29, 20, 16]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [20, 0, 1, 29, 30, 32, 16]\n",
"DISTANCE: 185.4\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [32, 30, 1, 29, 0, 20, 16]\n",
"DISTANCE: 185.10000000000002\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [29, 1, 0, 16, 20, 32, 30]\n",
"DISTANCE: 185.00000000000003\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 5\n",
"PATH: [0, 32, 30, 29, 1, 16, 20]\n",
"DISTANCE: 185.0\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 21\n",
"PATH: [1, 29, 20, 16, 0, 32, 30]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [20, 16, 0, 32, 30, 29, 1]\n",
"DISTANCE: 185.10000000000002\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [1, 29, 0, 16, 20, 30, 32]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 21\n",
"PATH: [0, 32, 30, 1, 29, 20, 16]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [0, 30, 32, 1, 29, 20, 16]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [16, 0, 1, 29, 30, 32, 20]\n",
"DISTANCE: 185.4\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [32, 1, 29, 0, 20, 16, 30]\n",
"DISTANCE: 185.1\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 3\n",
"PATH: [32, 1, 29, 20, 16, 0, 30]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [30, 32, 1, 29, 20, 16, 0]\n",
"DISTANCE: 185.0\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 6\n",
"PATH: [30, 32, 1, 29, 16, 20, 0]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [32, 1, 29, 0, 20, 16, 30]\n",
"DISTANCE: 185.1\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [16, 20, 32, 30, 29, 1, 0]\n",
"DISTANCE: 185.0\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 34\n",
"PATH: [32, 30, 1, 29, 20, 16, 0]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [32, 30, 20, 16, 0, 1, 29]\n",
"DISTANCE: 185.3\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [30, 32, 1, 29, 0, 20, 16]\n",
"DISTANCE: 185.1\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [29, 20, 16, 0, 30, 32, 1]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 61\n",
"PATH: [30, 29, 1, 16, 20, 0, 32]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [0, 30, 1, 29, 32, 16, 20]\n",
"DISTANCE: 190.4\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [32, 29, 1, 0, 16, 20, 30]\n",
"DISTANCE: 185.10000000000002\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [30, 32, 1, 29, 20, 16, 0]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 72\n",
"PATH: [30, 32, 1, 29, 16, 20, 0]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [20, 30, 32, 1, 29, 0, 16]\n",
"DISTANCE: 185.0\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 11\n",
"PATH: [30, 32, 1, 29, 16, 20, 0]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [1, 29, 0, 20, 16, 32, 30]\n",
"DISTANCE: 185.1\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 3\n",
"PATH: [20, 32, 30, 29, 1, 0, 16]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 7\n",
"PATH: [32, 30, 29, 1, 16, 20, 0]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [29, 32, 30, 16, 20, 0, 1]\n",
"DISTANCE: 185.20000000000002\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [16, 20, 30, 32, 29, 1, 0]\n",
"DISTANCE: 185.1\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [0, 16, 20, 30, 32, 1, 29]\n",
"DISTANCE: 185.0\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 19\n",
"PATH: [1, 29, 20, 16, 0, 32, 30]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [16, 32, 29, 1, 30, 0, 20]\n",
"DISTANCE: 190.5\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 1\n",
"PATH: [29, 1, 30, 32, 0, 20, 16]\n",
"DISTANCE: 185.60000000000002\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [30, 0, 16, 20, 1, 29, 32]\n",
"DISTANCE: 185.20000000000002\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 2\n",
"PATH: [32, 30, 29, 1, 20, 16, 0]\n",
"DISTANCE: 185.1\n",
"BEE: R\n",
"\n",
"\n",
"CYCLE: 4\n",
"PATH: [20, 0, 32, 30, 29, 1, 16]\n",
"DISTANCE: 185.0\n",
"BEE: F\n",
"\n",
"\n",
"CYCLE: 6\n",
"PATH: [0, 32, 30, 1, 29, 20, 16]\n",
"DISTANCE: 184.99999999999997\n",
"BEE: F\n",
"\n",
"\n"
]
}
],
"source": [
"if __name__ == '__main__':\n",
" for i in range (0, 20):\n",
" \n",
" main()\n",
"\n",
" # main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"best_path = getting_best_path()\n",
"best_path"
]
}
],
"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