Donald J. Norris

Beginning Artificial Intelligence with the Raspberry Pi

Donald J. Norris

Barrington, New Hampshire, USA

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-2742-8 . For more detailed information, please visit http://www.apress.com/source-code .

ISBN 978-1-4842-2742-8

e-ISBN 978-1-4842-2743-5

DOI 10.1007/978-1-4842-2743-5

Library of Congress Control Number: 2017943462

© Donald J. Norris 2017

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

This book is dedicated to my good friend and colleague, Dr. Lundy Lewis. Dr. Lewis and I both teach in the Information Technology and Computer Science Department at Southern New Hampshire University, located in Manchester, NH.

Lundy is a recognized expert in the field of artificial intelligence and robotics, often sought after to present talks on those subjects at international conferences and universities. He has been both a mentor and advisor to me in the exciting field of artificial intelligence.

Preface

Artificial intelligence, or AI, is an exciting field and my purpose in writing this book is to convey some of that excitement to you. I will be using the Raspberry Pi single-board computer as the primary tool through which you can explore how AI works and, consequently, gain additional insight on how you might incorporate AI into your projects and/or applications.

I do want to make something perfectly clear at the outset: reading this book and completing all the projects will not make you an expert in AI. This is analogous to the situation where a layperson taking a first aid course could never claim to be a medical doctor or a nurse after taking that course. Becoming an AI expert requires that you take many college courses—both undergraduate and graduate—in a variety of areas, including mathematics, computer science, logic, and even philosophy. There are also AI experts who come from other spheres of interest, including music and the allied arts. Having made the previous statements, I do want you to understand that gaining a reasonable introduction to AI is very achievable by reading this book and other readily available resources. It is just that you should not try to claim that you are an AI expert after reading this book.

I will next discuss why the Raspberry Pi is a good platform with which to examine AI. You should first note that it is a very capable computer on its own merit. Why certainly not as fast nor as memory capable as a modern PC or Mac, it is no slouch, especially when using a Raspberry Pi 3. This model has a clock speed of 1 GHz, uses four cores, and has 1 GB of dynamic ram. This is quite impressive when you realize that this performance comes with a price tag of only $35 (USD). However, the key feature that makes the Raspberry Pi so attractive for AI demonstrations is that it is a microcontroller. This means you can directly control things based upon the outcome of AI events. Microcontrollers also allow sensors to be easily connected to them, thus allowing AI applications a means to interact with the real world.

While PCs can also be set up to both sense and control, it often requires expensive and complex interfaces to achieve these capabilities. The Raspberry Pi was initially designed to be able to sense and control devices with minimal interface requirements, and perhaps more importantly, minimal software requirements. PC software interfaces are often very complex, expensive, and typically proprietary—meaning making user changes or modifications is a difficult-to-impossible task.

The Raspberry Pis that I use in this book use a Raspian Linux distribution named Jessie. This distribution is completely open source and freely available from the Raspberry Pi Foundation’s download website. It is a very stable operating system (OS) and supports several extremely large open-source applications repositories. This means that all the software used in this book is freely available and easily downloadable into the Raspberry Pi.

I use a variety of languages and applications in the book’s various demonstrations and projects. The languages used are mainly Python, Prolog, and the Wolfram Language. Each of these languages brings some unique features that allow the book demonstrations to be quickly and easily implemented.

The main application that I use is Mathematica, which is a full-featured symbolic processing program that also happens to be part of the Jessie distribution. Mathematica is also a commercial program that ordinarily costs hundreds of dollars, but is provided gratis due to the very generous gift of the Wolfram Corporation and Dr. Stephen Wolfram (CEO) in particular.

I tried to layout the book in a logical manner by first introducing AI in Chapter 1 . It is difficult to explain AI to people who have never heard of it, although it is often surprising to inform that them that AI often affects them in their daily lives. I have provided a considerable amount of detail in the first chapter by trying to define AI and how it is commonly applied in everyday life situations. It will soon become apparent to you how invasive AI has become in modern society, whether you like it or not. Please note that I used the term invasive in a non-derogatory way, simply to point out that AI is commonly applied in many areas, some of which will surprise you. In addition, I also discuss the topic of business intelligence (BI), as it is very closely allied to AI and is often the vehicle through which AI affects most people. Some AI practitioners often refer to BI as simply AI applied in a business setting . You will learn that it is much more than that, however. I adopt it because it is a useful simplification.

