Python code for Artificial Intelligence: Foundations of ...

1

Python code for Artificial Intelligence: Foundations of

Computational Agents

David L. Poole and Alan K. Mackworth

Version 0.9.1 of September 12, 2021.

?David L Poole and Alan K Mackworth 2017-2021. All code is licensed under a Creative Commons Attribution-NonCommercial-

ShareAlike 4.0 International License. See: by-nc-sa/4.0/deed.en US

This document and all the code can be downloaded from or from

The authors and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation contained in this book. The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.



Version 0.9.1

September 12, 2021

Contents

Contents

3

1 Python for Artificial Intelligence

9

1.1 Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.2 Getting Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.3 Running Python . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.4 Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.5 Features of Python . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.5.1 Lists, Tuples, Sets, Dictionaries and Comprehensions . . 11

1.5.2 Functions as first-class objects . . . . . . . . . . . . . . . . 12

1.5.3 Generators and Coroutines . . . . . . . . . . . . . . . . . 14

1.6 Useful Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.6.1 Timing Code . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.6.2 Plotting: Matplotlib . . . . . . . . . . . . . . . . . . . . . 16

1.7 Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

1.7.1 Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

1.7.2 Argmax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.7.3 Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

1.7.4 Dictionary Union . . . . . . . . . . . . . . . . . . . . . . . 19

1.8 Testing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2 Agents and Control

21

2.1 Representing Agents and Environments . . . . . . . . . . . . . 21

2.2 Paper buying agent and environment . . . . . . . . . . . . . . 22

2.2.1 The Environment . . . . . . . . . . . . . . . . . . . . . . . 22

2.2.2 The Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3

4

Contents

2.2.3 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.3 Hierarchical Controller . . . . . . . . . . . . . . . . . . . . . . . 25

2.3.1 Environment . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.3.2 Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.3.3 Middle Layer . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.3.4 Top Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.3.5 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

3 Searching for Solutions

33

3.1 Representing Search Problems . . . . . . . . . . . . . . . . . . 33

3.1.1 Explicit Representation of Search Graph . . . . . . . . . . 34

3.1.2 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

3.1.3 Example Search Problems . . . . . . . . . . . . . . . . . . 37

3.2 Generic Searcher and Variants . . . . . . . . . . . . . . . . . . . 41

3.2.1 Searcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

3.2.2 Frontier as a Priority Queue . . . . . . . . . . . . . . . . . 42 3.2.3 A Search . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

3.2.4 Multiple Path Pruning . . . . . . . . . . . . . . . . . . . . 45

3.3 Branch-and-bound Search . . . . . . . . . . . . . . . . . . . . . 47

4 Reasoning with Constraints

51

4.1 Constraint Satisfaction Problems . . . . . . . . . . . . . . . . . 51

4.1.1 Varaibles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

4.1.2 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . 52

4.1.3 CSPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

4.1.4 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

4.2 A Simple Depth-first Solver . . . . . . . . . . . . . . . . . . . . 61

4.3 Converting CSPs to Search Problems . . . . . . . . . . . . . . . 63

4.4 Consistency Algorithms . . . . . . . . . . . . . . . . . . . . . . 65

4.4.1 Direct Implementation of Domain Splitting . . . . . . . . 67

4.4.2 Domain Splitting as an interface to graph searching . . . 69

4.5 Solving CSPs using Stochastic Local Search . . . . . . . . . . . 71

4.5.1 Any-conflict . . . . . . . . . . . . . . . . . . . . . . . . . . 73

4.5.2 Two-Stage Choice . . . . . . . . . . . . . . . . . . . . . . . 74

4.5.3 Updatable Priority Queues . . . . . . . . . . . . . . . . . 76

4.5.4 Plotting Runtime Distributions . . . . . . . . . . . . . . . 78

4.5.5 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

4.6 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

5 Propositions and Inference

81

5.1 Representing Knowledge Bases . . . . . . . . . . . . . . . . . . 81

