Example #1
0
    public void PlayRandomGreedy(Game game)
    {
        RandomAgent player1 = new RandomAgent(1);
        GreedyAgent player2 = new GreedyAgent(2);

        PlayGame(game, player1, player2);
    }
Example #2
0
        private void RandomMoveButton_Click(object sender, EventArgs e)
        {
            IOthelloAgent myAgent = new RandomAgent();

            _myGame.MakeMove(myAgent.MakeMove(_myGame, _myGame.WhosTurn));
            RefreshControls();
        }
Example #3
0
        /// <summary>
        /// Run GA against itself till evolution <paramref name="recursionDepth"/> times
        /// </summary>
        /// <param name="recursionDepth"></param>
        public static void RunRecursiveGeneticAlgorithm(int recursionDepth = 3)
        {
            //double[] priorGenes = LoadChromosome(@"E:\Source\SamOthellop\SamOthellop\Model\Agents\Genetic\bestchromosome5-10.dat");

            FloatingPointChromosome chromosome = new FloatingPointChromosome(
                new double[] { -100, -100, -100, -100, -100, -100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                new double[] { 100, 100, 100, 100, 100, 100, 60, 60, 60, 60, 60, 60, 3, 3, 3, 3, 3, 3 },
                new int[] { 64, 64, 64, 64, 64, 64, 8, 8, 8, 8, 8, 8, 64, 64, 64, 64, 64, 64 },
                new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3 }
                );

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < recursionDepth; i++)
            {
                if (i == 0)
                {
                    IOthelloAgent randAgent = new RandomAgent();
                    EvolveGeneticAlgorithm(chromosome, randAgent, "test" + i.ToString());
                }
                else
                {
                    double[]      genes     = LoadChromosome();
                    IOthelloAgent heurAgent = new HeuristicAgent(LoadChromosome(@"E:\Source\SamOthellop\SamOthellop\Model\Agents\Genetic\test" + (i - 1).ToString()));
                    EvolveGeneticAlgorithm(chromosome, heurAgent, "test" + i.ToString());
                }
            }
            stopwatch.Stop();
            Console.WriteLine("Sucessfully recursively geneticially trained.  Training cost {0} time with {1} recursions", stopwatch.Elapsed, recursionDepth);
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("Setup gameConfig");

            //todo: rename to Main
            GameConfig gameConfig = new GameConfig
            {
                StartPlayer      = 1,
                Player1HeroClass = CardClass.MAGE,
                Player2HeroClass = CardClass.MAGE,
                FillDecks        = true,
                Logging          = false
            };

            Console.WriteLine("Setup POGameHandler");
            AbstractAgent player1     = new MyAgent();
            AbstractAgent player2     = new RandomAgent();
            var           gameHandler = new POGameHandler(gameConfig, player1, player2, debug: true);

            Console.WriteLine("PlayGame");
            //gameHandler.PlayGame();
            gameHandler.PlayGames(10);
            GameStats gameStats = gameHandler.getGameStats();

            gameStats.printResults();


            Console.WriteLine("Test successful");
            Console.ReadLine();
        }
Example #5
0
        public static void TestMinMax(OthelloGame _myGame, int minimaxDepth = 3)
        {
            const int testCount = 100;

            object wonGamesLock = new object();
            int    wonGames     = 0;
            object tieGamesLock = new object();
            int    tieGames     = 0;

            var stopwatch = Stopwatch.StartNew();

            //Parallel.For(0, testCount, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },index =>
            //{
            for (int index = 0; index < testCount; index++)
            {//non-parallel for loop to debug
                BoardStates player = (index % 2 == 0) ? BoardStates.black : BoardStates.white;

                OthelloGame testGame     = new OthelloGame();
                MinMaxAgent othelloAgent = new MinMaxAgent(2);
                RandomAgent randAgent    = new RandomAgent();
                while (!testGame.GameComplete)
                {
                    if (testGame.WhosTurn == player)
                    {
                        testGame.MakeMove(othelloAgent.MakeMove(testGame, player));
                    }
                    else
                    {
                        testGame.MakeMove(randAgent.MakeMove(testGame, ~player));
                    }
                }
                if (testGame.GameComplete)//just gotta check
                {
                    if (testGame.FinalWinner == player)
                    {
                        lock (wonGamesLock) { wonGames++; }
                    }
                    else if (testGame.FinalWinner == BoardStates.empty)
                    {
                        lock (tieGamesLock) { tieGames++; }
                    }
                    Console.WriteLine("Finished Game " + index + ", " + testGame.FinalWinner.ToString()
                                      + " won " + testGame.GetPieceCount(testGame.FinalWinner) + " to "
                                      + testGame.GetPieceCount(OthelloGame.OpposingPlayer(testGame.FinalWinner)));;
                }
                else
                {
                    throw new Exception("MiniMax Testing didn't complete a game");
                }
            }

            //});
            stopwatch.Stop();
            Console.WriteLine("Won " + wonGames + " / " + testCount + " games, " + ((double)wonGames / testCount) * 100 + " %");
            Console.WriteLine("Tied " + tieGames + " / " + testCount + " games, " + ((double)tieGames / testCount) * 100 + " %");
            Console.WriteLine("Lost " + (testCount - wonGames - tieGames) + " / " + testCount + " games, " + ((double)(testCount - wonGames - tieGames) / testCount) * 100 + " %");
            Console.WriteLine("Elapsed time for games : {0}", stopwatch.Elapsed);
        }
