Book Image

Artificial Intelligence with Python

Book Image

Artificial Intelligence with Python

Overview of this book

Artificial Intelligence is becoming increasingly relevant in the modern world. By harnessing the power of algorithms, you can create apps which intelligently interact with the world around you, building intelligent recommender systems, automatic speech recognition systems and more. Starting with AI basics you'll move on to learn how to develop building blocks using data mining techniques. Discover how to make informed decisions about which algorithms to use, and how to apply them to real-world scenarios. This practical book covers a range of topics including predictive analytics and deep learning.
Table of Contents (23 chapters)
Artificial Intelligence with Python
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Building a maze solver


Let's use the A* algorithm to solve a maze. Consider the following figure:

The # symbols indicate obstacles. The symbol o represents the starting point and x represents the goal. Our goal is to find the shortest path from the start to the end point. Let's see how to do it in Python. The following solution is a variant of the solution provided in the simpleai library. Create a new Python file and import the following packages:

import math 
from simpleai.search import SearchProblem, astar 

Create a class that contains the methods needed to solve the problem:

# Class containing the methods to solve the maze 
class MazeSolver(SearchProblem): 

Define the initializer method:

    # Initialize the class  
    def __init__(self, board): 
      self.board = board 
      self.goal = (0, 0) 

Extract the initial and final positions:

        for y in range(len(self.board)):
            for x in range(len(self.board[y])):
          ...