public void TestOptimizeCorrectOptimalValueReached() { TestIntegerEvaluableState initialState = new TestIntegerEvaluableState(2); TestIntegerEvaluableState result = algorithm.Optimize(initialState); Assert.AreEqual(100, result.Value); }
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"); } }
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"); }
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"); } }
public void TestOptimizeCorrectOptimalValueReached() { TestIntegerEvaluableState initialState = new TestIntegerEvaluableState(2); Task <TestIntegerEvaluableState> task = Task.Run(() => algorithm.Optimize(initialState)); Stopwatch timer = new Stopwatch(); timer.Start(); while (!task.IsCompleted && timer.ElapsedMilliseconds < 5000) { } timer.Stop(); Assert.IsTrue(task.IsCompleted, "Optimization took too long to complete"); TestIntegerEvaluableState result = task.Result; Assert.AreEqual(100, result.Value); }
/// <summary> /// Performs the climber optimization from a given initialState /// </summary> /// <param name="initialState">The initial state for which to optimize</param> /// <returns>The most optimal state encountered by the climber. This may not be the best possible state</returns> public override TState PerformOptimization(TState initialState) { return(algorithm.Optimize(initialState)); }