def result(self, state, action): _, food_left = state food_left = tuple(food for food in food_left if food != action) return (action, food_left) def cost(self, state, action, state2): return 1 def print_state_representation(self, state): rat, food = state elements_to_print = { "R": [rat], "C": food, "XXX": WALLS_POSTITIONS, "E": [(3, 5)] } print_grid(ROWS, COLUMNS, elements_to_print) metodos = ( breadth_first, depth_first, iterative_limited_depth_first, uniform_cost, ) for metodo_busqueda in metodos: try_search_method(metodo_busqueda, RatabotsProblem, INITIAL_STATE)
class Laberinto(SearchProblem): def is_goal(self, state): return state == GOAL_STATE def actions(self, state): return tuple(DOORS[state]) def result(self, state, action): return action def cost(self, state, action, state2): return 1 def print_state_representation(self, state): state_str = str(state).rjust(2, '0') print(TEMPLATE.replace(state_str, "XX")) methods = ( breadth_first, depth_first, iterative_limited_depth_first, uniform_cost, ) for search_method in methods: try_search_method(search_method, Laberinto, INITIAL_STATE)
(row, column) for row, column in adjacent_positions(*state) if not enemy_can_attack(row, column) and 0 <= row < BOARD_SIZE and 0 <= column < BOARD_SIZE ] return posible_actions def result(self, state, action): return action def cost(self, state, action, state2): return 1 def print_state_representation(self, state): elements = { "K": [state], "X": ENEMIES, } print_grid(BOARD_SIZE, BOARD_SIZE, elements) methods = ( breadth_first, depth_first, iterative_limited_depth_first, uniform_cost, ) for search_method in methods: try_search_method(search_method, HnefataflProblem, INITIAL_STATE)
monkey_now, push = action if push: direction = monkey_now - monkey_before chair_now = chair + direction return (monkey_now, chair_now) return (monkey_now, chair) def cost(self, state, action, action2): return 1 def print_state_representation(self, state): monkey, chair = state elements = { "M": [(0, monkey)], "C": [(0, chair)], "B": [(0, BANANA_POSITION)], } print_grid(1, ROOM_SLOTS, elements) methods = ( breadth_first, depth_first, iterative_limited_depth_first, uniform_cost, ) for search_method in methods: try_search_method(search_method, MonoProblem, INITIAL_STATE)
return tuple(state) def cost(self, state, action, state2): return 1 def heuristic(self, state): water_exceeded_vases = [1 for vase_liters in state if vase_liters > 1] water_missing_vases = [1 for vase_liters in state if vase_liters < 1] return max(len(water_missing_vases), len(water_exceeded_vases)) def print_state_representation(self, state): elements = defaultdict(list) for vase_position, vase_content in enumerate(state): elements["X" * vase_content].append((0, vase_position)) print_grid(1, VASES, elements, VASES) methods = ( # breadth_first, # depth_first, # iterative_limited_depth_first, # uniform_cost, greedy, astar, ) for search_method in methods: try_search_method(search_method, JarrosProblem, INITIAL_STATE)
print("the elements are:", elements) print("the state is:", state) print_grid(1, 3, elements) methods = ( # breadth_first, # depth_first, # iterative_limited_depth_first, # uniform_cost, greedy, astar, ) for search_method in methods: try_search_method(search_method, NaveAlienigena, INITIAL_STATE) """ Iteraciones con astar: GOAL_STATE = (5, 1, 8) STATE REFERENCES: +----+-------------+-----------------+-----------------------------+------------------+ | | STATE | ACUMULATED COST | HEURISTIC | ACUM + ESTIMATED | | | | |n0+3|n0-2 | CALC | COST| | +----+-------------+-----------------+----+-----+------------+-----+------------------+ | N1 | (0, 0, 0) | 0 | 3 | -2 | (2+2.5+3) | 7.5 | 7.5 | +----+-------------+-----------------+----+-----+------------+-----+------------------+ | N2 | (3, 0, 0) | 1 | 6 | 1 | 2+1.5+3 | 6.5 | 7.5 | +----+-------------+-----------------+----+-----+------------+-----+------------------+
return cost def print_state_representation(self, state): cell_size = len(ALL_PEOPLE * 4) + 2 cells = 5 left, right, torch_side, time = state elements = { str(left): [(0, 0)], str(right): [(0, cells - 1)], "T": [(0, 1) if torch_side == 0 else (0, cells - 2)], str(time): [(0, round(cells / 2))], } print_grid(1, cells, elements, cell_size) methods = ( breadth_first, depth_first, iterative_limited_depth_first, uniform_cost, greedy, astar, ) for search_method in methods: try_search_method(search_method, BridgeProblem, INITIAL_STATE)
return estimated_cost def print_state_representation(self, state): position, samples, helicopter_launched = state if helicopter_launched: robot_symbol = "R" else: robot_symbol = "RH" elements = { "X": INTERESTING_ZONES, "O": OBSTACLES, "S": samples, robot_symbol: [position], } print_grid(4, 4, elements) methods = ( # breadth_first, # depth_first, # iterative_limited_depth_first, # uniform_cost, # greedy, astar, ) for search_method in methods: try_search_method(search_method, PerseveranceProblem, INITIAL_STATE)
def result(self, state, action): _, available_positions = state available_positions = list(available_positions) available_positions.remove(action) available_positions = tuple(available_positions) return (action, available_positions) def cost(self, state, action, state2): return 1 def print_state_representation(self, state): horse, available_positions = state elements = { "C": horse, "X": available_positions, } print_grid(ROWS, COLUMNS, elements) methods = ( breadth_first, depth_first, iterative_limited_depth_first, uniform_cost, ) for search_method in methods: try_search_method(search_method, CaballeroProblem, INITIAL_STATE)
# attack enemy hero if enemy_hero == action: return (player_hero, None, enemy_base) # move player hero return (action, enemy_hero, enemy_base) def cost(self, state, action, state2): return 1 def print_state_representation(self, state): hero, enemy, base = state elements = {"H": [hero]} if enemy: elements["He"] = [enemy] if base: elements["Be"] = [base] print_grid(ROWS, COLUMNS, elements) methods = ( breadth_first, depth_first, iterative_limited_depth_first, uniform_cost, ) for search_method in methods: try_search_method(search_method, DotaProblem, INITIAL_STATE)