/// <summary> /// Vytvor prvu generaciu jedincov /// </summary> /// <param name="settings"></param> private static void CreateFirstGeneration(Settings settings) { for (var i = 0; i < settings.MaxJedincov; ++i) { _aktualnaGeneracia[i] = new Jedinec(settings.InitRadnom); } }
/// <summary> /// Nacitaj nastavenia /// </summary> /// <param name="settings">Nastavenia, kt. sa nactiaju zo suboru</param> /// <returns>Aktualne nastavenia</returns> private static bool Setup(out Settings settings) { LoadSettings(out settings); if (settings == null) { Console.WriteLine("Error loading settings"); Console.WriteLine("Press any key"); Console.ReadKey(true); return(false); } WriteSettings(settings); StopTimer.Elapsed -= OnStopTimerOnElapsed; if (settings.StopAfter.Typ == StopType.Seconds) { StopTimer.Interval = settings.StopAfter.Hodnota * 1000; StopTimer.Elapsed += OnStopTimerOnElapsed; settings.StopAfter.Hodnota = int.MaxValue; _timer = true; } _aktualnaGeneracia = new Jedinec[settings.MaxJedincov]; _novaGeneracia = new Jedinec[settings.MaxJedincov]; return(true); }
/// <summary> /// Hlada jedincov zo zoradeneho pola pomocou rulety /// </summary> /// <param name="sorted">Zoradene pole jedincov</param> /// <param name="total">Suma fitness vsetkych jedincov</param> /// <param name="a">Rodic A</param> /// <param name="b">Rodic B</param> private static void Ruleta(IReadOnlyList <Jedinec> sorted, int total, out Jedinec a, out Jedinec b) { var last = 0; var index = Rand.Next(total); a = null; var skip = -1; for (var i = 0; i < sorted.Count; i++) { if (index < sorted[i].Fitness + last) { a = sorted[i]; skip = i; break; } last += sorted[i].Fitness; } if (a == null) { a = sorted.Last(); skip = sorted.Count - 1; } total -= a.Fitness; last = 0; index = Rand.Next(total); b = null; for (var i = 0; i < sorted.Count; i++) { if (i == skip) { continue; } if (index < sorted[i].Fitness + last) { b = sorted[i]; break; } last += sorted[i].Fitness; } if (b == null) { b = sorted.Last(); } }
internal Jedinec Krizenie(Jedinec other, Settings settings) { var result = new Jedinec(); var point = Rand.Next(settings.BodKrizenia.Min, settings.BodKrizenia.Max); var i = 0; for (; i < point; i++) { result._bunky[i] = _bunky[i]; } for (; i < 64; i++) { result._bunky[i] = other._bunky[i]; } return(result); }
/// <summary> /// Vyber rodicov pomocou turnaja /// </summary> /// <param name="populacia">Pole jedincov</param> /// <param name="a">Prvy rodic</param> /// <param name="b">Druhy rodic</param> private static void Turnaj(IReadOnlyList <Jedinec> populacia, out Jedinec a, out Jedinec b) { var vyber = new List <Jedinec>(4); while (vyber.Count < 4) { var dalsi = populacia[Rand.Next(populacia.Count)]; if (vyber.Contains(dalsi)) { continue; } vyber.Add(dalsi); } vyber.Sort((jedinec1, jedinec2) => jedinec1.Fitness.CompareTo(jedinec2.Fitness)); a = vyber[3]; b = vyber[2]; }