├── AntColony_python3_code.py ├── AntColonycode_forjupiternotebook.ipynb ├── Example_of_working └── README.md /AntColony_python3_code.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from numpy import inf 3 | 4 | #given values for the problems 5 | 6 | d = np.array([[0,10,12,11,14] 7 | ,[10,0,13,15,8] 8 | ,[12,13,0,9,14] 9 | ,[11,15,9,0,16] 10 | ,[14,8,14,16,0]]) 11 | 12 | 13 | iteration = 100 14 | n_ants = 5 15 | n_citys = 5 16 | 17 | # intialization part 18 | 19 | m = n_ants 20 | n = n_citys 21 | e = .5 #evaporation rate 22 | alpha = 1 #pheromone factor 23 | beta = 2 #visibility factor 24 | 25 | #calculating the visibility of the next city visibility(i,j)=1/d(i,j) 26 | 27 | visibility = 1/d 28 | visibility[visibility == inf ] = 0 29 | 30 | #intializing pheromne present at the paths to the cities 31 | 32 | pheromne = .1*np.ones((m,n)) 33 | 34 | #intializing the rute of the ants with size rute(n_ants,n_citys+1) 35 | #note adding 1 because we want to come back to the source city 36 | 37 | rute = np.ones((m,n+1)) 38 | 39 | for ite in range(iteration): 40 | 41 | rute[:,0] = 1 #initial starting and ending positon of every ants '1' i.e city '1' 42 | 43 | for i in range(m): 44 | 45 | temp_visibility = np.array(visibility) #creating a copy of visibility 46 | 47 | for j in range(n-1): 48 | #print(rute) 49 | 50 | combine_feature = np.zeros(5) #intializing combine_feature array to zero 51 | cum_prob = np.zeros(5) #intializing cummulative probability array to zeros 52 | 53 | cur_loc = int(rute[i,j]-1) #current city of the ant 54 | 55 | temp_visibility[:,cur_loc] = 0 #making visibility of the current city as zero 56 | 57 | p_feature = np.power(pheromne[cur_loc,:],beta) #calculating pheromne feature 58 | v_feature = np.power(temp_visibility[cur_loc,:],alpha) #calculating visibility feature 59 | 60 | p_feature = p_feature[:,np.newaxis] #adding axis to make a size[5,1] 61 | v_feature = v_feature[:,np.newaxis] #adding axis to make a size[5,1] 62 | 63 | combine_feature = np.multiply(p_feature,v_feature) #calculating the combine feature 64 | 65 | total = np.sum(combine_feature) #sum of all the feature 66 | 67 | probs = combine_feature/total #finding probability of element probs(i) = comine_feature(i)/total 68 | 69 | cum_prob = np.cumsum(probs) #calculating cummulative sum 70 | #print(cum_prob) 71 | r = np.random.random_sample() #randon no in [0,1) 72 | #print(r) 73 | city = np.nonzero(cum_prob>r)[0][0]+1 #finding the next city having probability higher then random(r) 74 | #print(city) 75 | 76 | rute[i,j+1] = city #adding city to route 77 | 78 | left = list(set([i for i in range(1,n+1)])-set(rute[i,:-2]))[0] #finding the last untraversed city to route 79 | 80 | rute[i,-2] = left #adding untraversed city to route 81 | 82 | rute_opt = np.array(rute) #intializing optimal route 83 | 84 | dist_cost = np.zeros((m,1)) #intializing total_distance_of_tour with zero 85 | 86 | for i in range(m): 87 | 88 | s = 0 89 | for j in range(n-1): 90 | 91 | s = s + d[int(rute_opt[i,j])-1,int(rute_opt[i,j+1])-1] #calcualting total tour distance 92 | 93 | dist_cost[i]=s #storing distance of tour for 'i'th ant at location 'i' 94 | 95 | dist_min_loc = np.argmin(dist_cost) #finding location of minimum of dist_cost 96 | dist_min_cost = dist_cost[dist_min_loc] #finging min of dist_cost 97 | 98 | best_route = rute[dist_min_loc,:] #intializing current traversed as best route 99 | pheromne = (1-e)*pheromne #evaporation of pheromne with (1-e) 100 | 101 | for i in range(m): 102 | for j in range(n-1): 103 | dt = 1/dist_cost[i] 104 | pheromne[int(rute_opt[i,j])-1,int(rute_opt[i,j+1])-1] = pheromne[int(rute_opt[i,j])-1,int(rute_opt[i,j+1])-1] + dt 105 | #updating the pheromne with delta_distance 106 | #delta_distance will be more with min_dist i.e adding more weight to that route peromne 107 | 108 | print('route of all the ants at the end :') 109 | print(rute_opt) 110 | print() 111 | print('best path :',best_route) 112 | print('cost of the best path',int(dist_min_cost[0]) + d[int(best_route[-2])-1,0]) 113 | 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /AntColonycode_forjupiternotebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 61, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "from numpy import inf\n", 11 | "\n", 12 | "#given values for the problems\n", 13 | "\n", 14 | "d = np.array([[0,10,12,11,14]\n", 15 | " ,[10,0,13,15,8]\n", 16 | " ,[12,13,0,9,14]\n", 17 | " ,[11,15,9,0,16]\n", 18 | " ,[14,8,14,16,0]])\n", 19 | "\n", 20 | "\n", 21 | "iteration = 100\n", 22 | "n_ants = 5\n", 23 | "n_citys = 5" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 62, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "# intialization part\n", 33 | "\n", 34 | "m = n_ants\n", 35 | "n = n_citys\n", 36 | "e = .5 #evaporation rate\n", 37 | "alpha = 1 #pheromone factor\n", 38 | "beta = 2 #visibility factor" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "#calculating the visibility of the next city visibility(i,j)=1/d(i,j)\n", 48 | "\n", 49 | "visibility = 1/d\n", 50 | "visibility[visibility == inf ] = 0" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 64, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "#intializing pheromne present at the paths to the cities\n", 60 | "\n", 61 | "pheromne = .1*np.ones((m,n))\n", 62 | "\n", 63 | "#intializing the rute of the ants with size rute(n_ants,n_citys+1) \n", 64 | "#note adding 1 because we want to come back to the source city\n", 65 | "\n", 66 | "rute = np.ones((m,n+1))\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 77, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "route of all the ants at the end :\n", 79 | "[[1. 2. 5. 4. 3. 1.]\n", 80 | " [1. 2. 5. 4. 3. 1.]\n", 81 | " [1. 2. 5. 4. 3. 1.]\n", 82 | " [1. 2. 5. 4. 3. 1.]\n", 83 | " [1. 2. 5. 4. 3. 1.]]\n", 84 | "\n", 85 | "best path : [1. 2. 5. 4. 3. 1.]\n", 86 | "cost of the best path 55\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "for ite in range(iteration):\n", 92 | " \n", 93 | " rute[:,0] = 1 #initial starting and ending positon of every ants '1' i.e city '1'\n", 94 | " \n", 95 | " for i in range(m):\n", 96 | " \n", 97 | " temp_visibility = np.array(visibility) #creating a copy of visibility\n", 98 | " \n", 99 | " for j in range(n-1):\n", 100 | " #print(rute)\n", 101 | " \n", 102 | " combine_feature = np.zeros(5) #intializing combine_feature array to zero\n", 103 | " cum_prob = np.zeros(5) #intializing cummulative probability array to zeros\n", 104 | " \n", 105 | " cur_loc = int(rute[i,j]-1) #current city of the ant\n", 106 | " \n", 107 | " temp_visibility[:,cur_loc] = 0 #making visibility of the current city as zero\n", 108 | " \n", 109 | " p_feature = np.power(pheromne[cur_loc,:],beta) #calculating pheromne feature \n", 110 | " v_feature = np.power(temp_visibility[cur_loc,:],alpha) #calculating visibility feature\n", 111 | " \n", 112 | " p_feature = p_feature[:,np.newaxis] #adding axis to make a size[5,1]\n", 113 | " v_feature = v_feature[:,np.newaxis] #adding axis to make a size[5,1]\n", 114 | " \n", 115 | " combine_feature = np.multiply(p_feature,v_feature) #calculating the combine feature\n", 116 | " \n", 117 | " total = np.sum(combine_feature) #sum of all the feature\n", 118 | " \n", 119 | " probs = combine_feature/total #finding probability of element probs(i) = comine_feature(i)/total\n", 120 | " \n", 121 | " cum_prob = np.cumsum(probs) #calculating cummulative sum\n", 122 | " #print(cum_prob)\n", 123 | " r = np.random.random_sample() #randon no in [0,1)\n", 124 | " #print(r)\n", 125 | " city = np.nonzero(cum_prob>r)[0][0]+1 #finding the next city having probability higher then random(r) \n", 126 | " #print(city)\n", 127 | " \n", 128 | " rute[i,j+1] = city #adding city to route \n", 129 | " \n", 130 | " left = list(set([i for i in range(1,n+1)])-set(rute[i,:-2]))[0] #finding the last untraversed city to route\n", 131 | " \n", 132 | " rute[i,-2] = left #adding untraversed city to route\n", 133 | " \n", 134 | " rute_opt = np.array(rute) #intializing optimal route\n", 135 | " \n", 136 | " dist_cost = np.zeros((m,1)) #intializing total_distance_of_tour with zero \n", 137 | " \n", 138 | " for i in range(m):\n", 139 | " \n", 140 | " s = 0\n", 141 | " for j in range(n-1):\n", 142 | " \n", 143 | " s = s + d[int(rute_opt[i,j])-1,int(rute_opt[i,j+1])-1] #calcualting total tour distance\n", 144 | " \n", 145 | " dist_cost[i]=s #storing distance of tour for 'i'th ant at location 'i' \n", 146 | " \n", 147 | " dist_min_loc = np.argmin(dist_cost) #finding location of minimum of dist_cost\n", 148 | " dist_min_cost = dist_cost[dist_min_loc] #finging min of dist_cost\n", 149 | " \n", 150 | " best_route = rute[dist_min_loc,:] #intializing current traversed as best route\n", 151 | " pheromne = (1-e)*pheromne #evaporation of pheromne with (1-e)\n", 152 | " \n", 153 | " for i in range(m):\n", 154 | " for j in range(n-1):\n", 155 | " dt = 1/dist_cost[i]\n", 156 | " pheromne[int(rute_opt[i,j])-1,int(rute_opt[i,j+1])-1] = pheromne[int(rute_opt[i,j])-1,int(rute_opt[i,j+1])-1] + dt \n", 157 | " #updating the pheromne with delta_distance\n", 158 | " #delta_distance will be more with min_dist i.e adding more weight to that route peromne\n", 159 | "\n", 160 | "print('route of all the ants at the end :')\n", 161 | "print(rute_opt)\n", 162 | "print()\n", 163 | "print('best path :',best_route)\n", 164 | "print('cost of the best path',int(dist_min_cost[0]) + d[int(best_route[-2])-1,0])\n", 165 | " \n", 166 | "\n", 167 | " \n", 168 | " \n", 169 | " " 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | } 179 | ], 180 | "metadata": { 181 | "kernelspec": { 182 | "display_name": "Python 3", 183 | "language": "python", 184 | "name": "python3" 185 | }, 186 | "language_info": { 187 | "codemirror_mode": { 188 | "name": "ipython", 189 | "version": 3 190 | }, 191 | "file_extension": ".py", 192 | "mimetype": "text/x-python", 193 | "name": "python", 194 | "nbconvert_exporter": "python", 195 | "pygments_lexer": "ipython3", 196 | "version": "3.6.5" 197 | } 198 | }, 199 | "nbformat": 4, 200 | "nbformat_minor": 2 201 | } 202 | -------------------------------------------------------------------------------- /Example_of_working: -------------------------------------------------------------------------------- 1 | rute= [[1. 0. 0. 0. 0. 0.] 2 | [1. 0. 0. 0. 0. 0.] 3 | [1. 0. 0. 0. 0. 0.] 4 | [1. 0. 0. 0. 0. 0.] 5 | [1. 0. 0. 0. 0. 0.]] 6 | 7 | tho= [[0. 0.1 0.08333333 0.09090909 0.07142857] 8 | [0.1 0. 0.07692308 0.06666667 0.125 ] 9 | [0.08333333 0.07692308 0. 0.11111111 0.07142857] 10 | [0.09090909 0.06666667 0.11111111 0. 0.0625 ] 11 | [0.07142857 0.125 0.07142857 0.0625 0. ]] 12 | 13 | mh= [[0. 0.1 0.08333333 0.09090909 0.07142857] 14 | [0. 0. 0.07692308 0.06666667 0.125 ] 15 | [0. 0.07692308 0. 0.11111111 0.07142857] 16 | [0. 0.06666667 0.11111111 0. 0.0625 ] 17 | [0. 0.125 0.07142857 0.0625 0. ]] 18 | 19 | tho[1]^beta=[0.01 0.01 0.01 0.01 0.01] 20 | mh[1]^alpha=[0. 0.1 0.08333333 0.09090909 0.07142857] 21 | 22 | resulting thoinit=[0. 0.28929242 0.24107702 0.26299311 0. ] 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | rute= [[1. 2. 0. 0. 0. 0.] 36 | [1. 0. 0. 0. 0. 0.] 37 | [1. 0. 0. 0. 0. 0.] 38 | [1. 0. 0. 0. 0. 0.] 39 | [1. 0. 0. 0. 0. 0.]] 40 | 41 | tho= [[0. 0.1 0.08333333 0.09090909 0.07142857] 42 | [0.1 0. 0.07692308 0.06666667 0.125 ] 43 | [0.08333333 0.07692308 0. 0.11111111 0.07142857] 44 | [0.09090909 0.06666667 0.11111111 0. 0.0625 ] 45 | [0.07142857 0.125 0.07142857 0.0625 0. ]] 46 | 47 | mh= [[0. 0. 0.08333333 0.09090909 0.07142857] 48 | [0. 0. 0.07692308 0.06666667 0.125 ] 49 | [0. 0. 0. 0.11111111 0.07142857] 50 | [0. 0. 0.11111111 0. 0.0625 ] 51 | [0. 0. 0.07142857 0.0625 0. ]] 52 | 53 | tho[2]^beta= [0.01 0.01 0.01 0.01 0.01] 54 | mh[2]^apha = [0. 0. 0.07692308 0.06666667 0.125 ] 55 | 56 | resulting thoinit=[0. 0. 0.28639618 0.24821002 0. ] 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | rute= [[1. 2. 5. 0. 0. 0.] 69 | [1. 0. 0. 0. 0. 0.] 70 | [1. 0. 0. 0. 0. 0.] 71 | [1. 0. 0. 0. 0. 0.] 72 | [1. 0. 0. 0. 0. 0.]] 73 | 74 | 75 | tho= [[0. 0.1 0.08333333 0.09090909 0.07142857] 76 | [0.1 0. 0.07692308 0.06666667 0.125 ] 77 | [0.08333333 0.07692308 0. 0.11111111 0.07142857] 78 | [0.09090909 0.06666667 0.11111111 0. 0.0625 ] 79 | [0.07142857 0.125 0.07142857 0.0625 0. ]] 80 | 81 | mh= [[0. 0. 0.08333333 0.09090909 0. ] 82 | [0. 0. 0.07692308 0.06666667 0. ] 83 | [0. 0. 0. 0.11111111 0. ] 84 | [0. 0. 0.11111111 0. 0. ] 85 | [0. 0. 0.07142857 0.0625 0. ]] 86 | 87 | tho[3]^beta= [0.01 0.01 0.01 0.01 0.01] 88 | mh[3]^apha = [0. 0. 0.07142857 0.0625 0. ] 89 | 90 | resulting thoinit=[0. 0. 0.53333333 0.46666667 0. ] 91 | 92 | rute[3]= [[1. 2. 5. 3. 0. 0.] 93 | [1. 0. 0. 0. 0. 0.] 94 | [1. 0. 0. 0. 0. 0.] 95 | [1. 0. 0. 0. 0. 0.] 96 | [1. 0. 0. 0. 0. 0.]] 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ant-Colony-Optimization 2 | implementation of Ant colony Optimization using Python 3 | --------------------------------------------------------------------------------