public async Task RunAsync() { Load(); PathAndCost pnc = await DoBranchAndBound(citiesInOrder); bestScenarioFound = pnc.Path; }
/// <returns>Best route</returns> async public Task <PathAndCost> DoBranchAndBound( List <City> points, List <City> route = null, List <City> visited = null, float?overallBest = float.PositiveInfinity) { if (visited == null) { var firstPoint = points[0]; points.Remove(firstPoint); route = new List <City>(); route.Add(firstPoint); visited = new List <City>(); } var available = points.Except(visited).ToList(); var error = CalculateError(route); if (error > overallBest) { //Console.WriteLine("Bounding here because this is worst than the best solution"); return(new PathAndCost { Path = null, Cost = null }); } if (available.Count == 0) { //bestScenarioFound = route; for (int i = 0; i < route.Count; i++) { Console.Write(route[i].Id + ", "); } return(new PathAndCost { Path = route, Cost = error }); } float? bestCost = float.PositiveInfinity; List <City> bestPath = null; foreach (var c in available) { //var copy = c.Copy(); visited.Add(c); route.Add(c); PathAndCost currentPathAndCost = await DoBranchAndBound(points, route, visited, overallBest); if (currentPathAndCost.Cost < bestCost) { //Console.WriteLine("Found an improvement."); bestCost = currentPathAndCost.Cost; bestPath = currentPathAndCost.Path; if (bestCost < overallBest) { Console.WriteLine($"best Error = {bestCost}, overall best = {overallBest}"); overallBest = bestCost; //Console.WriteLine($"cities in route: {currentPathAndCost.Path.Count}"); } } //Console.WriteLine($"Removing inserted indexes {visitedInsertionIndex} and {routeInsertionIndex}"); visited.Remove(c); route.Remove(c); } return(new PathAndCost { Cost = bestCost, Path = bestPath }); }