Ejemplo n.º 1
0
        public void TestSolutionOpen()
        {
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create problem.
            var problem = TSPHelper.CreateTSP(0, 5, 10);

            // create the solver.
            var solver    = new RandomSolver();
            var objective = new TSPObjective();

            for (var i = 0; i < 100; i++)
            {
                // generate solution.
                float fitness;
                var   solution = solver.Solve(problem, objective, out fitness);

                // test contents.
                Assert.AreEqual(40, fitness);
                Assert.AreEqual(problem.First, solution.First);
                Assert.AreEqual(null, 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);
            }
        }
Ejemplo n.º 2
0
        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);
        }