public void SetupForEvolution(Population the_population, FitnessDirection fd ) { this.direction = fd; this.CurrentGeneration = new Generation(0); this.CurrentGeneration.population = the_population ; if ( this.CurrentGeneration.population.IsEmpty ) { throw new GeneticError("Cannot start with empty population"); } }
public Environment( ) { this.CurrentGeneration = null; }
public void MakeNextGenerationThroughCrossover() { Generation next_generation = new Generation( Environment.generation_id_generator.GetUniqueID() ); next_generation.population = new Population( this.CurrentGeneration.population.Capacity ); Organism best_organism = this.FindBestOrganismInPopulation( this.CurrentGeneration.population ); next_generation.population.AddOrganism( best_organism ); while (!next_generation.population.IsFull) { geneticfx.Population offspring_population = new geneticfx.Population( this.CurrentGeneration.population.Capacity ); Organism parent0 = this.SelectForCrossover(); bool perform_crossover = Environment.rand.NextDouble() < this. CrossoverRate; if ( perform_crossover ) { Organism parent1 = this.SelectForCrossover(); Organism child0; Organism child1; this.Crossover( parent0, parent1, out child0, out child1); child0.GenerationID = next_generation.ID; offspring_population.AddOrganism(child0); child1.GenerationID = next_generation.ID; offspring_population.AddOrganism(child1); } else { offspring_population.AddOrganism( parent0 ); } for (int i=0;i<offspring_population.Size;i++) { if (next_generation.population.IsFull) { break; } Organism child = offspring_population[ i ]; for (int gi=0;gi<child.Genes.GetLength();gi++) { bool perform_mutation = Environment.rand.NextDouble() < this.MutationRate ; if ( perform_mutation ) { child.Genes.MutateGene(gi); } } child.Fitness = child.Genes.CalculateFitness(); next_generation.population.AddOrganism( child ); } } if ( this.CurrentGeneration.population.Size != next_generation.population.Size ) { throw new GeneticError("Population sizes do not match"); } this.CurrentGeneration = next_generation; }