Skip to content

Commit

Permalink
Done a pass with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrico Scala authored and Enrico Scala committed Oct 6, 2024
1 parent 368e0d4 commit 974e3bf
Show file tree
Hide file tree
Showing 53 changed files with 1,019 additions and 817 deletions.
58 changes: 37 additions & 21 deletions docs/notebooks/01-basic-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
},
"outputs": [],
"source": [
"Location = UserType('Location')"
"Location = UserType(\"Location\")"
]
},
{
Expand All @@ -137,8 +137,10 @@
},
"outputs": [],
"source": [
"robot_at = unified_planning.model.Fluent('robot_at', BoolType(), l=Location)\n",
"connected = unified_planning.model.Fluent('connected', BoolType(), l_from=Location, l_to=Location)"
"robot_at = unified_planning.model.Fluent(\"robot_at\", BoolType(), l=Location)\n",
"connected = unified_planning.model.Fluent(\n",
" \"connected\", BoolType(), l_from=Location, l_to=Location\n",
")"
]
},
{
Expand Down Expand Up @@ -185,9 +187,11 @@
}
],
"source": [
"move = unified_planning.model.InstantaneousAction('move', l_from=Location, l_to=Location)\n",
"l_from = move.parameter('l_from')\n",
"l_to = move.parameter('l_to')\n",
"move = unified_planning.model.InstantaneousAction(\n",
" \"move\", l_from=Location, l_to=Location\n",
")\n",
"l_from = move.parameter(\"l_from\")\n",
"l_to = move.parameter(\"l_to\")\n",
"move.add_precondition(connected(l_from, l_to))\n",
"move.add_precondition(robot_at(l_from))\n",
"move.add_effect(robot_at(l_from), False)\n",
Expand All @@ -214,7 +218,7 @@
},
"outputs": [],
"source": [
"problem = unified_planning.model.Problem('robot')\n",
"problem = unified_planning.model.Problem(\"robot\")\n",
"problem.add_fluent(robot_at, default_initial_value=False)\n",
"problem.add_fluent(connected, default_initial_value=False)\n",
"problem.add_action(move)"
Expand All @@ -238,7 +242,7 @@
"outputs": [],
"source": [
"NLOC = 10\n",
"locations = [unified_planning.model.Object('l%s' % i, Location) for i in range(NLOC)]\n",
"locations = [unified_planning.model.Object(\"l%s\" % i, Location) for i in range(NLOC)]\n",
"problem.add_objects(locations)"
]
},
Expand All @@ -263,7 +267,7 @@
"source": [
"problem.set_initial_value(robot_at(locations[0]), True)\n",
"for i in range(NLOC - 1):\n",
" problem.set_initial_value(connected(locations[i], locations[i+1]), True)"
" problem.set_initial_value(connected(locations[i], locations[i + 1]), True)"
]
},
{
Expand Down Expand Up @@ -386,7 +390,7 @@
}
],
"source": [
"with OneshotPlanner(name='pyperplan') as planner:\n",
"with OneshotPlanner(name=\"pyperplan\") as planner:\n",
" result = planner.solve(problem)\n",
" if result.status == up.engines.PlanGenerationResultStatus.SOLVED_SATISFICING:\n",
" print(\"Pyperplan returned: %s\" % result.plan)\n",
Expand Down Expand Up @@ -511,9 +515,9 @@
"plan = result.plan\n",
"with PlanValidator(problem_kind=problem.kind, plan_kind=plan.kind) as validator:\n",
" if validator.validate(problem, plan):\n",
" print('The plan is valid')\n",
" print(\"The plan is valid\")\n",
" else:\n",
" print('The plan is invalid')"
" print(\"The plan is invalid\")"
]
},
{
Expand Down Expand Up @@ -693,20 +697,26 @@
}
],
"source": [
"with Compiler(problem_kind=problem.kind, compilation_kind=CompilationKind.GROUNDING) as grounder:\n",
"with Compiler(\n",
" problem_kind=problem.kind, compilation_kind=CompilationKind.GROUNDING\n",
") as grounder:\n",
" grounding_result = grounder.compile(problem, CompilationKind.GROUNDING)\n",
" ground_problem = grounding_result.problem\n",
" print(ground_problem)\n",
"\n",
" # The grounding_result can be used to \"lift\" a ground plan back to the level of the original problem\n",
" with OneshotPlanner(problem_kind=ground_problem.kind) as planner:\n",
" ground_plan = planner.solve(ground_problem).plan\n",
" print('Ground plan: %s' % ground_plan)\n",
" print(\"Ground plan: %s\" % ground_plan)\n",
" # Replace the action instances of the grounded plan with their correspoding lifted version\n",
" lifted_plan = ground_plan.replace_action_instances(grounding_result.map_back_action_instance)\n",
" print('Lifted plan: %s' % lifted_plan)\n",
" lifted_plan = ground_plan.replace_action_instances(\n",
" grounding_result.map_back_action_instance\n",
" )\n",
" print(\"Lifted plan: %s\" % lifted_plan)\n",
" # Test the problem and plan validity\n",
" with PlanValidator(problem_kind=problem.kind, plan_kind=ground_plan.kind) as validator:\n",
" with PlanValidator(\n",
" problem_kind=problem.kind, plan_kind=ground_plan.kind\n",
" ) as validator:\n",
" ground_validation = validator.validate(ground_problem, ground_plan)\n",
" lift_validation = validator.validate(problem, lifted_plan)\n",
" Valid = up.engines.ValidationResultStatus.VALID\n",
Expand Down Expand Up @@ -771,8 +781,10 @@
}
],
"source": [
"with OneshotPlanner(names=['tamer', 'tamer', 'pyperplan'],\n",
" params=[{'heuristic': 'hadd'}, {'heuristic': 'hmax'}, {}]) as planner:\n",
"with OneshotPlanner(\n",
" names=[\"tamer\", \"tamer\", \"pyperplan\"],\n",
" params=[{\"heuristic\": \"hadd\"}, {\"heuristic\": \"hmax\"}, {}],\n",
") as planner:\n",
" plan = planner.solve(problem).plan\n",
" print(\"%s returned: %s\" % (planner.name, plan))"
]
Expand Down Expand Up @@ -810,18 +822,22 @@
"from functools import partial\n",
"import os, uuid, tempfile as tf\n",
"\n",
"\n",
"# Define the function that will be executed instead\n",
"def _function(original_function, *args, **kwargs):\n",
" try:\n",
" original_function(*args, **kwargs)\n",
" except Exception as e:\n",
" if \"could not locate runnable browser\" in str(e):\n",
" original_function(*args, **kwargs,\n",
" filename=f\"{os.path.join(tf.gettempdir(), str(uuid.uuid1()))}.png\"\n",
" original_function(\n",
" *args,\n",
" **kwargs,\n",
" filename=f\"{os.path.join(tf.gettempdir(), str(uuid.uuid1()))}.png\",\n",
" )\n",
" else:\n",
" raise e\n",
"\n",
"\n",
"# Iterate over all the functions of the plot package\n",
"for function_name, function in getmembers(plot, isfunction):\n",
" # Override the original function with the new one\n",
Expand Down
56 changes: 30 additions & 26 deletions docs/notebooks/01-numeric-planning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@
},
"outputs": [],
"source": [
"Counter = UserType('Counter')\n",
"Counter = UserType(\"Counter\")\n",
"\n",
"value = Fluent('value', IntType(), m=Counter)"
"value = Fluent(\"value\", IntType(), m=Counter)"
]
},
{
Expand All @@ -136,15 +136,15 @@
},
"outputs": [],
"source": [
"inc = InstantaneousAction('increment',c=Counter)\n",
"c = inc.parameter('c')\n",
"inc = InstantaneousAction(\"increment\", c=Counter)\n",
"c = inc.parameter(\"c\")\n",
"inc.add_precondition(LE(value(c), 10))\n",
"inc.add_increase_effect(value(c), 1)\n",
"\n",
"dec = InstantaneousAction('decrement',c=Counter)\n",
"c = dec.parameter('c')\n",
"dec = InstantaneousAction(\"decrement\", c=Counter)\n",
"c = dec.parameter(\"c\")\n",
"dec.add_precondition(GT(value(c), 0))\n",
"dec.add_decrease_effect(value(c),1)\n"
"dec.add_decrease_effect(value(c), 1)"
]
},
{
Expand Down Expand Up @@ -219,19 +219,21 @@
}
],
"source": [
"problem = Problem('problem')\n",
"problem = Problem(\"problem\")\n",
"\n",
"problem.add_fluent(value, default_initial_value=0)\n",
"C0 = Object('c0', Counter)\n",
"C1 = Object('c1', Counter)\n",
"C2 = Object('c2', Counter)\n",
"C0 = Object(\"c0\", Counter)\n",
"C1 = Object(\"c1\", Counter)\n",
"C2 = Object(\"c2\", Counter)\n",
"problem.add_object(C0)\n",
"problem.add_object(C1)\n",
"problem.add_object(C2)\n",
"problem.add_action(inc)\n",
"problem.add_action(dec)\n",
"problem.add_goal(And( GE(value(C2),Plus(value(C1),1)), GE(value(C1),Plus(value(C0),1))))\n",
"problem\n"
"problem.add_goal(\n",
" And(GE(value(C2), Plus(value(C1), 1)), GE(value(C1), Plus(value(C0), 1)))\n",
")\n",
"problem"
]
},
{
Expand Down Expand Up @@ -315,17 +317,17 @@
}
],
"source": [
"N = 9 # This is the number of counters\n",
"N = 9 # This is the number of counters\n",
"\n",
"p2 = Problem('Large_problems')\n",
"p2 = Problem(\"Large_problems\")\n",
"\n",
"p2.add_fluent(value, default_initial_value=0)\n",
"p2.add_objects([Object(f'c{i}',Counter) for i in range(N)])\n",
"p2.add_objects([Object(f\"c{i}\", Counter) for i in range(N)])\n",
"p2.add_action(inc)\n",
"p2.add_action(dec)\n",
"\n",
"for i in range(N-1):\n",
" p2.add_goal(GE(value(p2.object(f'c{i+1}')),Plus(value(p2.object(f'c{i}')),1)))\n",
"for i in range(N - 1):\n",
" p2.add_goal(GE(value(p2.object(f\"c{i+1}\")), Plus(value(p2.object(f\"c{i}\")), 1)))\n",
"\n",
"p2"
]
Expand Down Expand Up @@ -372,7 +374,7 @@
}
],
"source": [
"with OneshotPlanner(name='enhsp') as planner:\n",
"with OneshotPlanner(name=\"enhsp\") as planner:\n",
" result = planner.solve(problem)\n",
" plan = result.plan\n",
" if plan is not None:\n",
Expand Down Expand Up @@ -514,28 +516,30 @@
"source": [
"from unified_planning.model.metrics import MinimizeSequentialPlanLength\n",
"\n",
"N = 7 #This is the number of counters\n",
"N = 7 # This is the number of counters\n",
"\n",
"mediumSizeProblem = Problem('Medium_sized_problem')\n",
"mediumSizeProblem = Problem(\"Medium_sized_problem\")\n",
"\n",
"mediumSizeProblem.add_fluent(value, default_initial_value=0)\n",
"mediumSizeProblem.add_objects([Object(f'c{i}',Counter) for i in range(N)])\n",
"mediumSizeProblem.add_objects([Object(f\"c{i}\", Counter) for i in range(N)])\n",
"mediumSizeProblem.add_action(inc)\n",
"mediumSizeProblem.add_action(dec)\n",
"metric = MinimizeSequentialPlanLength()\n",
"mediumSizeProblem.add_quality_metric(metric)\n",
"\n",
"for i in range(N-1):\n",
" mediumSizeProblem.add_goal(GE(value(p2.object(f'c{i+1}')),Plus(value(p2.object(f'c{i}')),1)))\n",
"for i in range(N - 1):\n",
" mediumSizeProblem.add_goal(\n",
" GE(value(p2.object(f\"c{i+1}\")), Plus(value(p2.object(f\"c{i}\")), 1))\n",
" )\n",
"\n",
"with OneshotPlanner(problem_kind=problem.kind,optimality_guarantee=True) as planner:\n",
"with OneshotPlanner(problem_kind=problem.kind, optimality_guarantee=True) as planner:\n",
" result = planner.solve(mediumSizeProblem)\n",
" plan = result.plan\n",
" if plan is not None:\n",
" print(\"%s returned:\" % planner.name)\n",
" print(plan)\n",
" else:\n",
" print(\"No plan found.\")\n"
" print(\"No plan found.\")"
]
}
],
Expand Down
8 changes: 2 additions & 6 deletions docs/notebooks/02-optimal-planning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@
"metadata": {},
"outputs": [],
"source": [
"problem.add_quality_metric(\n",
" up.model.metrics.MinimizeActionCosts({a: 10, b: 1, c: 1})\n",
")"
"problem.add_quality_metric(up.model.metrics.MinimizeActionCosts({a: 10, b: 1, c: 1}))"
]
},
{
Expand Down Expand Up @@ -328,9 +326,7 @@
},
"outputs": [],
"source": [
"expected_plan = up.plans.SequentialPlan(\n",
" [up.plans.ActionInstance(a)]\n",
")\n",
"expected_plan = up.plans.SequentialPlan([up.plans.ActionInstance(a)])\n",
"assert final_report.status == PlanGenerationResultStatus.SOLVED_OPTIMALLY\n",
"assert plan == expected_plan"
]
Expand Down
Loading

0 comments on commit 974e3bf

Please sign in to comment.