예제 #1
0
        //you are advised not to set more than 3 parallel threads if you are doing this on your laptop, otherwise the laptop will not survive
        private static void Main(string[] args)
        {
            //10ktests

            //Console.WriteLine(Cards.FromName("This is an invalid name") == Cards.FromName("Default"));-ignore this, this was for testing
            //  Console.WriteLine("Starting test setup. v7.6251test: Parallel=" + parallelOrNot + "in parallel " + parallelThreads + "x and in each parallel, no of tasks:" + testsInEachThread + " and inner parallel:" + parallelThreadsInner + " and each within inner parallel, inner tasks:" + testsInEachThreadInner + " times, different decks, get winrates and time avg of each and print max depth =" + maxDepth + " , max width = " + maxWidth + "");
            GamePlayer.CreateAndMutate createMutateObj = new CreateAndMutate(); //this is the class I added which contains all functions1
                                                                                //this above object will help you mutate or create a deck, without worrying about underlying code.
            Dictionary <int, Dictionary <int, List <Card> > > victoryMany = new Dictionary <int, Dictionary <int, List <Card> > >();
            //  Dictionary<string, int> cardStats = new Dictionary<string, int>();
            Dictionary <int, float> winRates = new Dictionary <int, float>();
            //OneTurn();-ignore this
            Dictionary <int, string> allCards = getAllCards(); //important function, must be done in main before anything else, this will get all the valid hearthstone cards (1300+ cards in total) from the data file
                                                               // string[] results = new string[100];//max 15 tests, can be increased/changed without any problems, make sure max j * max i <= size of results array
            Dictionary <int, string>       results        = new Dictionary <int, string>();
            Dictionary <int, List <Card> > resultsMutated = new Dictionary <int, List <Card> >();
            // List<int,List<Card>> mutations = new List<List<Card>>();
            //List<Card> playerDeck = createMutateObj.createRandomDeck(allCards, cardname);//this liine returns randomly created deck from all cards in hearthsone, which is passed as parameter
            //List<Card> playerDeck = Decks.AggroPirateWarrior;
            bool end = false;

            // Decks.PaladinBasic;

            stopwatch2.Start();
            //DateTime start = new DateTime();
            //DateTime stop = new DateTime();
            ParallelOptions parallelOptions = new ParallelOptions();

            parallelOptions.MaxDegreeOfParallelism = parallelThreads;
            //string folderName = Console.ReadLine();
            //int GPUID = 1;
            //int numGames = 10;

            int GPUID = int.Parse(args[0]) - 1;        //1;//1;// -1 for Professor hoover0;// 0;//

            string folderName    = args[1].ToString(); //"22-02-2018-23-54-44";// "starter-0";/"starter-0";//
            int    numGames      = int.Parse(args[2]); //200;// 100;// 100;//100;//
            int    deckIncrement = int.Parse(args[3]); //2;// 0;//  0;//

            int deckID       = GPUID / numGames;
            int remainder    = GPUID % numGames;
            int gamesPerCPUs = int.Parse(args[4]);//25

            //sample: GamePlayer.exe array_ID starter-0 400 0 25

            // int numGames = 0;
            Console.WriteLine("here for Hunter Deck GPUID=" + GPUID + "numgames=" + numGames + "folderanme=" + folderName);
            Console.WriteLine("here GPUID=" + GPUID + "numgames=" + numGames + "folderanme=" + folderName);
            string path  = folderName + "/Decks.txt";
            int    level = int.Parse(folderName.Split('-')[1]);

            while (true)
            {
                while (Directory.Exists(folderName))
                {
                    Console.WriteLine(" this code is for 10k to run on  HPC count of results=" + results.Count);
                    Console.WriteLine("Found " + folderName);
                    Thread.Sleep(10000);
                    path = folderName + "/Decks.txt";
                    // Dictionary<int, List<Card>> nDecks = getDecksFromFile(path);

                    int j          = 0;
                    int currDeckID = deckID;
                    while (j < gamesPerCPUs)
                    {
                        List <Card> playerDeck2 = getDeckFromFile(folderName + "/EnemyDeck.txt");
                        List <Card> playerDeck  = getDeckFromFile(folderName + "/FriendDeck.txt"); //nDecks[currDeckID];
                        Console.WriteLine("Enemy deck:-size=" + playerDeck2.Count);
                        createMutateObj.print(playerDeck2);
                        Console.WriteLine("Friend deck:- size=" + playerDeck.Count);
                        createMutateObj.print(playerDeck);
                        string gameLogAddr = folderName + "/Deck" + currDeckID;
                        enemyDeckClass  = getClassFromFile(folderName + "/EnemyDeck.txt");
                        friendDeckClass = getClassFromFile(folderName + "/FriendDeck.txt");
                        Console.WriteLine("currently on deck =" + currDeckID);
                        gameLogAddr += "/" + GPUID + "-" + j + ".txt";

                        createMutateObj.printToFile(playerDeck, gameLogAddr);//printed once in begining
                        Stopwatch stopwatch = new Stopwatch();
                        if (!results.ContainsKey(j))
                        {
                            string winRate_timeMean = "";
                            stopwatch.Start();
                            bool retry = true;
                            int  tries = 0;
                            while (retry)
                            {
                                try
                                {
                                    var thread = new Thread(() =>
                                    {
                                        winRate_timeMean = "GPUID:" + GPUID + "-" + j + " Game Deck:" + currDeckID + ": " + getWinRateTimeMean(playerDeck, j, playerDeck2, gameLogAddr);
                                    });
                                    thread.Start();
                                    bool finished = thread.Join(600000);
                                    if (!finished)
                                    {
                                        //thread.Abort();
                                        retry = true;
                                        Console.WriteLine("had to continue here for deck=" + currDeckID);
                                        //  winRateLater = float.Parse(winRate_timeMean.Split('%')[0].Split('=')[1]);
                                        tries++;
                                        continue;
                                    }
                                    else
                                    {
                                        retry = false;
                                    }
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                }
                                if (tries > 3)
                                {
                                    break;
                                }
                            }
                            //FullGame(playerDeck, i, playerDeck2);//
                            Console.WriteLine("here after game is played GPUID=" + GPUID + "-" + j);

                            stopwatch.Stop();

                            //  winRateLater = float.Parse(winRate_timeMean.Split('%')[0].Split('=')[1]);
                            long     seconds = (stopwatch.ElapsedMilliseconds / 1000);//(stop - start).ToString();//
                            TimeSpan t       = TimeSpan.FromSeconds(seconds);

                            if (!results.ContainsKey(j))
                            {
                                results.Add(j, winRate_timeMean + "Time taken:" + t.ToString());
                                // winRates.Add(j, winRateLater);
                            }

                            // Console.WriteLine(results[j]);
                            string cardStatisticsForPrint = produceCardStatsString();

                            stopwatch.Reset();
                            Console.WriteLine("here just before file print GPUID=" + GPUID + "-" + j + "numgames=" + numGames + "folderanme=" + folderName);
                            string overallGameStat = folderName + "/Overall/Deck" + currDeckID;
                            if (!Directory.Exists(overallGameStat))
                            {
                                Directory.CreateDirectory(overallGameStat);
                            }
                            try
                            {
                                path = overallGameStat + "/" + GPUID + "-" + j + ".txt";
                                //createMutateObj.print(playerDeck);
                                createMutateObj.printToFile(playerDeck, path);
                                using (StreamWriter tw = File.AppendText(path))
                                {
                                    tw.WriteLine(results[j]);
                                    tw.WriteLine(cardStatisticsForPrint + "/n");

                                    //print card stats here.
                                    tw.Close();
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                            foreach (int key in results.Keys)
                            {
                                // Console.WriteLine("Game " + key + " : " + results[key] + "\n");
                                if (resultsMutated.ContainsKey(key))
                                {
                                    //  createMutateObj.print(resultsMutated[key]);
                                }
                            }
                            j++;
                            currDeckID += deckIncrement;
                            GC.Collect();
                            cardStats.Clear();
                            long memory = GC.GetTotalMemory(false);
                            Console.WriteLine("Memory usage here:" + memory);
                        }
                    }
                    level++;
                    folderName = folderName.Split('-')[0] + "-" + level.ToString();
                    results.Clear();
                    //  nDecks.Clear();
                    //  Console.WriteLine("count of nDecks=" + nDecks.Count);
                    Console.WriteLine("trying to find " + folderName);
                }
            }
            foreach (int key in results.Keys)
            {
                Console.WriteLine("Game " + key + " : " + results[key] + "\n");
                if (resultsMutated.ContainsKey(key))
                {
                    //createMutateObj.print(resultsMutated[key]);
                }
                stopwatch2.Stop();
                TimeSpan tempeForOverall = TimeSpan.FromSeconds(stopwatch2.ElapsedMilliseconds / 1000);
                Console.WriteLine("Overall time taken:" + tempeForOverall.ToString());
            }
        }
예제 #2
0
        public static string getWinRateTimeMeanLinear(List <Card> player1Deck, int where, List <Card> player2Deck)
        {
            int[] wins          = Enumerable.Repeat(0, 1000).ToArray();
            long  sum_Timetaken = 0;
            int   winss         = 0;
            int   numOfTests    = 70;

            object[] temp()
            {
                object[] obj = new object[1002];
                for (int i = 0; i < numOfTests; i++)
                {
                    obj[i] = new Stopwatch();
                }
                return(obj);
            }

            object[] stopwatches = temp();
            //Parallel.For(0, parallelThreadsInner, parallelOptions, j =>
            {
                // int i = 0;
                long max = 0;
                // while (!end)
                for (int i = 0; i < numOfTests; i++) ////
                {
                    Console.WriteLine("Inner i, or here inside getWinRateTimeMean at here= " + i);
                    ((Stopwatch)stopwatches[i]).Start();
                    // start = DateTime.Now;
                    string s = "";
                    try
                    {
                        //s = FullGame(player1Deck, i, player2Deck);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        CreateAndMutate createAndMutate = new CreateAndMutate();
                        Console.WriteLine("Player 1 deck that caused issue:");
                        createAndMutate.print(player1Deck);
                    }
                    //stop = DateTime.Now;
                    ((Stopwatch)stopwatches[i]).Stop();
                    long seconds = (((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000);//(stop - start).ToString();//
                    Console.WriteLine("secondes:" + seconds);
                    if (max < seconds)
                    {
                        max = seconds;
                    }
                    TimeSpan tempe = TimeSpan.FromSeconds(seconds);
                    Console.WriteLine("time taken for " + i + ":" + tempe);
                    sum_Timetaken = sum_Timetaken + seconds;
                    Console.WriteLine("sum_TimeTaken in loop:" + sum_Timetaken);
                    //((Stopwatch)stopwatches[i]).Reset();
                    if (s.Contains("Player1: WON"))
                    {
                        wins[i]++;
                        winss++;
                        Console.WriteLine("Winss:" + winss);
                    }
                }
                Console.WriteLine("Max was:" + max);
                max = 0;
            }//);
            Console.WriteLine("Starting test setup. v6.7: all linear, 10 and in each 50 linear all randomly gen decks Not running in Parallel at ALL, this is in Linear Method run in parallel " + parallelThreads + "x and in each parallel, no of tasks:" + testsInEachThread + " and inner parallel:" + parallelThreadsInner + " and each within inner parallel, inner tasks:" + testsInEachThreadInner + " times, different decks, get winrates and time avg of each and print max depth =" + maxDepth + " , max width = " + maxWidth + "");

            for (int i = 0; i < (numOfTests); i++)
            {
                Console.WriteLine("i:" + i + " wins:" + wins[i]);
            }
            sum_Timetaken = 0;
            for (int i = 0; i < (numOfTests); i++)
            {
                Console.WriteLine("i:" + i + " Times:" + ((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000);
                sum_Timetaken = sum_Timetaken + ((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000;
            }
            Console.WriteLine("New sum_timetaken=" + sum_Timetaken);
            TimeSpan t       = TimeSpan.FromSeconds(sum_Timetaken / (numOfTests));
            float    winsSum = 0;

            for (int i = 0; i < (numOfTests); i++)
            {
                if (wins[i] > 0)
                {
                    winsSum++;
                }
            }
            //winsSum = winss;
            Console.WriteLine(winsSum + "is winsSum");
            Console.WriteLine(winss + "is winss");
            float winrateDiv = (float)((float)winsSum / ((float)numOfTests));

            return("Win rate =" + winrateDiv * 100 + "% and average time of each round (hh:mm:ss) = " + t.ToString());
        }