Import Packages: Pip Install Pants
Import Packages: Pip Install Pants
Import Packages: Pip Install Pants
Import Packages
pip install pants
#import packages
import pants
import math
import random
2. Input
length function to the algorithm this is able to calculate the distances from node 𝑖
The input for ACOPants is a list of coordinates (x, y) of the nodes, and providing a
to 𝑗.
Here we have a csv .file that contains information about cities all around the world from the
webpage: http://simplemaps.com/data/world-cities. We will work with the cities from India and
with the coordinates in decimal degrees (lat and lng).
import pandas as pd
import numpy as np
cities = pd.read_csv('worldcities.csv')
INDIAcities = cities.loc[cities['country'] == 'India'] #only the
cities that belong to INDIA
print('Dimention INDIAcities:', INDIAcities.shape) #dimention of
INDIAcities dataset
Hyper Parameters
Optional arguments: -a A, --alpha A relative importance placed on pheromones; default=1 -b B,
--beta B relative importance placed on distances; default=3 -l L, --limit L number of iterations to
perform; default=100 -p P, --rho P ratio of evaporated pheromone (0 <= P <= 1); default=0.8 -e
E, --elite E ratio of elite ant's pheromone; default=0.5 -q Q, --Q Q total pheromone capacity of
each ant (Q > 0); default=1 -t T, --t0 T initial amount of pheromone on every edge (T > 0);
default=0.01 -c N, --count N number of ants used in each iteration (N > 0); default=10
Arguments are very important and they can affect the result. Usually, it is used as many number
of ants (N) as nodes. Also, is better to use a higher value of beta(distance) than
beta(pheromone).
DISTANCE: 129.0580588554432
[(26.6128, 89.825), (25.8738, 87.9637), (25.8564, 87.9124), (25.8723,
87.8978), (22.4156, 88.3051), (22.2194, 88.2142), (22.22, 88.17),
(22.6236, 87.9195), (22.2847, 88.4053), (22.1745, 88.4184), (22.3465,
88.9167), (22.5475, 88.6606), (22.65, 88.29), (22.737, 88.1918),
(22.81, 88.23), (22.79, 88.32), (23.5984, 88.0871), (23.25, 87.85),
(23.2091, 87.6983), (23.3427, 87.6885), (23.8, 87.38), (23.8229,
86.9839), (22.8, 86.95), (23.5142, 86.498), (23.37, 85.97), (23.37,
85.9), (24.4663, 87.9022), (24.3841, 87.884), (24.4724, 87.9589),
(24.1472, 87.8802), (24.2463, 87.8509), (24.0912, 88.4947), (20.17,
85.7), (19.32, 84.8), (17.0185, 82.2349), (17.08, 82.13), (17.2333,
82.2), (17.605, 82.408), (17.67, 82.62), (16.93, 81.63), (16.95,
80.7833), (17.0331, 80.8056), (16.8991, 81.1764), (16.801, 80.63),
(16.5167, 80.6167), (16.3785, 80.6146), (16.3036, 80.6172), (16.517,
81.988), (16.4814, 81.6267), (16.5333, 81.5333), (16.6, 81.4667),
(16.0667, 80.5667), (15.9806, 80.6347), (15.9333, 80.55), (15.994,
80.378), (15.298, 80.034), (14.5942, 80.0297), (14.8667, 79.3167),
(13.3667, 79.1833), (13.514, 78.227), (13.85, 78.2667), (13.8408,
78.3056), (13.7611, 78.425), (14.1667, 78.7), (14.4167, 78.2333),
(13.8032, 77.6097), (13.9369, 77.2694), (13.3667, 78.4333), (12.4972,
76.8878), (12.4181, 76.6947), (11.65, 78.1667), (10.8, 79.15),
(8.7833, 78.1333), (16.5787, 79.8756), (16.2372, 79.8464), (17.1528,
79.6861), (17.2, 80.55), (16.892, 80.287), (16.6167, 77.85), (17.1447,
78.2886), (17.8517, 78.6828), (18.3597, 79.0875), (18.4667, 78.8833),
(18.83, 79.45), (17.9756, 79.6011), (16.2833, 78.5167), (16.2319,
76.9553), (16.0528, 76.8877), (14.6, 74.8333), (15.48, 73.83),
(15.6294, 73.7358), (17.72, 73.38), (20.9, 74.7833), (21.45, 80.2),
(18.7667, 84.1667), (28.45, 77.02), (29.5833, 74.3167), (30.6167,
74.8), (30.9083, 75.8486), (34.4225, 74.6375)]
#To get the names of the cities visited from the nodes values:
Indiancities.set_index(['lat','lng'])['city'].loc[tour].tolist()
['Kumārgrām',
'Gopālpur',
'Rānīganj',
'Bagela',
'Amgachia',
'Pātra',
'Amtala',
'Jhikra',
'Multi',
'Jaynagar-Majilpur',
'Khulna',
'Kamargani',
'Jagdispur',
'Mashāt',
'Singur',
'Baidyabāti',
'Srikhanda',
'Barddhamān',
'Khandaghosh',
'Galsi',
'Dubrājpur',
'Pānuria',
'Raipur',
'Pāra',
'Jhalidā',
'Tūlin',
'Kāthia',
'Rudra Nagar',
'Jājigrām',
'Baswa',
'Mehegrām',
'Bhagirathpur',
'Jatani',
'Brahmapur',
'Panasapādu',
'Peddāpuram',
'Prattipādu',
'Nātavaram',
'Narsīpatnam',
'Kalavalapalle',
'Vissannapeta',
'Channubanda',
'Tadikalapūdi',
'Chandragūdem',
'Bezwāda',
'Chiluvūru',
'Kolakalūru',
'Bandamūrlanka',
'Matsyapuri',
'Bhīmavaram',
'Undi',
'Ponnūru',
'Pittalavānipālem',
'Karlapālem',
'Cherukūru',
'Mūlaguntapādu',
'Vidavalūru',
'Udayagiri',
'Penumūr',
'Gavunipalli',
'Somapalle',
'Mulakalacheruvu',
'Kosuvāripalle',
'Lakkireddipalle',
'Pulivendla',
'Lepākshi',
'Madakasīra',
'Rāmasamudram',
'Sante Kasalgere',
'Shrīrangapattana',
'Salem',
'Tanjore',
'Tuticorin',
'Kottapālem',
'Muppālla',
'Chiwemla',
'Kallūr',
'Anigandlapādu',
'Devarkadra',
'Kotūr',
'Gajwel',
'Timmāpuram',
'Vemalwāda',
'Nāspur',
'Warangal',
'Lingāl',
'Kyādgeri',
'Bāgalvād',
'Kālkuni',
'Panaji',
'Morgim',
'Khed',
'Dhūlia',
'Gondiā',
'Meliyāputtu',
'Gurgaon',
'Hanumāngarh',
'Sandhwān',
'Ludhiāna',
'Bandipura']
DISTANCE: 136.13490737264505
[(23.5142, 86.498), (23.8229, 86.9839), (23.37, 85.9), (23.37, 85.97),
(23.5984, 88.0871), (23.8, 87.38), (22.8, 86.95), (22.6236, 87.9195),
(22.4156, 88.3051), (22.65, 88.29), (22.2847, 88.4053), (22.1745,
88.4184), (22.2194, 88.2142), (22.22, 88.17), (24.0912, 88.4947),
(24.4724, 87.9589), (24.4663, 87.9022), (24.2463, 87.8509), (24.3841,
87.884), (24.1472, 87.8802), (23.25, 87.85), (23.2091, 87.6983),
(23.3427, 87.6885), (25.8738, 87.9637), (25.8723, 87.8978), (25.8564,
87.9124), (22.79, 88.32), (22.81, 88.23), (22.737, 88.1918), (22.5475,
88.6606), (22.3465, 88.9167), (19.32, 84.8), (18.7667, 84.1667),
(17.67, 82.62), (17.2333, 82.2), (17.08, 82.13), (17.0185, 82.2349),
(17.605, 82.408), (16.0667, 80.5667), (15.9333, 80.55), (16.3785,
80.6146), (16.3036, 80.6172), (16.5167, 80.6167), (16.801, 80.63),
(16.95, 80.7833), (17.0331, 80.8056), (17.2, 80.55), (16.892, 80.287),
(17.1528, 79.6861), (17.8517, 78.6828), (18.4667, 78.8833), (18.3597,
79.0875), (18.83, 79.45), (17.9756, 79.6011), (15.994, 80.378),
(15.9806, 80.6347), (16.8991, 81.1764), (16.93, 81.63), (16.4814,
81.6267), (16.5333, 81.5333), (16.6, 81.4667), (16.517, 81.988),
(16.2372, 79.8464), (16.5787, 79.8756), (14.8667, 79.3167), (13.3667,
78.4333), (13.514, 78.227), (13.85, 78.2667), (13.8408, 78.3056),
(13.7611, 78.425), (13.8032, 77.6097), (13.9369, 77.2694), (14.4167,
78.2333), (14.1667, 78.7), (13.3667, 79.1833), (14.5942, 80.0297),
(15.298, 80.034), (17.1447, 78.2886), (16.2833, 78.5167), (16.6167,
77.85), (16.0528, 76.8877), (16.2319, 76.9553), (15.48, 73.83),
(15.6294, 73.7358), (14.6, 74.8333), (12.4181, 76.6947), (12.4972,
76.8878), (11.65, 78.1667), (10.8, 79.15), (8.7833, 78.1333), (17.72,
73.38), (20.9, 74.7833), (28.45, 77.02), (29.5833, 74.3167), (30.6167,
74.8), (30.9083, 75.8486), (34.4225, 74.6375), (21.45, 80.2), (20.17,
85.7), (26.6128, 89.825)]
Indiancities.set_index(['lat','lng'])['city'].loc[tour1].tolist()
['Pāra',
'Pānuria',
'Tūlin',
'Jhalidā',
'Srikhanda',
'Dubrājpur',
'Raipur',
'Jhikra',
'Amgachia',
'Jagdispur',
'Multi',
'Jaynagar-Majilpur',
'Pātra',
'Amtala',
'Bhagirathpur',
'Jājigrām',
'Kāthia',
'Mehegrām',
'Rudra Nagar',
'Baswa',
'Barddhamān',
'Khandaghosh',
'Galsi',
'Gopālpur',
'Bagela',
'Rānīganj',
'Baidyabāti',
'Singur',
'Mashāt',
'Kamargani',
'Khulna',
'Brahmapur',
'Meliyāputtu',
'Narsīpatnam',
'Prattipādu',
'Peddāpuram',
'Panasapādu',
'Nātavaram',
'Ponnūru',
'Karlapālem',
'Chiluvūru',
'Kolakalūru',
'Bezwāda',
'Chandragūdem',
'Vissannapeta',
'Channubanda',
'Kallūr',
'Anigandlapādu',
'Chiwemla',
'Gajwel',
'Vemalwāda',
'Timmāpuram',
'Nāspur',
'Warangal',
'Cherukūru',
'Pittalavānipālem',
'Tadikalapūdi',
'Kalavalapalle',
'Matsyapuri',
'Bhīmavaram',
'Undi',
'Bandamūrlanka',
'Muppālla',
'Kottapālem',
'Udayagiri',
'Rāmasamudram',
'Gavunipalli',
'Somapalle',
'Mulakalacheruvu',
'Kosuvāripalle',
'Lepākshi',
'Madakasīra',
'Pulivendla',
'Lakkireddipalle',
'Penumūr',
'Vidavalūru',
'Mūlaguntapādu',
'Kotūr',
'Lingāl',
'Devarkadra',
'Bāgalvād',
'Kyādgeri',
'Panaji',
'Morgim',
'Kālkuni',
'Shrīrangapattana',
'Sante Kasalgere',
'Salem',
'Tanjore',
'Tuticorin',
'Khed',
'Dhūlia',
'Gurgaon',
'Hanumāngarh',
'Sandhwān',
'Ludhiāna',
'Bandipura',
'Gondiā',
'Jatani',
'Kumārgrām']