Exemple #1
0
        public EvolutionaryStrategies(string configFilename)
        {
            // Grab the config info
            _configFilename = configFilename;
            var config = Toml.ReadFile <Configuration>(_configFilename);

            _params = config.Search;

            // Setup the deck pool and grab our deck and class
            var deckPoolManager = new DeckPoolManager();

            deckPoolManager.AddDeckPools(config.Evaluation.DeckPools);
            string poolName = config.Player.DeckPool;
            string deckName = config.Player.DeckName;

            Console.WriteLine(string.Format("names {0} {1}", poolName, deckName));
            _playerDeck = deckPoolManager.GetDeck(poolName, deckName);


            // Setup the logs to record the data on individuals
            InitLogs();
        }
Exemple #2
0
        public DistributedSearch(string configFilename)
        {
            // Grab the config info
            _configFilename = configFilename;
            var config = Toml.ReadFile <Configuration>(_configFilename);

            // Figure out the number of search parameters based on whether
            // we are searching a linear combination or neural network weights
            int numParams = -1;

            if (config.Evaluation.PlayerStrategies[0].Strategy == "Custom")
            {
                numParams = 15;
            }
            else if (config.Evaluation.PlayerStrategies[0].Strategy == "NeuralNet")
            {
                // Peak in to the neural network configuration and compute
                // the number of parameters we need to compute weights for.
                int[] layerSizes = config.Network.LayerSizes;
                numParams = 0;
                for (int i = 0; i < layerSizes.Length - 1; i++)
                {
                    numParams += layerSizes[i] * layerSizes[i + 1]; // edge weights
                    numParams += layerSizes[i + 1];                 // bias weights
                }
            }
            Console.WriteLine(string.Format("Search for {0} parameters...", numParams));

            // Setup the search algorithm to use to optimize the strategy.
            if (config.Search.Type.Equals("EvolutionStrategy"))
            {
                var searchConfig =
                    Toml.ReadFile <EvolutionStrategyParams>(config.Search.ConfigFilename);
                _searchAlgo = new EvolutionStrategyAlgorithm(searchConfig, numParams);
            }
            else if (config.Search.Type.Equals("CMA-ES"))
            {
                var searchConfig =
                    Toml.ReadFile <CMA_ES_Params>(config.Search.ConfigFilename);
                _searchAlgo = new CMA_ES_Algorithm(searchConfig, numParams);
            }
            else if (config.Search.Type.Equals("CMA-ME"))
            {
                var searchConfig =
                    Toml.ReadFile <CMA_ME_Params>(config.Search.ConfigFilename);
                _searchAlgo = new CMA_ME_Algorithm(searchConfig, numParams);
            }
            else if (config.Search.Type.Equals("MAP-Elites"))
            {
                var searchConfig =
                    Toml.ReadFile <MapElitesParams>(config.Search.ConfigFilename);
                _searchAlgo = new MapElitesAlgorithm(searchConfig, numParams);
            }
            else if (config.Search.Type.Equals("MAP-Elites-Line"))
            {
                var searchConfig =
                    Toml.ReadFile <MapElitesParams>(config.Search.ConfigFilename);
                _searchAlgo = new MapElitesLineAlgorithm(searchConfig, numParams);
            }
            else
            {
                Console.WriteLine(string.Format("Strategy {} not supported.",
                                                config.Search.Type));
            }

            // Setup the deck pool and grab our deck and class
            var deckPoolManager = new DeckPoolManager();

            deckPoolManager.AddDeckPools(config.Evaluation.DeckPools);
            string poolName = config.Player.DeckPool;
            string deckName = config.Player.DeckName;

            Console.WriteLine(string.Format("names {0} {1}", poolName, deckName));
            _playerDeck = deckPoolManager.GetDeck(poolName, deckName);

            // Setup the logs to record the data on individuals
            InitLogs();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            string nodeName = args[0];
            int    nodeId   = Int32.Parse(args[0]);

            Console.WriteLine("Node Id: " + nodeId);

            // These files are for asyncronous communication between this
            // worker and it's scheduler.
            //
            // Decks to evaluate come in the inbox and are dished out of the
            // outbox.
            string boxesDirectory = "boxes/";
            string inboxPath      = boxesDirectory +
                                    string.Format("deck-{0,4:D4}-inbox.tml", nodeId);
            string outboxPath = boxesDirectory +
                                string.Format("deck-{0,4:D4}-outbox.tml", nodeId);

            // Hailing
            string activeDirectory  = "active/";
            string activeWorkerPath = activeDirectory +
                                      string.Format("worker-{0,4:D4}.txt", nodeId);
            string activeSearchPath = activeDirectory + "search.txt";

            if (!File.Exists(activeSearchPath))
            {
                Console.WriteLine("No search has been found.");
                return;
            }

            // The opponent deck doesn't change so we can load it here.
            string[] textLines = File.ReadAllLines(activeSearchPath);
            Console.WriteLine("Config File: " + textLines[1]);
            var config = Toml.ReadFile <Configuration>(textLines[1]);

            // Apply nerfs if nerfs are available
            ApplyNerfs(config.Nerfs);

            // Setup the pools of card decks for possible opponents.
            var deckPoolManager = new DeckPoolManager();

            deckPoolManager.AddDeckPools(config.Evaluation.DeckPools);

            // Setup test suites: (strategy, deck) combos to play against.
            var suiteConfig = Toml.ReadFile <DeckSuite>(
                config.Evaluation.OpponentDeckSuite);
            var gameSuite = new GameSuite(suiteConfig.Opponents,
                                          deckPoolManager);

            // Let the scheduler know we are here.
            using (FileStream ow = File.Open(activeWorkerPath,
                                             FileMode.Create, FileAccess.Write, FileShare.None))
            {
                WriteText(ow, "Hail!");
                ow.Close();
            }

            // Loop while the guiding search is running.
            while (File.Exists(activeSearchPath))
            {
                // Wait until we have some work.
                while (!File.Exists(inboxPath) && File.Exists(activeSearchPath))
                {
                    Console.WriteLine("Waiting... (" + nodeId + ")");
                    Thread.Sleep(5000);
                }

                if (!File.Exists(activeSearchPath))
                {
                    break;
                }

                // Wait for the file to be finish being written
                Thread.Sleep(5000);

                // Run games, evaluate the deck, and then save the results.
                var  playMessage = Toml.ReadFile <PlayMatchesMessage>(inboxPath);
                Deck playerDeck  = playMessage.Deck.ContructDeck();

                int numStrats    = config.Evaluation.PlayerStrategies.Length;
                var stratStats   = new StrategyStatistics[numStrats];
                var overallStats = new OverallStatistics();
                overallStats.UsageCounts = new int[playerDeck.CardList.Count];
                RecordDeckProperties(playerDeck, overallStats);
                for (int i = 0; i < numStrats; i++)
                {
                    // Setup the player with the current strategy
                    PlayerStrategyParams curStrat =
                        config.Evaluation.PlayerStrategies[i];
                    var player = new PlayerSetup(playerDeck,
                                                 PlayerSetup.GetStrategy(curStrat.Strategy,
                                                                         config.Network,
                                                                         playMessage.Strategy));

                    List <PlayerSetup> opponents =
                        gameSuite.GetOpponents(curStrat.NumGames);

                    var launcher = new GameDispatcher(
                        player, opponents
                        );

                    // Run the game and collect statistics
                    OverallStatistics stats = launcher.Run();
                    stratStats[i]            = new StrategyStatistics();
                    stratStats[i].WinCount  += stats.WinCount;
                    stratStats[i].Alignment += stats.StrategyAlignment;
                    overallStats.Accumulate(stats);
                }

                // Write the results
                overallStats.ScaleByNumStrategies(numStrats);
                var results = new ResultsMessage();
                results.PlayerDeck    = playMessage.Deck;
                results.OverallStats  = overallStats;
                results.StrategyStats = stratStats;
                Toml.WriteFile <ResultsMessage>(results, outboxPath);

                // Wait for the TOML file to write (buffers are out of sync)
                // Then tell the search that we are done writing the file.
                Thread.Sleep(3000);
                File.Delete(inboxPath);

                // Cleanup.
                GC.Collect();

                // Look at all the files in the current directory.
                // Eliminate anythings that matches our log file.

                /*
                 * string[] oFiles = Directory.GetFiles(".", "DeckEvaluator.o*");
                 * foreach (string curFile in oFiles)
                 * {
                 * if (curFile.EndsWith(nodeName))
                 * {
                 *    File.Delete(curFile);
                 * }
                 * }*/
            }
        }