astar implementation #3

Merged
s473558 merged 2 commits from astar into master 2023-05-08 16:29:30 +02:00
3 changed files with 121 additions and 4 deletions

110
astar_search.py Normal file
View File

@ -0,0 +1,110 @@
class Node:
def __init__(self, state, parent='', action='', distance=0):
self.state = state
self.parent = parent
self.action = action
self.distance = distance
class Search:
def __init__(self, cell_size, cell_number):
self.cell_size = cell_size
self.cell_number = cell_number
def succ(self, state):
x = state[0]
y = state[1]
angle = state[2]
match(angle):
case 'UP':
possible = [['left', x, y, 'LEFT'], ['right', x, y, 'RIGHT']]
if y != 0: possible.append(['move', x, y - self.cell_size, 'UP'])
return possible
case 'RIGHT':
possible = [['left', x, y, 'UP'], ['right', x, y, 'DOWN']]
if x != self.cell_size*(self.cell_number-1): possible.append(['move', x + self.cell_size, y, 'RIGHT'])
return possible
case 'DOWN':
possible = [['left', x, y, 'RIGHT'], ['right', x, y, 'LEFT']]
if y != self.cell_size*(self.cell_number-1): possible.append(['move', x, y + self.cell_size, 'DOWN'])
return possible
case 'LEFT':
possible = [['left', x, y, 'DOWN'], ['right', x, y, 'UP']]
if x != 0: possible.append(['move', x - self.cell_size, y, 'LEFT'])
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
def astarsearch(self, istate, goaltest, stones, flowers):
#to be expanded
def cost_old(x, y):
if (x, y) in stones:
return 10
else:
return 1
x = istate[0]
y = istate[1]
angle = istate[2]
# 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 = []
while True:
if len(fringe) == 0:
return False
fringe.sort(key=lambda x: x[1])
elem = fringe.pop(0)[0]
# if goal_test(elem.state):
# return
# print(elem.state[0], elem.state[1], elem.state[2])
if elem.state[0] == goaltest[0] and elem.state[1] == goaltest[1]: # checks if we reached the given point
steps = []
while elem.parent:
steps.append([elem.action, elem.state[0], elem.state[1]]) # should return only elem.action in prod
elem = elem.parent
steps.reverse()
print(steps) # only for dev
return steps
explored.append(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.parent = elem
priority = self.cost(elem, stones, goaltest, flowers) + self.heuristic(elem, goaltest)
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:
fringe.append((x, priority))
elif x.state in fringe_states:
for i in range(len(fringe)):
if fringe[i][0].state == x.state:
if fringe[i][1] > priority:
fringe[i] = (x, priority)

View File

@ -5,6 +5,7 @@ import soil
class Blocks: class Blocks:
def __init__(self, parent_screen,cell_size): def __init__(self, parent_screen,cell_size):
self.parent_screen = parent_screen self.parent_screen = parent_screen
self.flower_image = pygame.image.load(r'resources/flower.png').convert_alpha() self.flower_image = pygame.image.load(r'resources/flower.png').convert_alpha()
@ -28,6 +29,9 @@ class Blocks:
self.soil = soil.Soil() self.soil = soil.Soil()
self.stones = set()
#bandaid to let astar know about stones
def locate_blocks(self, blocks_number, cell_number, body): def locate_blocks(self, blocks_number, cell_number, body):
for i in range(blocks_number): for i in range(blocks_number):
self.x = random.randint(0, cell_number-1) self.x = random.randint(0, cell_number-1)
@ -47,6 +51,8 @@ class Blocks:
self.parent_screen.blit(self.alive_leaf_image, (x, y)) self.parent_screen.blit(self.alive_leaf_image, (x, y))
if color == 'stone': if color == 'stone':
self.parent_screen.blit(self.stone_image, (x, y)) self.parent_screen.blit(self.stone_image, (x, y))
#bandaid to let astar know about stones
self.stones.add((x, y))
if color == 'flower': if color == 'flower':
self.parent_screen.blit(self.flower_image, (x, y)) self.parent_screen.blit(self.flower_image, (x, y))
if color == 'fawn_seed': if color == 'fawn_seed':

View File

@ -4,7 +4,7 @@ import random
import land import land
import tractor import tractor
import blocks import blocks
import graph_search import astar_search
from pygame.locals import * from pygame.locals import *
@ -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, self.cell_number) astar_search_object = astar_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
@ -100,8 +100,9 @@ class Game:
print("Generated target: ",random_x, random_y) print("Generated target: ",random_x, random_y)
# below line should be later moved into tractor.py # below line should be later moved into tractor.py
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'} angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
tractor_next_moves = graph_search_object.graphsearch( #bandaid to know about stones
[self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y]) 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.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: