-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForwardSearch.py
92 lines (74 loc) · 2.81 KB
/
ForwardSearch.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from queue import PriorityQueue
from State import State
def forward_search(goal_state, initial_state, actions):
fringe = [initial_state]
in_fringe = [initial_state.hash()]
explored = []
while fringe:
current_state = fringe.pop(0)
in_fringe.pop(0)
explored.append(current_state.hash())
successors = get_successors(current_state, goal_state, actions)
for successor in successors:
if goal_test(successor, goal_state):
print_solution(successor)
return
else:
if successor.hash() not in in_fringe and successor.hash() not in explored:
fringe.append(successor)
in_fringe.append(successor.hash())
def forward_search_heuristic(goal_state, initial_state, actions):
fringe = PriorityQueue()
fringe.put(initial_state)
in_fringe = [initial_state.hash()]
explored = []
while not fringe.empty():
current_state = fringe.get(0)
in_fringe.pop(0)
explored.append(current_state.hash())
successors = get_successors(current_state, goal_state, actions)
for successor in successors:
if goal_test(successor, goal_state):
print_solution(successor)
return
else:
if successor.hash() not in in_fringe and successor.hash() not in explored:
fringe.put(successor)
in_fringe.append(successor.hash())
def get_successors(state, goal, actions):
result = []
for action in actions:
if action.is_relevantForward(state):
successor=State(state,action,state.positive_literals,state.negative_literals)
action.progress(successor)
successor.heuristic = ignorePreconditions(successor, goal)
result.append(successor)
return result
def ignorePreconditions(state, goal):
cost = 0
for pos in goal.positive_literals:
if pos not in state.positive_literals:
cost += 1
for pos in goal.positive_literals:
if pos in state.negative_literals:
cost += 1
return cost
def ignoreDeleteList(state, goal):
cost = 0
for p in goal.positive_literals:
if p not in state.positive_literals:
cost += 1
return cost
def goal_test(state, goal_state):
for positive_literal in goal_state.positive_literals:
if positive_literal not in state.positive_literals:
return False
for positive_literal in goal_state.positive_literals:
if positive_literal in state.negative_literals:
return False
return True
def print_solution(state):
if state.action == None or state.parent == None:
return
print_solution(state.parent)
print(state.action.to_string())