Untitled
gorazd
python
03/18/2026 12:56 PM
4.9 KB
13
Indexable
from searching_framework import *
class Robot(Problem):
def __init__(self, initial_state, goal_state, wall_positions, machine1_pos, machine2_pos,
machine1_steps, machine2_steps, machine1_parts, machine2_parts):
super().__init__(initial_state, goal_state)
self.walls = wall_positions
self.machine1_pos = machine1_pos
self.machine2_pos = machine2_pos
self.machine1_steps = machine1_steps
self.machine2_steps = machine2_steps
self.machine1_parts = machine1_parts
self.machine2_parts = machine2_parts
def successor(self, state):
pos_x, pos_y, collected_parts, m1_fixed, repair_counter = state
next_states = {}
moves = {
"Up": (0, 1), "Down": (0, -1),
"Left": (-1, 0), "Right": (1, 0)
}
# Movement actions
for move, (dx, dy) in moves.items():
new_x, new_y = pos_x + dx, pos_y + dy
if 0 <= new_x < 10 and 0 <= new_y < 10 and (new_x, new_y) not in self.walls:
updated_parts = collected_parts
if not m1_fixed and (new_x, new_y) in self.machine1_parts:
if (new_x, new_y) not in collected_parts:
updated_parts = collected_parts + ((new_x, new_y),)
elif m1_fixed and (new_x, new_y) in self.machine2_parts:
if (new_x, new_y) not in collected_parts:
updated_parts = collected_parts + ((new_x, new_y),)
next_states[move] = (new_x, new_y, updated_parts, m1_fixed, 0)
# Repair action
if not m1_fixed:
if (pos_x, pos_y) == self.machine1_pos and \
len(collected_parts) == len(self.machine1_parts) and \
set(collected_parts) == set(self.machine1_parts):
new_counter = repair_counter + 1
if new_counter == self.machine1_steps:
next_states["Repair"] = (pos_x, pos_y, (), True, 0)
else:
next_states["Repair"] = (pos_x, pos_y, collected_parts, False, new_counter)
else:
if (pos_x, pos_y) == self.machine2_pos and \
len(collected_parts) == len(self.machine2_parts) and \
set(collected_parts) == set(self.machine2_parts):
new_counter = repair_counter + 1
next_states["Repair"] = (pos_x, pos_y, collected_parts, True, new_counter)
return next_states
def actions(self, state):
return self.successor(state).keys()
def result(self, state, action):
return self.successor(state)[action]
def goal_test(self, state):
_, _, _, m1_fixed, repair_counter = state
return m1_fixed and repair_counter == self.machine2_steps
if __name__ == '__main__':
start_pos = tuple(map(int, input().split(',')))
machine1_pos = tuple(map(int, input().split(',')))
machine1_steps = int(input())
machine2_pos = tuple(map(int, input().split(',')))
machine2_steps = int(input())
num_parts_m1 = int(input())
machine1_parts = tuple([tuple(map(int, input().split(','))) for _ in range(num_parts_m1)])
num_parts_m2 = int(input())
machine2_parts = tuple([tuple(map(int, input().split(','))) for _ in range(num_parts_m2)])
walls = [(4,0),(5,0),(7,5),(8,5),(9,5),(1,6),(1,7),(0,6),(0,8),(0,9),(1,9),(2,9),(3,9)]
initial_state = (start_pos[0], start_pos[1], (), False, 0)
problem = Robot(initial_state, None, walls,
machine1_pos, machine2_pos,
machine1_steps, machine2_steps,
machine1_parts, machine2_parts)
solution = breadth_first_graph_search(problem)
if solution:
print(solution.solution())Editor is loading...
Leave a Comment