FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Hierarchical by MariannaSpyrakou · Pull Request #943 · aimacode/aima-python · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ipynb  (6) .png  (1) .py  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Binary file added images/refinement.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.
2,813 changes: 386 additions & 2,427 deletions planning.ipynb

Large diffs are not rendered by default.

51 changes: 25 additions & 26 deletions planning.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1308,27 +1308,23 @@ def hierarchical_search(problem, hierarchy):
The problem is a real-world problem defined by the problem class, and the hierarchy is
a dictionary of HLA - refinements (see refinements generator for details)
"""
act = Node(problem.actions[0])
act = Node(problem.init, None, [problem.actions[0]])
frontier = deque()
frontier.append(act)
while True:
if not frontier:
return None
plan = frontier.popleft()
print(plan.state.name)
hla = plan.state # first_or_null(plan)
prefix = None
if plan.parent:
prefix = plan.parent.state.action # prefix, suffix = subseq(plan.state, hla)
outcome = Problem.result(problem, prefix)
if hla is None:
(hla, index) = Problem.find_hla(plan, hierarchy) # finds the first non primitive hla in plan actions
prefix = plan.action[:index]
outcome = Problem(Problem.result(problem.init, prefix), problem.goals , problem.actions )
suffix = plan.action[index+1:]
if not hla: # hla is None and plan is primitive
if outcome.goal_test():
return plan.path()
return plan.action
else:
print("else")
for sequence in Problem.refinements(hla, outcome, hierarchy):
print("...")
frontier.append(Node(plan.state, plan.parent, sequence))
for sequence in Problem.refinements(hla, outcome, hierarchy): # find refinements
frontier.append(Node(outcome.init, plan, prefix + sequence+ suffix))

def result(state, actions):
"""The outcome of applying an action to the current problem"""
Expand Down Expand Up @@ -1365,12 +1361,12 @@ def angelic_search(problem, hierarchy, initialPlan):
if Problem.is_primitive( plan, hierarchy ):
return ([x for x in plan.action])
guaranteed = problem.intersects_goal(pes_reachable_set)
if guaranteed and Problem.making_progress(plan, plan):
if guaranteed and Problem.making_progress(plan, initialPlan):
final_state = guaranteed[0] # any element of guaranteed
#print('decompose')
return Problem.decompose(hierarchy, problem, plan, final_state, pes_reachable_set)
(hla, index) = Problem.find_hla(plan, hierarchy) # there should be at least one HLA/Angelic_HLA, otherwise plan would be primitive.
prefix = plan.action[:index-1]
prefix = plan.action[:index]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good catch!

suffix = plan.action[index+1:]
outcome = Problem(Problem.result(problem.init, prefix), problem.goals , problem.actions )
for sequence in Problem.refinements(hla, outcome, hierarchy): # find refinements
Expand Down Expand Up @@ -1450,30 +1446,33 @@ def find_hla(plan, hierarchy):

def making_progress(plan, initialPlan):
"""
Not correct
Prevents from infinite regression of refinements

Normally should from infinite regression of refinements

Only case covered: when plan contains one action (then there is no regression to be done)
(infinite regression of refinements happens when the algorithm finds a plan that
its pessimistic reachable set intersects the goal inside a call to decompose on the same plan, in the same circumstances)
"""
if (len(plan.action)==1):
return False
for i in range(len(initialPlan)):
if (plan == initialPlan[i]):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is better than the previous version, but I'm not sure if this is sufficient.

return False
return True

def decompose(hierarchy, s_0, plan, s_f, reachable_set):
solution = []
i = max(reachable_set.keys())
while plan.action_pes:
action = plan.action_pes.pop()
i = max(reachable_set.keys())
if (i==0):
return solution
s_i = Problem.find_previous_state(s_f, reachable_set,i, action)
problem = Problem(s_i, s_f , plan.action)
j=0
for x in Problem.angelic_search(problem, hierarchy, [Angelic_Node(s_i, Node(None), [action],[action])]):
solution.insert(j,x)
j+=1
angelic_call = Problem.angelic_search(problem, hierarchy, [Angelic_Node(s_i, Node(None), [action],[action])])
if angelic_call:
for x in angelic_call:
solution.insert(0,x)
else:
return None
s_f = s_i
i-=1
return solution


Expand Down
Loading

Back | FazBrowse Home | New Git URL