Guest User

Untitled

a guest
Mar 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.04 KB | None | 0 0
  1. 10
  2. /
  3. 6 14
  4. / /
  5. 5 8 11 18
  6.  
  7. def playout(board):
  8. '''from Tristan Cazenave's algorithm'''
  9. moves = np.zeros(2)# shoudln't we get them from the board ?
  10. while(True):
  11. nb,moves = board.legalMoves(moves)
  12. if((nb == 0) or board.terminal()):
  13. return board.score()
  14. n = random.randint(0, nb) # chose a number between 0 and the number of legal moves
  15. board.play(moves[n]) # play a random move
  16. if(board.length >= MaxPlayoutLength -20):
  17. return 0
  18.  
  19. def nested(board, n):
  20. '''Nested Monte Carlo algorithm
  21. a general name for a broad class of algorithms that use random sampling to obtain numerical results.
  22. It is used to solve statistical problems by simulation.'''
  23.  
  24. nbMoves = 0
  25. moves = np.zeros(2)
  26.  
  27. lengthBestRollout[n] -1
  28. scoreBestRollout[n] - DBL_MAX
  29. res = -DBL_MAX
  30. while(True):
  31. # we test wether we reached the bottom of the tree
  32. if(board.terminal()):
  33. return 0.0 # but why should it be 0.0 ?
  34. # we get the number of moves and moves we can go from the selected nodes
  35. nbMoves,moves = board.legalMoves(moves)
  36. # ???
  37. for i in range(0,nbMoves):
  38. moves = list(filter (lambda a: a != 0.0, moves))
  39. print("moves : ")
  40. print(moves)
  41. b = board
  42. b.play(moves[i])
  43. if(n==1):
  44. playout(board)
  45. else:
  46. nested (board, n-1)
  47. score = board.score()
  48. if(score > scoreBestRollout [n]):
  49. scoreBestRollout [n] = score
  50. lengthBestRollout [n] = board.length
  51. for k in range(0,board.length):
  52. bestRollout[n][k]=board.rollout[k]
  53. if(n>3):
  54. for i in range(0,t<n-1):
  55. print("n =", n,"progress =", board.length, "score =", scoreBestRollout [n])
  56. depth = 0
  57. # board.print(stderr) # what
  58. print("")
  59. bestBoard = board
  60. if ((n > 1) and (score > bestScoreNested)):
  61. bestScoreNested = score
  62. print("best score = ", score)
  63. print("")
  64. bestBoard = board
  65. print("nbMoves = ",nbMoves,"so we're going to play", bestRollout[n][board.length])
  66. board.play(bestRollout[n][board.length])
  67. #board.play(5)
  68. print()
  69. return 0.0
  70.  
  71. # Attempt to apply a Nested Monte Carlo Algorithm to binary trees
  72.  
  73. import random
  74. from random import randint
  75. import numpy as np
  76.  
  77. MaxPlayoutLength = 20 # what ?
  78.  
  79. # Class for construct the nodes of the tree. (Subtrees)
  80. class Node:
  81. def __init__(self, key, parent_node = None):
  82. self.left = None
  83. self.right = None
  84. self.key = key
  85. if parent_node == None:
  86. self.parent = self
  87. else:
  88. self.parent = parent_node
  89.  
  90. # Class with the structure of the tree.
  91. # I'm not sure if this Tree is balanced.
  92. class Tree:
  93. def __init__(self):
  94. self.root = None
  95.  
  96. # Insert a single element
  97. def insert(self, x):
  98. if(self.root == None):
  99. self.root = Node(x)
  100. else:
  101. self._insert(x, self.root)
  102.  
  103. # place it at the right palce
  104. def _insert(self, x, node):
  105. if(x < node.key):
  106. if(node.left == None):
  107. node.left = Node(x, node)
  108. else:
  109. self._insert(x, node.left)
  110. else:
  111. if(node.right == None):
  112. node.right = Node(x, node)
  113. else:
  114. self._insert(x, node.right)
  115.  
  116. # Given a element, return a node in the tree with key x.
  117. def find(self, x):
  118. if(self.root == None):
  119. return None
  120. else:
  121. return self._find(x, self.root)
  122.  
  123. def _find(self, x, node):
  124. if(x == node.key):
  125. return node
  126. elif(x < node.key):
  127. if(node.left == None):
  128. return None
  129. else:
  130. return self._find(x, node.left)
  131. elif(x > node.key):
  132. if(node.right == None):
  133. return None
  134. else:
  135. return self._find(x, node.right)
  136.  
  137. # Given a node, return the node in the tree with the next largest element.
  138. def next(self, node):
  139. if node.right != None:
  140. return self._left_descendant(node.right)
  141. else:
  142. return self._right_ancestor(node)
  143.  
  144. def _left_descendant(self, node):
  145. if node.left == None:
  146. return node
  147. else:
  148. return self._left_descendant(node.left)
  149.  
  150. def _right_ancestor(self, node):
  151. if node.key <= node.parent.key:
  152. return node.parent
  153. else:
  154. return self._right_ancestor(node.parent)
  155.  
  156. # Delete an element of the tree
  157. def delete(self, x):
  158. node = self.find(x)
  159. if node == None:
  160. print(x, "isn't in the tree")
  161. else:
  162. if node.right == None:
  163. if node.left == None:
  164. if node.key < node.parent.key:
  165. node.parent.left = None
  166. del node # Clean garbage
  167. else:
  168. node.parent.right = None
  169. del Node # Clean garbage
  170. else:
  171. node.key = node.left.key
  172. node.left = None
  173. else:
  174. x = self.next(node)
  175. node.key = x.key
  176. x = None
  177.  
  178. # a tree with a selected node at a given time
  179. class Board:
  180. '''a Board is a tree with a node selected which gives a score'''
  181. def __init__(self, btree):
  182. self.tree = btree
  183. self.root = btree.root
  184. self.root.left = btree.root.left
  185. self.root.right = btree.root.right
  186. self.length = 0 # length = NULL; //TO-DO : number of nodes which have leaves BUT how to count them ?
  187.  
  188. print("Board initialized")
  189. print("root :")
  190. print(self.root.key)
  191.  
  192. if(btree.root.left is not None):
  193. print("btree.root.left.key")
  194. print(btree.root.left.key)
  195.  
  196. if(btree.root.right is not None):
  197. print("btree.root.right.key")
  198. print(btree.root.right.key)
  199.  
  200.  
  201.  
  202. moves = np.zeros(2)
  203. if(btree.root.left != None):
  204. self.moves[0] = btree.root.left
  205. self.moves[1] = btree.root.right
  206. score = btree.root.key
  207.  
  208. def legalMoves(self, moves):
  209. '''Assign the number of legal moves from root and gives the number of legal Moves'''
  210. numberLegal = 0
  211.  
  212. # we assign the array of possible moves
  213. if(self.root.left is not None):
  214. moves[0] = self.root.left.key
  215. if(self.root.right is not None):
  216. moves[1] = self.root.right.key
  217.  
  218. # we get the number of legalMoves
  219. for i in range(0,len(moves)):
  220. if(moves[i] != 0):
  221. numberLegal = numberLegal+1
  222.  
  223. # and we give back the number of possible moves and the moves theselves
  224. return numberLegal, moves
  225.  
  226. def terminal(self):
  227. '''Test wether we reached the end of a branch of a tree'''
  228. if((self.root.left == None) and (self.root.right == None)):
  229. print("board terminal")
  230. return True
  231. else:
  232. return False
  233.  
  234. def score(self):
  235. '''Gives the value of the root'''
  236. return self.root.key
  237.  
  238. def play(self,key):
  239. '''chose the next node we dive into, if legal :
  240. - node : next node the player wants to dive into
  241. '''
  242. # no test for the moment, let's see if it can make it
  243. node = self.tree.find(key)
  244. print("key we try to play",key)
  245. if(node is not None):
  246. self.root = node
  247. self.root.left = node.left
  248. self.root.right = node.right
  249. print("BOARD HAS CHANGED")
  250. else:
  251. print("Trying to play a key which doesn't belong to the tree")
  252. print(key)
  253.  
  254. # length = NULL; //TO-DO : number of nodes which have leaves BUT how to count them ?
  255.  
  256. def playout(board):
  257. '''from Tristan Cazenave's algorithm'''
  258. moves = np.zeros(2)# shoudln't we get them from the board ?
  259. while(True):
  260. nb,moves = board.legalMoves(moves)
  261. if((nb == 0) or board.terminal()):
  262. return board.score()
  263. print("nb",nb)
  264. n = random.randint(0, nb) # chose a number between 0 and the number of legal moves
  265. board.play(moves[n]) # play a random move
  266. if(board.length >= MaxPlayoutLength -20):
  267. return 0
  268.  
  269. bestScoreNested = -9999
  270. DBL_MAX = -9999
  271.  
  272. arraySize = 10
  273.  
  274. lengthBestRollout = np.zeros(10) # array of size 10
  275. scoreBestRollout = np.zeros(10) # array of size 10
  276.  
  277. bestRollout =np.zeros((10,MaxPlayoutLength)) # 2 dimensional array of size 10*MaxPlayoutLength
  278.  
  279. def nested(board, n):
  280. '''Nested Monte Carlo algorithm
  281. a general name for a broad class of algorithms that use random sampling to obtain numerical results.
  282. It is used to solve statistical problems by simulation.'''
  283.  
  284. nbMoves = 0
  285. moves = np.zeros(2)
  286.  
  287. lengthBestRollout[n] -1
  288. scoreBestRollout[n] - DBL_MAX
  289. res = -DBL_MAX
  290. while(True):
  291. # we test wether we reached the bottom of the tree
  292. if(board.terminal()):
  293. return 0.0 # but why should it be 0.0 ?
  294. # we get the number of moves and moves we can go from the selected nodes
  295. nbMoves,moves = board.legalMoves(moves)
  296. # ???
  297. for i in range(0,nbMoves): # what ???? If I have one move we turn two times ???
  298. moves = list(filter (lambda a: a != 0.0, moves))
  299. print("moves : ")
  300. print(moves)
  301. b = board
  302. b.play(moves[i])
  303. if(n==1):
  304. playout(board)
  305. else:
  306. nested (board, n-1)
  307. score = board.score()
  308. if(score > scoreBestRollout [n]):
  309. scoreBestRollout [n] = score
  310. lengthBestRollout [n] = board.length
  311. for k in range(0,board.length):
  312. bestRollout[n][k]=board.rollout[k]
  313. if(n>3):
  314. for i in range(0,t<n-1):
  315. print("n =", n,"progress =", board.length, "score =", scoreBestRollout [n])
  316. depth = 0
  317. # board.print(stderr) # what
  318. print("")
  319. bestBoard = board
  320. if ((n > 1) and (score > bestScoreNested)):
  321. bestScoreNested = score
  322. print("best score = ", score)
  323. print("")
  324. bestBoard = board
  325. print("nbMoves = ",nbMoves,"so we're going to play", bestRollout[n][board.length])
  326. board.play(bestRollout[n][board.length])
  327. #board.play(5)
  328. print()
  329. return 0.0
  330.  
  331. if __name__ == "__main__":
  332. # tests
  333. t = Tree()
  334. t.insert(5)
  335. t.insert(6)
  336. t.insert(8)
  337. t.insert(10)
  338. t.insert(11)
  339. t.insert(14)
  340. t.insert(18)
  341.  
  342. b = Board(t)
  343.  
  344. score = nested(b,3)
  345. print("the algorithm score is ",score)
  346.  
  347. # Remember: Find method return the node object.
  348. # To return a number use t.find(nº).key
  349. # But it will cause an error if the number is not in the tree.
  350.  
  351. Board initialized
  352. root :
  353. 5
  354. btree.root.right.key
  355. 6
  356. moves :
  357. [6.0]
  358. key we try to play 6.0
  359. BOARD HAS CHANGED
  360. moves :
  361. [8.0]
  362. key we try to play 8.0
  363. BOARD HAS CHANGED
  364. moves :
  365. [10.0]
  366. key we try to play 10.0
  367. BOARD HAS CHANGED
  368. key we try to play 0.0
  369. Trying to play a key which doesn't belong to the tree
  370. 0.0
  371. nbMoves = 1 so we're going to play 0.0
  372. key we try to play 0.0
  373. Trying to play a key which doesn't belong to the tree
  374. 0.0
  375.  
  376. Traceback (most recent call last):
  377. File "main.py", line 273, in <module>
  378. score = nested(b,3)
  379. File "main.py", line 235, in nested
  380. nested (board, n-1)
  381. File "main.py", line 235, in nested
  382. nested (board, n-1)
  383. File "main.py", line 224, in nested
  384. nbMoves,moves = board.legalMoves(moves)
  385. File "main.py", line 146, in legalMoves
  386. moves[1] = self.root.right.key
  387. IndexError: list assignment index out of range
Add Comment
Please, Sign In to add comment