Guest User

Untitled

a guest
Feb 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.33 KB | None | 0 0
  1. //add file header comment
  2.  
  3. import java.util.*;
  4.  
  5. //add class header comment
  6. public class AntColonyOptimization {
  7.  
  8. /**
  9. * Finds the shortest route it can (though it might not be the absolute
  10. * shortest route) from the queen ant's nest to the chamber with food based
  11. * on the map of the colony in the AntWorld class, and displays this route
  12. * and its length.
  13. *
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. int[] shortestRoute = findShortestRoute(AntWorld.antWorld.MAP,
  18. AntWorld.antWorld.NEST_LOCATION,
  19. AntWorld.antWorld.FOOD_LOCATION, AntWorld.antWorld.N_ANTS,
  20. AntWorld.antWorld.N_ITERATIONS,
  21. AntWorld.antWorld.PHEROMONE_EVAPORATION_COEFFICIENT,
  22. AntWorld.antWorld.PHEROMONE_STRENGTH_COEFFICIENT,
  23. AntWorld.antWorld.SEED);
  24. System.out.print("Shortest route found: ");
  25. printRoute(shortestRoute);
  26. System.out.println("Length: " + calcLengthOfRoute(shortestRoute,
  27. AntWorld.antWorld.MAP));
  28. }
  29.  
  30. /**
  31. * Repeatedly finds a new set of routes from the queen's nest to the food
  32. * chamber for the number of iterations given, updates the attractiveness
  33. * of the tunnels based on those routes, and keeps track of the shortest
  34. * route found so far (but it might not be the absolute shortest route).
  35. *
  36. * @param map
  37. * distances between all pairs of chambers in which is possible
  38. * to move from one chamber to the other, if it is not possible
  39. * to move from one chamber to another then the distance for
  40. * that pair is zero
  41. * @param nestLocation
  42. * the chamber of the queen's nest
  43. * @param foodLocation
  44. * the chamber of the food
  45. * @param nAnts
  46. * the number of ants that will find a route from the nest to
  47. * the food in each iteration of the algorithm
  48. * @param nIterations
  49. * the number of times to repeat the algorithm
  50. * @param pheromoneEvaporationCoefficient
  51. * the proportion of each tunnel attractiveness that is taken
  52. * away when the tunnel attractivenesses are updated
  53. * @param pheromoneStrengthCoefficient
  54. * a factor used in updating the tunnel attractivenesses
  55. * @param seed
  56. * a seed for the random number generator
  57. * @return the sequence of chambers from the queen's nest to the food
  58. * chamber, in this case without any trailing negative ones, that
  59. * had the smallest length of all such sequences that were
  60. * considered
  61. */
  62. public static int[] findShortestRoute(double[][] map, int nestLocation,
  63. int foodLocation, int nAnts, int nIterations,
  64. double pheromoneEvaporationCoefficient,
  65. double pheromoneStrengthCoefficient, int seed) {
  66. Random random = new Random(seed);
  67.  
  68. return null;
  69. }
  70.  
  71. /**
  72. * Creates the initial tunnelAttractivenesses two-dimensional double
  73. * array, with a value of 1.0 for every ordered pair of indices whose
  74. * value in map was positive and 0.0 for every ordered pair of indices
  75. * whose value in map was zero.
  76. *
  77. * @param map
  78. * distances between all pairs of chambers in which is possible
  79. * to move from one chamber to the other, if it is not possible
  80. * to move from one chamber to another then the distance for
  81. * that pair is zero
  82. * @return initialTunnelAttractivenesses
  83. */
  84. public static double[][] initializeTunnelAttractivenesses(double[][] map) {
  85.  
  86. double [][] initializeTunnelAttractivenesses = new double [map.length][map[0].length];
  87. for (int i =0; i < map.length; i++){
  88. for(int j = 0; j < map[i].length; j++){
  89. if (map[i][j]>0){
  90. initializeTunnelAttractivenesses [i][j] = 1.0;
  91. }
  92. else {
  93. initializeTunnelAttractivenesses [i][j] = 0.0;
  94. }
  95. }
  96. }
  97. return initializeTunnelAttractivenesses;
  98. }
  99.  
  100. /**
  101. * Probabilistically chooses the next chamber a particular ant should
  102. * travel to based on the attractiveness of the tunnels from the ant's
  103. * current location to the other chambers. To do this, it: 1. Finds the sum
  104. * of the attractiveness of all the tunnels from the ant's location to
  105. * all other chambers. 2. Calculates a threshold by multiplying a randomly
  106. * generated double between 0.0 and 1.0 by the sum of the attractivenesses.
  107. * 3. Starting from chamber 0, again sums the attractiveness of all the
  108. * tunnels from the ant's location to all other chambers until the sum
  109. * is greater than the threshold. The chamber in which the
  110. * tunnel attractiveness from the ant's location to that chamber caused the
  111. * sum to equal or exceed the threshold is the chamber that the ant will
  112. * travel to next.
  113. *
  114. * @param location
  115. * the current chamber of an ant
  116. * @param tunnelAttractivenesses
  117. * the attractiveness of the tunnel between every ordered
  118. * pair of chambers
  119. * @param random
  120. * a random number generator
  121. * @return the chosen chamber that the ant will travel to
  122. */
  123. public static int chooseTunnel(int location,
  124. double[][] tunnelAttractivenesses, Random random) {
  125. double totalAttractiveness = 0;
  126. double sumAttractiveness = 0;
  127. double threshold;
  128. int destination = 0;
  129. int count = 0;
  130. for (int i = location; i < tunnelAttractivenesses[location].length - 1; i++){
  131. totalAttractiveness += tunnelAttractivenesses[location][i];
  132. }
  133. threshold = random.nextDouble() * totalAttractiveness;
  134. while(sumAttractiveness < threshold){
  135. sumAttractiveness += tunnelAttractivenesses[location][count];
  136. destination = count;
  137. count ++;
  138. }
  139. return destination;
  140. }
  141.  
  142. /**
  143. * Finds a new set of routes from the queen's nest chamber to the food
  144. * chamber. To do this, it: 1. Creates the antRoutes two-dimensional int
  145. * array. The length of each row is the total number of chambers. 2. For
  146. * every ant (i.e. row), initializes its first chamber in antRoutes to be
  147. * the queen's nest chamber, and initializes all other values in antRoutes
  148. * to negative one. 3. As long as is necessary, repeatedly computes for all
  149. * ants (i.e., rows) in order whether or not the ant has already reached
  150. * the food. If an ant has not already reached the food, a tunnel is
  151. * chosen and antRoutes is updated. All ants that have not already reached
  152. * the food chamber are moved for the nth time before any ant is moved for
  153. * the (n + 1)th time.
  154. *
  155. * @param tunnelAttractivenesses
  156. * the attractiveness of the tunnel between every ordered
  157. * pair of chambers
  158. * @param nestLocation
  159. * the chamber of the queen's nest
  160. * @param foodLocation
  161. * the chamber of the food
  162. * @param nAnts
  163. * the number of ants that will find a route from the nest to
  164. * the food in each iteration of the algorithm
  165. * @param random
  166. * a random number generator
  167. * @return antRoutes
  168. */
  169. public static int[][] findAntRoutes(double[][] tunnelAttractivenesses,
  170. int nestLocation, int foodLocation, int nAnts, Random random) {
  171. int chamberIndex = 0;
  172. int [][] antRoutes = new int [nAnts][tunnelAttractivenesses[0].length];
  173. for (int i = 0; i < antRoutes.length; i++){
  174. for(int j = 0; j < antRoutes[i].length; j++){
  175. if(j==0){
  176. antRoutes[i][j] = nestLocation;
  177. }
  178. else {
  179. antRoutes[i][j] = -1;
  180. }
  181. }
  182. }
  183. //TODO while ()
  184. return null;
  185. }
  186.  
  187. /**
  188. * Calculates the length of a route according to the distances in the map.
  189. *
  190. * @param route
  191. * a sequence of chambers from the queen's chamber to the food
  192. * chamber, followed by some number of negative ones
  193. * @param map
  194. * distances between all pairs of chambers in which is possible
  195. * to move from one chamber to the other, if it is not possible
  196. * to move from one chamber to another then the distance for
  197. * that pair is zero
  198. * @return the length of the route
  199. */
  200. public static double calcLengthOfRoute(int[] route, double[][] map) {
  201. int length = 0;
  202. route = trimRoute(route);
  203. for (int i = 0; i < route.length - 1; i++) {
  204. length += map[route[i]][route[i + 1]];
  205. }
  206. return length;
  207. }
  208.  
  209. /**
  210. * Changes the attractiveness of tunnels based on the length of any routes
  211. * they were used in, and then proportionately decreases the attractiveness
  212. * of all of the tunnels. To do this, it: 1. For every tunnel taken in
  213. * every route in antRoutes, the pheromoneStrengthCoefficient / the length
  214. * of the route is added to the attractiveness of that tunnel. 2. The
  215. * attractiveness of all tunnels is multiplied by 1.0 - the
  216. * pheromoneEvaporationCoefficient.
  217. *
  218. * @param antRoutes
  219. * a set of routes from the nest to the food for a number of
  220. * ants. The first dimension corresponds to ants and the second
  221. * dimension corresponds to time. For each ant, the values over
  222. * time correspond to the chambers the ant visited. The length
  223. * of the second dimension is the total number of chambers in
  224. * the world, and the values over time after an ant has reached
  225. * the food are -1.
  226. * @param tunnelAttractivenesses
  227. * the attractiveness of the tunnel between every ordered
  228. * pair of chambers
  229. * @param pheromoneEvaporationCoefficient
  230. * the proportion of each tunnel attractiveness that is taken
  231. * away when the tunnel attractivenesses are updated
  232. * @param pheromoneStrengthCoefficient
  233. * a factor used in updating the tunnel attractivenesses
  234. * @param map
  235. * distances between all pairs of chambers in which is possible
  236. * to move from one chamber to the other, if it is not possible
  237. * to move from one chamber to another then the distance for
  238. * that pair is zero
  239. */
  240. public static void updateTunnelAttractivenesses(int[][] antRoutes,
  241. double[][] tunnelAttractivenesses,
  242. double pheromoneEvaporationCoefficient,
  243. double pheromoneStrengthCoefficient, double[][] map) {
  244.  
  245. for (int i = 0; i < antRoutes.length; i++){
  246. for(int j = 0; j < antRoutes[i].length; j++){
  247. //TODO finish this
  248. }
  249. }
  250. }
  251.  
  252. /**
  253. * Finds if there is a shorter route than currentShortestRoute of the
  254. * routes in antRoutes using to the distances in the map to determine their
  255. * length. If there is more than one route tieing for shortest, it will
  256. * return the first one, starting from currentShortestRoute and
  257. * continuing in increasing order through the indexes of antRoutes.
  258. *
  259. * @param currentShortestRoute
  260. * the sequence of chambers from the queen's nest to the food
  261. * chamber, followed by some number of negative ones, that has
  262. * the smallest length of any such sequence considered so far.
  263. * This parameter may be passed null, in which case the
  264. * currentShortestRoute is not to be considered.
  265. * @param antRoutes
  266. * a set of routes from the nest to the food for a number of
  267. * ants. The first dimension corresponds to ants and the second
  268. * dimension corresponds to time. For each ant, the values over
  269. * time correspond to the chambers the ant visited. The length
  270. * of the second dimension is the total number of chambers in
  271. * the world, and the values over time after an ant has reached
  272. * the food are -1.
  273. * @param map
  274. * distances between all pairs of chambers in which is possible
  275. * to move from one chamber to the other, if it is not possible
  276. * to move from one chamber to another then the distance for
  277. * that pair is zero
  278. * @return the sequence of chambers from the queen's nest to the food
  279. * chamber, followed by some number of negative ones, that has the
  280. * smallest length of such sequences amongst the
  281. * currentShortestRoute and the routes in antRoutes
  282. */
  283. public static int[] findShorterRoute(int[] currentShortestRoute,
  284. int[][] antRoutes, double[][] map) {
  285. double routeLength = 0.0;
  286. double shortestCurrent = calcLengthOfRoute(currentShortestRoute, map);
  287. for (int i = 0; i < antRoutes[0].length; i++){
  288. routeLength = calcLengthOfRoute(antRoutes[i], map);
  289. if (routeLength < shortestCurrent){
  290. currentShortestRoute = antRoutes[i];
  291. }
  292. }
  293. return currentShortestRoute;
  294. }
  295.  
  296. /**
  297. * Makes a new array representing the same sequence of chambers as in route
  298. * but without any trailing negative ones.
  299. *
  300. * @param route
  301. * a sequence of chambers from the queen's nest to the food
  302. * chamber, followed by some number of negative ones
  303. * @return a sequence of chambers from the queen's nest to the food
  304. * chamber
  305. */
  306. public static int[] trimRoute(int[] route) {
  307. int count = 0;
  308. while (route[count] != -1){
  309. count ++;
  310. }
  311. int [] trimmedRoute = Arrays.copyOf(route, count-1);
  312.  
  313. return trimmedRoute;
  314. }
  315.  
  316. /**
  317. * Displays the route horizontally on the screen separated by spaces.
  318. *
  319. * @param route
  320. * a sequence of chambers from the queen's nest to the food
  321. * chamber
  322. */
  323. public static void printRoute(int[] route) {
  324.  
  325. for (int i = 0; i <= route.length; i++){
  326. System.out.print(route[i] + " ");
  327. }
  328. System.out.println();
  329. }
  330. }
Add Comment
Please, Sign In to add comment