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 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 ClimberSuccessorPicker(ISuccessorGenerator <TState, TEvaluation> generator, IComparer <TEvaluation> comparer) { this.generator = generator; this.comparer = comparer; encounteredStates = new HashSet <TState>(); }
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); }
/// <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))); }
public void Setup() { comparer = new MaximizingComparer <TestIntegerEvaluableState>(); generator = new TestLinearIntegerSuccessorGenerator(); climber = new ClimberConfiguration <TestIntegerEvaluableState, int>() .ComparesUsing(new MaximizingComparer <TestIntegerEvaluableState>()) .GeneratesSuccessorsWith(generator) .Build(); }
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); }
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); }
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 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); }
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); }
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 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"); } }
/// <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)) { }
public void Setup() { generator = new TestLinearIntegerSuccessorGenerator(); picker = new ClimberSuccessorSelector <TestIntegerEvaluableState, int>(generator, new MaximizingComparer <TestIntegerEvaluableState>()); algorithm = new LocalClimberAlgorithm <TestIntegerEvaluableState, int>(picker); }
/// <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))) { }
/// <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))) { }
public void SetUp() { generator = new TestLinearIntegerSuccessorGenerator(); }
public void Setup() { generator = new TestLinearIntegerSuccessorGenerator(); picker = new ClimberSuccessorPicker <TestIntegerEvaluableState, int>(generator, new MaximizingComparer <int>()); }