Beispiel #1
0
 //creates children from two parents
 public override void Reproduction()
 {
     for (int i = 0; i < nPairs; i++)
     {
         //define a split point in the parent's data
         int splitPoint = rand.Next(0, chromLength - 1);
         //get the current pair's index
         int p1Index = breedingPairs[i][0];
         int p2Index = breedingPairs[i][1];
         //get the current pair
         GABeingColour p1 = chromosomes[p1Index];
         GABeingColour p2 = chromosomes[p2Index];
         //generate two new children
         Color[] child1Data = new Color[chromLength];
         Color[] child2Data = new Color[chromLength];
         //crossover
         for (int digit = 0; digit < chromLength; digit++)
         {
             if (digit < splitPoint)
             {
                 child1Data[digit] = p1.Data[digit];
                 child2Data[digit] = p2.Data[digit];
             }
             else
             {
                 child1Data[digit] = p2.Data[digit];
                 child2Data[digit] = p1.Data[digit];
             }
         }
         GABeingColour child1 = new GABeingColour(child1Data, target);
         GABeingColour child2 = new GABeingColour(child2Data, target);
         //overwrite low ranking chromosomes
         int nextIndex = nKeep + i * 2;
         if (nextIndex < population - 1) //stop index exceptions when prKeep is over 0.5
         {
             chromosomes[nextIndex]     = child1;
             chromosomes[nextIndex + 1] = child2;
         }
     }
 }
Beispiel #2
0
        //end constructors


        //generated a random population of coloured chromosomes
        public override void GenerateRandomBeings()
        {
            //make a new being for everyone in the population
            for (int pop = 0; pop < population; pop++)
            {
                //new data for the new being
                Color[] newData = new Color[chromLength];
                //generate new being's colour
                for (int digit = 0; digit < chromLength; digit++)
                {
                    int   r         = rand.Next(Constants.MAX_COLOUR_VAL + 1);
                    int   b         = rand.Next(Constants.MAX_COLOUR_VAL + 1);
                    int   g         = rand.Next(Constants.MAX_COLOUR_VAL + 1);
                    Color curColour = Color.FromArgb(r, b, g);
                    newData[digit] = curColour;
                }
                //make the being and give it data
                GABeingColour newguy = new GABeingColour(newData, target);
                //put the being in chromosomes
                chromosomes[pop] = newguy;
            }
        }
Beispiel #3
0
        //draws the current chromosome
        private void DrawChromosome(GABeingColour guy, Graphics canvas, int canvasSize, int blockSize, int sideSize)
        {
            int index = 0; //index of the color in the chromosome to be drawn

            for (int y = 0; y < sideSize; y++)
            {
                //generate ypos
                int yPos = y * blockSize;
                for (int x = 0; x < sideSize; x++)
                {
                    //generate xpos
                    int xPos = x * blockSize;
                    //generate rectangle
                    Rectangle drawRect = new Rectangle(xPos, yPos, blockSize, blockSize);
                    //pull the drawing color from the chromosome
                    Color drawCol = guy.Data[index];
                    index++;
                    //draw
                    Brush fill = new SolidBrush(drawCol);
                    canvas.FillRectangle(fill, drawRect);
                }
            }
        }