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

Solving the region-coloring problem


Let's use the Constraint Satisfaction framework to solve the region-coloring problem. Consider the following screenshot:

We have a few regions in the preceding figure that are labeled with names. Our goal is to color with four colors so that no adjacent regions have the same color.

Create a new Python file and import the following packages:

from simpleai.search import CspProblem, backtrack 

Define the constraint that specifies that the values should be different:

# Define the function that imposes the constraint  
# that neighbors should be different 
def constraint_func(names, values): 
    return values[0] != values[1]   

Define the main function and specify the list of names:

if __name__=='__main__':
    # Specify the variables
    names = ('Mark', 'Julia', 'Steve', 'Amanda', 'Brian', 
            'Joanne', 'Derek', 'Allan', 'Michelle', 'Kelly')

Define the list of possible colors:

    # Define the possible colors...