public AlgorithmsTestViewModel Main() { FindRouteBusinessLayer findRouteBL = new FindRouteBusinessLayer(); AlgorithmsTestViewModel algorithmsTestVM = new AlgorithmsTestViewModel(); Route fullGAPathNodeList = new Route(); Route fullCWPathNodeList = new Route(); GAResults(); SumGACosts(); BestGASolution(); ClarkeWrightResult(); InsertResultsToExcelFile(); algorithmsTestVM.GAPath = findRouteBL.ConvertSolution(bestGASolution.Last().NodeIndexList, vrpGraph); algorithmsTestVM.GAPath.Add(algorithmsTestVM.GAPath[0]); fullGAPathNodeList = findRouteBL.CreateFullRoute(algorithmsTestVM.GAPath, pathMemoryList); algorithmsTestVM.GAPath = fullGAPathNodeList.Path; algorithmsTestVM.GAPathCost = bestGASolution.Last().FitnessValue; algorithmsTestVM.GAPathCost = Math.Round(algorithmsTestVM.GAPathCost, 3); algorithmsTestVM.ClarkeWrightPath = findRouteBL.ConvertSolution(cwResultNodeIndexList, vrpGraph); fullCWPathNodeList = findRouteBL.CreateFullRoute(algorithmsTestVM.ClarkeWrightPath, pathMemoryList); algorithmsTestVM.ClarkeWrightPath = fullCWPathNodeList.Path; algorithmsTestVM.ClarkeWrightPathCost = fullCWPathNodeList.Cost; algorithmsTestVM.ClarkeWrightPathCost = Math.Round(algorithmsTestVM.ClarkeWrightPathCost, 3); algorithmsTestVM.GAExecutionTime = gaExecutionTime; algorithmsTestVM.ClarkeWrightExecutionTime = cwExecutionTime; return(algorithmsTestVM); }
private void BestGASolution() { List <List <Chromosome> > allSolutionsList = new List <List <Chromosome> >(); FindRouteBusinessLayer findRouteBL = new FindRouteBusinessLayer(); for (int i = 0; i < gaResultsList.Count(); i++) { for (int j = 0; j < gaResultsList[i].Count(); j++) { allSolutionsList.Add(gaResultsList[i][j]); } } List <Chromosome> best = allSolutionsList[0]; int bestIndex = 0; for (int i = 1; i < allSolutionsList.Count(); i++) { if (best.Last().FitnessValue > allSolutionsList[i].Last().FitnessValue) { best = allSolutionsList[i]; bestIndex = i; } } gaExecutionTime = gaExecutionTimeList[bestIndex]; bestGASolution = best; }
public void Evaluation(List <Chromosome> population) { FindRouteBusinessLayer findRouteBL = new FindRouteBusinessLayer(); for (int i = 0; i < population.Count(); i++) { population[i].FitnessValue = 0; for (int j = 0; j < population[i].NodeIndexList.Count() - 1; j++) { population[i].FitnessValue += Graph.Edges[population[i].NodeIndexList[j]] .Where(x => x.Item1 == population[i].NodeIndexList[j + 1]).First().Item2; } population[i].FitnessValue += Graph.Edges[population[i].NodeIndexList.Last()] .Where(x => x.Item1 == population[i].NodeIndexList[0]).First().Item2; } }
public void ConnectAddressesToGraph(List <Node> addressList) { for (int i = 0; i < addressList.Count(); i++) { //item1: wierzchołek, item2: odległośc do tego wierzchołka List <Tuple <Node, double> > nodeBelongToStreetList = new List <Tuple <Node, double> >(); string streetName = addressList[i].dict["addr:street"]; for (int j = 0; j < NodeList.Count(); j++) { if (NodeList[j].dict.ContainsKey("addr:street") && NodeList[j].dict["addr:street"].Equals(streetName)) { double distance = FindRouteBusinessLayer.CalculateDistance(addressList[i], NodeList[j]); nodeBelongToStreetList.Add(new Tuple <Node, double>(NodeList[j], distance)); } } nodeBelongToStreetList = nodeBelongToStreetList.OrderBy(x => x.Item2).ToList(); if (nodeBelongToStreetList.Count > 0) { //add node addressList[i].ArrayIndex = Edges.Length; NodeList.Add(addressList[i]); //add edges int newArraySize = Edges.Length + 1; List <Tuple <int, double> >[] tmpEdges = Edges; Array.Resize <List <Tuple <int, double> > >(ref tmpEdges, newArraySize); Edges = new List <Tuple <int, double> > [newArraySize]; Edges = tmpEdges; Edges[newArraySize - 1] = new List <Tuple <int, double> >(); Edges[newArraySize - 1].Add(new Tuple <int, double>(nodeBelongToStreetList[0].Item1.ArrayIndex, nodeBelongToStreetList[0].Item2)); Edges[nodeBelongToStreetList[0].Item1.ArrayIndex].Add(new Tuple <int, double>(addressList[i].ArrayIndex, nodeBelongToStreetList[0].Item2)); } } }
public List <Node> AStar(List <Tuple <int, double> >[] adjacencList, List <Node> nodeList, Node source, Node destination, double speed) { FindRouteBusinessLayer findRouteBL = new FindRouteBusinessLayer(); List <int> closetSet = new List <int>(); List <int> openSet = new List <int>(); int nodesNumber = adjacencList.Length; double[] gScore = new double[nodesNumber]; double[] fScore = new double[nodesNumber]; int[] cameFrom = new int[nodesNumber]; for (int i = 0; i < nodesNumber; i++) { gScore[i] = double.MaxValue; fScore[i] = double.MaxValue; } gScore[source.ArrayIndex] = 0; //fScore[source.ArrayIndex] = findRouteBL.GetEstimateRouteCost(source, destination) * speed; fScore[source.ArrayIndex] = GetDistanceInKM(source, destination) * speed; for (int i = 0; i < adjacencList.Length; i++) { openSet.Add(i); } while (openSet.Count() != 0) { int current = FindNodeWithLowestCost(fScore, openSet); if (current == destination.ArrayIndex) { int[] path = ReconstructPath(cameFrom, source.ArrayIndex, destination.ArrayIndex); List <Node> result = new List <Node>(); foreach (var index in path) { result.Add(nodeList[index]); } return(result); } openSet.RemoveAt(openSet.FindIndex(x => x == current)); closetSet.Add(current); for (int i = 0; i < adjacencList[current].Count(); i++) { if (closetSet.Contains(adjacencList[current][i].Item1)) { continue; } double tentative_gScore = gScore[current] + adjacencList[current][i].Item2; if (openSet.Contains(adjacencList[current][i].Item1) == false) { openSet.Add(adjacencList[current][i].Item1); } if (tentative_gScore >= gScore[adjacencList[current][i].Item1]) { continue; } cameFrom[adjacencList[current][i].Item1] = current; gScore[adjacencList[current][i].Item1] = tentative_gScore; //fScore[adjacencList[current][i].Item1] = gScore[adjacencList[current][i].Item1] // + findRouteBL.GetEstimateRouteCost(nodeList.Where(x => x.ArrayIndex == adjacencList[current][i].Item1).First(), destination) * speed; fScore[adjacencList[current][i].Item1] = gScore[adjacencList[current][i].Item1] + GetDistanceInKM(nodeList.Where(x => x.ArrayIndex == adjacencList[current][i].Item1).First(), destination) * speed; } } return(null); }