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 <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"); }
/// <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); }
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 Setup() { generator = new TestLinearIntegerSuccessorGenerator(); picker = new ClimberSuccessorSelector <TestIntegerEvaluableState, int>(generator, new MaximizingComparer <TestIntegerEvaluableState>()); algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(picker); }