def mctsNormal(self, board): root = Node(None, None, None, self.opponent.color) # set initial child layer initial_moves = board.getAllValidMoves(self.pieces) if initial_moves == []: return None for move in initial_moves: #consider using piece size as first play urgency as initialVal child = Node(move, root, self.color, initialVal=move[0].size() * 10) root.addChild(child) num_simulations = 0 startTime = time.perf_counter() while (self.resourcesAvailable(startTime)): #selection phase currentNode = Node.getMctsLeaf(root) #make board accurate for current state board.makeMovesInStack(currentNode.moveStack) #expansion phase useable_pieces = [] if currentNode.turnColor == self.color: # opponents turn, use their pieces useable_pieces = self.opponent.getPiecesNotIn( currentNode.moveStack) else: #our turn, use our pieces useable_pieces = self.getPiecesNotIn(currentNode.moveStack) valid_moves = board.getAllValidMoves(useable_pieces) if valid_moves == []: #game in terminal moveStack for this player pass else: # add all substates for move in valid_moves: child = Node(move, currentNode, move[0].color, initialVal=move[0].size() * 10) currentNode.addChild(child) #simulation phase res = self.playoutRandomGame(currentNode, board) num_simulations += 1 #board is returned to initial state #backpropogate currentNode.backpropogate(res) #select highest val node node = Node.getMaxFirstLayer(root) #Node.delTree(root) #print(num_simulations, " simulations executed") return node.move
def makeTree(ts, tree): while True: if (len(ts) == 0): return (ts, tree) (key, value), *ts = ts if key == "function_definition": print("Funciton Definition") funcTree = Node("function_defenition").addChild(Node(value)) funcParams = Node("parameters") while True: if ts[0][0] != "parameter_definition": break (key, value), *ts = ts funcParams.addChild(Node(value)) funcTree.addChild(funcParams) (key, value), *ts = ts if key != "statement_start": raise ValueError("Function bondy is not a Statement '(' expected") statementTree = Node("statement") (ts, statementTree) = makeTree(ts, statementTree) funcTree.addChild(statementTree) tree.addChild(funcTree) elif key == "statement_start": print("Statement") statementTree = Node("statement") (ts, statementTree) = makeTree(ts, statementTree) tree.addChild(statementTree) elif key == "statement_end": return (ts, tree) elif key == "function_call": funcTree = Node("function_call") funcTree.addChild(Node(value)) while True: if ts[0][0] != "additional_parameter": break (key, value), *ts = ts (key, value), *ts = ts if key != "statement_start": raise ValueError("Function call parameter is not a Statement '(' expected") statementTree = Node("statement") (ts, statementTree) = makeTree(ts, statementTree) funcTree.addChild(statementTree) tree.addChild(funcTree) elif key == "identifier": tree.addChild(Node("identifier").addChild(Node(value))) elif key == "string_literal": tree.addChild(Node("string_literal").addChild(Node(value))) else: raise ValueError("Something went wrong identifier: " + key)