Book Image

Artificial Intelligence with Python - Second Edition

By : Prateek Joshi
Book Image

Artificial Intelligence with Python - Second Edition

By: Prateek Joshi

Overview of this book

Artificial Intelligence with Python, Second Edition is an updated and expanded version of the bestselling guide to artificial intelligence using the latest version of Python 3.x. Not only does it provide you an introduction to artificial intelligence, this new edition goes further by giving you the tools you need to explore the amazing world of intelligent apps and create your own applications. This edition also includes seven new chapters on more advanced concepts of Artificial Intelligence, including fundamental use cases of AI; machine learning data pipelines; feature selection and feature engineering; AI on the cloud; the basics of chatbots; RNNs and DL models; and AI and Big Data. Finally, this new edition explores various real-world scenarios and teaches you how to apply relevant AI algorithms to a wide swath of problems, starting with the most basic AI concepts and progressively building from there to solve more difficult challenges so that by the end, you will have gained a solid understanding of, and when best to use, these many artificial intelligence techniques.
Table of Contents (26 chapters)
Other Books You May Enjoy

Building an intelligent robot controller

Let's see how to build a robot controller using a GA. We are given a map with targets sprinkled all over it.

The map looks like this. The hashes represent the targets that the robot needs to hit:

Figure 11: A map with targets that an AI robot needs to hit, with targets represented by hashes

There are 124 targets in the preceding map. The goal of the robot controller is to automatically traverse the map and consume all those targets. This program is a variant of the artificial ant program given in the deap library.

Create a new Python file and import the following:

import copy
import random
from functools import partial
import numpy as np
from deap import algorithms, base, creator, tools, gp

Create the class to control the robot:

class RobotController(object):
    def __init__(self, max_moves): 
        self.max_moves = max_moves
        self.moves = 0
        self.consumed = 0