Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

Artificial Intelligence

V05: Logic and planning

Knowledge representation with logic


Planning as search

Based on material by
• Stuart Russell, UC Berkeley
• Peter Ljunglöf, U Gothenburg / Chalmers
• Malte Helmert, U Basel

Zurich University of Applied Sciences and Arts


CAI Centre for Artificial Intelligence (stdm)
Educational objectives

• Explain how the representation of knowledge with formal


languages (e.g., logic) facilitates reasoning

• Remember PDDL semantics

• Comprehend and extend plans given in PDDL

“In which we design agents that can form representations of the world, use a
process of inference to derive new representations about the world, and use
these new representations to deduce what to do.”

“In which we see how an agent can take advantage of the structure of a problem
to efficiently construct complex plans of action.”

➔ Reading: AIMA, ch. 7-9,11


Zurich University of Applied Sciences and Arts 2
CAI Centre for Artificial Intelligence (stdm)
1. KNOWLEDGE REPRESENTATION WITH LOGIC

Zurich University of Applied Sciences and Arts 3


CAI Centre for Artificial Intelligence (stdm)
Knowledge bases

Knowledge base (KB): a set of sentences in a formal language


• The declarative approach to building an agent
→Tell it what it needs to know
→Then it can ask itself what to do (answers should follow from the KB)
• Two views of an agent (regardless of approach)
→ At the knowledge level: i.e., what they know, regardless of how implemented
→ At the implementation level: i.e., data structures in KB and algorithms manipulating them

function KB-Agent(percept) returns an action


static: KB, a knowledge base
The agent must be able to
t, a counter, initially 0, indicating time • Represent states, actions, etc.
Tell(KB, Make-Percept-Sentence(percept, t)) • Incorporate new percepts
action  Ask(KB, Make-Action-Query(t))
Tell(KB, Make-Action-Sentence(action, t)) • Update internal representations of the world
t  t+1 • Deduce hidden properties of the world
return action • Deduce appropriate actions

Zurich University of Applied Sciences and Arts 4


CAI Centre for Artificial Intelligence (stdm)
Example: The wumpus world
PEAS description

Performance measure
• gold +1000, death −1000, −1 per step, −10 for using arrow

Environment
• Squares adjacent to wumpus are smelly
• Squares adjacent to pit are breezy
• Glitter 𝑖𝑓𝑓 gold is in the same square
• Shooting kills wumpus if you are facing it
• Shooting uses up the only arrow
• Grabbing picks up gold if in same square

Actuators
• Left turn, Right turn, Wumpus world characterization
• Observable? No (only local perception)
• Forward (1 square), Grab, Shoot
• Deterministic? Yes (outcomes exactly specified)
• Episodic? No (sequential at the level of actions)
Sensors
• Static? Yes (wumpus and pits do not move)
• Breeze, Glitter, Smell (in current square) • Discrete? Yes
• Single-agent? Yes (wumpus is a natural feature)
Zurich University of Applied Sciences and Arts 5
CAI Centre for Artificial Intelligence (stdm)
Exploring a wumpus world

Start (square 1/1) “Squares adjacent to pit are breezy”


• Move forward ➔ sense breeze
• Infer possible pits (because of breeze)
• Move to 1/2 (row/col) ➔ sense stench
• Infer pit, wumpus (and no pit in 2/2)
• Move to 2/2 (only save square)
• Sense nothing ➔ 2/3 and 3/2 are ok
• Move to 2/3 ➔ sense breeze, glitter, stench

➔ What next?

Zurich University of Applied Sciences and Arts 6


CAI Centre for Artificial Intelligence (stdm)
Logic in general

Logics are formal languages for representing information


• …such that conclusions can be drawn
• Syntax defines the “structure” of sentences in the language
• Semantics defines the “meaning" of sentences (i.e., truth of a sentence in a world/model)

Model: a formally structured possible world with respect to


which truth can be evaluated.
➔ We say “m is a model of a sentence α” or “𝑚 satisfies
α” if α is true in m
(i.e., 𝑚 instantiates all variables in α such that α is true)

Example: the language of arithmetic


• 𝑥+2≥𝑦 is a sentence; 𝑥2 + 𝑦 > is not a sentence
• 𝑥+2≥𝑦 is true 𝑖𝑓𝑓 the number 𝑥 + 2 is no less than the number 𝑦
• 𝑥+2≥𝑦 is true in a world where 𝑥 = 7; 𝑦 = 1
• 𝑥+2≥𝑦 is false in a world where 𝑥 = 0; 𝑦 = 6

Zurich University of Applied Sciences and Arts 7


CAI Centre for Artificial Intelligence (stdm)
Key concept: Inference

𝐾𝐵 ⊨𝑖 𝛼
• Meaning: sentence 𝛼 can be derived from 𝐾𝐵 by procedure 𝒊
• Intuition:
• Consequences of 𝐾𝐵 are a haystack
• 𝛼 is a needle
• Entailment (⊨, the principle of “logical consequence”) says: “needle is in haystack”
• Inference: finding it

Desirable properties of 𝑖
• Soundness: 𝑖 is sound if whenever 𝐾𝐵 ⊨𝑖 𝛼, it is also true that 𝐾𝐵 ⊨ 𝛼
• Completeness: 𝑖 is complete if whenever 𝐾𝐵 ⊨ 𝛼, it is also true that 𝐾𝐵 ⊨𝑖 𝛼

Outlook (see appendix on Datalog)


• Possible & practical to define: a logic (first-order definite clauses) expressive enough to say almost
anything storable in a RDBMS, and a sound and complete inference procedure (forward chaining)
• That is: The procedure will answer any question whose answer follows from what is known by KB

Zurich University of Applied Sciences and Arts 8


CAI Centre for Artificial Intelligence (stdm)
Inference, satisfiability and the SAT problem

A sentence is satisfiable (SAT) if it is true in some model


• Examples: 𝐴 ∨ 𝐵, 𝐶

A sentence is unsatisfiable if it is true in no models


• Examples: 𝐴 ∧ ¬𝐴

CSPs and logical inference: Map coloring as (a) a


constraint graph and (b) as a logical sentence (single
Satisfiability is connected to inference via the following: “definite clause”).
• 𝐾𝐵 ⊨ 𝛼 if and only if (𝐾𝐵 ∧ ¬𝛼) is unsatisfiable
i.e., SAT solvers are used to prove 𝜶 by reductio ad absurdum

The SAT problem


• Deciding if a sentence in propositional logic is satisfiable (SAT) is the prototypical NP-complete problem
(→ see appendix of V03)
• Many computer science problems can be reduced to SAT (e.g., all CSPs of V04)
➔ SAT plays in important role in the literature of AI / complexity theory / computer science in general

Zurich University of Applied Sciences and Arts 9


CAI Centre for Artificial Intelligence (stdm)
Propositional logic
(DE “Aussagenlogik”)

The logical implication 𝑆1 ⇒ 𝑆2 (a.k.a.


Reasoning over (unrelated) facts rule: “𝑆2 if 𝑆1 is true”) shows paradox
• The simplest of all logics to illustrate basic ideas behavior when interpreted in a
colloquial way:
• “if I teach AI then the earth is a
Syntax sphere” is formally true indepen-
dent of content-wise connection.
• If 𝑆 is a sentence, ¬𝑆 is a sentence (negation) But the definition makes sense:
• If 𝑆1 and 𝑆2 are sentences, 𝑆1 ∧ 𝑆2 is a sentence (conjunction, “and”) • “if it is raining then the street gets
• If 𝑆1 and 𝑆2 are sentences, 𝑆1 ∨ 𝑆2 is a sentence (disjunction, “or”) wet” has to be true (as a rule)
regardless of if it is raining (there
• If 𝑆1 and 𝑆2 are sentences, 𝑆1 ⇒ 𝑆2 is a sentence (implication) might be other reasons for a wet
• If 𝑆1 and 𝑆2 are sentences, 𝑆1 ⇔ 𝑆2 is a sentence (biconditional) street).
See it as if saying “if S1 is true then I
claim S2 to be true as well; else, I
make no claim”.
Semantics (rules for evaluating truth with respect to a model 𝑚)
• ¬𝑆 is true 𝑖𝑓𝑓 𝑆 is false
• 𝑆1 ∧ 𝑆2 is true 𝑖𝑓𝑓 𝑆1 is true and 𝑆2 is true
• 𝑆1 ∨ 𝑆2 is true 𝑖𝑓𝑓 𝑆1 is true or 𝑆2 is true
• 𝑆1 ⇒ 𝑆2 is false 𝑖𝑓𝑓 𝑆1 is true and 𝑆2 is false
• 𝑆1 ⇔ 𝑆2 is true 𝑖𝑓𝑓 𝑆1 ⇒ 𝑆2 is true and 𝑆2 ⇒ 𝑆1 is true
𝑖𝑓𝑓
Zurich University of Applied Sciences and Arts 10
CAI Centre for Artificial Intelligence (stdm)
Example: Wumpus world sentences
How logic serves well as a representation language

Notation
• Let 𝑃𝑖,𝑗 be true if there is a pit in i/j
• Let 𝐵𝑖,𝑗 be true if there is a breeze in i/j

Facts: representing factual knowledge


• ¬𝑃1,1
• ¬𝐵1,1 For concrete locations – no variables!
• 𝐵1,2

Rules: representing procedural knowledge


• “Pits cause breezes in adjacent squares“,
→ Example for concrete squares: 𝐵1,1 ⟺ 𝑃1,2 ∨ 𝑃2,1 , …
• “A square is breezy if and only if there is an adjacent pit"
→ Example for concrete squares: 𝐵1,2 ⟺ 𝑃2,2 ∨ 𝑃1,3 , …

Zurich University of Applied Sciences and Arts 11


CAI Centre for Artificial Intelligence (stdm)
Only in higher-order logics do predicates have
other predicates (or functions) as parameters
First-order logic
(FOL, DE “Prädikatenlogik 1. Stufe”)

Pros and cons of propositional logic (as compared to atomic knowledge representation)
• Declarative: pieces of syntax correspond to facts
• Allows partial/disjunctive/negated information (unlike most data structures and databases)
• Compositional: meaning of 𝐵1,1 ∧ 𝑃1,2 is derived from meaning of 𝐵1,1 and of 𝑃1,2
• Meaning is context-independent (unlike natural language, where meaning depends on context)
• Very limited expressive power (unlike natural language)
→ E.g., cannot say “pits cause breezes in adjacent squares“ except by one sentence for each square!
→ It is useful to view the world as consisting of objects and relationships between them

Much greater expressiveness of FOL (like natural language)


• Quantifiable variables over non-logical objects (quantifiers ∀, ∃, ∄: for all, it exists, it doesn’t exist)
• Objects: people, houses, numbers, theories, Ronald McDonald, colors, soccer matches, wars,
centuries, … Assert that the relationship exists: brother_of(x,y)
• Relations (predicates): red, round, bogus, prime, multistoried, brother of, bigger than, inside, part of,
has color, occurred after, owns, comes between, …
• Functions: father of, best friend, third inning of, one more than, end of, …
A function is a relation with only one “value” for any given “parameter”/input: father_of(x)=y

Zurich University of Applied Sciences and Arts 12


CAI Centre for Artificial Intelligence (stdm)
Example: Wumpus world sentences in FOL
How logic serves well as a representation language
We keep track of time/situation via quantification over 𝑡; in propositional
logic, we would need copies of each sentence for each time step.

Perception
• Timestep 𝑡 is smelly if I perceive a Smell (and whatever else) at 𝑡
∀𝑏, 𝑔, 𝑡 𝑃𝑒𝑟𝑐𝑒𝑝𝑡 𝑆𝑚𝑒𝑙𝑙, 𝑏, 𝑔 , 𝑡 ⇒ 𝑆𝑚𝑒𝑙𝑙 𝑡
• If I perceive a Glitter at 𝑡, I am at the place of the gold
∀𝑠, 𝑏, 𝑡 𝑃𝑒𝑟𝑐𝑒𝑝𝑡 𝑠, 𝑏, 𝐺𝑙𝑖𝑡𝑡𝑒𝑟 , 𝑡 ⇒ 𝐴𝑡𝐺𝑜𝑙𝑑(𝑡)

Reflex
• ∀𝑡 𝐴𝑡𝐺𝑜𝑙𝑑 𝑡 ⇒ 𝐴𝑐𝑡𝑖𝑜𝑛(𝐺𝑟𝑎𝑏, 𝑡)

Deduction of hidden properties of a location


• ∀𝑥, 𝑡 𝐴𝑡 𝐴𝑔𝑒𝑛𝑡, 𝑥, 𝑡 ∧ 𝑆𝑚𝑒𝑙𝑙 𝑡 ⇒ 𝑆𝑚𝑒𝑙𝑙𝑦(𝑥)
• A diagnostic rule: ∀𝑦 𝐵𝑟𝑒𝑒𝑧𝑦 𝑦 ⇒ ∃𝑥 𝑃𝑖𝑡(𝑥) ∧ 𝐴𝑑𝑗𝑎𝑐𝑒𝑛𝑡(𝑥, 𝑦)
• A causal rule: ∀𝑥, 𝑦 𝑃𝑖𝑡 𝑥 ∧ 𝐴𝑑𝑗𝑎𝑐𝑒𝑛𝑡 𝑥, 𝑦 ⇒ 𝐵𝑟𝑒𝑒𝑧𝑦(𝑦)
• Definition of the Breezy predicate: ∀𝑦 𝐵𝑟𝑒𝑒𝑧𝑦 𝑦 ⟺ ∃𝑥 𝑃𝑖𝑡(𝑥) ∧ 𝐴𝑑𝑗𝑎𝑐𝑒𝑛𝑡(𝑥, 𝑦)
Zurich University of Applied Sciences and Arts 13
CAI Centre for Artificial Intelligence (stdm)
Building a hybrid agent for the wumpus world
Combining (propositional) logic and problem-solving search

1. Start with an initial KB of atemporal knowledge (e.g., relating breeziness to presence of pits)

2. At each time step, add current percept and temporal axioms (e.g., successor-state axioms)
State what happens to each fluent (i.e., any aspect of the
world that changes) depending on what action is taken

3. Construct a plan based on a hard-coded order of goals (in decreasing priority):


a. If glitter: grab the gold → plan a route to initial location → get out of cave All route planning/finding in the
b. If no such plan: find save route to closest unvisited save square using A* hybrid agent is done using A*
c. If no safe square, but arrow: make a safe square by shooting at a possible wumpus location x/y
(determined by asking 𝐴𝑆𝐾 𝐾𝐵, ¬𝑊𝑥,𝑦 == 𝑓𝑎𝑙𝑠𝑒) after going there
d. If killing wumpus fails: find a square that is not provably unsafe (𝐴𝑆𝐾 𝐾𝐵, ¬𝑂𝐾 𝑡 𝑥,𝑦 == 𝑓𝑎𝑙𝑠𝑒)
and plan to go there
e. If no such square: mission impossible, find route to get out of cave

➔ See AIMA ch. 7.7.2 and Fig. 7.20

Zurich University of Applied Sciences and Arts 14


CAI Centre for Artificial Intelligence (stdm)
More on logic
→ see remarks on knowledge representation in the appendix

«Just as a student of physics requires some familiarity with mathematics, a student of AI


must develop a talent for working with logical notation» (AIMA)

On the importance of logic in AI


• For many years, systems based on logic dominated AI (research & successful practice)
• Example applications:
• Expert systems (e.g., in health & medicine)
• NASA spacecraft control (planning of action sequences, recovery from failures)
• Electronic circuit checking (does it perform the task it is designed for?) and synthesis
• Automatic theorem proving
• They are still broadly applied today
(e.g., in deductive languages like SQL)
• A relevant subfield is logic programming
(e.g., Prolog)

Further reading
• Rege, «Logik Programmierung 1&2» in «Programmiersprachen und -Paradigmen»
• AIMA ch. 7.4-7.6; 8.2-8.3; 9
Zurich University of Applied Sciences and Arts 15
CAI Centre for Artificial Intelligence (stdm)
Where’s the intelligence?
Man vs. machine

• Logical reasoning as a higher-order cognitive process is also applied by humans

• The undertaking of reducing intelligent (human) behavior to logic has failed


→ either because of expressiveness of the language, or because of computational intractability
→ the real world includes uncertainties, but logic works only deterministically
→ it is doubtful if all reasoning in humans can be reduced to logic, too

• Expert systems based on domain ontologies are still helpful in very specific domains
• As with humans, symbolic (i.e., logical) reasoning might be a higher-order process on top of
subsymbolic learning (i.e., machine learning)

Zurich University of Applied Sciences and Arts 16


