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");
        }
Beispiel #2
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");
            }
        }
Beispiel #3
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");
        }
        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");
            }
        }
Beispiel #5
0
 public void Setup()
 {
     generator = new TestLinearIntegerSuccessorGenerator();
     picker    = new ClimberSuccessorSelector <TestIntegerEvaluableState, int>(generator, new MaximizingComparer <TestIntegerEvaluableState>());
     algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(picker);
 }
Beispiel #6
0
 /// <summary>
 /// Creates a HillClimber that will perform an optimization from the given ClimberAlgrithm.
 /// </summary>
 /// <param name="algorithm">The climber algorithm to use for optimzation</param>
 public HillClimber(ClimberAlgorithm <TState, TEvaluation> algorithm) : base(algorithm.SuccessorPicker)
 {
     this.algorithm = algorithm;
     this.algorithm.ClimbStepPerformed += OnClimberStepEvent;
 }
 /// <summary>
 /// Creates a new GeneralHillClimber using the given ClimberAlgorithm for integer evaluation
 /// </summary>
 /// <param name="algorithm">The ClimberAlgorithm to use for optimization</param>
 public GeneralHillClimber(ClimberAlgorithm <TState, int> algorithm) : base(algorithm)
 {
 }
 /// <summary>
 /// Creates a RandomRestartHillClimber that will restart the Optimization from the given ClimberAlgorithm
 /// up to numRestarts in parallel with the given ParallelOptions
 /// </summary>
 /// <param name="stateRandomizer">A successor picker that will generate a random successor state from the initial state</param>
 /// <param name="algorithm">The climber algorithm to use for optimzation</param>
 /// <param name="numRestarts">The number of random restart operations to complete</param>
 /// <param name="parallelOptions">The options for the Parallel restart operations</param>
 public RandomRestartHillClimber(ISuccessorPicker <TState, TEvaluation> stateRandomizer, ClimberAlgorithm <TState, TEvaluation> algorithm, uint numRestarts, ParallelOptions parallelOptions) : base(algorithm)
 {
     this.numRestarts     = numRestarts;
     this.parallelOptions = parallelOptions;
     this.stateRandomizer = stateRandomizer;
 }
 /// <summary>
 /// Creates a RandomRestartHillClimber that will restart the Optimization from the given ClimberAlgorithm
 /// up to numRestarts. The operation will run in parallel with as many available processors up to maxDegreeOfParallelism.
 /// </summary>
 /// <param name="stateRandomizer">A successor picker that will generate a random successor state from the initial state</param>
 /// <param name="algorithm">The climber algorithm to use for optimzation</param>
 /// <param name="numRestarts">The number of random restart operations to complete</param>
 /// <param name="maxDegreeOfParallelism">The maximum number of processors that will attempt to perform an optimization operation</param>
 public RandomRestartHillClimber(ISuccessorPicker <TState, TEvaluation> stateRandomizer, ClimberAlgorithm <TState, TEvaluation> algorithm, uint numRestarts, int maxDegreeOfParallelism)
     : this(stateRandomizer, algorithm, numRestarts, new ParallelOptions() { MaxDegreeOfParallelism = maxDegreeOfParallelism })
 {
 }
 /// <summary>
 /// Creates a RandomRestartHillClimber that will restart the Optimization from the given ClimberAlgorithm
 /// up to numRestarts. If parallel is true, the operation will be completed with all available processors
 /// </summary>
 /// <param name="stateRandomizer">A successor picker that will generate a random successor state from the initial state</param>
 /// <param name="algorithm">The climber algorithm to use for optimzation</param>
 /// <param name="numRestarts">The number of random restart operations to complete</param>
 /// <param name="parallel">Flag to determine if the process should run in parallel</param>
 public RandomRestartHillClimber(ISuccessorPicker <TState, TEvaluation> stateRandomizer, ClimberAlgorithm <TState, TEvaluation> algorithm, uint numRestarts, bool parallel)
     : this(stateRandomizer, algorithm, numRestarts, parallel ? -1 : 1)
 {
 }
 /// <summary>
 /// Creates a RandomRestartHillClimber that will restart the Optimization from the given ClimberAlgorithm
 /// up to numRestarts in serial (one thread)
 /// </summary>
 /// <param name="stateRandomizer">A successor picker that will generate a random successor state from the initial state</param>
 /// <param name="algorithm">The climber algorithm to use for optimzation</param>
 /// <param name="numRestarts">The number of random restart operations to complete</param>
 public RandomRestartHillClimber(ISuccessorPicker <TState, TEvaluation> stateRandomizer, ClimberAlgorithm <TState, TEvaluation> algorithm, uint numRestarts)
     : this(stateRandomizer, algorithm, numRestarts, false)
 {
 }