private int ChooseNextCityToVisit(PheromoneLookup pheromoneLookup, RouteEvaluator routeEvaluator, Graph graph) { int currentCity = CurrentRoute.Last(); List <double> componentProducts = new List <double>(); List <int> citiesBeingConsideredToTravelTo = new List <int>(); for (int cityToConsiderTravellingTo = 1; cityToConsiderTravellingTo <= graph.GraphOfNodes.Count; cityToConsiderTravellingTo++) { if (VisitedCities.Contains(cityToConsiderTravellingTo)) { continue; } citiesBeingConsideredToTravelTo.Add(cityToConsiderTravellingTo); double pheremoneOnEdge = pheromoneLookup.GetPheromoneLevelForEdge(new int[] { currentCity, cityToConsiderTravellingTo }); double pheremoneComponent = Math.Pow(pheromoneLookup.GetPheromoneLevelForEdge(new int[] { currentCity, cityToConsiderTravellingTo }), ACOConstants.ALPHA_PHEROMONE_IMPORTANCE); double edgeComponent = Math.Pow(routeEvaluator.CalculateCostOfEdge(new int[] { currentCity, cityToConsiderTravellingTo }, graph), ACOConstants.BETA_EDGE_IMPORTANCE); componentProducts.Add(pheremoneComponent * edgeComponent); } componentProducts = SumComponentProductsCumulatively(componentProducts); int cityToMoveTo = PickCityBasedOnComponents(componentProducts, citiesBeingConsideredToTravelTo); return(cityToMoveTo); }
public ACOController() { BestRouteInGeneration = new Dictionary <int, Route>(); pheromoneLookup = new PheromoneLookup(); routeEvaluator = new RouteEvaluator(); antPopulation = new List <Ant>(); }
public Route WalkRoute(PheromoneLookup pheromoneLookup, RouteEvaluator routeEvaluator, Graph graph) { int startEndCity = InitialiseFirstCity(graph); while (CurrentRoute.Count <= graph.GraphOfNodes.Count - 1) { int city = ChooseNextCityToVisit(pheromoneLookup, routeEvaluator, graph); CurrentRoute.Add(city); VisitedCities.Add(city); } CurrentRoute.Add(startEndCity); AntRoute = new Route(CurrentRoute.ToArray(), double.MaxValue); return(AntRoute); }