In mathematical terminology, total order, linear order or simple order refers to a set X which is said to be totally ordered under ≤ if the following statements hold for all a, b and c in X:
from planning import *
from notebook import psource
psource(Linearize)
class Linearize:
def __init__(self, planningproblem):
self.planningproblem = planningproblem
def filter(self, solution):
"""Filter out persistence actions from a solution"""
new_solution = []
for section in solution[0]:
new_section = []
for operation in section:
if not (operation.op[0] == 'P' and operation.op[1].isupper()):
new_section.append(operation)
new_solution.append(new_section)
return new_solution
def orderlevel(self, level, planningproblem):
"""Return valid linear order of actions for a given level"""
for permutation in itertools.permutations(level):
temp = copy.deepcopy(planningproblem)
count = 0
for action in permutation:
try:
temp.act(action)
count += 1
except:
count = 0
temp = copy.deepcopy(planningproblem)
break
if count == len(permutation):
return list(permutation), temp
return None
def execute(self):
"""Finds total-order solution for a planning graph"""
graphplan_solution = GraphPlan(self.planningproblem).execute()
filtered_solution = self.filter(graphplan_solution)
ordered_solution = []
planningproblem = self.planningproblem
for level in filtered_solution:
level_solution, planningproblem = self.orderlevel(level, planningproblem)
for element in level_solution:
ordered_solution.append(element)
return ordered_solution
The filter
method removes the persistence actions (if any) from the planning graph representation.
# total-order solution for air_cargo problem
Linearize(air_cargo()).execute()
[Load(C1, P1, SFO), Fly(P1, SFO, JFK), Load(C2, P2, JFK), Fly(P2, JFK, SFO), Unload(C2, P2, SFO), Unload(C1, P1, JFK)]
# total-order solution for spare_tire problem
Linearize(spare_tire()).execute()
[Remove(Spare, Trunk), Remove(Flat, Axle), PutOn(Spare, Axle)]
# total-order solution for three_block_tower problem
Linearize(three_block_tower()).execute()
[MoveToTable(C, A), Move(B, Table, C), Move(A, Table, B)]
# total-order solution for simple_blocks_world problem
Linearize(simple_blocks_world()).execute()
[ToTable(A, B), FromTable(B, A), FromTable(C, B)]
# total-order solution for socks_and_shoes problem
Linearize(socks_and_shoes()).execute()
[RightSock, LeftSock, RightShoe, LeftShoe]