Esempio n. 1
0
        public void TestOptimizeEmitsEventsInOrder()
        {
            IComparer <TestIntegerEvaluableState> comparer = new MaximizingComparer <TestIntegerEvaluableState>();

            generator = new TestIntegerLocalMaximaSuccessorGenerator();
            picker    = new ClimberSuccessorSelector <TestIntegerEvaluableState, int>(generator, comparer);
            algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(picker);

            List <int> encounteredStates = new List <int>();
            List <int> expectedStates    = new List <int>();

            for (int i = 3; i <= 50; i++)
            {
                expectedStates.Add(i);
            }

            void OnEvent(object source, ClimberStepEvent <TestIntegerEvaluableState, int> e)
            {
                encounteredStates.Add(e.CurrentState.Value);
            }

            algorithm.ClimbStepPerformedEvent += OnEvent;

            TestIntegerEvaluableState initialState = new TestIntegerEvaluableState(2);
            TestIntegerEvaluableState result       = algorithm.Optimize(initialState);

            Assert.AreEqual(expectedStates.Count, encounteredStates.Count);
            for (int i = 0; i < expectedStates.Count; i++)
            {
                Assert.AreEqual(encounteredStates[i], expectedStates[i], "Encountered states do not match");
            }
        }
Esempio n. 2
0
        public void TestOptimizeStopsAtLocalExtrema()
        {
            IComparer <TestIntegerEvaluableState> comparer = new MaximizingComparer <TestIntegerEvaluableState>();

            generator = new TestIntegerLocalMaximaSuccessorGenerator();
            picker    = new ClimberSuccessorSelector <TestIntegerEvaluableState, int>(generator, comparer);
            algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(picker);

            TestIntegerEvaluableState initialState = new TestIntegerEvaluableState(2);

            TestIntegerEvaluableState result = algorithm.Optimize(initialState);

            Assert.AreEqual(50, result.Value, "Optimized state is incorrect");
        }
Esempio n. 3
0
        /// <summary>
        /// Returns the configured <see cref="IClimberAlgorithm{TState, TEvaluation}"/> if it is available or creates a default one with the given configuration.
        /// </summary>
        /// <returns>The <see cref="IClimberAlgorithm{TState, TEvaluation}"/> for this configuration</returns>
        private IClimberAlgorithm <TState, TEvaluation> ResolveAlgorithm(IComparer <TState> comparer, Func <TState, IEnumerable <TState> > successorGenerationFunction)
        {
            IClimberAlgorithm <TState, TEvaluation> algorithm;

            // create a climber algorithm if one is not already configured
            if (ClimberAlgorithm == null)
            {
                ClimberSuccessorSelector <TState, TEvaluation> successorSelector = new ClimberSuccessorSelector <TState, TEvaluation>(comparer, successorGenerationFunction);
                algorithm = new LocalClimberAlgorithm <TState, TEvaluation>(successorSelector);
            }
            else
            {
                algorithm = ClimberAlgorithm;
            }

            return(algorithm);
        }
Esempio n. 4
0
        public void TestPerformOptimizationReturnsLocalExtrema()
        {
            TestIntegerEvaluableState initial = new TestIntegerEvaluableState(2);
            TestIntegerEvaluableState result;

            generator = new TestIntegerLocalMaximaSuccessorGenerator();
            picker    = new ClimberSuccessorSelector <TestIntegerEvaluableState, int>(generator, comparer);
            algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(picker);
            climber   = new ClimberConfiguration <TestIntegerEvaluableState, int>()
                        .ComparesUsing(comparer)
                        .GeneratesSuccessorsWith(generator)
                        .Build();

            result = climber.Optimize(initial);

            Assert.AreEqual(50, result.Value);
        }
Esempio n. 5
0
 public void Setup()
 {
     generator = new TestLinearIntegerSuccessorGenerator();
     picker    = new ClimberSuccessorSelector <TestIntegerEvaluableState, int>(generator, new MaximizingComparer <TestIntegerEvaluableState>());
     algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(picker);
 }