-
Notifications
You must be signed in to change notification settings - Fork 0
/
ida.py
49 lines (44 loc) · 1.41 KB
/
ida.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
49
import random
import time
import heuristic
import board
md_step = 0
hu_step = 0
def ida_run(evaluation_board,method):
if method == "MD":
ida_threshold = heuristic.manhattan_distance_node(evaluation_board)
else:
ida_threshold = heuristic.hungarian_method(evaluation_board)
while True:
search_result = ida_search_function(evaluation_board,0,ida_threshold,method)
if (search_result == -1):
return "GOAL"
ida_threshold = search_result
def ida_search_function(node,g,threshold,method):
global md_step
global hu_step
if method == "MD":
f_value = g + heuristic.manhattan_distance_node(node)
md_step += 1
else:
f_value = g + heuristic.hungarian_method(node)
hu_step += 1
if (f_value >threshold):
return f_value
if (board.check_goal(node)):
print("IDA* Goal achieved:")
if method == "MD":
print("Steps: {}" .format(md_step))
else:
print("Steps: {}" .format(hu_step))
board.draw_board(node)
return -1
min = float("inf")
frontier = board.gen_frontier(node)
for i in range(1, len(frontier), 2):
tempvalue = ida_search_function(frontier[i], g+1, threshold, method)
if (tempvalue == -1):
return -1
if (tempvalue < min):
min = tempvalue
return min