Example #1
0
 public static void CHECK_Q(double q)
 {
     if (!MyChromosome.is_valid_q(q))
     {
         throw new Exception("Relative q is out of range");
     }
 }
Example #2
0
 public static void CHECK_SIDE(int side)
 {
     if (!MyChromosome.is_valid_side(side))
     {
         throw new Exception("side is out of range");
     }
 }
Example #3
0
 public MyGene(int a, double b)
 {
     MyChromosome.CHECK_SIDE(a);
     MyChromosome.CHECK_Q(b);
     this.side = a;
     this.q    = b;
 }
Example #4
0
		public geneticfx.IChromosome Clone()
		{
			MyChromosome the_clone = new MyChromosome( this.original_rects );
			int i=0;
			foreach ( MyGene g in this.genes )
			{
				the_clone.genes[i] = g;
				i++;
			}
			return the_clone;
		}
Example #5
0
        public void SwapGenesBetween(int first_index, int last_index, geneticfx.IChromosome other_chromosome)
        {
            MyChromosome ocs = (MyChromosome)other_chromosome;

            for (int i = first_index; i <= last_index; i++)
            {
                MyGene swap = (MyGene)this.genes[i];
                this.genes[i] = ocs.genes[i];
                ocs.genes[i]  = swap;
            }
        }
Example #6
0
		public geneticfx.IChromosome Clone()
		{
			// create a by-value copy of the object
			MyChromosome the_clone = new MyChromosome();
			int i=0;
			foreach ( bool v in this.genes )
			{
				the_clone.genes[i] = v;
				i++;
			}
			return the_clone;
		}
Example #7
0
        public void SwapGenesBetween(int first_index, int last_index, geneticfx.IChromosome other_chromosome)
        {
            // swap genes between two chromosomes
            MyChromosome ocs = (MyChromosome)other_chromosome;

            for (int i = first_index; i <= last_index; i++)
            {
                bool swap = this.genes[i];
                this.genes[i] = ocs.genes[i];
                ocs.genes[i]  = swap;
            }
        }
Example #8
0
        public geneticfx.IChromosome Clone()
        {
            MyChromosome the_clone = new MyChromosome(this.original_rects);
            int          i         = 0;

            foreach (MyGene g in this.genes)
            {
                the_clone.genes[i] = g;
                i++;
            }
            return(the_clone);
        }
Example #9
0
        public geneticfx.IChromosome Clone()
        {
            // create a by-value copy of the object
            MyChromosome the_clone = new MyChromosome();
            int          i         = 0;

            foreach (bool v in this.genes)
            {
                the_clone.genes[i] = v;
                i++;
            }
            return(the_clone);
        }
Example #10
0
        public void MutateGene(int index)
        {
            double rate = 0.5;
            MyGene g    = (MyGene)this.genes[index];

            if (r.NextDouble() > rate)
            {
                g.side = MyChromosome.get_random_side();
            }
            else
            {
                g.q = MyChromosome.get_random_q();
            }
            this.genes[index] = g;
        }
Example #11
0
        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);
        }
Example #12
0
		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);

		}
Example #13
0
        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();
        }
Example #14
0
		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();

		}