private PrgPopulation Create() { EncogProgramContext context = new EncogProgramContext(); context.DefineVariable("x"); StandardExtensions.CreateAll(context); PrgPopulation pop = new PrgPopulation(context, 10); EncogProgram prg1 = new EncogProgram(context); EncogProgram prg2 = new EncogProgram(context); prg1.CompileExpression("x+1"); prg2.CompileExpression("(x+5)/2"); ISpecies defaultSpecies = pop.CreateSpecies(); defaultSpecies.Add(prg1); defaultSpecies.Add(prg2); return(pop); }
/// <summary> /// Add a population member from one of the threads. /// </summary> /// <param name="population">The population to add to.</param> /// <param name="prg">The program to add.</param> public void AddPopulationMember(IPopulation population, EncogProgram prg) { lock (this) { ISpecies defaultSpecies = population.Species[0]; prg.Species = defaultSpecies; defaultSpecies.Add(prg); _contents.Add(prg.DumpAsCommonExpression()); } }
/// <summary> /// Construct a method genetic algorithm. /// </summary> /// <param name="phenotypeFactory">The phenotype factory.</param> /// <param name="calculateScore">The score calculation object.</param> /// <param name="populationSize">The population size.</param> public MLMethodGeneticAlgorithm(MLMethodGenomeFactory.CreateMethod phenotypeFactory, ICalculateScore calculateScore, int populationSize) : base(TrainingImplementationType.Iterative) { // create the population IPopulation population = new BasicPopulation(populationSize, null); population.GenomeFactory = new MLMethodGenomeFactory(phenotypeFactory, population); ISpecies defaultSpecies = population.CreateSpecies(); for (int i = 0; i < population.PopulationSize; i++) { IMLEncodable chromosomeNetwork = (IMLEncodable)phenotypeFactory(); MLMethodGenome genome = new MLMethodGenome(chromosomeNetwork); defaultSpecies.Add(genome); } defaultSpecies.Leader = defaultSpecies.Members[0]; // create the trainer this.genetic = new MLMethodGeneticAlgorithmHelper(population, calculateScore); this.genetic.CODEC = new MLEncodableCODEC(); IGenomeComparer comp = null; if (calculateScore.ShouldMinimize) { comp = new MinimizeScoreComp(); } else { comp = new MaximizeScoreComp(); } this.genetic.BestComparer = comp; this.genetic.SelectionComparer = comp; // create the operators int s = Math .Max(defaultSpecies.Members[0].Size / 5, 1); Genetic.Population = population; this.genetic.AddOperation(0.9, new Splice(s)); this.genetic.AddOperation(0.1, new MutatePerturb(1.0)); }
/// <summary> /// Add a genome. /// </summary> /// <param name="species">The species to add to.</param> /// <param name="genome">The genome to add.</param> public void AddSpeciesMember(ISpecies species, IGenome genome) { if (_owner.ValidationMode) { if (species.Members.Contains(genome)) { throw new GeneticError("Species already contains genome: " + genome); } } if (_owner.SelectionComparer.Compare(genome, species.Leader) < 0) { species.BestScore = genome.AdjustedScore; species.GensNoImprovement = 0; species.Leader = genome; } species.Add(genome); }