def assign_stmt(self): node = Node(self.values[self.iterator]) #node.set_children(Node(self.values[self.iterator])) self.match('identifier') self.match(':=') node.set_children(self.exp()) return node
def add_two_nums_ll(l1: Node, l2: Node) -> Node: root_node = Node(0) current_node = root_node carry = 0 while l1 or l2: sum = carry if l1: sum += l1.val l1 = l1.next if l2: sum += l2.val l2 = l2.next val = sum % 10 carry = sum // 10 current_node.val = val if l1 or l2: current_node.next = Node(None) current_node = current_node.next if carry > 0: current_node.next = Node(1) return root_node
def displayPathtoPrincess(n, grid, start_position): q = deque() check = [] for i in range(n): inner = [0 for j in range(n)] check.append(inner) start = Node(start_position) q.append(start) x, y = start.curr check[x][y] = 1 results = deque() while q: val = q.popleft() x, y = val.curr if grid[x][y] == 'p': results.append(val) possible_moves = possible_moves_for_val(x, y, n) for i in possible_moves: x, y = i if check[x][y] == 0: check[x][y] = 1 child = Node(i) child.parent = val q.append(child) for i in best_result(results): print(i)
def term(self): right_node = self.factor() while self.token=='*' or self.token=='/': op_node = Node(self.values[self.iterator]) self.mulop() op_node.set_children(right_node) right_node = op_node right_node.set_children(self.factor()) return right_node
def term(self): right_node = self.factor() while self.token == '*' or self.token == '/': op_node = Node(self.values[self.iterator]) self.match(self.token, nonTerminal=True) op_node.set_children(right_node) right_node = op_node right_node.set_children(self.factor()) return right_node
def expand_node(node: Node, to_play: Player, actions: List[Action], network_output: NetworkOutput): node.to_play = to_play node.hidden_state = network_output.hidden_state node.reward = network_output.reward policy = {a: math.exp(network_output.policy_logits[a]) for a in actions} policy_sum = sum(policy.values()) for action, p in policy.items(): node.children[action] = Node(p / policy_sum)
def repeat_stmt(self): repeat_node = Node(self.values[self.iterator]) if self.token == 'repeat': self.match('repeat') repeat_node.set_children(self.stmt_sequence()) self.match('until') until_node = Node('until') until_node.set_children(self.exp()) repeat_node.set_children(until_node) return repeat_node
def factor(self): if self.token=='(': self.match('(') node = self.exp() self.match(')') elif self.token=='number': node = Node(self.values[self.iterator]) self.match('number') elif self.token=='identifier': node = Node(self.values[self.iterator]) self.match('identifier') else: self.clear_tables() raise ValueError(self.token) return None return node
def play_game(config: MuZeroConfig, network: Network, train: bool = True) -> Game: game = config.new_game() mode_action_select = 'softmax' if train else 'max' while not game.terminal() and len(game.history) < config.max_moves: # At the root of the search tree we use the representation function to # obtain a hidden state given the current observation. root = Node(0) current_observation = game.make_image(-1) expand_node(root, game.to_play(), game.legal_actions(), network.initial_inference(current_observation)) if train: add_exploration_noise(config, root) # We then run a Monte Carlo Tree Search using only action sequences and the # model learned by the networks. run_mcts(config, root, game.action_history(), network) action = select_action(config, len(game.history), root, network, mode=mode_action_select) game.apply(action) game.store_search_statistics(root) return game
def generate_children(node, open_list, visited, open_list_hashed, puzzle): for i in range(puzzle['size'] * puzzle['size']): temp_board = helper.flip_dot(i, puzzle, node.board_state) if temp_board not in visited and temp_board not in open_list_hashed: heapq.heappush( open_list, Node(helper.compute_h_simple(temp_board, puzzle['size']), temp_board, node, i, True)) open_list_hashed.add(temp_board)
def ucb_score(config: MuZeroConfig, parent: Node, child: Node, min_max_stats: MinMaxStats) -> float: pb_c = math.log((parent.visit_count + config.pb_c_base + 1) / config.pb_c_base) + config.pb_c_init pb_c *= math.sqrt(parent.visit_count) / (child.visit_count + 1) prior_score = pb_c * child.prior value_score = min_max_stats.normalize(child.value()) return prior_score + value_score
def __init__(self, width, height, m_start, m_goal, wall_arr): # def __init__(self): self.visited = [] self.maze_width = width # number of columns self.maze_height = height # number of rows self.start = m_start self.goal = m_goal self.walls = wall_arr self.explored_tracks = [] self.frontier = QueueFrontier() start_node = Node(self.start, None, None) self.frontier.add(start_node)
def simple_exp(self): left_node = self.term() while self.token == '+' or self.token == '-': op_node = Node(self.values[self.iterator]) self.match(self.token, nonTerminal=True) op_node.set_children(left_node) op_node.set_children(self.term()) left_node = op_node return left_node
def exp(self): left_node=self.simple_exp() if self.token=='<' or self.token=='>' or self.token=='=': op_node=Node(self.values[self.iterator]) self.comparison_op() op_node.set_children(left_node) op_node.set_children(self.simple_exp()) left_node = op_node return left_node
def simple_exp(self): left_node=self.term() while self.token=='+' or self.token=='-': op_node = Node(self.values[self.iterator]) self.addop() op_node.set_children(left_node) op_node.set_children(self.term()) left_node = op_node return left_node
def stmt_sequence(self): start_node = Node(' ') start_node.set_children(self.statement()) end_if = self.types[self.iterator-1] == 'end' while self.token==';' or end_if: if not end_if: if not self.match(';'): break next_node=self.statement() if next_node == None: break else: start_node.set_children(next_node) end_if = self.types[self.iterator-1] == 'end' return start_node
def search(puzzle): print('Solving puzzle #', puzzle['id']) visited = set() search_path = deque() open_list = [ Node(helper.compute_h_simple(puzzle['board'], puzzle['size']), puzzle['board'], None, None, True) ] open_list_hashed = set() open_list_hashed.add(open_list[0].board_state) heapq.heapify(open_list) max_length = puzzle['max_l'] while len(open_list) > 0 and max_length >= 0: max_length -= 1 next_to_visit = heapq.heappop(open_list) open_list_hashed.remove(next_to_visit.board_state) # add to visited visited.add(next_to_visit.board_state) search_path.append(next_to_visit) # check for win condition if helper.is_win(next_to_visit.board_state): puzzle['is_solved'] = True break # expand children nodes generate_children(next_to_visit, open_list, visited, open_list_hashed, puzzle) if not puzzle['is_solved']: next_to_visit = None helper.write_to_file_node( str(puzzle['id']) + "_astar", puzzle['size'], next_to_visit, search_path)
def find_neighbours(self, n): neighbour_list = [] row, column = n.state # not looking at the diagonal neighbours neighbour_options = [ ((row, column + 1), "RIGHT"), # neighbour right ((row - 1, column), "TOP"), # neighbour top ((row, column - 1), "LEFT"), # neighbour left ((row + 1, column), "BOTTOM") ] # neighbiut bottom for option in neighbour_options: new_pos, action = option # new_pos[0] -> row -> (height) # new_pos[1] -> column -> (width) if ((new_pos[0] <= self.maze_height and new_pos[1] <= self.maze_width) and (new_pos[0] > 0 and new_pos[1] > 0) and not self.walls[new_pos[0] - 1][new_pos[1] - 1]): neighbour_list.append(Node(new_pos, n, action)) #neighbour right return neighbour_list
def add_tail(list_head, val): current = list_head while current.next != None: current = current.next current.next = Node(val)
from helper import Node def add_tail(list_head, val): current = list_head while current.next != None: current = current.next current.next = Node(val) def print_list(list_head): current = list_head while current != None: print(current.content) current = current.next firstHead = Node(3) secondNode = Node(4) thirdNode = Node(5) firstHead.next = secondNode secondNode.next = thirdNode add_tail(firstHead, 6) print_list(firstHead)
def if_stmt(self): if_node=Node(self.values[self.iterator]) if self.token=='if': self.match('if') exp_node = Node(' ') exp_node.set_children(self.exp()) if_node.set_children(exp_node) self.match('then') then_node = Node('then') then_node.set_children(self.stmt_sequence()) if_node.set_children(then_node) if self.token=='else': self.match('else') else_node = Node('else') else_node.set_children(self.stmt_sequence()) if_node.set_children(else_node) self.match('end') return if_node
def write_stmt(self): node=Node(self.values[self.iterator]) self.match('write') node.set_children(self.exp()) return node
def read_stmt(self): node=Node(self.values[self.iterator]) self.match('read') node.set_children(Node(self.values[self.iterator])) self.match('identifier') return node
current = list_head while current != None: print(current.content) current = current.next def merge(train1, train2): current = train1 while current.next != None: current = current.next current.next = train2 return train1 train1Head = Node(5) train1node2 = Node(4) train1node3 = Node(3) train1Head.next = train1node2 train1node2.next = train1node3 train2Head = Node(6) train2node2 = Node(5) train2node3 = Node(4) train2Head.next = train2node2 train2node2.next = train2node3 merged = merge(train1Head, train2Head) final = sort_asc(merged)
def add_two_nums_ll(l1: Node, l2: Node) -> Node: return Node(0)