avatarDebby Nirwan

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

4786

Abstract

iv id="7555"><pre>for <span class="hljs-keyword">action</span> <span class="hljs-keyword">in</span> actions: <span class="hljs-keyword">if</span> <span class="hljs-keyword">action</span>.pre(pacman_world): predicted_pacman_world = <span class="hljs-keyword">action</span>.eff()</pre></div><p id="a8eb">The preconditions of all simple actions that we have are:</p><ol><li>the action is legal (not moving towards the wall)</li><li>will not meet ghost</li></ol><p id="95c9">Another thing worth noting here is we provide the cost incurred to perform the action as well to the search algorithm.</p><h1 id="dbdd">Planning Problem</h1><p id="287a">We’ll look at one example of Planning Problem, start with a simple one.</p><p id="9998">From this initial state with 4 foods in the corner and without ghosts.</p><figure id="067e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*t4BKH2a_PGT1ZuVU62lboA.png"><figcaption>Simple Planning Problem with 4 foods in the corners</figcaption></figure><p id="3e4d">We want to have this goal state with 0 food, which means our AI agent, Pacman, has to eat all 4 foods intelligently.</p><figure id="203a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pgmW3pFdY_O-o6XTEeNnIQ.png"><figcaption>Goal State with no food at all in Pacman World</figcaption></figure><h1 id="371b">Algorithms</h1><p id="2890">Let’s look at how deterministic search algorithms solve this problem. In these implementations we are not using heuristics. Heuristics will be discussed in a separate post.</p><h2 id="4ed4">Breadth-first Search</h2><p id="2c05">We implement BFS as an instance of Deterministic search algorithm with node selection and node pruning as follows:</p><ol><li>Node selection Choose a node with shortest length</li><li>Pruning Remove from Children and Frontier nodes which are already in Expanded</li></ol> <figure id="bbcb"> <div> <div>

            <iframe class="gist-iframe" src="/gist/debbynirwan/ceea32a6141af53f2ca10895bf5c5ed4.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="cb87">When running this algorithm on our planning problem we will get optimal solution but long planning time. For relative comparisons, I will write each algorithm’s planning time when I ran it.</p><ul><li>Planning time: 185 seconds</li></ul>
    <figure id="3757">
        <div>
          <div>
            <img class="ratio" src="http://placehold.it/16x9">
            <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FECDlfYZzwSc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DECDlfYZzwSc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FECDlfYZzwSc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="e628">Depth-first Search</h2><ol><li>Node Selection

Choose node with longest length</li><li>Pruning Cycle checking</li></ol> <figure id="4a3d"> <div> <div>

            <iframe class="gist-iframe" src="/gist/debbynirwan/c8853363db07533924249a95993bbfae.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="6602">Running this algorithm on our planning problem gives us non-optimal solution but much shorter planning time.</p><ul><li>Planning time: 37 seconds</li></ul>
    <figure id="e1ec">
        <div>
          <div>
            <img class="ratio" src="http://placehold.it/16x9">
            <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1KG44GW9nZ8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1KG44GW9nZ8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F1KG44GW9nZ8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="1c27">Hill Climbing</h2><ol><li>Node Selection

Choose node that minimizes heuristic, for our implementation this is just any nodes because we have not implemented heuristic function</li><li>Pruning Clear Frontier so that we only follow current path</li></ol><p id="5d71">As we can see in the pruning process

Options

it is not guaranteed that we will have a solution. This algorithm fails to solve our planning problem.</p><h2 id="e6e6">Uniform-cost Search</h2><ol><li>Node Selection Choose node that minimizes total cost</li><li>Pruning Remove from Frontier and Children nodes that are already in Expanded</li></ol> <figure id="01ff"> <div> <div>

            <iframe class="gist-iframe" src="/gist/debbynirwan/7c808ae6e64ce8e3cc34b49733bbb4e8.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="ce80">This algorithm gives us optimal solution and shorter planning time compared to BFS.</p><ul><li>Planning time: 106 seconds</li></ul>
    <figure id="060b">
        <div>
          <div>
            <img class="ratio" src="http://placehold.it/16x9">
            <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1Qcgk_CUW7I%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1Qcgk_CUW7I&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F1Qcgk_CUW7I%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="9be8">A*</h2><ol><li>Node Selection

Choose node that minimizes f(v) = cost + heuristic. For our current implementation this will be exactly the same as Uniform-cost search because we have not implemented heuristics</li><li>Pruning Remove duplicated paths with higher cost</li></ol> <figure id="9cf4"> <div> <div>

            <iframe class="gist-iframe" src="/gist/debbynirwan/a9e6a4df5d2b800fc6a9dbf52d9c7628.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="0fb8">Because of the pruning process, the planning time is shorter than Uniform-cost search. The result is optimal.</p><ul><li>Planning time: 24 seconds</li></ul>
    <figure id="62e9">
        <div>
          <div>
            <img class="ratio" src="http://placehold.it/16x9">
            <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FVhjbGCHG23A%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVhjbGCHG23A&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVhjbGCHG23A%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="d621">Depth-first Branch and Bound (DFSBB)</h2><p id="47bd">This algorithm evaluates all possible paths in DFS and get the best. However, for our planning problem this algorithm takes too long to run. We’ll skip this algorithm this time.</p><h2 id="0289">Greedy Best-First Search (GBFS)</h2><ol><li>Node Selection

