Commit d11f1263 by Yolanda

PSO_update

parent 4037ea04
{
"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": 9,
"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>Pantai BUL BUL</th>\n",
" <th>BUKIT travel Gibeon</th>\n",
" <th>Pakkodian</th>\n",
" <th>Taman Eden 100 Tobasa</th>\n",
" <th>Water Park Tambunan</th>\n",
" <th>Bukit Pahoda</th>\n",
" <th>Bukit Senyum</th>\n",
" <th>Air Terjun Pandumaan</th>\n",
" <th>Long Beach</th>\n",
" <th>PANTAI AGADON</th>\n",
" <th>...</th>\n",
" <th>Dolok Surungan</th>\n",
" <th>Air Terjun Sampuran</th>\n",
" <th>Air Terjun Morena</th>\n",
" <th>Tornagodang</th>\n",
" <th>Hatulian Beach</th>\n",
" <th>Monumen Raja Sonakmalela</th>\n",
" <th>Lumban Binanga Beach</th>\n",
" <th>Pantai Pasifik Porsea</th>\n",
" <th>Desa Adat Ragi Hotang</th>\n",
" <th>Makam Raja Sisingamangaraja XII</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>...</td>\n",
" <td>74.2</td>\n",
" <td>37.1</td>\n",
" <td>64.5</td>\n",
" <td>36.1</td>\n",
" <td>11.6</td>\n",
" <td>3.30</td>\n",
" <td>10.7</td>\n",
" <td>22.5</td>\n",
" <td>14.4</td>\n",
" <td>5.70</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>...</td>\n",
" <td>102.0</td>\n",
" <td>83.5</td>\n",
" <td>71.7</td>\n",
" <td>63.9</td>\n",
" <td>46.1</td>\n",
" <td>49.70</td>\n",
" <td>45.2</td>\n",
" <td>32.9</td>\n",
" <td>60.8</td>\n",
" <td>52.10</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>...</td>\n",
" <td>82.5</td>\n",
" <td>30.4</td>\n",
" <td>72.8</td>\n",
" <td>44.4</td>\n",
" <td>19.9</td>\n",
" <td>9.50</td>\n",
" <td>18.9</td>\n",
" <td>31.8</td>\n",
" <td>4.5</td>\n",
" <td>7.70</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>...</td>\n",
" <td>95.8</td>\n",
" <td>77.2</td>\n",
" <td>65.5</td>\n",
" <td>57.7</td>\n",
" <td>39.9</td>\n",
" <td>43.50</td>\n",
" <td>39.0</td>\n",
" <td>26.7</td>\n",
" <td>54.5</td>\n",
" <td>45.90</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>...</td>\n",
" <td>70.5</td>\n",
" <td>38.4</td>\n",
" <td>60.9</td>\n",
" <td>32.4</td>\n",
" <td>7.9</td>\n",
" <td>4.70</td>\n",
" <td>7.0</td>\n",
" <td>19.9</td>\n",
" <td>15.7</td>\n",
" <td>7.10</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>...</td>\n",
" <td>76.7</td>\n",
" <td>36.2</td>\n",
" <td>67.1</td>\n",
" <td>38.6</td>\n",
" <td>14.1</td>\n",
" <td>3.80</td>\n",
" <td>13.2</td>\n",
" <td>26.1</td>\n",
" <td>13.5</td>\n",
" <td>4.00</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>...</td>\n",
" <td>112.0</td>\n",
" <td>93.1</td>\n",
" <td>81.4</td>\n",
" <td>73.6</td>\n",
" <td>55.8</td>\n",
" <td>59.40</td>\n",
" <td>54.9</td>\n",
" <td>42.6</td>\n",
" <td>70.4</td>\n",
" <td>61.80</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>...</td>\n",
" <td>74.9</td>\n",
" <td>44.9</td>\n",
" <td>65.3</td>\n",
" <td>36.8</td>\n",
" <td>12.8</td>\n",
" <td>11.20</td>\n",
" <td>11.9</td>\n",
" <td>24.3</td>\n",
" <td>22.2</td>\n",
" <td>13.60</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>...</td>\n",
" <td>114.0</td>\n",
" <td>95.0</td>\n",
" <td>83.3</td>\n",
" <td>75.5</td>\n",
" <td>57.7</td>\n",
" <td>61.30</td>\n",
" <td>56.8</td>\n",
" <td>44.5</td>\n",
" <td>72.3</td>\n",
" <td>64.60</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>...</td>\n",
" <td>102.0</td>\n",
" <td>83.1</td>\n",
" <td>71.3</td>\n",
" <td>63.5</td>\n",
" <td>45.7</td>\n",
" <td>49.30</td>\n",
" <td>44.8</td>\n",
" <td>19.8</td>\n",
" <td>60.4</td>\n",
" <td>51.70</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>...</td>\n",
" <td>104.0</td>\n",
" <td>85.2</td>\n",
" <td>73.5</td>\n",
" <td>65.7</td>\n",
" <td>47.9</td>\n",
" <td>51.50</td>\n",
" <td>47.0</td>\n",
" <td>34.7</td>\n",
" <td>62.5</td>\n",
" <td>53.80</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>...</td>\n",
" <td>94.2</td>\n",
" <td>75.6</td>\n",
" <td>26.2</td>\n",
" <td>56.1</td>\n",
" <td>38.2</td>\n",
" <td>41.80</td>\n",
" <td>37.3</td>\n",
" <td>25.1</td>\n",
" <td>52.9</td>\n",
" <td>44.20</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>...</td>\n",
" <td>103.0</td>\n",
" <td>84.4</td>\n",
" <td>15.5</td>\n",
" <td>64.9</td>\n",
" <td>47.0</td>\n",
" <td>50.60</td>\n",
" <td>46.1</td>\n",
" <td>33.8</td>\n",
" <td>61.7</td>\n",
" <td>53.00</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>...</td>\n",
" <td>107.0</td>\n",
" <td>88.8</td>\n",
" <td>77.1</td>\n",
" <td>69.3</td>\n",
" <td>51.5</td>\n",
" <td>55.10</td>\n",
" <td>50.6</td>\n",
" <td>38.3</td>\n",
" <td>66.1</td>\n",
" <td>57.50</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>...</td>\n",
" <td>75.7</td>\n",
" <td>32.3</td>\n",
" <td>66.0</td>\n",
" <td>37.6</td>\n",
" <td>13.1</td>\n",
" <td>2.80</td>\n",
" <td>12.2</td>\n",
" <td>25.1</td>\n",
" <td>9.6</td>\n",
" <td>0.35</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>...</td>\n",
" <td>81.4</td>\n",
" <td>29.3</td>\n",
" <td>71.8</td>\n",
" <td>43.3</td>\n",
" <td>18.8</td>\n",
" <td>8.50</td>\n",
" <td>17.9</td>\n",
" <td>30.8</td>\n",
" <td>5.6</td>\n",
" <td>6.60</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>...</td>\n",
" <td>84.3</td>\n",
" <td>32.1</td>\n",
" <td>74.6</td>\n",
" <td>46.2</td>\n",
" <td>21.7</td>\n",
" <td>11.40</td>\n",
" <td>20.8</td>\n",
" <td>33.7</td>\n",
" <td>0.6</td>\n",
" <td>9.50</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>...</td>\n",
" <td>112.0</td>\n",
" <td>93.2</td>\n",
" <td>9.7</td>\n",
" <td>73.7</td>\n",
" <td>55.9</td>\n",
" <td>59.50</td>\n",
" <td>55.0</td>\n",
" <td>42.7</td>\n",
" <td>70.5</td>\n",
" <td>67.10</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>...</td>\n",
" <td>78.7</td>\n",
" <td>39.6</td>\n",
" <td>69.0</td>\n",
" <td>40.6</td>\n",
" <td>16.1</td>\n",
" <td>5.90</td>\n",
" <td>15.2</td>\n",
" <td>28.1</td>\n",
" <td>16.9</td>\n",
" <td>8.30</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>...</td>\n",
" <td>93.5</td>\n",
" <td>74.9</td>\n",
" <td>63.2</td>\n",
" <td>55.4</td>\n",
" <td>37.6</td>\n",
" <td>41.20</td>\n",
" <td>36.7</td>\n",
" <td>24.4</td>\n",
" <td>52.2</td>\n",
" <td>43.60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>6.7</td>\n",
" <td>53.1</td>\n",
" <td>8.7</td>\n",
" <td>46.9</td>\n",
" <td>8.1</td>\n",
" <td>2.8</td>\n",
" <td>62.9</td>\n",
" <td>14.6</td>\n",
" <td>65.7</td>\n",
" <td>52.8</td>\n",
" <td>...</td>\n",
" <td>76.4</td>\n",
" <td>32.9</td>\n",
" <td>66.7</td>\n",
" <td>38.3</td>\n",
" <td>13.8</td>\n",
" <td>3.40</td>\n",
" <td>12.9</td>\n",
" <td>25.7</td>\n",
" <td>10.2</td>\n",
" <td>2.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>36.3</td>\n",
" <td>45.7</td>\n",
" <td>44.6</td>\n",
" <td>39.5</td>\n",
" <td>32.7</td>\n",
" <td>38.9</td>\n",
" <td>55.4</td>\n",
" <td>37.1</td>\n",
" <td>58.2</td>\n",
" <td>23.4</td>\n",
" <td>...</td>\n",
" <td>87.4</td>\n",
" <td>68.9</td>\n",
" <td>59.3</td>\n",
" <td>49.3</td>\n",
" <td>32.5</td>\n",
" <td>35.10</td>\n",
" <td>30.6</td>\n",
" <td>14.7</td>\n",
" <td>46.2</td>\n",
" <td>37.50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>4.4</td>\n",
" <td>47.9</td>\n",
" <td>12.7</td>\n",
" <td>41.7</td>\n",
" <td>2.9</td>\n",
" <td>7.0</td>\n",
" <td>57.6</td>\n",
" <td>11.9</td>\n",
" <td>60.5</td>\n",
" <td>47.5</td>\n",
" <td>...</td>\n",
" <td>71.2</td>\n",
" <td>37.0</td>\n",
" <td>61.5</td>\n",
" <td>33.1</td>\n",
" <td>8.6</td>\n",
" <td>3.20</td>\n",
" <td>7.6</td>\n",
" <td>20.5</td>\n",
" <td>14.3</td>\n",
" <td>5.60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>3.4</td>\n",
" <td>49.8</td>\n",
" <td>9.4</td>\n",
" <td>43.6</td>\n",
" <td>4.8</td>\n",
" <td>3.6</td>\n",
" <td>59.5</td>\n",
" <td>11.3</td>\n",
" <td>62.4</td>\n",
" <td>49.4</td>\n",
" <td>...</td>\n",
" <td>73.1</td>\n",
" <td>33.6</td>\n",
" <td>63.4</td>\n",
" <td>35.0</td>\n",
" <td>10.5</td>\n",
" <td>0.13</td>\n",
" <td>9.5</td>\n",
" <td>22.4</td>\n",
" <td>5.6</td>\n",
" <td>2.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>9.8</td>\n",
" <td>56.2</td>\n",
" <td>4.1</td>\n",
" <td>50.0</td>\n",
" <td>11.2</td>\n",
" <td>8.9</td>\n",
" <td>65.9</td>\n",
" <td>17.7</td>\n",
" <td>68.8</td>\n",
" <td>55.8</td>\n",
" <td>...</td>\n",
" <td>79.5</td>\n",
" <td>27.4</td>\n",
" <td>69.8</td>\n",
" <td>41.4</td>\n",
" <td>16.9</td>\n",
" <td>6.60</td>\n",
" <td>16.0</td>\n",
" <td>28.9</td>\n",
" <td>5.6</td>\n",
" <td>4.70</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>21.0</td>\n",
" <td>32.2</td>\n",
" <td>29.3</td>\n",
" <td>26.0</td>\n",
" <td>17.4</td>\n",
" <td>23.6</td>\n",
" <td>41.9</td>\n",
" <td>21.8</td>\n",
" <td>44.8</td>\n",
" <td>31.9</td>\n",
" <td>...</td>\n",
" <td>72.1</td>\n",
" <td>53.6</td>\n",
" <td>45.8</td>\n",
" <td>34.0</td>\n",
" <td>16.2</td>\n",
" <td>19.80</td>\n",
" <td>15.3</td>\n",
" <td>4.9</td>\n",
" <td>5.6</td>\n",
" <td>22.20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>74.2</td>\n",
" <td>102.0</td>\n",
" <td>82.5</td>\n",
" <td>95.8</td>\n",
" <td>70.5</td>\n",
" <td>76.7</td>\n",
" <td>112.0</td>\n",
" <td>74.9</td>\n",
" <td>115.0</td>\n",
" <td>102.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>107.0</td>\n",
" <td>118.0</td>\n",
" <td>36.5</td>\n",
" <td>69.3</td>\n",
" <td>72.90</td>\n",
" <td>68.4</td>\n",
" <td>74.6</td>\n",
" <td>84.0</td>\n",
" <td>75.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>51.5</td>\n",
" <td>83.5</td>\n",
" <td>59.8</td>\n",
" <td>78.1</td>\n",
" <td>47.8</td>\n",
" <td>36.2</td>\n",
" <td>93.1</td>\n",
" <td>52.3</td>\n",
" <td>96.8</td>\n",
" <td>83.1</td>\n",
" <td>...</td>\n",
" <td>103.0</td>\n",
" <td>0.0</td>\n",
" <td>15.1</td>\n",
" <td>69.4</td>\n",
" <td>46.7</td>\n",
" <td>34.60</td>\n",
" <td>45.7</td>\n",
" <td>56.9</td>\n",
" <td>61.3</td>\n",
" <td>31.90</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>64.5</td>\n",
" <td>71.7</td>\n",
" <td>72.8</td>\n",
" <td>65.5</td>\n",
" <td>60.9</td>\n",
" <td>67.1</td>\n",
" <td>81.4</td>\n",
" <td>65.3</td>\n",
" <td>84.2</td>\n",
" <td>71.3</td>\n",
" <td>...</td>\n",
" <td>116.0</td>\n",
" <td>97.1</td>\n",
" <td>0.0</td>\n",
" <td>77.5</td>\n",
" <td>59.7</td>\n",
" <td>63.30</td>\n",
" <td>58.8</td>\n",
" <td>46.5</td>\n",
" <td>74.3</td>\n",
" <td>65.70</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>36.1</td>\n",
" <td>63.9</td>\n",
" <td>44.3</td>\n",
" <td>57.7</td>\n",
" <td>32.4</td>\n",
" <td>38.6</td>\n",
" <td>73.6</td>\n",
" <td>36.8</td>\n",
" <td>76.5</td>\n",
" <td>63.5</td>\n",
" <td>...</td>\n",
" <td>36.5</td>\n",
" <td>68.6</td>\n",
" <td>77.5</td>\n",
" <td>0.0</td>\n",
" <td>31.2</td>\n",
" <td>34.80</td>\n",
" <td>30.3</td>\n",
" <td>36.5</td>\n",
" <td>45.9</td>\n",
" <td>37.20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>11.9</td>\n",
" <td>46.1</td>\n",
" <td>20.2</td>\n",
" <td>39.9</td>\n",
" <td>8.3</td>\n",
" <td>14.1</td>\n",
" <td>55.8</td>\n",
" <td>12.7</td>\n",
" <td>58.7</td>\n",
" <td>45.7</td>\n",
" <td>...</td>\n",
" <td>69.4</td>\n",
" <td>44.4</td>\n",
" <td>59.7</td>\n",
" <td>31.3</td>\n",
" <td>0.0</td>\n",
" <td>10.70</td>\n",
" <td>0.9</td>\n",
" <td>18.7</td>\n",
" <td>21.7</td>\n",
" <td>12.70</td>\n",
" </tr>\n",
" <tr>\n",
" <th>31</th>\n",
" <td>3.3</td>\n",
" <td>49.7</td>\n",
" <td>9.5</td>\n",
" <td>43.5</td>\n",
" <td>4.7</td>\n",
" <td>3.8</td>\n",
" <td>59.4</td>\n",
" <td>11.2</td>\n",
" <td>62.2</td>\n",
" <td>49.3</td>\n",
" <td>...</td>\n",
" <td>72.9</td>\n",
" <td>33.8</td>\n",
" <td>63.3</td>\n",
" <td>34.8</td>\n",
" <td>10.3</td>\n",
" <td>0.00</td>\n",
" <td>9.4</td>\n",
" <td>22.3</td>\n",
" <td>11.1</td>\n",
" <td>2.40</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32</th>\n",
" <td>11.2</td>\n",
" <td>45.2</td>\n",
" <td>19.4</td>\n",
" <td>39.1</td>\n",
" <td>7.5</td>\n",
" <td>13.2</td>\n",
" <td>54.9</td>\n",
" <td>11.9</td>\n",
" <td>57.9</td>\n",
" <td>44.8</td>\n",
" <td>...</td>\n",
" <td>68.6</td>\n",
" <td>43.7</td>\n",
" <td>59.0</td>\n",
" <td>30.5</td>\n",
" <td>0.9</td>\n",
" <td>9.90</td>\n",
" <td>0.0</td>\n",
" <td>18.0</td>\n",
" <td>21.0</td>\n",
" <td>11.80</td>\n",
" </tr>\n",
" <tr>\n",
" <th>33</th>\n",
" <td>23.5</td>\n",
" <td>32.9</td>\n",
" <td>31.8</td>\n",
" <td>26.7</td>\n",
" <td>19.9</td>\n",
" <td>26.1</td>\n",
" <td>42.6</td>\n",
" <td>24.3</td>\n",
" <td>45.5</td>\n",
" <td>19.8</td>\n",
" <td>...</td>\n",
" <td>74.6</td>\n",
" <td>56.1</td>\n",
" <td>46.5</td>\n",
" <td>36.5</td>\n",
" <td>18.7</td>\n",
" <td>22.30</td>\n",
" <td>17.8</td>\n",
" <td>0.0</td>\n",
" <td>33.4</td>\n",
" <td>24.70</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34</th>\n",
" <td>14.4</td>\n",
" <td>60.8</td>\n",
" <td>4.5</td>\n",
" <td>54.6</td>\n",
" <td>15.7</td>\n",
" <td>13.5</td>\n",
" <td>70.4</td>\n",
" <td>22.2</td>\n",
" <td>73.3</td>\n",
" <td>60.4</td>\n",
" <td>...</td>\n",
" <td>84.0</td>\n",
" <td>31.9</td>\n",
" <td>74.4</td>\n",
" <td>45.9</td>\n",
" <td>21.4</td>\n",
" <td>11.10</td>\n",
" <td>20.5</td>\n",
" <td>33.4</td>\n",
" <td>0.0</td>\n",
" <td>9.20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>35</th>\n",
" <td>5.7</td>\n",
" <td>52.1</td>\n",
" <td>7.7</td>\n",
" <td>45.9</td>\n",
" <td>7.1</td>\n",
" <td>3.6</td>\n",
" <td>61.8</td>\n",
" <td>13.6</td>\n",
" <td>64.6</td>\n",
" <td>51.7</td>\n",
" <td>...</td>\n",
" <td>75.3</td>\n",
" <td>31.9</td>\n",
" <td>65.7</td>\n",
" <td>37.2</td>\n",
" <td>12.7</td>\n",
" <td>2.40</td>\n",
" <td>11.8</td>\n",
" <td>24.7</td>\n",
" <td>9.2</td>\n",
" <td>0.00</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>36 rows × 36 columns</p>\n",
"</div>"
],
"text/plain": [
" Pantai BUL BUL BUKIT travel Gibeon Pakkodian Taman Eden 100 Tobasa \\\n",
"0 0.0 50.9 12.8 44.7 \n",
"1 50.9 0.0 59.2 6.2 \n",
"2 12.8 59.2 0.0 53.0 \n",
"3 44.7 6.2 53.0 0.0 \n",
"4 5.9 47.3 14.2 41.1 \n",
"5 7.1 53.5 11.1 47.2 \n",
"6 60.6 12.0 68.9 15.9 \n",
"7 12.4 51.7 20.7 45.5 \n",
"8 62.5 16.6 71.8 17.8 \n",
"9 50.5 18.6 58.8 12.3 \n",
"10 52.7 20.7 61.0 14.5 \n",
"11 43.1 50.3 51.4 44.0 \n",
"12 51.9 59.0 60.2 52.8 \n",
"13 56.3 7.7 64.6 11.6 \n",
"14 6.0 52.5 8.0 46.2 \n",
"15 11.8 58.2 4.1 52.0 \n",
"16 14.6 61.0 4.7 54.8 \n",
"17 60.7 67.9 74.2 61.7 \n",
"18 9.0 55.5 15.4 49.2 \n",
"19 42.4 8.7 50.7 2.4 \n",
"20 6.7 53.1 8.7 46.9 \n",
"21 36.3 45.7 44.6 39.5 \n",
"22 4.4 47.9 12.7 41.7 \n",
"23 3.4 49.8 9.4 43.6 \n",
"24 9.8 56.2 4.1 50.0 \n",
"25 21.0 32.2 29.3 26.0 \n",
"26 74.2 102.0 82.5 95.8 \n",
"27 51.5 83.5 59.8 78.1 \n",
"28 64.5 71.7 72.8 65.5 \n",
"29 36.1 63.9 44.3 57.7 \n",
"30 11.9 46.1 20.2 39.9 \n",
"31 3.3 49.7 9.5 43.5 \n",
"32 11.2 45.2 19.4 39.1 \n",
"33 23.5 32.9 31.8 26.7 \n",
"34 14.4 60.8 4.5 54.6 \n",
"35 5.7 52.1 7.7 45.9 \n",
"\n",
" Water Park Tambunan Bukit Pahoda Bukit Senyum Air Terjun Pandumaan \\\n",
"0 5.9 71.0 60.6 12.4 \n",
"1 47.3 53.5 12.0 51.4 \n",
"2 14.2 11.9 68.9 20.7 \n",
"3 41.1 47.2 15.9 45.5 \n",
"4 0.0 8.4 57.0 13.8 \n",
"5 8.4 0.0 63.2 14.9 \n",
"6 57.0 63.1 0.0 61.4 \n",
"7 11.3 14.9 61.4 0.0 \n",
"8 58.8 66.0 5.5 63.3 \n",
"9 46.9 53.1 28.2 51.3 \n",
"10 49.0 55.2 30.4 53.5 \n",
"11 39.4 45.6 59.9 43.8 \n",
"12 48.2 54.4 68.7 52.8 \n",
"13 52.7 58.8 10.8 57.1 \n",
"14 7.4 3.3 62.1 13.9 \n",
"15 13.2 10.9 67.9 19.6 \n",
"16 16.0 13.7 70.7 22.5 \n",
"17 57.0 68.5 82.8 61.5 \n",
"18 10.4 9.6 65.1 15.0 \n",
"19 38.8 44.9 18.3 43.2 \n",
"20 8.1 2.8 62.9 14.6 \n",
"21 32.7 38.9 55.4 37.1 \n",
"22 2.9 7.0 57.6 11.9 \n",
"23 4.8 3.6 59.5 11.3 \n",
"24 11.2 8.9 65.9 17.7 \n",
"25 17.4 23.6 41.9 21.8 \n",
"26 70.5 76.7 112.0 74.9 \n",
"27 47.8 36.2 93.1 52.3 \n",
"28 60.9 67.1 81.4 65.3 \n",
"29 32.4 38.6 73.6 36.8 \n",
"30 8.3 14.1 55.8 12.7 \n",
"31 4.7 3.8 59.4 11.2 \n",
"32 7.5 13.2 54.9 11.9 \n",
"33 19.9 26.1 42.6 24.3 \n",
"34 15.7 13.5 70.4 22.2 \n",
"35 7.1 3.6 61.8 13.6 \n",
"\n",
" Long Beach PANTAI AGADON ... Dolok Surungan Air Terjun Sampuran \\\n",
"0 63.5 50.5 ... 74.2 37.1 \n",
"1 17.6 18.6 ... 102.0 83.5 \n",
"2 71.8 58.8 ... 82.5 30.4 \n",
"3 18.8 12.3 ... 95.8 77.2 \n",
"4 59.8 46.9 ... 70.5 38.4 \n",
"5 66.0 53.1 ... 76.7 36.2 \n",
"6 5.5 28.2 ... 112.0 93.1 \n",
"7 64.2 51.3 ... 74.9 44.9 \n",
"8 0.0 31.1 ... 114.0 95.0 \n",
"9 31.1 0.0 ... 102.0 83.1 \n",
"10 33.2 5.7 ... 104.0 85.2 \n",
"11 62.8 49.9 ... 94.2 75.6 \n",
"12 71.6 58.6 ... 103.0 84.4 \n",
"13 23.0 23.9 ... 107.0 88.8 \n",
"14 65.0 52.0 ... 75.7 32.3 \n",
"15 70.7 57.8 ... 81.4 29.3 \n",
"16 73.6 60.6 ... 84.3 32.1 \n",
"17 80.4 72.7 ... 112.0 93.2 \n",
"18 68.0 55.1 ... 78.7 39.6 \n",
"19 21.2 10.0 ... 93.5 74.9 \n",
"20 65.7 52.8 ... 76.4 32.9 \n",
"21 58.2 23.4 ... 87.4 68.9 \n",
"22 60.5 47.5 ... 71.2 37.0 \n",
"23 62.4 49.4 ... 73.1 33.6 \n",
"24 68.8 55.8 ... 79.5 27.4 \n",
"25 44.8 31.9 ... 72.1 53.6 \n",
"26 115.0 102.0 ... 0.0 107.0 \n",
"27 96.8 83.1 ... 103.0 0.0 \n",
"28 84.2 71.3 ... 116.0 97.1 \n",
"29 76.5 63.5 ... 36.5 68.6 \n",
"30 58.7 45.7 ... 69.4 44.4 \n",
"31 62.2 49.3 ... 72.9 33.8 \n",
"32 57.9 44.8 ... 68.6 43.7 \n",
"33 45.5 19.8 ... 74.6 56.1 \n",
"34 73.3 60.4 ... 84.0 31.9 \n",
"35 64.6 51.7 ... 75.3 31.9 \n",
"\n",
" Air Terjun Morena Tornagodang Hatulian Beach Monumen Raja Sonakmalela \\\n",
"0 64.5 36.1 11.6 3.30 \n",
"1 71.7 63.9 46.1 49.70 \n",
"2 72.8 44.4 19.9 9.50 \n",
"3 65.5 57.7 39.9 43.50 \n",
"4 60.9 32.4 7.9 4.70 \n",
"5 67.1 38.6 14.1 3.80 \n",
"6 81.4 73.6 55.8 59.40 \n",
"7 65.3 36.8 12.8 11.20 \n",
"8 83.3 75.5 57.7 61.30 \n",
"9 71.3 63.5 45.7 49.30 \n",
"10 73.5 65.7 47.9 51.50 \n",
"11 26.2 56.1 38.2 41.80 \n",
"12 15.5 64.9 47.0 50.60 \n",
"13 77.1 69.3 51.5 55.10 \n",
"14 66.0 37.6 13.1 2.80 \n",
"15 71.8 43.3 18.8 8.50 \n",
"16 74.6 46.2 21.7 11.40 \n",
"17 9.7 73.7 55.9 59.50 \n",
"18 69.0 40.6 16.1 5.90 \n",
"19 63.2 55.4 37.6 41.20 \n",
"20 66.7 38.3 13.8 3.40 \n",
"21 59.3 49.3 32.5 35.10 \n",
"22 61.5 33.1 8.6 3.20 \n",
"23 63.4 35.0 10.5 0.13 \n",
"24 69.8 41.4 16.9 6.60 \n",
"25 45.8 34.0 16.2 19.80 \n",
"26 118.0 36.5 69.3 72.90 \n",
"27 15.1 69.4 46.7 34.60 \n",
"28 0.0 77.5 59.7 63.30 \n",
"29 77.5 0.0 31.2 34.80 \n",
"30 59.7 31.3 0.0 10.70 \n",
"31 63.3 34.8 10.3 0.00 \n",
"32 59.0 30.5 0.9 9.90 \n",
"33 46.5 36.5 18.7 22.30 \n",
"34 74.4 45.9 21.4 11.10 \n",
"35 65.7 37.2 12.7 2.40 \n",
"\n",
" Lumban Binanga Beach Pantai Pasifik Porsea Desa Adat Ragi Hotang \\\n",
"0 10.7 22.5 14.4 \n",
"1 45.2 32.9 60.8 \n",
"2 18.9 31.8 4.5 \n",
"3 39.0 26.7 54.5 \n",
"4 7.0 19.9 15.7 \n",
"5 13.2 26.1 13.5 \n",
"6 54.9 42.6 70.4 \n",
"7 11.9 24.3 22.2 \n",
"8 56.8 44.5 72.3 \n",
"9 44.8 19.8 60.4 \n",
"10 47.0 34.7 62.5 \n",
"11 37.3 25.1 52.9 \n",
"12 46.1 33.8 61.7 \n",
"13 50.6 38.3 66.1 \n",
"14 12.2 25.1 9.6 \n",
"15 17.9 30.8 5.6 \n",
"16 20.8 33.7 0.6 \n",
"17 55.0 42.7 70.5 \n",
"18 15.2 28.1 16.9 \n",
"19 36.7 24.4 52.2 \n",
"20 12.9 25.7 10.2 \n",
"21 30.6 14.7 46.2 \n",
"22 7.6 20.5 14.3 \n",
"23 9.5 22.4 5.6 \n",
"24 16.0 28.9 5.6 \n",
"25 15.3 4.9 5.6 \n",
"26 68.4 74.6 84.0 \n",
"27 45.7 56.9 61.3 \n",
"28 58.8 46.5 74.3 \n",
"29 30.3 36.5 45.9 \n",
"30 0.9 18.7 21.7 \n",
"31 9.4 22.3 11.1 \n",
"32 0.0 18.0 21.0 \n",
"33 17.8 0.0 33.4 \n",
"34 20.5 33.4 0.0 \n",
"35 11.8 24.7 9.2 \n",
"\n",
" Makam Raja Sisingamangaraja XII \n",
"0 5.70 \n",
"1 52.10 \n",
"2 7.70 \n",
"3 45.90 \n",
"4 7.10 \n",
"5 4.00 \n",
"6 61.80 \n",
"7 13.60 \n",
"8 64.60 \n",
"9 51.70 \n",
"10 53.80 \n",
"11 44.20 \n",
"12 53.00 \n",
"13 57.50 \n",
"14 0.35 \n",
"15 6.60 \n",
"16 9.50 \n",
"17 67.10 \n",
"18 8.30 \n",
"19 43.60 \n",
"20 2.00 \n",
"21 37.50 \n",
"22 5.60 \n",
"23 2.30 \n",
"24 4.70 \n",
"25 22.20 \n",
"26 75.30 \n",
"27 31.90 \n",
"28 65.70 \n",
"29 37.20 \n",
"30 12.70 \n",
"31 2.40 \n",
"32 11.80 \n",
"33 24.70 \n",
"34 9.20 \n",
"35 0.00 \n",
"\n",
"[36 rows x 36 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"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": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 23, 10, 22, 24, 5, 15]"
]
},
"execution_count": 6,
"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": 7,
"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": "markdown",
"metadata": {},
"source": [
"# PSO"
]
},
{
"cell_type": "code",
"execution_count": 267,
"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": 333,
"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": 346,
"metadata": {},
"outputs": [],
"source": [
"class City:\n",
" def __init__(self):\n",
" self.mX = 0\n",
" self.mY = 0 \n",
"def get_distance(firstCity,secondCity):\n",
" distance_route = []\n",
" last_distance = 0\n",
" distance = 0\n",
" for i in range(0,6):\n",
" source = particles[i].get_data(i)\n",
" target = particles[i].get_data(i+1)\n",
" distance_route.append(Data2.iloc[source][target])\n",
" for i in range(0,6):\n",
" source = particles[i].get_data(i)\n",
" target = particles[i].get_data(i)\n",
" last_distance = Data1.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": 347,
"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": 348,
"metadata": {},
"outputs": [],
"source": [
"def randomly_arrange(index = 0):\n",
" cityA = random.randrange(0, CITY_COUNT)\n",
" cityB = 0\n",
" done = False\n",
" while not done:\n",
" cityB = random.randrange(0, CITY_COUNT)\n",
" if cityB != cityA:\n",
" done = \tTrue \n",
" # swap cityA and cityB.\n",
" temp = particles[index].get_data(cityA)\n",
" particles[index].set_data(cityA, particles[index].get_data(cityB))\n",
" particles[index].set_data(cityB, temp)\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 349,
"metadata": {},
"outputs": [],
"source": [
"def initialize_particles():\n",
" for i in range(PARTICLE_COUNT):\n",
" newParticle = Particle() \n",
" for j in range(CITY_COUNT):\n",
" newParticle.set_data(j, j) \n",
" particles.append(newParticle) \n",
" for j in range(10): # just any number of times to randomize them.\n",
" randomly_arrange(len(particles) - 1) \n",
" get_total_distance(len(particles) - 1)\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 350,
"metadata": {},
"outputs": [],
"source": [
"def quicksort(array, left, right):\n",
" pivot = quicksort_partition(array, left, right) \n",
" if left < pivot:\n",
" quicksort(array, left, pivot - 1)\n",
" \n",
" if right > pivot:\n",
" quicksort(array, pivot + 1, right) \n",
" return array"
]
},
{
"cell_type": "code",
"execution_count": 351,
"metadata": {},
"outputs": [],
"source": [
"def quicksort_partition(numbers, left, right):\n",
" # The comparison is on each particle's pBest value.\n",
" I_hold = left\n",
" r_hold = right\n",
" pivot = numbers[left] \n",
" while left < right:\n",
" while (numbers[right].get_pBest_distance() >= pivot.get_pBest_distance()) and (left < right):\n",
" right -= 1\n",
"\n",
" if left != right:\n",
" numbers[left] = numbers[right]\n",
" left += 1\n",
" \n",
" while (numbers[left].get_pBest_distance() <= pivot.get_pBest_distance()) and (left < right):\n",
" left += 1 \n",
" if left != right:\n",
" numbers[right] = numbers[left]\n",
" right -= 1 \n",
" numbers[left] = pivot\n",
" pivot = left\n",
" left = I_hold\n",
" right = r_hold\n",
" \n",
" return pivot"
]
},
{
"cell_type": "code",
"execution_count": 352,
"metadata": {},
"outputs": [],
"source": [
"def get_velocity():\n",
" worstResults_distance = 0.0\n",
" vValue_distance = 0.0 \n",
" # After sorting, worst will be last in list.\n",
" worstResults_distance = particles[PARTICLE_COUNT - 1].get_pBest_distance() \n",
" for i in range(PARTICLE_COUNT):\n",
" vValue_distance = (Acceleration_constant * particles[i].get_pBest_distance()) / worstResults_distance \n",
" if (vValue_distance > Acceleration_constant): \n",
" particles[i].set_velocity(Acceleration_constant)\n",
" elif (vValue_distance < 0.0):\n",
" particles[i].set_velocity(0.0)\n",
" else:\n",
" particles[i].set_velocity(vValue_distance) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 353,
"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(Data4)):\n",
" if particles[source].get_data(i) == targetA:\n",
" if i == CITY_COUNT - 1:\n",
" targetB = particles[source].get_data(0) # if end of array, take from beginning.\n",
" else:\n",
" targetB = particles[source].get_data(i + 1)\n",
" \n",
" break\n",
" \n",
" # Move targetB next to targetA by switching values.\n",
" for j in range(CITY_COUNT):\n",
" if particles[destination].get_data(j) == targetA:\n",
" indexA = j\n",
" \n",
" if particles[destination].get_data(j) == targetB:\n",
" indexB = j \n",
" # get temp index succeeding indexA.\n",
" if indexA == CITY_COUNT - 1:\n",
" tempIndex = 0\n",
" else:\n",
" tempIndex = indexA + 1\n",
" \n",
" # Switch indexB value with tempIndex value.\n",
" temp = particles[destination].get_data(tempIndex)\n",
" particles[destination].set_data(tempIndex, particles[destination].get_data(indexB))\n",
" particles[destination].set_data(indexB, temp) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 325,
"metadata": {},
"outputs": [],
"source": [
"# updating particles city\n",
"def update_particles():\n",
" # Best was previously sorted to index 0, so start from the second best.\n",
" for i in range(5):\n",
" if i > 0:\n",
" # The higher the velocity score, the more changes it will need.\n",
" changes = math.floor(math.fabs(particles[i].get_velocity()))\n",
" sys.stdout.write(\"Changes for particle \" + str(i) + \": \" + str(changes) + \"\\n\")\n",
" for j in range(changes):\n",
" # 50/50 chance.\n",
" if random.random() > 0.5:\n",
" randomly_arrange(i)\n",
" \n",
" # Push it closer to it's best neighbor.\n",
" copy_from_particle(i - 1, i) \n",
" # Update pBest value.\n",
" get_total_distance(i) \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 326,
"metadata": {},
"outputs": [],
"source": [
"def PSO_algorithm():\n",
" epoch = 0\n",
" done = False\n",
" \n",
" initialize_particles() \n",
" while not done:\n",
" # Two conditions can end this loop:\n",
" # if the maximum number of epochs allowed has been reached, or,\n",
" # if the Target value has been found.\n",
" if epoch < MAX_EPOCHS:\n",
" for i in range(5): \n",
" sys.stdout.write(\"Route: \") \n",
" for j in range(CITY_COUNT):\n",
" sys.stdout.write(str(particles[i].get_data(j)) + \", \")\n",
" get_total_distance(i) \n",
" sys.stdout.write(\"Distance: \" + str(particles[i].get_pBest_distance()) + \"\\n\") \n",
" if (particles[i].get_pBest_distance() <= Maximum_distance):\n",
" done = True \n",
" quicksort(particles, 0, len(particles) - 1)\n",
" # list has to sorted in order for get_velocity() to work.\n",
" get_velocity() \n",
" update_particles() \n",
" sys.stdout.write(\"epoch number: \" + str(epoch) + \"\\n\")\n",
" \n",
" epoch += 1 \n",
" else:\n",
" done = True\n",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 327,
"metadata": {},
"outputs": [],
"source": [
"def print_best_solution():\n",
" if (particles[0].get_pBest_distance() <= Maximum_distance):\n",
" sys.stdout.write(\"Target reached.\\n\")\n",
" else:\n",
" sys.stdout.write(\"Target not reached.\\n\") \n",
" sys.stdout.write(\"Best Route: \")\n",
" for j in range(CITY_COUNT):\n",
" sys.stdout.write(str(particles[0].get_data(j)) + \", \") \n",
" sys.stdout.write(\"Distance: \" + str(particles[0].get_pBest_distance()) +\"\\n\")\n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 328,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Route: 2, 0, 1, 4, 6, 5, 3, Distance: 392.0\n",
"Route: 2, 4, 5, 1, 6, 0, 3, Distance: 392.0\n",
"Route: 2, 4, 5, 0, 1, 6, 3, Distance: 392.0\n",
"Route: 2, 6, 1, 0, 4, 5, 3, Distance: 392.0\n",
"Route: 6, 5, 3, 1, 0, 4, 2, Distance: 392.0\n",
"Changes for particle 1: 2\n",
"Changes for particle 2: 2\n",
"Changes for particle 3: 2\n",
"Changes for particle 4: 2\n",
"epoch number: 0\n",
"Target reached.\n",
"Best Route: 2, 0, 1, 4, 6, 5, 3, Distance: 392.0\n"
]
}
],
"source": [
" if __name__ == '__main__':\n",
" initialize_map()\n",
" PSO_algorithm()\n",
" print_best_solution() "
]
},
{
"cell_type": "code",
"execution_count": 221,
"metadata": {},
"outputs": [],
"source": [
"def checking_city():\n",
" Route = []\n",
" if (particles[0].get_pBest_distance() <= Maximum_distance):\n",
" for j in range(CITY_COUNT):\n",
" Route.append(particles[0].get_data(j))\n",
" return Route"
]
},
{
"cell_type": "code",
"execution_count": 223,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 223,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Route= checking_city()\n",
"Route"
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {},
"outputs": [],
"source": [
"def decoding(Route):\n",
" name = 0\n",
" for i in range(len(Route)):\n",
" name = Route[i]\n",
" print(Data5.iloc[name][3])\n",
" i+=1\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 225,
"metadata": {},
"outputs": [],
"source": [
"decoding(Route)"
]
},
{
"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