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.6 of June 19, 2023.

?David L Poole and Alan K Mackworth 2017-2023. 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.6

June 19, 2023

Contents

Contents

3

1 Python for Artificial Intelligence

9

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

1.2 Getting Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

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

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

1.5 Features of Python . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.5.1 f-strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.5.2 Lists, Tuples, Sets, Dictionaries and Comprehensions . . 12

1.5.3 Functions as first-class objects . . . . . . . . . . . . . . . . 13

1.5.4 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.6 Useful Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

1.6.1 Timing Code . . . . . . . . . . . . . . . . . . . . . . . . . 16

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

1.7 Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

1.7.1 Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

1.7.2 Argmax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

1.7.3 Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

1.7.4 Dictionary Union . . . . . . . . . . . . . . . . . . . . . . . 20

1.8 Testing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2 Agent Architectures and Hierarchical Control

23

2.1 Representing Agents and Environments . . . . . . . . . . . . . 23

2.2 Paper buying agent and environment . . . . . . . . . . . . . . 25

2.2.1 The Environment . . . . . . . . . . . . . . . . . . . . . . . 25

3

4

Contents

2.2.2 The Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.2.3 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.3 Hierarchical Controller . . . . . . . . . . . . . . . . . . . . . . . 28 2.3.1 Environment . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.3.2 Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.3.3 Middle Layer . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.3.4 Top Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.3.5 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

3 Searching for Solutions

37

3.1 Representing Search Problems . . . . . . . . . . . . . . . . . . 37

3.1.1 Explicit Representation of Search Graph . . . . . . . . . . 38

3.1.2 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

3.1.3 Example Search Problems . . . . . . . . . . . . . . . . . . 41

3.2 Generic Searcher and Variants . . . . . . . . . . . . . . . . . . . 45

3.2.1 Searcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

3.2.2 Frontier as a Priority Queue . . . . . . . . . . . . . . . . . 46

3.2.3 A Search . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

3.2.4 Multiple Path Pruning . . . . . . . . . . . . . . . . . . . . 49

3.3 Branch-and-bound Search . . . . . . . . . . . . . . . . . . . . . 51

4 Reasoning with Constraints

55

4.1 Constraint Satisfaction Problems . . . . . . . . . . . . . . . . . 55

4.1.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

4.1.2 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . 56

4.1.3 CSPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

4.1.4 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

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

4.3 Converting CSPs to Search Problems . . . . . . . . . . . . . . . 69

4.4 Consistency Algorithms . . . . . . . . . . . . . . . . . . . . . . 71

4.4.1 Direct Implementation of Domain Splitting . . . . . . . . 74

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

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

4.5.1 Any-conflict . . . . . . . . . . . . . . . . . . . . . . . . . . 80

4.5.2 Two-Stage Choice . . . . . . . . . . . . . . . . . . . . . . . 81

4.5.3 Updatable Priority Queues . . . . . . . . . . . . . . . . . 83

4.5.4 Plotting Run-Time Distributions . . . . . . . . . . . . . . 85

4.5.5 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

4.6 Discrete Optimization . . . . . . . . . . . . . . . . . . . . . . . 87

4.6.1 Branch-and-bound Search . . . . . . . . . . . . . . . . . . 88

5 Propositions and Inference

91

5.1 Representing Knowledge Bases . . . . . . . . . . . . . . . . . . 91

5.2 Bottom-up Proofs (with askables) . . . . . . . . . . . . . . . . . 94

5.3 Top-down Proofs (with askables) . . . . . . . . . . . . . . . . . 96



Version 0.9.6

June 19, 2023

Contents

5

5.4 Debugging and Explanation . . . . . . . . . . . . . . . . . . . . 97 5.5 Assumables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

6 Deterministic Planning

105

6.1 Representing Actions and Planning Problems . . . . . . . . . . 105

6.1.1 Robot Delivery Domain . . . . . . . . . . . . . . . . . . . 106

6.1.2 Blocks World . . . . . . . . . . . . . . . . . . . . . . . . . 108

6.2 Forward Planning . . . . . . . . . . . . . . . . . . . . . . . . . . 110

6.2.1 Defining Heuristics for a Planner . . . . . . . . . . . . . . 113

6.3 Regression Planning . . . . . . . . . . . . . . . . . . . . . . . . 115

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

6.4 Planning as a CSP . . . . . . . . . . . . . . . . . . . . . . . . . . 118

6.5 Partial-Order Planning . . . . . . . . . . . . . . . . . . . . . . . 121

7 Supervised Machine Learning

129

7.1 Representations of Data and Predictions . . . . . . . . . . . . . 130

7.1.1 Creating Boolean Conditions from Features . . . . . . . . 133

7.1.2 Evaluating Predictions . . . . . . . . . . . . . . . . . . . . 135

7.1.3 Creating Test and Training Sets . . . . . . . . . . . . . . . 136

7.1.4 Importing Data From File . . . . . . . . . . . . . . . . . . 137

7.1.5 Augmented Features . . . . . . . . . . . . . . . . . . . . . 140

7.2 Generic Learner Interface . . . . . . . . . . . . . . . . . . . . . 142

7.3 Learning With No Input Features . . . . . . . . . . . . . . . . . 143

7.3.1 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

7.4 Decision Tree Learning . . . . . . . . . . . . . . . . . . . . . . . 147

7.5 Cross Validation and Parameter Tuning . . . . . . . . . . . . . 151

7.6 Linear Regression and Classification . . . . . . . . . . . . . . . 155

7.7 Boosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

7.7.1 Gradient Tree Boosting . . . . . . . . . . . . . . . . . . . . 164

8 Neural Networks and Deep Learning

167

8.1 Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

8.2 Feedforward Networks . . . . . . . . . . . . . . . . . . . . . . . 170

8.3 Improved Optimization . . . . . . . . . . . . . . . . . . . . . . 172

8.3.1 Momentum . . . . . . . . . . . . . . . . . . . . . . . . . . 172

8.3.2 RMS-Prop . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

8.4 Dropout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

8.4.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

9 Reasoning with Uncertainty

181

9.1 Representing Probabilistic Models . . . . . . . . . . . . . . . . 181

9.2 Representing Factors . . . . . . . . . . . . . . . . . . . . . . . . 181

9.3 Conditional Probability Distributions . . . . . . . . . . . . . . 183

9.3.1 Logistic Regression . . . . . . . . . . . . . . . . . . . . . . 183

9.3.2 Noisy-or . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184



Version 0.9.6

June 19, 2023

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

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

Google Online Preview   Download