add heuristics #24

Merged
s473616 merged 1 commits from heuristics into master 2023-05-13 23:07:17 +02:00
7 changed files with 31 additions and 7 deletions

18
bfs.py
View File

@ -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
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 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)

View File

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

View File

@ -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)

View File

@ -7,4 +7,5 @@ class GridCellType(Enum):
GARBAGE_CAN = 3
VISITED_GARBAGE_CAN = 4
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)
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:

View File

@ -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