diff --git a/.idea/Traktor AI.iml b/.idea/Traktor AI.iml index 078ddf5..ff48265 100644 --- a/.idea/Traktor AI.iml +++ b/.idea/Traktor AI.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 6c75bb3..8d93904 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/source/__pycache__/astar.cpython-39.pyc b/source/__pycache__/astar.cpython-39.pyc new file mode 100644 index 0000000..d9b59f2 Binary files /dev/null and b/source/__pycache__/astar.cpython-39.pyc differ diff --git a/source/__pycache__/bfs.cpython-39.pyc b/source/__pycache__/bfs.cpython-39.pyc new file mode 100644 index 0000000..d9ed805 Binary files /dev/null and b/source/__pycache__/bfs.cpython-39.pyc differ diff --git a/source/__pycache__/crop_protection_product.cpython-39.pyc b/source/__pycache__/crop_protection_product.cpython-39.pyc new file mode 100644 index 0000000..42a84e3 Binary files /dev/null and b/source/__pycache__/crop_protection_product.cpython-39.pyc differ diff --git a/source/__pycache__/ground.cpython-39.pyc b/source/__pycache__/ground.cpython-39.pyc new file mode 100644 index 0000000..d3c6d44 Binary files /dev/null and b/source/__pycache__/ground.cpython-39.pyc differ diff --git a/source/__pycache__/plant.cpython-39.pyc b/source/__pycache__/plant.cpython-39.pyc new file mode 100644 index 0000000..c7fc95f Binary files /dev/null and b/source/__pycache__/plant.cpython-39.pyc differ diff --git a/source/__pycache__/tile.cpython-39.pyc b/source/__pycache__/tile.cpython-39.pyc new file mode 100644 index 0000000..a03d927 Binary files /dev/null and b/source/__pycache__/tile.cpython-39.pyc differ diff --git a/source/area/__pycache__/__init__.cpython-39.pyc b/source/area/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..14026e7 Binary files /dev/null and b/source/area/__pycache__/__init__.cpython-39.pyc differ diff --git a/source/area/__pycache__/constants.cpython-39.pyc b/source/area/__pycache__/constants.cpython-39.pyc new file mode 100644 index 0000000..77408d2 Binary files /dev/null and b/source/area/__pycache__/constants.cpython-39.pyc differ diff --git a/source/area/__pycache__/field.cpython-39.pyc b/source/area/__pycache__/field.cpython-39.pyc new file mode 100644 index 0000000..3badf32 Binary files /dev/null and b/source/area/__pycache__/field.cpython-39.pyc differ diff --git a/source/area/__pycache__/tractor.cpython-39.pyc b/source/area/__pycache__/tractor.cpython-39.pyc new file mode 100644 index 0000000..d83674d Binary files /dev/null and b/source/area/__pycache__/tractor.cpython-39.pyc differ diff --git a/source/astar.py b/source/astar.py new file mode 100644 index 0000000..51634b3 --- /dev/null +++ b/source/astar.py @@ -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) diff --git a/source/main.py b/source/main.py index d5de3ef..d2e3713 100644 --- a/source/main.py +++ b/source/main.py @@ -10,6 +10,7 @@ from area.field import get_tile_coordinates from ground import Dirt from plant import Plant from bfs import graphsearch, Istate, succ +from astar import a_star WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Intelligent tractor') @@ -45,9 +46,11 @@ def main(): tractor.rect.x += fieldX tractor.rect.y += fieldY 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])) - moves = (graphsearch(istate, succ, goaltest, tractor)) + #moves = (graphsearch(istate, succ, goaltest, tractor)) + moves = (a_star(istate, succ, goaltest, tractor)) print(moves) diff --git a/source/tile.py b/source/tile.py index c94e77a..654f59b 100644 --- a/source/tile.py +++ b/source/tile.py @@ -2,7 +2,7 @@ import random import os # path to plant images folder (used in randomize_photo function) -folder_path = "resources\images\plant_photos" +folder_path = "resources/images/plant_photos" class Tile: