Exemplo n.º 1
0
        private void UpdateStats()
        {
            if (Stats.Count > 0)
            {
                Last.SolveTime = Stats[Stats.Count - 1].SolveTime;
                Last.Info      = Stats[Stats.Count - 1].Info;
                if (Best.Ticks == 0 || Best > Last.SolveTime)
                {
                    Best = ((Best.Subtract(Best)).Add(Last.SolveTime));
                }
                if (Worst < Last.SolveTime)
                {
                    Worst = ((Worst.Subtract(Worst)).Add(Last.SolveTime));
                }
                Average = ((Average.Subtract(Average)).Add(new TimeSpan(((Average.Ticks * (Stats.Count - 1)) + Last.SolveTime.Ticks) / Stats.Count)));

                List <Statistic> temp = new List <Statistic>();
                temp.AddRange(Stats);
                temp   = temp.OrderBy(s => s.SolveTime).ToList();
                Median = ((Median.Subtract(Median)).Add((temp.Count % 2 == 1) ? (temp[(temp.Count - 1) / 2].SolveTime) : (new TimeSpan((((temp[(temp.Count / 2) - 1]).SolveTime.Ticks) + ((temp[temp.Count / 2]).SolveTime.Ticks)) / 2))));
                temp.Clear();

                Statistic b = null, w = null;
                long      a = 0;

                if (Stats.Count >= 5)
                {
                    temp.AddRange(Stats.Where((s) => { return((Stats.IndexOf(s) + 5) >= Stats.Count); }));
                    temp.ForEach((s) => { a += s.SolveTime.Ticks; if (b == null || b > s)
                                          {
                                              b = s;
                                          }
                                          if (w == null || w < s)
                                          {
                                              w = s;
                                          }
                                 });
                    AvgLastFive = ((AvgLastFive.Subtract(AvgLastFive)).Add(new TimeSpan(a / 5)));
                    if (AvgBestFive.Ticks == 0 || AvgBestFive > AvgLastFive)
                    {
                        AvgBestFive = ((AvgBestFive.Subtract(AvgBestFive)).Add(AvgLastFive));
                    }
                    temp.Remove(b);
                    temp.Remove(w);
                    a = 0;
                    temp.ForEach((s) => { a += s.SolveTime.Ticks; });
                    AvgLastThreeFive = ((AvgLastThreeFive.Subtract(AvgLastThreeFive)).Add(new TimeSpan(a / 3)));
                    if (AvgBestThreeFive.Ticks == 0 || AvgBestThreeFive > AvgLastThreeFive)
                    {
                        AvgBestThreeFive = ((AvgBestThreeFive.Subtract(AvgBestThreeFive)).Add(AvgLastThreeFive));
                    }
                    temp.Clear();
                    a = 0;
                    b = null;
                    w = null;

                    if (Stats.Count >= 10)
                    {
                        temp.AddRange(Stats.Where((s) => { return((Stats.IndexOf(s) + 10) >= Stats.Count); }));
                        temp.ForEach((s) => { a += s.SolveTime.Ticks; });
                        AvgLastTen = ((AvgLastTen.Subtract(AvgLastTen)).Add(new TimeSpan(a / 10)));
                        if (AvgBestTen.Ticks == 0 || AvgBestTen > AvgLastFive)
                        {
                            AvgBestTen = ((AvgBestTen.Subtract(AvgBestTen)).Add(AvgLastTen));
                        }
                        temp.Clear();
                        a = 0;

                        if (Stats.Count >= 12)
                        {
                            temp.AddRange(Stats.Where((s) => { return((Stats.IndexOf(s) + 12) >= Stats.Count); }));
                            temp.ForEach((s) => { if (b == null || b > s)
                                                  {
                                                      b = s;
                                                  }
                                                  if (w == null || w < s)
                                                  {
                                                      w = s;
                                                  }
                                         });
                            temp.Remove(b);
                            temp.Remove(w);
                            temp.ForEach((s) => { a += s.SolveTime.Ticks; });
                            AvgLastTenTwelve = ((AvgLastTenTwelve.Subtract(AvgLastTenTwelve)).Add(new TimeSpan(a / 10)));
                            if (AvgBestTenTwelve.Ticks == 0 || AvgBestTenTwelve > AvgLastTenTwelve)
                            {
                                AvgBestTenTwelve = ((AvgBestTenTwelve.Subtract(AvgBestTenTwelve)).Add(AvgLastTenTwelve));
                            }
                            temp.Clear();
                            a = 0;
                            b = null;
                            w = null;
                        }

                        #region Resets
                        else
                        {
                            ResetTwelves();
                        }
                    }

                    else
                    {
                        ResetTens();
                        ResetTwelves();
                    }
                }

                else
                {
                    ResetFives();
                    ResetTens();
                    ResetTwelves();
                }
                #endregion
            }

            else
            {
                ResetStats();
            }

            #region UpdateProperty() calls
            UpdateProperty("Last");
            UpdateProperty("Best");
            UpdateProperty("Worst");
            UpdateProperty("Average");
            UpdateProperty("Median");
            UpdateProperty("AvgLastFive");
            UpdateProperty("AvgBestFive");
            UpdateProperty("AvgLastTen");
            UpdateProperty("AvgBestTen");
            UpdateProperty("AvgLastThreeFive");
            UpdateProperty("AvgBestThreeFive");
            UpdateProperty("AvgLastTenTwelve");
            UpdateProperty("AvgBestTenTwelve");
            #endregion
        }
