Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
ProB 2 Jupyter Kernel
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
general
stups
ProB 2 Jupyter Kernel
Commits
eb824225
Commit
eb824225
authored
7 years ago
by
Michael Leuschel
Browse files
Options
Downloads
Patches
Plain Diff
update example notebook
parent
34f6927d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
notebooks/tutorials/prob_solver_intro.ipynb
+58
-10
58 additions, 10 deletions
notebooks/tutorials/prob_solver_intro.ipynb
with
58 additions
and
10 deletions
notebooks/tutorials/prob_solver_intro.ipynb
+
58
−
10
View file @
eb824225
...
...
@@ -4,7 +4,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use ProB to perform computations:"
"# Introduction to ProB's constraint solving capabilities\n",
"We can use ProB to perform computations:\n",
"\n",
"## Expressions\n",
"Expressions in B have a value. With ProB and with ProB's Jupyter backend, you can evaluate expresssions such as:"
]
},
{
...
...
@@ -58,7 +62,36 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can find solutions for equations. Open variables are implicitly existentially quantified:"
"## Predicates\n",
"ProB can also be used to evaluate predicates (B distinguishes between expressions which have a value and predicates which are either true or false)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"TRUE"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+2>3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Within predicates you can use **open** variables, which are implicitly existentially quantified.\n",
"ProB will display the solution for the open variables, if possible."
]
},
{
...
...
@@ -85,7 +118,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can find all solutions to a predicate by using the set comprehension notation."
"We can find all solutions to a predicate by using the set comprehension notation.\n",
"Note that by this we turn a predicate into an expression."
]
},
{
...
...
@@ -112,6 +146,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Send More Money Puzzle\n",
"We now try and solve the SEND+MORE=MONEY arithmetic puzzle in B, involving 8 distinct digits:"
]
},
...
...
@@ -207,6 +242,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## KISS PASSION Puzzle\n",
"A slightly more complicated puzzle (involving multiplication) is the KISS * KISS = PASSION problem."
]
},
...
...
@@ -238,6 +274,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## N-Queens Puzzle\n",
"Here is how we can solve the famous N-Queens puzzle for n=8."
]
},
...
...
@@ -293,15 +330,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"A Puzzle from Smullyan:\n",
" Knights: always tell the truth\n",
" Knaves: always lie\n",
"## Knights and Knave Puzzle\n",
"Here is a puzzle from Smullyan involving an island with only knights and knaves.\n",
"We know that:\n",
" - Knights: always tell the truth\n",
" - Knaves: always lie\n",
"\n",
" 1: A says: “B is a knave or C is a knave”\n",
" 2: B says “A is a knight”\n",
"We are given the following information about three persons A,B,C on the island:\n",
" 1. A says: “B is a knave or C is a knave”\n",
" 2. B says “A is a knight”\n",
"\n",
"
What are A
&
B
&
C?\n",
"
Note:
A,B,C
are equal to TRUE if they are a knight and FALSE if they are a knave."
"What are A
,
B
and
C?\n",
"Note:
we model A,B,C as boolean variables which
are equal to TRUE if they are a knight and FALSE if they are a knave."
]
},
{
...
...
@@ -325,6 +365,14 @@
" (B=TRUE <=> A=TRUE) // Sentence 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that in B there are no propositional variables: A,B and C are expressions with a value.\n",
"To turn them into a predicate we need to use the comparison with TRUE."
]
},
{
"cell_type": "code",
"execution_count": 15,
...
...
%% Cell type:markdown id: tags:
# Introduction to ProB's constraint solving capabilities
We can use ProB to perform computations:
## Expressions
Expressions in B have a value. With ProB and with ProB's Jupyter backend, you can evaluate expresssions such as:
%% Cell type:code id: tags:
```
prob
2**10
```
%% Output
1024
%% Cell type:markdown id: tags:
ProB supports
*mathematical*
integers without restriction (apart from memmory consumption):
%% Cell type:code id: tags:
```
prob
2**100
```
%% Output
1267650600228229401496703205376
%% Cell type:markdown id: tags:
We can find solutions for equations. Open variables are implicitly existentially quantified:
## Predicates
ProB can also be used to evaluate predicates (B distinguishes between expressions which have a value and predicates which are either true or false).
%% Cell type:code id: tags:
```
prob
2+2>3
```
%% Output
TRUE
%% Cell type:markdown id: tags:
Within predicates you can use
**open**
variables, which are implicitly existentially quantified.
ProB will display the solution for the open variables, if possible.
%% Cell type:code id: tags:
```
prob
x*x=100
```
%% Output
TRUE (x = −10)
%% Cell type:markdown id: tags:
We can find all solutions to a predicate by using the set comprehension notation.
Note that by this we turn a predicate into an expression.
%% Cell type:code id: tags:
```
prob
{x|x*x=100}
```
%% Output
{−10,10}
%% Cell type:markdown id: tags:
## Send More Money Puzzle
We now try and solve the SEND+MORE=MONEY arithmetic puzzle in B, involving 8 distinct digits:
%% Cell type:code id: tags:
```
prob
{S,E,N,D, M,O,R, Y} <: 0..9 & S >0 & M >0 &
card({S,E,N,D, M,O,R, Y}) = 8 &
S*1000 + E*100 + N*10 + D +
M*1000 + O*100 + R*10 + E =
M*10000 + O*1000 + N*100 + E*10 + Y
```
%% Output
TRUE (R = 8 ∧ S = 9 ∧ D = 7 ∧ E = 5 ∧ Y = 2 ∧ M = 1 ∧ N = 6 ∧ O = 0)
%% Cell type:markdown id: tags:
Observe how we have used the cardinality constraint to express that all digits are distinct.
If we leave out this cardinality constraint, other solutions are possible:
%% Cell type:code id: tags:
```
prob
{S,E,N,D, M,O,R, Y} <: 0..9 & S >0 & M >0 &
// card({S,E,N,D, M,O,R, Y}) = 8 & // commented out
S*1000 + E*100 + N*10 + D +
M*1000 + O*100 + R*10 + E =
M*10000 + O*1000 + N*100 + E*10 + Y
```
%% Output
TRUE (R = 0 ∧ S = 9 ∧ D = 0 ∧ E = 0 ∧ Y = 0 ∧ M = 1 ∧ N = 0 ∧ O = 0)
%% Cell type:markdown id: tags:
We can find all solutions (to the unmodified puzzle) using a set comprehension and make sure that there is just a single soltuion:
%% Cell type:code id: tags:
```
prob
{S,E,N,D, M,O,R, Y |
{S,E,N,D, M,O,R, Y} <: 0..9 & S >0 & M >0 &
card({S,E,N,D, M,O,R, Y}) = 8 &
S*1000 + E*100 + N*10 + D +
M*1000 + O*100 + R*10 + E =
M*10000 + O*1000 + N*100 + E*10 + Y }
```
%% Output
{(((((((9↦5)↦6)↦7)↦1)↦0)↦8)↦2)}
%% Cell type:markdown id: tags:
## KISS PASSION Puzzle
A slightly more complicated puzzle (involving multiplication) is the KISS
*
KISS = PASSION problem.
%% Cell type:code id: tags:
```
prob
{K,P} <: 1..9 &
{I,S,A,O,N} <: 0..9 &
(1000*K+100*I+10*S+S) * (1000*K+100*I+10*S+S)
= 1000000*P+100000*A+10000*S+1000*S+100*I+10*O+N &
card({K, I, S, P, A, O, N}) = 7
```
%% Output
TRUE (P = 4 ∧ A = 1 ∧ S = 3 ∧ I = 0 ∧ K = 2 ∧ N = 9 ∧ O = 8)
%% Cell type:markdown id: tags:
## N-Queens Puzzle
Here is how we can solve the famous N-Queens puzzle for n=8.
%% Cell type:code id: tags:
```
prob
n = 8 &
queens : perm(1..n) /* for each column the row in which the queen is in */
&
!(q1,q2).(q1:1..n & q2:2..n & q2>q1
=> queens(q1)+(q2-q1) /= queens(q2) & queens(q1)+(q1-q2) /= queens(q2))
```
%% Output
TRUE (queens = {(1↦1),(2↦5),(3↦8),(4↦6),(5↦3),(6↦7),(7↦2),(8↦4)} ∧ n = 8)
%% Cell type:code id: tags:
```
prob
n = 16 &
queens : perm(1..n) /* for each column the row in which the queen is in */
&
!(q1,q2).(q1:1..n & q2:2..n & q2>q1
=> queens(q1)+(q2-q1) /= queens(q2) & queens(q1)+(q1-q2) /= queens(q2))
```
%% Output
TRUE (queens = {(1↦1),(2↦3),(3↦5),(4↦13),(5↦11),(6↦4),(7↦15),(8↦7),(9↦16),(10↦14),(11↦2),(12↦8),(13↦6),(14↦9),(15↦12),(16↦10)} ∧ n = 16)
%% Cell type:markdown id: tags:
A Puzzle from Smullyan:
Knights: always tell the truth
Knaves: always lie
## Knights and Knave Puzzle
Here is a puzzle from Smullyan involving an island with only knights and knaves.
We know that:
-
Knights: always tell the truth
-
Knaves: always lie
We are given the following information about three persons A,B,C on the island:
1.
A says: “B is a knave or C is a knave”
2.
B says “A is a knight”
1: A says: “B is a knave or C is a knave”
2: B says “A is a knight”
What are A & B & C?
Note: A,B,C are equal to TRUE if they are a knight and FALSE if they are a knave.
What are A, B and C?
Note: we model A,B,C as boolean variables which are equal to TRUE if they are a knight and FALSE if they are a knave.
%% Cell type:code id: tags:
```
prob
(A=TRUE <=> (B=FALSE or C=FALSE)) & // Sentence 1
(B=TRUE <=> A=TRUE) // Sentence 2
```
%% Output
TRUE (A = TRUE ∧ B = TRUE ∧ C = FALSE)
%% Cell type:markdown id: tags:
Note that in B there are no propositional variables: A,B and C are expressions with a value.
To turn them into a predicate we need to use the comparison with TRUE.
%% Cell type:code id: tags:
```
prob
/* this computes the set of all models: */
{A,B,C| (A=TRUE <=> (B=FALSE or C=FALSE)) &
(B=TRUE <=> A=TRUE) }
```
%% Output
{((TRUE↦TRUE)↦FALSE)}
%% Cell type:code id: tags:
```
prob
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment