def _search(grid=grid, init=init, goal=goal): unchecked = [[0, None] + init] checked = [] expansion = [[-1 for col in range(len(grid[0]))] for row in range(len(grid))] expansion_index = 0 actions = [[-1 for col in range(len(grid[0]))] for row in range(len(grid))] while unchecked: # sort by g-value unchecked.sort(key=lambda x: x[0], reverse=True) # expand g, action, y, x = unchecked.pop() checked.append([y, x]) actions[y][x] = action expansion[y][x] = expansion_index expansion_index += 1 if [y, x] == goal: return [g, y, x], actions for action, dt in enumerate(delta): pos = move(grid, [y, x], dt) if pos and pos not in checked: node = [g + 1, action] + pos # use a set for this? if node not in unchecked: unchecked.append(node) return FAIL, actions
def _search(grid=grid, init=init, goal=goal): open_nodes = [[0] + init] checked_nodes = [] expansion = [[-1 for col in range(len(grid[0]))] for row in range(len(grid))] expansion_index = 0 while open_nodes: # sort by g-value open_nodes.sort(key=lambda x: x[0], reverse=True) # expand g, y, x = open_nodes.pop() checked_nodes.append([y, x]) expansion[y][x] = expansion_index expansion_index += 1 if [y, x] == goal: return [g, y, x], expansion for d in delta: t = move(grid, [y, x], d) if t and t not in checked_nodes: new_node = [g + 1] + t # use a set for here? if new_node not in open_nodes: open_nodes.append(new_node) return 'fail', expansion
def _optimum_policy(grid=grid, goal=goal): value = [[99 for col in range(len(grid[0]))] for row in range(len(grid))] policy = [[' ' for col in range(len(grid[0]))] for row in range(len(grid))] y, x = goal value[y][x] = 0 policy[y][x] = '*' updated = True while updated: updated = False for y in xrange(len(grid)): for x in xrange(len(grid[0])): if [y, x] != goal and grid[y][x] == 0: for index, action in enumerate(delta): pos = move(grid, [y, x], action) if pos: v = value[pos[0]][pos[1]] + cost_step if v < value[y][x]: value[y][x] = v policy[y][x] = delta_name[index] updated = True return value, policy
def _search(grid=grid, init=init, goal=goal): y, x = init g = 0 h = heuristic[y][x] f = g + h # action = None # unchecked = [[f, g, h, action, y, x]] unchecked = [[f, g, y, x]] checked = [[0 for col in range(len(grid[0]))] for row in range(len(grid))] expansion = [[-1 for col in range(len(grid[0]))] for row in range(len(grid))] expansion_index = 0 # actions = [[-1 for col in range(len(grid[0]))] # for row in range(len(grid))] while unchecked: # sort by g-value unchecked.sort(reverse=True) # expand # f, g, h, action, y, x = unchecked.pop() # print f, g, h, action, y, x f, g, y, x = unchecked.pop() checked[y][x] = 1 # actions[y][x] = action expansion[y][x] = expansion_index expansion_index += 1 if [y, x] == goal: return [g, y, x], expansion for action, dt in enumerate(delta): pos = move(grid, [y, x], dt) if pos: y1, x1 = pos if not checked[y1][x1]: g1 = g + cost h = heuristic[y1][x1] # node = [f, g1, h, action] + pos node = [g1 + h, g1, y1, x1] # use a set for this? if node not in unchecked: unchecked.append(node) return FAIL, expansion
def _compute_value(grid=grid, goal=goal): value = [[99 for col in range(len(grid[0]))] for row in range(len(grid))] value[goal[0]][goal[1]] = 0 updated = True while updated: updated = False for y in xrange(len(grid)): for x in xrange(len(grid[0])): if [y, x] != goal and grid[y][x] == 0: for action in delta: pos = move(grid, [y, x], action) if pos: v = value[pos[0]][pos[1]] + cost_step if v < value[y][x]: value[y][x] = v updated = True return value