/// <summary> /// Constructs a mutation operator that adds a random number from a Normal distribution to zero or more elements in the <see cref="DecisionVector"/>. /// </summary> /// <param name="normalStandardDeviation">The standard deviation of the Normal distribution around zero.</param> /// <param name="mutationProbability">The probability that any mutation will occur.</param> /// <param name="maximumNumberOfMutations">The maximum number of times a mutation should be tried.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown when any of the input values are illegal.</exception> public MutationAddRandomNumber(double normalStandardDeviation, double mutationProbability, int maximumNumberOfMutations) : base($"Add random number from N(0,{Math.Pow(normalStandardDeviation, 2).ToString("F2", CultureInfo.InvariantCulture)}) " + $"to up to {maximumNumberOfMutations} locations " + $"with chance {mutationProbability.ToString("F2", CultureInfo.InvariantCulture)}") { if (normalStandardDeviation <= 0) { throw new ArgumentOutOfRangeException(nameof(normalStandardDeviation), "Mutation variance must be greater than 0."); } this.normalStandardDeviation = normalStandardDeviation; if (mutationProbability < 0 || mutationProbability > 1) { throw new ArgumentOutOfRangeException(nameof(mutationProbability), "Mutation probability must be a value between 0 and 1."); } this.mutationProbability = mutationProbability; if (maximumNumberOfMutations <= 0) { throw new ArgumentOutOfRangeException(nameof(maximumNumberOfMutations), "Maximum number of mutations must be greater than 0."); } this.maximumNumberOfMutations = maximumNumberOfMutations; rngManager = new RandomNumberManager(); }
public RandomNumberManagerTests() { testDv = DecisionVector.CreateFromArray( DecisionSpace.CreateForUniformIntArray(8, 0, 7), new int[8] { 7, 6, 5, 4, 3, 2, 1, 0 }); rngManager = new RandomNumberManager(new MersenneTwister(123456789)); }
/// <summary> /// Constructs a mutation operator that does nothing or swaps two elements in the <see cref="DecisionVector"/>. /// </summary> /// <param name="mutationProbability">The probability that the mutation will occur.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown when the mutation probability is illegal.</exception> public MutationRandomSwap(double mutationProbability) : base($"Swap random pair with chance {mutationProbability.ToString("F2", CultureInfo.InvariantCulture)}") { if (mutationProbability < 0 || mutationProbability > 1) { throw new ArgumentOutOfRangeException(nameof(mutationProbability), "Mutation probability must be a value between 0 and 1."); } this.mutationProbability = mutationProbability; rngManager = new RandomNumberManager(); }
/// <summary> /// Constructs a crossover operator to perform simulated binary crossover on real-valued decision vectors. /// </summary> /// <param name="eta">The expansion parameter (lower values create children further from the parents).</param> public CrossoverSimulatedBinary(int eta) : base($"Simulated Binary (eta = {eta.ToString()})") { if (eta < 0) { throw new System.ArgumentOutOfRangeException(nameof(eta), "Eta should not be negative."); } this.eta = eta; rngManager = new RandomNumberManager(); }
/// <summary> /// Constructs a crossover operator to perform uniform two-parent crossover. /// </summary> /// <param name="crossoverBias">The bias towards the first parent, by default 0.5 (unbiased).</param> /// <exception cref="ArgumentOutOfRangeException">Thrown when the <see cref="crossoverBias"/> is not a legal probability.</exception> public CrossoverUniform(double crossoverBias = 0.5) : base($"Uniform (first parent chosen with probability {crossoverBias.ToString("F2", CultureInfo.InvariantCulture)})") { if (crossoverBias < 0.0 || crossoverBias > 1.0) { throw new ArgumentOutOfRangeException(nameof(crossoverBias), "Parent bias probability must be a value between 0 and 1"); } rngManager = new RandomNumberManager(); this.crossoverBias = crossoverBias; }
/// <summary> /// Constructs a crossover operator to perform weighted arithmetic (flat) two-parent crossover. /// </summary> /// <param name="allRandomWeights"><see langword="false"/> if weights are fixed to one value, specified by <see cref="fixedWeight"/>.</param> /// <param name="fixedWeight">The value to weight the first parent's Decision Vector elements by when summing them.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown if the fixed weight is not between 0 and 1.</exception> public CrossoverArithmeticWeighted(bool allRandomWeights = false, double fixedWeight = 0.5) : base(allRandomWeights ? $"Arithmetic (random weights)" : $"Arithmetic (weight = {fixedWeight.ToString("F2", CultureInfo.InvariantCulture)})") { if (!allRandomWeights && (fixedWeight < 0 || fixedWeight > 1)) { throw new ArgumentOutOfRangeException(nameof(fixedWeight), "Weight must be between 0 and 1"); } rngManager = new RandomNumberManager(); this.allRandomWeights = allRandomWeights; this.fixedWeight = fixedWeight; }
/// <summary> /// Creates a tournament parent selection operator. /// </summary> /// <param name="tournamentSize">The number of potential parents which are entered into the tournament.</param> /// <param name="alwaysReturnBest"><see langword="true"/> if the best individual is always entered into the tournament.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown if the tournament size is less than 1.</exception> public ParentSelectionTournament(int tournamentSize = 40, bool alwaysReturnBest = false) : base($"Tournament (size {tournamentSize}" + (alwaysReturnBest ? ", keeping best)" : ")")) { if (tournamentSize <= 0) { throw new ArgumentOutOfRangeException(nameof(tournamentSize), "The tournament size must be greater than zero."); } this.tournamentSize = tournamentSize; this.alwaysReturnBest = alwaysReturnBest; rngManager = new RandomNumberManager(); }
/// <summary> /// Creates a new PCX operator for Evolutionary Algorithm recombination /// </summary> /// <param name="sigmaEta">Gain parallel to direction of search.</param> /// <param name="sigmaZeta">Gain perpendicular to direction of search.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown if <see cref="sigmaEta"/> or <see cref="sigmaZeta"/> are not greater than zero.</exception> public RecombinationParentCentric( double sigmaEta = 0.1, double sigmaZeta = 0.1) : base($"PCX (sigma_eta = {sigmaEta.ToString("F2", System.Globalization.NumberFormatInfo.InvariantInfo)}, " + $"sigma_zeta = {sigmaZeta.ToString("F2", System.Globalization.NumberFormatInfo.InvariantInfo)})") { if (sigmaEta <= 0) { throw new ArgumentOutOfRangeException(nameof(sigmaEta), "Sigma_eta must be greater than zero."); } this.sigmaEta = sigmaEta; if (sigmaZeta <= 0) { throw new ArgumentOutOfRangeException(nameof(sigmaZeta), "Sigma_eta must be greater than zero."); } this.sigmaZeta = sigmaZeta; rngManager = new RandomNumberManager(); }
/// <summary> /// Constructs a mutation operator that can replace zero or more elements in the <see cref="DecisionVector"/> with new random values. /// </summary> /// <param name="mutationProbability">The probability that any mutation will occur.</param> /// <param name="maximumNumberOfMutations">The maximum number of times a mutation should be tried.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown when any of the input values are illegal.</exception> public MutationReplaceWithRandomNumber(double mutationProbability, int maximumNumberOfMutations) : base($"Replace up to {maximumNumberOfMutations} elements with a random number, " + $"with chance {mutationProbability.ToString("F2", CultureInfo.InvariantCulture)}") { if (mutationProbability < 0 || mutationProbability > 1) { throw new ArgumentOutOfRangeException(nameof(mutationProbability), "Mutation probability must be a value between 0 and 1."); } this.mutationProbability = mutationProbability; if (maximumNumberOfMutations <= 0) { throw new ArgumentOutOfRangeException(nameof(maximumNumberOfMutations), "Maximum number of mutations must be greater than 0."); } this.maximumNumberOfMutations = maximumNumberOfMutations; rngManager = new RandomNumberManager(); }
/// <summary> /// Constructs a mutation operator that adds an integer, chosen randomly from a set of numbers provided, to zero or more elements in the <see cref="DecisionVector"/>. /// </summary> /// <param name="numberSet">The set of numbers to choose from.</param> /// <param name="mutationProbability">The probability that any mutation will occur.</param> /// <param name="maximumNumberOfMutations">The maximum number of times a mutation should be tried.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown when any of the input values are illegal.</exception> public MutationAddRandomIntegerFromSet(IEnumerable <int> numberSet, double mutationProbability, int maximumNumberOfMutations) : base($"Add random number from set of size {numberSet.Count()}") { var numbers = numberSet.ToArray(); if (mutationProbability < 0 || mutationProbability > 1) { throw new ArgumentOutOfRangeException(nameof(mutationProbability), "Mutation probability must be a value between 0 and 1."); } this.mutationProbability = mutationProbability; if (maximumNumberOfMutations <= 0) { throw new ArgumentOutOfRangeException(nameof(maximumNumberOfMutations), "Maximum number of mutations must be greater than 0."); } this.maximumNumberOfMutations = maximumNumberOfMutations; this.numberSet = numbers; rngManager = new RandomNumberManager(); }
/// <summary> /// Constructs a mutation operator that adds a random integer from a Uniform distribution to zero or more elements in the <see cref="DecisionVector"/>. /// </summary> /// <param name="minimum">The smallest integer to add.</param> /// <param name="maximum">The largest integer to add.</param> /// <param name="includeZero">Whether to allow no change.</param> /// <param name="mutationProbability">The probability that any mutation will occur.</param> /// <param name="maximumNumberOfMutations">The maximum number of times a mutation should be tried.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown when any of the input values are illegal.</exception> public MutationAddRandomInteger(int minimum, int maximum, bool includeZero, double mutationProbability, int maximumNumberOfMutations) : base($"Add random integer between {minimum} and {maximum} " + (includeZero ? "" : "(excluding zero) ") + $"to up to {maximumNumberOfMutations} locations " + $"with chance {mutationProbability.ToString("F2", CultureInfo.InvariantCulture)}") { if (minimum > maximum) { throw new ArgumentOutOfRangeException(nameof(maximum), "Largest value must be greater than smallest value."); } this.minimum = minimum; this.maximum = maximum; if (minimum > 0) { includeZero = true; } this.includeZero = includeZero; if (mutationProbability < 0 || mutationProbability > 1) { throw new ArgumentOutOfRangeException(nameof(mutationProbability), "Mutation probability must be a value between 0 and 1."); } this.mutationProbability = mutationProbability; if (maximumNumberOfMutations <= 0) { throw new ArgumentOutOfRangeException(nameof(maximumNumberOfMutations), "Maximum number of mutations must be greater than 0."); } this.maximumNumberOfMutations = maximumNumberOfMutations; rngManager = new RandomNumberManager(); }
/// <summary> /// Creates a roulette wheel parent selection operator. /// </summary> /// <param name="alwaysReturnBest"><see langword="true"/> if the best individual is always returned as one of the parents.</param> public ParentSelectionRoulette(bool alwaysReturnBest) : base("Roulette wheel" + (alwaysReturnBest ? " (keeping best)" : "")) { this.alwaysReturnBest = alwaysReturnBest; rngManager = new RandomNumberManager(); }
/// <summary> /// Creates a random parent selection operator. /// </summary> public ParentSelectionRandom() : base("Random") { rngManager = new RandomNumberManager(); }
/// <summary> /// Creates a replace-random re-insertion operator. /// </summary> public ReinsertionReplaceRandom() : base("Replace a randomly-selected individual if better") { rngManager = new RandomNumberManager(); }
/// <summary> /// Constructs a crossover operator to perform multi-point two-parent crossover. /// </summary> public CrossoverMultiPoint(int numberOfCrossoverLocations = 1) : base($"{numberOfCrossoverLocations}-point (not permutation-safe)") { rngManager = new RandomNumberManager(); this.numberOfCrossoverLocations = numberOfCrossoverLocations; }