/// <summary> /// To display the image, create a Bitmap (easist way to render to a winforms picturebox) /// Write our image data pixel by pixel to the new bitmap. /// </summary> /// <param name="colorData"></param> /// <param name="header"></param> /// <returns></returns> public Bitmap GetImageFromColorData(Individual individual, int width, int height) { Bitmap image = new Bitmap(width, height); var colorData = individual.Genome; for (int x = image.Width - 1; x > 0; x--) { for (int y = image.Height - 1; y > 0; y--) { int index = TranslateXYToListIndex(x, y, image.Width); if (index >= colorData.Length) index = colorData.Length - 1; Color color = Color.FromArgb(colorData[index].R, colorData[index].G, colorData[index].B); image.SetPixel(x, y, color); } } //Flip the bmp to display it on screen image.RotateFlip(RotateFlipType.Rotate270FlipX); return image; }
/// <summary> /// Choose a child to survive. /// (ideally this would be done via a gladiator match between the two children. (pay per view is not out of the question)) /// </summary> /// <param name="childOne"></param> /// <param name="childTwo"></param> /// <returns></returns> private Individual ChooseRandomChild(Individual childOne, Individual childTwo) { int randomChild = RNG.Next(0, 2); if (randomChild == 0) return childOne; else return childTwo; }
/// <summary> /// Creates the initial population of individuals, each with a random starting genome. /// </summary> protected override void CreateInitialPopulation() { var configuration = (_configuration as ImageConfiguration); for (int index = 0; index < _configuration.PopulationSize; index++) { Individual individual = new Individual( configuration.Width * configuration.Height ); _currentGeneration.Add(individual); } }
/// <summary> /// Create a child using crossover reproduction between the two parents. /// </summary> /// <param name="one"></param> /// <param name="two"></param> /// <returns></returns> private Individual GenerateChild(Individual one, Individual two) { //StringBuilder genome = new StringBuilder(one.Genome.Length); RGB[] genome = new RGB[one.Genome.Length]; int crossoverPoint = one.Genome.Length / 2; GetFirstParentGenome(one, genome, crossoverPoint); GetSecondParentGenome(two, genome, crossoverPoint); return new Individual(genome); }
/// <summary> /// Iterate over each allele, each has a random probability of being mutated to a new color value. /// </summary> /// <param name="toMutate"></param> private void ChangeGenome(Individual toMutate) { double probabilityOfMutation = _configuration.MutationRate; for (int index = 0; index < toMutate.Genome.Length; index++) { if (RNG.NextDouble() < probabilityOfMutation) { var newAllele = GetNewRandomAllele(); toMutate.Genome[index] = newAllele; } } }
/// <summary> /// Give the child the second parents genomes. /// </summary> /// <param name="parentTwo"></param> /// <param name="childCoins"></param> /// <param name="crossoverPoint"></param> private void GetSecondParentGenome(Individual two, RGB[] genome, int crossoverPoint) { for (int index = crossoverPoint; index < two.Genome.Length; index++) genome[index] = (two.Genome[index]); }
/// <summary> /// give the child the first parents genome. /// </summary> /// <param name="parentOne"></param> /// <param name="childCoins"></param> /// <param name="crossoverPoint"></param> private void GetFirstParentGenome(Individual one, RGB[] genome, int crossoverPoint) { for (int index = 0; index < crossoverPoint; index++) genome[index] = (one.Genome[index]); }