Python Code for AI: Foundations of Computational Agents

Telechargé par korteigelevinas
1
Python code for
Artificial Intelligence
Foundations of Computational Agents
David L. Poole and Alan K. Mackworth
Version 0.9.18 of April 15, 2026.
https://aipython.org https://artint.info
©David L Poole and Alan K Mackworth 2017-2025.
All code is licensed under a Creative Commons Attribution-NonCommercial-
ShareAlike 4.0 International License. See: https://creativecommons.org/licenses/
by-nc-sa/4.0/deed.en
This document and all the code can be downloaded from
https://artint.info/AIPython/ or from https://aipython.org
The authors and publisher of this book have used their best efforts in prepar-
ing this book. These efforts include the development, research and testing of
the 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.
https://aipython.org Version 0.9.18 April 15, 2026
Contents
Contents 3
1 Python for Artificial Intelligence 9
1.1 WhyPython? ............................ 9
1.2 GettingPython ........................... 10
1.3 RunningPython .......................... 10
1.4 Pitfalls ................................ 11
1.5 FeaturesofPython ......................... 11
1.5.1 f-strings ............................ 11
1.5.2 Lists, Tuples, Sets, Dictionaries and Comprehensions . . 12
1.5.3 Generators........................... 13
1.5.4 Functions as first-class objects . . . . . . . . . . . . . . . . 14
1.6 UsefulLibraries........................... 16
1.6.1 TimingCode ......................... 16
1.6.2 Plotting: Matplotlib . . . . . . . . . . . . . . . . . . . . . 16
1.7 Utilities................................ 18
1.7.1 Display............................. 18
1.7.2 Argmax ............................ 19
1.7.3 Probability........................... 20
1.8 TestingCode............................. 21
2 Agent Architectures and Hierarchical Control 25
2.1 Representing Agents and Environments . . . . . . . . . . . . . 25
2.2 Paper buying agent and environment . . . . . . . . . . . . . . 27
2.2.1 The Environment . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.2 TheAgent........................... 28
3
4Contents
2.2.3 Plotting ............................ 29
2.3 Hierarchical Controller . . . . . . . . . . . . . . . . . . . . . . . 31
2.3.1 Body .............................. 31
2.3.2 MiddleLayer ......................... 33
2.3.3 TopLayer ........................... 35
2.3.4 World ............................. 35
2.3.5 Plotting ............................ 36
3 Searching for Solutions 41
3.1 Representing Search Problems . . . . . . . . . . . . . . . . . . 41
3.1.1 Explicit Representation of Search Graph . . . . . . . . . . 43
3.1.2 Paths.............................. 45
3.1.3 Example Search Problems . . . . . . . . . . . . . . . . . . 47
3.2 Generic Searcher and Variants . . . . . . . . . . . . . . . . . . . 54
3.2.1 Searcher ............................ 54
3.2.2 GUI for Tracing Search . . . . . . . . . . . . . . . . . . . . 55
3.2.3 Frontier as a Priority Queue . . . . . . . . . . . . . . . . . 60
3.2.4 ASearch ........................... 61
3.2.5 Multiple Path Pruning . . . . . . . . . . . . . . . . . . . . 63
3.3 Branch-and-bound Search . . . . . . . . . . . . . . . . . . . . . 65
4 Reasoning with Constraints 69
4.1 Constraint Satisfaction Problems . . . . . . . . . . . . . . . . . 69
4.1.1 Variables............................ 69
4.1.2 Constraints .......................... 70
4.1.3 CSPs .............................. 71
4.1.4 Examples ........................... 74
4.2 A Simple Depth-first Solver . . . . . . . . . . . . . . . . . . . . 83
4.3 Converting CSPs to Search Problems . . . . . . . . . . . . . . . 85
4.4 Consistency Algorithms . . . . . . . . . . . . . . . . . . . . . . 87
4.4.1 Direct Implementation of Domain Splitting . . . . . . . . 89
4.4.2 Consistency GUI . . . . . . . . . . . . . . . . . . . . . . . 91
4.4.3 Domain Splitting as an interface to graph searching . . . 94
4.5 Solving CSPs using Stochastic Local Search . . . . . . . . . . . 96
4.5.1 Any-conict.......................... 98
4.5.2 Two-Stage Choice . . . . . . . . . . . . . . . . . . . . . . . 99
4.5.3 Updatable Priority Queues . . . . . . . . . . . . . . . . . 101
4.5.4 Plotting Run-Time Distributions . . . . . . . . . . . . . . 103
4.5.5 Testing............................. 104
4.6 Discrete Optimization . . . . . . . . . . . . . . . . . . . . . . . 105
4.6.1 Branch-and-bound Search . . . . . . . . . . . . . . . . . . 107
5 Propositions and Inference 109
5.1 Representing Knowledge Bases . . . . . . . . . . . . . . . . . . 109
5.2 Provers................................ 113
https://aipython.org Version 0.9.18 April 15, 2026
Contents 5
5.3 Bottom-up Proofs (with askables) . . . . . . . . . . . . . . . . . 113
5.4 Top-down Proofs (with askables) . . . . . . . . . . . . . . . . . 115
5.5 Top-down with Tabling . . . . . . . . . . . . . . . . . . . . . . 116
5.6 Debugging and Explanation . . . . . . . . . . . . . . . . . . . . 119
5.7 Assumables ............................. 123
5.8 Negation-as-failure . . . . . . . . . . . . . . . . . . . . . . . . . 128
6 Deterministic Planning 131
6.1 Representing Actions and Planning Problems . . . . . . . . . . 131
6.1.1 Robot Delivery Domain . . . . . . . . . . . . . . . . . . . 132
6.1.2 BlocksWorld ......................... 134
6.2 ForwardPlanning.......................... 136
6.2.1 Defining Heuristics for a Planner . . . . . . . . . . . . . . 139
6.3 Regression Planning . . . . . . . . . . . . . . . . . . . . . . . . 141
6.3.1 Defining Heuristics for a Regression Planner . . . . . . . 143
6.4 PlanningasaCSP.......................... 144
6.5 Partial-Order Planning . . . . . . . . . . . . . . . . . . . . . . . 148
7 Supervised Machine Learning 155
7.1 Representations of Data and Predictions . . . . . . . . . . . . . 156
7.1.1 Creating Boolean Conditions from Features . . . . . . . . 159
7.1.2 Evaluating Predictions . . . . . . . . . . . . . . . . . . . . 161
7.1.3 Creating Test and Training Sets . . . . . . . . . . . . . . . 163
7.1.4 Importing Data From File . . . . . . . . . . . . . . . . . . 164
7.1.5 Augmented Features . . . . . . . . . . . . . . . . . . . . . 167
7.2 Generic Learner Interface . . . . . . . . . . . . . . . . . . . . . 169
7.3 Learning With No Input Features . . . . . . . . . . . . . . . . . 170
7.3.1 Evaluation........................... 173
7.4 Decision Tree Learning . . . . . . . . . . . . . . . . . . . . . . . 174
7.5 k-fold Cross Validation and Parameter Tuning . . . . . . . . . 179
7.6 Linear Regression and Classification . . . . . . . . . . . . . . . 182
7.6.1 RatingData .......................... 189
7.7 Boosting ............................... 195
7.7.1 Gradient Tree Boosting . . . . . . . . . . . . . . . . . . . . 198
8 Neural Networks and Deep Learning 201
8.1 Layers ................................ 201
8.1.1 LinearLayer.......................... 202
8.1.2 ReLULayer .......................... 204
8.1.3 SigmoidLayer......................... 205
8.2 Feedforward Networks . . . . . . . . . . . . . . . . . . . . . . . 205
8.3 Optimizers.............................. 207
8.3.1 Stochastic Gradient Descent . . . . . . . . . . . . . . . . . 207
8.3.2 Momentum .......................... 208
8.3.3 RMS-Prop ........................... 209
https://aipython.org Version 0.9.18 April 15, 2026
1 / 442 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!