improve limits of succ(), comments cleanup

This commit is contained in:
Szymon Szczubkowski 2023-04-17 16:24:08 +02:00
parent 2cb44dcb01
commit eaf7ed46fe
2 changed files with 25 additions and 25 deletions

View File

@ -6,34 +6,36 @@ class Node:
class Search: class Search:
def __init__(self, cell_size): def __init__(self, cell_size, cell_number):
self.cell_size = cell_size self.cell_size = cell_size
self.cell_number = cell_number
#this method needs to check the size of the plane - for now, it assumes 15x15, like in main.py
def succ(self, state): def succ(self, state):
x = state[0] x = state[0]
y = state[1] y = state[1]
angle = state[2] angle = state[2]
if angle == 0: # up angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
if y != 0: #this can stay as 0, negative values are not tolerated match(angles[angle]):
return [['move', x, y - self.cell_size, 0], ['left', x, y, 270], ['right', x, y, 90]] case 'UP':
else: if y != 0:
return [['left', x, y, 270], ['right', x, y, 90]] return [['move', x, y - self.cell_size, 0], ['left', x, y, 270], ['right', x, y, 90]]
if angle == 90: # right else:
if x != 700: #this needs to be changed to relative once size of plane is read by succ (parse in Search constructor?) return [['left', x, y, 270], ['right', x, y, 90]]
return [['move', x + self.cell_size, y, 90], ['left', x, y, 0], ['right', x, y, 180]] case 'RIGHT':
else: if x != self.cell_size*(self.cell_number-1):
return [['left', x, y, 0], ['right', x, y, 180]] return [['move', x + self.cell_size, y, 90], ['left', x, y, 0], ['right', x, y, 180]]
if angle == 180: # down else:
if y != 700: #this needs to be changed to relative once size of plane is read by succ (parse in Search constructor?) return [['left', x, y, 0], ['right', x, y, 180]]
return [['move', x, y + self.cell_size, 180], ['left', x, y, 90], ['right', x, y, 270]] case 'DOWN':
else: if y != self.cell_size*(self.cell_number-1):
return [['left', x, y, 90], ['right', x, y, 270]] return [['move', x, y + self.cell_size, 180], ['left', x, y, 90], ['right', x, y, 270]]
if angle == 270: # left else:
if x != 0: #this can stay as 0, negative values are not tolerated return [['left', x, y, 90], ['right', x, y, 270]]
return [['move', x - self.cell_size, y, 270], ['left', x, y, 180], ['right', x, y, 0]] case 'LEFT':
else: if x != 0:
return [['left', x, y, 180], ['right', x, y, 0]] return [['move', x - self.cell_size, y, 270], ['left', x, y, 180], ['right', x, y, 0]]
else:
return [['left', x, y, 180], ['right', x, y, 0]]
def graphsearch(self, istate, goaltest): def graphsearch(self, istate, goaltest):
x = istate[0] x = istate[0]
@ -67,8 +69,6 @@ class Search:
explored.append(elem.state) explored.append(elem.state)
for (action, state_x, state_y, state_angle) in self.succ(elem.state): for (action, state_x, state_y, state_angle) in self.succ(elem.state):
if state_x < 0 or state_y < 0: # check if any of the values are negative
continue
if [state_x, state_y, state_angle] not in fringe_state and \ if [state_x, state_y, state_angle] not in fringe_state and \
[state_x, state_y, state_angle] not in explored: [state_x, state_y, state_angle] not in explored:
x = Node([state_x, state_y, state_angle]) x = Node([state_x, state_y, state_angle])

View File

@ -69,7 +69,7 @@ class Game:
move_tractor_event = pygame.USEREVENT + 1 move_tractor_event = pygame.USEREVENT + 1
pygame.time.set_timer(move_tractor_event, 1000) # tractor moves every 1000 ms pygame.time.set_timer(move_tractor_event, 1000) # tractor moves every 1000 ms
tractor_next_moves = [] tractor_next_moves = []
graph_search_object = graph_search.Search(self.cell_size) graph_search_object = graph_search.Search(self.cell_size, self.cell_number)
while running: while running:
clock.tick(60) # manual fps control not to overwork the computer clock.tick(60) # manual fps control not to overwork the computer