//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) { //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 + ""); Sabbertest2.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; List <Card> playerDeck2 = Decks.MidrangeJadeShaman; 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 = 1; // int.Parse(args[0]);//1;// string folderName = "folderwith decks"; // args[1].ToString();//"2d7d2018-11-26-56-AM";// int numGames = 200; // int.Parse(args[2]);// 200;// int deckIncrement = 1; // int.Parse(args[3]); int deckID = GPUID / numGames; int remainder = GPUID % numGames; // int numGames = 0; Console.WriteLine("here GPUID=" + GPUID + "numgames=" + numGames + "folderanme=" + folderName); /* if(remainder == 3 || remainder == 5) * { * numGames = 34; * } * else * { * numGames = 33; * }*/ Console.WriteLine("here GPUID=" + GPUID + "numgames=" + numGames + "folderanme=" + folderName); string path = folderName + "/Decks.txt"; //int initialID = 5;// int.Parse(args[0]); int level = int.Parse(folderName.Split('-')[1]); while (true) { while (Directory.Exists(folderName)) { Console.WriteLine("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 (currDeckID < nDecks.Count) { Console.WriteLine("Using Player1 = deck:MidRangeJadeShaman (control strategy, class: DRUID) and player 2 as deck:MidRangeJadeShaman (Mid Range strategy, class:SHAMAN)"); List <Card> playerDeck = Decks.MidrangeJadeShaman; string gameLogAddr = folderName + "/Deck" + currDeckID; Console.WriteLine("currently on deck =" + currDeckID); if (!Directory.Exists(gameLogAddr)) { Directory.CreateDirectory(gameLogAddr); } string overallGameStat = folderName + "/Overall/Deck" + currDeckID; if (!Directory.Exists(overallGameStat)) { Directory.CreateDirectory(overallGameStat); } gameLogAddr += "/" + GPUID + "-" + j + ".txt"; createMutateObj.printToFile(playerDeck, gameLogAddr);//printed once in begining Stopwatch stopwatch = new Stopwatch(); // playerDeck = createMutateObj.createRandomDeck(allCards, cardname); // playerDeck = createMutateObj.createRandomDeck(allCards, cardname); if (!results.ContainsKey(j)) { //Console.WriteLine("main i, deck number=" + i); // Console.WriteLine("outer i, deck number=" + j); // Console.WriteLine("Printing Deck player 1, loop is here=" + j); // createMutateObj.print(playerDeck); // Console.WriteLine("Printing Deck player 2 loop is here=" + j); // createMutateObj.print(playerDeck2); 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("outer i, deck number=" + j); // Console.WriteLine("Printing Deck player 2 loop is here=" + j); // createMutateObj.print(playerDeck2); // Console.WriteLine("Printing Deck player 1, loop is here=" + j); // createMutateObj.print(playerDeck); // Console.WriteLine(results[j]); string cardStatisticsForPrint = produceCardStatsString(); stopwatch.Reset(); Console.WriteLine("here just before file print GPUID=" + GPUID + "-" + j + "numgames=" + numGames + "folderanme=" + folderName); 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); } }//); /* for (int i = 0; i < results.Length; i++)//for 15 results here, if parallel threads * testInEachThread = 6, then 6 tests will show here * { * Console.WriteLine("Game " + i + " : " + results[i] + "\n"); * }*/ //Console.WriteLine("Starting test setup. v7.0:inner parallel 5x10 testing deviations, 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 + ""); 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()); /* createMutateObj.print(victorious); * * * List<Card> myDeck = new List<Card>(); * myDeck = victorious;//myDeck can be anything, I have made it =victorious/last deck created in the loop. * List<Card> mutated = createMutateObj.mutate(myDeck, allCards, cardname);//make your deck myDeck and pass it here to mutate it. * * //RandomGames(); - ignore this line * Console.WriteLine("\n Mutated Deck: \n"); * createMutateObj.print(mutated); * Console.WriteLine("Test end!");*/ //Console.ReadLine(); } // Console.WriteLine("Before Mutation Victory Decks:"); }
//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) { //Console.WriteLine(Cards.FromName("This is an invalid name") == Cards.FromName("Default"));-ignore this, this was for testing Console.WriteLine("Starting test setup. v6.7: 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 + ""); Sabbertest2.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, List <Card> > victoryMany = new Dictionary <int, List <Card> >(); //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>(); List <Card> victorious = new 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 bool end = false; List <Card> playerDeck2 = Decks.MidrangeJadeShaman; stopwatch.Start(); //DateTime start = new DateTime(); //DateTime stop = new DateTime(); Parallel.For(0, parallelThreads, j => { // int i = 0; // while (!end) // for (int i = testsInEachThread * j; i < (j + 1) * testsInEachThread; i++)//(int i = 0; i < 10 ; i++) // { if (!results.ContainsKey(j)) { Console.WriteLine("outer i, deck number=" + j); Console.WriteLine("Printing Deck player 1, loop is here=" + j); createMutateObj.print(playerDeck); Console.WriteLine("Printing Deck player 2 loop is here=" + j); createMutateObj.print(playerDeck); string winRate_timeMean = getWinRateTimeMean(playerDeck, j, playerDeck2); results.Add(j, winRate_timeMean); Console.WriteLine(results[j]); } /*if (results[i].Contains("Player1: WON"))//-this is to get the deck only if it wins * { * playerDeck2 = playerDeck; * Console.WriteLine("Testing with inital deck...."); * string s = "Testing with inital deck...."; * stopwatch.Start(); * s = s + FullGame(playerDeck, i, Decks.MidrangeJadeShaman); * stopwatch.Stop(); * long seconds = (stopwatch.ElapsedMilliseconds / 1000);//(stop - start).ToString();// * TimeSpan t = TimeSpan.FromSeconds(seconds); * if (s.Contains("Player1: LOST")) * { * s = s + " time taken to execute completely (hh:mm:ss): " + t.ToString(); * results.Add(i+1, s); * break; * } * playerDeck = playerDeck = createMutateObj.createRandomDeck(allCards, cardname); * * //break; * //victorious = playerDeck; * /* if (!victoryMany.ContainsKey(i)) * { * victoryMany.Add(i, playerDeck); //this gives the last deck that played the game * } * //end = true; * } * else * { * playerDeck = createMutateObj.mutate(playerDeck, allCards, cardname);//keep mutating till victory * }*/ //i++; if (j == 100) { // end = true; } playerDeck = createMutateObj.createRandomDeck(allCards, cardname); } }); /* for (int i = 0; i < results.Length; i++)//for 15 results here, if parallel threads * testInEachThread = 6, then 6 tests will show here * { * Console.WriteLine("Game " + i + " : " + results[i] + "\n"); * }*/ foreach (int key in results.Keys) { Console.WriteLine("Game " + key + " : " + results[key] + "\n"); } Console.WriteLine("Before Mutation Victory Decks:"); stopwatch.Stop(); TimeSpan tempeForOverall = TimeSpan.FromSeconds(stopwatch.ElapsedMilliseconds / 1000); Console.WriteLine("Overall time taken:" + tempeForOverall.ToString()); /* createMutateObj.print(victorious); * * * List<Card> myDeck = new List<Card>(); * myDeck = victorious;//myDeck can be anything, I have made it =victorious/last deck created in the loop. * List<Card> mutated = createMutateObj.mutate(myDeck, allCards, cardname);//make your deck myDeck and pass it here to mutate it. * * //RandomGames(); - ignore this line * Console.WriteLine("\n Mutated Deck: \n"); * createMutateObj.print(mutated); * Console.WriteLine("Test end!");*/ Console.ReadLine(); }
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()); }
//create /* * public static Dictionary<int, int> createDeck(Dictionary<int, string> allcards) * { * Dictionary<int, int> chosenDeck = new Dictionary<int, int>(); * Random rand = new Random(); * int Count = 0; * while (Count != 30) * { * int oneRand = rand.Next(0, allcards.Count + 1); * if (allcards.ContainsKey(oneRand)) //&& (Cards.FromName(allcards[oneRand]))) * { * if (chosenDeck.ContainsKey(oneRand) && (chosenDeck[oneRand] == 1)) * { * chosenDeck[oneRand] = 2; * Count++; * } * else if (!chosenDeck.ContainsKey(oneRand)) * { * chosenDeck.Add(oneRand, 1); * Count++; * } * } * } * //List<Card> playerDeck = convertDictToList(chosenDeck, allcards); * Console.WriteLine("Count:" + Count); * return chosenDeck; * } */ //build allcards in dictionary from txt file public static string getWinRateTimeMean(List <Card> player1Deck, int where, List <Card> player2Deck) { int[] wins = Enumerable.Repeat(0, 1000).ToArray(); long sum_Timetaken = 0; int winss = 0; object[] temp() { object[] obj = new object[1002]; for (int i = 0; i < parallelThreadsInner * testsInEachThreadInner; i++) { obj[i] = new Stopwatch(); } return(obj); } object[] stopwatches = temp(); Parallel.For(0, parallelThreadsInner, j => { // int i = 0; long max = 0; // while (!end) for (int i = testsInEachThreadInner * j; i < (j + 1) * testsInEachThreadInner; i++)//(int i = 0; i < 10 ; 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; }); for (int i = 0; i < (parallelThreadsInner * testsInEachThreadInner); i++) { Console.WriteLine("wins:" + wins[i]); } sum_Timetaken = 0; for (int i = 0; i < (parallelThreadsInner * testsInEachThreadInner); i++) { Console.WriteLine("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 / (parallelThreadsInner * testsInEachThreadInner)); int winsSum = 0; for (int i = 0; i < (parallelThreadsInner * testsInEachThreadInner); i++) { if (wins[i] > 0) { winsSum++; } } //winsSum = winss; Console.WriteLine(winsSum + "is winsSum"); Console.WriteLine(winss + "is winss"); long winrate = ((winsSum / (parallelThreadsInner * testsInEachThreadInner)) * 100); return("Win rate = " + winrate + "% and average time of each round (hh:mm:ss) = " + t.ToString()); }