add heuristics

This commit is contained in:
Pawel Felcyn 2023-05-13 23:06:42 +02:00
parent 65bef51958
commit c708663d39
7 changed files with 31 additions and 7 deletions

18
bfs.py
View File

@ -1,5 +1,6 @@
from agentState import AgentState from agentState import AgentState
from typing import Dict, Tuple from typing import Dict, Tuple
from city import City
from gridCellType import GridCellType from gridCellType import GridCellType
from agentActionType import AgentActionType from agentActionType import AgentActionType
from agentOrientation import AgentOrientation from agentOrientation import AgentOrientation
@ -104,4 +105,19 @@ def is_state_valid(state: AgentState, grid: Dict[Tuple[int, int], GridCellType])
try: try:
return grid[state.position] == GridCellType.STREET_HORIZONTAL or grid[state.position] == GridCellType.STREET_VERTICAL or grid[state.position] == GridCellType.SPEED_BUMP return grid[state.position] == GridCellType.STREET_HORIZONTAL or grid[state.position] == GridCellType.STREET_VERTICAL or grid[state.position] == GridCellType.SPEED_BUMP
except: except:
return False 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

10
city.py
View File

@ -1,21 +1,23 @@
from typing import List from typing import List, Dict, Tuple
from garbageCan import GarbageCan from garbageCan import GarbageCan
from speedBump import SpeedBump from speedBump import SpeedBump
from street import Street from street import Street
from gameContext import GameContext from gameContext import GameContext
class City: class City:
nodes: List[GarbageCan] cans: List[GarbageCan]
bumps: List[SpeedBump] bumps: List[SpeedBump]
streets: List[Street] streets: List[Street]
cans_dict: Dict[Tuple[int, int], GarbageCan] = {}
def __init__(self) -> None: def __init__(self) -> None:
self.nodes = [] self.nodes = []
self.streets = [] self.streets = []
self.bumps = [] self.bumps = []
def add_node(self, node: GarbageCan) -> None: def add_can(self, can: GarbageCan) -> None:
self.nodes.append(node) self.nodes.append(can)
self.cans_dict[can.position] = can
def add_street(self, street: Street) -> None: def add_street(self, street: Street) -> None:
self.streets.append(street) self.streets.append(street)

View File

@ -15,6 +15,8 @@ class GameContext:
grid: Dict[Tuple[int, int], GridCellType] = {} grid: Dict[Tuple[int, int], GridCellType] = {}
dust_car = None dust_car = None
landfill = None landfill = None
def __init__(self) -> None: def __init__(self) -> None:
self._init_grid() self._init_grid()

View File

@ -6,10 +6,12 @@ from gridCellType import GridCellType
class GarbageCan: class GarbageCan:
position: Tuple[int, int] position: Tuple[int, int]
garbage: List[Garbage] garbage: List[Garbage]
is_visited: bool
def __init__(self, position: Tuple[int, int]) -> None: def __init__(self, position: Tuple[int, int]) -> None:
self.position = position self.position = position
self.garbage = [] self.garbage = []
self.is_visited = False
def add_garbage(self, garbage: Garbage) -> None: def add_garbage(self, garbage: Garbage) -> None:
self.garbage.append(garbage) self.garbage.append(garbage)

View File

@ -7,4 +7,5 @@ class GridCellType(Enum):
GARBAGE_CAN = 3 GARBAGE_CAN = 3
VISITED_GARBAGE_CAN = 4 VISITED_GARBAGE_CAN = 4
LANDFILL = 5 LANDFILL = 5
SPEED_BUMP = 6 SPEED_BUMP = 6
UNKNOWN = None

View File

@ -19,6 +19,7 @@ def collect_garbage(game_context: GameContext) -> None:
move_dust_car(path, game_context) move_dust_car(path, game_context)
next_position = calculate_next_position(game_context.dust_car) next_position = calculate_next_position(game_context.dust_car)
game_context.grid[next_position] = GridCellType.VISITED_GARBAGE_CAN game_context.grid[next_position] = GridCellType.VISITED_GARBAGE_CAN
game_context.city.cans_dict[next_position].is_visited = True
pass pass
def move_dust_car(actions: list[AgentActionType], game_context: GameContext) -> None: def move_dust_car(actions: list[AgentActionType], game_context: GameContext) -> None:

View File

@ -35,7 +35,7 @@ def create_city() -> City:
for s in streets: for s in streets:
city.add_street(s) city.add_street(s)
for t in trashcans: for t in trashcans:
city.add_node(t) city.add_can(t)
for b in bumps: for b in bumps:
city.add_bump(b) city.add_bump(b)
return city return city