Guest User

ICR_Pack_Sim.R

a guest
Jul 8th, 2021
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.17 KB | None | 0 0
  1. #Rare Completion - ICRs and Unopened Packs
  2. #Written: 2021-0705 by Emil Karpinski
  3.  
  4. #Setting up some variables
  5. #cards - tracks the cards that have been obtained in each method (no duplicates)
  6. #ItCount - tracks how many random ICRs were needed to complete each set (i.e. 1:[Rare_Amount] x )
  7. #Iteration - tracks how many times you've gotten an ICR (effectively a counter for rounds.)
  8. #i and a - general all purpose counters for the while and for loops
  9. #Rare_Amount - Integer denoting the amount of rares in the set
  10. #Rare_Obt - Integer denoting how many rares have already been obtained (used to seed [cards] during each simulation)
  11. #Simulations - The number of simulations to run.
  12.  
  13. #Load in the dplyr library which we use to maintain the Col_Size dataframe.
  14. library(dplyr)
  15.  
  16. #Defining some empty variables and setting some flags
  17. cards <- vector()
  18. ItCount <- vector()
  19. iteration <- 0
  20. i <- 0
  21. b <- 0
  22. Col_Size <- data.frame()
  23. Col_new <- data.frame()
  24. Col_time <- data.frame()
  25. It_Size <- vector()
  26. Uniq_It <- vector()
  27. Mean_Size <- vector()
  28. Mean_ICRTime <- vector()
  29.  
  30. #Defining the model parameters
  31. Rare_Amount <- 60
  32. Rare_Obt <- 0
  33. Simulations <- 10000
  34.  
  35.  
  36. #Does simulations tracking the time needed to obtain a full set of rares (in full playsets, 4 of each).
  37. #Sets up the loop for the desired number of simulations (recommended 1k+)
  38. for (a in 1:Simulations)
  39. {
  40.   #Runs until the set is complete - denoted by the flag (i) changing from 0 to 1
  41.   while (i < 1)
  42.   {
  43.     #Seeding the collection with the currently owned amount of rares.
  44.     #Checks if the amount of rares already obtained (Rare_obt is greater than 0 - i.e. you have something); and we haven't already gone through the loop.
  45.     #If both satisfied, start populating the cards vector with your obtained rares. Does this in a while loop so you can control that you can't get >4 copies of a card.
  46.     if ((Rare_Obt > 0) & (iteration == 0))
  47.     {
  48.       while (b < Rare_Obt)
  49.       {
  50.         x <- sample(1:Rare_Amount, 1, replace=T)
  51.        
  52.         if (sum(cards == x) < 4)
  53.         {
  54.           cards <- c(cards,x)
  55.           b <- b+1
  56.         }
  57.       }
  58.       b <- 0
  59.     }
  60.    
  61.     #Counts how many times we've been through this loop. i.e. how many times have we drawn a card and checked if we have it     or not.
  62.     iteration <- iteration + 1
  63.    
  64.     #Samples a random number from 1:Rare_Amount (number of rares). Replace does nothing here.
  65.     x <- sample(1:Rare_Amount, 1, replace=T)
  66.    
  67.     #Checks if the chosen card currently has less than 4 copies. If so then it adds that entry to the list of cards drawn (cards vector).
  68.     #Then checks to see if the full set is complete (i.e. Rare_Amount*4 entries in that vector). If so it trips the i flag to leave the loop.
  69.     if (sum(cards == x) < 4)
  70.     {
  71.       cards <- c(cards,x)
  72.      
  73.       #Records the iterations at which unique cards (i.e. those for which we don't have a playset are drawn)
  74.       Uniq_It <- c(Uniq_It, iteration)
  75.      
  76.       if (length(cards) == (Rare_Amount*4))
  77.       {
  78.         i <- 1
  79.       }
  80.     }
  81.    
  82.     #Adds the amount of unique collected cards to a temporary vector (It_size) storing the size of the collection at each step of the iteration.
  83.     It_Size <- c(It_Size, length(cards))
  84.   }
  85.  
  86.   #Extends the vector ItCount (stores the number of draws it took to get a full playset - i.e. iteration), and adds the last value of iteration to it.  
  87.   ItCount <- c(ItCount, iteration)
  88.  
  89.   #Adds It_Size to Col_Size, to track how the size of the collection grew at each iteration within each simulation
  90.   Col_Size <- bind_rows(Col_Size, as.data.frame(t(It_Size)))
  91.  
  92.   #Adds Uniq_It to Col_new, to track at which iterations we actually obtained a new unique cards across simulations
  93.   Col_new <- bind_rows(Col_new, as.data.frame(t(Uniq_It)))
  94.  
  95.   #Cleans up the cards vector (sets it to empty), sets iterations to 0, and resets the flag to re-enable the while loop.
  96.   cards <- vector()
  97.   iteration <- 0
  98.   i <- 0
  99.   It_Size <- vector()
  100.   Uniq_It <- vector()
  101. }
  102.  
  103. #Doing some formatting on the Col_Size dataframe.
  104. #Replacing NA values with the max collection size (Rare_Amount*4)
  105. Col_Size[is.na(Col_Size)] <- (Rare_Amount*4)
  106.  
  107. #Cleaning up the column names to strip out the V prefix.
  108. colnames(Col_Size) <- (c(1:max(ItCount)))
  109. colnames(Col_new) <- (c((Rare_Obt+1):(Rare_Amount*4)))
  110.  
  111. #Calculating the average collection size at each iteration
  112. for (c in 1:max(ItCount))
  113. {
  114.   Mean_Size <- c(Mean_Size, mean(Col_Size[,c]))
  115. }
  116.  
  117. #Plot each simulation on the same graph and the mean size at each iteration as a line.
  118. plot(unlist(Col_Size[1,]), xlab = "Number of ICRs", ylab = "Collection Size", main = "Number of ICRs vs Collection Size")
  119. for (d in 2:Simulations)
  120. {
  121.   points(unlist(Col_Size[d,]))
  122. }
  123. lines(Mean_Size, lwd = 3, col = "green")
  124.  
  125. #Similar to the above, plots the number of ICRs needed to obtain the next rare within each simulation.
  126. #Also adds a line with the average across all simulations.
  127. for (e in 1:Simulations)
  128. {
  129.   #Creates a temporary vector which stores the number of ICRs drawn from the previous unique rare to the next one
  130.   temp <- c(0)
  131.  
  132.   #Computes the number of ICRs it took to get a new rare at each stage.
  133.   for (f in 1:(length(Col_new)))
  134.   {
  135.     if (f==1)
  136.     {
  137.       temp <- c(temp, unlist(Col_new[e,f]))
  138.     }
  139.     else
  140.     {
  141.       temp <- c(temp, unlist(Col_new[e,f]-Col_new[e,(f-1)]))
  142.     }
  143.   }
  144.   #Adds the temp vector to a new data.frame - Col_time
  145.   Col_time <- bind_rows(Col_time, as.data.frame(t(temp)))
  146.  
  147. }
  148.  
  149. #Calculating the average time to next unique rare
  150. for (c in 1:length(Col_time))
  151. {
  152.   Mean_ICRTime <- c(Mean_ICRTime, mean(Col_time[,c]))
  153. }
  154.  
  155. #Plots the data on time to next unique rare. If this is the first time through the loop it creates the plot; else it adds to it.
  156. for (g in 1:Simulations)
  157. {
  158.   if (g == 1)
  159.   {
  160.     plot(x = ((Rare_Obt):(Rare_Amount*4)), y = Col_time[g,], xlab = "Number of rares", ylab = "ICRs until next unique rare", main = "Time to Next Rare", ylim = c(0, Mean_ICRTime[((Rare_Amount*4+1)-(Rare_Obt))]*2))
  161.   }
  162.   else
  163.   {
  164.     points(x = ((Rare_Obt):((Rare_Amount*4))), y = Col_time[g,])
  165.   }
  166. }
  167. lines(x = ((Rare_Obt):((Rare_Amount*4))), y = Mean_ICRTime, lwd = 3, col = "green")
Advertisement
Add Comment
Please, Sign In to add comment