Example #6
0
        static void Main(string[] args)
        {
            var agent = new RandomAgent();

            var e = new GridWorldEnvironment(2, 2);

            e.SetAgent(agent);
            e.RunTrajectory();
        }
Example #7
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Setup gameConfig");

            //todo: rename to Main
            GameConfig gameConfig = new GameConfig
            {
                StartPlayer = 1,

                Logging = true
            };

            gameConfig.Player1Name = "Sky";
            gameConfig.Player2Name = "Net";
            gameConfig.Player1Deck = Decks.RenoKazakusMage;                                                     //My Deck

            Dictionary <string, List <Card> > decksAvailable = new Dictionary <string, List <Card> >();         //set opponents decks.

            decksAvailable.Add("AggroPirateWarrior", Decks.AggroPirateWarrior);                                 //AggroPirateWarrior
            decksAvailable.Add("MidrangeJadeShaman", Decks.MidrangeJadeShaman);                                 //MidrangeJadeShaman
            decksAvailable.Add("RenoKazakusMage", Decks.RenoKazakusMage);                                       //RenoKazakusMage

            Console.WriteLine("Setup POGameHandler");
            AbstractAgent player1 = new src.Agent.MyAgent();
            AbstractAgent player2;

            Console.WriteLine("Start Games ");

            Console.WriteLine("=== MyAgent vs Random Agent=== ");
            player2 = new RandomAgent();                                                                                                                        //play all games against the Random Agent
            Helper.SimulateAllGames(gameConfig, player1, player2, decksAvailable);


            Console.WriteLine("=== MyAgent vs RandomLateEnd Agent ===");

            /* i achieve around  >80 % on one occasions against AggroPirate
             * with this agent and >90% in others, but it was not announced,
             * until 25 june that i had to compete against this as well,
             * so i didn' try to make something better and sophisticated, cause
             * no time.
             */

            //play all games against the RandomLateEnd Agent,
            player2 = new RandomAgentLateEnd();
            Helper.SimulateAllGames(gameConfig, player1, player2, decksAvailable);

            Console.WriteLine("=== My Agent vs FaceHunter Agent ===");                                       //play all games against the RandomLateEnd Agent
            player2 = new FaceHunter();
            Helper.SimulateAllGames(gameConfig, player1, player2, decksAvailable);

            Console.WriteLine("Test Ended");
            Console.ReadLine();
        }
        public void ShouldPlayRandomCardFromHand()
        {
            var state = TestUtilities.GenerateTestState(2);

            state.CurrentPlayerHand.Cards.Clear();
            state.CurrentPlayerHand.Add(CardType.Blue);
            var player = new RandomAgent();

            var play = player.FormulatePlay(state);

            Assert.AreEqual(CardType.Blue, play.Card);
            Assert.AreEqual(0, play.Position);
        }
Example #9
0
    // Initialisation des agents, du runner et du GameState
    public void InitializeGame(int agent1, int agent2)
    {
        switch (agent1)
        {
        case 0:
            PlayerOne.GetComponent <HumanPlayerScript>().ControlType = 0;
            agentP1 = new HumanPlayerAgent(PlayerOne.GetComponent <HumanPlayerScript>());
            break;

        case 1:
            PlayerOne.GetComponent <HumanPlayerScript>().ControlType = 1;
            agentP1 = new HumanPlayerAgent(PlayerOne.GetComponent <HumanPlayerScript>());
            break;

        case 2:
            agentP1 = new RandomAgent();
            break;

        case 3:
            agentP1 = new RandomRolloutAgent(RandomRNbIteration, x, z);
            break;
        }
        switch (agent2)
        {
        case 0:
            PlayerTwo.GetComponent <HumanPlayerScript>().ControlType = 0;
            agentP2 = new HumanPlayerAgent(PlayerTwo.GetComponent <HumanPlayerScript>());
            break;

        case 1:
            PlayerTwo.GetComponent <HumanPlayerScript>().ControlType = 1;
            agentP2 = new HumanPlayerAgent(PlayerTwo.GetComponent <HumanPlayerScript>());
            break;

        case 2:
            agentP2 = new RandomAgent();
            break;

        case 3:
            agentP2 = new RandomRolloutAgent(RandomRNbIteration, x, z);
            break;
        }
        gs     = new PacManGameState(x, z, PlayerOne.transform.position, PlayerTwo.transform.position, Obstacles, Doors);
        runner = new PacManRunner(agentP1, agentP2, gs, speed);
        InGame = true;
        GumBall.transform.position = gs.GetGumVector();
    }