I next explore some basic AI concepts in Chapter 2 . There is initially some discussion regarding basic logical constructs, as they are important to understand inference, which is an AI core foundation. Expert knowledge systems are next discussed, which constitute a major portion of the more general knowledge management systems (KMS)—an important part of BI. The discussion then turns to machine learning, which is a huge research area in modern AI. Finally, I conclude the chapter with an introduction to fuzzy logic (FL), which is thoroughly demonstrated in a later book project.

Chapter 3 shows you how to implement a practical expert system using the Prolog language. I explore some key Prolog features and explain how this somewhat specialized language is so useful in implementing AI concepts, without requiring extensive programing support as would be necessary if general-purpose languages such as C/C++ or Java were used for the same purposes. A simple console question-and-answer program is used in the practical demonstration.

Chapter 4 focuses on using AI with games. Admittedly, the games are quite simple; however, the chapter’s goal is to simply demonstrate how AI is incorporated into gaming logic. These gaming AI concepts may then be easily expanded to handle much more complex games. I used Python to implement the games, which are controlled through a traditional text-console interface. Do not expect to see World of Warcraft ( WoW )–quality graphics in this chapter, but rest assured that WoW does use AI in its games.

In Chapter 5 , I return to using Prolog to implement some fuzzy logic controls for a practical project demonstration. There is also a simplified expert rules system incorporated into the project. A Raspberry Pi system using both temperature and humidity sensors will control a virtual heating and cooling system.

Chapter 6 introduces the concept of shallow machine learning. A Python program is created, in which the computer “learns” your favorite color and make “decisions” regarding color selection. Finally, I close the chapter with a discussion of adaptive learning, which plays a large role in BI.

Chapter 7 continues the machine learning topic with an examination of machine learning using artificial neural networks (ANN). ANNs are by far the most prevalent AI method used to implement machine learning. I go through a detailed discussion on how an ANN is constructed, and then demonstrate an actual neural network created with Python.

The machine learning continues into Chapter 8 , where deep learning is discussed. In this chapter’s project I go through a detailed discussion on how a multi-layer ANN functions incuding the gradient search feature.

Chapter 9 contains two demonstrations of deep learning using multi-layer ANNs. The first one recognizes hand-wriiten numbers based on the MNIST training and test dataset. The second one uses a Pi Camera with a Raspberry Pi to image a hand-written number and then uses the previously trained ANN to determine the closest match.

Chapter 10 deals with evolutionary computing (EC), which encompasses, but is not limited to, evolutionary programming, genetic algorithms and genetic programming. I have provided several interesting demonstrations highlighting some of the EC features to provide you with a good introduction to this fascinating field.

Chapter 11 discusses subsumption, which is a behavior-based robotic study area. It is closely allied with AI. I use the robot car first introduced in Chapter 7 to conduct several demonstrations. You will quickly realize that a robot employing subsumption behaviors can remarkably mimic actual human behavior, thus completing the AI loop between human thinking and motor behavior.

I am quite confident that after reading through this book and duplicating most—if not all the projects and demonstrations, you will come away with an excellent appreciation of AI and how to incorporate it into your future projects.

