Skip to content

added examples to logic.ipynb #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 63 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
e14c34b
Update utils.py
sofmonk Mar 17, 2017
8298332
Update utils.py
sofmonk Mar 17, 2017
588c318
Update learning.py
sofmonk Mar 17, 2017
8b2b94b
Update probability.py
sofmonk Mar 17, 2017
35c9d51
Update search.py
sofmonk Mar 18, 2017
4a67871
included double_tennis_problem from chapter 11
sofmonk Mar 18, 2017
0be9f2c
Update planning.py
sofmonk Mar 18, 2017
76bd895
Update utils.py
sofmonk Mar 18, 2017
f9b8ad4
Update learning.py
sofmonk Mar 18, 2017
a60ef5a
Update probability.py
sofmonk Mar 18, 2017
f1b4e25
Update search.py
sofmonk Mar 18, 2017
d45d8d4
Update planning.py
sofmonk Mar 18, 2017
c43fd7e
Update planning.py
sofmonk Mar 18, 2017
dc20b93
Update planning.py
sofmonk Mar 18, 2017
4bac058
Update planning.py
sofmonk Mar 18, 2017
a31c09b
Merge branch 'master' into master
sofmonk Mar 18, 2017
cdbbde7
Update planning.py
sofmonk Mar 18, 2017
86615b3
Update planning.py
sofmonk Mar 18, 2017
9d272b7
Update test_agents.py
sofmonk Mar 18, 2017
1581f7a
Update test_agents.py
sofmonk Mar 18, 2017
7f9d053
Update test_agents.py
sofmonk Mar 18, 2017
594736d
Update test_agents.py
sofmonk Mar 18, 2017
b6f9de4
Update test_text.py
sofmonk Mar 18, 2017
7d5dcba
Update utils.py
sofmonk Mar 18, 2017
2783b6a
Update search.py
sofmonk Mar 18, 2017
99cbbaf
Update probability.py
sofmonk Mar 18, 2017
620cb41
Update learning.py
sofmonk Mar 18, 2017
ab26cd8
Update planning.py
sofmonk Mar 18, 2017
f69e7c1
Update rl.py
sofmonk Mar 19, 2017
fba8b92
Merge pull request #4 from sofmonk/patch-2
sofmonk Mar 19, 2017
fce259a
Update search.py
sofmonk Mar 19, 2017
6b82783
Update search.py
sofmonk Mar 19, 2017
5e628be
Update search.py
sofmonk Mar 19, 2017
86f10d5
Update search.py
sofmonk Mar 19, 2017
a925fe6
Merge branch 'master' into patch-1
norvig Mar 22, 2017
1bd46b9
Update README.md
sofmonk Mar 23, 2017
6e3fb8e
Update README.md
sofmonk Mar 23, 2017
3f3ef6d
Update README.md
sofmonk Mar 23, 2017
534cfa7
Merge branch 'master' into patch-1
sofmonk Mar 23, 2017
75fcf6e
Merge pull request #3 from sofmonk/patch-1
sofmonk Mar 23, 2017
5be56d4
Merge branch 'master' into sofmonk-planning
sofmonk Mar 23, 2017
430b908
Merge pull request #2 from sofmonk/sofmonk-planning
sofmonk Mar 23, 2017
2d4a0d5
Merge branch 'master' into sofmonk-patch-1
sofmonk Mar 23, 2017
4b4f70e
Merge pull request #1 from sofmonk/sofmonk-patch-1
sofmonk Mar 23, 2017
8bbd8e5
Merge branch 'master' into master
sofmonk Mar 23, 2017
8f0899a
Update planning.py
sofmonk Mar 23, 2017
0ed9512
Update planning.py
sofmonk Mar 23, 2017
8845d4b
Update planning.py
sofmonk Mar 23, 2017
da4733f
Update test_planning.py
sofmonk Mar 23, 2017
32cd0e6
Update test_planning.py
sofmonk Mar 23, 2017
7afe352
Update logic.py
sofmonk Mar 25, 2017
00d29b4
Update logic.py
sofmonk Mar 25, 2017
b5b1b91
Update test_planning.py
sofmonk Mar 25, 2017
43e7753
Merge remote-tracking branch 'origin/master'
sofmonk Mar 26, 2017
aad19c8
Merge remote-tracking branch 'origin/master'
sofmonk Mar 26, 2017
5721528
Merge remote-tracking branch 'origin/master'
sofmonk Mar 26, 2017
ae9174e
Merge remote-tracking branch 'origin/master'
sofmonk Mar 26, 2017
90b48eb
Update logic.ipynb
sofmonk Mar 26, 2017
fa8b0ae
Update logic.ipynb
sofmonk Mar 26, 2017
8f6604c
Merge branch 'master' into sofmonk-new
sofmonk Mar 26, 2017
daaf906
Update planning.py
sofmonk Mar 26, 2017
9bd5c7d
Add files via upload
sofmonk Mar 26, 2017
5b9167c
Delete planning.ipynb
sofmonk Mar 26, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/master'
# Conflicts:
#	planning.py
  • Loading branch information
sofmonk committed Mar 26, 2017
commit aad19c832d89115bb5fa8827227f657c2eeeb760
328 changes: 322 additions & 6 deletions logic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 11,
"metadata": {
"collapsed": true
},
Expand Down Expand Up @@ -306,11 +306,11 @@
"|--------------------------|----------------------|-------------------------|---|---|\n",
"| Negation | ¬ P | `~P` | `~P` | `Expr('~', P)`\n",
"| And | P ∧ Q | `P & Q` | `P & Q` | `Expr('&', P, Q)`\n",
"| Or | P &or; Q | `P`<tt> &#124; </tt>`Q`| `P`<tt> &#124; </tt>`Q` | `Expr('`&#124;`', P, Q)`\n",
"| Or | P &or; Q | `P`<tt> &#124; </tt>`Q`| `P`<tt> &#124; </tt>`Q` | `Expr('`&#124;`', P, Q)\n",
"| Inequality (Xor) | P &ne; Q | `P ^ Q` | `P ^ Q` | `Expr('^', P, Q)`\n",
"| Implication | P &rarr; Q | `P` <tt>&#124;</tt>`'==>'`<tt>&#124;</tt> `Q` | `P ==> Q` | `Expr('==>', P, Q)`\n",
"| Reverse Implication | Q &larr; P | `Q` <tt>&#124;</tt>`'<=='`<tt>&#124;</tt> `P` |`Q <== P` | `Expr('<==', Q, P)`\n",
"| Equivalence | P &harr; Q | `P` <tt>&#124;</tt>`'<=>'`<tt>&#124;</tt> `Q` |`P <=> Q` | `Expr('<=>', P, Q)`\n",
"| Equivalence | P &harr; Q | `P` <tt>&#124;</tt>`'<=>'`<tt>&#124;</tt> `Q` |`P ==> Q` | `Expr('==>', P, Q)`\n",
"\n",
"Here's an example of defining a sentence with an implication arrow:"
]
Expand Down Expand Up @@ -412,6 +412,7 @@
"\n",
"The class `PropKB` can be used to represent a knowledge base of propositional logic sentences.\n",
"\n",
"\n",
"We see that the class `KB` has four methods, apart from `__init__`. A point to note here: the `ask` method simply calls the `ask_generator` method. Thus, this one has already been implemented and what you'll have to actually implement when you create your own knowledge base class (if you want to, though I doubt you'll ever need to; just use the ones we've created for you), will be the `ask_generator` function and not the `ask` function itself.\n",
"\n",
"The class `PropKB` now.\n",
Expand All @@ -421,6 +422,321 @@
"* `retract(self, sentence)` : This function removes all the clauses of the sentence given, from the knowledge base. Like the `tell` function, you don't have to pass clauses to remove them from the knowledge base; any sentence will do fine. The function will take care of converting that sentence to clauses and then remove those."
]
},
{
"cell_type": "heading",
"metadata": {},
"level": 6,
"source": [
"The following gives an idea how the python code works in logic.py:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"KB = PropKB()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"creates an empty knowledge base"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"KB.tell(A & C)\n",
"KB.ask(A) == KB.ask(C) == {}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"adds sentence and checks if KB entails the query."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"KB.ask(E)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{}"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"kb.tell(E)\n",
"kb.ask(E)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"KB.retract(C)\n",
"KB.ask(C)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"removes all clauses of 'C' from the sentence."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The truth table enumeration algorithm from Figure 7.10 codes as follows:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def tt_entails(kb, alpha):\n",
" \n",
" assert not variables(alpha)\n",
" return tt_check_all(kb, alpha, prop_symbols(kb & alpha), {})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tt_entails(expr('P & Q'), expr('Q'))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tt_check_all(expr('P & Q'), expr('Q'), prop_symbols(expr('Q')), {})"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{B: True, A: True}\n{Q: True}\n"
]
}
],
"source": [
"print (dpll_satisfiable(expr('A & B')))\n",
"print (dpll_satisfiable(expr('P | Q')))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{x: 2}\n"
]
}
],
"source": [
"print(unify(x, 2, {}))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{B: True, C: True, A: True}"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"WalkSAT([A & B, A & C], 0.5, 100 )"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Left', 'Left']"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"transition = {'A': {'Left': 'A', 'Right': 'B'},\n",
" 'B': {'Left': 'A', 'Right': 'C'},\n",
" 'C': {'Left': 'B', 'Right': 'C'}}\n",
"\n",
"SAT_plan('C', transition, 'A', 3)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<generator object fol_bc_or at 0x7f8d8c5f6ba0>"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fol_bc_ask(expr('P & Q'), expr('Q'))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Expr' object has no attribute 'clauses'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-52-1f559a7b8f18>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpl_fc_entails\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexpr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'P & Q'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Q'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/home/ubuntu/PycharmProjects/GSOC_17/sofmonk/aima-python/logic.py\u001b[0m in \u001b[0;36mpl_fc_entails\u001b[0;34m(KB, q)\u001b[0m\n\u001b[1;32m 498\u001b[0m \"\"\"\n\u001b[1;32m 499\u001b[0m count = {c: len(conjuncts(c.args[0]))\n\u001b[0;32m--> 500\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mKB\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclauses\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 501\u001b[0m if c.op == '==>'}\n\u001b[1;32m 502\u001b[0m \u001b[0minferred\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdefaultdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbool\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'Expr' object has no attribute 'clauses'"
],
"output_type": "error"
}
],
"source": [
"pl_fc_entails(expr('P & Q'), expr('Q'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -701,16 +1017,16 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 3.0
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy