public Population <Expression <Func <double, double> > > GeneratePopulation() { this.CheckForDispose(); var chromosomes = new List <Chromosome <Expression <Func <double, double> > > >(); while (chromosomes.Count < ExpressionEvolverGeneticAlgorithmParameters.PopulationSizeValue) { var parameter = Expression.Parameter(typeof(double), "a"); var maximumExpressions = this.Random.Next( ExpressionEvolverGeneticAlgorithmParameters.ExpressionMaximum); var body = new RandomExpressionGenerator(maximumExpressions, ExpressionEvolverGeneticAlgorithmParameters.InjectConstantProbabilityValue, ExpressionEvolverGeneticAlgorithmParameters.ConstantLimit, parameter, this.Random).Body.Compress(); if (body.IsValid()) { var value = Expression.Lambda <Func <double, double> >(body, parameter); chromosomes.Add(new Chromosome <Expression <Func <double, double> > >( value, this.FitnessEvaluator(value))); } } return(new Population <Expression <Func <double, double> > >(chromosomes)); }
private void GetRandomOperation(Operators @operator) { var isLeftConstant = this.Random.NextDouble() < this.InjectConstantProbabilityValue; var isRightConstant = this.Random.NextDouble() < this.InjectConstantProbabilityValue; var isLeftBody = true; var isRightBody = true; if (!isLeftConstant && !isRightConstant) { isLeftBody = this.Random.NextDouble() < 0.5; isRightBody = !isLeftBody; } else if (isLeftConstant && isRightConstant) { isLeftConstant = this.Random.NextDouble() < this.InjectConstantProbabilityValue; isRightConstant = !isLeftConstant; } else if (@operator == Operators.Divide && !isLeftConstant && !isRightConstant) { isLeftConstant = this.Random.NextBoolean(); isRightConstant = !isLeftConstant; } this.Body = RandomExpressionGenerator.GetExpressionFunction(@operator)( isLeftConstant ? this.GetConstant() : (isLeftBody ? this.Body : this.Parameter), isRightConstant ? this.GetConstant() : (isRightBody ? this.Body : this.Parameter)); }
public Expression <Func <double, double> > Mutator(Expression <Func <double, double> > chromosome) { this.CheckForDispose(); chromosome.CheckParameterForNull("chromosome"); Expression <Func <double, double> > mutated = null; if (this.Random.NextDouble() < this.MutationProbability) { var chromosomeBody = chromosome.Body; var nodeCount = chromosomeBody.GetNodeCount(); var selectedNodeIndex = this.Random.Next(nodeCount); var selectedNode = chromosomeBody.GetNode(selectedNodeIndex); var selectedNodeCount = selectedNode.GetNodeCount(); var mutatedNode = new RandomExpressionGenerator(this.Random.Next(selectedNodeCount), ExpressionEvolverGeneticAlgorithmParameters.InjectConstantProbabilityValue, ExpressionEvolverGeneticAlgorithmParameters.ConstantLimit, chromosome.Parameters[0], this.Random).Body; chromosomeBody = chromosomeBody.Replace(null, selectedNode, mutatedNode).Compress(); mutated = Expression.Lambda <Func <double, double> >(chromosomeBody, (from param in chromosome.Parameters select param)); } else { mutated = chromosome; } return(mutated.IsValid() ? mutated : null); }