public void TestShift1and3() { // create the problem and make sure 0->1->2->3->4 is the solution. var objective = new TSPTWObjective(); var problem = TSPTWHelper.CreateDirectedTSPTW(0, 0, 5, 10, 1); problem.Windows[2] = new TimeWindow() { Min = 1, Max = 3 }; // create a route with one shift. var route = new Optimization.Tours.Tour(new int[] { 0, 5, 8, 12, 17 }, 0); // apply the operator this should shift 5 to 4. var localSearch = new Local1ShiftTurn(); var delta = 0.0f; Assert.IsTrue(localSearch.Apply(problem, objective, route, out delta)); Assert.AreEqual(2, delta); Assert.AreEqual(new int[] { 0, 4, 8, 12, 17 }, route.ToArray()); // apply the operator again, this should shift 17 to 16. Assert.IsTrue(localSearch.Apply(problem, objective, route, out delta)); Assert.AreEqual(2, delta); Assert.AreEqual(new int[] { 0, 4, 8, 12, 16 }, route.ToArray()); }
public void Test1MovePossible() { // create the problem and make sure 0->1->2->3->4 is the solution. var objective = new TSPTWObjective(); var problem = TSPTWHelper.CreateTSPTW(0, 0, 5, 10); problem.Times[0][1] = 1; problem.Times[1][2] = 1; problem.Times[2][3] = 1; problem.Times[3][4] = 1; problem.Times[4][0] = 1; problem.Times[3][1] = 100; var route = new Optimization.Tours.Tour(new int[] { 0, 3, 2, 1, 4 }, 0); var localSearch = new Local2Opt(); var delta = 0.0f; Assert.IsTrue(localSearch.Apply(problem, objective, route, out delta)); // test result. Assert.AreEqual(36, delta); Assert.AreEqual(new int[] { 0, 1, 2, 3, 4 }, route.ToArray()); }
public void Test1MoveImpossible() { // create the problem and make sure 0->1->2->3->4 is the solution. var objective = new TSPTWObjective(); var problem = TSPTWHelper.CreateTSPTW(0, 0, 5, 10); problem.Times[0][1] = 1; problem.Times[0][3] = 2; problem.Times[1][2] = 1; problem.Times[2][3] = 1; problem.Times[3][4] = 1; problem.Times[4][0] = 1; problem.Times[3][1] = 100; problem.Windows[3] = new TimeWindow() { Min = 1, Max = 2 }; problem.Windows[2] = new TimeWindow() { Min = 11, Max = 12 }; var route = new Optimization.Tours.Tour(new int[] { 0, 3, 2, 1, 4 }, 0); var localSearch = new Local2Opt(); var delta = 0.0f; Assert.IsFalse(localSearch.Apply(problem, objective, route, out delta)); }
public void Test1MoveImpossible() { // create the problem and make sure 0->1->2->3->4 is the solution. var objective = new TSPTWObjective(); var problem = TSPTWHelper.CreateDirectedTSPTW(0, 0, 5, 10, 1); problem.Times.SetWeight(0, 1, 1); problem.Times.SetWeight(0, 3, 2); problem.Times.SetWeight(1, 2, 1); problem.Times.SetWeight(2, 3, 1); problem.Times.SetWeight(3, 4, 1); problem.Times.SetWeight(4, 0, 1); problem.Times.SetWeight(3, 1, 100); problem.Windows[3] = new TimeWindow() { Min = 1, Max = 2 }; problem.Windows[2] = new TimeWindow() { Min = 11, Max = 12 }; var route = new Optimization.Tours.Tour(new int[] { 0, 13, 9, 4, 16 }, 0); var localSearch = new Local2Opt(); var delta = 0.0f; Assert.IsFalse(localSearch.Apply(problem, objective, route, out delta)); }
public void Test1MovePossible() { // create the problem and make sure 0->1->2->3->4 is the solution. var objective = new TSPTWObjective(); var problem = TSPTWHelper.CreateDirectedTSPTW(0, 0, 5, 10, 1); problem.Times.SetWeight(0, 1, 1); problem.Times.SetWeight(1, 2, 1); problem.Times.SetWeight(2, 3, 1); problem.Times.SetWeight(3, 4, 1); problem.Times.SetWeight(4, 0, 1); problem.Times.SetWeight(3, 1, 100); var route = new Optimization.Tours.Tour(new int[] { 0, 13, 9, 5, 17 }, 0); var localSearch = new Local2Opt(); var delta = 0.0f; Assert.IsTrue(localSearch.Apply(problem, objective, route, out delta)); // test result. Assert.AreEqual(42, delta); Assert.AreEqual(new int[] { 0, 4, 8, 12, 17 }, route.ToArray()); }
public void TestPerturbationSomeViolations() { // set the seed manually. RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create the perturber. var perturber = new Random1Shift <TSPTWObjective>(); // create a problem where any move would violate windows. var problem = TSPTWHelper.CreateDirectedTSPTW(0, 0, 5, 2, 1); problem.Windows[2] = new TimeWindow() { Min = 4, Max = 5 }; problem.Windows[3] = new TimeWindow() { Min = 6, Max = 7 }; var objective = new TSPTWObjective(); // execute random shifts. for (int i = 0; i < 1000; i++) { // create solution. var solution = new Optimization.Tours.Tour(new int[] { 0, 4, 8, 12, 16 }); var fitnessBefore = objective.Calculate(problem, solution); // shift one customer. float difference; perturber.Apply(problem, objective, solution, out difference); // check if valid solution. var solutionList = new List <int>(); foreach (var directed in solution) { solutionList.Add(DirectedHelper.ExtractId(directed)); } Assert.AreEqual(0, solutionList[0]); Assert.IsTrue(solutionList.Remove(0)); Assert.IsTrue(solutionList.Remove(1)); Assert.IsTrue(solutionList.Remove(2)); Assert.IsTrue(solutionList.Remove(3)); Assert.IsTrue(solutionList.Remove(4)); Assert.AreEqual(0, solutionList.Count); // test if first is still first. Assert.AreEqual(problem.First, solution.First); // calculate expected difference. var fitnessAfter = objective.Calculate(problem, solution); Assert.AreEqual(fitnessAfter - fitnessBefore, difference, 0.0000001); } }
public void TestSolution5ClosedFixed() { // create problem. var problem = TSPTWHelper.CreateDirectedTSPTW(0, 4, 5, 10, 1); problem.Times.SetWeight(0, 1, 2); problem.Times.SetWeight(1, 2, 2); problem.Times.SetWeight(2, 3, 2); problem.Times.SetWeight(3, 4, 2); problem.Times.SetWeight(4, 0, 2); problem.Windows[2] = new TimeWindow() { Min = 3, Max = 5 }; problem.Windows[4] = new TimeWindow() { Min = 7, Max = 9 }; var objective = new TSPTWObjective(); // create the solver. var solver = new VNSSolver(); for (int i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, objective, out fitness); // test contents. Assert.IsTrue(fitness <= 12); var solutionList = new List <int>(); foreach (var directed in solution) { solutionList.Add(DirectedHelper.ExtractId(directed)); } Assert.AreEqual(5, solutionList.Count); Assert.AreEqual(0, solutionList[0]); Assert.AreEqual(4, solutionList[4]); } }
public void TestSolution5ClosedNotFixed() { // create problem. var problem = TSPTWHelper.CreateTSPTW(0, 0, 5, 10); problem.Times[0][1] = 2; problem.Times[1][2] = 2; problem.Times[2][3] = 2; problem.Times[3][4] = 2; problem.Times[4][0] = 2; problem.Windows[2] = new TimeWindow() { Min = 3, Max = 5 }; problem.Windows[4] = new TimeWindow() { Min = 7, Max = 9 }; var objective = new TSPTWObjective(); // create the solver. var solver = new VNSSolver(); for (int i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, objective, out fitness); // test contents. Assert.AreEqual(10, fitness); var solutionList = new List <int>(solution); Assert.AreEqual(0, solutionList[0]); Assert.IsTrue(solutionList.Remove(0)); Assert.IsTrue(solutionList.Remove(1)); Assert.IsTrue(solutionList.Remove(2)); Assert.IsTrue(solutionList.Remove(3)); Assert.IsTrue(solutionList.Remove(4)); Assert.AreEqual(0, solutionList.Count); } }
public void TestPerturbation() { // set the seed manually. RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create the perturber. var perturber = new Random1Shift <TSPTWObjective>(); // create a problem. var problem = TSPTWHelper.CreateTSPTW(0, 0, 5, 10); var objective = new TSPTWObjective(); // execute random shifts. for (int i = 0; i < 1000; i++) { // create solution. var solution = new Optimization.Tours.Tour(new int[] { 0, 1, 2, 3, 4 }); var fitnessBefore = objective.Calculate(problem, solution); // shift one customer. float difference; perturber.Apply(problem, objective, solution, out difference); // check if valid solution. var solutionList = new List <int>(solution); Assert.AreEqual(0, solutionList[0]); Assert.IsTrue(solutionList.Remove(0)); Assert.IsTrue(solutionList.Remove(1)); Assert.IsTrue(solutionList.Remove(2)); Assert.IsTrue(solutionList.Remove(3)); Assert.IsTrue(solutionList.Remove(4)); Assert.AreEqual(0, solutionList.Count); // test if first is still first. Assert.AreEqual(problem.First, solution.First); // calculate expected difference. var fitnessAfter = objective.Calculate(problem, solution); Assert.AreEqual(fitnessAfter - fitnessBefore, difference, 0.0000001); } }
public void TestSolution1() { // create problem. var problem = TSPTWHelper.CreateDirectedTSPTW(0, 0, 1, 0, 1); var objective = new TSPTWObjective(); // create the solver. var solver = new VNSSolver(); for (int i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, objective, out fitness); // test contents. Assert.IsTrue(fitness < 2); var solutionList = new List <int>(solution); Assert.AreEqual(0, DirectedHelper.ExtractId(solutionList[0])); } }
public void TestSolution1() { // create problem. var problem = TSPTWHelper.CreateTSPTW(0, 0, 1, 0); var objective = new TSPTWObjective(); // create the solver. var solver = new VNSSolver(); for (int i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, objective, out fitness); // test contents. Assert.AreEqual(0, fitness); var solutionList = new List <int>(solution); Assert.AreEqual(0, solutionList[0]); Assert.IsTrue(solutionList.Remove(0)); Assert.AreEqual(0, solutionList.Count); } }
public void TestSolution5ClosedNotFixed() { // create problem. var problem = TSPTWHelper.CreateDirectedTSPTW(0, 0, 5, 10, 1); problem.Times.SetWeight(0, 1, 2); problem.Times.SetWeight(1, 2, 2); problem.Times.SetWeight(2, 3, 2); problem.Times.SetWeight(3, 4, 2); problem.Times.SetWeight(4, 0, 2); problem.Windows[2] = new TimeWindow() { Min = 3, Max = 5 }; problem.Windows[4] = new TimeWindow() { Min = 7, Max = 9 }; var objective = new TSPTWObjective(); // create the solver. var solver = new VNSSolver(); for (int i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, objective, out fitness); // test contents. Assert.AreEqual(10, fitness); var solutionList = new List <int>(solution); Assert.AreEqual(5, solutionList.Count); } }