Exemplo n.º 2
0
        public Population Breed()
        {
            if (indexes == null)
            {
                indexes = new List <int>();
                for (var i = 0; i < Individuals.Count; i++)
                {
                    for (var j = 0; j <= i; j++)
                    {
                        indexes.Add(j);
                    }
                }

                indexes.Sort();
            }

            var ranked = Rank().Individuals.ToList();



            var bitmap = new System.Drawing.Bitmap(32, 32);

            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
            foreach (var chromosome in ranked.First().Chromosomes)
            {
                var argb = (chromosome.Color.A << 24) | (chromosome.Color.R << 16) | (chromosome.Color.G << 8) | chromosome.Color.B;

                var brush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(argb));
                graphics.FillRectangle(brush, chromosome.X, chromosome.Y, chromosome.Radius, chromosome.Radius);
            }

            FittestFile = Path.GetTempFileName() + ".ga.png";
            bitmap.Save(FittestFile, System.Drawing.Imaging.ImageFormat.Png);

            Best    = ((int)Math.Abs(ranked[0].Fitness)).ToString("n0");
            Worst   = ((int)Math.Abs(ranked[ranked.Count - 1].Fitness)).ToString("n0");
            Total   = ranked.Sum(x => Math.Abs((long)x.Fitness)).ToString("n0");
            Average = (((long)ranked.Average(x => Math.Abs((long)x.Fitness)))).ToString("n0");

            Console.WriteLine($"{Utility.Generation.ToString().PadRight(6)}Best/Worst/Total/Average: {Best.PadLeft(15)}{Worst.PadLeft(15)}{Total.PadLeft(15)}{Average.PadLeft(15)}");
            var nextGeneration = new List <Individual>();

            while (nextGeneration.Count < Individuals.Count)
            {
                var twoRandoms = new[] { indexes[Utility.Random.Next(indexes.Count)], indexes[Utility.Random.Next(indexes.Count)] };

                var momIndex = (twoRandoms[0] * twoRandoms[0]) / ranked.Count;
                var popIndex = (twoRandoms[1] * twoRandoms[1]) / ranked.Count;
                var mom      = ranked[momIndex];
                var pop      = ranked[popIndex];

                var child = Individual.Breed(mom, pop);
                nextGeneration.Add(child);
            }

            return(new Population(nextGeneration.ToArray()));
        }
Exemplo n.º 3
0
 public void AddPoints(Population population)
 {
     Best.Add(new Point(Best.Count, population.CalculateBestDistance()));
     Worst.Add(new Point(Worst.Count, population.CalculateWorstDistance()));
     Average.Add(new Point(Average.Count, population.CalculateAverageDistance()));
 }