コード例 #1
0
        public void TestOptimizeStopsAtLocalExtrema()
        {
            IComparer <int> comparer = new MaximizingComparer <int>();

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

            TestIntegerEvaluableState        initialState = new TestIntegerEvaluableState(2);
            Task <TestIntegerEvaluableState> optimizeTask = Task.Run(() => algorithm.Optimize(initialState));

            Stopwatch timer = new Stopwatch();

            timer.Start();
            while (!optimizeTask.IsCompleted && timer.ElapsedMilliseconds < 10000)
            {
            }

            timer.Stop();

            Assert.IsTrue(optimizeTask.IsCompleted, "Optimization did not stop at local maxima");
            Assert.IsTrue(optimizeTask.IsCompletedSuccessfully, "FAILED");

            TestIntegerEvaluableState result = optimizeTask.Result;

            Assert.AreEqual(50, result.Value, "Encountered states do not match");
        }
コード例 #2
0
        public void TestPerformOptimizationReturnsLocalExtrema()
        {
            TestIntegerEvaluableState initial = new TestIntegerEvaluableState(2);
            TestIntegerEvaluableState result;

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

            Task <TestIntegerEvaluableState> optimizeTask = Task.Run(() => climber.PerformOptimization(initial));

            bool      complete = false;
            Stopwatch timer    = new Stopwatch();

            timer.Start();

            while (!complete && timer.ElapsedMilliseconds < 5000)
            {
                complete = optimizeTask.IsCompleted;
            }

            timer.Stop();
            Assert.IsTrue(complete, "Optimization did not stop at local extraema");

            result = optimizeTask.Result;

            Assert.AreEqual(50, result.Value);
        }
コード例 #3
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");
            }
        }
コード例 #4
0
        public ClimberSuccessorPicker(ISuccessorGenerator <TState, TEvaluation> generator, IComparer <TEvaluation> comparer)
        {
            this.generator = generator;
            this.comparer  = comparer;

            encounteredStates = new HashSet <TState>();
        }
コード例 #5
0
 public void Setup()
 {
     comparer  = new MaximizingComparer <int>();
     generator = new TestLinearIntegerSuccessorGenerator();
     picker    = new ClimberSuccessorPicker <TestIntegerEvaluableState, int>(generator, comparer);
     algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(comparer, picker);
     climber   = new GeneralHillClimber <TestIntegerEvaluableState>(algorithm);
 }
コード例 #6
0
 /// <summary>
 /// Use a concrete SuccessorGenerator object to generate successor states.
 /// </summary>
 /// <param name="successorGenerator">A successor generator that will create neighbor states for any given state</param>
 /// <returns>The modified configuration</returns>
 public ClimberConfiguration <TState, TEvaluation> GeneratesSuccessorsWith(ISuccessorGenerator <TState, TEvaluation> successorGenerator)
 {
     if (successorGenerator == null)
     {
         throw new ArgumentNullException("SuccessorGenerator cannot be null");
     }
     return(GeneratesSuccessorsWith(c => successorGenerator.GetSuccessors(c)));
 }
コード例 #7
0
 public void Setup()
 {
     comparer  = new MaximizingComparer <TestIntegerEvaluableState>();
     generator = new TestLinearIntegerSuccessorGenerator();
     climber   = new ClimberConfiguration <TestIntegerEvaluableState, int>()
                 .ComparesUsing(new MaximizingComparer <TestIntegerEvaluableState>())
                 .GeneratesSuccessorsWith(generator)
                 .Build();
 }
コード例 #8
0
        public void TestRandomRestartHillClimberSameRestartPointEachTime()
        {
            comparer   = new MaximizingComparer <int>();
            generator  = new TestLinearIntegerSuccessorGenerator();
            picker     = new ClimberSuccessorPicker <TestIntegerEvaluableState, int>(generator, comparer);
            algorithm  = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(comparer, picker);
            randomizer = new TestIntegerEvaluableStateNonRandomizer();
            climber    = new RandomRestartHillClimber <TestIntegerEvaluableState, int>(randomizer, algorithm, 5);

            RunTest(climber, 2, 100);
        }
コード例 #9
0
        public void TestRandomRestartHillClimberIncrementingRestartPoint()
        {
            comparer   = new MaximizingComparer <int>();
            generator  = new TestExponentialIntegerSuccessorGenerator();
            picker     = new ClimberSuccessorPicker <TestIntegerEvaluableState, int>(generator, comparer);
            algorithm  = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(comparer, picker);
            randomizer = new TestIntegerRandomizerSimulator();
            climber    = new RandomRestartHillClimber <TestIntegerEvaluableState, int>(randomizer, algorithm, 5);


            RunTest(climber, 1, 64);
        }
コード例 #10
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");
        }
