private double Score(PolygonGroup polygonGroup) { Bitmap polygonBitmap = polygonGroup.GetBitmap(); int redSum = 0; int greenSum = 0; int blueSum = 0; int totalSum = 0; //int luminanceSum = 0; ImageStatisticsHSL imageStatsHsl = new ImageStatisticsHSL(polygonBitmap); int[] luminanceValues = imageStatsHsl.Luminance.Values; ImageStatistics imgStats = new ImageStatistics(polygonBitmap); int[] redValues = imgStats.Red.Values; int[] greenValues = imgStats.Green.Values; int[] blueValues = imgStats.Blue.Values; for (int i = 0; i < redValues.Length; i++) { redSum += Math.Abs(this.originalRedValues[i] - redValues[i]); } for (int i = 0; i < greenValues.Length; i++) { greenSum += Math.Abs(this.originalGreenValues[i] - greenValues[i]); } for (int i = 0; i < blueValues.Length; i++) { blueSum += Math.Abs(this.originalBlueValues[i] - blueValues[i]); } totalSum = redSum + greenSum + blueSum; return (int.MaxValue - totalSum); }
private void DrawPolygonGroup(PolygonGroup group) { this.drawingwatch = Stopwatch.StartNew(); foreach (Rectangle r in group.Polygons) { this.Canvas.Children.Add(r.Shape); Canvas.SetTop(r.Shape, r.Y); Canvas.SetLeft(r.Shape, r.X); } this.drawingwatch.Stop(); this.timeDrawingPopulation = this.drawingwatch.ElapsedMilliseconds; }
private void RunGenerations(int maxGenerations) { int generations = 0; this.CreateRandomPopulation(); this.totalwatch = Stopwatch.StartNew(); while (generations < maxGenerations) { double topScore = 0; this.scorewatch = Stopwatch.StartNew(); // Score population foreach(PolygonGroup pg in this.population) { pg.Score = this.Score(pg); if(pg.Score > topScore) { topScore = pg.Score; this.bestGroup = pg; } } foreach (PolygonGroup pg in this.population) { pg.AdjustedScore = pg.Score / topScore; } this.scorewatch.Stop(); this.timeScoring += this.scorewatch.ElapsedMilliseconds; // Create new generation List<PolygonGroup> newGeneration = this.CreateNewGeneration(); this.population.Clear(); this.population = newGeneration; generations++; } this.avgScoring = this.timeScoring / this.population.Count; this.totalwatch.Stop(); this.timeRunningGenerations = this.totalwatch.ElapsedMilliseconds; }
private Tuple<PolygonGroup, PolygonGroup> CreateOffspring(PolygonGroup parent1, PolygonGroup parent2) { Random rand = new Random(); double crossoverProbability = rand.NextDouble(); double mutationProbability = rand.NextDouble(); string parent1Chromosome = parent1.GetChromosome(); string parent2Chromosome = parent2.GetChromosome(); string offspring1Chromosome = string.Empty; string offspring2Chromosome = string.Empty; if (crossoverProbability < this.CrossoverRate) { int crossoverIndex = rand.Next(0, parent1Chromosome.Length); offspring1Chromosome = string.Format("{0}{1}", parent1Chromosome.Substring(0, crossoverIndex), parent2Chromosome.Substring(crossoverIndex)); offspring2Chromosome = string.Format("{0}{1}", parent2Chromosome.Substring(0, crossoverIndex), parent1Chromosome.Substring(crossoverIndex)); } else { offspring1Chromosome = parent1Chromosome; offspring2Chromosome = parent2Chromosome; } if (mutationProbability < this.MutationRate) { // TODO: Mutate offspring chromosomes } PolygonGroup offspring1 = new PolygonGroup(offspring1Chromosome); PolygonGroup offspring2 = new PolygonGroup(offspring2Chromosome); return new Tuple<PolygonGroup, PolygonGroup>(offspring1, offspring2); }