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 TestOptimizeCorrectOptimalValueReached() { TestIntegerEvaluableState initialState = new TestIntegerEvaluableState(2); TestIntegerEvaluableState result = algorithm.Optimize(initialState); Assert.AreEqual(100, result.Value); }
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 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); }
public void TestPerformOptimization() { TestIntegerEvaluableState initial = new TestIntegerEvaluableState(2); TestIntegerEvaluableState result; List <TestIntegerEvaluableState> states = new List <TestIntegerEvaluableState>(); List <TestIntegerEvaluableState> expectedStates = new List <TestIntegerEvaluableState>(); for (int i = 3; i <= 100; i++) { expectedStates.Add(new TestIntegerEvaluableState(i)); } void eventCallback(object sender, ClimberStepEvent <TestIntegerEvaluableState, int> args) { states.Add(args.CurrentState); }; climber.ClimberStepPerformedEvent += eventCallback; result = climber.Optimize(initial); Assert.AreEqual(100, result.Value); Assert.AreEqual(expectedStates.Count, states.Count); for (int i = 0; i < states.Count; i++) { Assert.IsTrue(states[i].CompareTo(expectedStates[i]) == 0); } }
public void TestNext() { TestIntegerEvaluableState initial = new TestIntegerEvaluableState(1); TestIntegerEvaluableState result = picker.Next(initial); Assert.AreEqual(2, result.Value); }
public void TestNextIsEqualToCurrent() { TestIntegerEvaluableState initial = new TestIntegerEvaluableState(100); TestIntegerEvaluableState result = picker.Next(initial); Assert.AreEqual(100, result.Value); Assert.AreEqual(initial, result); }
public void TestClone() { TestIntegerEvaluableState state = new TestIntegerEvaluableState(1); TestIntegerEvaluableState clone = state.Clone(); Assert.AreEqual(state.Value, clone.Value); state.Value = 2; Assert.AreNotEqual(state.Value, clone.Value); Assert.AreNotEqual(2, clone.Value); }
public void TestLazyEvaluation() { TestIntegerEvaluableState state = new TestIntegerEvaluableState(1); Assert.AreEqual(0, state.TimesEvaluated); int evaluation = state.GetEvaluation(); Assert.AreEqual(1, evaluation); Assert.AreEqual(1, state.TimesEvaluated); state.GetEvaluation(); Assert.AreEqual(1, state.TimesEvaluated); }
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 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); }
public void TestPerformOptimization() { TestIntegerEvaluableState initial = new TestIntegerEvaluableState(2); TestIntegerEvaluableState result; List <TestIntegerEvaluableState> states = new List <TestIntegerEvaluableState>(); List <TestIntegerEvaluableState> expectedStates = new List <TestIntegerEvaluableState>(); for (int i = 3; i <= 100; i++) { expectedStates.Add(new TestIntegerEvaluableState(i)); } void eventCallback(object sender, ClimberStepEvent <TestIntegerEvaluableState, int> args) { states.Add(args.StepState); }; climber.ClimberStepPerformedEvent += eventCallback; 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 exceeded time limit"); result = optimizeTask.Result; Assert.AreEqual(100, result.Value); Assert.AreEqual(expectedStates.Count, states.Count); for (int i = 0; i < states.Count; i++) { Assert.IsTrue(states[i].CompareTo(expectedStates[i]) == 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"); } }
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); }
private void RunTest(RandomRestartHillClimber <TestIntegerEvaluableState, int> climber, int initialStateValue, int expectedOptimalValue) { TestIntegerEvaluableState initialState = new TestIntegerEvaluableState(initialStateValue); TestIntegerEvaluableState resultState = new TestIntegerEvaluableState(initialStateValue); Dictionary <int, Tuple <TestIntegerEvaluableState, TestIntegerEvaluableState> > localWinners = new Dictionary <int, Tuple <TestIntegerEvaluableState, TestIntegerEvaluableState> >(); void OnClimberRestartEvent(object source, ClimberCompleteEvent <TestIntegerEvaluableState, int> e) { localWinners[e.CLimberIndex] = new Tuple <TestIntegerEvaluableState, TestIntegerEvaluableState>(e.InitialState, e.OptimizedState); } climber.ClimberCompleteEvent += OnClimberRestartEvent; resultState = climber.Optimize(initialState); Assert.AreEqual(expectedOptimalValue, resultState.Value); }
private void RunTest(Optimizer <TestIntegerEvaluableState, int> climber, int initialStateValue, int expectedOptimalValue) { TestIntegerEvaluableState initialState = new TestIntegerEvaluableState(initialStateValue); TestIntegerEvaluableState resultState = new TestIntegerEvaluableState(initialStateValue); Stopwatch timer = new Stopwatch(); Task <TestIntegerEvaluableState> optimizeTask = Task.Run(() => climber.PerformOptimization(initialState)); timer.Start(); while (!optimizeTask.IsCompleted && timer.ElapsedMilliseconds < 20000) { } timer.Stop(); Assert.IsTrue(optimizeTask.IsCompleted, "Operation took too long to complete"); Assert.IsTrue(optimizeTask.IsCompletedSuccessfully, "Operation failed"); resultState = optimizeTask.Result; Assert.AreEqual(expectedOptimalValue, resultState.Value); }