Example #10
0
 private void RandomGameButton_Click(object sender, EventArgs e)
 {
     EnableControlButtons(false);
     new Thread(() =>
     {
         IOthelloAgent myAgent = new RandomAgent();
         while (!_myGame.GameComplete)
         {
             _myGame.MakeMove(myAgent.MakeMove(_myGame, _myGame.WhosTurn));
         }
         Invoke(new Action(() =>
         {
             RefreshControls();
             EnableControlButtons();
         }));
     }).Start();
 }
Example #11
0
        public void RandomAgentTests_Run_Children()
        {
            AIAgent randAgent = new RandomAgent();

            randAgent.run(new TestAIState("children"));
            bool aiDone = false;

            while (!aiDone)
            {
                if (randAgent.done)
                {
                    aiDone = true;
                }
            }
            AIState next = randAgent.next;

            Assert.True(next != null);
        }
Example #12
0
        public void RandomAgentTests_Run_NullInitalState()
        {
            AIAgent randAgent = new RandomAgent();

            randAgent.run(null);
            bool aiDone = false;

            while (!aiDone)
            {
                if (randAgent.done)
                {
                    aiDone = true;
                }
            }
            AIState next = randAgent.next;

            Assert.True(next == null);
        }
Example #13
0
        private void RunGamesButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GameCountTextBox.Text.Length; i++)
            {
                if (!char.IsNumber((char)(GameCountTextBox.Text[i])))
                {
                    GameCountTextBox.BackColor = Color.Red;
                    _twoSecondTimer.Elapsed   += new System.Timers.ElapsedEventHandler(GameCountTextBox_ColorControl);
                    _twoSecondTimer.Enabled    = true;
                    return;
                }
            }

            int gameCount = Convert.ToInt32(GameCountTextBox.Text);

            EnableControlButtons(false);

            new Thread(() =>
            {
                IOthelloAgent myAgent = new RandomAgent();
                for (int i = 0; i < gameCount; i++)
                {
                    while (!_myGame.GameComplete)
                    {
                        _myGame.MakeMove(myAgent.MakeMove(_myGame, _myGame.WhosTurn));
                    }
                    Invoke(new Action(() =>
                    {
                        RefreshControls();
                    }));
                    _myGame.ResetBoard();
                    Thread.Sleep(5);
                }
                Invoke(new Action(() =>
                {
                    RefreshControls();
                    EnableControlButtons();
                }));
            }).Start();
        }
Example #14
0
        public static void Main(string[] args)
        {
            Environment map = new Environment(3);

            RandomAgent agent1 = new RandomAgent(map, 1, 1);

            agent1.Think(500);

            Console.WriteLine(agent1.ToString());

            /*ReflexAgent agent1 = new ReflexAgent(map, 1, 1);
             * agent1.Think(100);
             *
             * Console.WriteLine(agent1.ToString());
             *
             * /* ReflexModelAgent agent1 = new ReflexModelAgent(map, 1, 1);
             * agent1.Think(2000);
             *
             * Console.WriteLine(agent1.ToString());*/

            Console.Read();
        }
Example #15
0
    public static AAgent CreateAgent(AgentType a, Game g, PlayerTag t)
    {
        AAgent agent;

        if (a == AgentType.Player)
        {
            agent = new PlayerAgent();
        }
        else if (a == AgentType.Random)
        {
            agent = new RandomAgent();
        }
        else
        {
            agent = new MCTSAgent();
        }

        agent.SetGame(g);
        agent.SetPlayerTag(t);

        return(agent);
    }
Example #16
0
            public static void MoltenTest()
            {
                var rnd    = new Random();
                var p1Deck = new Deck(CardClass.WARRIOR, Archetype.QUEST, "IceFireWarrior", 100,
                                      new List <Card>
                {
                    Cards.FromName("Fire Plume's Heart"),
                    Cards.FromName("Goldshire Footman"),
                    Cards.FromName("Wax Elemental"),
                    Cards.FromName("Wax Elemental"),
                    Cards.FromName("Cleave"),
                    Cards.FromName("Cornered Sentry"),
                    Cards.FromName("Drywhisker Armorer"),
                    Cards.FromName("Drywhisker Armorer"),
                    Cards.FromName("Execute"),
                    Cards.FromName("Execute"),
                    Cards.FromName("Plated Beetle"),
                    Cards.FromName("Plated Beetle"),
                    Cards.FromName("Warpath"),
                    Cards.FromName("Warpath"),
                    Cards.FromName("Phantom Militia"),
                    Cards.FromName("Phantom Militia"),
                    Cards.FromName("Shield Block"),
                    Cards.FromName("Shield Block"),
                    Cards.FromName("Stonehill Defender"),
                    Cards.FromName("Stonehill Defender"),
                    Cards.FromName("Brawl"),
                    Cards.FromName("Direhorn Hatchling"),
                    Cards.FromName("Direhorn Hatchling"),
                    Cards.FromName("Rotten Applebaum"),
                    Cards.FromName("Ornery Direhorn"),
                    Cards.FromName("Unidentified Shield"),
                    Cards.FromName("Unidentified Shield"),
                    Cards.FromName("Geosculptor Yip"),
                    Cards.FromName("Scourgelord Garrosh"),
                    Cards.FromName("Molten Blade")
                });

                var p2Deck = new Deck();

                var gameConfig = new GameConfig
                {
                    StartPlayer      = rnd.Next(1, 2),
                    Player1HeroClass = CardClass.WARRIOR,
                    Player1Name      = "Garrosh Hellscream",
                    Player1Deck      = p1Deck,
                    Player2HeroClass = CardClass.DRUID,
                    Player2Name      = "Malfurion Stormrage",
                    Player2Deck      = p2Deck.FromDB(CardClass.DRUID, deckName: "Malygos_Druid1"),
                    Shuffle          = false,
                    Logging          = true,
                    History          = true,
                    //SkipMulligan = false
                };

                var testGame = new Game(gameConfig);

                var           player1 = new MCTSAgent();
                AbstractAgent player2 = new RandomAgent();

                player1.InitializeAgent();
                player2.InitializeAgent();

                player1.InitializeGame();
                player2.InitializeGame();

                testGame.StartGame();

                //bool check = true;
                //var molten = (IPlayable)testGame.CurrentPlayer.HandZone.Find(c => c.Type == CardType.WEAPON);
                //if (molten?.Cost != molten?.Card.Cost)
                //	check = false;
                var root = new HearthNode(null, testGame, null);

                //molten = (IPlayable)root.Game.CurrentPlayer.HandZone.Find(c => c.Type == CardType.WEAPON);
                //if (molten.Cost != molten.Card.Cost)
                //	check = false;

                HearthNode state = root.Frontier.Find(p => p.IsEndTurn);

                for (int i = 0; i < 5; ++i)
                {
                    state = state.Frontier.Find(p => p.IsEndTurn);
                }
            }
