Title: Reasoning
1Reasoning
- Shyh-Kang Jeng
- Department of Electrical Engineering/
- Graduate Institute of Communication Engineering
- National Taiwan University
2References
- J. P. Bigus and J. Bigus, Constructing
Intelligent Agents with Java, Wiley Computer
Publishing, 1998 - S. Russell and P. Norvig, Artificial
Intelligence A Modern Approach, Englewood
Cliffs, NJ Prentice Hall, 1995
3Goal-Based Agents
Sensors
Environment
State
Environment Model
Options
Decision Maker
Goals
Agent
Effectors
4Knowledge-Based Agents
Sensors
Environment
Knowledge Base Management System
Knowledge Base
Agent
Effectors
5Knowledge Base
- A set of representation of facts about the world
- Each individual representation is a sentence
- Sentences are expressed in a knowledge
representation language - Knowledge representation languages are composed
of symbols - Representation and reasoning support the
operation of a knowledge-based agent, accessed
through a knowledge base management system (KBMS)
6General Knowledge-Based Agent (1)
- class KBAgent
- KnowledgeBase kb
- KBMS kbms
- counter t // indicating time
- public KBAgent()
- kb new KnowledgeBase()
- kbms new KBMS( kb )
- t 0
-
7General Knowledge-Based Agent (2)
- public Action run(Percept percept)
- kbms.tell(new
- PerceptSentence(percept, t))
- Action action kbms.ask( new
- ActionQuery(t) )
- kbms.tell( new
- ActionSentence(action,t) )
- t return action
-
8Rule-Based System
- Rule-basde processing system
- Rule base
- Working memory
- Inference engine
- Rules
- Antecedent clauses
- Consequent clauses
- Trigger or ready to fire
- Sensors and effectors
- Retractions
9Vehicles Rule Bases (1)
- Bicycle IF vehicleType cycle
- AND num_wheel 2
- AND motor no
- THEN vehicle Bicycle
- Tricycle IF vehicleType cycle
- AND num_wheel 3
- AND motor no
- THEN vehicle Tricycle
10Vehicles Rule Bases (2)
- Motorcycle IF vehicleType cycle
- AND num_wheel 2
- AND motor yes
- THEN vehicle Motorcycle
- SportsCar IF vehicleType automobile
- AND size small
- AND num_doors 2
- THEN vehicle Sports_Car
11Vehicles Rule Bases (3)
- Sedan IF vehicleType automobile
- AND size medium
- AND num_doors 4
- THEN vehicle Sedan
- MiniVan IF vehicleType automobile
- AND size medium
- AND num_doors 3
- THEN vehicle MiniVan
12Vehicles Rule Bases (4)
- SUV IF vehicleType automobile
- AND size large
- AND num_doors 4
- THEN vehicle Sports_Utility_Vehicle
- Cycle IF num_wheelslt4
- THEN vehicleType cycle
- Automobile IF num_wheels 4
- AND motor yes
- THEN vehicleType automobile
13Forward-Chaining Reasoning
- Load rule base to the inference engine, and any
facts from the knowledge base into the working
memory - Match the rules to obtain the conflict set
- Use the conflict resolution procedure to select a
single rule from the conflict set - Fire the selected rule
- Repeat steps 2,3,4 until the conflict set is empty
14Conflict Resolution Alternatives
- Select the first rule
- Select the rule with the highest specificity or
number of antecedent clauses - Select the rule that refers to the data which has
changed most recently - If the rule has fired on the previous cycle, do
not add it to the conflict set - In case where there is a tie, select a rule
randomly from this subset of the original
conflict set
15A Forward-Chaining Example (1)
- Data in working memory
- num_wheel 4
- motor yes
- num_doors 3
- size medium
- Trigger rule Automobile
- Conflict set rule Automobile
- Fire rule Automobile
16A Forward-Chaining Example (2)
- Data in working memory
- num_wheels 4
- motor yes
- num_doors 3
- size medium
- vehicleType automobile
- Trigger rule MiniVan
- Conflict set rule MiniVan
- Fire rule MiniVan
17A Forward-Chaining Example (3)
- Data in working memory
- num_wheels 4
- motor yes
- num_doors 3
- size medium
- vehicleType automobile
- vehicle MiniVan
- Trigger rule MiniVan
- Not added to the conflict set
- Conflict set is empty
18Forward-Chaining vs. Backward-Chaining Reasoning
- Forward-chaining
- Used to produce new information
- Backward-chaining
- Used to answer questions about whether a goal
clause is true or not - Goal-directed inferencing
- More focused
19Backward-Chaining Reasoning (1)
- Load the rule base into the inference engine, and
any facts from the knowledge base into the
working memory - Specify a goal variable for the inference engine
to find - Find the set of rules which refer to the goal
variable in a consequent clause. Put each rule
on the goal stack - If the goal is empty, halt
- Take the top rule off the goal stack
20Backward-Chaining Reasoning (2)
- 6. For each antecedent clause
- If the clause is true, go on to the next
antecedent clause - If the clause is false, pop the rule off the goal
stack and go to step 4 - If the truth value is unknown, go to step 3, with
the antecedent variable as the new goal variable - If all antecedent clauses are true, fire the
rule, setting the consequent variable to the
consequent value, pop the rule off the goal
stack, and go to step 4
21A Backward-Chaining Example (1)
- Query vehicle MiniVan?
- Working memory empty
- Goal stack (vehicle, rule Minivan)
- Antecedent vehicleType automobile?
- Goal stack (vehicleType, rule Automobile),
- (vehicle, rule MiniVan)
- Antecedent num_wheels 4?
- No rules that have num_wheels 4 as a
consequence
22A Backward-Chaining Example (2)
- Ask the user to obtain that num_wheels 4
- Working memory
- num_wheels 4
- Antecedent motor yes?
- Ask the user to know that motor yes
- Working memory
- num_wheels 4
- motor yes
23A Backward-Chaining Example (3)
- Fire and pop off rule Automobile
- Working memory
- num_wheels 4
- motor yes
- vehicleType automobile
- Goal stack (vehicle, rule MiniVan)
- Antecedent size medium?
24A Backward-Chaining Example (4)
- Ask the user to find size medium
- Working memory
- num_wheels 4
- motor yes
- vehicleType automobile
- size medium
- Antecedent num_doors 3?
- Ask the user to know num_doors 3
- Fire and pop off rule MiniVan
- vehicle MiniVan
25RuleBase Test
26Class Diagram of RuleBase
RuleBase
Rule
RuleVariable
Clause
Condition
27Class RuleBase Fields
- String name
- HashMap variableList
- Clause clauseVarList
- ArrayList ruleList
- ArrayList conclusionVarList
- Rule rulePtr
- Clause clausePtr
- Stack goalClauseStack
28Some Class RuleBase Methods
- public void reset()
- public void forwardChain()
- public ArrayList match()
- // determine which rules can fire
- public Rule selectRule()
- // select a rule to fire based on
- // specificity
- public void backwardChain()
29Method RuleBase.forwardChain()
- public void forwardChain()
- ArrayList conflictRuleSet new
- ArrayList()
- conflictRuleSet match(true)
- while( conflictRuleSet.size() gt 0 )
- Rule selected
- selectRule(conflictRuleSet)
- selected.fire()
- conflictRuleSet match(false)
-
30Method RuleBase.backwardChain() (1)
- public void backwardChain(
- String goalVarName)
- RuleVariable goalVar (RuleVariable)
- variableList.get(goalVarName)
- ListIterator goalClauses
- goalVar.clauseRefs.listIterator()
- while(goalClauses.hasNext())
- Clause goalClause (Clause)
- goalClauses.next()
- if( !goalClause.isConsequent )
- continue
- goalClauseStack.push(goalClause)
31Method RuleBase.backwardChain() (2)
- Rule goalRule goalClause.getRule()
- Boolean ruleTruth goalRule.backChain()
- if( ruleTruth null )
- // can't determine truth value
- else if( ruleTruth.booleanValue()
- true )
- // rule is OK, assign consequent
- // value to variable
- goalVar.setValue(goalClause.rhs)
- goalVar.setRuleName(goalRule.name)
- goalClauseStack.pop()
32Method RuleBase.backwardChain() (3)
- if(goalClauseStack.empty())
- //Found Solution for goal
- break
-
- else
- // clear item from subgoal stack
- goalClauseStack.pop()
-
- if(goalVar.value null)
- //Could Not Find Solution for goal
-
33Class Rule Fields
- RuleBase rb
- String name
- Clause antecedents
- Clause consequent
- Boolean truth
- // states null(unknown),
- // true or false
- boolean fired false
34Some Class Rule Methods
- Boolean check()
- // check if antecedent is true and // rule has
not fired - void fire()
- public static void checkRules
- // a variable value was found, so
- // reset all clauses
- // that reference that variable,
- // and then all rules which
- // reference those clauses
- Boolean backChain()
35Method Rule.BackChain() (1)
- Boolean backChain()
- for(int i0 iltantecedents.length i)
- if(antecedentsi.truth null)
- rb.backwardChain(
- antecedentsi.lhs.name)
- if(antecedentsi.truth null)
- antecedentsi.lhs.askUser()
- truth antecedentsi.check()
-
36Method Rule.BackChain() (2)
- if(antecedentsi.
- truth.booleanValue() true)
- continue
- else
- truth new Boolean(false)
- return truth
-
-
- truth new Boolean(true)
- return truth
-
37Class Clause Fields
- ArrayList ruleRefs
- RuleVariable lhs
- Condition cond
- String rhs
- boolean isConsequent
- Boolean truth
- // states null(unknown), true or // false
38Class Clause Methods
- Boolean check()
- // evaluate the truth value of the
- // clause
- Rule getRule()
- void setFlagConsequent()
- void addRuleRef()
39Class Condition
- Fields
- int index
- String symbol
- // ,gt,lt,!
- Methods
- public String toString()
40Class RuleVariable Fields
- String name
- String value
- ArrayList clauseRefs
- String promptString
- ArrayList labels
41Class RuleVariable Methods
- void updateClauses()
- public String getName()
- public void setValue()
- String askUser()
- void addClauseRef()
- public void setPromptText()
- public ArrayList getLabels()
- public String getLabel()
- public String getLabelsString()
- int getIndex()
42Initializing a RuleBase (1)
- Stack goalClauseStack rb.getGoalClauseStack()
- HashMap variableList rb.getVariableList()
- RuleVariable vehicle new RuleVariable("vehicle")
- vehicle.setLabels("Bicycle Tricycle MotorCycle
Sports_Car Sedan MiniVan " "Sports_Utility_Vehic
le") - vehicle.setPromptText("What kind of vehicle is
it?") - variableList.put( vehicle.getName(), vehicle )
- . . . . . .
43Initializing a RuleBase (2)
- Condition cEquals new Condition("")
- Condition cNotEquals new Condition("!")
- Condition cLessThan new Condition("lt")
- ArrayList ruleList rb.getRuleList()
- Rule bicycle new Rule( rb, "bicycle",
- new Clause(vehicleType, cEquals,
- "cycle"),
- new Clause(num_wheels, cEquals, "2"),
- new Clause(motor, cEquals, "no"),
- new Clause(vehicle, cEquals,
- "Bicycle") )
- . . . . . .
44ForwardChain Demo
- HashMap variableList rb.getVariableList()
- ((RuleVariable) variableList.get("vehicle")).setVa
lue( - null)
- ((RuleVariable) variableList.get("vehicleType")).
- setValue(null)
- . . . . . .
- rb.displayVariables(logsArea)
- rb.forwardChain()
- rb.displayVariables(logsArea)
45BackwardChain Demo
- HashMap variableList rb.getVariableList()
- ((RuleVariable) variableList.get("vehicle")).setVa
lue( - null)
- ((RuleVariable) variableList.get("vehicleType")).
- setValue(null)
- . . . . . .
- rb.displayVariables(logsArea)
- rb.backwardChain("vehicle")
- rb.displayVariables(logsArea)
46Logics
- A logic consists of
- A formal system for describing states of affairs,
consisting of the syntax and the semantics of the
language - A proof theory
- The ontological commitments of a logic have to do
with the nature of reality in the related world - The epistemological commitments of a logic have
to do with states of knowledge an agent can have
47Some Formal Languages
48Propositional Logic Syntax
- Syntax in Backs-Naur Form (BNF)
- Example
-
49Inference Rules (1)
- The soundness of an inference can also be
established through truth tables - Inference rules are some inference patterns that
occur frequently - Inference rules can be used to make inferences
without going through the tedious process of
building truth tables - The inference rule that b can be derived from a
is denoted as
50Inference Rules (2)
- Conjunctions and conjuncts
- Disjunctions and disjuncts
51Inference Rules (3)
- Modus Ponens or Implication-Elimination
- And Elimination
52Inference Rules (4)
- And-Introduction
- Or-Introduction
53Inference Rules (5)
- Double-Negation Elimination
- Unit Resolution
54Inference Rules (6)
- Resolution
- b cannot be both true and false, one of the
other disjuncts must be true in one of the
premises - Or, equivalently, implication is transitive
55First-Order Logic Syntax
56Inference Rules Involving Quantifiers (1)
- Substitution
- Example
- Universal Elimination
57Inference Rules Involving Quantifiers (2)
- Existential Elimination
- Existential Introduction
58An Example Proof (1)
- Situation
- The law says that it is a crime for an American
to sell weapons to hostile nations. The country
Nono, an enemy of America, has some missiles, and
all of its missiles were sold to it by Colonel
West, who is American - To be proved
- West is a criminal
59An Example Proof (2)
- Facts in first-order logic
60An Example Proof (3)
61An Example Proof (4)
62Proof as a Search Process
- Initial state Knowledge base
- Operators applicable inference rules
- Goal test Knowledge base containing the
sentence to be proved - The branching factor increases as the knowledge
base grows - Universal Elimination can have enormous branching
factor on its own
63Generalized Modus Ponens (1)
- And-Introduction, Universal Elimination, and
Modus Ponens can be combined - Example
64Generalized Modus Ponens (2)
- There is a substitution q such that
65Canonical Form
- Horn sentences
- Horn Normal Form
- A knowledge base consisting of only Horn
sentences - An inferencing mechanism with one inference rule,
the generalized Modus Ponens, can be built
66Unification
- A unification routine, UNIFY, takes two atomic
sentences and returns a substitution that would
make both sentences look the same, i.e., - Example
67Standardize Apart
- Rename the variables of one or both sentences,
when two sentences are being unified - Example
68Sample Proof Revisited (1)
69Sample Proof Revisited (2)
70Composition of Substitution
- Composition of substitution is the substitution
whose effect is identical to the effect of
applying each substitution in turn - SUBST(COMPOSE(q1, q2), p)
- SUBST(q2, SUBST(q1, p))
71Algorithm FORWARD-CHAIN(KB, p)
- If there is a sentence in KB that is a renaming
of p then return - Add p to KB
- For each in KB
- such that for some i, UNIFY(pi, p) q
- succeeds do
72Algorithm FIND-AND-INFER(KB, premises,
conclusion, q)
- If premise then
- FORWARD-CHAIN(KB,
- SUBST(q,conclusion))
- else for each p in KB such that
- UNIFY(p,SUBST(q,FIRST(premises)))q2
- do
- FIND-AND-INFER(KB, REST(premises),
- conclusion, COMPOSE(q1, q2))
73Example of Forward-Chaining (1)
74Example of Forward-Chaining (2)
- FORWARD-CHAIN(KB,American(West))
- FORWARD-CHAIN(KB,Nation(Nono))
- FORWARD-CHAIN(KB,Enemy(Nono,America))
- FORWARD-CHAIN(KB,Hostile(Nono))
- FORWARD-CHAIN(KB,Owns(Nono,M1))
- FORWARD-CHAIN(KB,Missile(M1))
- FORWARD-CHAIN(KB,Sells(West, Nono, M1)
- FORWARD-CHAIN(KB,Missile(M1))
- FORWARD-CHAIN(KB,Weapon(M1))
- FORWARD-CHAIN(KB,Criminal(West))
75Algorithm BACK-CHAIN(KB,q)
76Algorithm BACK-CHAIN-LIST(KB, qlist, q) (1)
- answers empty
- If qlist is empty return q
- qFIRST(qlist)
- For each atomic sentence qi in KB such that
- qi UNIFY(q,qi ) succeeds do
- Add COMPOSE(q,qi) to answers
77Algorithm BACK-CHAIN-LIST(KB, qlist, q) (2)
- 5. For each sentence
- in KB such that qi UNIFY(q,qi) succeeds do
- Return the union of
78Proof Tree
Criminal(x)
Sells(West,Nono,M1)
American(x)
Yes, x/West
Weapon(y)
Hostile(Nono)
Owns(Nono,M1)
Missile(y)
Yes,
Yes, y/M1
Missile(M1)
Nation(z)
Enemy(Nono,America)
Yes,
Yes, z/Nono
Yes,
79Incompleteness of Modus Ponens
- A proof procedure using Modus Ponens is
incomplete - Example We can not derive S(A) using chaining of
Modus Ponens from the following knowledge base
80Gödels Completeness Theorem
- For first-order logic, any sentence that is
entailed by another set of sentences can be
proved from that set - In other words, a complete proof procedure can be
generated to prove a sentence entailed by another
set of sentences - A such procedure is the resolution algorithm
proposed by Robinson
81Resolution
- Resolution
- b cannot be both true and false, one of the
other disjuncts must be true in one of the
premises - Or, equivalently, implication is transitive
82The Resolution Inference Rule
- Generalized resolution (disjunctions)
- Generalized resolution (implications)
83Canonical Forms for Resolution
- Conjunctive Normal Form
- Example
- Implicative Normal Form
- Example
84Resolution Proofs Using Forward-Chaining Algorithm
y/w
w/x
x/A,z/A
85Resolution with Refutation
- Chaining with resolution is still not complete
- Example For an empty KB, we can do nothing with
- Proof by contradiction (reduction ad absurdum)
- Example
- To prove P, assume that P is false, and prove a
contradiction
86Proofs Using Resolution with Refutation
87Conversion to Normal Form (1)
- Any first-order logic sentence can be put into
implicative (or conjunctive) normal form - Conversion procedure
- 1. Eliminate implications
- 2. Move negation inwards by de Morgans law
88Conversion to Normal Form (2)
- Conversion procedure (continue)
- 3. Standardize variables
- 4. Move quantifiers left
- 5. Skolemize
89Conversion to Normal Form (3)
- Conversion procedure (continue)
- 6. Distribute AND over OR
- 7. Flatten nested conjunctions and disjunctions
- 8. Convert disjunctions to implications
90Dealing with Equality