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 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 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); }
public void Setup() { comparer = new MaximizingComparer <TestIntegerEvaluableState>(); generator = new TestLinearIntegerSuccessorGenerator(); climber = new ClimberConfiguration <TestIntegerEvaluableState, int>() .ComparesUsing(new MaximizingComparer <TestIntegerEvaluableState>()) .GeneratesSuccessorsWith(generator) .Build(); }
public void TestCreateFromConfigurationWithDefaults() { ClimberConfiguration <TestIntegerEvaluableState, int> config = ClimberConfiguration <TestIntegerEvaluableState, int> .Create() .GeneratesSuccessorsWith(generator); IComparer <TestIntegerEvaluableState> expectedComparer = new MaximizingComparer <TestIntegerEvaluableState>(); Assert.IsNotNull(config); Assert.IsTrue(config.IsValid()); }
public void TestExtremaMaximumLargeRandomInput() { IComparer <TestIntegerEvaluable> comparer = new MaximizingComparer <TestIntegerEvaluable>(); List <int> randomNumbers = GetLargeRandomInput(); int j = 0; List <TestIntegerEvaluable> unsortedIntList = randomNumbers.Select(n => new TestIntegerEvaluable(j++, n)).ToList(); var max = unsortedIntList.Extrema <TestIntegerEvaluable, int>(comparer); int maxInt = unsortedIntList.Max(s => s.Value); Assert.AreEqual(maxInt, max.GetEvaluation()); }
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 TestExtremaListInReverseOrder() { IComparer <TestIntegerEvaluable> comparer = new MaximizingComparer <TestIntegerEvaluable>(); List <int> numbers = new List <int>(); for (int i = 0; i < 1000; i++) { numbers.Add(i); } List <TestIntegerEvaluable> reversedList = numbers.Select(n => new TestIntegerEvaluable(n, n)) .OrderByDescending(e => e.Value).ToList(); TestIntegerEvaluable max = reversedList.Extrema <TestIntegerEvaluable, int>(comparer); Assert.AreEqual(999, max.GetEvaluation()); }
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 TestExtremaAllValuesEqual() { IComparer <TestIntegerEvaluable> comparer = new MaximizingComparer <TestIntegerEvaluable>(); List <TestIntegerEvaluable> unsortedIntList = new List <TestIntegerEvaluable>() { new TestIntegerEvaluable(0, 10), new TestIntegerEvaluable(1, 10), new TestIntegerEvaluable(2, 10), new TestIntegerEvaluable(3, 10), new TestIntegerEvaluable(4, 10), new TestIntegerEvaluable(5, 10), new TestIntegerEvaluable(6, 10), }; var max = unsortedIntList.Extrema <TestIntegerEvaluable, int>(comparer); Assert.AreEqual(0, max.SequenceNumber); Assert.AreEqual(10, max.GetEvaluation()); }
public void TestExtremaIntegerMaximumComparer() { IComparer <TestIntegerEvaluable> comparer = new MaximizingComparer <TestIntegerEvaluable>(); List <TestIntegerEvaluable> unsortedIntList = new List <TestIntegerEvaluable>() { new TestIntegerEvaluable(0, 7), new TestIntegerEvaluable(1, 1), new TestIntegerEvaluable(2, 3), new TestIntegerEvaluable(3, 8), new TestIntegerEvaluable(4, 2), new TestIntegerEvaluable(5, 5), new TestIntegerEvaluable(6, 4), }; var max = unsortedIntList.Extrema <TestIntegerEvaluable, int>(comparer); Assert.AreEqual(3, max.SequenceNumber); Assert.AreEqual(8, max.GetEvaluation()); }
static void Main(string[] args) { IComparer <int> comparer = new MaximizingComparer <int>(); HillClimber <AddativeMatrixState, int> climber = new HillClimber <AddativeMatrixState, int>(comparer, new MatrixSwapGenerator()); climber.ClimberStepPerformedEvent += OnEvent; int[,] initialMatrix = new int[, ] { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 1 }, { 10, 2, 3, 4, 5 }, }; AddativeMatrixState initialState = new AddativeMatrixState(initialMatrix); AddativeMatrixState optimizedState = climber.Optimize(initialState); Console.WriteLine($"Optimized Evaluation after {iterations} iterations: {optimizedState.GetEvaluation()}\n{optimizedState.ToString()}"); }
/// <summary> /// Creates an <see cref="IComparable{TState}"/> for the desired climber direction /// </summary> /// <typeparam name="TState">The state for which the climber will optimize</typeparam> /// <typeparam name="TEvaluation">The type of the climber state's evaluation</typeparam> /// <param name="direction">The direction in which the climber will climb</param> /// <returns>An <see cref="IComparable{TState, TEvaluation}"/> that will promote climbing in the desired direction</returns> public IComparer <TState> Create <TState, TEvaluation>(ClimberDirection direction) where TState : EvaluableState <TEvaluation> where TEvaluation : IComparable <TEvaluation> { IComparer <TState> comparer; switch (direction) { case ClimberDirection.Maximize: comparer = new MaximizingComparer <TState>(); break; case ClimberDirection.Minimize: comparer = new MinimizingComparer <TState>(); break; default: throw new ArgumentException("Unknown climber direction"); } return(comparer); }