Contents

  1. Chapter 1:​ Introduction to Artificial Intelligence
    1. AI Historical Origins
    2. Intelligence
    3. Strong AI vs.​ Weak AI, Broad AI vs.​ Narrow AI
    4. Reasoning
    5. AI Categories
    6. AI and Big Data
    7. Summary
  2. Chapter 2:​ Basic AI Concepts
    1. Boolean Algebra
      1. Some Additional Boolean Laws
    2. Inference
    3. Expert Systems
      1. Conflict Resolution
      2. Backward Chaining
    4. Raspberry Pi Configuration
    5. Introduction to SWI Prolog
    6. Installing Prolog on a Raspberry Pi
    7. Initial Prolog Demonstration
    8. Introduction to Fuzzy Logic
      1. Example of FL
      2. Defuzzification
    9. Problem Solving
      1. Breadth-First Search
      2. Depth-First Search
      3. Depth-Limited Search
      4. Bidirectional Search
      5. Other Problem-Solving Examples
    10. Machine Learning
      1. Prediction
      2. Classification
      3. Further Classification
    11. Neural Networks
    12. Shallow Learning vs.​ Deep Learning
    13. Evolutionary Computing
      1. Genetic Algorithms
    14. Summary
  3. Chapter 3:​ Expert System Demonstrations
    1. Demo 3-1:​ Office Database
    2. Demo 3-2:​ Animal Identification
    3. Demo 3-3:​ tic-tac-toe
    4. Demo 3-4:​ Cold or Flu Diagnosis
    5. Demo 3-5:​ Expert System with Raspberry Pi GPIO Control
      1. Installing PySWIP
      2. Hardware Setup
      3. Rpi.​GPIO Setup
      4. Expert System with LED Control
    6. Summary
  4. Chapter 4:​ Games
    1. Demo 4-1:​ Rock-Paper-Scissors
      1. Rock-Paper-Scissors Game with Switches and LEDs
      2. Interrupts
    2. Demo 4-2:​ Nim
      1. Nim with LCD and Switches
      2. LCD Display
      3. Loading the Adafruit LCD Library
      4. LCD Test
      5. automated_​nim.​py
    3. Summary
  5. Chapter 5:​ Fuzzy Logic System
    1. Parts List
    2. Software Installation
    3. Basic FLS
    4. Initialization:​ Define Linguistic Variables and Terms
    5. Demo 5-1:​ Using FL to Calculate a Tip
    6. Initialization:​ Construct Membership Functions
      1. Membership Function Visualization
    7. Initialization:​ Build Rule Set
    8. Inference:​ Evaluate Fuzzy Set According to Rule Set
    9. Aggregation:​ Combine Results from Each Rule Evaluation
    10. Defuzzification:​ Convert Fuzzy Set to Crisp Output Values
    11. Demo 5-2:​ Modifications to the tipping.​py Program
    12. Demo 5-3:​ FLS Heating and Cooling System
      1. Fuzzification
      2. Inference
      3. Aggregation
      4. Defuzzification
      5. Testing the Control Program
    13. Demo 5-4:​ Modifications to the HVAC Program
    14. Summary
  6. Chapter 6:​ Machine Learning
    1. Parts List
    2. Demo 6-1:​ Color Selection
      1. Algorithm
      2. Roulette Wheel Algorithm
    3. Demo 6-2:​ Autonomous Robot
      1. Autonomous Algorithm
      2. Test Run
      3. Additional Learning
    4. Demo 6-3:​ Adaptive Learning with an Energy Consumption Consideration
      1. Test Run
    5. Summary
  7. Chapter 7:​ Machine Learning:​ Artificial Neural Networks
    1. Parts List
    2. Hopfield Network
    3. Demo 7-1:​ Numerical Figure Recognition Demonstration
    4. Demo 7-2:​ Autonomous Robot Car Using ANN
    5. Demo 7-3:​ Python Control Script for the Obstacle-Avoiding Robot Car
      1. Test Run
    6. Demo 7-4:​ Light-Seeking Robot
      1. The Unknowns
      2. Brain Mapping
      3. Light Intensity Sensor
      4. Python Control Script for the Goal-Seeking Robot Car
      5. Test Run
      6. Obstacle Avoidance and Light Seeking
    7. Summary
  8. Chapter 8:​ Machine Learning:​ Deep Learning
    1. Generalized ANN
      1. Larger ANN
      2. Back Propagation In Three-layer ANNs
      3. Updating the Weighting Matrix
    2. The Gradient Descent Applied to an ANN
      1. Matrix Multiplications for Weight Change Determination
    3. Worked-through Example
      1. Issues with ANN Learning
      2. Initial Weight Selection
    4. Demo 8-1:​ ANN Python Scripts
      1. Initialization
      2. Test Run
    5. Demo 8-2:​ Training an ANN
      1. Test Run
    6. Summary
  9. Chapter 9:​ Machine Learning:​ Practical ANN Demonstrations
    1. Parts List
    2. Demo 9-1:​ MNIST Data Set
      1. Imaging a MNIST Record
      2. Adjusting the Input and Output Data Sets
      3. Configuring the ANN for Handwritten Number Detection
      4. Test Run
    3. Demo 9-2:​ Handwritten Number Recognition with a Pi Camera
      1. Modifying the trainAN.​py Script
      2. Automated Number Recognition with an ANN
      3. Test Run
    4. Summary
  10. Chapter 10:​ Evolutionary Computing
    1. Alife
    2. Evolutionary Programing
    3. Demo 10-1:​ Manual Calculation
      1. Python Script
    4. Demo 10-2:​ Conway’s Game of Life
      1. Sense HAT Hardware Installation
      2. Sense HAT Software Installation
      3. Game of Life:​ Python Version
      4. Test Run
      5. Single Generation of the Game of Life
    5. Summary
  11. Chapter 11:​ Behavior-Based Robotics
    1. Parts List
    2. Human Brain Structure
    3. Subsumption Architecture
      1. Traditional Approach
      2. Behavior-Based Robotics Approach
    4. Demo 11-1:​ The Breve Project
    5. Demo 11-2:​ Building a Subsumption-Controlled Robot Car
    6. Demo 11-3:​ Alfie Robot Car
      1. Adding Another Behavior
      2. Test Run
    7. Summary
  12. Appendix A: Build Instructions for the Alfie Robot Car
  13. Robotic Car Power Supply
  14. CR Servo Drive Pulse Width Modulation (PWM)
  15. Mount Plates
  16. Electrical and Wiring Instructions
  17. Ping Sensor
  18. MCP3008 Analog-to-Digital Converter (ADC)
  19. Software Installation
  20. Final Thoughts
  21. Index

