Пример #1
0
        public Dictionary <int, Route> RunACO(Graph graph)
        {
            pheromoneLookup.InitialisePheremoneOnAllEdges(graph.GraphOfNodes.Count);
            antPopulation = InitialiseAntPopulation();

            for (int generation = 0; generation < ACOConstants.DEFAULT_ANT_GENERATIONS; generation++)
            {
                foreach (Ant ant in antPopulation)
                {
                    Route route = ant.WalkRoute(pheromoneLookup, routeEvaluator, graph);
                    route.RouteCost = routeEvaluator.CalculateCostOfSingleRoute(route.RouteIds, graph);
                }

                // Seperate loop for pheremones so ants do not converge on a single route immediately
                foreach (Ant ant in antPopulation)
                {
                    pheromoneLookup.UpdatePheromoneForSingleRoute(ant.AntRoute.RouteIds);
                    ant.ClearRoutes();
                }

                pheromoneLookup.DecayAllPheromones();

                List <Route> antRoutes = new List <Route>();
                antPopulation.ForEach(ant => antRoutes.Add(ant.AntRoute));

                FindBestRouteInGeneration(generation, antRoutes);
            }

            return(BestRouteInGeneration);
        }
        private List <Route> InitialisePoplulation(Graph graph)
        {
            List <Route> tempPopulation = new List <Route>();

            for (int index = 0; index < EvolutionaryAlgorithmConstants.POPULATION_SIZE; index++)
            {
                int[]  routeIds  = randomRouteGenerator.GenerateSingleRandomRoute(graph.GraphOfNodes.Count);
                double routeCost = routeEvaluator.CalculateCostOfSingleRoute(routeIds, graph);

                Route currentRoute     = new Route(routeIds, routeCost);
                Route currentBestRoute = BestRouteInGeneration.ElementAt(0).Value;

                if (currentRoute.RouteCost < currentBestRoute.RouteCost)
                {
                    BestRouteInGeneration.Remove(0);
                    BestRouteInGeneration.Add(0, currentRoute);
                }

                tempPopulation.Add(currentRoute);
            }

            return(tempPopulation);
        }
Пример #3
0
        public void ConstructGreedyRoute(Graph graph, RouteEvaluator routeEvaluator)
        {
            int startEndCity = InitialiseFirstCity(graph);

            while (CurrentRoute.Count <= graph.GraphOfNodes.Count)
            {
                CurrentRoute.Add(graph.GetClosestCity(CurrentRoute.Count - 1, VisitedCities));
            }

            CurrentRoute.Add(startEndCity);
            int[] CurrentRouteArray = CurrentRoute.ToArray();

            double routeCost = routeEvaluator.CalculateCostOfSingleRoute(CurrentRouteArray, graph);

            AntRoute = new Route(CurrentRouteArray, routeCost);
        }
Пример #4
0
        private Route FindBestTourInNeighbourhood(List <int[]> routes, Graph graph)
        {
            int[]  bestRoute = new int[routes.ElementAt(0).Length];
            double bestCost  = 0;

            foreach (int[] route in routes)
            {
                double routeCost = routeEvaluator.CalculateCostOfSingleRoute(route, graph);

                if (bestCost == 0 || routeCost < bestCost)
                {
                    bestCost  = routeCost;
                    bestRoute = route;
                }
            }

            return(new Route(bestRoute, bestCost));
        }
Пример #5
0
        public KeyValuePair <int[], double> CalculateBestRandomRouteInGivenTime(Graph graph, int timeToExecuteFor)
        {
            int[]  lowestCostRoute            = new int[graph.GraphOfNodes.Count + 1];
            double currentLowestCostRoute     = 0;
            int    timeToExecuteForInMilliSec = timeToExecuteFor * 1000;
            var    now = DateTime.Now;

            while (DateTime.Now < now.AddMilliseconds(timeToExecuteForInMilliSec))
            {
                // This is duplicated in menu. Must be a better layout than copy paste
                int[]  route       = randomRouteGenerator.GenerateSingleRandomRoute(graph.GraphOfNodes.Count);
                double costOfRoute = routeEvaluator.CalculateCostOfSingleRoute(route, graph);

                if (IsLowestCostOfRouteSoFar(currentLowestCostRoute, costOfRoute))
                {
                    lowestCostRoute        = route;
                    currentLowestCostRoute = costOfRoute;
                }
            }

            return(new KeyValuePair <int[], double>(lowestCostRoute, currentLowestCostRoute));
        }