{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### TOTAL ORDER PLANNER\n",
"\n",
"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*:\n",
"
\n",
"If *a* ≤ *b* and *b* ≤ *a*, then *a* = *b* (antisymmetry).\n",
"
\n",
"If *a* ≤ *b* and *b* ≤ *c*, then *a* ≤ *c* (transitivity).\n",
"
\n",
"*a* ≤ *b* or *b* ≤ *a* (connex relation).\n",
"\n",
"
\n",
"In simpler terms, a total order plan is a linear ordering of actions to be taken to reach the goal state.\n",
"There may be several different total-order plans for a particular goal depending on the problem.\n",
"
\n",
"
\n",
"In the module, the `Linearize` class solves problems using this paradigm.\n",
"At its core, the `Linearize` uses a solved planning graph from `GraphPlan` and finds a valid total-order solution for it.\n",
"Let's have a look at the class."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from planning import *\n",
"from notebook import psource"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
class Linearize:\n",
"\n",
" def __init__(self, planningproblem):\n",
" self.planningproblem = planningproblem\n",
"\n",
" def filter(self, solution):\n",
" """Filter out persistence actions from a solution"""\n",
"\n",
" new_solution = []\n",
" for section in solution[0]:\n",
" new_section = []\n",
" for operation in section:\n",
" if not (operation.op[0] == 'P' and operation.op[1].isupper()):\n",
" new_section.append(operation)\n",
" new_solution.append(new_section)\n",
" return new_solution\n",
"\n",
" def orderlevel(self, level, planningproblem):\n",
" """Return valid linear order of actions for a given level"""\n",
"\n",
" for permutation in itertools.permutations(level):\n",
" temp = copy.deepcopy(planningproblem)\n",
" count = 0\n",
" for action in permutation:\n",
" try:\n",
" temp.act(action)\n",
" count += 1\n",
" except:\n",
" count = 0\n",
" temp = copy.deepcopy(planningproblem)\n",
" break\n",
" if count == len(permutation):\n",
" return list(permutation), temp\n",
" return None\n",
"\n",
" def execute(self):\n",
" """Finds total-order solution for a planning graph"""\n",
"\n",
" graphplan_solution = GraphPlan(self.planningproblem).execute()\n",
" filtered_solution = self.filter(graphplan_solution)\n",
" ordered_solution = []\n",
" planningproblem = self.planningproblem\n",
" for level in filtered_solution:\n",
" level_solution, planningproblem = self.orderlevel(level, planningproblem)\n",
" for element in level_solution:\n",
" ordered_solution.append(element)\n",
"\n",
" return ordered_solution\n",
"