예제 #1
0
        void Init()
        {
            Best = new FinalAnswer(_model, _checker.Solve(_model));

            Bots = new List <Bot>();

            for (var i = 0; i < TotalCount; i++)
            {
                var bot = new Bot(_model, Best.Price);

                for (var j = 0; j < bot.Dna.Length; j++)
                {
                    bot.Dna[j] = BotCommandExtension.GetRandomCommand();
                }

                Bots.Add(bot);
            }
        }
예제 #2
0
        public void NextGeneration()
        {
            Bots.Sort((a, b) => a.Best.CompareTo(b.Best));

            var count = Math.Max(1, Math.Min(BestCount, Bots.Count));

            var bestBots = Bots.Take(count).ToArray();

            Bots.Clear();

            var i = 0;

            while (Bots.Count < TotalCount)
            {
                var child = new Bot(_model, _checker.Solve(_model))
                {
                    Dna = bestBots[i].Dna,
                };

                // Вероятностная мутация
                if (_random.Next(100) <= 35)
                {
                    // Количество ячеек мутации
                    var mutationsCount = _random.Next(1, child.Dna.Length / 8);

                    for (var j = 0; j < mutationsCount; j++)
                    {
                        var k = _random.Next(child.Dna.Length);
                        child.Dna[k] = BotCommandExtension.GetRandomCommand();
                    }

                    child.IsMutant = true;
                }

                Bots.Add(child);

                if (++i >= bestBots.Length)
                {
                    i = 0;
                }
            }

            Generation++;
        }