static void Main(string[] args) { geneticfx.Environment env = new geneticfx.Environment( ); env.StartGeneration += new geneticfx.Environment.EventHandlerDelegate(MaximizeBitArray.StartGenerationHandler); env.EndGeneration += new geneticfx.Environment.EventHandlerDelegate(MaximizeBitArray.EndGenerationHandler); geneticfx.Population initial_population = new geneticfx.Population(9); for (int i = 0; i < initial_population.Capacity; i++) { MyChromosome cs = new MyChromosome(); cs.RandomizeGenes(); geneticfx.Organism o = new geneticfx.Organism(cs, 0.0F); initial_population.AddOrganism(o); } env.Run(initial_population, geneticfx.FitnessDirection.Maximize); }
static void Main(string[] args) { geneticfx.Environment env = new geneticfx.Environment( ); env.StartGeneration+= new geneticfx.Environment.EventHandlerDelegate( MaximizeBitArray.StartGenerationHandler ); env.EndGeneration+= new geneticfx.Environment.EventHandlerDelegate( MaximizeBitArray.EndGenerationHandler ); geneticfx.Population initial_population = new geneticfx.Population( 9 ); for (int i=0;i<initial_population.Capacity;i++) { MyChromosome cs = new MyChromosome(); cs.RandomizeGenes(); geneticfx.Organism o = new geneticfx.Organism( cs, 0.0F ); initial_population.AddOrganism(o); } env.Run( initial_population , geneticfx.FitnessDirection.Maximize); }
public void Run() { geneticfx.Environment env = new geneticfx.Environment( ); env.StartGeneration += new geneticfx.Environment.EventHandlerDelegate(this.StartGenerationHandler); env.EndGeneration += new geneticfx.Environment.EventHandlerDelegate(this.EndGenerationHandler); geneticfx.Population initial_population = new geneticfx.Population(50); System.Collections.ArrayList rects = new System.Collections.ArrayList(); rects.Add(new RECT(0, 0, 100, 100)); rects.Add(new RECT(0, 0, 100, 200)); rects.Add(new RECT(0, 0, 200, 100)); rects.Add(new RECT(0, 0, 200, 200)); rects.Add(new RECT(0, 0, 200, 100)); rects.Add(new RECT(0, 0, 200, 200)); rects.Add(new RECT(0, 0, 100, 100)); rects.Add(new RECT(0, 0, 100, 200)); rects.Add(new RECT(0, 0, 100, 100)); rects.Add(new RECT(0, 0, 100, 200)); rects.Add(new RECT(0, 0, 100, 100)); rects.Add(new RECT(0, 0, 100, 200)); for (int i = 0; i < initial_population.Capacity; i++) { MyChromosome cs = new MyChromosome(rects); cs.RandomizeGenes(); geneticfx.Organism o = new geneticfx.Organism(cs, 0.0F); initial_population.AddOrganism(o); } env.MutationRate = 0.10F; int num_generations = 10; env.SetupForEvolution(initial_population, geneticfx.FitnessDirection.Minimize); string fname = "out.svg"; fname = System.IO.Path.GetFullPath(fname); System.Xml.XmlWriter xw = new System.Xml.XmlTextWriter(fname, System.Text.Encoding.UTF8); xw.WriteStartElement("svg"); int cur_y = 100; for (int i = 0; i < num_generations; i++) { env.EvolveNextGeneration(); geneticfx.Generation generation = env.CurrentGeneration; int cur_x = 100; for (int generation_index = 0; generation_index < generation.population.Size; generation_index++) { geneticfx.Organism o = generation.population[generation_index]; MyChromosome mcr = (MyChromosome)o.Genes; RECT bb = RECTTOOLS.get_bounding_box(mcr.layout_rects); xw.WriteStartElement("g"); xw.WriteAttributeString("transform", string.Format("translate({0},{1})", cur_x, cur_y)); for (int icr = 0; icr < mcr.Length; icr++) { RECT r = mcr.layout_rects [icr]; xw.WriteStartElement("rect"); xw.WriteAttributeString("x", r.x0.ToString()); xw.WriteAttributeString("y", r.y0.ToString()); xw.WriteAttributeString("width", r.w.ToString()); xw.WriteAttributeString("height", r.h.ToString()); xw.WriteAttributeString("opacity", "0.1"); xw.WriteEndElement(); xw.WriteStartElement("text"); xw.WriteAttributeString("x", "0"); xw.WriteAttributeString("y", "0"); string s = string.Format("Gen{0} / Org{1} / Fit={2}", generation_index, o.ID, o.Fitness); xw.WriteString(s); xw.WriteEndElement(); } xw.WriteEndElement(); cur_x += (int)(1000 + 100); } cur_y += (int)(1000 + 100); xw.Flush(); } xw.WriteEndElement(); xw.Flush(); xw.Close(); }
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; }
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; }
public void Run() { geneticfx.Environment env = new geneticfx.Environment( ); env.StartGeneration+= new geneticfx.Environment.EventHandlerDelegate( this.StartGenerationHandler ); env.EndGeneration+= new geneticfx.Environment.EventHandlerDelegate( this.EndGenerationHandler ); geneticfx.Population initial_population = new geneticfx.Population( 50 ); System.Collections.ArrayList rects = new System.Collections.ArrayList(); rects.Add( new RECT(0,0,100,100) ); rects.Add( new RECT(0,0,100,200) ); rects.Add( new RECT(0,0,200,100) ); rects.Add( new RECT(0,0,200,200) ); rects.Add( new RECT(0,0,200,100) ); rects.Add( new RECT(0,0,200,200) ); rects.Add( new RECT(0,0,100,100) ); rects.Add( new RECT(0,0,100,200) ); rects.Add( new RECT(0,0,100,100) ); rects.Add( new RECT(0,0,100,200) ); rects.Add( new RECT(0,0,100,100) ); rects.Add( new RECT(0,0,100,200) ); for (int i=0;i<initial_population.Capacity;i++) { MyChromosome cs = new MyChromosome( rects ); cs.RandomizeGenes(); geneticfx.Organism o = new geneticfx.Organism( cs, 0.0F ); initial_population.AddOrganism(o); } env.MutationRate = 0.10F; int num_generations=10; env.SetupForEvolution( initial_population , geneticfx.FitnessDirection.Minimize); string fname ="out.svg"; fname = System.IO.Path.GetFullPath( fname ); System.Xml.XmlWriter xw = new System.Xml.XmlTextWriter( fname, System.Text.Encoding.UTF8 ); xw.WriteStartElement("svg"); int cur_y = 100; for (int i=0;i<num_generations;i++) { env.EvolveNextGeneration(); geneticfx.Generation generation = env.CurrentGeneration; int cur_x = 100; for (int generation_index=0;generation_index<generation.population.Size;generation_index++) { geneticfx.Organism o = generation.population[ generation_index ]; MyChromosome mcr = (MyChromosome) o.Genes; RECT bb = RECTTOOLS.get_bounding_box( mcr.layout_rects ); xw.WriteStartElement("g"); xw.WriteAttributeString( "transform", string.Format( "translate({0},{1})", cur_x, cur_y ) ); for (int icr=0;icr<mcr.Length;icr++) { RECT r = mcr.layout_rects [icr]; xw.WriteStartElement("rect"); xw.WriteAttributeString( "x", r.x0.ToString() ); xw.WriteAttributeString( "y", r.y0.ToString()); xw.WriteAttributeString( "width", r.w.ToString()); xw.WriteAttributeString( "height", r.h.ToString() ); xw.WriteAttributeString( "opacity", "0.1"); xw.WriteEndElement(); xw.WriteStartElement("text"); xw.WriteAttributeString( "x", "0" ); xw.WriteAttributeString( "y", "0" ); string s = string.Format( "Gen{0} / Org{1} / Fit={2}", generation_index,o.ID,o.Fitness ); xw.WriteString( s ); xw.WriteEndElement(); } xw.WriteEndElement(); cur_x += (int) (1000 + 100); } cur_y += (int) (1000 + 100); xw.Flush(); } xw.WriteEndElement(); xw.Flush(); xw.Close(); }