CAI Centre for Artificial Intelligence (stdm)
2. PLANNING AS SEARCH

Zurich University of Applied Sciences and Arts 17


CAI Centre for Artificial Intelligence (stdm)
Planning and AI

Classical planning
• «Planning is the art and practice of thinking before acting» Patrik Haslum
«... finding a sequence of actions to accomplish a goal ...» AIMA ch. 11.1
• Planning agents seen so far:
• Problem solving agent (V03/V04): atomic representation → needs domain-specific heuristics
• Hybrid propositional logic agent (V05): ground (i.e., variable-free) sentences → may get swamped
➔ The part of AI being conducted by most researchers today calling themselves “AI guys”
without doing “machine learning” Entirely true 2016 – changed
with the current hype
Why is planning so big?
• Solved applications: Large logistics problems, operational planning, robotics, scheduling, …
• Community: Search is its basis; logic & knowledge representation is part of it
→ treated at specialized (ICAPS) and major AI (IJCAI, AAAI, ECCAI) international conferences
• AI’s tendency of spawning new disciplines:
• Many now autonomous disciplines started as a field of study within AI
• Examples: Computer vision, robotics, information retrieval, automatic speech recognition
• Machine learning could take this path, but currently the “buzzword” status of AI keeps it
• Other universalist tendencies: “Everything is search”, “everything is optimization”
One of planning’s big shots: Malte Helmert of University of Basel (→ see http://ai.cs.unibas.ch/misc/tutorial_aaai2015/)

Zurich University of Applied Sciences and Arts 18


CAI Centre for Artificial Intelligence (stdm)
Automated planning

Setting
• a single agent in a (→ multi-agent / game-playing possible)
• fully observable, (→ conformant planning possible)
• sequential and discrete, (→ temporal and real-time planning possible)
• deterministic and (→ probabilistic planning possible)
• static (offline) environment (→ online possible)
“Tower of Hanoi” is a classic
planning example

Tool: Planning Domain Definition Language (PDDL)


• A subset of FOL, more expressive than propositional logic
• Used to define the planning task as a search problem:
• Initial states and goal states
• A set of 𝐴𝑐𝑡𝑖𝑜𝑛(𝑠) in terms of preconditions and effects ➔ 𝑅𝑒𝑠𝑢𝑙𝑡(𝑠, 𝑎)
• Closed world assumption: Unmentioned state variables are assumed false
• Uses a factored representation for states and actions (collection of variables)
• Derived from the STRIPS planning language
Zurich University of Applied Sciences and Arts 19
CAI Centre for Artificial Intelligence (stdm)
PDDL / STRIPS operators
Tidily arranged action descriptions, restricted language
Note that capitalization of
atoms (predicates & terms)
From action schema to ground action: 2 examples is different here as compared
• Action schema: to Datalog (see appendix), to
be consistent with AIMA.
𝐴𝑐𝑡𝑖𝑜𝑛( 𝐹𝑙𝑦(𝑝, 𝑓𝑟𝑜𝑚, 𝑡𝑜),
𝑃𝑟𝑒𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛: 𝐴𝑡(𝑝, 𝑓𝑟𝑜𝑚) ∧ 𝑃𝑙𝑎𝑛𝑒(𝑝) ∧ 𝐴𝑖𝑟𝑝𝑜𝑟𝑡(𝑓𝑟𝑜𝑚) ∧ 𝐴𝑖𝑟𝑝𝑜𝑟𝑡(𝑡𝑜)
𝐸𝑓𝑓𝑒𝑐𝑡: ¬𝐴𝑡(𝑝, 𝑓𝑟𝑜𝑚) ∧ 𝐴𝑡(𝑝, 𝑡𝑜))
• Ground action (all variables have been substituted with values):
𝐴𝑐𝑡𝑖𝑜𝑛( 𝐵𝑢𝑦 𝑀𝑎𝑟𝑠ℎ𝑎𝑙𝑙𝐺𝑢𝑖𝑡𝑎𝑟𝐵𝑜𝑥, 𝐵𝑎𝑐𝑘𝑠𝑡𝑎𝑔𝑒𝑀𝑢𝑠𝑖𝑐 , Upper-case constants
𝑃𝑟𝑒𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛: 𝐴𝑡 𝐵𝑎𝑐𝑘𝑠𝑡𝑎𝑔𝑒𝑀𝑢𝑠𝑖𝑐 ∧ 𝑆𝑒𝑙𝑙𝑠 𝐵𝑎𝑐𝑘𝑠𝑡𝑎𝑔𝑒𝑀𝑢𝑠𝑖𝑐, 𝑀𝑎𝑟𝑠ℎ𝑎𝑙𝑙𝐺𝑢𝑖𝑡𝑎𝑟𝐵𝑜𝑥
𝐸𝑓𝑓𝑒𝑐𝑡: 𝐻𝑎𝑣𝑒 𝑀𝑎𝑟𝑠ℎ𝑎𝑙𝑙𝐺𝑢𝑖𝑡𝑎𝑟𝐵𝑜𝑥 )
→Note: this abstracts away many important details of buying

Restricted language allows for efficient algorithms


• Action precondition: conjunction of positive literals
• Action effect: conjunction of literals
• Applicability of action 𝑎 in state 𝑠: 𝑖𝑓𝑓 𝑠 ⊨ 𝑃𝑟𝑒𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛 𝑎
→ E.g., ∀𝑝, 𝑓𝑟𝑜𝑚, 𝑡𝑜 𝐹𝑙𝑦 𝑝, 𝑓𝑟𝑜𝑚, 𝑡𝑜 ∈ 𝐴𝑐𝑡𝑖𝑜𝑛𝑠 𝑠 ⟺ 𝑠 ⊨ (𝐴𝑡(𝑝, 𝑓𝑟𝑜𝑚) ∧ 𝑃𝑙𝑎𝑛𝑒(𝑝) ∧ 𝐴𝑖𝑟𝑝𝑜𝑟𝑡(𝑓𝑟𝑜𝑚) ∧ 𝐴𝑖𝑟𝑝𝑜𝑟𝑡(𝑡𝑜))
• Computing the result: 𝑅𝑒𝑠𝑢𝑙𝑡 𝑠, 𝑎 = 𝑠 − 𝐷𝑒𝑙(𝑎) ∪ 𝐴𝑑𝑑(𝑎) without explicit reference to time!
(delete list contains all negative literals in 𝐸𝑓𝑓𝑒𝑐𝑡𝑠(𝑎), add list all positives)

Zurich University of Applied Sciences and Arts 20


CAI Centre for Artificial Intelligence (stdm)
Example: Air cargo transport
See appendix for another example

• A classical mini transportation problem: Loading / unloading cargo, flying between different airports
• Actions: 𝐿𝑜𝑎𝑑(𝑐𝑎𝑟𝑔𝑜, 𝑝𝑙𝑎𝑛𝑒, 𝑎𝑖𝑟𝑝𝑜𝑟𝑡), 𝑈𝑛𝑙𝑜𝑎𝑑(𝑐𝑎𝑟𝑔𝑜, 𝑝𝑙𝑎𝑛𝑒, 𝑎𝑖𝑟𝑝𝑜𝑟𝑡), 𝐹𝑙𝑦(𝑝𝑙𝑎𝑛𝑒, 𝑎𝑖𝑟𝑝𝑜𝑟𝑡, 𝑎𝑖𝑟𝑝𝑜𝑟𝑡)
• Predicates: 𝐼𝑛(𝑐𝑎𝑟𝑔𝑜, 𝑝𝑙𝑎𝑛𝑒), 𝐴𝑡(𝑐𝑎𝑟𝑔𝑜 ∨ 𝑝𝑙𝑎𝑛𝑒, 𝑎𝑖𝑟𝑝𝑜𝑟𝑡)
• Complete PDDL planning problem description (with all variables existentially quantified [∃]):
Initial & goal state are
given; 𝐴𝑐𝑡𝑖𝑜𝑛(𝑠) and
𝑅𝑒𝑠𝑢𝑙𝑡(𝑠, 𝑎) follow
from action schemas.

• Plan: C1@P1
[𝐿𝑜𝑎𝑑(𝐶1, 𝑃1, 𝑆𝐹𝑂), 𝐹𝑙𝑦(𝑃1, 𝑆𝐹𝑂, 𝐽𝐹𝐾), 𝑈𝑛𝑙𝑜𝑎𝑑(𝐶1, 𝑃1, 𝐽𝐹𝐾),
SFO JFK
𝐿𝑜𝑎𝑑(𝐶2, 𝑃2, 𝐽𝐹𝐾), 𝐹𝑙𝑦(𝑃2, 𝐽𝐹𝐾, 𝑆𝐹𝑂), 𝑈𝑛𝑙𝑜𝑎𝑑(𝐶2, 𝑃2, 𝑆𝐹𝑂). ]
C2@P2

Zurich University of Applied Sciences and Arts 21


CAI Centre for Artificial Intelligence (stdm)
Planning as state-space search
…approachable with any algorithm from V03 or local search

Two formulations
• Forward (progression): search considers actions that are applicable
• Backward (regression): search considers actions that are relevant
• Neither of them is efficient without good heuristics!

Futility of uninformed forward search


• Example 1: Buying a copy of AIMA
• Tool: Action schema 𝐵𝑢𝑦(𝑖𝑠𝑏𝑛) with effect 𝑂𝑤𝑛 𝑖𝑠𝑏𝑛
→ 10-digit ISBN leads to 1010 = 𝟏𝟎 𝒃𝒊𝒍𝒍𝒊𝒐𝒏 ground actions to be enumerated
• Example 2: Moving all cargo from airport 𝐴 to airport 𝐵
• Setting: 10 airports with 5 planes and 20 pieces of cargo at each
• Obvious solution: load all cargo at 𝐴 in one of the planes, fly to 𝐵, unload everything (41 actions)
→ search graph has 𝟐𝟎𝟎𝟎𝟒𝟏 nodes up to this depth (assuming ~2’000 actions per state on average)
• 450 if all cargo is at airports without planes (10*5 airplanes can each fly to 9 other airports)
Zurich University of Applied Sciences and Arts 22
CAI Centre for Artificial Intelligence (stdm)
• 10450 if all cargo and planes are at the same airport (10*20 cargo packages can be each loaded
into 10*5 planes, plus the 50 planes can fly to 9 other airports)
Heuristics for forward state-space search
Enabled by factored representations for states & actions

Possible domain-independent heuristics


• Relaxing actions (i.e., adding new links to the graph to ease the problem)
• Ignore-preconditions heuristic: All actions are applicable anytime
→ leads e.g. easily to the 2 different heuristics for the 𝑛-puzzle of V03

• Ignore-delete-lists heuristic: Removing all negative literals from effects


→ enables making monotonic progress towards goal, achievable e.g. with hill climbing

• State abstractions (i.e., collapsing multiple states into a single one to shrink the graph)
• Reduce the state space by e.g. ignoring some fluents

Winners of the bi-annual ICAPS planning competition often used


• Heuristic search (→ see FastDownward system: Helmert et al. 2004, http://www.fast-downward.org/)
• Planning graphs (→ see appendix)
• SAT appendix (→ see earlier)
Zurich University of Applied Sciences and Arts 23
CAI Centre for Artificial Intelligence (stdm)
Hierarchical planning
A modern, more general alternative

The need for abstraction


• Atomic actions for humans: ca. 103 muscles, max. 10 mindful activations per second
• Plan for a lifetime: ca. 109 wake seconds ➔ ca. 1013 possible actions per life
• Plan for 2 weeks vacation: ca. (60 ∗ 60 ∗ 24 ∗ 14) × 103 × 10 ≈ 1010 actions
• Methods seen so far willful
How many work only for thousands
decisions (i.e.,
(= possible ≪ 1010 ) in
actions) ofaactions
2-week vacation?
➔ hierarchical decomposition
(e.g., “go to ZRH” → “get to train station, take train to Zurich airport, ascend to departure hall” → …)

Technical solution sketch


• Hierarchical task networks (HTN): more factored representations for actions (besides states)
• Two kinds of actions:
• Primitive actions: standard precondition-effect schemas
• High level actions (HLA): e.g., “go to ZRH” → have one or more possible refinements
• Refinement: a sequence of HLAs or primitive actions, maybe recursive
• Key benefits: Possibly huge speed improvements, possibility for humans to define HLAs

Zurich University of Applied Sciences and Arts 24


CAI Centre for Artificial Intelligence (stdm)
Where’s the intelligence?
Man vs. machine

• Planning is foremost an exercise in controlling combinatorial explosion


• It does so by combining efficient search & logical reasoning
→ necessary speedups are achieved by domain-independent heuristics that exploit structure in the
representation
→ this is really smart

• But: There is no clear understanding yet of which methods work best on what problems

• In contrast to popular opinion, AI planning is widely applied in practice today


→ Also, research is not “dead”, but less hyped at the moment
→ Probably planning is the best that symbolic AI currently offers

Zurich University of Applied Sciences and Arts 25


CAI Centre for Artificial Intelligence (stdm)
Review

• Logical agents
• apply inference to a knowledge base to derive new information and make decisions
• Wumpus world requires the ability to represent partial and negated information, reason by
cases, etc.

• Planning is AI’s main field, due to success stories like remotely controlling a NASA
spacecraft in real time
• Planning refers to problem solving techniques (i.e., search) on factored (i.e., logic-based)
representations of states and actions, allowing for fast algorithms
• PDDL describes the initial and goal states as conjunctions of literals; actions in terms of their
preconditions and effects

• Valid approaches include using SAT or CSP solvers


• FOL-based planning has much-needed expressiveness for larger real-world problems, but yet no
efficient algorithms (missing heuristics)

• Hierarchical task networks (HTN) allow the agent to take advice from the domain
designer in the form of high-level actions (HLAs) that can be implemented in various
ways by lower-level action sequences
• HTN methods can create very large plans required by many real-world applications

• It is yet unknown which approach to planning is best

Zurich University of Applied Sciences and Arts 26


CAI Centre for Artificial Intelligence (stdm)
APPENDIX

A) Logic
B) Reasoning in databases – an example
C) Datalog
D) Planning

Zurich University of Applied Sciences and Arts 27


CAI Centre for Artificial Intelligence (stdm)
A. LOGIC

Zurich University of Applied Sciences and Arts 28


CAI Centre for Artificial Intelligence (stdm)
Key concept: Entailment
(logical consequence, DE “semantische Implikation”)

𝐾𝐵 ⊨ 𝛼
• Intuitively, entailment means that one thing follows from another: “from 𝐾𝐵 I know that 𝛼”
• Formally, “knowledge base 𝐾𝐵 entails sentence 𝛼 𝑖𝑓𝑓 𝛼 is true in all worlds where 𝐾𝐵 is true”
• Example: A KB containing 𝑥 + 𝑦 = 4 entails 4 = 𝑥 + 𝑦

➔ Entailment is a relationship between sentences (i.e., syntax) that is based on semantics

Example: Entailment in the wumpus world


• Figure: the situation after detecting nothing in 1/1 → moving right → breeze
• Is 𝛼1 (“no pit in 2/1”) true, given the KB (wumpus-world rules & percept)?
➔ See next slide

Zurich University of Applied Sciences and Arts 29


CAI Centre for Artificial Intelligence (stdm)
Entailment in the wumpus world, contd.

Consider possible models for all “?”, assuming only pits


(3 Boolean choices give 23 possible models)

All possible models for the presence of pits in 2/1, 2/2 and 1/3:
• Red: all models compliant with the KB
(KB := wumpus-world rules + percepts)
• Blue: all models where KB is false
• Yellow: all models for sentence
𝛼1 =“no pit in 2/1”
➔ 𝐾𝐵 ⊨ 𝛼1
(the sentence, not all possible models for it!)
➔ i.e., 𝐾𝐵 is a stronger assertion than 𝛼1
(ruling out more possible worlds/models)
Formal: 𝛼 ⊨ 𝛽 𝑖𝑖𝑓 𝑀 𝛼 ⊆ 𝑀 𝛽
(with 𝑀(𝛼) being the set of all models of 𝛼)

Possible models for KB ⊆ 𝑝𝑜𝑠𝑠𝑖𝑏𝑙𝑒 𝑚𝑜𝑑𝑒𝑙𝑠 𝑓𝑜𝑟 𝛼1


➔ 𝐾𝐵 ⊨ 𝛼1
Zurich University of Applied Sciences and Arts 30
CAI Centre for Artificial Intelligence (stdm)
Exercise: Precise difference between ⊢ and ⇒?

Intuitively, entailment and implication in propositional logic


are related, somehow. But what is the precise
relationship?

