public FloatingPointChromosome Execute(FloatingPointChromosome chromosome) { if (chromosome == null) { throw new ArgumentNullException(nameof(chromosome)); } FloatingPointChromosome mutant = (FloatingPointChromosome)chromosome.Copy(); IFloatingPointProblem problem = mutant.Problem; for (int i = 0; i < problem.Dimension; i++) { if (_random.NextDouble() <= _mutationProbability) { double lowerBound = problem.GetLowerBound(i); double upperBound = problem.GetUpperBound(i); double mu = (upperBound - lowerBound) / 2; double sigma = upperBound - mu; mu /= 3; mutant.SetValue(i, Math.Min(Math.Max(_random.NextGaussian(mu, sigma), lowerBound), upperBound)); } } return(mutant); }
public FloatingPointChromosome Execute(FloatingPointChromosome chromosome) { if (chromosome == null) { throw new ArgumentNullException(nameof(chromosome)); } FloatingPointChromosome mutant = (FloatingPointChromosome)chromosome.Copy(); if (_random.NextDouble() > _mutationProbability) { return(mutant); } IFloatingPointProblem problem = chromosome.Problem; for (int i = 0; i < problem.Dimension; i++) { double lowerBound = problem.GetLowerBound(i); double upperBound = problem.GetUpperBound(i); double mutantValue = _random.NextDouble(lowerBound, upperBound); mutant.SetValue(i, mutantValue); } return(mutant); }
public IList <FloatingPointChromosome> Execute(IList <FloatingPointChromosome> parents) { if (parents == null) { throw new ArgumentNullException(nameof(parents)); } if (parents.Count != NumberOfParents) { throw new ArgumentException($"There must be {NumberOfParents} parents instead of {parents.Count}"); } FloatingPointChromosome parent1 = (FloatingPointChromosome)parents[0].Copy(); FloatingPointChromosome parent2 = (FloatingPointChromosome)parents[1].Copy(); IList <FloatingPointChromosome> result = new List <FloatingPointChromosome>(); if (_random.NextDouble() > _crossoverProbability) { result.Add(parent1); result.Add(parent2); return(result); } IFloatingPointProblem problem = parent1.Problem; // Order parents so that the first has the worst Fitness IList <FloatingPointChromosome> orderedParents = parents.OrderBy(c => c, problem.FitnessComparer).ToList(); parent1 = (FloatingPointChromosome)orderedParents[0].Copy(); parent2 = (FloatingPointChromosome)orderedParents[1].Copy(); FloatingPointChromosome child = parent1; for (int i = 0; i < parent1.Dimension; i++) { double parent1Value = parent1.GetValue(i); double parent2Value = parent2.GetValue(i); double a = _random.NextDouble(); var childValue = a * (parent2Value - parent1Value) + parent2Value; while (childValue < problem.GetLowerBound(i) || childValue > problem.GetUpperBound(i)) { a = _random.NextDouble(); childValue = a * (parent2Value - parent1Value) + parent2Value; } child.SetValue(i, childValue); } result.Add(child); return(result); }
public FloatingPointChromosome(IFloatingPointProblem problem) : base(problem) { _problem = problem; // Initialize randomly for (int i = 0; i < Dimension; i++) { double lowerBound = _problem.GetLowerBound(i); double upperBound = _problem.GetUpperBound(i); SetValue(i, RandomNumberGeneratorProvider.Instance.NextDouble(lowerBound, upperBound)); } }
public FloatingPointChromosome(FloatingPointChromosome chromosome) : base(chromosome) { _problem = chromosome._problem; }