microsoft/AI-For-Beginners

Public

mirrored fromhttps://github.com/microsoft/AI-For-BeginnersAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fa78bc6fb0b30eea0678c27a54b915b79ad16fe8

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

lessons/2-Symbolic/Animals.ipynb

464lines · modecode

1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {
6 "collapsed": true
7 },
8 "source": [
9 "# Implementing an Animal Expert System\n",
10 "\n",
11 "An example from [AI for Beginners Curriculum](http://github.com/microsoft/ai-for-beginners).\n",
12 "\n",
13 "In this sample, we will implement a simple knowledge-based system to determine an animal based on some physical characteristics. The system can be represented by the following AND-OR tree (this is a part of the whole tree, we can easily add some more rules):\n",
14 "\n",
15 "![](images/AND-OR-Tree.png)"
16 ]
17 },
18 {
19 "cell_type": "markdown",
20 "metadata": {},
21 "source": [
22 "## Our own expert systems shell with backward inference\n",
23 "\n",
24 "Let's try to define a simple language for knowledge representation based on production rules. We will use Python classes as keywords to define rules. There would be essentially 3 types of classes:\n",
25 "* `Ask` represents a question that needs to be asked to the user. It contains the set of possible answers.\n",
26 "* `If` represents a rule, and it is just a syntactic sugar to store the content of the rule\n",
27 "* `AND`/`OR` are classes to represent AND/OR branches of the tree. They just store the list of arguments inside. To simplify code, all functionality is defined in the parent class `Content`"
28 ]
29 },
30 {
31 "cell_type": "code",
32 "execution_count": 1,
33 "metadata": {
34 "trusted": true
35 },
36 "outputs": [],
37 "source": [
38 "class Ask():\n",
39 " def __init__(self,choices=['y','n']):\n",
40 " self.choices = choices\n",
41 " def ask(self):\n",
42 " if max([len(x) for x in self.choices])>1:\n",
43 " for i,x in enumerate(self.choices):\n",
44 " print(\"{0}. {1}\".format(i,x),flush=True)\n",
45 " x = int(input())\n",
46 " return self.choices[x]\n",
47 " else:\n",
48 " print(\"/\".join(self.choices),flush=True)\n",
49 " return input()\n",
50 "\n",
51 "class Content():\n",
52 " def __init__(self,x):\n",
53 " self.x=x\n",
54 " \n",
55 "class If(Content):\n",
56 " pass\n",
57 "\n",
58 "class AND(Content):\n",
59 " pass\n",
60 "\n",
61 "class OR(Content):\n",
62 " pass"
63 ]
64 },
65 {
66 "cell_type": "markdown",
67 "metadata": {},
68 "source": [
69 "In our system, working memory would contain the list of **facts** as **attribute-value pairs**. The knowledgebase can be defined as one big dictionary that maps actions (new facts that should be inserted into working memory) to conditions, expressed as AND-OR expressions. Also, some facts can be `Ask`-ed."
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 2,
75 "metadata": {
76 "trusted": true
77 },
78 "outputs": [],
79 "source": [
80 "rules = {\n",
81 " 'default': Ask(['y','n']),\n",
82 " 'color' : Ask(['red-brown','black and white','other']),\n",
83 " 'pattern' : Ask(['dark stripes','dark spots']),\n",
84 " 'mammal': If(OR(['hair','gives milk'])),\n",
85 " 'carnivor': If(OR([AND(['sharp teeth','claws','forward-looking eyes']),'eats meat'])),\n",
86 " 'ungulate': If(['mammal',OR(['has hooves','chews cud'])]),\n",
87 " 'bird': If(OR(['feathers',AND(['flies','lies eggs'])])),\n",
88 " 'animal:monkey' : If(['mammal','carnivor','color:red-brown','pattern:dark spots']),\n",
89 " 'animal:tiger' : If(['mammal','carnivor','color:red-brown','pattern:dark stripes']),\n",
90 " 'animal:giraffe' : If(['ungulate','long neck','long legs','pattern:dark spots']),\n",
91 " 'animal:zebra' : If(['ungulate','pattern:dark stripes']),\n",
92 " 'animal:ostrich' : If(['bird','long nech','color:black and white','cannot fly']),\n",
93 " 'animal:pinguin' : If(['bird','swims','color:black and white','cannot fly']),\n",
94 " 'animal:albatross' : If(['bird','flies well'])\n",
95 "}"
96 ]
97 },
98 {
99 "cell_type": "markdown",
100 "metadata": {},
101 "source": [
102 "To perform the backward inference, we will define `Knowledgebase` class. It will contain:\n",
103 "* Working `memory` - a dictionary that maps attributes to values\n",
104 "* Knowledgebase `rules` in the format as defined above\n",
105 "\n",
106 "Two main methods are:\n",
107 "* `get` to obtain the value of an attribute, performing inference if necessary. For example, `get('color')` would get the value of a color slot (it will ask if necessary, and store the value for later usage in the working memory). If we ask `get('color:blue')`, it will ask for a color, and then return `y`/`n` value depending on the color.\n",
108 "* `eval` performs the actual inference, i.e. traverses AND/OR tree, evaluates sub-goals, etc."
109 ]
110 },
111 {
112 "cell_type": "code",
113 "execution_count": 3,
114 "metadata": {
115 "trusted": true
116 },
117 "outputs": [],
118 "source": [
119 "class KnowledgeBase():\n",
120 " def __init__(self,rules):\n",
121 " self.rules = rules\n",
122 " self.memory = {}\n",
123 " \n",
124 " def get(self,name):\n",
125 " if ':' in name:\n",
126 " k,v = name.split(':')\n",
127 " vv = self.get(k)\n",
128 " return 'y' if v==vv else 'n'\n",
129 " if name in self.memory.keys():\n",
130 " return self.memory[name]\n",
131 " for fld in self.rules.keys():\n",
132 " if fld==name or fld.startswith(name+\":\"):\n",
133 " # print(\" + proving {}\".format(fld))\n",
134 " value = 'y' if fld==name else fld.split(':')[1]\n",
135 " res = self.eval(self.rules[fld],field=name)\n",
136 " if res!='y' and res!='n' and value=='y':\n",
137 " self.memory[name] = res\n",
138 " return res\n",
139 " if res=='y':\n",
140 " self.memory[name] = value\n",
141 " return value\n",
142 " # field is not found, using default\n",
143 " res = self.eval(self.rules['default'],field=name)\n",
144 " self.memory[name]=res\n",
145 " return res\n",
146 " \n",
147 " def eval(self,expr,field=None):\n",
148 " # print(\" + eval {}\".format(expr))\n",
149 " if isinstance(expr,Ask):\n",
150 " print(field)\n",
151 " return expr.ask()\n",
152 " elif isinstance(expr,If):\n",
153 " return self.eval(expr.x)\n",
154 " elif isinstance(expr,AND) or isinstance(expr,list):\n",
155 " expr = expr.x if isinstance(expr,AND) else expr\n",
156 " for x in expr:\n",
157 " if self.eval(x)=='n':\n",
158 " return 'n'\n",
159 " return 'y'\n",
160 " elif isinstance(expr,OR):\n",
161 " for x in expr.x:\n",
162 " if self.eval(x)=='y':\n",
163 " return 'y'\n",
164 " return 'n'\n",
165 " elif isinstance(expr,str):\n",
166 " return self.get(expr)\n",
167 " else:\n",
168 " print(\"Unknown expr: {}\".format(expr))"
169 ]
170 },
171 {
172 "cell_type": "markdown",
173 "metadata": {},
174 "source": [
175 "Now let's define our animal knowledgebase and perform the consultation. Note that this call will ask you questions. You can answer by typing `y`/`n` for yes-no questions, or by specifying number (0..N) for questions with longer multiple-choice answers."
176 ]
177 },
178 {
179 "cell_type": "code",
180 "execution_count": 4,
181 "metadata": {
182 "trusted": true
183 },
184 "outputs": [
185 {
186 "name": "stdout",
187 "output_type": "stream",
188 "text": [
189 "hair\n",
190 "y/n\n",
191 "sharp teeth\n",
192 "y/n\n",
193 "claws\n",
194 "y/n\n",
195 "forward-looking eyes\n",
196 "y/n\n",
197 "color\n",
198 "0. red-brown\n",
199 "1. black and white\n",
200 "2. other\n",
201 "has hooves\n",
202 "y/n\n",
203 "long neck\n",
204 "y/n\n",
205 "long legs\n",
206 "y/n\n",
207 "pattern\n",
208 "0. dark stripes\n",
209 "1. dark spots\n"
210 ]
211 },
212 {
213 "data": {
214 "text/plain": [
215 "'giraffe'"
216 ]
217 },
218 "execution_count": 4,
219 "metadata": {},
220 "output_type": "execute_result"
221 }
222 ],
223 "source": [
224 "kb = KnowledgeBase(rules)\n",
225 "kb.get('animal')"
226 ]
227 },
228 {
229 "cell_type": "markdown",
230 "metadata": {},
231 "source": [
232 "## Using Experta for Forward Inference\n",
233 "\n",
234 "In the next example, we will try to implement forward inference using one of the libraries for knowledge representation, [Experta](https://github.com/nilp0inter/experta). **Experta** is a library for creating forward inference systems in Python, which is designed to be similar to classical old system [CLIPS](http://www.clipsrules.net/index.html). \n",
235 "\n",
236 "We could have also implemented forward chaining ourselves without many problems, but naive implementations are usually not very efficient. For more effective rule matching a special algorithm [Rete](https://en.wikipedia.org/wiki/Rete_algorithm) is used."
237 ]
238 },
239 {
240 "cell_type": "code",
241 "execution_count": 5,
242 "metadata": {
243 "trusted": true
244 },
245 "outputs": [
246 {
247 "name": "stdout",
248 "output_type": "stream",
249 "text": [
250 "Collecting git+https://github.com/nilp0inter/experta\n",
251 " Cloning https://github.com/nilp0inter/experta to /tmp/pip-req-build-7qurtwk3\n",
252 " Running command git clone --filter=blob:none --quiet https://github.com/nilp0inter/experta /tmp/pip-req-build-7qurtwk3\n",
253 " Resolved https://github.com/nilp0inter/experta to commit c6d5834b123861f5ae09e7d07027dc98bec58741\n",
254 " Installing build dependencies ... \u001b[?25ldone\n",
255 "\u001b[?25h Getting requirements to build wheel ... \u001b[?25ldone\n",
256 "\u001b[?25h Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n",
257 "\u001b[?25hRequirement already satisfied: frozendict~=2.4.6 in /opt/conda/envs/ai4beg/lib/python3.12/site-packages (from experta==1.9.5.dev1) (2.4.7)\n",
258 "Collecting schema~=0.6.7 (from experta==1.9.5.dev1)\n",
259 " Downloading schema-0.6.8-py2.py3-none-any.whl.metadata (14 kB)\n",
260 "Downloading schema-0.6.8-py2.py3-none-any.whl (14 kB)\n",
261 "Building wheels for collected packages: experta\n",
262 " Building wheel for experta (pyproject.toml) ... \u001b[?25ldone\n",
263 "\u001b[?25h Created wheel for experta: filename=experta-1.9.5.dev1-py3-none-any.whl size=34804 sha256=888c459512a5e713f4b674caa9a0f96cfdf07ec0d6eb56cc318ce0653d218014\n",
264 " Stored in directory: /tmp/pip-ephem-wheel-cache-1eeii9zy/wheels/3d/e8/bb/22d7956359603fa8dd679aa09f5b8efb3f29991c3986fdc787\n",
265 "Successfully built experta\n",
266 "Installing collected packages: schema, experta\n",
267 "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2/2\u001b[0m [experta]\n",
268 "\u001b[1A\u001b[2KSuccessfully installed experta-1.9.5.dev1 schema-0.6.8\n"
269 ]
270 }
271 ],
272 "source": [
273 "import sys\n",
274 "!{sys.executable} -m pip install git+https://github.com/nilp0inter/experta"
275 ]
276 },
277 {
278 "cell_type": "code",
279 "execution_count": 13,
280 "metadata": {
281 "trusted": true
282 },
283 "outputs": [],
284 "source": [
285 "from experta import *\n",
286 "#import experta"
287 ]
288 },
289 {
290 "cell_type": "markdown",
291 "metadata": {},
292 "source": [
293 "We will define our system as a class that subclasses `KnowledgeEngine`. Each rule is defined by a separate function with `@Rule` annotation, which specifies when the rule should fire. Inside the rule, we can add new facts using `declare` function, and adding those facts will result in some more rules being called by forward inference engine. "
294 ]
295 },
296 {
297 "cell_type": "code",
298 "execution_count": 14,
299 "metadata": {
300 "trusted": true
301 },
302 "outputs": [],
303 "source": [
304 "class Animals(KnowledgeEngine):\n",
305 " @Rule(OR(\n",
306 " AND(Fact('sharp teeth'),Fact('claws'),Fact('forward looking eyes')),\n",
307 " Fact('eats meat')))\n",
308 " def cornivor(self):\n",
309 " self.declare(Fact('carnivor'))\n",
310 " \n",
311 " @Rule(OR(Fact('hair'),Fact('gives milk')))\n",
312 " def mammal(self):\n",
313 " self.declare(Fact('mammal'))\n",
314 "\n",
315 " @Rule(Fact('mammal'),\n",
316 " OR(Fact('has hooves'),Fact('chews cud')))\n",
317 " def hooves(self):\n",
318 " self.declare('ungulate')\n",
319 " \n",
320 " @Rule(OR(Fact('feathers'),AND(Fact('flies'),Fact('lays eggs'))))\n",
321 " def bird(self):\n",
322 " self.declare('bird')\n",
323 " \n",
324 " @Rule(Fact('mammal'),Fact('carnivor'),\n",
325 " Fact(color='red-brown'),\n",
326 " Fact(pattern='dark spots'))\n",
327 " def monkey(self):\n",
328 " self.declare(Fact(animal='monkey'))\n",
329 "\n",
330 " @Rule(Fact('mammal'),Fact('carnivor'),\n",
331 " Fact(color='red-brown'),\n",
332 " Fact(pattern='dark stripes'))\n",
333 " def tiger(self):\n",
334 " self.declare(Fact(animal='tiger'))\n",
335 "\n",
336 " @Rule(Fact('ungulate'),\n",
337 " Fact('long neck'),\n",
338 " Fact('long legs'),\n",
339 " Fact(pattern='dark spots'))\n",
340 " def giraffe(self):\n",
341 " self.declare(Fact(animal='giraffe'))\n",
342 "\n",
343 " @Rule(Fact('ungulate'),\n",
344 " Fact(pattern='dark stripes'))\n",
345 " def zebra(self):\n",
346 " self.declare(Fact(animal='zebra'))\n",
347 "\n",
348 " @Rule(Fact('bird'),\n",
349 " Fact('long neck'),\n",
350 " Fact('cannot fly'),\n",
351 " Fact(color='black and white'))\n",
352 " def straus(self):\n",
353 " self.declare(Fact(animal='ostrich'))\n",
354 "\n",
355 " @Rule(Fact('bird'),\n",
356 " Fact('swims'),\n",
357 " Fact('cannot fly'),\n",
358 " Fact(color='black and white'))\n",
359 " def pinguin(self):\n",
360 " self.declare(Fact(animal='pinguin'))\n",
361 "\n",
362 " @Rule(Fact('bird'),\n",
363 " Fact('flies well'))\n",
364 " def albatros(self):\n",
365 " self.declare(Fact(animal='albatross'))\n",
366 " \n",
367 " @Rule(Fact(animal=MATCH.a))\n",
368 " def print_result(self,a):\n",
369 " print('Animal is {}'.format(a))\n",
370 " \n",
371 " def factz(self,l):\n",
372 " for x in l:\n",
373 " self.declare(x)"
374 ]
375 },
376 {
377 "cell_type": "markdown",
378 "metadata": {},
379 "source": [
380 "Once we have defined a knowledgebase, we populate our working memory with some initial facts, and then call `run()` method to perform the inference. You can see as a result that new inferred facts are added to the working memory, including the final fact about the animal (if we set up all the initial facts correctly)."
381 ]
382 },
383 {
384 "cell_type": "code",
385 "execution_count": 15,
386 "metadata": {
387 "trusted": true
388 },
389 "outputs": [
390 {
391 "name": "stdout",
392 "output_type": "stream",
393 "text": [
394 "Animal is tiger\n"
395 ]
396 },
397 {
398 "data": {
399 "text/plain": [
400 "FactList([(0, InitialFact()),\n",
401 " (1, Fact(color='red-brown')),\n",
402 " (2, Fact(pattern='dark stripes')),\n",
403 " (3, Fact('sharp teeth')),\n",
404 " (4, Fact('claws')),\n",
405 " (5, Fact('forward looking eyes')),\n",
406 " (6, Fact('gives milk')),\n",
407 " (7, Fact('mammal')),\n",
408 " (8, Fact('carnivor')),\n",
409 " (9, Fact(animal='tiger'))])"
410 ]
411 },
412 "execution_count": 15,
413 "metadata": {},
414 "output_type": "execute_result"
415 }
416 ],
417 "source": [
418 "ex1 = Animals()\n",
419 "ex1.reset()\n",
420 "ex1.factz([\n",
421 " Fact(color='red-brown'),\n",
422 " Fact(pattern='dark stripes'),\n",
423 " Fact('sharp teeth'),\n",
424 " Fact('claws'),\n",
425 " Fact('forward looking eyes'),\n",
426 " Fact('gives milk')])\n",
427 "ex1.run()\n",
428 "ex1.facts"
429 ]
430 },
431 {
432 "cell_type": "code",
433 "execution_count": null,
434 "metadata": {},
435 "outputs": [],
436 "source": []
437 }
438 ],
439 "metadata": {
440 "kernelspec": {
441 "display_name": "Python 3.7.4 64-bit (conda)",
442 "metadata": {
443 "interpreter": {
444 "hash": "86193a1ab0ba47eac1c69c1756090baa3b420b3eea7d4aafab8b85f8b312f0c5"
445 }
446 },
447 "name": "python3"
448 },
449 "language_info": {
450 "codemirror_mode": {
451 "name": "ipython",
452 "version": 3
453 },
454 "file_extension": ".py",
455 "mimetype": "text/x-python",
456 "name": "python",
457 "nbconvert_exporter": "python",
458 "pygments_lexer": "ipython3",
459 "version": "3.11.2"
460 }
461 },
462 "nbformat": 4,
463 "nbformat_minor": 2
464}
465