-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidastar.py
48 lines (42 loc) · 1.45 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
42
43
44
45
46
47
48
"""
IDA* Algorithm is implemented in this file.
Algorithm uses State class for operations.
"""
from state import State
from typing import List, Tuple, Optional
from common import *
from copy import deepcopy
import numpy as np
def ida_star(state: State) -> Optional[State]:
if not state.is_solvable:
return None
upper_bound = state.this_dist
while True:
res = search(state, 0, upper_bound, [])
if res[0] == -1:
return res[1]
elif res[0] == 1000:
return None
upper_bound = res[0]
def search(state: State, g_val: int, upper_bound: int, previous: List[np.array]) -> Tuple[int, Optional[State]]:
if g_val > 31:
return 1000, None
if state.get_distance() == 0:
print_helper(state.this_state.take(0, axis=2).flatten())
for arr in reversed(previous):
print_helper(arr)
return -1, state
f_val = g_val + state.this_dist
if f_val > upper_bound:
return f_val, None
min_val = 1000
for i in range(4):
in_state = deepcopy(state)
if in_state.gen_next_state(i):
previous_deep = deepcopy(previous)
previous_deep.append(state.this_state.take(0, axis=2).flatten())
res = search(in_state, g_val + 1, upper_bound, previous_deep)
if res[0] == -1:
return res
min_val = min(min_val, res[0])
return min_val, None