About the Author and About the Technical Reviewer

About the Author

A436848_1_En_BookFrontmatter_Figb_HTML.jpg

Donald J. Norris has a degree in electrical engineering and an MBA specializing in production management. He is currently teaching undergrad and grad courses in the computer science subject area at Southern New Hampshire University. He has also created and taught several robotics courses there. He has over 33 years of teaching experience as an adjunct professor at a variety of colleges and universities.

Don retired from civilian government service with the US Navy, where he specialized in acoustics related to nuclear submarines and associated advanced digital signal processing. Since then, he has spent more than 23 years as a professional software developer using C, C#, C++, Python, MicroPython, Node.js, and Java, as well as 6 years as a certified IT security consultant.

He has written and published six books, including two on the Raspberry Pi, how to build and fly your own drone, MicroPython, the Internet of Things, and the Edison microprocessor.

Don started a consultancy, Norris Embedded Software Solutions (dba NESS LLC), which specializes in developing application solutions using microprocessors and microcontrollers. He likes to think of himself as a perpetual hobbyist and geek and is always trying out new approaches and out-of-the-box experiments. He is a licensed private pilot, photography buff, amateur extra-class radio operator, and an avid runner.

About the Technical Reviewer

A436848_1_En_BookFrontmatter_Figc_HTML.jpg

Massimo Nardone has more than 22 years of experience in security, web/mobile development, and cloud and IT architecture. His true IT passions are security and the Android.

He has been programming and teaching how to program with Android, Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years. He holds a Master of Science degree in computing science from the University of Salerno, Italy.

Massimo has worked as a project manager, software engineer, research engineer, chief security architect, information security manager, PCI/SCADA auditor, and senior lead IT security/cloud/SCADA architect.

His technical skills include security, Android, cloud, Java, MySQL, Drupal, Cobol, Perl, web and mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch, and more.

He currently works as chief information security officer (CISO) for Cargotec Oyj.

He also worked as visiting lecturer and supervisor for exercises at the Networking Laboratory of the Helsinki University of Technology (Aalto University). He holds four international patents (PKI, SIP, SAML, and Proxy areas).

Massimo has reviewed more than 40 IT books for different publishing companies. He is the coauthor of Pro Android Games (Apress, 2015).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.181.145