Choose node that minimizes heuristic</li><li>Pruning Same as A*</li></ol> <figure id="2277"> <div> <div>

            <iframe class="gist-iframe" src="/gist/debbynirwan/399192c14bb79732b98200e070ec35d9.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="b9c6">Normally this algorithm runs quicker than A*, however because we haven’t implemented heuristic, the planning time is a little bit longer.</p><ul><li>Planning time: 51 seconds</li></ul>
    <figure id="b6a1">
        <div>
          <div>
            <img class="ratio" src="http://placehold.it/16x9">
            <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FLhVUniRS_Vk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DLhVUniRS_Vk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FLhVUniRS_Vk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="fd9e">Now we have looked into different algorithms to solve our first simple planning problem. In the next posts, we’ll solve more difficult planning problems and techniques to solve them.</p></article></body>

Deterministic Search Algorithms Part 1

Learning to solve AI Planning Problems with Deterministic Search Algorithms

Pacman Game

Read previous post to understand the generic implementation of Deterministic-search algorithms.

To understand more easily we use real life examples, a game, to visualize how the algorithms solve the planning problems.

The example that we will use here is Pacman Game. You can read the link for the details of the gameplay and other information. In short, the mission of Pacman is to eat all smaller dots (foods) while avoiding ghosts.

If Pacman meets a ghost, it loses. It wins after it has eaten all foods.

The bigger dots are capsules which when eaten by Pacman, gives temporary power to Pacman to eat the ghosts.

You can search in Github to find the implementation of Pacman game engine written in Python.

Pacman World Representation

For us to solve planning problems in Pacman, first we need to represent how Pacman World looks like. The following are all the objects in Pacman World:

  1. Pacman
  2. Ghosts
  3. Foods
  4. Capsules
  5. Walls

Walls are rigid, the positions and number of walls are fixed. Foods and Capsules are fixed in positions but the number of them can reduce.

Pacman and ghosts do not reduce in number but their positions can change throughout the game.

We can write the Pacman World like shown in the code snippet above. You can see that we have all the objects implemented in our class along with their getters.

The most important thing is the implementation of the equality operators (== and !=), which will be used frequently by the algorithms to compare the states of the world.

Action Templates and State Predictor

The next bit that we need is the action template and its state predictor. There are 4 actions that Pacman can do, they are:

  1. Go up (north)
  2. Go down (south)
  3. Go left (west)
  4. Go right (east)

The implementation of action template looks like this:

Recall that the action templates has input parameters, preconditions and effects. Read this post:

When the algorithm looks for applicable actions, it simply calls precondition functions and to get the predicted state of the world, it simply call effect function.

for action in actions:
 if action.pre(pacman_world):
  predicted_pacman_world = action.eff()

The preconditions of all simple actions that we have are:

  1. the action is legal (not moving towards the wall)
  2. will not meet ghost

Another thing worth noting here is we provide the cost incurred to perform the action as well to the search algorithm.

Planning Problem

We’ll look at one example of Planning Problem, start with a simple one.

From this initial state with 4 foods in the corner and without ghosts.

Simple Planning Problem with 4 foods in the corners

We want to have this goal state with 0 food, which means our AI agent, Pacman, has to eat all 4 foods intelligently.

Goal State with no food at all in Pacman World

Algorithms

Let’s look at how deterministic search algorithms solve this problem. In these implementations we are not using heuristics. Heuristics will be discussed in a separate post.

Breadth-first Search

We implement BFS as an instance of Deterministic search algorithm with node selection and node pruning as follows:

  1. Node selection Choose a node with shortest length
  2. Pruning Remove from Children and Frontier nodes which are already in Expanded

When running this algorithm on our planning problem we will get optimal solution but long planning time. For relative comparisons, I will write each algorithm’s planning time when I ran it.

  • Planning time: 185 seconds

Depth-first Search

  1. Node Selection Choose node with longest length
  2. Pruning Cycle checking

Running this algorithm on our planning problem gives us non-optimal solution but much shorter planning time.

  • Planning time: 37 seconds

Hill Climbing

  1. Node Selection Choose node that minimizes heuristic, for our implementation this is just any nodes because we have not implemented heuristic function
  2. Pruning Clear Frontier so that we only follow current path

As we can see in the pruning process it is not guaranteed that we will have a solution. This algorithm fails to solve our planning problem.

Uniform-cost Search

  1. Node Selection Choose node that minimizes total cost
  2. Pruning Remove from Frontier and Children nodes that are already in Expanded

This algorithm gives us optimal solution and shorter planning time compared to BFS.

  • Planning time: 106 seconds

A*

  1. Node Selection Choose node that minimizes f(v) = cost + heuristic. For our current implementation this will be exactly the same as Uniform-cost search because we have not implemented heuristics
  2. Pruning Remove duplicated paths with higher cost

Because of the pruning process, the planning time is shorter than Uniform-cost search. The result is optimal.

  • Planning time: 24 seconds

Depth-first Branch and Bound (DFSBB)

This algorithm evaluates all possible paths in DFS and get the best. However, for our planning problem this algorithm takes too long to run. We’ll skip this algorithm this time.

Greedy Best-First Search (GBFS)

  1. Node Selection Choose node that minimizes heuristic
  2. Pruning Same as A*

Normally this algorithm runs quicker than A*, however because we haven’t implemented heuristic, the planning time is a little bit longer.

  • Planning time: 51 seconds

Now we have looked into different algorithms to solve our first simple planning problem. In the next posts, we’ll solve more difficult planning problems and techniques to solve them.

Automated Planning
Artificial Intelligence
Search Algorithm
Data Science
Machine Learning
Recommended from ReadMedium