public void TestFirstAndLast() { var route = new Optimization.Tours.Tour(new int[] { 0 }, 0); Assert.AreEqual(0, route.First); Assert.AreEqual(0, route.Last); route.InsertAfter(0, 1); Assert.AreEqual(0, route.First); Assert.AreEqual(0, route.Last); route = new Optimization.Tours.Tour(new int[] { 0 }, null); Assert.AreEqual(0, route.First); Assert.AreEqual(null, route.Last); route.InsertAfter(0, 1); Assert.AreEqual(0, route.First); Assert.AreEqual(null, route.Last); var routeEnumerable = new int[] { 0, 1, 3, 2, 4 }; route = new Optimization.Tours.Tour(routeEnumerable, 4); Assert.AreEqual(0, route.First); Assert.AreEqual(4, route.Last); route = new Optimization.Tours.Tour(routeEnumerable, 0); Assert.AreEqual(0, route.First); Assert.AreEqual(0, route.Last); route = new Optimization.Tours.Tour(routeEnumerable, null); Assert.AreEqual(0, route.First); Assert.AreEqual(null, route.Last); }
public void Test4Reset() { var route = new Optimization.Tours.Tour(new int[] { 0, 1, 2, 3, 4 }, 0); var pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed()); var pairEnumerator = pairEnumerable.GetEnumerator(); var pairs = new List <Pair>(); while (pairEnumerator.MoveNext()) { pairs.Add(pairEnumerator.Current); } Assert.AreEqual(5, pairs.Count); Assert.Contains(new Pair(0, 1), pairs); Assert.Contains(new Pair(1, 2), pairs); Assert.Contains(new Pair(2, 3), pairs); Assert.Contains(new Pair(3, 4), pairs); Assert.Contains(new Pair(4, 0), pairs); pairEnumerator.Reset(); pairs = new List <Pair>(); while (pairEnumerator.MoveNext()) { pairs.Add(pairEnumerator.Current); } Assert.AreEqual(5, pairs.Count); Assert.Contains(new Pair(0, 1), pairs); Assert.Contains(new Pair(1, 2), pairs); Assert.Contains(new Pair(2, 3), pairs); Assert.Contains(new Pair(3, 4), pairs); Assert.Contains(new Pair(4, 0), pairs); }
public void TestClone() { var routeEnumerable = new int[] { 0, 1, 3, 2, 4 }; var route = new Optimization.Tours.Tour(routeEnumerable, 0); var cloned = route.Clone() as Optimization.Tours.Tour; Assert.AreEqual(route.First, cloned.First); Assert.AreEqual(route.Last, cloned.Last); Assert.AreEqual(route.Count, cloned.Count); ExtraAssert.ItemsAreEqual(route, cloned); routeEnumerable = new int[] { 0, 1, 3, 2, 4 }; route = new Optimization.Tours.Tour(routeEnumerable, 4); cloned = route.Clone() as Optimization.Tours.Tour; Assert.AreEqual(route.First, cloned.First); Assert.AreEqual(route.Last, cloned.Last); Assert.AreEqual(route.Count, cloned.Count); ExtraAssert.ItemsAreEqual(route, cloned); routeEnumerable = new int[] { 0, 1, 3, 2, 4 }; route = new Optimization.Tours.Tour(routeEnumerable, null); cloned = route.Clone() as Optimization.Tours.Tour; Assert.AreEqual(route.First, cloned.First); Assert.AreEqual(route.Last, cloned.Last); Assert.AreEqual(route.Count, cloned.Count); ExtraAssert.ItemsAreEqual(route, cloned); }
public void TestRemoveOpen() { // create a new empty route. int count = 100; var customers = new List <int>(); while (count > 0) { customers = new List <int>(new int[] { 0, 1, 2, 3, 4 }); var route = new Optimization.Tours.Tour(customers, null); // test removing first. Assert.Catch <InvalidOperationException>(() => { route.Remove(route.First); }); // remove customers. while (customers.Count > 2) { var customerIdx = RandomGeneratorExtensions.GetRandom().Generate( customers.Count); var customer = customers[customerIdx]; if (customer != route.First && customer != route.Last) { customers.Remove(customer); route.Remove(customer); Assert.AreEqual(customers.Count, route.Count); Assert.AreEqual(0, route.First); Assert.AreEqual(null, route.Last); } } count--; } }
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 TestInsertAfterClose() { var route = new Optimization.Tours.Tour(new int[] { 0, 1, 2, 3 }, 0); // test arguments. Assert.Catch <ArgumentOutOfRangeException>(() => { route.InsertAfter(0, -1); }); Assert.Catch <ArgumentOutOfRangeException>(() => { route.InsertAfter(-1, 0); }); Assert.Catch <ArgumentException>(() => { route.InsertAfter(1, 1); }); Assert.Catch <ArgumentOutOfRangeException>(() => { route.InsertAfter(10, 11); }); // insert customers. Assert.AreEqual(4, route.Count); for (var customer = 4; customer < 100; customer++) { route.InsertAfter(customer - 1, customer); Assert.AreEqual(customer + 1, route.Count); Assert.AreEqual(0, route.First); Assert.AreEqual(0, route.Last); } }
public void TestEnumerateBetweenClosed() { var route = new Optimization.Tours.Tour(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 0); for (var from = 0; from < route.Count; from++) { for (var to = 0; to < route.Count; to++) { var enumerator = route.Between(from, to).GetEnumerator(); if (from <= to) { for (var customer = from; customer < to + 1; customer++) { Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(customer, enumerator.Current); } } else { for (var customer = from; customer < route.Count; customer++) { Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(customer, enumerator.Current); } for (var customer = 0; customer < to + 1; customer++) { Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(customer, enumerator.Current); } } Assert.IsFalse(enumerator.MoveNext()); } } }
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.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 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 TestOneShiftNonViolatedForward() { // create the problem and make sure 0->1->2->3->4 is the solution. var objective = new TSPTWFeasibleObjective(); var problem = TSPTWHelper.CreateTSPTW(0, 0, 5, 2); problem.Windows[2] = new TimeWindow() { Min = 1, Max = 3 }; // create a route with one shift. var route = new Optimization.Tours.Tour(new int[] { 0, 1, 2, 3, 4 }, 0); // apply the 1-shift local search, it should find the customer to replocate. var localSearch = new Local1Shift <TSPTWFeasibleObjective>(); var delta = 0.0f; Assert.IsTrue(localSearch.MoveNonViolatedForward(problem, objective, route, out delta)); // shifts 1 after 2. // test result. Assert.AreEqual(1, delta); Assert.AreEqual(new int[] { 0, 2, 1, 3, 4 }, route.ToArray()); // create a feasible route. route = new Optimization.Tours.Tour(new int[] { 0, 2, 4, 1, 3 }, 0); // apply the 1-shift local search, it should find the customer to replocate. Assert.IsFalse(localSearch.MoveNonViolatedForward(problem, objective, route, out delta)); }
public void TestFixedViolatedUnmovableCustomerValidBackward() { // create the problem and make sure 0->1->2->3->4 is the solution. var objective = new TSPTWFeasibleObjective(); var problem = TSPTWHelper.CreateTSPTW(0, 4, 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[4] = new TimeWindow() { Min = 7, Max = 9 }; // create a route with one shift. var route = new Optimization.Tours.Tour(new int[] { 0, 1, 3, 2, 4 }, 0); // apply the 1-shift local search, it should find the customer to replocate. var localSearch = new Local1Shift <TSPTWFeasibleObjective>(); var delta = 0.0f; Assert.IsTrue(localSearch.MoveNonViolatedBackward(problem, objective, route, out delta)); // shifts 2 after 1 // test result. Assert.AreEqual(23, delta); Assert.AreEqual(new int[] { 0, 1, 2, 3, 4 }, route.ToArray()); }
public void TestClosed() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create the problem and make sure 0->1->2->3->4 is the solution. var problem = STSPHelper.CreateSTSP(0, 0, 5, 10, 40); problem.Weights[0][1] = 1; problem.Weights[1][2] = 1; problem.Weights[2][3] = 1; problem.Weights[3][4] = 1; problem.Weights[4][0] = 1; // create a route with one shift. var route = new Optimization.Tours.Tour(new int[] { 0, 2, 3, 1, 4 }, 0); // apply the 1-shift local search, it should find the customer to replocate. var localSearch = new CheapestInsertionOperator(n: 50); STSPFitness delta; Assert.IsTrue(localSearch.Apply(problem, new STSPObjective(), route, out delta)); // test result. Assert.AreEqual(-27, delta.Weight); Assert.AreEqual(0, delta.Customers); Assert.AreEqual(new int[] { 0, 1, 2, 3, 4 }, route.ToArray()); }
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 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 TestClear() { var routeEnumerable = new int[] { 0, 1, 3, 2, 4 }; var route = new Optimization.Tours.Tour(routeEnumerable, 0); route.Clear(); Assert.AreEqual(1, route.Count); Assert.AreEqual(0, route.First); Assert.AreEqual(0, route.Last); }
public void Test2Closed() { var route = new Optimization.Tours.Tour(new int[] { 0, 1, 2, 3, 4 }, 0); var pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed()); var pairs = new List <Pair>(pairEnumerable); Assert.AreEqual(5, pairs.Count); Assert.Contains(new Pair(0, 1), pairs); Assert.Contains(new Pair(1, 2), pairs); Assert.Contains(new Pair(2, 3), pairs); Assert.Contains(new Pair(3, 4), pairs); Assert.Contains(new Pair(4, 0), pairs); pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed(), 0); pairs = new List <Pair>(pairEnumerable); Assert.AreEqual(5, pairs.Count); Assert.Contains(new Pair(0, 1), pairs); Assert.Contains(new Pair(1, 2), pairs); Assert.Contains(new Pair(2, 3), pairs); Assert.Contains(new Pair(3, 4), pairs); Assert.Contains(new Pair(4, 0), pairs); pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed(), 1); pairs = new List <Pair>(pairEnumerable); Assert.AreEqual(4, pairs.Count); Assert.Contains(new Pair(1, 2), pairs); Assert.Contains(new Pair(2, 3), pairs); Assert.Contains(new Pair(3, 4), pairs); Assert.Contains(new Pair(4, 0), pairs); pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed(), 2); pairs = new List <Pair>(pairEnumerable); Assert.AreEqual(3, pairs.Count); Assert.Contains(new Pair(2, 3), pairs); Assert.Contains(new Pair(3, 4), pairs); Assert.Contains(new Pair(4, 0), pairs); pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed(), 3); pairs = new List <Pair>(pairEnumerable); Assert.AreEqual(2, pairs.Count); Assert.Contains(new Pair(3, 4), pairs); Assert.Contains(new Pair(4, 0), pairs); pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed(), 4); pairs = new List <Pair>(pairEnumerable); Assert.AreEqual(1, pairs.Count); Assert.Contains(new Pair(4, 0), pairs); }
public void TestShiftOpen() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(116542346); // create a new empty route. var testCount = 10; var customers = new List <int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); var route = new Optimization.Tours.Tour(customers, null); // remove customers. int testIdx = 0; while (testIdx < testCount) { var customerIdx = RandomGeneratorExtensions.GetRandom().Generate( customers.Count); var insertIdx = RandomGeneratorExtensions.GetRandom().Generate( customers.Count - 2); if (customerIdx <= insertIdx) { insertIdx = insertIdx + 1; } var customer = customers[customerIdx]; var insert = customers[insertIdx]; if (customer != route.First && customer != route.Last) { if (customerIdx < insertIdx) { customers.Insert(insertIdx + 1, customer); customers.RemoveAt(customerIdx); } else { customers.RemoveAt(customerIdx); customers.Insert(insertIdx + 1, customer); } route.ShiftAfter(customer, insert); Assert.AreEqual(customers.Count, route.Count); Assert.AreEqual(0, route.First); Assert.AreEqual(null, route.Last); ExtraAssert.ItemsAreEqual(customers, route); testIdx++; } } }
public void Test6TwoCustomers() { var route = new Optimization.Tours.Tour(new int[] { 0, 1 }, 0); var pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed()); var pairs = new List <Pair>(pairEnumerable); Assert.AreEqual(2, pairs.Count); route = new Optimization.Tours.Tour(new int[] { 0, 1 }, null); pairEnumerable = new PairEnumerable <Tour>(route, route.IsClosed()); pairs = new List <Pair>(pairEnumerable); Assert.AreEqual(1, pairs.Count); }
public void TestClosed() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create the problem and make sure 0->1->2->3->4 is the solution. var problem = STSPHelper.CreateDirectedSTSP(0, 0, 5, 10, 1, 40); // create a route with one shift. var route = new Optimization.Tours.Tour(new int[] { 0, 4, 9, 12, 16 }, 0); // apply the operator. var localSearch = new DirectionLocalSearchOperator(); STSPFitness delta; Assert.IsTrue(localSearch.Apply(problem, new STSPObjective(), route, out delta)); }
public void TestGetNeighboursClosed() { var count = 10; var customers = new List <int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); var route = new Optimization.Tours.Tour(customers, 0); var neighbour = Constants.NOT_SET; for (int customer = 0; customer < count - 1; customer++) { neighbour = route.GetNeigbour(customer); Assert.AreEqual(customer + 1, neighbour); } neighbour = route.GetNeigbour(count - 1); Assert.AreEqual(0, neighbour); }
public void TestEnumerableConstructorOpen() { var routeEnumerable = new int[] { 0, 1, 3, 2, 4 }; var route = new Optimization.Tours.Tour(routeEnumerable, null); Assert.AreEqual(5, route.Count); Assert.AreEqual(0, route.First); Assert.AreEqual(null, route.Last); var solutionList = new List <int>(route); Assert.AreEqual(0, solutionList[0]); Assert.AreEqual(1, solutionList[1]); Assert.AreEqual(3, solutionList[2]); Assert.AreEqual(2, solutionList[3]); Assert.AreEqual(4, solutionList[4]); }
public void Test5CrossOverOpen() { // create problem. var problem = TSPHelper.CreateTSP(0, 5, 10); // create solutions. var solution1 = new Optimization.Tours.Tour(new int[] { 0, 1, 2, 3, 4 }, null); var solution2 = new Optimization.Tours.Tour(new int[] { 0, 1, 3, 2, 4 }, null); // execute crossover. var crossover = new EAXOperator(); float fitness; var result = crossover.Apply(problem, new TSPObjective(), solution1, solution2, out fitness); Assert.IsNotNull(result); Assert.AreEqual(5, result.Count); }
public void TestReplaceClosed() { var customers = new List <int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); var route = new Optimization.Tours.Tour(customers, 0); route.Replace(0, 10); ExtraAssert.ItemsAreEqual(new int[] { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, route); route.Replace(5, 15); ExtraAssert.ItemsAreEqual(new int[] { 10, 1, 2, 3, 4, 15, 6, 7, 8, 9 }, route); route.Replace(9, 19); ExtraAssert.ItemsAreEqual(new int[] { 10, 1, 2, 3, 4, 15, 6, 7, 8, 19 }, route); }
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 TestGetCustomerAt() { var routeEnumerable = new int[] { 0, 1, 3, 2, 4 }; var route = new Optimization.Tours.Tour(routeEnumerable, 0); Assert.AreEqual(routeEnumerable[0], route.GetCustomerAt(0)); Assert.AreEqual(routeEnumerable[1], route.GetCustomerAt(1)); Assert.AreEqual(routeEnumerable[2], route.GetCustomerAt(2)); Assert.AreEqual(routeEnumerable[3], route.GetCustomerAt(3)); Assert.AreEqual(routeEnumerable[4], route.GetCustomerAt(4)); Assert.Catch <ArgumentOutOfRangeException>(() => { route.GetCustomerAt(-1); }); Assert.Catch <ArgumentOutOfRangeException>(() => { route.GetCustomerAt(5); }); }
public void TestEnumerateFixed() { var list = new List <int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); var route = new Optimization.Tours.Tour(list, 9); var enumeratedRoute = new List <int>(route); Assert.AreEqual(list, enumeratedRoute); var start = 0; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 1; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 2; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 3; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 4; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 5; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 6; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 7; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 8; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); start = 9; enumeratedRoute = route.GetEnumerator(start).ToList(); Assert.AreEqual(list.GetRange(start, list.Count - start).ToArray(), enumeratedRoute.ToArray()); }
public void TestClosed() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create the problem and make sure 0->1->2->3->4 is the solution. var problem = STSPHelper.CreateDirectedSTSP(0, 0, 5, 10, 1, 40); problem.Weights.SetWeight(0, 1, 1); problem.Weights.SetWeight(1, 2, 1); problem.Weights.SetWeight(2, 3, 1); problem.Weights.SetWeight(3, 4, 1); problem.Weights.SetWeight(4, 0, 1); // apply the operator. var route = new Optimization.Tours.Tour(new int[] { 0, 12, 4, 16 }, 0); // apply the 1-shift local search, it should find the customer to replocate. var localSearch = new CheapestInsertionOperator(4, 10); STSPFitness delta; Assert.IsTrue(localSearch.Apply(problem, new STSPObjective(), route, out delta)); }
public void TestContainsFixed() { var routeEnumerable = new int[] { 0, 1, 2, 3, 4 }; var route = new Optimization.Tours.Tour(routeEnumerable, 4); Assert.IsFalse(route.Contains(10)); Assert.IsTrue(route.Contains(0)); Assert.IsTrue(route.Contains(1)); Assert.IsTrue(route.Contains(2)); Assert.IsTrue(route.Contains(3)); Assert.IsTrue(route.Contains(4)); Assert.IsFalse(route.Contains(8, 0)); Assert.IsFalse(route.Contains(4, 2)); Assert.IsFalse(route.Contains(3, 2)); Assert.IsTrue(route.Contains(0, 1)); Assert.IsTrue(route.Contains(1, 2)); Assert.IsTrue(route.Contains(2, 3)); Assert.IsTrue(route.Contains(3, 4)); Assert.IsFalse(route.Contains(4, 0)); }
public void TestOneShiftViolatedForward() { // create the problem and make sure 0->1->2->3->4 is the solution. var objective = new TSPTWFeasibleObjective(); var problem = TSPTWHelper.CreateDirectedTSPTW(0, 0, 5, 10, 1); problem.Times.SetWeight(0, 3, 1, 1, 1, 1); // [0][3] = 1; problem.Times.SetWeight(3, 1, 1, 1, 1, 1); // [3][1] = 1; problem.Windows[1] = new TimeWindow() { Min = 1, Max = 3 }; problem.Windows[3] = new TimeWindow() { Min = 0, Max = 2 }; // create a route with one shift. var route = new Optimization.Tours.Tour(new int[] { 0, 4, 12, 8, 16 }, 0); // apply the 1-shift local search, it should find the customer to replocate. var localSearch = new Local1Shift <TSPTWFeasibleObjective>(); var delta = 0.0f; Assert.IsTrue(localSearch.MoveViolatedForward(problem, objective, route, out delta)); // shifts 4 after 12. // test result. Assert.AreEqual(25, delta); Assert.AreEqual(new int[] { 0, 12, 4, 8, 16 }, route.ToArray()); // create a feasible route. route = new Optimization.Tours.Tour(new int[] { 0, 12, 4, 8, 16 }, 0); // apply the 1-shift local search, it should find the customer to replocate. Assert.IsFalse(localSearch.MoveViolatedForward(problem, objective, route, out delta)); }