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 a problem with constraints


We have already discussed how Constraint Satisfaction Problems are formulated. Let's apply them to a real-world problem. In this problem, we have a list of names and each name can only take a fixed set of values. We also have a set of constraints between these people that needs to be satisfied. Let's see how to do it.

Create a new Python file and import the following packages:

from simpleai.search import CspProblem, backtrack, \ 
    min_conflicts, MOST_CONSTRAINED_VARIABLE, \ 
    HIGHEST_DEGREE_VARIABLE, LEAST_CONSTRAINING_VALUE 

Define the constraint that specifies that all the variables in the input list should have unique values:

# Constraint that expects all the different variables  
# to have different values 
def constraint_unique(variables, values): 
    # Check if all the values are unique 
    return len(values) == len(set(values))   

Define the constraint that specifies that the first variable should be bigger...