-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
42 lines (34 loc) · 1.34 KB
/
search.py
File metadata and controls
42 lines (34 loc) · 1.34 KB
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
import util
import copy
from Node import Node
import constants
import sys
from RGBNode import RGBNode
def graphSearch(root_env, fringe, lookahead_size = constants.LOOKAHEAD_SIZE, max_depth = constants.MAX_DEPTH_SIZE, mode = "ram"):
"""Search through the successors of a problem to find a goal.
The argument fringe should be an empty queue. [Fig. 3.18]"""
state = root_env._get_obs()
if mode == "ram":
rootNode = Node(root_env, ale_state=root_env.ale.cloneState())
else:
rootNode = RGBNode(root_env, ale_state=root_env.ale.cloneState())
rootNode.env.frameskip = constants.FRAMESKIP
rootNode.set_content(state)
rootNode.add_new_features()
visited = set()
total_expanded = 0
best_node = None
fringe.push(rootNode)
while not fringe.isEmpty() and total_expanded < lookahead_size:
node = fringe.pop()
if node.isTerminal() or node.depth == max_depth:
continue
n = node.expand()
visited.add(node)
best_node = n if ((best_node and n and best_node.reward <= n.reward) or (best_node is None)) else best_node
total_expanded += 1
for nextnode in node.children:
if nextnode not in visited and nextnode.has_novelty_1():
nextnode.add_new_features()
fringe.push(nextnode)
return best_node.path()