Exemplo n.º 1
0
        static void TrainMemoryQ(int size, byte qnumber, MemoryQAgent agent, String opponent, String logname, String savename, int iterations, int saveinterval = 360)
        {
            Virus        virus = new Virus(2, size);
            int          wins = 0, wins2 = 0;
            byte         oppnumber = qnumber == 1 ? (byte)2 : (byte)1;
            Agent        opp       = new BruteForceAgent(oppnumber);
            StreamWriter writer    = new StreamWriter(logname);

            for (int i = 1; i <= iterations; i++)
            {
                switch (opponent)
                {
                case "brute":
                    break;

                case "minimax4":
                    opp = new MinimaxAgent(4, oppnumber);
                    break;

                case "minimax3":
                    opp = new MinimaxAgent(3, oppnumber);
                    break;

                case "minimax2":
                    opp = new MinimaxAgent(2, oppnumber);
                    break;

                default:
                    opp = new BruteForceAgent(oppnumber);
                    break;
                }

                int winner = RunGame(virus, qnumber == 1 ? (Agent)agent : opp, qnumber == 2 ? (Agent)agent : opp);
                wins  += winner == 1 ? 1 : 0;
                wins2 += winner == 1 ? 1 : 0;

                agent.ProcessShortTermMemory();

                if (i % saveinterval == 0)
                {
                    agent.Save(savename);
                    Console.WriteLine("Iteration: " + i);
                    Console.WriteLine("Wins: " + wins);
                    wins = 0;
                }
                virus = new Virus(2, size);
            }
            for (int n = 0; n < 10; n++)
            {
                MemoryQAgent ag = new MemoryQAgent();
                for (int i = 0; i < 1000; i++)
                {
                    agent.TellOfMemoryTo(ag);
                }
                wins = 0;
                opp  = new BruteForceAgent(oppnumber);
                for (int i = 1; i <= 10000; i++)
                {
                    switch (opponent)
                    {
                    case "brute":
                        break;

                    case "minimax4":
                        opp = new MinimaxAgent(4, oppnumber);
                        break;

                    case "minimax3":
                        opp = new MinimaxAgent(3, oppnumber);
                        break;

                    case "minimax2":
                        opp = new MinimaxAgent(2, oppnumber);
                        break;

                    default:
                        opp = new BruteForceAgent(oppnumber);
                        break;
                    }

                    int winner = RunGame(virus, qnumber == 1 ? (Agent)ag : opp, qnumber == 2 ? (Agent)ag : opp);
                    wins += winner == 1 ? 1 : 0;
                    virus = new Virus(2, size);
                }
                Console.WriteLine("After 1000 memories");
                Console.WriteLine("Wins: " + wins);
                writer.Write(((double)wins) / 10000.0 + ";");
                for (int i = 0; i < 9000; i++)
                {
                    agent.TellOfMemoryTo(ag);
                }
                wins = 0;
                opp  = new BruteForceAgent(oppnumber);
                for (int i = 1; i <= 10000; i++)
                {
                    switch (opponent)
                    {
                    case "brute":
                        break;

                    case "minimax4":
                        opp = new MinimaxAgent(4, oppnumber);
                        break;

                    case "minimax3":
                        opp = new MinimaxAgent(3, oppnumber);
                        break;

                    case "minimax2":
                        opp = new MinimaxAgent(2, oppnumber);
                        break;

                    default:
                        opp = new BruteForceAgent(oppnumber);
                        break;
                    }

                    int winner = RunGame(virus, qnumber == 1 ? (Agent)ag : opp, qnumber == 2 ? (Agent)ag : opp);
                    wins += winner == 1 ? 1 : 0;
                    virus = new Virus(2, size);
                }
                Console.WriteLine("After 10000 memories");
                Console.WriteLine("Wins: " + wins);
                writer.Write(((double)wins) / 10000.0 + ";");
                for (int i = 0; i < 90000; i++)
                {
                    agent.TellOfMemoryTo(ag);
                }
                wins = 0;
                opp  = new BruteForceAgent(oppnumber);
                for (int i = 1; i <= 10000; i++)
                {
                    switch (opponent)
                    {
                    case "brute":
                        break;

                    case "minimax4":
                        opp = new MinimaxAgent(4, oppnumber);
                        break;

                    case "minimax3":
                        opp = new MinimaxAgent(3, oppnumber);
                        break;

                    case "minimax2":
                        opp = new MinimaxAgent(2, oppnumber);
                        break;

                    default:
                        opp = new BruteForceAgent(oppnumber);
                        break;
                    }

                    int winner = RunGame(virus, qnumber == 1 ? (Agent)ag : opp, qnumber == 2 ? (Agent)ag : opp);
                    wins += winner == 1 ? 1 : 0;
                    virus = new Virus(2, size);
                }
                Console.WriteLine("After 100000 memories");
                Console.WriteLine("Wins: " + wins);
                writer.WriteLine(((double)wins) / 10000.0);
            }
            writer.Close();
            agent.SaveLongTermMemory("m");
        }
Exemplo n.º 2
0
        static void comTestMemQ()
        {
            //train a MemQ agent, and once every ten games have it teach an observer
            //do this with ten agents
            const int teachingagents       = 10;
            const int gamesbetweenteaching = 10;
            const int learninggames        = 10000;

            int wins = 0;
            int n    = 0;

            while (File.Exists("observer" + n + ".log"))
            {
                n++;
            }
            StreamWriter writer = new StreamWriter("observer" + n + ".log");

            writer.WriteLine("sources");

            Virus        virus;
            MemoryQAgent observer = new MemoryQAgent(1);
            Agent        opponent = new BruteForceAgent(2);

            for (int i = 0; i < teachingagents; i++)
            {
                MemoryQAgent agent = new MemoryQAgent(1);

                wins = 0;
                for (int j = 0; j < learninggames; j++)
                {
                    virus = new Virus(2, 5);
                    wins += RunGame(virus, agent, opponent) == 1 ? 1 : 0;
                    agent.ProcessShortTermMemory();

                    if (j % gamesbetweenteaching == gamesbetweenteaching - 1)
                    {
                        agent.TellOfMemoryTo(observer, true);
                    }
                    if (j % 1000 == 999)
                    {
                        if (j > 9500)
                        {
                            writer.WriteLine(wins);
                            Console.WriteLine("Wins: " + wins);
                        }

                        wins = 0;
                    }
                }
            }

            writer.WriteLine("observer");
            for (int i = 0; i < 10; i++)
            {
                wins = 0;
                for (int j = 0; j < 1000; j++)
                {
                    virus = new Virus();
                    wins += RunGame(virus, observer, opponent) == 1 ? 1 : 0;
                    observer.ForgetShortTerm();
                }
                writer.WriteLine(wins);
                Console.WriteLine("Observer wins: " + wins);
            }
            writer.Close();
        }