avatarDebby Nirwan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4164

Abstract

an class="hljs-function"><span class="hljs-title">location</span><span class="hljs-params">(object1)</span></span> = Room1 <span class="hljs-function"><span class="hljs-title">location</span><span class="hljs-params">(object2)</span></span> = Room2 <span class="hljs-function"><span class="hljs-title">occupied</span><span class="hljs-params">(Room1)</span></span> = True <span class="hljs-function"><span class="hljs-title">occupied</span><span class="hljs-params">(Room2)</span></span> = True <span class="hljs-function"><span class="hljs-title">occupied</span><span class="hljs-params">(Room3)</span></span> = False</pre></div><p id="f2a7">So, current state of our world is:</p><div id="990d"><pre>current_state = [<span class="hljs-keyword">location</span>(robot) = Room2, <span class="hljs-keyword">location</span>(object1) = Room1, <span class="hljs-keyword">location</span>(object2) = Room2, occupied(Room1) = <span class="hljs-keyword">True</span>, occupied(Room2) = <span class="hljs-keyword">True</span>, occupied(Room3) = <span class="hljs-keyword">False</span>]</pre></div><blockquote id="3908"><p>If one of the state variables in <b>current_state</b> changes, it is another state.</p></blockquote><p id="e575">The code snippet is just for illustration, when we code it is better to create a class to represent the “world”.</p><h1 id="fd89">Actions and Action Templates</h1><p id="53e5">The next part in our planning domain is action. Action is important part of the domain model because this is the component where we predict what the next state will be.</p><p id="b821">Action has the three parts:</p><ul><li>input parameters</li><li>pre-conditions</li><li>effects</li></ul><blockquote id="60d9"><p>Action template is a generic form, action is an instance of action template.</p></blockquote><p id="28b8">Action template is written in the following format:</p><div id="2a95"><pre><span class="hljs-function"><span class="hljs-title">action</span><span class="hljs-params">(*params)</span></span>: cost = <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> <span class="hljs-built_in">preconditions</span>(current_state): return <span class="hljs-built_in">effects</span>(), cost <span class="hljs-keyword">else</span>: return current_state, cost</pre></div><p id="3711">if <b>preconditions()</b> returns true, the <b>effects</b> of the action is returned. Otherwise this action isn’t applicable in current state of the world/environment.</p><p id="9d42">Let’s look at an example. In <b>Example 1</b> above we can add an action, let’s call it <i>move</i>.</p><div id="e228"><pre>move(<span class="hljs-keyword">from</span>, <span class="hljs-keyword">to</span>): cost = <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> preconditions(current_state, <span class="hljs-keyword">from</span>, <span class="hljs-keyword">to</span>): <span class="hljs-built_in"> return</span> effects(), cost <span class="hljs-keyword">else</span>: <span class="hljs-built_in"> return</span> current_state, cost</pre></div><p id="e624">Now, the preconditions:</p><div id="3ab7"><pre>preconditions(current_state, <span class="hljs-keyword">from</span>, <span class="hljs-keyword">to</span>): <span class="hljs-keyword">if</span> current_state.door(<span class="hljs-keyword">from</span>, <span class="hljs-keyword">to</span>) == OPENED: <span class="hljs-keyword">return</span> <span class="hljs-literal">True</span> <span class="hljs-keyword">else</span>: <span class="hljs-keyword">return</span> <span class="hljs-literal">False</span></pre></div><p id="1edc">The effects:</p><div id="84e3"><pre>effects(current_state, <span class="hljs-keyword">from</span>, <span class="hljs-keyword">to</span>): predicted_state = current_state predicted_state.<span class="hljs-keyword">location</span>(robot, <span class="hljs-keyword">to</span>) <span class="hljs-keyword">return</span> predicted_state</pre></div><p id="ec5f">We can see from the example above how the action and action template works, the predicted state will contain robot’s location in the new room. If the door is not opened, the action is not applica

Options

ble.</p><figure id="4d27"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*no00fLtbCpEYqCGV5uE2BA.png"><figcaption>Example 1 — Door is closed</figcaption></figure><h1 id="7347">Plans and Planning Problems</h1><p id="20f7">The final part is plans and planning problems.</p><h2 id="4e70">Plan</h2><p id="c088">The definition of plan is:</p><blockquote id="d675"><p>A finite sequence of actions</p></blockquote><div id="c5e8"><pre><span class="hljs-attr">plan</span> = [action1, action2, action3]</pre></div><p id="d1de">In this example, the length of the plan is 3 and the cost is the total cost of all the actions.</p><div id="ee38"><pre>plan_cost = <span class="hljs-number">0</span> for <span class="hljs-keyword">action</span> <span class="hljs-keyword">in</span> plan: plan_cost = plan_cost + <span class="hljs-keyword">action</span>.cost</pre></div><h2 id="40e9">Planning Problem</h2><blockquote id="dc5c"><p>Planning problem is the tuple of planning domain, initial state and goal(desired) state.</p></blockquote><p id="568c">Planning Problem is the problem that we want to solve, the plan solves the planning problem.</p><div id="72c4"><pre><span class="hljs-attr">planning_problem</span> = PlanningProblem(planning_domain, initial_state, goal_state)</pre></div><p id="246c">Remember that Planning Domain is our tuple of:</p><ul><li>states</li><li>actions</li><li>state-transition functions</li><li>cost</li></ul><p id="5eaf">Now, that we have the complete understanding of the representation, let’s look at one example.</p><p id="5aa5">This is the initial state,</p><figure id="b4b4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*SDTPrVyTnr7kWYSFFseLQA.png"><figcaption>Example 2 — Initial State</figcaption></figure><p id="d80b">This is our goal state,</p><figure id="7909"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*dx1_jNERQxcOj4jvBA_00A.png"><figcaption>Example 2 — Goal State</figcaption></figure><p id="3b93">We add 3 more actions:</p><div id="4096"><pre><span class="hljs-function"><span class="hljs-title">grab</span><span class="hljs-params">(object)</span></span> <span class="hljs-function"><span class="hljs-title">carry</span><span class="hljs-params">(object, from, to)</span></span> <span class="hljs-function"><span class="hljs-title">place</span><span class="hljs-params">(object)</span></span></pre></div><p id="b316">So, we can create the plan as follows to solve this planning problem:</p><div id="63de"><pre>plan = [move(room<span class="hljs-number">2</span>, room<span class="hljs-number">3</span>), move(room<span class="hljs-number">3</span>, room<span class="hljs-number">1</span>), grab(<span class="hljs-keyword">object</span><span class="hljs-number">1</span>), carry(<span class="hljs-keyword">object</span><span class="hljs-number">1</span>, room<span class="hljs-number">1</span>, room<span class="hljs-number">3</span>), place(<span class="hljs-keyword">object</span><span class="hljs-number">1</span>)]</pre></div><blockquote id="6fd2"><p>We say that <b>plan</b> is the <b>solution</b> to our <b>planning problem</b>.</p></blockquote><p id="fbaf">Of course this is a simplified example where there is only one actor, our robot, and the assumption that the world is static.</p><p id="b45a">In later chapter, we’ll see the examples with Python to solve real planning problems.</p><p id="141c">We’ll discuss about search algorithms in the next posts.</p><div id="ea3a" class="link-block"> <a href="https://readmedium.com/using-forward-search-algorithms-to-solve-ai-planning-problems-361ad4910239"> <div> <div> <h2>Using Forward-search algorithms to solve AI Planning Problems</h2> <div><h3>Let’s use deterministic version of forward-search algorithms to solve AI Planning Problems</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*p2DEfarNhM8uBNKfHx7VPQ.png)"></div> </div> </div> </a> </div></article></body>

How do we represent our models?

Before diving into the details, we need to understand how we represent the data.

Representing our models

If you haven’t read the previous post, the introduction, here’s the link.

Book

The posts about “Automated Planning and Acting” here are based on the book with same title by Malik Ghallab, Danau Nau and Paolo Traverso.

Planning Domain

In the previous post, we discussed about descriptive models of actions which describe the states that may happen as result of performing actions.

The models are often called Planning Domain or State-Transition System (think about finite state machine).

A planning domain is a 4-tuple of:

Σ =(S, A, γ, cost)

Where: S: a set of states A: a set of actions γ: state-transition function, or prediction function cost: the cost of performing an action

For state transition function, we can write in the following form: γ(s₀, a) = s₁

Example

An example is a simple door system. S = {opened, closed} A = {open, close}

