private async void SimulatedAnnealingLocalSearchTSP() { (int, int)switchPair; Cost = TSPCost(CityOrder); Temperature = 100d; double deltaCost, bestCost = double.MaxValue; List <int> bestAnswer = null; while (Temperature >= .5d) { for (int i = 0; i < 0x0000FFFF; i++) { //SA Cost = TSPCost(CityOrder); int seperateStart = Random.Next(0, N - 1); int seperateEnd = Random.Next(seperateStart + 1, N); CityOrder.Reverse(seperateStart, seperateEnd - seperateStart); switchPair = RandomPair(); SwapPair(CityOrder, switchPair); deltaCost = TSPCost(CityOrder) - Cost; if (deltaCost >= 0 && (Random.NextDouble() >= Math.Exp(-(deltaCost / Temperature)))) { SwapPair(CityOrder, switchPair); CityOrder.Reverse(seperateStart, seperateEnd - seperateStart); } if (Cost < bestCost) { bestCost = Cost; bestAnswer = new List <int>(CityOrder); } if (DisplayEveryStep) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { DisplayRoute(TSPCanvas); Bindings.Update(); }); } } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { DisplayRoute(TSPCanvas); Bindings.Update(); }); Temperature *= 0.99; } Cost = bestCost; CityOrder = new List <int>(bestAnswer); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { DisplayRoute(TSPCanvas); Bindings.Update(); TSPCostTextBlock.Text += " Done!"; }); }
private async void LocalSearchTSP() { Cost = TSPCost(CityOrder); double minCost, currentCost; List <int> bestNeighbour = new List <int>(); while (true) { minCost = double.MaxValue; bestNeighbour.Clear(); for (int i = 0; i < N; i++) { int seperateStart = Random.Next(0, N - 1); int seperateEnd = Random.Next(seperateStart + 1, N); CityOrder.Reverse(seperateStart, seperateEnd - seperateStart); currentCost = TSPCost(CityOrder); if (currentCost < minCost) { minCost = currentCost; bestNeighbour = CityOrder.ToList(); } CityOrder.Reverse(seperateStart, seperateEnd - seperateStart); } ////Random switch four //for (int i = 0; i < N * N; i++) { // var switchPair1 = RandomPair(); // var switchPair2 = RandomPair(); // SwapPair(CityOrder, switchPair1); // SwapPair(CityOrder, switchPair2); // currentCost = TSPCost(CityOrder); // if (currentCost < minCost) { // minCost = currentCost; // bestNeighbour = CityOrder.ToList(); // } // SwapPair(CityOrder, switchPair2); // SwapPair(CityOrder, switchPair1); //} //Switch two for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { SwapPair(CityOrder, (i, j)); currentCost = TSPCost(CityOrder); if (currentCost < minCost) { minCost = currentCost; bestNeighbour = CityOrder.ToList(); } SwapPair(CityOrder, (i, j)); } } //Check Dead End if (minCost == Cost) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { TSPCostTextBlock.Text += " DeadEnd X_X"; }); return; } CityOrder = bestNeighbour.ToList(); Cost = TSPCost(CityOrder); if (DisplayEveryStep) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { DisplayRoute(TSPCanvas); Bindings.Update(); }); } } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { DisplayRoute(TSPCanvas); TSPCostTextBlock.Text += " Done!"; }); }