Example #17
0
        /// <summary>
        /// Returns a instance of a Hearthstone agent based on the profited card class and agent type.
        /// The card class of the hero is key for configuring the agent correctly, especially the predator MCTS.
        /// </summary>
        /// <param name="cardClass">the card class of the agent's hero</param>
        /// <param name="type">the type of agent</param>
        /// <returns></returns>
        public AbstractAgent GetAgent(CardClass cardClass, AgentType type)
        {
            double simulationTime = 25000;
            IScore scoring        = new WeightedScore();

            AbstractAgent agent = new RandomAgent();

            switch (type)
            {
            case AgentType.PredatorMCTS:
                switch (cardClass)
                {
                // the default decks
                case CardClass.WARRIOR:
                    Console.WriteLine("Aggro Deck");
                    agent = new PredatorMctsAgent(scoring,
                                                  new MctsParameters
                    {
                        SimulationTime  = simulationTime,
                        AggregationTime = 100,
                        RolloutDepth    = 5,
                        UCTConstant     = 9000
                    },
                                                  new PredictionParameters
                    {
                        File            = Environment.CurrentDirectory + @"\src\Bigramms\bigramm_1-2017-12-2016.json.gz",
                        CardCount       = 10,
                        StepWidth       = 2,
                        DeckCount       = 1,
                        SetCount        = 3,
                        LeafCount       = 5,
                        SimulationDepth = 1,
                    });
                    break;

                case CardClass.SHAMAN:
                    agent = new PredatorMctsAgent(scoring,
                                                  new MctsParameters
                    {
                        SimulationTime  = simulationTime,
                        AggregationTime = 100,
                        RolloutDepth    = 5,
                        UCTConstant     = 9000
                    },
                                                  new PredictionParameters
                    {
                        File            = Environment.CurrentDirectory + @"\src\Bigramms\bigramm_1-2017-12-2016.json.gz",
                        CardCount       = 10,
                        StepWidth       = 2,
                        DeckCount       = 1,
                        SetCount        = 3,
                        LeafCount       = 5,
                        SimulationDepth = 3,
                    });
                    break;

                case CardClass.MAGE:
                    agent = new PredatorMctsAgent(scoring,
                                                  new MctsParameters
                    {
                        SimulationTime  = simulationTime,
                        AggregationTime = 100,
                        RolloutDepth    = 5,
                        UCTConstant     = 9000
                    },
                                                  new PredictionParameters
                    {
                        File            = Environment.CurrentDirectory + @"\src\Bigramms\bigramm_1-2017-12-2016.json.gz",
                        CardCount       = 10,
                        StepWidth       = 2,
                        DeckCount       = 1,
                        SetCount        = 3,
                        LeafCount       = 5,
                        SimulationDepth = 5,
                    });
                    break;

                case CardClass.WARLOCK:
                    agent = new PredatorMctsAgent(scoring,
                                                  new MctsParameters
                    {
                        SimulationTime  = simulationTime,
                        AggregationTime = 100,
                        RolloutDepth    = 5,
                        UCTConstant     = 9000
                    },
                                                  new PredictionParameters
                    {
                        File            = Environment.CurrentDirectory + @"\src\Bigramms\bigramm_3-2018-10-2017.json.gz",
                        CardCount       = 10,
                        StepWidth       = 2,
                        DeckCount       = 1,
                        SetCount        = 3,
                        LeafCount       = 5,
                        SimulationDepth = 1,
                    });
                    break;
                }
                break;
            }
            ;
            return(agent);
        }
