public void NextStep() { while (!paused) { if (set.populationSize == 1) { DNAPicture copy = population[0].Copy(); copy.Mutate(); if (copy.IsCalculated == false) { ++generation; copy.fitness = GetFitness(copy); copy.IsCalculated = true; if (copy.fitness >= population[0].fitness) { population[0] = copy; } } } else // Momentan nu merge daca populatia este mai mare de 1 { int selected = Math.Max(1, (int)Math.Round(set.populationSize * set.selectedAmount)); for (int i = selected; i < set.populationSize; ++i) { int ind1 = rand.Next(selected); int ind2 = rand.Next(selected); //while (ind1 == ind2) // ind2 = rand.Next(selected); population[i] = new DNAPicture(population[ind1], population[ind2]); population[i].Mutate(); population[i].fitness = GetFitness(population[i]); population[i].IsCalculated = true; } for (int i = 0; i < selected; ++i) { DNAPicture copy = population[i].Copy(); copy.Mutate(); if (copy.IsCalculated == false) { copy.fitness = GetFitness(copy); copy.IsCalculated = true; if (copy.fitness >= population[0].fitness) { population[i] = copy; } } } Array.Sort(population, (a, b) => b.fitness.CompareTo(a.fitness)); ++generation; } } }
public double GetFitness(DNAPicture ind) { Bitmap bmp = ind.GenerateBitmap(resX, resY); double fit = Deviation(bmp); return(100 - fit * 100 / maxFitness); }
public DNAPicture Copy() { DNAPicture copy = new DNAPicture(); copy.polygons.Clear(); copy.IsCalculated = calculated; copy.fitness = fitness; for (int i = 0; i < polygons.Count; ++i) { copy.polygons.Add(polygons[i].Copy()); } return(copy); }
public Genetic(Bitmap bmp, Settings set) { this.set = set; DNAPicture.set = set; DNAPicture.gen = this; DNAPolygon.set = set; DNAPolygon.gen = this; DNAPoint.set = set; DNAPoint.gen = this; DNABrush.set = set; DNABrush.gen = this; paused = true; rand = new Random(set.seed); resX = (int)Math.Round(bmp.Width * set.scale); resY = (int)Math.Round(bmp.Height * set.scale); generation = 1; maxFitness = (double)resX * resY * 255 * 255 * 3; targetBmp = new Bitmap(bmp, new Size(resX, resY)); bmpdata = new int[targetBmp.Height, targetBmp.Width, 3]; for (int y = 0; y < targetBmp.Height; ++y) { for (int x = 0; x < targetBmp.Width; ++x) { Color col = targetBmp.GetPixel(x, y); bmpdata[y, x, 0] = col.B; bmpdata[y, x, 1] = col.G; bmpdata[y, x, 2] = col.R; } } population = new DNAPicture[set.populationSize]; for (int i = 0; i < set.populationSize; ++i) { population[i] = new DNAPicture(); population[i].fitness = GetFitness(population[i]); population[i].IsCalculated = true; } Array.Sort(population, (a, b) => b.fitness.CompareTo(a.fitness)); }
public DNAPicture(DNAPicture mother, DNAPicture father) { calculated = false; fitness = 0; polygons = new List <DNAPolygon>(); int i = mother.polygons.Count - 1; int j = father.polygons.Count - 1; while (i >= 0 || j >= 0) { if (i >= 0 && j >= 0) { if (gen.GetRandom() < 0.5) { polygons.Add(mother.polygons[i]); } else { polygons.Add(father.polygons[j]); } } else if (i >= 0) { polygons.Add(mother.polygons[i]); } else { polygons.Add(father.polygons[j]); } --i; --j; } polygons.Reverse(); }