public void UpdateCostNeighboursTest(int firstIndex, int secondIndex) { var solution = new uint[] { 3, 2, 0, 1, 4 }; var filename = "../../../simpleMatrix"; var xmlLoader = new XMLDataLoader(); var loadedInstance = xmlLoader.LoadInstance(filename); heuristic.UseInstance(loadedInstance); heuristic.Solution = solution; var initialCost = heuristic.CalculateCost(); var swapper = new DefaultSwapper(); var updatedCost = heuristic.CalculateSwapCost(solution, initialCost, firstIndex, secondIndex); swapper.Swap(solution, firstIndex, secondIndex); var updateCalculatedCost = heuristic.CalculateCost(); Assert.Equal(updateCalculatedCost, updatedCost); // revert the swap updatedCost = heuristic.CalculateSwapCost(solution, updatedCost, firstIndex, secondIndex); swapper.Swap(solution, firstIndex, secondIndex); updateCalculatedCost = heuristic.CalculateCost(); Assert.Equal(updateCalculatedCost, updatedCost); }
public void UpdateCostBR17InstanceTest() { var solution = new uint[] { 3, 2, 1, 0, 4, 6, 12, 11, 7, 8, 5, 9, 13, 15, 16, 14, 10 }; var filename = "../../../../instances/br17/br17"; var xmlLoader = new XMLDataLoader(); var loadedInstance = xmlLoader.LoadInstance(filename); heuristic.UseInstance(loadedInstance); heuristic.Solution = solution; var initialCost = heuristic.CalculateCost(); var swapper = new DefaultSwapper(); var randomizer = new Random(0); var updatedCost = initialCost; for (int i = 0; i < 10000; i++) { var firstToSwap = randomizer.Next(solution.Length - 1); var secondToSwap = randomizer.Next(solution.Length - 1); updatedCost = heuristic.CalculateSwapCost(solution, updatedCost, firstToSwap, secondToSwap); swapper.Swap(solution, firstToSwap, secondToSwap); var updateCalculatedCost = heuristic.CalculateCost(); Assert.Equal(updateCalculatedCost, updatedCost); updatedCost = heuristic.CalculateSwapCost(solution, updatedCost, firstToSwap, secondToSwap); swapper.Swap(solution, firstToSwap, secondToSwap); updateCalculatedCost = heuristic.CalculateCost(); Assert.Equal(updateCalculatedCost, updatedCost); } }
public override void NextStep() { if (Steps == 0) { currentCost = CalculateCost(); } var size = Solution.Length; var visited = new bool[size]; var currentNode = (int)Solution[0]; visited[currentNode] = true; var i = 1; while (!visited.All(x => x)) { currentNode = ChooseClosest(visited, vertices, currentNode); currentCost = CalculateSwapCost(Solution, currentCost, i, currentNode); swapper.Swap(Solution, i, currentNode); visited[currentNode] = true; i++; } IsEnd = true; }