diff --git a/app/__init__.py b/app/__init__.py index f5cfb0c..0841a2b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 import copy +import random from queue import Queue, PriorityQueue from threading import Event @@ -44,7 +45,6 @@ class App: if keys[pygame.K_w]: self.__tractor.move() - self.__tractor.choose_action() print(self.__tractor) if keys[pygame.K_n]: @@ -87,27 +87,36 @@ class App: self.__bot_is_running.set() self.__tractor.run_bot_handler(self.__moves, self.__bot_is_running) + if keys[pygame.K_a]: + self.auto_moving() + def update_screen(self) -> None: pygame.display.flip() def quit(self) -> None: pygame.quit() + def auto_moving(self) -> None: + self.__tractor.choose_action() + def get_moves_by_a_star(self) -> None: x, y = self.__tractor.get_position() node = Node(None, x, y, self.__tractor.get_direction(), 0, "movement", "initial state") board = copy.deepcopy(self.__board) - self.__moves = AStar.search(PriorityQueue(), Queue(), node, - lambda n=node, b=board: AStar.succ(n, b), - lambda n=node: AStar.goaltest(n), board) + x1 = random.randint(0, HORIZONTAL_NUM_OF_FIELDS) + y1 = random.randint(0, VERTICAL_NUM_OF_FIELDS) + dest = (x1, y1) + self.__moves = AStar.search_solution(PriorityQueue(), Queue(), dest, node, + lambda n=node, b=board: AStar.succ(n, b), + lambda d=dest, n=node: AStar.goaltest_by_coords(d, n), board) def get_moves_by_bfs(self) -> None: x, y = self.__tractor.get_position() node = Node(None, x, y, self.__tractor.get_direction(), 0, "movement", "initial state") board = copy.deepcopy(self.__board) self.__moves = Bfs.search(Queue(), Queue(), node, - lambda n=node, b=board: Bfs.succ(n, b), - lambda n=node: Bfs.goaltest(n), board) + lambda n=node, b=board: Bfs.succ(n, b), + lambda n=node: Bfs.goaltest(n), board) def run(self) -> None: self.initialize() diff --git a/app/a_star.py b/app/a_star.py index 585047b..e881d25 100644 --- a/app/a_star.py +++ b/app/a_star.py @@ -1,6 +1,7 @@ from __future__ import annotations import copy +import math from typing import Callable, Union from queue import Queue, PriorityQueue @@ -10,7 +11,7 @@ from app.graphsearch import Node, Graphsearch class PriorityItem: - def __init__(self, node: Node, priority: int): + def __init__(self, node: Node, priority: float): self.node = node self.priority = priority @@ -46,7 +47,7 @@ class AStar(Graphsearch): fringe.put(tmp_queue.get()) @staticmethod - def replace_nodes(fringe: PriorityQueue[PriorityItem], priority: int, state: tuple[int, int, float]): + def replace_nodes(fringe: PriorityQueue[PriorityItem], priority: float, state: tuple[int, int, float]): fringe_items = AStar.convert_queue_of_priority_items_with_priority_to_list(fringe) for s in fringe_items: if s[:-1] == state[:-1]: @@ -57,7 +58,7 @@ class AStar(Graphsearch): break @staticmethod - def g(board: Board, node: Node) -> int: + def g(board: Board, node: Node) -> float: """cost function""" result = 0 while node.get_node() is not None: @@ -67,20 +68,28 @@ class AStar(Graphsearch): return result @staticmethod - def h(node: Node) -> int: + def h(destination: tuple[int, int], node: Node) -> float: """heuristic function""" - crops_to_harvested = AMOUNT_OF_CROPS - node.get_amount_of_harvested_crops() - return crops_to_harvested ** 2 + crops_to_harvested * 10 + return round(math.sqrt(abs(destination[0] - node.get_x())**2 + abs(destination[0] - node.get_y())**2), 2) @staticmethod - def f(board: Board, node: Node) -> int: + def f(board: Board, destination: tuple[int, int], node: Node) -> float: """evaluation function""" - return AStar.g(board, node) + AStar.h(node) + return AStar.g(board, node) + AStar.h(destination, node) @staticmethod - def search(fringe: PriorityQueue, explored: Queue, istate: Node, + def goaltest_by_coords(destination: tuple[int, int], item: Node) -> bool: + # print(item) + # print(destination) + if destination[0] == item.get_x() and destination[1] == item.get_y(): + return True + else: + return False + + @staticmethod + def search_solution(fringe: PriorityQueue, explored: Queue, destination: tuple[int, int], istate: Node, succ: Callable[[Node, Board], list], - goaltest: Callable[[Node], bool], board: Board) -> Union[bool, list]: + goaltest: Callable[[tuple[int,int], Node], bool], board: Board) -> Union[bool, list]: print(f"Start A*") fringe.put(PriorityItem(istate, 0)) @@ -90,7 +99,7 @@ class AStar(Graphsearch): return False item = fringe.get() - if goaltest(item.node): + if goaltest(destination, item.node): return AStar.get_all_moves(item.node) copied_item = copy.deepcopy(item) @@ -102,7 +111,7 @@ class AStar(Graphsearch): explored_items = AStar.convert_queue_of_priority_items_to_list(explored) n = Node(item.node, *state, *action) - priority = AStar.f(board, n) + priority = AStar.f(board, destination, n) # print(priority) if state[:-1] not in fringe_items and state[:-1] not in explored_items: fringe.put(PriorityItem(n, priority)) diff --git a/app/tractor.py b/app/tractor.py index b9616f6..f725b65 100644 --- a/app/tractor.py +++ b/app/tractor.py @@ -309,7 +309,7 @@ class Tractor(BaseField): is_running.clear() - def choose_action(self) -> None: + def choose_action(self) -> tuple[tuple[int,int],str]: vectors = self.__board.convert_fields_to_vectors() print(vectors) coords = None @@ -321,6 +321,4 @@ class Tractor(BaseField): coords = (i, j) break print(coords, action) - if coords is not None: - # astar coords - pass + return coords, action diff --git a/config.py b/config.py index c248a84..22bdcfd 100644 --- a/config.py +++ b/config.py @@ -86,10 +86,10 @@ A_DO_NOTHING = "do nothing" TYPES_OF_ACTION = [A_SOW, A_HARVEST, A_HYDRATE, A_FERTILIZE, A_DO_NOTHING] # Costs fields: -VALUE_OF_CROPS = 1 -VALUE_OF_PLANT = 4 -VALUE_OF_SAND = 7 -VALUE_OF_CLAY = 10 +VALUE_OF_CROPS = 0.25 +VALUE_OF_PLANT = 0.5 +VALUE_OF_SAND = 0.75 +VALUE_OF_CLAY = 1 # Weather W_SUNNY = 'Sunny'