public void TestCreateFromConfigurationWithCustomComparer() { IComparer <TestIntegerEvaluableState> comparer = new MinimizingComparer <TestIntegerEvaluableState>(); ClimberConfiguration <TestIntegerEvaluableState, int> config = ClimberConfiguration <TestIntegerEvaluableState, int> .Create() .GeneratesSuccessorsWith(generator) .ComparesUsing(comparer); Assert.IsNotNull(config); Assert.IsTrue(config.IsValid()); }
public void TestExtremaMinimizingLargeRandomInput() { IComparer <TestIntegerEvaluable> comparer = new MinimizingComparer <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.Min(s => s.Value); Assert.AreEqual(maxInt, max.GetEvaluation()); }
public void TestExtremaIntegerMinimumComparer() { IComparer <TestIntegerEvaluable> comparer = new MinimizingComparer <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(1, max.SequenceNumber); Assert.AreEqual(1, max.GetEvaluation()); }
public void TestBuildCreatesClimberWhenValid() { IComparer <TestIntegerEvaluableState> comparer = new MinimizingComparer <TestIntegerEvaluableState>(); ClimberConfiguration <TestIntegerEvaluableState, int> config = ClimberConfiguration <TestIntegerEvaluableState, int> .Create() .GeneratesSuccessorsWith(generator) .ComparesUsing(comparer); IHillClimber <TestIntegerEvaluableState, int> climber = null; try { climber = config.Build(); } catch { Assert.Fail("Build threw an exception"); } Assert.IsNotNull(climber); }
public void TestBuildThrowsCorrectExceptionWhenInvalid() { IComparer <TestIntegerEvaluableState> comparer = new MinimizingComparer <TestIntegerEvaluableState>(); ClimberConfiguration <TestIntegerEvaluableState, int> config = ClimberConfiguration <TestIntegerEvaluableState, int> .Create(); IHillClimber <TestIntegerEvaluableState, int> climber = null; bool caught = false; try { climber = config.Build(); } catch (ConfigurationException) { caught = true; } Assert.IsTrue(caught); Assert.IsNull(climber); }
/// <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); }