-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDAstar.py
41 lines (33 loc) · 1.44 KB
/
IDAstar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from MapHandler import MapState
from Successor import Successor
from GoalStateChecker import isGoalState
from Heuristic import heuristic
def IDAstarSearch(initialState: MapState):
def Search(currState, cost, limit):
if isGoalState(currState):
currState.cost += int(
currState.gameMap[currState.playerLocation[0]][currState.playerLocation[1]][0])
return currState, True
if cost >= limit:
return None, False # not found
newCost = float('inf')
found = False
successors = Successor(currState, set())
for successor in successors:
try:
if ((successor.playerLocation, tuple(successor.ballsLocations), tuple(successor.obstacles))) != (currState.parent.playerLocation, tuple(currState.parent.ballsLocations), tuple(currState.parent.obstacles)):
newCost = cost + heuristic(successor) + successor.cost
result, found = Search(successor, newCost, limit)
except:
newCost = cost + heuristic(successor) + successor.cost
result, found = Search(successor, newCost, limit)
if found:
return result, True
return None, False # not found
limit = heuristic(initialState)
while True:
result, found = Search(initialState, 0, limit)
if found:
return result
limit += 1
return None