Newer
Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
"None\n",
"['Right']\n",
"['Left', 'Left']\n"
]
}
],
"source": [
"transition = {'A': {'Left': 'A', 'Right': 'B'},\n",
" 'B': {'Left': 'A', 'Right': 'C'},\n",
" 'C': {'Left': 'B', 'Right': 'C'}}\n",
"\n",
"\n",
"print(SAT_plan('A', transition, 'C', 2)) \n",
"print(SAT_plan('A', transition, 'B', 3))\n",
"print(SAT_plan('C', transition, 'A', 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us do the same for another transition."
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Right', 'Down']\n"
]
}
],
"source": [
"transition = {(0, 0): {'Right': (0, 1), 'Down': (1, 0)},\n",
" (0, 1): {'Left': (1, 0), 'Down': (1, 1)},\n",
" (1, 0): {'Right': (1, 0), 'Up': (1, 0), 'Left': (1, 0), 'Down': (1, 0)},\n",
" (1, 1): {'Left': (1, 0), 'Up': (0, 1)}}\n",
"\n",
"\n",
"print(SAT_plan((0, 0), transition, (1, 1), 4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## First-Order Logic Knowledge Bases: `FolKB`\n",
"\n",
"The class `FolKB` can be used to represent a knowledge base of First-order logic sentences. You would initialize and use it the same way as you would for `PropKB` except that the clauses are first-order definite clauses. We will see how to write such clauses to create a database and query them in the following sections."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Criminal KB\n",
"In this section we create a `FolKB` based on the following paragraph.<br/>\n",
"<em>The law says that it is a crime for an American to sell weapons to hostile nations. The country Nono, an enemy of America, has some missiles, and all of its missiles were sold to it by Colonel West, who is American.</em><br/>\n",
"The first step is to extract the facts and convert them into first-order definite clauses. Extracting the facts from data alone is a challenging task. Fortunately, we have a small paragraph and can do extraction and conversion manually. We'll store the clauses in list aptly named `clauses`."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"clauses = []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<em>“... it is a crime for an American to sell weapons to hostile nations”</em><br/>\n",
"The keywords to look for here are 'crime', 'American', 'sell', 'weapon' and 'hostile'. We use predicate symbols to make meaning of them.\n",
"\n",
"* `Criminal(x)`: `x` is a criminal\n",
"* `American(x)`: `x` is an American\n",
"* `Sells(x ,y, z)`: `x` sells `y` to `z`\n",
"* `Weapon(x)`: `x` is a weapon\n",
"* `Hostile(x)`: `x` is a hostile nation\n",
"\n",
"Let us now combine them with appropriate variable naming to depict the meaning of the sentence. The criminal `x` is also the American `x` who sells weapon `y` to `z`, which is a hostile nation.\n",
"\n",
"$\\text{American}(x) \\land \\text{Weapon}(y) \\land \\text{Sells}(x, y, z) \\land \\text{Hostile}(z) \\implies \\text{Criminal} (x)$"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"clauses.append(expr(\"(American(x) & Weapon(y) & Sells(x, y, z) & Hostile(z)) ==> Criminal(x)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<em>\"The country Nono, an enemy of America\"</em><br/>\n",
"We now know that Nono is an enemy of America. We represent these nations using the constant symbols `Nono` and `America`. the enemy relation is show using the predicate symbol `Enemy`.\n",
"\n",
"$\\text{Enemy}(\\text{Nono}, \\text{America})$"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"clauses.append(expr(\"Enemy(Nono, America)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<em>\"Nono ... has some missiles\"</em><br/>\n",
"This states the existence of some missile which is owned by Nono. $\\exists x \\text{Owns}(\\text{Nono}, x) \\land \\text{Missile}(x)$. We invoke existential instantiation to introduce a new constant `M1` which is the missile owned by Nono.\n",
"\n",
"$\\text{Owns}(\\text{Nono}, \\text{M1}), \\text{Missile}(\\text{M1})$"
]
},
{
"cell_type": "code",
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"clauses.append(expr(\"Owns(Nono, M1)\"))\n",
"clauses.append(expr(\"Missile(M1)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"<em>\"All of its missiles were sold to it by Colonel West\"</em><br/>\n",
"If Nono owns something and it classifies as a missile, then it was sold to Nono by West.\n",
"\n",
"$\\text{Missile}(x) \\land \\text{Owns}(\\text{Nono}, x) \\implies \\text{Sells}(\\text{West}, x, \\text{Nono})$"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"clauses.append(expr(\"(Missile(x) & Owns(Nono, x)) ==> Sells(West, x, Nono)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<em>\"West, who is American\"</em><br/>\n",
"West is an American.\n",
"\n",
"$\\text{American}(\\text{West})$"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"clauses.append(expr(\"American(West)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also know, from our understanding of language, that missiles are weapons and that an enemy of America counts as “hostile”.\n",
"\n",
"$\\text{Missile}(x) \\implies \\text{Weapon}(x), \\text{Enemy}(x, \\text{America}) \\implies \\text{Hostile}(x)$"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"clauses.append(expr(\"Missile(x) ==> Weapon(x)\"))\n",
"clauses.append(expr(\"Enemy(x, America) ==> Hostile(x)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have converted the information into first-order definite clauses we can create our first-order logic knowledge base."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"crime_kb = FolKB(clauses)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Inference in First-Order Logic\n",
"In this section we look at a forward chaining and a backward chaining algorithm for `FolKB`. Both aforementioned algorithms rely on a process called <strong>unification</strong>, a key component of all first-order inference algorithms."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Unification\n",
"We sometimes require finding substitutions that make different logical expressions look identical. This process, called unification, is done by the `unify` algorithm. It takes as input two sentences and returns a <em>unifier</em> for them if one exists. A unifier is a dictionary which stores the substitutions required to make the two sentences identical. It does so by recursively unifying the components of a sentence, where the unification of a variable symbol `var` with a constant symbol `Const` is the mapping `{var: Const}`. Let's look at a few examples."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{x: 3}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"unify(expr('x'), 3)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{x: B}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"unify(expr('A(x)'), expr('A(B)'))"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{x: Bella, y: Dobby}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"unify(expr('Cat(x) & Dog(Dobby)'), expr('Cat(Bella) & Dog(y)'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In cases where there is no possible substitution that unifies the two sentences the function return `None`."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(unify(expr('Cat(x)'), expr('Dog(Dobby)')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also need to take care we do not unintentionally use the same variable name. Unify treats them as a single variable which prevents it from taking multiple value."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(unify(expr('Cat(x) & Dog(Dobby)'), expr('Cat(Bella) & Dog(x)')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Forward Chaining Algorithm\n",
"We consider the simple forward-chaining algorithm presented in <em>Figure 9.3</em>. We look at each rule in the knoweldge base and see if the premises can be satisfied. This is done by finding a substitution which unifies each of the premise with a clause in the `KB`. If we are able to unify the premises, the conclusion (with the corresponding substitution) is added to the `KB`. This inferencing process is repeated until either the query can be answered or till no new sentences can be added. We test if the newly added clause unifies with the query in which case the substitution yielded by `unify` is an answer to the query. If we run out of sentences to infer, this means the query was a failure.\n",
"The function `fol_fc_ask` is a generator which yields all substitutions which validate the query."
]
},
{
"cell_type": "code",
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">fol_fc_ask</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"p\">,</span> <span class=\"n\">alpha</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">"""A simple forward-chaining algorithm. [Figure 9.3]"""</span>\n",
" <span class=\"c1\"># TODO: Improve efficiency</span>\n",
" <span class=\"n\">kb_consts</span> <span class=\"o\">=</span> <span class=\"nb\">list</span><span class=\"p\">({</span><span class=\"n\">c</span> <span class=\"k\">for</span> <span class=\"n\">clause</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span> <span class=\"k\">for</span> <span class=\"n\">c</span> <span class=\"ow\">in</span> <span class=\"n\">constant_symbols</span><span class=\"p\">(</span><span class=\"n\">clause</span><span class=\"p\">)})</span>\n",
" <span class=\"k\">def</span> <span class=\"nf\">enum_subst</span><span class=\"p\">(</span><span class=\"n\">p</span><span class=\"p\">):</span>\n",
" <span class=\"n\">query_vars</span> <span class=\"o\">=</span> <span class=\"nb\">list</span><span class=\"p\">({</span><span class=\"n\">v</span> <span class=\"k\">for</span> <span class=\"n\">clause</span> <span class=\"ow\">in</span> <span class=\"n\">p</span> <span class=\"k\">for</span> <span class=\"n\">v</span> <span class=\"ow\">in</span> <span class=\"n\">variables</span><span class=\"p\">(</span><span class=\"n\">clause</span><span class=\"p\">)})</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">assignment_list</span> <span class=\"ow\">in</span> <span class=\"n\">itertools</span><span class=\"o\">.</span><span class=\"n\">product</span><span class=\"p\">(</span><span class=\"n\">kb_consts</span><span class=\"p\">,</span> <span class=\"n\">repeat</span><span class=\"o\">=</span><span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">query_vars</span><span class=\"p\">)):</span>\n",
" <span class=\"n\">theta</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"n\">x</span><span class=\"p\">:</span> <span class=\"n\">y</span> <span class=\"k\">for</span> <span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">y</span> <span class=\"ow\">in</span> <span class=\"nb\">zip</span><span class=\"p\">(</span><span class=\"n\">query_vars</span><span class=\"p\">,</span> <span class=\"n\">assignment_list</span><span class=\"p\">)}</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">theta</span>\n",
"\n",
" <span class=\"c1\"># check if we can answer without new inferences</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">q</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span><span class=\"p\">:</span>\n",
" <span class=\"n\">phi</span> <span class=\"o\">=</span> <span class=\"n\">unify</span><span class=\"p\">(</span><span class=\"n\">q</span><span class=\"p\">,</span> <span class=\"n\">alpha</span><span class=\"p\">,</span> <span class=\"p\">{})</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">phi</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">phi</span>\n",
"\n",
" <span class=\"k\">while</span> <span class=\"bp\">True</span><span class=\"p\">:</span>\n",
" <span class=\"n\">new</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">rule</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span><span class=\"p\">:</span>\n",
" <span class=\"n\">p</span><span class=\"p\">,</span> <span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">parse_definite_clause</span><span class=\"p\">(</span><span class=\"n\">rule</span><span class=\"p\">)</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">theta</span> <span class=\"ow\">in</span> <span class=\"n\">enum_subst</span><span class=\"p\">(</span><span class=\"n\">p</span><span class=\"p\">):</span>\n",
" <span class=\"k\">if</span> <span class=\"nb\">set</span><span class=\"p\">(</span><span class=\"n\">subst</span><span class=\"p\">(</span><span class=\"n\">theta</span><span class=\"p\">,</span> <span class=\"n\">p</span><span class=\"p\">))</span><span class=\"o\">.</span><span class=\"n\">issubset</span><span class=\"p\">(</span><span class=\"nb\">set</span><span class=\"p\">(</span><span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span><span class=\"p\">)):</span>\n",
" <span class=\"n\">q_</span> <span class=\"o\">=</span> <span class=\"n\">subst</span><span class=\"p\">(</span><span class=\"n\">theta</span><span class=\"p\">,</span> <span class=\"n\">q</span><span class=\"p\">)</span>\n",
" <span class=\"k\">if</span> <span class=\"nb\">all</span><span class=\"p\">([</span><span class=\"n\">unify</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">q_</span><span class=\"p\">,</span> <span class=\"p\">{})</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span> <span class=\"k\">for</span> <span class=\"n\">x</span> <span class=\"ow\">in</span> <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">clauses</span> <span class=\"o\">+</span> <span class=\"n\">new</span><span class=\"p\">]):</span>\n",
" <span class=\"n\">new</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">q_</span><span class=\"p\">)</span>\n",
" <span class=\"n\">phi</span> <span class=\"o\">=</span> <span class=\"n\">unify</span><span class=\"p\">(</span><span class=\"n\">q_</span><span class=\"p\">,</span> <span class=\"n\">alpha</span><span class=\"p\">,</span> <span class=\"p\">{})</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">phi</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>\n",
" <span class=\"k\">yield</span> <span class=\"n\">phi</span>\n",
" <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">new</span><span class=\"p\">:</span>\n",
" <span class=\"k\">break</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">clause</span> <span class=\"ow\">in</span> <span class=\"n\">new</span><span class=\"p\">:</span>\n",
" <span class=\"n\">KB</span><span class=\"o\">.</span><span class=\"n\">tell</span><span class=\"p\">(</span><span class=\"n\">clause</span><span class=\"p\">)</span>\n",
" <span class=\"k\">return</span> <span class=\"bp\">None</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's find out all the hostile nations. Note that we only told the `KB` that Nono was an enemy of America, not that it was hostile."
]
},
{
"cell_type": "code",
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{x: Nono}]\n"
]
}
],
"source": [
"answer = fol_fc_ask(crime_kb, expr('Hostile(x)'))\n",
"print(list(answer))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The generator returned a single substitution which says that Nono is a hostile nation. See how after adding another enemy nation the generator returns two substitutions."
]
},
{
"cell_type": "code",
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{x: Nono}, {x: JaJa}]\n"
]
}
],
"source": [
"crime_kb.tell(expr('Enemy(JaJa, America)'))\n",
"answer = fol_fc_ask(crime_kb, expr('Hostile(x)'))\n",
"print(list(answer))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong><em>Note</em>:</strong> `fol_fc_ask` makes changes to the `KB` by adding sentences to it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Backward Chaining Algorithm\n",
"This algorithm works backward from the goal, chaining through rules to find known facts that support the proof. Suppose `goal` is the query we want to find the substitution for. We find rules of the form $\\text{lhs} \\implies \\text{goal}$ in the `KB` and try to prove `lhs`. There may be multiple clauses in the `KB` which give multiple `lhs`. It is sufficient to prove only one of these. But to prove a `lhs` all the conjuncts in the `lhs` of the clause must be proved. This makes it similar to <em>And/Or</em> search."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### OR\n",
"The <em>OR</em> part of the algorithm comes from our choice to select any clause of the form $\\text{lhs} \\implies \\text{goal}$. Looking at all rules's `lhs` whose `rhs` unify with the `goal`, we yield a substitution which proves all the conjuncts in the `lhs`. We use `parse_definite_clause` to attain `lhs` and `rhs` from a clause of the form $\\text{lhs} \\implies \\text{rhs}$. For atomic facts the `lhs` is an empty list."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%psource fol_bc_or"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### AND\n",
"The <em>AND</em> corresponds to proving all the conjuncts in the `lhs`. We need to find a substitution which proves each <em>and</em> every clause in the list of conjuncts."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%psource fol_bc_and"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now the main function `fl_bc_ask` calls `fol_bc_or` with substitution initialized as empty. The `ask` method of `FolKB` uses `fol_bc_ask` and fetches the first substitution returned by the generator to answer query. Let's query the knowledge base we created from `clauses` to find hostile nations."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Rebuild KB because running fol_fc_ask would add new facts to the KB\n",
"crime_kb = FolKB(clauses)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{v_5: x, x: Nono}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"crime_kb.ask(expr('Hostile(x)'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may notice some new variables in the substitution. They are introduced to standardize the variable names to prevent naming problems as discussed in the [Unification section](#Unification)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Appendix: The Implementation of `|'==>'|`\n",
"Consider the `Expr` formed by this syntax:"
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the funny `|'==>'|` syntax? The trick is that \"`|`\" is just the regular Python or-operator, and so is exactly equivalent to this: "
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In other words, there are two applications of or-operators. Here's the first one:"
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is going on here is that the `__or__` method of `Expr` serves a dual purpose. If the right-hand-side is another `Expr` (or a number), then the result is an `Expr`, as in `(P | Q)`. But if the right-hand-side is a string, then the string is taken to be an operator, and we create a node in the abstract syntax tree corresponding to a partially-filled `Expr`, one where we know the left-hand-side is `P` and the operator is `==>`, but we don't yet know the right-hand-side.\n",
"The `PartialExpr` class has an `__or__` method that says to create an `Expr` node with the right-hand-side filled in. Here we can see the combination of the `PartialExpr` with `Q` to create a complete `Expr`:"
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"partial = PartialExpr('==>', P) \n",
"partial | ~Q"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This [trick](http://code.activestate.com/recipes/384122-infix-operators/) is due to [Ferdinand Jamitzky](http://code.activestate.com/recipes/users/98863/), with a modification by [C. G. Vedant](https://github.com/Chipe1),\n",
"who suggested using a string inside the or-bars.\n",
"\n",
"## Appendix: The Implementation of `expr`\n",
"\n",
"How does `expr` parse a string into an `Expr`? It turns out there are two tricks (besides the Jamitzky/Vedant trick):\n",
"\n",
"1. We do a string substitution, replacing \"`==>`\" with \"`|'==>'|`\" (and likewise for other operators).\n",
"2. We `eval` the resulting string in an environment in which every identifier\n",
"is bound to a symbol with that identifier as the `op`.\n",
"\n",
"In other words,"
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"P, Q = symbols('P, Q')\n",
"~(P & Q) |'==>'| (~P | ~Q)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One thing to beware of: this puts `==>` at the same precedence level as `\"|\"`, which is not quite right. For example, we get this:"
]
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": [
"(((P & Q) ==> P) | Q)"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"P & Q |'==>'| P | Q"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"which is probably not what we meant; when in doubt, put in extra parens:"
]
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": [
"((P & Q) ==> (P | Q))"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(P & Q) |'==>'| (P | Q)"
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples"
]
},
{
"cell_type": "code",
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
"<div>\n",
"<canvas id=\"canvas_bc_ask\" width=\"800\" height=\"600\" style=\"background:rgba(158, 167, 184, 0.2);\" onclick='click_callback(this, event, \"canvas_bc_ask\")'></canvas>\n",
"</div>\n",
"\n",
"<script> var canvas_bc_ask_canvas_object = new Canvas(\"canvas_bc_ask\");</script>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<script>\n",
"canvas_bc_ask_canvas_object.clear();\n",
"canvas_bc_ask_canvas_object.strokeWidth(3);\n",
"canvas_bc_ask_canvas_object.stroke(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.font(\"12px Arial\");\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(340, 85, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(340, 85, 460, 85);\n",
"canvas_bc_ask_canvas_object.line(340, 85, 340, 115);\n",
"canvas_bc_ask_canvas_object.line(460, 85, 460, 115);\n",
"canvas_bc_ask_canvas_object.line(340, 115, 460, 115);\n",
"canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.fill_text(\"Criminal(West)\", 348, 109);\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(55, 255, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(55, 255, 175, 255);\n",
"canvas_bc_ask_canvas_object.line(55, 255, 55, 285);\n",
"canvas_bc_ask_canvas_object.line(175, 255, 175, 285);\n",
"canvas_bc_ask_canvas_object.line(55, 285, 175, 285);\n",
"canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.fill_text(\"American(West)\", 63, 279);\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(245, 255, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(245, 255, 365, 255);\n",
"canvas_bc_ask_canvas_object.line(245, 255, 245, 285);\n",
"canvas_bc_ask_canvas_object.line(365, 255, 365, 285);\n",
"canvas_bc_ask_canvas_object.line(245, 285, 365, 285);\n",
"canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.fill_text(\"Weapon(M1)\", 253, 279);\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(435, 255, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(435, 255, 555, 255);\n",
"canvas_bc_ask_canvas_object.line(435, 255, 435, 285);\n",
"canvas_bc_ask_canvas_object.line(555, 255, 555, 285);\n",
"canvas_bc_ask_canvas_object.line(435, 285, 555, 285);\n",
"canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.fill_text(\"Sells(West, M1, Nono)\", 443, 279);\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(625, 255, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(625, 255, 745, 255);\n",
"canvas_bc_ask_canvas_object.line(625, 255, 625, 285);\n",
"canvas_bc_ask_canvas_object.line(745, 255, 745, 285);\n",
"canvas_bc_ask_canvas_object.line(625, 285, 745, 285);\n",
"canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.fill_text(\"Hostile(Nono)\", 633, 279);\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(55, 425, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(55, 425, 175, 425);\n",
"canvas_bc_ask_canvas_object.line(55, 425, 55, 455);\n",
"canvas_bc_ask_canvas_object.line(175, 425, 175, 455);\n",
"canvas_bc_ask_canvas_object.line(55, 455, 175, 455);\n",
"canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.fill_text(\"Missile(M1)\", 63, 449);\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(245, 425, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(245, 425, 365, 425);\n",
"canvas_bc_ask_canvas_object.line(245, 425, 245, 455);\n",
"canvas_bc_ask_canvas_object.line(365, 425, 365, 455);\n",
"canvas_bc_ask_canvas_object.line(245, 455, 365, 455);\n",
"canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.fill_text(\"Missile(M1)\", 253, 449);\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(435, 425, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(435, 425, 555, 425);\n",
"canvas_bc_ask_canvas_object.line(435, 425, 435, 455);\n",
"canvas_bc_ask_canvas_object.line(555, 425, 555, 455);\n",
"canvas_bc_ask_canvas_object.line(435, 455, 555, 455);\n",
"canvas_bc_ask_canvas_object.fill(0, 0, 0);\n",
"canvas_bc_ask_canvas_object.fill_text(\"Owns(Nono, M1)\", 443, 449);\n",
"canvas_bc_ask_canvas_object.fill(200, 200, 200);\n",
"canvas_bc_ask_canvas_object.rect(625, 425, 120, 30);\n",
"canvas_bc_ask_canvas_object.line(625, 425, 745, 425);\n",
"canvas_bc_ask_canvas_object.line(625, 425, 625, 455);\n",
"canvas_bc_ask_canvas_object.line(745, 425, 745, 455);\n",
"canvas_bc_ask_canvas_object.line(625, 455, 745, 455);\n",