示例#1
0
 /// <summary>
 /// Remove all robots from the instance. This is only usable for visualization purposes.
 /// </summary>
 public void VisClearBots()
 {
     Bots.Clear();
     foreach (var tier in Compound.Tiers)
     {
         tier.Bots.Clear();
     }
 }
示例#2
0
 public void Reset()
 {
     Bots.Clear();
     Bombs.Clear();
     Missiles.Clear();
     Explosions.Clear();
     Board = new BoardTile[Board.GetLength(0), Board.GetLength(1)];
     OnArenaChanged();
 }
示例#3
0
 private void ResetState()
 {
     LiveAccounts.Clear();
     DemoAccounts.Clear();
     Accounts.Clear();
     accountsByName.Clear();
     Bots.Clear();
     //Alerts.Clear();
     Sessions.Clear();
     State = ExecutionStateEx.Uninitialized;
 }
示例#4
0
        public void Dispose()
        {
            Bots?.Clear();
            Indicators?.Clear();

            MainLoopRunning = false;

            BackTestingChange  -= HandleBackTestingChange;
            AllowTradesChanged -= HandleAllowTradesChanged;

            PriceData = null;
            User      = null;
            Shutdown  = null;
            Util.Dispose(Log);
        }
示例#5
0
 public static void Deinitialize()
 {
     if (Bots != null)
     {
         foreach (IBot bot in Bots)
         {
             try
             {
                 bot.Deinitialize();
             }
             catch (Exception exception)
             {
                 ilog_0.ErrorFormat("[BotManager] An exception occured in {0}'s Deinitialize function. {1}", bot.Name, exception);
             }
         }
         Bots.Clear();
     }
 }
示例#6
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++;
        }
示例#7
0
        /// <summary>
        /// Load given robots, compile, instantiate and initialize
        /// Sets loaded robots Id's and calls each robots Initialize method
        /// </summary>
        /// <param name="botsSource"></param>
        /// <returns></returns>
        public bool Load(string[] botsSource)
        {
            string currentSourceFile = String.Empty;

            Bots.Clear();
            if (null != Errors)
            {
                Errors.Clear();
            }

            try
            {
                int botId = 1;
                foreach (var sourceFile in botsSource)
                {
                    // Create a unique assembly name so we can pit the same bot class against itself
                    string assemblyName = Path.GetFileNameWithoutExtension(sourceFile) + botId;
                    currentSourceFile = sourceFile;
                    BotAssembly bot = Compile(sourceFile, assemblyName);

                    if (null == bot || null == bot.AssemblyInstance)
                    {
                        return(false);
                    }
                    Ininialize(ref bot, ++botId);
                    Bots.Add(bot);
                }
            }
            catch (Exception ex)
            {
                if (null != Errors)
                {
                    Errors.Add(new CompilerError(currentSourceFile, 0, 0, "0", ex.ToString()));
                }
                return(false);
            }

            return(true);
        }
示例#8
0
        private void HandleBackTestingChange(object sender, PrePostEventArgs e)
        {
            // If back testing is about to be enabled...
            if (!BackTesting && e.Before)
            {
                // Turn off the update thread for each exchange
                foreach (var exch in Exchanges)
                {
                    exch.UpdateThreadActive = false;
                }

                // Turn off the update thread for each price data instance
                foreach (var pd in PriceData)
                {
                    pd.UpdateThreadActive = false;
                }

                // Deactivate all live trading bots (actually, deactivate all of them)
                Bots.Clear();

                // Integrate market updates so that updates from live data don't end up in back testing data.
                IntegrateDataUpdates();

                // Save and reset the current Fund container
                Funds.SaveToSettings(Exchanges);
                Funds.AssignFunds(new FundData[0]);

                // Save and reset the indicator container
                Indicators.Save();
                Indicators.Clear();

                // Disable live trading
                AllowTrades = false;
            }

            // If back testing has just been enabled...
            if (BackTesting && e.After)
            {
                // Create the fund container from the back testing settings
                Funds.AssignFunds(SettingsData.Settings.BackTesting.TestFunds);

                // Create the back testing bots
                Bots.LoadFromSettings();

                // Reset the balance collections in each exchange
                foreach (var exch in Exchanges)
                {
                    exch.Balance.Clear();
                }

                // Create the simulation manager
                Simulation = new Simulation(TradingExchanges, PriceData, Bots);

                // Enable backtesting bots based on their settings
                foreach (var bot in Bots.Where(x => x.BackTesting))
                {
                    bot.Active = bot.BotData.Active;
                }

                // Restore the backtesting indicators
                Indicators.Load();
            }

            // If back testing is about to be disabled...
            if (BackTesting && e.Before)
            {
                // Deactivate all back testing bots (actually, deactivate all of them)
                Bots.Clear();

                // Remove (without saving) the current fund container
                Funds.AssignFunds(new FundData[0]);

                // Save and reset the indicator container
                Indicators.Save();
                Indicators.Clear();
            }

            // If back testing has just been disabled...
            if (!BackTesting && e.After)
            {
                // Clean up the simulation
                Simulation = null;

                // Restore the fund container from settings
                Funds.AssignFunds(SettingsData.Settings.LiveFunds);

                // Restore the indicators
                Indicators.Load();

                // Restore the live trading bots
                Bots.LoadFromSettings();

                // Reset the balance collections in each exchange
                foreach (var exch in Exchanges)
                {
                    exch.Balance.Clear();
                }

                // Turn on the update thread for each price data instance
                foreach (var pd in PriceData)
                {
                    pd.UpdateThreadActive = pd.RefCount != 0;
                }

                // Turn on the update thread for each exchange
                foreach (var exch in Exchanges)
                {
                    exch.UpdateThreadActive = exch.Enabled;
                }

                // Enable live trading bots based on their settings
                foreach (var bot in Bots.Where(x => !x.BackTesting))
                {
                    bot.Active = bot.BotData.Active;
                }
            }
        }
        private async Task ApplyBalanceResults(BalanceTeamsResult balance)
        {
            if (!IsNullOrEmpty(balance.Message))
            {
                await SayBattle(balance.Message);
            }
            if ((balance.Players != null) && (balance.Players.Count > 0))
            {
                foreach (var p in balance.Players)
                {
                    UserBattleStatus u;
                    if (Users.TryGetValue(p.Name, out u))
                    {
                        u.IsSpectator = p.IsSpectator;
                        u.AllyNumber  = p.AllyID;
                    }
                }

                foreach (var u in Users.Where(x => !balance.Players.Any(y => y.Name == x.Key)))
                {
                    u.Value.IsSpectator = true;
                }
            }

            if (balance.DeleteBots)
            {
                foreach (var b in Bots.Keys)
                {
                    await server.Broadcast(Users.Keys, new RemoveBot()
                    {
                        Name = b
                    });
                }
                Bots.Clear();
            }

            if ((balance.Bots != null) && (balance.Bots.Count > 0))
            {
                foreach (var p in balance.Bots)
                {
                    Bots.AddOrUpdate(p.BotName,
                                     s => new BotBattleStatus(p.BotName, p.Owner ?? FounderName, p.BotAI)
                    {
                        AllyNumber = p.AllyID
                    },
                                     (s, status) =>
                    {
                        status.AllyNumber = p.AllyID;
                        status.owner      = p.Owner ?? FounderName;
                        status.aiLib      = p.BotAI;
                        status.Name       = p.BotName;
                        return(status);
                    });
                }
            }

            foreach (var u in Users.Values.Select(x => x.ToUpdateBattleStatus()).ToList())
            {
                await server.Broadcast(Users.Keys, u);                                                                            // send other's status to self
            }
            foreach (var u in Bots.Values.Select(x => x.ToUpdateBotStatus()).ToList())
            {
                await server.Broadcast(Users.Keys, u);
            }
        }