Example #18
0
    void Start()
    {
        //Redirect the console output (for debugging purposes).
        UnitySystemConsoleRedirector.Redirect();
        //Make a new agent
        AIAgent agent;
        //And model
        Model model = new Model(GameData.modelFiles [gameIndx]);
        //Set the settings
        string settings = GameData.settingsFiles [gameIndx];

        //And init the right agent with the model and settings above.
        if (GameData.selectedAgent == 0)
        {
            agent = new RandomAgent();
        }
        else if (GameData.selectedAgent == 1)
        {
            agent = new ModelBasedAgent(model);
        }
        else if (GameData.selectedAgent == 2)
        {
            agent = new MCTSSimpleAgent(settings);
        }
        else if (GameData.selectedAgent == 3)
        {
            agent = new MCTSWithPruning(model, settings);
        }
        else if (GameData.selectedAgent == 4)
        {
            agent = new MCTSWithSoftPruning(model, settings);
        }
        else if (GameData.selectedAgent == 5)
        {
            agent = new MCTSWithLearning(model, settings);
        }
        else
        {
            agent = new MCTSSimpleAgent(settings);
        }

        //Init the correct game
        if (gameIndx == 0)
        {
            currentGame = gameObject.AddComponent <TicTacToe> ();
        }
        else if (gameIndx == 1)
        {
            currentGame = gameObject.AddComponent <OrderAndChaos> ();
        }
        else if (gameIndx == 2)
        {
            currentGame = gameObject.AddComponent <Hex> ();
        }

        //Once the game has been made set the rest of the game up
        currentGame.ai         = agent;
        currentGame.playerIndx = GameData.playerIndex;;
        currentGame.preFabTile = preFabTile;
        currentGame.AIThinking = AIThinking;
        currentGame.EndGame    = EndGame;
        currentGame.winlose    = winlose;
    }
Example #19
0
        private static void runSimulation(Dictionary <string, CardClass> classMap, Dictionary <string, List <Card> > deckMap, List <string> deckList)
        {
            int    numGames = 3000;
            Random r        = new Random();

            Console.WriteLine("Simulate Games");

            List <List <double> > games = new List <List <double> >();

            for (int i = 0; i < numGames; i++)
            {
                double        max         = 10;
                double        min         = -10;
                List <double> randWeights =
                    Enumerable.Range(0, 24)                          // create sequence of 100 elements
                    .Select(_ => r.NextDouble() * (max - min) + min) // for each element select random value
                    .ToList();                                       // convert to array.

                string p1Deck = deckList[r.Next(deckList.Count)];
                string p2Deck = deckList[r.Next(deckList.Count)];

                var gameConfig = new GameConfig()
                {
                    StartPlayer      = r.Next(2) + 1,
                    Player1HeroClass = classMap.GetValueOrDefault(p1Deck, CardClass.MAGE),
                    Player2HeroClass = classMap.GetValueOrDefault(p2Deck, CardClass.ROGUE),
                    Player1Deck      = deckMap.GetValueOrDefault(p1Deck, Decks.RenoKazakusMage),
                    Player2Deck      = deckMap.GetValueOrDefault(p2Deck, Decks.MiraclePirateRogue),
                    FillDecks        = true,
                    Shuffle          = true,
                    Logging          = false
                };

                AbstractAgent player1 = new RandomAgent();
                AbstractAgent player2 = new NNAgent(randWeights);
                Console.WriteLine("Game " + i + " " + p1Deck + " " + p2Deck);
                var gameHandler = new POGameHandler(gameConfig, player1, player2, repeatDraws: false);
                gameHandler.PlayGames(nr_of_games: 1, addResultToGameStats: true, debug: false);
                GameStats gameStats = gameHandler.getGameStats();
                // gameStats.printResults();
                randWeights.Add(gameStats.PlayerB_Wins);
                games.Add(randWeights);
                // Console.WriteLine(String.Join(",", randWeights.Select(p=>p.ToString()).ToArray()));
            }

            StringBuilder sb = new StringBuilder();

            if (!File.Exists(@"hsdata.csv"))
            {
                sb.Append("HERO_HEALTH_REDUCED,HERO_ATTACK_REDUCED,WEAPON_DURABILITY_REDUCED,MINION_HEALTH_REDUCED,MINION_ATTACK_REDUCED,MINION_KILLED,MINION_APPEARED,SECRET_REMOVED,MANA_REDUCED,M_HEALTH,M_ATTACK,M_HAS_CHARGE,M_HAS_DEAHTRATTLE,M_HAS_DIVINE_SHIELD,M_HAS_LIFE_STEAL,M_HAS_STEALTH,M_HAS_TAUNT,M_HAS_WINDFURY,M_IS_RUSH,M_MANA_COST,M_POISONOUS,M_SILENCED,M_SUMMONED,M_CANT_BE_TARGETED_BY_SPELLS,RESULT\r\n");
            }

            for (int i = 0; i < games.Count(); i++)
            {
                for (int j = 0; j < games[0].Count(); j++)
                {
                    sb.Append((j == 0 ? "" : ",") + games[i][j]);
                }
                sb.AppendLine();
            }

            File.AppendAllText(@"hsdata.csv", sb.ToString());
            Console.WriteLine("Simulation successful");
            Console.ReadLine();
        }
