private double GetRouteCost(int[] route) { double routeCost = 0.0; for (var i = 0; i < route.Length - 1; i++) { routeCost += Vertice.GetDistance(problem.Graph.Vertices[route[i]], problem.Graph.Vertices[route[i + 1]]); } return(routeCost); }
public void FindRoute() { Routes = new List <int[]>(); List <Vertice> notVisited = new List <Vertice>(); for (int i = 1; i < problem.Dimensions; i++) { notVisited.Add(problem.Graph.Vertices[i]); } pheromoneTrait.Add(problem.Graph.DepotIndex); List <int> route = new List <int>(); Vertice currentNode = problem.Graph.Vertices[startingNode]; pheromoneTrait.Add(startingNode); route.Add(startingNode); int capacity = problem.Capacity - problem.Graph.Vertices[startingNode].Demand; Cost = Vertice.GetDistance(problem.Graph.Vertices[startingNode], problem.Graph.Vertices[problem.Graph.DepotIndex]); notVisited.RemoveAt(startingNode - 1); while (notVisited.Count > 0) { while (capacity > 0 && notVisited.Count > 0) { int nextNodeIndex = GetNextVertice(notVisited, currentNode, capacity); if (nextNodeIndex == -1) { break; } Cost += Vertice.GetDistance(currentNode, notVisited[nextNodeIndex]); capacity -= notVisited[nextNodeIndex].Demand; route.Add(notVisited[nextNodeIndex].Id); pheromoneTrait.Add(notVisited[nextNodeIndex].Id); currentNode = notVisited[nextNodeIndex]; notVisited.RemoveAt(nextNodeIndex); } Cost += Vertice.GetDistance(currentNode, problem.Graph.Vertices[problem.Graph.DepotIndex]); Routes.Add(route.ToArray()); pheromoneTrait.Add(problem.Graph.DepotIndex); currentNode = problem.Graph.Vertices[problem.Graph.DepotIndex]; route = new List <int>(); capacity = problem.Capacity; } }
public TimeSpan Solve() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); routes = new List <int[]>(); cost = 0; List <Vertice> notVisited = new List <Vertice>(); for (int i = 1; i < problem.Dimensions; i++) { notVisited.Add(problem.Graph.Vertices[i]); } while (notVisited.Count > 0) { List <int> route = new List <int>(); double routeCost = 0.0; Vertice currentNode = problem.Graph.Vertices[problem.Graph.DepotIndex]; int capacity = problem.Capacity; while (capacity > 0 && notVisited.Count > 0) { (int closestNodeIndex, double closestNodeCost) = FindClosestNodeIndex(notVisited, currentNode, capacity, routeCost); if (closestNodeIndex == -1) { break; } cost += closestNodeCost; routeCost += closestNodeCost; capacity -= notVisited[closestNodeIndex].Demand; route.Add(notVisited[closestNodeIndex].Id); currentNode = notVisited[closestNodeIndex]; notVisited.RemoveAt(closestNodeIndex); } cost += Vertice.GetDistance(currentNode, problem.Graph.Vertices[problem.Graph.DepotIndex]); if (route.Count == 0) { break; } routes.Add(route.ToArray()); } stopwatch.Stop(); return(stopwatch.Elapsed); }
private (int index, double cost) FindClosestNodeIndex(List <Vertice> node, Vertice from, int capacityLeft, double routeCost) { int closestIndex = -1; double closestValue = double.MaxValue; for (int i = 0; i < node.Count; i++) { if (node[i].Demand <= capacityLeft) { double currentValue = Vertice.GetDistance(node[i], from); if (maxVehicleDistance >= routeCost + currentValue + Vertice.GetDistance(node[i], problem.Graph.Vertices[problem.Graph.DepotIndex])) { if (currentValue < closestValue) { closestIndex = i; closestValue = currentValue; } } } } return(closestIndex, closestValue); }
private int GetNextVertice(List <Vertice> notVisited, Vertice currentNode, int capacity) { List <(int index, double value)> probability = new List <(int index, double value)>(); double sum = 0.0; for (int i = 0; i < notVisited.Count; i++) { if (notVisited[i].Demand < capacity) { double val = Math.Pow(edges.GetEdge(currentNode.Id, notVisited[i].Id).GetPheromone(), parameters.Alpha) / Math.Pow(Vertice.GetDistance(currentNode, notVisited[i]), parameters.Beta); probability.Add((i, val)); sum += val; } } double random = RandomGenerator.GetDoubleRangeRandomNumber(0.0, 1.0); double prob = 0.0; for (int i = 0; i < probability.Count; i++) { prob += probability[i].value / sum; if (random < prob) { return(probability[i].index); } } return(-1); }
public Edge(Vertice start, Vertice end) { Start = start; End = end; Length = start.GetDistance(end); }
public void FindRoute(int iterationNumber) { Routes = new List <int[]>(); List <Vertice> notVisited = new List <Vertice>(); for (int i = 1; i < problem.Dimensions; i++) { notVisited.Add(problem.Graph.Vertices[i]); } pheromoneTrait.Add(problem.Graph.DepotIndex); List <int> route = new List <int>(); Vertice currentNode = problem.Graph.Vertices[startingNode]; pheromoneTrait.Add(startingNode); route.Add(startingNode); int capacity = problem.Capacity - problem.Graph.Vertices[startingNode].Demand; Cost = Vertice.GetDistance(problem.Graph.Vertices[startingNode], problem.Graph.Vertices[problem.Graph.DepotIndex]); notVisited.RemoveAt(startingNode - 1); while (notVisited.Count > 0) { while (capacity > 0 && notVisited.Count > 0) { int nextNodeIndex = GetNextVertice(notVisited, currentNode, capacity); if (nextNodeIndex == -1) { break; } Cost += Vertice.GetDistance(currentNode, notVisited[nextNodeIndex]); capacity -= notVisited[nextNodeIndex].Demand; route.Add(notVisited[nextNodeIndex].Id); pheromoneTrait.Add(notVisited[nextNodeIndex].Id); currentNode = notVisited[nextNodeIndex]; notVisited.RemoveAt(nextNodeIndex); } Cost += Vertice.GetDistance(currentNode, problem.Graph.Vertices[problem.Graph.DepotIndex]); Routes.Add(route.ToArray()); pheromoneTrait.Add(problem.Graph.DepotIndex); currentNode = problem.Graph.Vertices[problem.Graph.DepotIndex]; route = new List <int>(); capacity = problem.Capacity; } Mutate(iterationNumber); // if (show) // { // Console.WriteLine("%%%%%%%%%%%%%"); // // foreach (var route2 in Routes) // { // foreach (var customer in route2) // { // Console.Write(customer + " "); // } // Console.WriteLine(); // Console.WriteLine(); // Console.WriteLine(); // // } // Console.WriteLine("%%%%%%%%%%%%%"); // Console.WriteLine("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); // // } }