Пример #1
0
        public void TestStopCondition()
        {
            // set the seed manually.
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create solver.
            var best = new SolutionMock()
            {
                Value = 1000
            };
            IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float> solver = null;

            solver = new IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), 10, (i, p, o, s) =>
            {
                if (s != null && best.Value > s.Value)
                {     // keep best solution.
                    best = s;
                    if (best.Value < 100)
                    {
                        return(true);
                    }
                }
                return(false);
            });

            // run solver.
            var solution = solver.Solve(new ProblemMock()
            {
                Max = 1000
            }, new ObjectiveMock());

            Assert.AreEqual(best.Value, solution.Value);
        }
Пример #2
0
        public void TestStop()
        {
            // set the seed manually.
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create solver.
            var best = new SolutionMock()
            {
                Value = 1000
            };
            IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float> solver = null;

            solver = new IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), 10, (i, p, o, s) =>
            {
                if (s != null && best.Value > s.Value)
                {     // keep best solution.
                    best = s;
                }
                if (i > 5)
                {     // stop solver.
                    solver.Stop();
                }
                if (i > 6)
                {     // solver was not stopped.
                    Assert.Fail("Solver has not stopped!");
                }
                // ... but always return false.
                return(false);
            });

            // run solver.
            var solution = solver.Solve(new ProblemMock()
            {
                Max = 1000
            }, new ObjectiveMock());

            Assert.AreEqual(best.Value, solution.Value);
        }