public void CalculateDifferences(GeneticPopulation population, GeneticBlackBox blackBox) { foreach (GeneticSample sample in population.Samples) { sample.DifferenceUnits = blackBox.GetDifferenceUnits(sample.Color); sample.DifferencePercents = blackBox.GetDifferencePercents(sample.Color); sample.SimilarityUnits = blackBox.GetSimilarityUnits(sample.Color); sample.SimilarityPercents = blackBox.GetSimilarityPercents(sample.Color); } }
private void AddPopulation(GeneticPopulation population) { dgvPopulations.Rows.Add ( dgvPopulations.Rows.Count + 1, GetEliteImage(population), string.Concat(population.Elite.Color.Red, " ", population.Elite.Color.Green, " ", population.Elite.Color.Blue), population.Elite.DifferenceUnits, population.Elite.SimilarityPercents, GetPopulationImage(population) ); }
private void _algorythm_PopulationCalculated(byte progressPercents, GeneticPopulation population) { if (this.InvokeRequired) this.Invoke(new GeneticAlgorithm.PopulationDelegate(_algorythm_PopulationCalculated), progressPercents, population); else { pbProgress.Value = progressPercents; AddPopulation(population); } }
private void _algorythm_CalculationSucess(byte progressPercents, GeneticPopulation population) { if (this.InvokeRequired) this.Invoke(new GeneticAlgorithm.PopulationDelegate(_algorythm_CalculationSucess), progressPercents, population); else { ShowResults(population); ToggleFromControls(true); if (_closeFlag) this.Close(); } }
private void ShowResults(GeneticPopulation population) { var color = population.Elite.Color; btnEliteColor.BackColor = Color.FromArgb(byte.MaxValue, color.Red, color.Green, color.Blue); tbxEliteValue.Text = string.Concat(color.Red, " ", color.Green, " ", color.Blue); if (color.Red == btnTarget.BackColor.R && color.Green == btnTarget.BackColor.G && color.Blue == btnTarget.BackColor.B) { tbxResult.Text = @"SUCCESS"; } else { tbxResult.Text = string.Format("{0} %", population.Elite.SimilarityPercents); } }
private Image GetPopulationImage(GeneticPopulation population) { const int size = 10; const int w = size - 1; var bmp = new Bitmap(size * population.Samples.Count, size); var g = Graphics.FromImage(bmp); for (int i = 0; i < population.Samples.Count; i++) { var color = Color.FromArgb(byte.MaxValue, population.Samples[i].Color.Red, population.Samples[i].Color.Green, population.Samples[i].Color.Blue); g.FillRectangle(new SolidBrush(color), i * size, 0, size, w); g.DrawRectangle(Pens.Black, i * size, 0, size, w); } g.DrawLine(Pens.Black, population.Samples.Count * size - 1, 0, population.Samples.Count * size - 1, size); return bmp; }
private Image GetEliteImage(GeneticPopulation population) { const int sizex = 45, sizey = 15; const int w = sizex - 1, h = sizey - 1; var bmp = new Bitmap(sizex, sizey); var g = Graphics.FromImage(bmp); var srcColor = population.Elite.Color; var color = Color.FromArgb(byte.MaxValue, srcColor.Red, srcColor.Green, srcColor.Blue); g.FillRectangle(new SolidBrush(color), 0, 0, w, h); g.DrawRectangle(Pens.Black, 0, 0, w, h); return bmp; }
public void CrossPopulation(GeneticPopulation population) { var count = population.Samples.Count; var restCount = Convert.ToInt32(0.01 * count * _settings.Elitism); if (restCount < 1) restCount = 1; var removeCount = count - restCount; var minDeviation = Convert.ToInt32(0.01*byte.MaxValue*_settings.MutabilityMin); var maxDeviation = Convert.ToInt32(0.01*byte.MaxValue*_settings.MutabilityMax)+1; population.Samples.RemoveRange(restCount, removeCount); for (var i = restCount; i < count; i++) { var parent1Index = _random.Next(0, restCount); var parent2Index = _random.Next(0, restCount); var parent1Percentage = Convert.ToByte(_random.Next(0, 101)); var childColor = population.Samples[parent1Index].Color.Blend( population.Samples[parent2Index].Color, parent1Percentage); if (_random.Next(0, 101) < _settings.Mutation) { childColor.Red += Convert.ToByte(_random.Next(minDeviation, maxDeviation)); childColor.Green += Convert.ToByte(_random.Next(minDeviation, maxDeviation)); childColor.Blue += Convert.ToByte(_random.Next(minDeviation, maxDeviation)); } var child = new GeneticSample { Color = childColor, DifferenceUnits = 0, DifferencePercents = 0, SimilarityPercents = 0, SimilarityUnits = 0, Age = 0 }; population.Samples.Add(child); } for(var i=0; i<restCount;i++) { population.Samples[i].Age++; if (population.Samples[i].Age>=_settings.Age) { population.Samples[i] = GeneticSample.GetRandomGeneticSample(_random); } } }
private void InitPopulation(out GeneticPopulation population) { population = new GeneticPopulation(); for (var i=0;i<_settings.Size;i++) { var sample = GeneticSample.GetRandomGeneticSample(_random); population.Samples.Add(sample); } }
public void SortPopulation(GeneticPopulation population) { var samples = new List<GeneticSample>(); population.Samples.Sort(new Comparison<GeneticSample>( (s1, s2) => s1.DifferenceUnits.CompareTo(s2.DifferenceUnits))); var samplesByDifference = population.Samples.GroupBy(s => s.DifferenceUnits); foreach (IGrouping<int, GeneticSample> samplesGroup in samplesByDifference) { var samplesList = samplesGroup.ToList(); samplesList.Sort((s1, s2) => s2.Age.CompareTo(s1.Age)); samples.AddRange(samplesList); } population.Samples = samples; }