Example #20
0
        public void StartGame(
            Virus virus,
            PerformedMoveCallback callback,
            UpdatePiecesCallback piecesCallback,
            EndCallback end,
            string id,
            params VirusPlayer[] players)
        {
            Random rand = new Random();

            PerformedMove    = callback;
            UpdatePieces     = piecesCallback;
            End              = end;
            PlayerID         = id;
            this.virus       = virus;
            this.immediateAI = true;
            this.MouseClick += MouseClickHandler1;
            tileSize         = 49;
            this.Size        = new Size(
                virus.Size * tileSize + 17,
                virus.Size * tileSize + 55);
            int smallestSide = this.Size.Height < this.Size.Width ? this.Size.Height : this.Size.Width;

            tileSize = smallestSide / virus.Size;
            this.players.Add(new VirusPlayer("Player 0", "", Color.White));
            this.players.AddRange(players);
            while (this.players.Count < virus.Players + 1)
            {
                this.players.Add(new VirusPlayer("BruteAI", "AI", Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))));
            }
            //Save("Lalalafil");
            agents = new Agent[this.players.Count];
            int n = 1;

            for (byte i = 1; i < this.players.Count; i++)
            {
                String p = this.players[i].Name;
                switch (p)
                {
                case "AIQ":
                    agents[i] = new QAgent(i);
                    if (File.Exists("TrainingData.Q") && File.Exists("TrainingData.N"))
                    {
                        ((QAgent)agents[i]).Load("TrainingData");
                        ((QAgent)agents[i]).TurnOffExploration();
                        ((QAgent)agents[i]).TurnOffLearning();
                    }
                    //this.players[i].Name = "AI " + n;
                    n++;
                    break;

                case "AIMQ":
                    agents[i] = new MemoryQAgent(i);
                    if (File.Exists("TrainingData.Q") && File.Exists("TrainingData.N"))
                    {
                        ((MemoryQAgent)agents[i]).Load("TrainingData");
                        ((MemoryQAgent)agents[i]).TurnOffExploration();
                    }
                    //this.players[i].Name = "AI " + n;
                    n++;
                    break;

                case "AIMinimax":
                    agents[i] = new MinimaxAgent(4, i);
                    //this.players[i].Name = "AI " + n;
                    n++;
                    break;

                case "AIMiniMaxMix":
                    if (File.Exists("TrainingData.Q"))
                    {
                        agents[i] = new MiniMaxMixAgent("TrainingData", 2, i);
                    }
                    else
                    {
                        agents[i] = new BruteForceAgent(i);
                    }
                    //this.players[i].Name = "AI " + n;
                    n++;
                    break;

                case "AIMixed":
                    agents[i] = new MixedAgent(0.5, false, i);
                    //this.players[i].Name = "AI " + n;
                    n++;
                    break;

                case "AIBrute":
                    agents[i] = new BruteForceAgent(i);
                    //this.players[i].Name = "AI " + n;
                    n++;
                    break;

                case "AIRandom":
                    agents[i] = new RandomAgent(i);
                    //this.players[i].Name = "AI " + n;
                    n++;
                    break;

                case "AISimple":
                    agents[i] = new SimpleAgent(i);
                    //this.players[i].Name = "AI " + n;
                    n++;
                    break;
                }
            }

            message = this.players[1].Name + "'s turn";

            /*colors = new Color[virus.Players + 1];
             * colors[0] = Color.White;
             * colors[1] = Color.FromArgb(128, 160, 255);
             * colors[2] = Color.FromArgb(96, 255, 96);
             * if(virus.Players > 2)
             *      colors[3] = Color.FromArgb(255, 96, 96);
             * if(virus.Players > 3)
             *      colors[4] = Color.FromArgb(255, 255, 64);
             *
             * for (int i = 5; i <= virus.Players; i++)
             *      colors[i] = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));*/
        }
