dodanie algorytmu a*

This commit is contained in:
Alicja Puzio 2024-04-27 22:49:47 +02:00
parent 81602fb3cf
commit dae9ee8996
15 changed files with 137 additions and 4 deletions

View File

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.10 (Traktor)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (Traktor)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project> </project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

130
source/astar.py Normal file
View File

@ -0,0 +1,130 @@
from area.constants import TILE_SIZE, ROWS
import copy
import heapq
from area.field import tiles, fieldX, fieldY
from bfs import get_moves
class Node:
def __init__(self, priority, x, y, direction, parent, action, cost=0):
self.x = x
self.y = y
self.direction = direction
self.action = action
self.parent = parent
self.cost = cost
self.priority = priority
def __int__(self):
return self.priority
# getters and setters:
def get_parent(self):
return self.parent
def set_parent(self, _parent):
self.parent = _parent
def get_action(self):
return self.action
def set_action(self, _action):
self.parent = _action
def get_x(self):
return self.x
def set_x(self, _x):
self.x = _x
def get_y(self):
return self.y
def set_y(self, _y):
self.y = _y
def get_direction(self):
return self.direction
def set_direction(self, _direction):
self.parent = _direction
def get_cost(self):
return self.cost
def set_cost(self, _cost):
self.cost = _cost
def get_priority(self):
return self.priority
def set_priority(self, _priority):
self.priority = _priority
def __lt__(self, other):
return self.get_priority() < other.get_priority()
def copy(self):
return copy.copy(self)
def goal_test(elem, goalstate):
if elem.get_x() == goalstate[0] and elem.get_y() == goalstate[1]:
return True
else:
return False
def tile_cost(tile):
if tile.image == "resources/images/sampling.png":
return 100
else:
return 1
def heuristic(current_x, current_y, end_x, end_y):
return abs(end_x - current_x) + abs(end_y - current_y)
# State as a tuple (x,y,direction)
# actions(string): move, rotate_to_left, rotate_to_right
# main search function:
def a_star(istate, succ, goaltest, tractor):
fringe = []
explored = set()
node = Node(0, istate.get_x(), istate.get_y(), istate.get_direction(), None, None, 0)
heapq.heappush(fringe, node)
while True:
if not fringe:
return False
elem = heapq.heappop(fringe)
temp = copy.copy(elem)
if goal_test(elem, goaltest) is True: # jesli True zwroc ciag akcji
return get_moves(elem)
explored.add(elem)
for (action, state) in succ(temp, tractor):
fringe_tuple = []
explored_tuple = []
for node in fringe:
fringe_tuple.append((node.get_x(), node.get_y(), node.get_direction()))
for node in explored:
explored_tuple.append((node.get_x(), node.get_y(), node.get_direction()))
tile = int((temp.get_y() - fieldY) / TILE_SIZE) * ROWS + int((temp.get_x() - fieldX) / TILE_SIZE)
cost = temp.cost + tile_cost(tiles[tile])
priority = cost + heuristic(state[0], state[1], goaltest[0], goaltest[1])
x = Node(priority, state[0], state[1], state[2], elem, action, cost)
if state not in fringe_tuple and state not in explored_tuple:
heapq.heappush(fringe, x)
elif state in fringe_tuple and elem.get_priority() < priority:
elem.set_priority(priority)

View File

@ -10,6 +10,7 @@ from area.field import get_tile_coordinates
from ground import Dirt from ground import Dirt
from plant import Plant from plant import Plant
from bfs import graphsearch, Istate, succ from bfs import graphsearch, Istate, succ
from astar import a_star
WIN = pygame.display.set_mode((WIDTH, HEIGHT)) WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Intelligent tractor') pygame.display.set_caption('Intelligent tractor')
@ -45,9 +46,11 @@ def main():
tractor.rect.x += fieldX tractor.rect.x += fieldX
tractor.rect.y += fieldY tractor.rect.y += fieldY
tractor.tractor_start = ((istate.get_x(), istate.get_y())) tractor.tractor_start = ((istate.get_x(), istate.get_y()))
#tractor.tractor_start = ((istate.get_x(), istate.get_y(), istate.get_direction))
tractor.tractor_end = ((goaltest[0], goaltest[1])) tractor.tractor_end = ((goaltest[0], goaltest[1]))
moves = (graphsearch(istate, succ, goaltest, tractor)) #moves = (graphsearch(istate, succ, goaltest, tractor))
moves = (a_star(istate, succ, goaltest, tractor))
print(moves) print(moves)

View File

@ -2,7 +2,7 @@ import random
import os import os
# path to plant images folder (used in randomize_photo function) # path to plant images folder (used in randomize_photo function)
folder_path = "resources\images\plant_photos" folder_path = "resources/images/plant_photos"
class Tile: class Tile: