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 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); } }