Example #21
0
        public VirusInterface(Virus virus, int tilesize = 20, bool immediateAI = false, params String[] names)
        {
            InitializeComponent();
            this.virus       = virus;
            this.tileSize    = tilesize;
            this.immediateAI = immediateAI;
            this.MouseClick += MouseClickHandler1;
            this.Size        = new Size(
                virus.Size * tileSize + 17,
                virus.Size * tileSize + 55);
            this.names.Add("Player 0");
            this.names.AddRange(names);
            while (this.names.Count < virus.Players + 1)
            {
                this.names.Add("Player " + this.names.Count);
            }
            //Save("Lalalafil");
            agents = new Agent[this.names.Count];
            int n = 1;

            for (byte i = 1; i < this.names.Count; i++)
            {
                String p = this.names[i];
                switch (p)
                {
                case "QAI":
                    agents[i] = new QAgent(i);
                    if (File.Exists("TrainingData.Q") && File.Exists("TrainingData.N"))
                    {
                        ((QAgent)agents[i]).Load("TrainingData");
                        ((QAgent)agents[i]).TurnOffExploration();
                        ((QAgent)agents[i]).TurnOffLearning();
                    }
                    this.names[i] = "AI " + n;
                    n++;
                    break;

                case "AnnAI":
                    agents[i]     = new AnnAgent(false, virus.Size, i);
                    this.names[i] = "AI " + n;
                    n++;
                    break;

                case "MinimaxAI":
                    agents[i]     = new MinimaxAgent(4, i);
                    this.names[i] = "AI " + n;
                    n++;
                    break;

                case "MiniMaxMixAI":
                    if (File.Exists("TrainingData.Q"))
                    {
                        agents[i] = new MiniMaxMixAgent("TrainingData", 2, i);
                    }
                    else
                    {
                        agents[i] = new BruteForceAgent(i);
                    }
                    this.names[i] = "AI " + n;
                    n++;
                    break;

                case "MixedAI":
                    agents[i]     = new MixedAgent(0.5, false, i);
                    this.names[i] = "AI " + n;
                    n++;
                    break;

                case "BruteAI":
                    agents[i]     = new BruteForceAgent(i);
                    this.names[i] = "AI " + n;
                    n++;
                    break;

                case "RandomAI":
                    agents[i]     = new RandomAgent(i);
                    this.names[i] = "AI " + n;
                    n++;
                    break;

                case "SimpleAI":
                    agents[i]     = new SimpleAgent(i);
                    this.names[i] = "AI " + n;
                    n++;
                    break;
                }
            }

            message = this.names[1] + "'s turn";

            colors    = new Color[virus.Players + 1];
            colors[0] = Color.White;
            colors[1] = Color.FromArgb(128, 160, 255);
            colors[2] = Color.FromArgb(96, 255, 96);
            if (virus.Players > 2)
            {
                colors[3] = Color.FromArgb(255, 96, 96);
            }
            if (virus.Players > 3)
            {
                colors[4] = Color.FromArgb(255, 255, 64);
            }
            Random rand = new Random();

            for (int i = 5; i <= virus.Players; i++)
            {
                colors[i] = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
            }
        }