Discuss with your neighbor (5 minutes).

(In case of prolonged confusion, look up Mike Shulman’s answer of January 4, 2011, in this
thread: https://golem.ph.utexas.edu/category/2011/01/entailment_and_implication.html)

Zurich University of Applied Sciences and Arts 31


CAI Centre for Artificial Intelligence (stdm)
Exercise: Pen & paper logic
→ see P03b

Following Russell & Norvig’s advice that “a student of AI


must develop a talent for working with logical notation”, this
is to get you acquainted with formulating and manipulating
known facts in logical notation, and to do inference to arrive
at new conclusions.
Get together in teams of 2-3 and collectively solve the
following exercises from P03b using pen, paper and the
previous and next 2 slides. Distribute the work amongst
your group and make sure to explain each result to every
group member.

1.1 – truth of sentences in propositional logic


1.2 – validity & satisfiability in propositional logic
1.3 – entailment in the wumpus world
2.1 – formulating sentences in first-order logic
3.1 – inference in first-order logic

Prepare to explain your findings to the class.


Zurich University of Applied Sciences and Arts 32
CAI Centre for Artificial Intelligence (stdm)
Exercise: Pen & paper logic (contd.)
→ see P03b

Following Russell & Norvig’s finding that “a student of AI


must develop a talent for working with logical notation”
[AIMA 3rd ed.], this is to get you acquainted with
formulating and manipulating known facts in logical
notation, and to do inference to arrive at new conclusions.

Get together in teams of 2-3 and collectively solve the


following exercises from P03b using pen, paper and the
previous slides. Distribute the work amongst you group and
make sure to explain each result to every group member.

2.2 – formulating sentences in first-order logic


2.3 – formulating sentences in first-order logic
3.2 – inference in first-order logic

Prepare to explain your findings to the class.

➔ See also definitions in the appendix

Zurich University of Applied Sciences and Arts 33


CAI Centre for Artificial Intelligence (stdm)
Exercise background: Logical equivalence
i.e., rules to manipulate sentences of logic

Two sentences are logically equivalent 𝑖𝑓𝑓 true in same models:


• 𝛼 ≡ 𝛽 if and only if 𝛼 ⊨ 𝛽 and 𝛽 ⊨ 𝛼

(𝛼 ∧ 𝛽) ≡ (𝛽 ∧ 𝛼) commutativity of ∧
(𝛼 ∨ 𝛽) ≡ (𝛽 ∨ 𝛼) commutativity of ∨
((𝛼 ∧ 𝛽) ∧ 𝛾) ≡ (𝛼 ∧ (𝛽 ∧ 𝛾)) associativity of ∧
((𝛼 ∨ 𝛽) ∨ 𝛾) ≡ (𝛼 ∨ (𝛽 ∨ 𝛾)) associativity of ∨
¬(¬𝛼 ) ≡ 𝛼 double-negation elimination
(𝛼 ⇒ 𝛽) ≡ (¬𝛽 ⇒ ¬𝛼) contraposition
(𝛼 ⇒ 𝛽) ≡ (¬𝛼 ∨ 𝛽) implication elimination
(𝛼 ⟺ 𝛽) ≡ ( 𝛼 ⇒ 𝛽 ∧ 𝛽 ⇒ 𝛼 ) biconditional elimination
¬(𝛼 ∧ 𝛽) ≡ (¬𝛼 ∨ ¬𝛽) De Morgan
¬(𝛼 ∨ 𝛽) ≡ (¬𝛼 ∧ ¬𝛽) De Morgan
(𝛼 ∧ (𝛽 ∨ 𝛾)) ≡ ((𝛼 ∧ 𝛽) ∨ (𝛼 ∧ 𝛾)) distributivity of ∧ over ∨
(𝛼 ∨ (𝛽 ∧ 𝛾)) ≡ ((𝛼 ∨ 𝛽) ∧ (𝛼 ∨ 𝛾)) distributivity of ∧ over ∨

Zurich University of Applied Sciences and Arts 34


CAI Centre for Artificial Intelligence (stdm)
Propositional logic cheat sheet

Zurich University of Applied Sciences and Arts 35


CAI Centre for Artificial Intelligence (stdm)
First order logic cheat sheet

Zurich University of Applied Sciences and Arts 36


CAI Centre for Artificial Intelligence (stdm)
Building a purely logical agent
Example using propositional logic

1. Construct a sentence that includes


a. 𝐼𝑛𝑖𝑡 0 : collection of assertions about the initial state
b. 𝑇𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛1 , … , 𝑇𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛𝑡 : successor-state axioms for all possible actions at each time up to 𝑡
c. 𝐻𝑎𝑣𝑒𝐺𝑜𝑙𝑑𝑡 ∧ 𝐶𝑙𝑖𝑚𝑏𝑒𝑑𝑂𝑢𝑡𝑡 : the assertion that the goal is reached at time 𝑡

2. Solve with a SAT solver (see appendix)

3. If satisfiable: extract all variables representing actions that are assigned true in the model
➔ this is the plan

➔ See AIMA ch. 7.7.4

Zurich University of Applied Sciences and Arts 37


CAI Centre for Artificial Intelligence (stdm)
Practical considerations in building a KB

Building a representation and reasoning system


1. Begin with a task domain
2. Decide on which objects (individuals) you want to talk about
(includes determining correspondence between symbols in the computer and objects/relations in world)
3. Determine what relationships (predicate symbols) you want to represent
(includes determining which sentences will be true and which will be false)
4. Choose symbols in the computer to denote objects and relations
(includes deciding which constant denotes which individual)
5. Tell the system knowledge about the domain (see below)
6. Ask the system questions

Ontological engineering
• It is unclear (computationally and philosophically) if special-purpose ontologies can be merged
into a general-purpose one
• But: Using upper ontologies («world knowledge») connected to task-specific ones is a way
that works for many domains
• OWL, the W3C-standardized description logic, is very expressive but still seldom used
Zurich University of Applied Sciences and Arts 38
CAI Centre for Artificial Intelligence (stdm)
More on knowledge representation (KR)

Remarks
• KR as presented here is most helpful today in the context of ontologies (expert systems,
semantic web)
• There are alternative forms of KR besides formal (deductive) languages, e.g.:
• Procedural languages (e.g., Python code)
• Subsymbolic representations (e.g., the weights in a neural network)
• AI planning uses KR and reasoning in a less formal way (→ see V07)
A neural network encodes all its
knowledge (i.e., all it has learned about
data) in the weights 𝑤𝑖,𝑗 and 𝑤′𝑗,𝑘

The OWL-DL part of the web


ontology language refers to a
decidable subset of FOL → queries
will eventually be answered

Further reading
• AIMA ch. 8.4; 10
Zurich University of Applied Sciences and Arts 39
CAI Centre for Artificial Intelligence (stdm)
Other types of knowledge & their representation
based on Z. Alvi, VU “Artificial Intelligence CS607”, lecture 14

• Procedural knowledge: Describes how to do things, provides a set of directions of how to perform
certain tasks, e.g., how to drive a car
• Declarative knowledge: It describes objects, rather than processes. What is known about a
situation, e.g., it is sunny today, cherries are red
• Meta knowledge: Knowledge about knowledge, e.g., the knowledge that blood pressure is more
important for diagnosing a medical condition than eye color
• Heuristic knowledge: (Empirical) rule-of-thumb, e.g., if I start seeing shops, I am close to a market
• Structural knowledge: Describes structures and their relationships, e.g., how the various parts of
the car fit together to make a car, or knowledge structures in terms of concepts, sub concepts, and
objects

Representations
• Pictures and symbols: This is how the earliest humans represented knowledge when sophisticated
linguistic systems had not yet evolved
• Graphs and Networks:
→ allow relationships between entities, e.g., to show family relationships, now we can use a graph
→ May be used to represent procedural knowledge, e.g., how to start a car?
• Numbers: Eventually, every representation we use gets translated to numbers in the computer's
internal representation

Zurich University of Applied Sciences and Arts 40


CAI Centre for Artificial Intelligence (stdm)
B. REASONING IN DATABASES – AN EXAMPLE

Inspired by Bill Howe’s «Introduction to Data Science», lecture 9


Coursera / University of Washington

Zurich University of Applied Sciences and Arts 41


CAI Centre for Artificial Intelligence (stdm)
Prerequisite: Storing knowledge in graphs
based on Jana Koehler’s “DB & SemWeb: Subsumption in OWL-DL”, HSLU 2016

Semantic web technology stack Implementing an ontology (a graph)…

…using triples

…in a database

Zurich University of Applied Sciences and Arts 42


CAI Centre for Artificial Intelligence (stdm)
Problem: We’re interested in pattern matching
…in graphs such as records in relational databases

Task
• For a given graph and pattern, find all instances of the pattern

𝑌
𝑋

• Results:
• 𝑋 = 𝑎, 𝑌 = 𝑏; 𝑋 = 𝑏, 𝑌 = 𝑎
• 𝑋 = 𝑔, 𝑌 = 𝑐; 𝑋 = 𝑐, 𝑌 = 𝑔

Zurich University of Applied Sciences and Arts 43


CAI Centre for Artificial Intelligence (stdm)
Example: Adverse drug reaction research

Given a graph with edge labels


• Drug 𝑋 interferes with drug 𝑌
• Drug 𝑌 regulates the expression of gene 𝑍
• Gene 𝑍 is associated with disease 𝑊

…find drugs that interfere with another drug involved in the treatment of a disease

𝑊
𝑋 𝑌 𝑍

Zurich University of Applied Sciences and Arts 44


CAI Centre for Artificial Intelligence (stdm)
A Datalog solution

Datalog – a pattern expression language for DB queries, based on logic


• Assuming a relation 𝑟(𝑠𝑢𝑏𝑗𝑒𝑐𝑡, 𝑝𝑟𝑒𝑑𝑖𝑐𝑎𝑡𝑒, 𝑜𝑏𝑗𝑒𝑐𝑡) and pseudo syntax
result(X) <=
r(X, interferes_with, Y) &
r(Y, regulates, Z) &
r(Z, associated_with, W)

𝑊
𝑋 𝑌 𝑍

• Assuming relations 𝑖𝑛𝑡𝑒𝑟𝑓𝑒𝑟𝑒𝑠𝑊𝑖𝑡ℎ(𝑑𝑟𝑢𝑔1, 𝑑𝑟𝑢𝑔2), 𝑟𝑒𝑔𝑢𝑙𝑎𝑡𝑒𝑠(𝑑𝑟𝑢𝑔, 𝑔𝑒𝑛𝑒) and


𝑎𝑠𝑠𝑜𝑐𝑖𝑎𝑡𝑒𝑑𝑊𝑖𝑡ℎ(𝑔𝑒𝑛𝑒, 𝑑𝑖𝑠𝑒𝑎𝑠𝑒):
result(X) <=
interferesWith(X, Y) &
regulates(Y, Z) &
associatedWith(Z, W)

Zurich University of Applied Sciences and Arts 45


CAI Centre for Artificial Intelligence (stdm)
Example: PRISM-like dragnet investigation
https://www.bloomberg.com/news/articles/2011-11-22/palantir-the-war-on-terrors-secret-weapon

Datalog-syntax: add fact to KB

+ boughtFlight(‘Fikri’, ‘Cairo’, ‘Miami’, ‘oneway’,


«In October, a foreign national named Mike 2018-10-4)
Fikri purchased a one-way plane ticket
A predicate A term (constant)
from Cairo to Miami, where he rented an
apartment»

«Over the next few weeks, he’d made a + withdrawal(‘Fikri’, 5000, ‘some bank’, 2016-11-2)
number of large withdrawals from a + withdrawal(‘Fikri’, 2000, ‘some bank’, 2016-11-21)
Russian bank account and placed repeated

calls to a few people in Syria»

«More recently, he rented a truck, drove to + rented(‘Fikiri’, ‘truck’, ‘Miami’, ‘Orlando’, 2019-
01-30)
Orlando, and visited Disney World himself»

Zurich University of Applied Sciences and Arts 46


CAI Centre for Artificial Intelligence (stdm)
PRISM modeled in Datalog
Rules to reason over the just stated facts
A rule Logical AND
flag(Person, 1, Date) <= boughtFlight(Person, Origin, Destination, Oneway, Date) &
flaggedAirport(Origin) &
usAirports(Destination) &
Oneway=‘oneway’

foreignWithdrawal(Person, sum(Amount), Date) <= withdrawal(Person, Amount, Bank, Date) &


foreignBank(Bank) &
Amount > 1000

flag(Person, 1, Date) <= foreignWithdrawal(Person, Tot_amount, Date) & Tot_amount > 10000

flag(Person, 1, Date) <= rented(Person, Vehicle, Origin, Dest, Date) & importantLocation(Dest)

totalflags(Person, sum(Flag), min(Date), max(Date)) <= flag(Person, Flag, Date)

alert(Person, Flag_cnt, Min_date, Max_date) <= totalflags(Person, Flag_cnt, Min_date, Max_date) &
Max_date - Min_date < 10 & #days
Flag_cnt > 3

Zurich University of Applied Sciences and Arts 47


CAI Centre for Artificial Intelligence (stdm)
Strengths of a high-level logical language

Who has contacted whom, when?


contacted(Person1, Person2, Time) <= email(Person1, Person2, Message, Time,…)
contacted(Person1, Person2, Time) <= called(Time, Voicemail, Person1, Person2, …)
contacted(Person1, Person2, Time) <= text_message(Time, Message, Person1, Person2, …)

The data probably comes from a lot of different systems


(RDBMS, triple-store, files on Hadoop, …), but syntactic
integration doesn’t take a lot of work

Who could have known before January 30 that 𝑋 was going to happen?
knew(‘Smith’)
Self-reference / recursion: not possible in standard SQL
knew(Person2) <= (though certain DBMS implement it)
knew(Person1) &
contacted(Person1, Person2, Message, Time) &
Time < 2019-01-30

knew(Person2) <=
knew(Person1) &
met_with(Person1, Person2, Time) &
Time < 2019-01-30

Zurich University of Applied Sciences and Arts 48


CAI Centre for Artificial Intelligence (stdm)
C. DATALOG

Zurich University of Applied Sciences and Arts 49


CAI Centre for Artificial Intelligence (stdm)
Datalog - A relevant subset of FOL
Decision problem: A question (e.g., “is a sentence of
FOL true?”) is decidable if an efficient algorithm
exists that can and will return the answer (yes/no).
Background
• Full FOL is very expressive, but not decidable in general
• Thus: Fallback to first-order definite clauses: “∧” of unnegated terms ⟹ unnegated term
• Can represent the type of knowledge typically found in relational databases
• Still powerful (allows recursion!), but not Turing-complete

FYI – more precisely, Horn clauses: also valid without the implication
Some modifications (for efficient evaluation):
• Variables in the head also appear in the body of a clause
• Under certain conditions, up to one negated term in the body is allowed (“stratified negation”)
• Usually no functional symbols (not true in pyDatalog)

Datalog terminology head body


• Knowledge base: a set of clauses
• Clause: is either an atomic symbol (fact) or of the form 𝛼 ⟸ 𝛽1 ∧ ⋯ ∧ 𝛽𝑚 (rule) (with atoms 𝛼, 𝛽𝑖 )
• Atom: has either the form 𝑝 or 𝑝(𝑡1 , … , 𝑡𝑛 ), (with predicate 𝑝 and terms 𝑡𝑖 ) → e.g., 𝑝(𝑋), 𝑡𝑒𝑎𝑐ℎ𝑒𝑠(𝑠𝑡𝑎𝑑𝑒𝑙𝑚𝑎𝑛𝑛, 𝑎𝑖)
• Predicate symbol: starts with lower-case letter → e.g., 𝑝, 𝑡𝑒𝑎𝑐ℎ𝑒𝑠
• Term: is either a variable or a constant
• Variable: starts with upper-case letter → e.g., 𝑋, 𝑃𝑒𝑟𝑠𝑜𝑛1
• Constant: starts with lower-case letter or is a sequence of digits → e.g., 5, 𝑠𝑡𝑎𝑑𝑒𝑙𝑚𝑎𝑛𝑛, 𝑎𝑖

Zurich University of Applied Sciences and Arts 50


CAI Centre for Artificial Intelligence (stdm)
Example: Converting measures with pyDatalog
Here pure Python without DB connection

from pyDatalog import pyDatalog


#create terms
pyDatalog.create_terms('scale', 'A, B, C, V', 'conv') #rather atoms (terms and predicates)
#some facts (atoms, here specifically functional predicates)
scale['meter', 'inch'] = 39.3700787
scale['mile', 'inch'] = 63360.0
scale['feet', 'inch'] = 12.0
#some rules (these make it powerful: e.g., the 1. one that computes an arbitrary conversion path via recursion)
scale[A, B] = scale[A, C] * scale[C, B] #adding transitivity
scale[A, B] = 1/scale[B, A]
conv[V, A, B] = V * scale[A, B]
#some queries
print(scale['inch', 'meter'] == V)
print(scale['mile', 'meter'] == V)
print(conv[3, 'mile', 'meter'] == V) #note that we never explicitly defined how to convert miles to meters
print(conv[1, 'meter', 'feet'] == V)

Source: https://mcturra2000.wordpress.com/2014/09/14/logic-programming-example-unit-conversion-using-datalog/
Installing pyDatalog: pip install pyDatalog
See also: https://sites.google.com/site/pydatalog/

Zurich University of Applied Sciences and Arts 51


CAI Centre for Artificial Intelligence (stdm)
Inference in Datalog

Foundation: Modus Ponens


• An inference rule known since antiquity: «If 𝛼 ⇒ 𝛽 and 𝛼 == 𝑡𝑟𝑢𝑒, then 𝛽 == 𝑡𝑟𝑢𝑒»
• Also known as implication elimination

Example: Your new pet «Fritz» croaks and eats flies; is it green? (see https://en.wikipedia.org/wiki/Backward_chaining)
• Facts:
• 𝑐𝑟𝑜𝑎𝑘𝑒𝑠(𝑓𝑟𝑖𝑡𝑧)
• 𝑒𝑎𝑡𝑠𝐹𝑙𝑖𝑒𝑠(𝑓𝑟𝑖𝑡𝑧)
• Rules:
• 𝑐𝑟𝑜𝑎𝑘𝑒𝑠 𝑋 ∧ 𝑒𝑎𝑡𝑠𝐹𝑙𝑖𝑒𝑠 𝑋 ⇒ 𝑓𝑟𝑜𝑔 𝑋
• 𝑐ℎ𝑖𝑟𝑝𝑠 𝑋 ∧ 𝑠𝑖𝑛𝑔𝑠 𝑋 ⇒ 𝑐𝑎𝑛𝑎𝑟𝑦 𝑋
• 𝑓𝑟𝑜𝑔 𝑋 ⇒ 𝑔𝑟𝑒𝑒𝑛 𝑋 Logic notation for the
• 𝑐𝑎𝑛𝑎𝑟𝑦 𝑋 ⇒ 𝑦𝑒𝑙𝑙𝑜𝑤 𝑋 Modus Ponens rule

Applying Modus Ponens forward


2 ways of answering this
• Data-driven: start from true facts → use rules to derive new true facts → eventually arrive at goal
• Goal-driven: assume goal is true → use rules to assert other facts as true → eventually arrive at
known true facts
Applying Modus Ponens backward

Zurich University of Applied Sciences and Arts 52


CAI Centre for Artificial Intelligence (stdm)
Inference in Datalog (contd.)

Forward chaining Backward chaining


• The data-driven approach: • The goal-driven approach: produces no
search for true antecedents («if clauses») → unnecessary facts
infer consequent («then clause») to be true → • Sound and complete for Horn clauses
add this information to KB • Typically implemented using a form of
• Intuitively understandable SLD resolution (usually using depth-first
• Sound and complete for Datalog search)
• Efficiently implementable for Datalog → also used in pyDatalog
(a clause can be viewed as defining a CSP)
→ runs in polynomial time

• Humans control forward chaining carefully


to not get flooded by irrelevant facts; but:
“if I am indoors and I hear rain fall ➔ I might Map coloring as (a) a constraint graph and (b) as a
conclude that the picnic will be canceled” single definite clause.

Zurich University of Applied Sciences and Arts 53


CAI Centre for Artificial Intelligence (stdm)
Datalog in practice

History and future


• Lots of research during the 1980’s, including many extensions
→ Ceri et al., “What You Always Wanted to Know About Datalog (And Never Dared to Ask)”,
IEEE Trans. Knowledge & Data Engineering, 1989
• Ideas influenced mainstream database technology (e.g., recursion in SQL:1999) and the
semantic web
→ https://en.wikipedia.org/wiki/Datalog
• Resurged interest since the rise of big data (compare also B. Howe’s lecture)
→ deMoor et al., “Datalog Reloaded – 1st International Workshop, Datalog 2010”, Springer LNCS, 2010
• LogicBlox is a company build around Datalog (product, research and tech. transfer)
→ Aref, “Datalog for Enterprise Software – From Industrial Applications to Research”, ICLP 2010
• Used to interpret deep neural networks (XAI)
→ Bartoli et al., “From Common Sense Reasoning to Neural Network Models: a Conditional and Multi-
preferential Approach for Explainability and Neuro-Symbolic Integration (an Overview)”

The pyDatalog interpreter


• Light weight, fast, and includes many extensions that facilitate efficiency and convenience
• Memoization of intermediate results
• Access to 11 SQL dialects via integration with SQLAlchemy, incl.aggregate functions & OOP support
• Mapping of logical terms to Python data structures or records from a DB
Zurich University of Applied Sciences and Arts 54
CAI Centre for Artificial Intelligence (stdm)
Example: Querying the chinook.db database

• A DB on music/media information and a company that sells them


• Access it from a terminal (DB in current directory, sqlite installed): sqlite3 chinook.db
• List all tables using the sqlite prompt: .tables

Zurich University of Applied Sciences and Arts 55


CAI Centre for Artificial Intelligence (stdm)
Example (contd.): DB query using pyDatalog
Accessing existing relations and fetching results

from sqlalchemy.ext.declarative import declarative_base; from sqlalchemy import create_engine


from sqlalchemy.orm import sessionmaker; from pyDatalog import pyDatalog
# define a base class with SQLAlchemy and pyDatalog capabilities
Base = declarative_base(cls=pyDatalog.Mixin, metaclass=pyDatalog.sqlMetaMixin)
# load a database from the same directory and create a session, then associate it to the Base class
engine = create_engine('sqlite:///chinook.db') #, echo=False)
Session = sessionmaker(bind=engine)
session = Session()
Base.session = session
# classes that inherit from Base will now have both pyDatalog and SQLAlchemy capability
# the approach can be used to load an existing KB from a database relation, using __table_args__ :
class Track(Base):
__tablename__ = 'tracks'
__table_args__ = {'autoload':True, 'autoload_with':engine} #autoload the model

def __repr__(self): #specifies how to print a Track


return "'" + self.Name + "' costs $" + str(self.UnitPrice)
# the Track class can now be used in in-line queries; example: which track is at least 5s long?
X = pyDatalog.Variable()
Track.Milliseconds[X] >= 5000000
print(X) #outputs ['Through a Looking Glass' costs $1.99, 'Occupation / Precipice' costs $1.99]

Installing SQLAlchemy: conda install sqlalchemy


Using SQLite: https://www.codeproject.com/Articles/850834/Installing-and-Using-SQLite-on-Windows
Finding the example DB: http://www.sqlitetutorial.net/sqlite-sample-database/ (→ see also DB schema on previous slide)

Zurich University of Applied Sciences and Arts 56


CAI Centre for Artificial Intelligence (stdm)
Datalog vs. SPARQL

Pro SPARQL
• Designed for graph queries

Con SPARQL
• Not algebraically closed (input is a graph, but output is a set of records)
• Limited expressiveness (no arbitrary recursion)
• If input is tabular, you have to shred it into a graph before using SPARQL
(➔ often a 3x-x blow up in size!)
• Everything has to be in one graph

Zurich University of Applied Sciences and Arts 57


CAI Centre for Artificial Intelligence (stdm)
Where’s the intelligence?
Man vs. machine

Datalog makes the following assumptions about the world


• An agent's knowledge can be usefully described in terms of
individuals and relations among individuals
• An agent's knowledge base consists of
definite and positive statements
• The environment is static and deterministic
• There are only a finite number of individuals
of interest in the domain
• Each individual can be given a unique name

Under these assumptions, Datalog is a powerful yet fast


system for inference
• Modeling the real world to conform to the assumptions is
up to the developer

Zurich University of Applied Sciences and Arts 58


CAI Centre for Artificial Intelligence (stdm)
Review

• Datalog combines expressive power (about individuals and their relations) with
efficient inference

• Forward and backward chaining are fast & complete for Horn clauses
(Datalog)

• While Datalog might gain popularity in big data applications in the future,
logic in general remains very important for AI

Zurich University of Applied Sciences and Arts 59


CAI Centre for Artificial Intelligence (stdm)
D. PLANNING

Zurich University of Applied Sciences and Arts 60


CAI Centre for Artificial Intelligence (stdm)
How difficult is planning?
Computational complexity of classical planning

Problem definition (see earlier for SAT)


• The PlanSAT problem: Does there exist a plan that achieves the goal?
• The bounded PlanSAT problem: Does there exist a solution of length at most k?
→ useful for optimal (i.e., shortest plan) planning
The PSPACE class contains problems solvable by a
deterministic algorithm with its memory constrained to be
polynomial in the input length
→ larger & more difficult than NP (but no constraint on time)
Complexity
• PlanSAT and bounded PlanSAT are PSPACE-complete
→i.e., difficult (assumed to be not even in NP)!
• PlanSAT without negative preconditions and without negative effects is in P
→ i.e., solvable

Practice
• Sub-optimal planning is sometimes easy
• PDDL has facilitated the development of very accurate domain-independent heuristics
making planning feasible (formalisms based on FOL have had less success)

Zurich University of Applied Sciences and Arts 61


CAI Centre for Artificial Intelligence (stdm)
SATplan and CSP solvers
One option for planning

Translate PDDL description into a SAT problem or a CSP


• Goal state and all actions have to be propositionalized
• Action schemas have to be replaced by a set of ground actions (variables to be replaced
by constants)
• Fluents need to be introduced for each time step
• …
➔ combinatorial explosion

Cost – benefit
• Remove a part of the benefits of the expressiveness of PDDL to…
• …gain access to efficient solution methods of SAT and CSP solvers

Zurich University of Applied Sciences and Arts 62


CAI Centre for Artificial Intelligence (stdm)
Example: blocks world

The blocks world


• A block is either on the table or on another block
• Blocks can be stacked (only if one fits directly on another)
• Goal: produce a given configuration of blocks on the table (specified as which is on top of what)
• Challenge: No explicit quantifiers in PDDL → need to introduce artificial predicates
• Example: 𝑃𝑟𝑒𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛: ¬∃𝑥 𝑂𝑛(𝑥, 𝐴) not directly expressible → introduce predicate 𝐶𝑙𝑒𝑎𝑟(𝐴)

The necessity for 𝐴 having no block on top before becoming movable


Example

➔ A possible solution sequence: [𝑀𝑜𝑣𝑒𝑇𝑜𝑇𝑎𝑏𝑙𝑒 (𝐶, 𝐴), 𝑀𝑜𝑣𝑒(𝐵, 𝑇𝑎𝑏𝑙𝑒, 𝐶), 𝑀𝑜𝑣𝑒(𝐴, 𝑇𝑎𝑏𝑙𝑒, 𝐵)]

Zurich University of Applied Sciences and Arts 63


CAI Centre for Artificial Intelligence (stdm)
Planning in the real world

Use cases
• Spacecraft operation in real time (1998)
NASA’s Deep Space 1 was controlled by a planning & scheduling system devising and
carrying out plans like «During the next week take pictures of the following asteroids
and thrust 90% of the time» (→ see [Nilsson, 2010] ch. 32.2.1)
• Factory scheduling (1985)
4-week (3 shifts a day) production plan at Hitachi for assembly line of 350 products with
35 machines and >2000 different operations (→ see AIMA ch. 11.4.2, HTN on next slide)
• Military operation planning (1990)
A scheduling program helped with the logistics of 1st gulf war and is said to have
«paid back all of DARPAs 30 years of investment in AI in a matter of a few months»
(→ see [Nilsson, 2010] ch. 23.3.3)

Challenges
• Taking resources (incl. time) into account → scheduling
• Being overwhelmed by state space size → hierarchical planning
• Needing to incorporate human wisdom → hierarchical planning
• Coping with uncertainty → conformant / contingency / online planning (analog to AIMA ch. 4)
• Planning with multiple agents → planning with cooperative and adversarial multiagents is unsolved

Zurich University of Applied Sciences and Arts 64


CAI Centre for Artificial Intelligence (stdm)
Automating university timetabling by planning?
A search exercise

Automatic scheduling is a relevant subfield of AI planning.


Likewise, automated timetable generation (often focused on
university teaching timetables) is a vibrant field of study.

• Conduct a quick literature research on automated timetabling


(e.g., https://scholar.google.ch/scholar?q=automated+timetabling)
• What kind of approaches are proposed? How do they relate to
AI planning as you have heard of here?
• With your current understanding of AI – how would you
approach the problem? What are your options?

Zurich University of Applied Sciences and Arts 65


CAI Centre for Artificial Intelligence (stdm)
Hierarchical planning algorithms

HLA’s generally have multiple possible Initial plan: [Act]


REFINEMENTS(): returns set of action
implementations ... two approaches: sequences whose preconditions are satisfied
A. Search for primitive solutions
B. Search for abstract solutions (reason about
HLA’s)

A. Search for primitive solutions

• Algortithm HIERARCHICAL-SEARCH
• Recursively chose an HLA in current plan
• Replace HLA with one of its refinements, until plan
achieves its goal
• Can be implemented as breadth-first search
(alternatives possible)

• Computational advantage: • In practice:


• “Flat” problem with d primitive actions (=depth), b allowable actions at
each state (=branching): O(b^d) • Store once-used implementations of complex
• Hierarchical problem: Assume each HLA has r refinements into k HLA’s, put in library, then re-use for even more
actions at next lower level: n=(d-1)/(k-1) refinement nodes, i.e., r^n
decomposition trees
complex problems
• Small r, large k: huge savings (small number of refinements with long • Ability to generalize from details specific to
action sequence), equiv. to k-th root of non-hierarchical cost problem instance (name of worker in factory etc.)
Zurich University of Applied Sciences and Arts 66
CAI Centre for Artificial Intelligence (stdm)
Hierarchical planning algorithms (cont.)

Problem: HIERARCHICAL-SEARCH refines all HLA’s to primitive action


sequences to determine if plan is workable
➔ Better: Reason about the HLA’s directly

B. Search for abstract solutions

• Example: Determine if the following 2-HLA high level plan


[Drive(Home, SFOLongTermParking), Shuttle(SFOLongTermParking,SFO)]
is a valid implementation of Go(Home,SFO)
• Goal: Derive high-level plan which provably achieves goal (results in exponential
reduction of computational cost), i.e. has at least one valid implementation (downward
refinement property)
• Ansatz: Consider precondition-effect descriptions of HLA’s
• Problem: Describe effects of multiple-implementation-HLA’s

• First try: positive (negative) effects resulting of every (any) implementation


• too restrictive, e.g., if precondition for one implementation is not fulfilled
• equivalent to someone else choosing implementation (i.e. nondeterministic), a.k.a.
“demonic nondeterminisim”
• Better: Agent makes choices (“angelic nondetermininsm”)

Zurich University of Applied Sciences and Arts 67


CAI Centre for Artificial Intelligence (stdm)
Hierarchical planning algorithms (cont.)
Angelic Search
Reachable set for
• Reachable set: set of states reachable by any of HLA ℎ’s implementation, given HLA sequence [h1,h2]
state 𝑠: REACH(s,h)
• More rich/powerful for HLA’s with multiple refinements
• High-level plan achieves goal if reachable set intersects set of goal states
• Algorithm: Find high-level plan whose reachable set intersects goal, then refine

• In practice: HLA’s may have infinitely many implementations → need


approximate descriptions of reachable set: optimistic REACH+(s,h) / Goal set
pessimistic REACH-(s,h)
• If REACH- does intersect goal, plan does work
• If REACH+ does not intersect the goal, plan does not work
• Else, undecidable: need to refine plan (see bottom figure)
Optimistic
• Algorithm “ANGELIC-SEARCH” (details see AIMA, sec. 11.4.3) set REACH+
• Computationally much better than hierarchical search

• Example: Clean your apartment (several rooms, connected by narrow corridors) Pessimistic
• 5 Low-level actions (u,d,l,r,suck) set REACH-
• 2 HLA’s: Navigate, CleanRoom
• Breadth-first: 5𝑑 (𝑑: length of shortest solution)
• Hierarchical search: still exponential (tries all ways consistent with hierarchy)
• Angelic search: approx. linear in number of squares

Zurich University of Applied Sciences and Arts 68


CAI Centre for Artificial Intelligence (stdm)

You might also like