5.2 Bottom-up Proofs . . . . . . . . . . . . . . . . . . . . . . . . . . 83

5.3 Top-down Proofs . . . . . . . . . . . . . . . . . . . . . . . . . . 85

5.4 Assumables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86



Version 0.9.1

September 12, 2021

Contents

5

6 Planning with Certainty

91

6.1 Representing Actions and Planning Problems . . . . . . . . . . 91

6.1.1 Robot Delivery Domain . . . . . . . . . . . . . . . . . . . 92

6.1.2 Blocks World . . . . . . . . . . . . . . . . . . . . . . . . . 94

6.2 Forward Planning . . . . . . . . . . . . . . . . . . . . . . . . . . 96

6.2.1 Defining Heuristics for a Planner . . . . . . . . . . . . . . 99

6.3 Regression Planning . . . . . . . . . . . . . . . . . . . . . . . . 101

6.3.1 Defining Heuristics for a Regression Planner . . . . . . . 103

6.4 Planning as a CSP . . . . . . . . . . . . . . . . . . . . . . . . . . 104

6.5 Partial-Order Planning . . . . . . . . . . . . . . . . . . . . . . . 107

7 Supervised Machine Learning

115

7.1 Representations of Data and Predictions . . . . . . . . . . . . . 116

7.1.1 Evaluating Predictions . . . . . . . . . . . . . . . . . . . . 117

7.1.2 Creating Test and Training Sets . . . . . . . . . . . . . . . 119

7.1.3 Importing Data From File . . . . . . . . . . . . . . . . . . 119

7.1.4 Creating Binary Features . . . . . . . . . . . . . . . . . . . 121

7.1.5 Augmented Features . . . . . . . . . . . . . . . . . . . . . 124

7.2 Generic Learner Interface . . . . . . . . . . . . . . . . . . . . . 126

7.3 Learning With No Input Features . . . . . . . . . . . . . . . . . 127

7.3.1 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . 128

7.4 Decision Tree Learning . . . . . . . . . . . . . . . . . . . . . . . 130

7.5 Cross Validation and Parameter Tuning . . . . . . . . . . . . . 134

7.6 Linear Regression and Classification . . . . . . . . . . . . . . . 137

7.6.1 Batched Stochastic Gradient Descent . . . . . . . . . . . . 142

7.7 Deep Neural Network Learning . . . . . . . . . . . . . . . . . 143

7.8 Boosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

8 Reasoning Under Uncertainty

151

8.1 Representing Probabilistic Models . . . . . . . . . . . . . . . . 151

8.2 Representing Factors . . . . . . . . . . . . . . . . . . . . . . . . 152

8.3 Conditional Probability Distributions . . . . . . . . . . . . . . 153

8.3.1 Logistic Regression . . . . . . . . . . . . . . . . . . . . . . 154

8.3.2 Noisy-or . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

8.3.3 Tabular Factors . . . . . . . . . . . . . . . . . . . . . . . . 155

8.4 Graphical Models . . . . . . . . . . . . . . . . . . . . . . . . . . 156

8.4.1 Example Belief Networks . . . . . . . . . . . . . . . . . . 158

8.5 Inference Methods . . . . . . . . . . . . . . . . . . . . . . . . . 163

8.6 Recursive Conditioning . . . . . . . . . . . . . . . . . . . . . . 164

8.7 Variable Elimination . . . . . . . . . . . . . . . . . . . . . . . . 169

8.8 Stochastic Simulation . . . . . . . . . . . . . . . . . . . . . . . . 173

8.8.1 Sampling from a discrete distribution . . . . . . . . . . . 173

8.8.2 Sampling Methods for Belief Network Inference . . . . . 174

8.8.3 Rejection Sampling . . . . . . . . . . . . . . . . . . . . . . 175

8.8.4 Likelihood Weighting . . . . . . . . . . . . . . . . . . . . 176



Version 0.9.1

September 12, 2021

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download