{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "6nOTljC_mTMn" }, "source": [ "# Simulated Effects\n", "This python notebook shows the simulated effects usage in the unified planning library.\n", "\n", "[![Open In GitHub](https://img.shields.io/badge/see-Github-579aca?logo=github)](https:///github.com/aiplan4eu/unified-planning/blob/master/docs/notebooks/04-simulated-effects.ipynb)\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/aiplan4eu/unified-planning/blob/master/docs/notebooks/04-simulated-effects.ipynb)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "t8dCcpf7mivV" }, "source": [ "## Setup" ] }, { "cell_type": "markdown", "metadata": { "id": "CwlvEzKrm1jT" }, "source": [ "First, we install unified_planning library and its dependencies from PyPi. Here, we use the `--pre` flag to use the latest development build." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "BoqALxJWdfl8", "tags": [ "remove_from_CI" ] }, "outputs": [], "source": [ "%pip install unified-planning[tamer]" ] }, { "cell_type": "markdown", "metadata": { "id": "iNHFHxQKnKIp" }, "source": [ "We are now ready to use the Unified-Planning library!" ] }, { "cell_type": "markdown", "metadata": { "id": "9dP5scv7nNJu" }, "source": [ "## Demo\n" ] }, { "cell_type": "markdown", "metadata": { "id": "DXhHD7EAmxVx" }, "source": [ "### Basic imports\n", "The basic imports we need for this demo are abstracted in the `shortcuts` package." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "fiRk2F1llJO-" }, "outputs": [], "source": [ "from unified_planning.shortcuts import *" ] }, { "cell_type": "markdown", "metadata": { "id": "fXA1NuXgkntV" }, "source": [ "### Problem definition" ] }, { "cell_type": "markdown", "metadata": { "id": "JXDIDNwCm13b" }, "source": [ "We start the problem modeling defining the `UserType` and the `Fluent`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "nHXzkD2LmWkB" }, "outputs": [], "source": [ "Location = UserType('Location')\n", "Robot = UserType('Robot')\n", "\n", "at = Fluent('at', Location, robot=Robot)\n", "battery_charge = Fluent('battery_charge', IntType(0, 100), robot=Robot)" ] }, { "cell_type": "markdown", "metadata": { "id": "OVUn5VPTmXKY" }, "source": [ "We define an action `move` with a simulated effect that models the battery consumption.\n", "\n", "A `SimulatedEffect` instance can affect a list of fluent expressions, in this case only `battery_charge(robot)`.\n", "The function `fun` performs the computation of the simulated effect decreasing the battery value by 10. This function receives as parameters the problem, the state in which the effect is applied, and the actual parameters of the action instance whose effect is being calculated.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "cwXh99K2laqv" }, "outputs": [], "source": [ "move = InstantaneousAction('move', robot=Robot, l_from=Location, l_to=Location)\n", "robot = move.parameter('robot')\n", "l_from = move.parameter('l_from')\n", "l_to = move.parameter('l_to')\n", "move.add_precondition(Equals(at(robot), l_from))\n", "move.add_precondition(GE(battery_charge(robot), 10))\n", "move.add_precondition(Not(Equals(l_from, l_to)))\n", "move.add_effect(at(robot), l_to)\n", "def fun(problem, state, actual_params):\n", " value = state.get_value(battery_charge(actual_params.get(robot))).constant_value()\n", " return [Int(value - 10)]\n", "move.set_simulated_effect(SimulatedEffect([battery_charge(robot)], fun))" ] }, { "cell_type": "markdown", "metadata": { "id": "C92YH1nrmRb7" }, "source": [ "Finally, we define the `Object` instances and, after creating the `Problem`, we set the initial values and the goal." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "MczotRommR_k" }, "outputs": [], "source": [ "l1 = Object('l1', Location)\n", "l2 = Object('l2', Location)\n", "r1 = Object('r1', Robot)\n", "\n", "problem = Problem('robot_with_simulated_effects')\n", "problem.add_fluent(at)\n", "problem.add_fluent(battery_charge)\n", "problem.add_action(move)\n", "problem.add_object(l1)\n", "problem.add_object(l2)\n", "problem.add_object(r1)\n", "\n", "problem.set_initial_value(at(r1), l1)\n", "problem.set_initial_value(battery_charge(r1), 100)\n", "\n", "problem.add_goal(Equals(at(r1), l2))" ] }, { "cell_type": "markdown", "metadata": { "id": "pXvh83ljlabv" }, "source": [ "### Solving the problem" ] }, { "cell_type": "markdown", "metadata": { "id": "McV4znupqpkw" }, "source": [ "We solve the problem automatically selecting a suitable planner." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "bXHWJh2vl5RJ" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[96m\u001b[1mNOTE: To disable printing of planning engine credits, add this line to your code: `up.shortcuts.get_environment().credits_stream = None`\n", "\u001b[0m\u001b[96m *** Credits ***\n", "\u001b[0m\u001b[96m * In operation mode `OneshotPlanner` at line 1 of `/tmp/ipykernel_153950/3190604266.py`, \u001b[0m\u001b[96myou are using the following planning engine:\n", "\u001b[0m\u001b[96m * Engine name: Tamer\n", " * Developers: FBK Tamer Development Team\n", "\u001b[0m\u001b[96m * Description: \u001b[0m\u001b[96mTamer offers the capability to generate a plan for classical, numerical and temporal problems.\n", " * For those kind of problems tamer also offers the possibility of validating a submitted plan.\u001b[0m\u001b[96m\n", "\u001b[0m\u001b[96m\n", "\u001b[0mTamer returned: [move(r1, l1, l2)]\n" ] } ], "source": [ "with OneshotPlanner(problem_kind=problem.kind) as planner:\n", " result = planner.solve(problem)\n", " print(\"%s returned: %s\" % (planner.name, result.plan))" ] } ], "metadata": { "celltoolbar": "Tags", "colab": { "collapsed_sections": [], "name": "Simulated Effects", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" }, "vscode": { "interpreter": { "hash": "fcfc934ecfdac8ddac62d6a80ba8d82faf47dc8d54fd6a313f0c016b85ebec0e" } } }, "nbformat": 4, "nbformat_minor": 1 }