Example #22
0
    //? To score a game we set up three kinds of matchup, condense each one into a score,
    //? add the scores together and then normalise them between 0 and 1.
    public IEnumerator ScoreGame(Game game)
    {
        //? Reset
        playerBiasScore       = 0f;
        greedIsGoodScore      = 0f;
        skillIsBetterScore    = 0f;
        drawsAreBadScore      = 0f;
        highSkillBalanceScore = 0f;
        string scoresSoFar = "";

        if (interfaceEnabled)
        {
            currentRulesText.text  = game.GameToString();
            scoresSoFar            = "First Play Bias: " + ToScore(playerBiasScore) + "\n";
            currentScoresText.text = scoresSoFar;
            yield return(0);
        }

        //? Random vs. Random: These games can go either way, the only thing we're interested
        //? is if there's a clear bias towards playing first or second. This is a good indicator.
        //? Score is therefore proportional to the amount one agent won over the other.
        RandomAgent randomAgent1 = new RandomAgent(1);
        RandomAgent randomAgent2 = new RandomAgent(2);
        int         firstWon = 0; int secondWon = 0;

        for (int i = 0; i < randomRandomMatches; i++)
        {
            if (interfaceEnabled)
            {
                currentScoresText.text = "First Play Bias: Playing (" + i + "/" + randomRandomMatches + ")\n";
            }

            //NOTE MJ: Playing the games could be coroutines, so they don't block UI.
            //res is redundant game.endStatus already has info.
            yield return(GameEvaluation.instance.PlayGame(game, randomAgent1, randomAgent2));

            if (game.endStatus == 1)
            {
                firstWon++;
            }
            if (game.endStatus == 2)
            {
                secondWon++;
            }
            //? Yield after each playout - we could yield more frequently, this is OK though.
            yield return(0);
        }

        playerBiasScore = 1 - (Mathf.Abs(firstWon - secondWon) / randomRandomMatches);

        if (interfaceEnabled)
        {
            scoresSoFar            = "First Play Bias: " + ToScore(playerBiasScore) + "\n";
            currentScoresText.text = scoresSoFar;
            yield return(0);
        }

        //? We could also add in a measure of 'decisiveness' - i.e. games shouldn't end in draws.
        //? However for random agents this might happen just because they aren't very good.

        //? Random vs. Greedy: Greedy may not always win the game, but we expect it to
        //? win more than random. Score is proportion to the number of games greedy won or tied.
        int randomAgentWon = 0;

        for (int i = 0; i < greedyRandomMatches; i++)
        {
            if (interfaceEnabled)
            {
                currentScoresText.text = scoresSoFar + "Simple Beats Random: Playing (" + i + "/" + greedyRandomMatches + ")\n";
                yield return(0);
            }

            //? Small detail: note that we swap who plays first, to compensate
            //? for first-player advantage
            RandomAgent randomAgent = new RandomAgent(1 + (i % 2));
            GreedyAgent greedyAgent = new GreedyAgent(2 - (i % 2));

            //NOTE MJ: Playing the games could be coroutines, so they don't block UI. res could be an out parameter.
            yield return(GameEvaluation.instance.PlayGame(game, randomAgent, greedyAgent));

            if (game.endStatus == 1 + (i % 2))
            {
                randomAgentWon++;
            }
            yield return(0);
        }

        greedIsGoodScore = 1 - ((float)randomAgentWon / greedyRandomMatches);

        if (interfaceEnabled)
        {
            scoresSoFar           += "Simple Beats Random: " + ToScore(greedIsGoodScore) + "\n";
            currentScoresText.text = scoresSoFar;
            yield return(0);
        }

        //? Greedy vs. MCTS: We know that greedy players will avoid causing their own loss, and
        //? win if given the opportunity, but MCTS players can look ahead and plan. As a result,
        //? a more strategic game should be won by MCTS agents. Score is proportion of games MCTS
        //? agent won. Note that we might need to give the MCTS agent more computational resources
        //? for some games to ensure it is performing better.
        int mctsAgentWon = 0;

        for (int i = 0; i < greedySkilledMatches; i++)
        {
            if (interfaceEnabled)
            {
                currentScoresText.text = scoresSoFar + "Clever Beats Simple: Playing (" + i + "/" + greedySkilledMatches + ")\n";
                yield return(0);
            }

            MCTSAgent   skilledAgent = new MCTSAgent(1 + (i % 2));
            GreedyAgent greedyAgent  = new GreedyAgent(2 - (i % 2));

            //NOTE MJ: Playing the games could be coroutines, so they don't block UI. res could be an out parameter.
            yield return(GameEvaluation.instance.PlayGame(game, skilledAgent, greedyAgent));

            if (game.endStatus == 1 + (i % 2))
            {
                mctsAgentWon++;
            }
            yield return(0);
        }

        skillIsBetterScore = (float)mctsAgentWon / greedySkilledMatches;

        if (interfaceEnabled)
        {
            scoresSoFar           += "Clever Beats Simple: " + ToScore(skillIsBetterScore) + "\n";
            currentScoresText.text = scoresSoFar;
            yield return(0);
        }

        //? Finally, MCTS vs MCTS. If we wanted more depth, we could do two version of this,
        //? one with MCTS agents that are given different amounts of computation, to really
        //? test to see if more thinking time = better play. However, here we're just going to
        //? test a good old fashioned mirror matchup. For two good equal players, we want
        //? a) not too much imbalance in favour of either player and b) not too many draws.
        int       drawnGames = 0;
        int       firstPlayerWon = 0; int secondPlayerWon = 0;
        MCTSAgent skilledAgent1 = new MCTSAgent(1);
        MCTSAgent skilledAgent2 = new MCTSAgent(2);

        for (int i = 0; i < skilledMirrorMatches; i++)
        {
            if (interfaceEnabled)
            {
                currentScoresText.text = scoresSoFar +
                                         "Avoid Draws: Playing (" + i + "/" + skilledMirrorMatches + ")\n" +
                                         "High Skill Mirror Matchup: Playing (" + i + "/" + skilledMirrorMatches + ")\n";
                yield return(0);
            }

            //NOTE MJ: Playing the games could be coroutines, so they don't block UI. res could be an out parameter.
            yield return(GameEvaluation.instance.PlayGame(game, skilledAgent1, skilledAgent2));

            if (game.endStatus == 1)
            {
                firstPlayerWon++;
            }
            if (game.endStatus == 2)
            {
                secondPlayerWon++;
            }
            if (game.endStatus == 3 || game.endStatus == 0)
            {
                drawnGames++;
            }
            yield return(0);
        }

        drawsAreBadScore      = 1 - ((float)drawnGames / skilledMirrorMatches);
        highSkillBalanceScore = Mathf.Abs(firstPlayerWon - secondPlayerWon) / skilledMirrorMatches;

        if (interfaceEnabled)
        {
            currentScoresText.text = scoresSoFar + "Avoid Draws: " + ToScore(drawsAreBadScore) + "\n" +
                                     "High Skill Mirror Matchup: " + ToScore(highSkillBalanceScore) + "\n";
            yield return(0);
        }

        //? Now we can add up the scores and return them. If we wanted we could balance them so
        //? some scores are more important than others, or we could partition them into "must-haves"
        //? and "nice-to-haves". I discuss this in the tutorial video.

        // Debug.Log("Random vs. Random: "+playerBiasScore);
        // Debug.Log("Greedy vs. Random: "+greedIsGoodScore);
        // Debug.Log("MCTS vs. Greedy: "+skillIsBetterScore);
        // Debug.Log("MCTS vs. MCTS (draws): "+drawsAreBadScore);
        // Debug.Log("MCTS vs. MCTS (win balance): "+highSkillBalanceScore);

        game.evaluatedScore = (playerBiasScore + greedIsGoodScore + skillIsBetterScore + drawsAreBadScore + highSkillBalanceScore) / 5f;
    }