Beispiel #1
0
        private void onStartMessage(Message message)
        {
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                StartMessage smessage            = (StartMessage)message;
                AgentManagementSystem.finishFlag = false;
                Trace.TraceInformation("Started. System Info: Agents: {0}; Containers: {1}; Workers: {2}", smessage.totalAgents, smessage.totalContainers, smessage.totalWorkers);
                AgentManagementSystem.totalAgents     = smessage.totalAgents;
                AgentManagementSystem.totalContainers = smessage.totalContainers;
                AgentManagementSystem.totalWorkers    = smessage.totalWorkers;
                AgentManagementSystem.RunAgents();
                if (!AgentManagementSystem.finishFlag) // Process was stopped manually. No need for result sending
                {
                    Trace.TraceInformation("Results:\nSusceptible: {0}\nRecovered: {3}\nInfectious: {5}\nFuneral: {1}\nDead: {2}\nTime: {4}", AgentManagementSystem.susceptibleAgentsCount, AgentManagementSystem.funeralAgentsCount,
                                           AgentManagementSystem.deadAgentsCount, AgentManagementSystem.recoveredAgentsCount, GlobalTime.Time, AgentManagementSystem.infectiousAgentsCount);

                    ResultsMessage msg = new ResultsMessage(
                        MessageTransportSystem.Instance.Id,
                        AgentManagementSystem.susceptibleAgentsCount,
                        AgentManagementSystem.recoveredAgentsCount,
                        AgentManagementSystem.infectiousAgentsCount,
                        AgentManagementSystem.funeralAgentsCount,
                        AgentManagementSystem.deadAgentsCount,
                        AgentManagementSystem.exposedAgentsCount,
                        GlobalTime.Time
                        );
                    MessageTransportSystem.Instance.SendMessage(msg);
                }
                else
                {
                    Trace.TraceInformation("Process was stopped manually. No need for result sending");
                }

                AgentManagementSystem.finishFlag = false;
                isFinished = true;
            });
        }
Beispiel #2
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);
                 * }
                 * }*/
            }
        }