public void TestSolutionClosed() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create problem. var problem = TSPHelper.CreateTSP(0, 0, 5, 10); // create the solver. var solver = new RandomSolver(); for (int i = 0; i < 100; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, new TSPObjective(), out fitness); // test contents. Assert.AreEqual(50, fitness); Assert.AreEqual(0, solution.First); Assert.AreEqual(0, solution.Last); 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 IActionResult GetPermutationsSimple(string vers) { List <String> result = new List <string>(); List <string> list = new List <string>() { "1", "2", "3", "4" }; IList <IList <string> > perms = TSPHelper.Permutations(list); foreach (IList <string> perm in perms) { string sequence = ""; foreach (string s in perm) { Console.Write(s); sequence += s + ", "; } result.Add(sequence); Console.WriteLine(); } return(Json(result.ToArray())); }
public void TestSolution1() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create problem. var problem = TSPHelper.CreateTSP(0, 0, 1, 0); // create the solver. var solver = new EAXSolver(new GASettings() { CrossOverPercentage = 10, ElitismPercentage = 1, PopulationSize = 100, MaxGenerations = 100000, MutationPercentage = 0, StagnationCount = 100 }); for (int i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, new TSPObjective(), 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 TestSolutions5NotClosedNotFixed() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create problem. var problem = TSPHelper.CreateDirectedTSP(0, 5, 10, 1); 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); // create the solver. var solver = new HillClimbing3OptSolver(); for (int i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, new TSPObjective(), out fitness); // test contents. Assert.IsTrue(fitness <= 7); var solutionList = new List <int>(solution); Assert.AreEqual(0, DirectedHelper.ExtractId(solutionList[0])); Assert.AreEqual(5, solutionList.Count); } }
public void TestOpen() { var problem = TSPHelper.CreateDirectedTSP(0, 10, 100, 5); var random = new RandomSolver(); var randomRoute = random.Solve(problem, new Optimization.TSP.Directed.TSPObjective()); var op = new DirectionLocalSearch(); float delta; var optimized = op.Apply(problem, new Optimization.TSP.Directed.TSPObjective(), randomRoute, out delta); }
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 IActionResult FindRouteWithNN() { // Get cities x,y coordiates from file List <City> cities = TSPHelper.ParseCitiesFromFile(DATASET_FILE_1); TSPNearestNeighbour nn = new TSPNearestNeighbour(cities); nn.Execute(); return(Json(new { distance = nn.GetTotalDistanceRoute(), cityOrder = nn.GetCitiesResultToString() })); //// // return Json(nn.GetCitiesResult()); }
public void Test5Closed() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create problem. var objective = new TSPObjective(); var problem = TSPHelper.CreateTSP(0, 0, 5, 10); 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; // solve problem. var solver = new HillClimbing3OptSolver(); var solution = solver.Solve(problem, objective); // check result. var last = solution.Last(); Assert.AreEqual(0, solution.Last); }
public void TestSolution1() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); // create problem. var problem = TSPHelper.CreateDirectedTSP(0, 0, 1, 0, 1); // create the solver. var solver = new HillClimbing3OptSolver(); for (int i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, new TSPObjective(), out fitness); // test contents. Assert.AreEqual(0, fitness); var solutionList = new List <int>(solution); Assert.AreEqual(0, DirectedHelper.ExtractId(solutionList[0])); Assert.AreEqual(1, solutionList.Count); } }
public void Test2TwoShiftsClosed() { // create the problem and make sure 0->1->2->3->4 is the solution. var problem = TSPHelper.CreateTSP(0, 0, 5, 10); 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, 4, 1, 3 }, 0); // apply the 1-shift local search, it should find the customer to replocate. var localSearch = new Local1Shift(); var delta = 0.0f; localSearch.Apply(problem, new TSPObjective(), route, out delta); // test result. Assert.AreEqual(-45, delta); Assert.AreEqual(new int[] { 0, 1, 2, 3, 4 }, route.ToArray()); }
public IActionResult FindRoute() { // Get cities x,y coordiates from file List <City> cities = TSPHelper.ParseCitiesFromFile(DATASET_FILE_1); // Take the first 4 IEnumerable <City> filteredCities = cities.Take <City>(4); // Get permunations for 4 cities List <string> list = new List <string>() { "1", "2", "3", "4" }; IList <IList <string> > perms = TSPHelper.Permutations(list); TSPRoute winner = TSPBasic.CalculateTSP(perms, filteredCities.ToArray()); // print the results in console string output = ""; Console.WriteLine(); Console.WriteLine(); output += "Winner route is: "; foreach (City city in winner.Cities) { output += city.Id.ToString() + ", "; } Console.WriteLine(output + " with distance: " + winner.TotalDist.ToString("#.00")); return(Json(new { distance = winner.TotalDist.ToString("#.00"), cityOrder = output })); }
public IActionResult GetCities(string vers) { List <City> cities = TSPHelper.ParseCitiesFromFile(DATASET_FILE_1); return(Json(cities.ToArray())); }
public void TestSolutionbr17Open() { RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247); var weights = new float[][] { new float[] { 9999, 3, 5, 48, 48, 8, 8, 5, 5, 3, 3, 0, 3, 5, 8, 8, 5 }, new float[] { 3, 9999, 3, 48, 48, 8, 8, 5, 5, 0, 0, 3, 0, 3, 8, 8, 5 }, new float[] { 5, 3, 9999, 72, 72, 48, 48, 24, 24, 3, 3, 5, 3, 0, 48, 48, 24 }, new float[] { 48, 48, 74, 9999, 0, 6, 6, 12, 12, 48, 48, 48, 48, 74, 6, 6, 12 }, new float[] { 48, 48, 74, 0, 9999, 6, 6, 12, 12, 48, 48, 48, 48, 74, 6, 6, 12 }, new float[] { 8, 8, 50, 6, 6, 9999, 0, 8, 8, 8, 8, 8, 8, 50, 0, 0, 8 }, new float[] { 8, 8, 50, 6, 6, 0, 9999, 8, 8, 8, 8, 8, 8, 50, 0, 0, 8 }, new float[] { 5, 5, 26, 12, 12, 8, 8, 9999, 0, 5, 5, 5, 5, 26, 8, 8, 0 }, new float[] { 5, 5, 26, 12, 12, 8, 8, 0, 9999, 5, 5, 5, 5, 26, 8, 8, 0 }, new float[] { 3, 0, 3, 48, 48, 8, 8, 5, 5, 9999, 0, 3, 0, 3, 8, 8, 5 }, new float[] { 3, 0, 3, 48, 48, 8, 8, 5, 5, 0, 9999, 3, 0, 3, 8, 8, 5 }, new float[] { 0, 3, 5, 48, 48, 8, 8, 5, 5, 3, 3, 9999, 3, 5, 8, 8, 5 }, new float[] { 3, 0, 3, 48, 48, 8, 8, 5, 5, 0, 0, 3, 9999, 3, 8, 8, 5 }, new float[] { 5, 3, 0, 72, 72, 48, 48, 24, 24, 3, 3, 5, 3, 9999, 48, 48, 24 }, new float[] { 8, 8, 50, 6, 6, 0, 0, 8, 8, 8, 8, 8, 8, 50, 9999, 0, 8 }, new float[] { 8, 8, 50, 6, 6, 0, 0, 8, 8, 8, 8, 8, 8, 50, 0, 9999, 8 }, new float[] { 5, 5, 26, 12, 12, 8, 8, 0, 0, 5, 5, 5, 5, 26, 8, 8, 9999 } }; // create problem. var problem = TSPHelper.CreateTSP(0, weights); // create the solver. var solver = new EAXSolver(new GASettings() { CrossOverPercentage = 10, ElitismPercentage = 1, PopulationSize = 100, MaxGenerations = 100000, MutationPercentage = 0, StagnationCount = 100 }); for (var i = 0; i < 10; i++) { // generate solution. float fitness; var solution = solver.Solve(problem, new TSPObjective(), out fitness); // test contents. Assert.IsTrue(fitness <= 39); 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.IsTrue(solutionList.Remove(5)); Assert.IsTrue(solutionList.Remove(6)); Assert.IsTrue(solutionList.Remove(7)); Assert.IsTrue(solutionList.Remove(8)); Assert.IsTrue(solutionList.Remove(9)); Assert.IsTrue(solutionList.Remove(10)); Assert.IsTrue(solutionList.Remove(11)); Assert.IsTrue(solutionList.Remove(12)); Assert.IsTrue(solutionList.Remove(13)); Assert.IsTrue(solutionList.Remove(14)); Assert.IsTrue(solutionList.Remove(15)); Assert.IsTrue(solutionList.Remove(16)); Assert.AreEqual(0, solutionList.Count); } }