There are 4 transition functions: γ(opened, open) = opened γ(opened, close) = closed γ(closed, open) = opened γ(closed, close) = closed

For this simple example, let’s assume that the cost is uniform, 1.

Simple Planning Domain

Objects and State Variables

To understand state variables, we start with the meaning of the state in our model.

State is a description of the properties of various objects in the environment — Automated Planning and Acting

That means if we have multiple objects in the environment, we will have one unique state if one of the object’s property changes.

There are two different types of property:

  • Rigid If it remains the same in different states, for example the adjacency of rooms in a floor plan
  • Varying If it may differ in one state and another

State Variable

State variable is represented as x=sv(b₁, …, bᴋ), where:

  • sv: state variable’s name
  • b: object’s name

Let’s look at an example. In our floor plan below, we have 3 objects:

  • robot (circle)
  • objects (rectangle)
  • rooms
Example 1 — Robot and Objects

Rigid relations can be represented as:

Adjacent = [(Room1, Room2), (Room1, Room3), (Room2, Room1), (Room2, Room3), (Room3, Room1), (Room3, Room2)]

This won’t change because they are rigid.

State variables can be represented as follows, (let’s assume that rectangle in Room 1 is object 1):

location(robot) = Room2
location(object1) = Room1
location(object2) = Room2
occupied(Room1) = True
occupied(Room2) = True
occupied(Room3) = False

So, current state of our world is:

current_state = [location(robot) = Room2, location(object1) = Room1, location(object2) = Room2, occupied(Room1) = True, occupied(Room2) = True, occupied(Room3) = False]

If one of the state variables in current_state changes, it is another state.

The code snippet is just for illustration, when we code it is better to create a class to represent the “world”.

Actions and Action Templates

The next part in our planning domain is action. Action is important part of the domain model because this is the component where we predict what the next state will be.

Action has the three parts:

  • input parameters
  • pre-conditions
  • effects

Action template is a generic form, action is an instance of action template.

Action template is written in the following format:

action(*params):
cost = 1
 if preconditions(current_state):
  return effects(), cost
 else:
  return current_state, cost

if preconditions() returns true, the effects of the action is returned. Otherwise this action isn’t applicable in current state of the world/environment.

Let’s look at an example. In Example 1 above we can add an action, let’s call it move.

move(from, to):
 cost = 1
 if preconditions(current_state, from, to):
  return effects(), cost
 else:
  return current_state, cost

Now, the preconditions:

preconditions(current_state, from, to):
 if current_state.door(from, to) == OPENED:
  return True
 else:
  return False

The effects:

effects(current_state, from, to):
 predicted_state = current_state
 predicted_state.location(robot, to)
 return predicted_state

We can see from the example above how the action and action template works, the predicted state will contain robot’s location in the new room. If the door is not opened, the action is not applicable.

Example 1 — Door is closed

Plans and Planning Problems

The final part is plans and planning problems.

Plan

The definition of plan is:

A finite sequence of actions

plan = [action1, action2, action3]

In this example, the length of the plan is 3 and the cost is the total cost of all the actions.

plan_cost = 0
for action in plan:
 plan_cost = plan_cost + action.cost

Planning Problem

Planning problem is the tuple of planning domain, initial state and goal(desired) state.

Planning Problem is the problem that we want to solve, the plan solves the planning problem.

planning_problem = PlanningProblem(planning_domain, initial_state, goal_state)

Remember that Planning Domain is our tuple of:

  • states
  • actions
  • state-transition functions
  • cost

Now, that we have the complete understanding of the representation, let’s look at one example.

This is the initial state,

Example 2 — Initial State

This is our goal state,

Example 2 — Goal State

We add 3 more actions:

grab(object)
carry(object, from, to)
place(object)

So, we can create the plan as follows to solve this planning problem:

plan = [move(room2, room3), move(room3, room1), grab(object1), carry(object1, room1, room3), place(object1)]

We say that plan is the solution to our planning problem.

Of course this is a simplified example where there is only one actor, our robot, and the assumption that the world is static.

In later chapter, we’ll see the examples with Python to solve real planning problems.

We’ll discuss about search algorithms in the next posts.

Automated Planning
Artificial Intelligence
Data Science
Machine Learning
Programming
Recommended from ReadMedium