コード例 #11
0
        public void TestRandomRestartHillClimberIncrementingRestartPoint()
        {
            comparer  = new MaximizingComparer <TestIntegerEvaluableState>();
            generator = new TestExponentialIntegerSuccessorGenerator();

            var climberConfiguration = new ClimberConfiguration <TestIntegerEvaluableState, int>()
                                       .ComparesUsing(comparer)
                                       .GeneratesSuccessorsWith((c) => generator.GetSuccessors(c));

            randomizer = new TestIntegerRandomizerSimulator();
            climber    = new RandomRestartHillClimber <TestIntegerEvaluableState, int>(5, randomizer, climberConfiguration);

            RunTest(climber, 1, 10000);
        }
コード例 #12
0
        public void TestRandomRestartHillClimberSameRestartPointEachTime()
        {
            comparer   = new MaximizingComparer <TestIntegerEvaluableState>();
            generator  = new TestLinearIntegerSuccessorGenerator();
            randomizer = new TestIntegerEvaluableStateNonRandomizer();

            var climberConfiguration = new ClimberConfiguration <TestIntegerEvaluableState, int>()
                                       .ComparesUsing(comparer)
                                       .GeneratesSuccessorsWith((c) => generator.GetSuccessors(c));

            randomizer = new TestIntegerEvaluableStateNonRandomizer();
            climber    = new RandomRestartHillClimber <TestIntegerEvaluableState, int>(5, randomizer, climberConfiguration);

            RunTest(climber, 2, 100);
        }
コード例 #13
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);
        }
コード例 #14
0
        public void TestOptimizeEmitsEventsInOrder()
        {
            IComparer <int> comparer = new MaximizingComparer <int>();

            generator = new TestIntegerLocalMaximaSuccessorGenerator();
            picker    = new ClimberSuccessorPicker <TestIntegerEvaluableState, int>(generator, comparer);
            algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(comparer, 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.StepState.Value);
            }

            algorithm.ClimbStepPerformed += OnEvent;

            TestIntegerEvaluableState        initialState = new TestIntegerEvaluableState(2);
            Task <TestIntegerEvaluableState> optimizeTask = Task.Run(() => algorithm.Optimize(initialState));

            Stopwatch timer = new Stopwatch();

            timer.Start();
            while (!optimizeTask.IsCompleted && timer.ElapsedMilliseconds < 10000)
            {
            }

            timer.Stop();

            Assert.IsTrue(optimizeTask.IsCompletedSuccessfully, "FAILED");

            TestIntegerEvaluableState result = optimizeTask.Result;

            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");
            }
        }
コード例 #15
0
 /// <summary>
 /// Creates a <see cref="ClimberSuccessorSelector{TState, TEvaluation}" that will create neighbor states using the given
 /// <see cref="ISuccessorGenerator{TState, TEvaluable}"/>/>
 /// </summary>
 /// <param name="generator">The complex SuccessorGenerator that will be used to generate neighbor states</param>
 /// <param name="evaluationComparer">The comparison strategy to determine which successor state is most optimal</param>
 public ClimberSuccessorSelector(ISuccessorGenerator <TState, TEvaluation> generator, IComparer <TState> evaluationComparer)
     : this(evaluationComparer, (c) => generator.GetSuccessors(c))
 {
 }
コード例 #16
0
 public void Setup()
 {
     generator = new TestLinearIntegerSuccessorGenerator();
     picker    = new ClimberSuccessorSelector <TestIntegerEvaluableState, int>(generator, new MaximizingComparer <TestIntegerEvaluableState>());
     algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(picker);
 }
コード例 #17
0
ファイル: HillClimber.cs プロジェクト: DeanVanGreunen/TBCore
 /// <summary>
 /// Creates a HillClimber that will optimize using the given comparer and successor generator
 /// </summary>
 /// <param name="comparer">The comparison strategy to optimize with</param>
 /// <param name="successorGenerator">The successor genereator from which the best state will be selected</param>
 public HillClimber(IComparer <TEvaluation> comparer, ISuccessorGenerator <TState, TEvaluation> successorGenerator)
     : this(new LocalClimberAlgorithm <TState, TEvaluation>(comparer, new ClimberSuccessorPicker <TState, TEvaluation>(successorGenerator, comparer)))
 {
 }
コード例 #18
0
 /// <summary>
 /// Creates a new GeneralHillClimber using the given comparison strategy and successor generator for integer evaluation
 /// </summary>
 /// <param name="comparer">The comparison strategy to optimize with</param>
 /// <param name="successorGenerator">The successor genereator from which the best state will be selected</param>
 public GeneralHillClimber(IComparer <int> comparer, ISuccessorGenerator <TState, int> successorGenerator)
     : this(new LocalClimberAlgorithm <TState, int>(comparer, new ClimberSuccessorPicker <TState, int>(successorGenerator, comparer)))
 {
 }
コード例 #19
0
 public void SetUp()
 {
     generator = new TestLinearIntegerSuccessorGenerator();
 }
コード例 #20
0
 public void Setup()
 {
     generator = new TestLinearIntegerSuccessorGenerator();
     picker    = new ClimberSuccessorPicker <TestIntegerEvaluableState, int>(generator, new MaximizingComparer <int>());
 }