Graphsearch and successor - perhaps they work right, but we have to check it

This commit is contained in:
Art-cyber520 2021-04-27 14:15:48 +02:00
parent 0493082643
commit 4a2696bd89
3 changed files with 32 additions and 59 deletions

View File

@ -22,90 +22,77 @@ def successor(state):
node_state_left.state = State() node_state_left.state = State()
node_state_left.state.coord = state.coord node_state_left.state.coord = state.coord
node_state_left.state.direction = "north" node_state_left.state.direction = "north"
#node_state_left.parent = state
node_state_left.action = "Left" node_state_left.action = "Left"
node_state_right.state = State() node_state_right.state = State()
node_state_right.state.coord = state.coord node_state_right.state.coord = state.coord
node_state_right.state.direction = "south" node_state_right.state.direction = "south"
#node_state_right.parent = state
node_state_right.action = "Right" node_state_right.action = "Right"
node_state_forward.state = State() if state.coord[0] + 53 < 533:
node_state_forward.state.coord = [state.coord[0] + 53, state.coord[1]] node_state_forward.state = State()
node_state_forward.state.direction = state.direction node_state_forward.state.coord = [state.coord[0] + 53, state.coord[1]]
#node_state_forward.parent = state node_state_forward.state.direction = state.direction
node_state_forward.action = "Up" node_state_forward.action = "Up"
#return [node_state_left, node_state_right, node_state_forward]
elif state.direction == "west": elif state.direction == "west":
node_state_left.state = State() node_state_left.state = State()
node_state_left.state.coord = state.coord node_state_left.state.coord = state.coord
node_state_left.state.direction = "south" node_state_left.state.direction = "south"
#node_state_left.parent = state
node_state_left.action = "Left" node_state_left.action = "Left"
node_state_right.state = State() node_state_right.state = State()
node_state_right.state.coord = state.coord node_state_right.state.coord = state.coord
node_state_right.state.direction = "north" node_state_right.state.direction = "north"
#node_state_right.parent = state
node_state_right.action = "Right" node_state_right.action = "Right"
node_state_forward.state = State() if state.coord[0] > 3:
node_state_forward.state.coord = [state.coord[0] - 53, state.coord[1]] node_state_forward.state = State()
node_state_forward.state.direction = state.direction node_state_forward.state.coord = [state.coord[0] - 53, state.coord[1]]
#node_state_forward.parent = state node_state_forward.state.direction = state.direction
node_state_forward.action = "Up" node_state_forward.action = "Up"
#return [node_state_left, node_state_right, node_state_forward]
elif state.direction == "north": elif state.direction == "north":
node_state_left.state = State() node_state_left.state = State()
node_state_left.state.coord = state.coord node_state_left.state.coord = state.coord
node_state_left.state.direction = "west" node_state_left.state.direction = "west"
#node_state_left.parent = state
node_state_left.action = "Left" node_state_left.action = "Left"
node_state_right.state = State() node_state_right.state = State()
node_state_right.state.coord = state.coord node_state_right.state.coord = state.coord
node_state_right.state.direction = "east" node_state_right.state.direction = "east"
#node_state_right.parent = state
node_state_right.action = "Right" node_state_right.action = "Right"
node_state_forward.state = State() if state.coord[1] > 3:
node_state_forward.state.coord = [state.coord[0], state.coord[1] - 53] node_state_forward.state = State()
node_state_forward.state.direction = state.direction node_state_forward.state.coord = [state.coord[0], state.coord[1] - 53]
#node_state_forward.parent = state node_state_forward.state.direction = state.direction
node_state_forward.action = "Up" node_state_forward.action = "Up"
#return [node_state_left, node_state_right, node_state_forward]
elif state.direction == "south": elif state.direction == "south":
node_state_left.state = State() node_state_left.state = State()
node_state_left.state.coord = state.coord node_state_left.state.coord = state.coord
node_state_left.state.direction = "east" node_state_left.state.direction = "east"
#node_state_left.parent = state
node_state_left.action = "Left" node_state_left.action = "Left"
node_state_right.state = State() node_state_right.state = State()
node_state_right.state.coord = state.coord node_state_right.state.coord = state.coord
node_state_right.state.direction = "west" node_state_right.state.direction = "west"
#node_state_right.parent = state
node_state_right.action = "Right" node_state_right.action = "Right"
node_state_forward.state = State() if state.coord[1] + 53 < 533:
node_state_forward.state.coord = [state.coord[0], state.coord[1] + 53] node_state_forward.state = State()
node_state_forward.state.direction = state.direction node_state_forward.state.coord = [state.coord[0], state.coord[1] + 53]
#node_state_forward.parent = state node_state_forward.state.direction = state.direction
node_state_forward.action = "Up" node_state_forward.action = "Up"
#return [node_state_left, node_state_right, node_state_forward] if len(node_state_forward.state.coord) != 0:
return [node_state_left, node_state_right, node_state_forward]
return [node_state_left, node_state_right, node_state_forward] else:
return [node_state_left, node_state_right]
def graphsearch(fringe, explored, start_state, end_state_coord): def graphsearch(fringe, explored, start_state, end_state_coord):
@ -126,7 +113,6 @@ def graphsearch(fringe, explored, start_state, end_state_coord):
elem = fringe[iter] elem = fringe[iter]
if elem.state.coord == end_state_coord: if elem.state.coord == end_state_coord:
print("Gotowe!")
bool = False bool = False
return fringe return fringe
@ -166,8 +152,15 @@ def graphsearch(fringe, explored, start_state, end_state_coord):
another_states[i].parent = elem.state another_states[i].parent = elem.state
fringe.append(another_states[i]) fringe.append(another_states[i])
else: else:
if another_states[i] in fringe: states = []
for k in range(0, len(fringe)):
new_state = [fringe[k].state.coord, fringe[k].state.direction]
states.append(new_state)
now_state = [another_states[i].state.coord, another_states[i].state.direction]
if now_state in states:
break break
# if another_states[i] in fringe:
# break
if another_states[i].state.direction == fringe[j].state.direction: if another_states[i].state.direction == fringe[j].state.direction:
another_states[i].parent = elem.state another_states[i].parent = elem.state
fringe.append(another_states[i]) fringe.append(another_states[i])

View File

@ -164,25 +164,13 @@ def Action(event):
# Modified by Artem to search in the status area # Modified by Artem to search in the status area
def MouseClickEvent(event): def MouseClickEvent(event):
global fringe global fringe
#print(len(field.canvas_small_images), field.canvas_small_images)
for i in range(0, len(field.canvas_small_images)): for i in range(0, len(field.canvas_small_images)):
print(field.small_field_canvas.coords(field.canvas_small_images[i])) print(field.small_field_canvas.coords(field.canvas_small_images[i]))
#print("Lewy przycisk myszy zostal nacisniety!")
#node = nd.Node()
#print(node.state.coord, node.state.direction, node.action, node.parent)
#node.state = nd.State()
#node.state.coord = field.small_field_canvas.coords(player.image_canvas_id)
#node.state.direction = "east"
#node.state.coord = field.small_field_canvas.coords(field.canvas_small_images[5])
start_position = field.small_field_canvas.coords(player.image_canvas_id) start_position = field.small_field_canvas.coords(player.image_canvas_id)
end_state_coord = [] end_state_coord = []
print("Pierwsza pozycja: {} {}".format(start_position[0], start_position[1])) print("Pierwsza pozycja: {} {}".format(start_position[0], start_position[1]))
#print(node.state.coord, node.state.direction, node.parent, node.action)
#print("Pozycje myszy: {} {}".format(event.x, event.y))
for i in range(0, len(field.canvas_small_images)): for i in range(0, len(field.canvas_small_images)):
img_coords = field.small_field_canvas.coords(field.canvas_small_images[i]) img_coords = field.small_field_canvas.coords(field.canvas_small_images[i])
if (img_coords[0] <= event.x and event.x <= img_coords[0] + IMAGE_SIZE) and (img_coords[1] <= event.y and event.y <= img_coords[1] + IMAGE_SIZE): if (img_coords[0] <= event.x and event.x <= img_coords[0] + IMAGE_SIZE) and (img_coords[1] <= event.y and event.y <= img_coords[1] + IMAGE_SIZE):
@ -194,22 +182,14 @@ def MouseClickEvent(event):
if len(fringe) == 0: if len(fringe) == 0:
node.state.coord = field.small_field_canvas.coords(player.image_canvas_id) node.state.coord = field.small_field_canvas.coords(player.image_canvas_id)
node.state.direction = "east" node.state.direction = "east"
print("Pierwszy state - OK")
else: else:
node = fringe[len(fringe) - 1] node = fringe[len(fringe) - 1]
print("Pozostale states - OK")
fringe.clear() fringe.clear()
print("\nLIST IS EMPTY: {}\n".format(fringe))
explored.clear() explored.clear()
print("Czyszczenie list - OK")
# Successor - only east
fringe = nd.graphsearch(fringe, explored, node.state, end_state_coord) fringe = nd.graphsearch(fringe, explored, node.state, end_state_coord)
print("Fringe - OK")
#print(fringe)
print("{}".format(fringe))
for i in range(0, len(fringe)): for i in range(0, len(fringe)):
print('Node{} = State: {} {}, Parent: {} {}, Action: {}'.format(i + 1, fringe[i].state.coord, fringe[i].state.direction, fringe[i].parent.coord, fringe[i].parent.direction, fringe[i].action)) print('Node{} = State: {} {}, Parent: {} {}, Action: {}'.format(i + 1, fringe[i].state.coord, fringe[i].state.direction, fringe[i].parent.coord, fringe[i].parent.direction, fringe[i].action))