Skip to content

Commit 6297ebd

Browse files
authored
Merge pull request #27 from erohkohl/tableau
Merge tableau calculus
2 parents 134baa2 + 7fe82cf commit 6297ebd

28 files changed

+1338
-273
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
language: python
22

33
python:
4-
- 3.5
4+
- 3.6.2
55

66
install:
77
- pip install codecov
88
- pip install pytest-cov
99

1010
script:
11-
- py.test --cov=./src/
11+
- py.test --cov=./mlsolver/
1212

1313
after_success:
1414
- codecov

README.md

+96-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
Framework for modelling Kripke structure and modal logic formula
1+
Modal logic solver
22
================================================================
3-
[![Build Status](https://travis-ci.org/erohkohl/ai-modal-logic.svg?branch=master)](https://travis-ci.org/erohkohl/ai-modal-logic)
4-
[![codecov](https://codecov.io/gh/erohkohl/ai-modal-logic/branch/master/graph/badge.svg)](https://codecov.io/gh/erohkohl/ai-modal-logic)
3+
### Framework for modelling Kripke structures and solving modal logic formulas [![Build Status](https://travis-ci.org/erohkohl/ai-modal-logic.svg?branch=master)](https://travis-ci.org/erohkohl/ai-modal-logic) [![codecov](https://codecov.io/gh/erohkohl/ai-modal-logic/branch/master/graph/badge.svg)](https://codecov.io/gh/erohkohl/ai-modal-logic)
54

6-
This framework provides a tool for modelling Kripke structures, modal and multi modal
7-
logic formulas in **Python 3**. The aim of this framework is to describe the knowledge base of a multi agent system and its model, after one agent made an announcement. This knowledge base is mapped by a Kripke structure and one agents announcement is wrapped in a multi modal logic formula.
5+
This framework provides a tool for modelling Kripke structures and solving modal
6+
logic formulas in **Python 3.6**. The aim of this framework is to describe the knowledge base of a multi agent system and its model, after one agent made an announcement. This knowledge base is mapped by a Kripke structure and one agents announcement is wrapped in a multi modal logic formula.
87

98
#### Modelling Kripke structure
109
A Kripke Frame describes a simple directed graph and its extension, Kripke structure,
@@ -15,7 +14,7 @@ assigns to each node a subset of propositional variables. In Kripke's semantic a
1514
The following code snipped shows, how you can build the above Kripke structure with this framework. The Python syntax allows to model the transition relation of a Kripke frame very similar to its mathematical description. To model a valid Kripke frame, you have to ensure, that each node name of the transition relation appears in the list of worlds.
1615

1716
```python
18-
from src.kripke import World, KripkeStructure
17+
from mlsolver.kripke import World, KripkeStructure
1918

2019
worlds = [
2120
World('1', {'p': True, 'q': True}),
@@ -26,15 +25,15 @@ worlds = [
2625
relations = {('1', '2'), ('2', '1'), ('1', '3'), ('3', '3')}
2726
ks = KripkeStructure(worlds, relations)
2827
```
29-
I decided to model the set of propositional variables as dict, therefore it is not necessary to explicit assign false to a variable. Moreover World('2', {'p': False}) and World('2', {}) are equivalent.
28+
I decided to model the set of propositional variables as dict, therefore it is not necessary to explicit assign false to a variable. Moreover ```World('2', {'p': False})``` and ```World('2', {})``` are equivalent.
3029

3130
#### Describe modal logic formula and check its semantic over one world
3231
Further more this framework allows you to check wether a node of your Kripke structure forces a given modal logic formula. Therefore you can map a formula with this framework as following code snipped shows. To calculate the semantic of a modal logic formula over one world just call *semantic()*, pass in the Kripke structure and the name of the world, you want to check.
3332

34-
<img src="./doc/formula_example.png" width="250">
33+
<img src="./doc/formula_example.png" width="265">
3534

3635
```python
37-
from src.formula import *
36+
from mlsolver.formula import *
3837

3938
formula = Implies(
4039
Diamond(Atom('p')),
@@ -44,9 +43,92 @@ formula = Implies(
4443
)
4544
)
4645

47-
assert True == formula.semantic(ks, '1')
46+
assert formula.semantic(ks, '1') is True
4847
```
4948

49+
#### Solve modal logic formula with tableau calculus
50+
A common challenge in artificial intelligence is, to determine a valid Kripke
51+
structure to a given modal logic formula. Therefore the modal logic tableau
52+
calculus gives us a tool, that constructs a Kripke structure starting from
53+
one world. If the formula is satisfiable, it is true in this world. Imagine we
54+
are searching for a valid Kripke structure, that satisfies the below formula
55+
in the world *s*. The only thing to do, is to build this formula, like we already
56+
saw in the snippet above, pass it to an instance of ProofTree and call the *derive()*
57+
method. To check, whether the resolved Kripke structure really satisfies the formula
58+
in world s, you can again make use of Formula's semantic() function.
59+
60+
<img src="./doc/tableau_example.png" width="265">
61+
62+
```python
63+
from mlsolver.tableau import *
64+
from mlsolver.formula import ProofTree
65+
66+
formula = Or(
67+
And(Box(Atom('p')), Atom('r'))
68+
, And(Atom('r'), Diamond(Atom('q')))
69+
)
70+
pt = ProofTree(formula)
71+
pt.derive()
72+
73+
print(pt)
74+
assert formula.semantic(pt.kripke_structure, 's') is True
75+
```
76+
77+
**Output:**
78+
79+
```bash
80+
Proof tree
81+
==========
82+
s:((Box(p) and r) or (r and Diamond(q)))
83+
|
84+
|_ s:(Box(p) and r)
85+
|
86+
|_ s:Box(p)
87+
|
88+
|_ s:r
89+
|
90+
|_ s:(r and Diamond(q))
91+
|
92+
|_ s:r
93+
|
94+
|_ s:Diamond(q)
95+
|
96+
(s, t)
97+
|
98+
|_ t:q
99+
100+
Kripke structure
101+
================
102+
(W = {(s,{'r': True})}, R = set())
103+
```
104+
105+
The output shows, that the tableau calculus determines two possible Kripke
106+
structures, because both paths are not closed. Thus the second satisfiable Kripke
107+
structure would be ```(W = {(s,{'r': True}), (t,{'q': True})}, R = set(('s', 't')))```.
108+
One path is closed, if there is a conflict in one worlds partial assignment. The bottom
109+
symbol ```-|``` at the end of a leaf indicates a closed path (see snippet below).
110+
111+
```bash
112+
s:((p or q) and not (p -> q))
113+
|
114+
|_ s:(p or q)
115+
|
116+
|_ s:not (p -> q)
117+
|
118+
|_ s:p
119+
|
120+
|_ s:p
121+
|
122+
|_ s: not q
123+
|
124+
|_ s:q
125+
|
126+
|_ s:p
127+
|
128+
|_ s: not q -| # this path is closed
129+
```
130+
131+
50132
#### Modelchecking
51133
Moreover this framework allows to process new knowledge in addition to the current knowledge base, thus it applies a modal logic formula to a Kripke structure (knowledge base) and returns a model. This model is a valid Kripke structure, in terms all of its worlds forces the formula. Therefore the function *solve()* removes the minimum subset of worlds, that prevent the Kripke structure to force the formula.
52134
@@ -55,10 +137,10 @@ model = ks.solve(formula)
55137
```
56138
57139
#### Modelling multi agent systems
58-
Further this framework extends the classical modal logic by the semantics of Box_a and Diamond_a operators for describing multi agent systems. You can find their implementation in the Pyhton file [src.fromula](https://github.com/erohkohl/ai-modal-logic/blob/master/src/formula.py). To use this operators it is necessary to build a Kripke structure with additional transition relations for each agent. To illustrate the usage of the framework's multi modal logic implementation, I implemented the *three wise men puzzle*.
140+
Further this framework extends the classical modal logic by the semantics of Box_a and Diamond_a operators for describing multi agent systems. You can find their implementation in the Pyhton file [mlsolver.formula](https://github.com/erohkohl/mlsolver/blob/master/src/formula.py). To use this operators it is necessary to build a Kripke structure with additional transition relations for each agent. To illustrate the usage of the framework's multi modal logic implementation, I implemented the *three wise men puzzle*.
59141
60142
##### Example: Three wise men with hat
61-
The data model of this example is located in [src.model](https://github.com/erohkohl/ai-modal-logic/blob/master/src/model.py) and the Pyhton file [test_model.py](https://github.com/erohkohl/ai-modal-logic/blob/master/test/test_model.py) proves its results.
143+
The data model of this example is located in [mlsolver.model](https://github.com/erohkohl/mlsolver/blob/master/src/model.py) and the Pyhton file [test_model.py](https://github.com/erohkohl/mlsolver/blob/master/test/test_model.py) proves its results.
62144
63145
This puzzle is about three wise men, all of them wear either a red or a white hat. All in all there are two white and three red hats. Each wise men is only able to see the hats of his two neighbors and has to guess the color of his own hat. You can see the Kripke structure, that describes this knowledge base, in the picture below. For example the world name *RWW* denotes, that in this scenario the first wise man wears a red hat, the second and third wise man a white hat. The transition relation is defined by equivalence of two worlds for one agent. For example World *RWW* and *RRW* are equivalent for agent 2, because he can't distinguish these two possible scenarios.
64146
@@ -82,9 +164,9 @@ model = ks.solve(And(f, g))
82164
83165
#### Test-driven development
84166
85-
While developing this framework I made use of the test-driven approach. Thus this repository contains 56 py.test case to ensure, that the framework works as expected, and for documentation purposes. Before you are able to run all tests, make sure you have installed the setup.py, which only contains py.test as dependency, and you use **Python 3**.
167+
While developing this framework I made use of the test-driven approach. Thus this repository contains **106 py.test cases** to ensure, that the framework works as expected, and for documentation purposes. Before you are able to run all tests, make sure you have installed the setup.py, which only contains py.test as dependency, and you use **Python 3.6**.
86168
87169
```bash
88170
$ python setup.py install
89-
$ py.test -v
171+
$ py.test -vv
90172
```

doc/tableau_example.png

23.8 KB
Loading
File renamed without changes.

0 commit comments

Comments
 (0)