Beispiel #1
0
        public async Task StartAsync()
        {
            for (int i = 1; i <= Settings.MaxTurns; i++)
            {
                Turn = TurnFactory.Create(i);
                OnTurnStarting();
                await Turn.StartAsync(Bots);

                OnTurnFinished();
                if (Bots.Count(b => b.HP > 0) <= 1)
                {
                    break;
                }
                await WaitForNextTurnAsync();
            }

            var scores = new List <Score>();

            foreach (var bot in Bots)
            {
                scores.Add(new Score {
                    BotName = bot.Name, Kills = bot.Kills, Deaths = bot.Deaths
                });
            }
            Scores = scores;
        }
Beispiel #2
0
        public bool IsFull()
        {
            var size = Width * Height;
            var bots = Bots.Count();

            // Consider >10% of the board to be full
            // E.g. 10x10 with 10 bots is considered full
            return(bots > (int)(size * 0.1));
        }
Beispiel #3
0
        public void Start()
        {
            Iteration = 0;

            while (true)
            {
                if (Token.IsCancellationRequested)
                {
                    var model = Model.Copy(Best.Model);
                    model.Blocks.Clear();
                    model.Blocks.AddRange(_model.Blocks);

                    OnStepEvent?.Invoke(this, new FinalAnswer(model, _checker.Solve(model)));

                    throw new OperationCanceledException("Работа алгоритма остановлена в связи с отменой задачи");
                }

                if (Bots.Count(_ => !_.IsDead) == 0)
                {
                    NextGeneration();

                    continue;
                }

                for (var i = 0; i < Bots.Count; i++)
                {
                    var bot = Bots[i];

                    if (bot.IsDead)
                    {
                        continue;
                    }

                    ExecuteCommand(bot);

                    bot.Cur++;

                    var answer = bot.Root.ConvertToModel(bot.Current.Model);
                    var price  = _checker.Solve(answer);
                    var trace  = TraceBuilder.CalculateTrace(answer);

                    if (trace.Exceptions.Any() ||
                        price.Result < -1000) // TODO: const MIN_PRICE
                    {
                        bot.IsDead = true;
                    }

                    if (Math.Abs(price.Result - bot.Current.Price.Result) < Constants.Precision)
                    {
                        bot.DeadLoops++;

                        if (bot.DeadLoops > 100)
                        {
                            bot.IsDead = true;
                        }
                    }
                    else
                    {
                        bot.DeadLoops = 0;
                    }

                    bot.Current = new FinalAnswer(answer, price);

                    //OnStepEvent?.Invoke(this, bot.Current);

                    if (price > bot.Best.Price)
                    {
                        bot.Best = bot.Current;

                        if (price > Best.Price)
                        {
                            Best = bot.Best;
                            OnStepEvent?.Invoke(this, Best);
                        }
                    }
                }

                OnStepEvent?.Invoke(this, Best);

                Iteration++;
            }
        }
Beispiel #4
0
        private Task WaitForNextTurnAsync()
        {
            var speedUp = Bots.Any() ? new TimeSpan(Settings.NextTurnDelay.Ticks / Bots.Count(b => b.HP > 0)) : new TimeSpan();

            return(Task.Delay(Settings.NextTurnDelay - speedUp));
        }