Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. #module imports
  2. import random
  3. from scipy.stats import rankdata
  4. from scipy import stats
  5. import numpy as np
  6. import pandas as pd
  7. import matplotlib.pyplot as plt
  8. from pandas import ExcelWriter
  9.  
  10. #creating lists to be filled with regression slopes for each iteration
  11. a_slopes = []
  12. d_slopes = []
  13.  
  14. for x in range(3000):
  15. #setting number of armies to start
  16. a_army = 5
  17. d_army = 5
  18. #creating lists to be used for total_logs dataframe later
  19. attack_log = [a_army]
  20. def_log = [d_army]
  21. attack_dice_log = [0]
  22. def_dice_log = [0]
  23.  
  24. #creating a while statement to loop until one of the armies runs out
  25. while a_army >1 or d_army >0:
  26.  
  27. #die roll
  28. a_die_1 = random.randint(1,6)
  29. a_die_2 = random.randint(1,6)
  30. a_die_3 = random.randint(1,6)
  31. d_die_1 = random.randint(1,6)
  32. d_die_2 = random.randint(1,6)
  33.  
  34. #combining all sets of dice
  35. full_a = [a_die_1, a_die_2, a_die_3]
  36. full_a.sort(reverse=True)
  37. mid_a = [a_die_1,a_die_2]
  38. mid_a.sort(reverse=True)
  39. min_a = [a_die_1]
  40. full_d = [d_die_1, d_die_2]
  41. full_d.sort(reverse=True)
  42. min_d = [d_die_1]
  43.  
  44. #attacker 3 and defender 2
  45. if a_army > 3 and d_army > 2:
  46. if full_a[0]>full_d[0] and full_a[1]>full_d[1] :
  47. d_army -= 2
  48. attack_dice_log.append(full_a)
  49. def_dice_log.append(full_d)
  50. def_log.append(d_army)
  51. attack_log.append(a_army)
  52. elif full_a[0]<=full_d[0] and full_a[1]<=full_d[1]:
  53. a_army -= 2
  54. attack_dice_log.append(full_a)
  55. def_dice_log.append(full_d)
  56. def_log.append(d_army)
  57. attack_log.append(a_army)
  58. elif full_a[0]>full_d[0] and full_a[1]<=full_d[1]:
  59. a_army -= 1
  60. d_army -= 1
  61. attack_dice_log.append(full_a)
  62. def_dice_log.append(full_d)
  63. def_log.append(d_army)
  64. attack_log.append(a_army)
  65. elif full_a[0]<=full_d[0] and full_a[1]>full_d[1]:
  66. a_army -= 1
  67. d_army -= 1
  68. attack_dice_log.append(full_a)
  69. def_dice_log.append(full_d)
  70. def_log.append(d_army)
  71. attack_log.append(a_army)
  72.  
  73. #attacker 2 and defender 2
  74. elif 2<a_army<4 and d_army > 2:
  75. if mid_a[0]>full_d[0] and mid_a[1]>full_d[1] :
  76. d_army -= 2
  77. attack_dice_log.append(mid_a)
  78. def_dice_log.append(full_d)
  79. def_log.append(d_army)
  80. attack_log.append(a_army)
  81. elif mid_a[0]<=full_d[0] and mid_a[1]<=full_d[1]:
  82. a_army -= 2
  83. attack_dice_log.append(mid_a)
  84. def_dice_log.append(full_d)
  85. def_log.append(d_army)
  86. attack_log.append(a_army)
  87. elif mid_a[0]>full_d[0] and mid_a[1]<=full_d[1]:
  88. a_army -= 1
  89. d_army -= 1
  90. attack_dice_log.append(mid_a)
  91. def_dice_log.append(full_d)
  92. def_log.append(d_army)
  93. attack_log.append(a_army)
  94. elif mid_a[0]<=full_d[0] and mid_a[1]>full_d[1]:
  95. a_army -= 1
  96. d_army -= 1
  97. attack_dice_log.append(mid_a)
  98. def_dice_log.append(full_d)
  99. def_log.append(d_army)
  100. attack_log.append(a_army)
  101.  
  102. #attacker 1 and defender 2
  103. elif 1<a_army<3 and d_army > 2:
  104. if min_a[0]>full_d[0]:
  105. d_army -= 1
  106. attack_dice_log.append(min_a)
  107. def_dice_log.append(full_d)
  108. def_log.append(d_army)
  109. attack_log.append(a_army)
  110. elif min_a[0]<=full_d[0]:
  111. a_army -= 1
  112. attack_dice_log.append(min_a)
  113. def_dice_log.append(full_d)
  114. def_log.append(d_army)
  115. attack_log.append(a_army)
  116.  
  117. #attacker 3 and defender 1
  118. elif a_army > 3 and 0<d_army<2:
  119. if full_a[0]>min_d[0]:
  120. d_army -= 1
  121. attack_dice_log.append(full_a)
  122. def_dice_log.append(min_d)
  123. def_log.append(d_army)
  124. attack_log.append(a_army)
  125. elif full_a[0]<=min_d[0]:
  126. a_army -= 1
  127. attack_dice_log.append(full_a)
  128. def_dice_log.append(min_d)
  129. def_log.append(d_army)
  130. attack_log.append(a_army)
  131.  
  132. #attacker 2 and defender 1
  133. elif 2<a_army<4 and 0<d_army<2:
  134. if mid_a[0]>min_d[0]:
  135. d_army -= 1
  136. attack_dice_log.append(mid_a)
  137. def_dice_log.append(min_d)
  138. def_log.append(d_army)
  139. attack_log.append(a_army)
  140. elif mid_a[0]<=min_d[0]:
  141. a_army -= 1
  142. attack_dice_log.append(mid_a)
  143. def_dice_log.append(min_d)
  144. def_log.append(d_army)
  145. attack_log.append(a_army)
  146.  
  147. #attacker 1 and defender 1
  148. elif 1<a_army<3 and 0<d_army<2:
  149. if min_a[0]>min_d[0]:
  150. d_army -= 1
  151. attack_dice_log.append(min_a)
  152. def_dice_log.append(min_d)
  153. def_log.append(d_army)
  154. attack_log.append(a_army)
  155. elif min_a[0]<=min_d[0]:
  156. a_army -= 1
  157. attack_dice_log.append(min_a)
  158. def_dice_log.append(min_d)
  159. def_log.append(d_army)
  160. attack_log.append(a_army)
  161.  
  162. else:
  163. break
  164.  
  165. #turning lists into arrays
  166. attack_log = np.array(attack_log)
  167. def_log = np.array(def_log)
  168. attack_dice_log = np.array(attack_dice_log)
  169. def_dice_log = np.array(def_dice_log)
  170. #making dataframes, combining all the arrays
  171. total_log = pd.DataFrame({ 'Attacking Army': attack_log, 'Defending Army': def_log, 'Attacking Dice': attack_dice_log, 'Defending Dice': def_dice_log})
  172. total_log.insert(0, 'Iterations',range(len(total_log)))
  173. total_log = total_log[['Iterations','Attacking Army', 'Defending Army', 'Attacking Dice', 'Defending Dice']]
  174. attack_def_log = pd.DataFrame({ 'Attacking Army': attack_log, 'Defending Army': def_log})
  175. #creating variables for regression analysis
  176. a = total_log['Iterations']
  177. b = total_log['Attacking Army']
  178. c = total_log['Defending Army']
  179. #showing main dataframe
  180. total_log
  181.  
  182.  
  183. #attacker regression
  184. slope, intercept, r_value, p_value, std_err = stats.linregress(a,b)
  185. aslope = slope
  186.  
  187. #defender regression
  188. slope, intercept, r_value, p_value, std_err = stats.linregress(a,c)
  189. dslope = slope
  190.  
  191. #adding the slopes to their respective lists for each iteration
  192. a_slopes.append(aslope)
  193. d_slopes.append(dslope)
  194.  
  195. #creating dataframe for the slopes, each row is a new interation's value
  196. slopedf = pd.DataFrame({'Attack Regression': a_slopes, 'Defend Regression': d_slopes})
  197.  
  198. #creating variables to print values from the dataframe
  199. a_std = slopedf['Attack Regression'].std()
  200. d_std = slopedf['Defend Regression'].std()
  201. a_mean = slopedf['Attack Regression'].mean()
  202. d_mean = slopedf['Defend Regression'].mean()
  203.  
  204. print a_std
  205. print d_std
  206. print a_mean
  207. print d_mean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement