Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import search
  2.  
  3. y = 0
  4. ###################################minimax###############################
  5. def minimax_decision(state): #returns an action
  6. v = -99999
  7. v2 = 0
  8. for a in actions(state):
  9. temp = mm_min_value(result(a, state))
  10. if temp > v:
  11. v= temp
  12. v2 = a
  13. return v, v2
  14.  
  15. def mm_max_value(state): #returns a utility value
  16. if terminal_test(state):
  17. return utility(state)
  18. v = -3
  19. for a,s in successors(state):
  20. v = max(v, mm_min_value(s))
  21. return v
  22.  
  23. def mm_min_value(state):
  24. if terminal_test(state):
  25. return utility(state)
  26. v = 3
  27. for a,s in successors(state):
  28. v = min(v, mm_max_value(s))
  29. return v
  30. #########################################################################
  31.  
  32. X=1
  33. O=-1
  34.  
  35.  
  36. def actions(state):
  37. for i in range(9):
  38. if state[i] is None:
  39. yield i
  40.  
  41. def result(a, state):
  42. newstate = state.copy()
  43. tomove = state[9]
  44. newstate[a] = tomove
  45. newstate[9] = -tomove
  46. return newstate
  47.  
  48. def successors(state):
  49. for a in actions(state):
  50. s = result(a, state)
  51. yield (a, s)
  52.  
  53. def terminal_test(state):
  54. acts = list(actions(state))
  55. global y
  56. y += 1
  57. if not acts:
  58. return True
  59. value = utility(state)
  60. if value != 0:
  61. return True
  62. return False
  63.  
  64. def utility(state):
  65. triples = [(0, 1, 2),
  66. (3, 4, 5),
  67. (6, 7, 8),
  68. (0, 3, 6),
  69. (1, 4, 7),
  70. (2, 5, 8),
  71. (0, 4, 8),
  72. (6, 4, 2)]
  73. for a, b, c in triples:
  74. if state[a] is None: continue
  75. if (state[a] == state[b] and
  76. state[b] == state[c]):
  77. if state[a] == MYSIDE:
  78. return 1
  79. else:
  80. return -1
  81. return 0
  82.  
  83. algus1 = [ X, O, X,
  84. O, None, O,
  85. None, None, None,
  86. X]
  87. algus = [None, None, None,
  88. None, None, None,
  89. None, None, None,
  90. X]
  91.  
  92. MYSIDE = X
  93. print(y)
  94. print(alpha_beta_decision(algus))
  95. print(y)
  96. y=0
  97. print(minimax_decision(algus))
  98. print(y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement