From c708663d390b05871cee7aaa7810208d922c9d65 Mon Sep 17 00:00:00 2001 From: Pawel Felcyn Date: Sat, 13 May 2023 23:06:42 +0200 Subject: [PATCH] add heuristics --- bfs.py | 18 +++++++++++++++++- city.py | 10 ++++++---- gameContext.py | 2 ++ garbageCan.py | 2 ++ gridCellType.py | 3 ++- movement.py | 1 + startup.py | 2 +- 7 files changed, 31 insertions(+), 7 deletions(-) diff --git a/bfs.py b/bfs.py index 6766491..a559592 100644 --- a/bfs.py +++ b/bfs.py @@ -1,5 +1,6 @@ from agentState import AgentState from typing import Dict, Tuple +from city import City from gridCellType import GridCellType from agentActionType import AgentActionType from agentOrientation import AgentOrientation @@ -104,4 +105,19 @@ def is_state_valid(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) try: return grid[state.position] == GridCellType.STREET_HORIZONTAL or grid[state.position] == GridCellType.STREET_VERTICAL or grid[state.position] == GridCellType.SPEED_BUMP except: - return False \ No newline at end of file + return False + +def _heuristics(position: Tuple[int, int], city: City): + min_distance: int = 300 + found_nonvisited: bool = False + for can in city.cans: + if can.is_visited: + continue + found_nonvisited = True + distance = 3 * (abs(position[0] - can.position[0]) + abs(position[1] - can.position[1])) + if distance < min_distance: + min_distance = distance + if found_nonvisited: + return min_distance + return -1 + \ No newline at end of file diff --git a/city.py b/city.py index 2cd1a1f..431f582 100644 --- a/city.py +++ b/city.py @@ -1,21 +1,23 @@ -from typing import List +from typing import List, Dict, Tuple from garbageCan import GarbageCan from speedBump import SpeedBump from street import Street from gameContext import GameContext class City: - nodes: List[GarbageCan] + cans: List[GarbageCan] bumps: List[SpeedBump] streets: List[Street] + cans_dict: Dict[Tuple[int, int], GarbageCan] = {} def __init__(self) -> None: self.nodes = [] self.streets = [] self.bumps = [] - def add_node(self, node: GarbageCan) -> None: - self.nodes.append(node) + def add_can(self, can: GarbageCan) -> None: + self.nodes.append(can) + self.cans_dict[can.position] = can def add_street(self, street: Street) -> None: self.streets.append(street) diff --git a/gameContext.py b/gameContext.py index 6f6295f..dc54928 100644 --- a/gameContext.py +++ b/gameContext.py @@ -15,6 +15,8 @@ class GameContext: grid: Dict[Tuple[int, int], GridCellType] = {} dust_car = None landfill = None + + def __init__(self) -> None: self._init_grid() diff --git a/garbageCan.py b/garbageCan.py index 782e6f3..7d6f361 100644 --- a/garbageCan.py +++ b/garbageCan.py @@ -6,10 +6,12 @@ from gridCellType import GridCellType class GarbageCan: position: Tuple[int, int] garbage: List[Garbage] + is_visited: bool def __init__(self, position: Tuple[int, int]) -> None: self.position = position self.garbage = [] + self.is_visited = False def add_garbage(self, garbage: Garbage) -> None: self.garbage.append(garbage) diff --git a/gridCellType.py b/gridCellType.py index f8a637c..8ae4ad8 100644 --- a/gridCellType.py +++ b/gridCellType.py @@ -7,4 +7,5 @@ class GridCellType(Enum): GARBAGE_CAN = 3 VISITED_GARBAGE_CAN = 4 LANDFILL = 5 - SPEED_BUMP = 6 \ No newline at end of file + SPEED_BUMP = 6 + UNKNOWN = None \ No newline at end of file diff --git a/movement.py b/movement.py index 8cee448..308c64b 100644 --- a/movement.py +++ b/movement.py @@ -19,6 +19,7 @@ def collect_garbage(game_context: GameContext) -> None: move_dust_car(path, game_context) next_position = calculate_next_position(game_context.dust_car) game_context.grid[next_position] = GridCellType.VISITED_GARBAGE_CAN + game_context.city.cans_dict[next_position].is_visited = True pass def move_dust_car(actions: list[AgentActionType], game_context: GameContext) -> None: diff --git a/startup.py b/startup.py index 30c2740..e857080 100644 --- a/startup.py +++ b/startup.py @@ -35,7 +35,7 @@ def create_city() -> City: for s in streets: city.add_street(s) for t in trashcans: - city.add_node(t) + city.add_can(t) for b in bumps: city.add_bump(b) return city