2021-04-24 17:32:07 +02:00
|
|
|
class Node:
|
|
|
|
def __init__(self):
|
|
|
|
self.state = State()
|
|
|
|
self.parent = None
|
2021-04-26 18:21:56 +02:00
|
|
|
self.action = ""
|
2021-04-24 17:32:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class State:
|
|
|
|
def __init__(self):
|
|
|
|
self.coord = []
|
|
|
|
self.direction = ""
|
2021-04-24 23:44:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
def successor(state):
|
|
|
|
|
2021-04-26 18:21:56 +02:00
|
|
|
node_state_left = Node()
|
|
|
|
node_state_right = Node()
|
|
|
|
node_state_forward = Node()
|
|
|
|
|
2021-04-24 23:44:00 +02:00
|
|
|
if state.direction == "east":
|
|
|
|
|
|
|
|
node_state_left.state = State()
|
|
|
|
node_state_left.state.coord = state.coord
|
|
|
|
node_state_left.state.direction = "north"
|
|
|
|
node_state_left.action = "Left"
|
|
|
|
|
|
|
|
node_state_right.state = State()
|
|
|
|
node_state_right.state.coord = state.coord
|
|
|
|
node_state_right.state.direction = "south"
|
|
|
|
node_state_right.action = "Right"
|
|
|
|
|
2021-04-27 14:15:48 +02:00
|
|
|
if state.coord[0] + 53 < 533:
|
|
|
|
node_state_forward.state = State()
|
|
|
|
node_state_forward.state.coord = [state.coord[0] + 53, state.coord[1]]
|
|
|
|
node_state_forward.state.direction = state.direction
|
|
|
|
node_state_forward.action = "Up"
|
2021-04-26 18:21:56 +02:00
|
|
|
|
|
|
|
elif state.direction == "west":
|
|
|
|
|
|
|
|
node_state_left.state = State()
|
|
|
|
node_state_left.state.coord = state.coord
|
|
|
|
node_state_left.state.direction = "south"
|
|
|
|
node_state_left.action = "Left"
|
|
|
|
|
|
|
|
node_state_right.state = State()
|
|
|
|
node_state_right.state.coord = state.coord
|
|
|
|
node_state_right.state.direction = "north"
|
|
|
|
node_state_right.action = "Right"
|
|
|
|
|
2021-04-27 14:15:48 +02:00
|
|
|
if state.coord[0] > 3:
|
|
|
|
node_state_forward.state = State()
|
|
|
|
node_state_forward.state.coord = [state.coord[0] - 53, state.coord[1]]
|
|
|
|
node_state_forward.state.direction = state.direction
|
|
|
|
node_state_forward.action = "Up"
|
2021-04-26 18:21:56 +02:00
|
|
|
|
|
|
|
elif state.direction == "north":
|
|
|
|
|
|
|
|
node_state_left.state = State()
|
|
|
|
node_state_left.state.coord = state.coord
|
|
|
|
node_state_left.state.direction = "west"
|
|
|
|
node_state_left.action = "Left"
|
|
|
|
|
|
|
|
node_state_right.state = State()
|
|
|
|
node_state_right.state.coord = state.coord
|
|
|
|
node_state_right.state.direction = "east"
|
|
|
|
node_state_right.action = "Right"
|
|
|
|
|
2021-04-27 14:15:48 +02:00
|
|
|
if state.coord[1] > 3:
|
|
|
|
node_state_forward.state = State()
|
|
|
|
node_state_forward.state.coord = [state.coord[0], state.coord[1] - 53]
|
|
|
|
node_state_forward.state.direction = state.direction
|
|
|
|
node_state_forward.action = "Up"
|
2021-04-24 23:44:00 +02:00
|
|
|
|
2021-04-26 18:21:56 +02:00
|
|
|
elif state.direction == "south":
|
2021-04-24 23:44:00 +02:00
|
|
|
|
2021-04-26 18:21:56 +02:00
|
|
|
node_state_left.state = State()
|
|
|
|
node_state_left.state.coord = state.coord
|
|
|
|
node_state_left.state.direction = "east"
|
|
|
|
node_state_left.action = "Left"
|
2021-04-24 23:44:00 +02:00
|
|
|
|
2021-04-26 18:21:56 +02:00
|
|
|
node_state_right.state = State()
|
|
|
|
node_state_right.state.coord = state.coord
|
|
|
|
node_state_right.state.direction = "west"
|
|
|
|
node_state_right.action = "Right"
|
2021-04-24 23:44:00 +02:00
|
|
|
|
2021-04-27 14:15:48 +02:00
|
|
|
if state.coord[1] + 53 < 533:
|
|
|
|
node_state_forward.state = State()
|
|
|
|
node_state_forward.state.coord = [state.coord[0], state.coord[1] + 53]
|
|
|
|
node_state_forward.state.direction = state.direction
|
|
|
|
node_state_forward.action = "Up"
|
2021-04-26 18:21:56 +02:00
|
|
|
|
2021-04-27 14:15:48 +02:00
|
|
|
if len(node_state_forward.state.coord) != 0:
|
|
|
|
return [node_state_left, node_state_right, node_state_forward]
|
|
|
|
else:
|
|
|
|
return [node_state_left, node_state_right]
|
2021-04-26 18:21:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
def graphsearch(fringe, explored, start_state, end_state_coord):
|
|
|
|
|
|
|
|
node = Node()
|
|
|
|
node.state = start_state
|
|
|
|
node.parent = node.state
|
|
|
|
#node.action = "Right"
|
|
|
|
fringe.append(node)
|
|
|
|
iter = 0
|
|
|
|
|
|
|
|
bool = True
|
|
|
|
while bool:
|
|
|
|
if len(fringe) == 0:
|
|
|
|
bool = False
|
|
|
|
#return False
|
|
|
|
|
|
|
|
elem = fringe[iter]
|
|
|
|
|
|
|
|
if elem.state.coord == end_state_coord:
|
|
|
|
bool = False
|
|
|
|
return fringe
|
|
|
|
|
|
|
|
explored.append(elem)
|
|
|
|
|
|
|
|
another_states = successor(elem.state)
|
|
|
|
for i in range(0, len(another_states)):
|
|
|
|
n = len(fringe)
|
|
|
|
for j in range(0, n):
|
|
|
|
if another_states[i].state.coord[0] == fringe[j].state.coord[0] and another_states[i].state.coord[1] == fringe[j].state.coord[1]:
|
|
|
|
if another_states[i].state.direction == fringe[j].state.direction:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# states = []
|
|
|
|
# for k in range(0, len(fringe)):
|
|
|
|
# new_state = fringe[k].state
|
|
|
|
# states.append(new_state)
|
|
|
|
# now_state = another_states[i].state
|
|
|
|
# if now_state in states:
|
|
|
|
# break
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# bool_break = False
|
|
|
|
# for k in range(0, n):
|
|
|
|
# if another_states[i].state.coord[0] == fringe[k].state.coord[0] and another_states[i].state.coord[1] == fringe[k].state.coord[1]:
|
|
|
|
# if another_states[i].state.direction == fringe[k].state.direction:
|
|
|
|
# bool_break = True
|
|
|
|
# if bool_break:
|
|
|
|
# break
|
|
|
|
another_states[i].parent = elem.state
|
|
|
|
fringe.append(another_states[i])
|
|
|
|
else:
|
2021-04-27 14:15:48 +02:00
|
|
|
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:
|
2021-04-26 18:21:56 +02:00
|
|
|
break
|
2021-04-27 14:15:48 +02:00
|
|
|
# if another_states[i] in fringe:
|
|
|
|
# break
|
2021-04-26 18:21:56 +02:00
|
|
|
if another_states[i].state.direction == fringe[j].state.direction:
|
|
|
|
another_states[i].parent = elem.state
|
|
|
|
fringe.append(another_states[i])
|
|
|
|
iter += 1
|