Commit ad4a32f9 authored by Leo Peron's avatar Leo Peron
Browse files

improve heuristic

parent 78414eb4
......@@ -353,21 +353,24 @@ def manhattan_distance_factory(map_rules: Dict[str, set]) -> Callable:
def manhattan_distance_astar_factory(map_rules: Dict[str, set]) -> Callable:
def dist_to_closest_demon(state: State) -> int:
keyFactor = 0
def dist_to_closest_demon(state: State, action: Action) -> int:
passedCost = (1/(1 + map_rules["max"] - state.steps))*2
booster = 1.5
manDist = 0
if state.key:
keyFactor = 4
lockFactor = 0
manDist = abs(state.key[0] - state.hero[0]) + abs(state.key[1] - state.hero[1])
if action:
booster = (1.00, 0.75)[action.verb == "unlock/key"]
if state.lock and not state.key:
lockFactor = -1
return (
min(
abs(state.hero[0] - demon[0]) + abs(state.hero[1] - demon[1])
for demon in map_rules["D"]
)
+ (map_rules["max"] - state.steps)*0.7
+ keyFactor + lockFactor
)
manDist = abs(state.lock[0] - state.hero[0]) + abs(state.lock[1] - state.hero[1])
if action:
booster = (0.5, 0.25)[action.verb == "unlock/key"]
else:
manDist = min(
abs(state.hero[0] - demon[0]) + abs(state.hero[1] - demon[1])
for demon in map_rules["D"]
)
return manDist*booster + passedCost*1.5
return dist_to_closest_demon
......@@ -381,7 +384,7 @@ def search_heuristic(
) -> Tuple[State, Dict[State, Action]]:
l = []
heapq.heapify(l)
heapq.heappush(l, (heuristic(s0), s0))
heapq.heappush(l, (heuristic(s0, None), s0))
save = {s0: None}
heapq.heapify(l)
while l:
......@@ -397,7 +400,7 @@ def search_heuristic(
save[s2] = (s, a)
if goals(s2):
return s2, save
heapq.heappush(l, (heuristic(s2), s2))
heapq.heappush(l, (heuristic(s2, a), s2))
return None, save
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment