Every person on the island is a knight or a knave. Knights always tell the truth. Knaves always lie.
There are only four possibilities: knight or knave, for A and for B. Later today a computer checks all four, and finds the same answer. It is called model checking.
No. You could see where B was, so you headed towards it.
DFS and BFS. They know nothing about where the goal is, so they try squares in a fixed order.
Uses extra knowledge about the problem, such as a guess at how far each square is from the goal, to look in the right direction first.
The Week 1 maze. C and D are two squares to compare. The grey numbers (last click) are each square’s Manhattan distance to B: its h.
D: 0 rows and 6 columns away, so 6. C: 4 rows and 8 columns away, so 12.
No: the wall forces a detour of 10 steps. A heuristic is a guess, and it can be wrong. But it is very quick to work out.
h(n): a quick estimate of the cost from square n to the goal. In a maze, the Manhattan distance: rows apart + columns apart, ignoring walls.
Rule: always take the square in the frontier with the smallest h (the small number in its corner).
The shortest path goes down and along the bottom: 16 steps. Greedy only looks at h, the guess to go. It never counts the steps it has already taken.
| X | Y | |
|---|---|---|
| h(n): guess to B | 7 | 11 |
| g(n): steps from A so far | 11 | 5 |
| f(n) = g(n) + h(n) | 18 | 16 |
Greedy looks only at h, so it takes X.
Counting the steps already taken, Y is the better bet.
Take the frontier square with the smallest f(n) = g(n) + h(n): the cost so far plus the guessed cost to go.
Rule: take the square with the smallest f = g + h.
maze4.txt | Squares explored | Path |
|---|---|---|
| BFS | 32 | 16 |
| Greedy | 21 | 20 |
| A* | 27 | 16 |
Two ways to measure Salisbury to Winchester: along the road, or in a straight line.
No. So straight-line distance never overestimates. Nor does Manhattan distance in a maze: walls only make the real route longer.
Never overestimates the true cost to the goal. A* with an admissible heuristic always finds the shortest path.
Then f(Y) = 5 + 30 = 35, worse than the top route. A* would go the long way and return the 20-step path.
| Sentence | True or false? |
|---|---|
| Winchester is in Hampshire. | true |
| 2 + 2 = 5. | false |
| Close the door! | neither: an order |
| Is it raining? | neither: a question |
| It is raining. | true or false, depending on the day |
A sentence that is either true or false. Orders and questions are not statements.
A precise way to write statements, with rules for working out what follows from them. It is the language of symbolic AI from Week 1.
Today we write statements in logic, then let a computer work out what follows from them.
Harry visited Dumbledore, and not both, so he did not visit Hagrid.
If it hadn’t rained, he would have visited Hagrid. He didn’t, so it rained.
An agent that stores what it knows as sentences (statements written in a logic), and reasons with them to reach new conclusions.
Working out new sentences from old ones, as we just did for sentences 4 and 5.
Give each statement a short name:
rainhagriddumbledoreA short name for one statement. In any world, it is either true or false. Textbooks often use single letters: P, Q, R.
Two: rain (with a “not”) and hagrid, joined by “if … then”.
Symbols, joined by five words: not, and, or, if … then and if and only if. The next two slides give each word a symbol.
¬rainhagrid ∧ dumbledorehagrid ∨ dumbledoreRead ¬ as “not”, ∧ as “and”, ∨ as “or”.
P ∨ Q is true when P is true, or Q is true, or both. To say “but not both”, add ¬(P ∧ Q).
| P | Q | ¬P | P ∧ Q | P ∨ Q |
|---|---|---|---|---|
| false | false | true | false | false |
| false | true | true | false | true |
| true | false | false | false | true |
| true | true | false | true | true |
P and Q stand for any two sentences. Each row is one possible world.
(hagrid ∨ dumbledore) ∧ ¬(hagrid ∧ dumbledore)
rain → indoorsRead rain → indoors as “rain implies indoors”.
rain ↔ indoorsP → Q is false only when P is true and Q is false. P ↔ Q is true when P and Q are the same.
rain | indoors | rain → indoors | rain ↔ indoors |
|---|---|---|---|
| false | false | true | true |
| false | true | true | false |
| true | false | false: broken! | false |
| true | true | true | true |
When it is not raining, the promise says nothing about where I am, so it is not broken: the sentence counts as true.
rain truehagrid truerain truehagrid falserain falsehagrid truerain falsehagrid false3 symbols: 8 worlds. n symbols: 2n worlds.
Each new symbol doubles the number.
rain and hagrid. How many different worlds are possible? Whole classOne possible world: a true or false value for every symbol.
The sentences an agent knows are true.
rain ∨ hagrid. Which worlds does it rule out?Only world 4, where both are false. Knowledge rules worlds out.
Yes. Every world where it is a Tuesday in January is a world where it is January.
No. On a Wednesday in January, “It is January” is true, but “It is a Tuesday in January” is not.
A entails B when B is true in every world where A is true: B follows from A. Written A ⊨ B.
Our question is always: KB ⊨ query? Is the query true in every world that fits what we know?
¬rain → hagridhagrid ∨ dumbledore¬(hagrid ∧ dumbledore)dumbledoreQuery: rain. Did it rain?
List every world. Keep the worlds where the KB is true. If the query is true in all of them, then KB ⊨ query.
rain | hagrid | dumbledore | Ruled out by |
|---|---|---|---|
| false | false | false | 4 |
| false | false | true | 1: no rain, so Hagrid |
| false | true | false | 4 |
| false | true | true | 3: not both |
| true | false | false | 4 |
| true | false | true | fits everything |
| true | true | false | 4 |
| true | true | true | 3: not both |
rain is true in every world that is left, so KB ⊨ rain. It rained.
(AKnight ∨ AKnave) ∧ ¬(AKnight ∧ AKnave), and the same for BAKnight → (AKnave ∧ BKnave)AKnave → ¬(AKnave ∧ BKnave)Four symbols give 16 worlds. The first sentence leaves only the four on the right.
| A is a | B is a | Ruled out? |
|---|---|---|
| knight | knight | yes: a knight said “both knaves” |
| knight | knave | yes: a knight said “both knaves” |
| knave | knight | no: it fits |
| knave | knave | yes: a knave said something true |
One world is left, so the KB entails A is a knave and B is a knight: the answer you found in Week 1, now checked by a method a computer can follow.
harry.py
from logic import *
rain = Symbol("rain")
hagrid = Symbol("hagrid")
dumbledore = Symbol("dumbledore")
knowledge = And(
Implication(Not(rain), hagrid),
Or(hagrid, dumbledore),
Not(And(hagrid, dumbledore)),
dumbledore
)
print(model_check(knowledge, rain))
logic.py gives Symbol, Not, And, Or, Implication, Biconditional and model_check.
knowledge to sentences 1–4 on the model-checking slide. What does the last line print?Same four sentences, in the same order. And(…) joins them: the KB is true only when all four are.
It prints True: the knowledge base entails rain.
What model_check does, in plain steps:
The check is only as good as the knowledge base. A missing or wrong sentence, or something the symbols do not cover (a broken sensor), is outside the proof. People who know the railway must check the sentences, and the system still needs testing.
week04-knowledge/logic in the module code (harry.py)
COMP5001 Artificial Intelligence — Week 3: Heuristics and Logic