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); }
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)); }