image_recognition #5

Merged
s473558 merged 23 commits from image_recognition into master 2023-06-12 16:25:59 +02:00
2 changed files with 40 additions and 15 deletions
Showing only changes of commit 2437125100 - Show all commits

View File

@ -1,8 +1,9 @@
class Node: class Node:
def __init__(self, state, parent='', action=''): def __init__(self, state, parent='', action='', distance=0):
self.state = state self.state = state
self.parent = parent self.parent = parent
self.action = action self.action = action
self.distance = distance
class Search: class Search:
def __init__(self, cell_size, cell_number): def __init__(self, cell_size, cell_number):
@ -30,11 +31,27 @@ class Search:
possible = [['left', x, y, 'DOWN'], ['right', x, y, 'UP']] possible = [['left', x, y, 'DOWN'], ['right', x, y, 'UP']]
if x != 0: possible.append(['move', x - self.cell_size, y, 'LEFT']) if x != 0: possible.append(['move', x - self.cell_size, y, 'LEFT'])
return possible return possible
def cost(self, node, stones, goal, flowers):
# cost = node.distance
cost = 0
# cost += 10 if stones[node.state[0], node.state[1]] == 1 else 1
cost += 1000000 if (node.state[0], node.state[1]) in stones else 1
cost += 30 if (node.state[0], node.state[1]) in flowers else 1
if node.parent:
node = node.parent
cost += node.distance # should return only elem.action in prod
return cost
def heuristic(self, node, goal):
return abs(node.state[0] - goal[0]) + abs(node.state[1] - goal[1])
#bandaid to know about stones #bandaid to know about stones
def astarsearch(self, istate, goaltest, stones): def astarsearch(self, istate, goaltest, stones, flowers):
#to be expanded #to be expanded
def cost(x, y): def cost_old(x, y):
if (x, y) in stones: if (x, y) in stones:
return 10 return 10
else: else:
@ -45,7 +62,12 @@ class Search:
y = istate[1] y = istate[1]
angle = istate[2] angle = istate[2]
fringe = [(Node([x, y, angle]), cost(x, y))] # queue (moves/states to check) # fringe = [(Node([x, y, angle]), cost_old(x, y))] # queue (moves/states to check)
fringe = [(Node([x, y, angle]))] # queue (moves/states to check)
fringe[0].distance = self.cost(fringe[0], stones, goaltest, flowers)
fringe.append((Node([x, y, angle]), self.cost(fringe[0], stones, goaltest, flowers)))
fringe.pop(0)
explored = [] explored = []
while True: while True:
@ -71,15 +93,18 @@ 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):
x = Node([state_x, state_y, state_angle], elem, action) x = Node([state_x, state_y, state_angle], elem, action)
x.parent = elem
priority = cost(state_x, state_y) priority = self.cost(elem, stones, goaltest, flowers) + self.heuristic(elem, goaltest)
fringe_states = [node.state for (node, p) in fringe] elem.distance = priority
# priority = cost_old(x, y) + self.heuristic(elem, goaltest)
fringe_states = [node.state for (node, p) in fringe]
if x.state not in fringe_states and x.state not in explored: if x.state not in fringe_states and x.state not in explored:
fringe.append((x, priority)) fringe.append((x, priority))
elif x.state in fringe_states: elif x.state in fringe_states:
for i in range(len(fringe)): for i in range(len(fringe)):
if fringe[i][0].state == x.state: if fringe[i][0].state == x.state:
if fringe[i][1] > priority: if fringe[i][1] > priority:
fringe[i] = (x, priority) fringe[i] = (x, priority)

View File

@ -102,7 +102,7 @@ class Game:
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'} angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
#bandaid to know about stones #bandaid to know about stones
tractor_next_moves = astar_search_object.astarsearch( tractor_next_moves = astar_search_object.astarsearch(
[self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y], self.blocks.stones) [self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y], self.blocks.stones, self.flower_body)
else: else:
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number) self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
elif event.type == QUIT: elif event.type == QUIT: