850 likes | 996 Views
KM: The Knowledge Machine A knowledge representation and reasoning system. What is KM?. A Knowledge Representation Language (KRL) (like KIF, or CycL, or …) an interpreter sitting on top of Lisp expressive formal semantics A Reasoning Engine backward chaining automatic classification
E N D
KM: The Knowledge MachineA knowledge representation and reasoning system
What is KM? • A Knowledge Representation Language (KRL) • (like KIF, or CycL, or …) • an interpreter sitting on top of Lisp • expressive • formal semantics • A Reasoning Engine • backward chaining • automatic classification • reasoning with actions (simulation) • defaults (inheritance with overrides) • Mature • Free
What Do You Do with KM? • Create Knowledge Bases • descriptions of things in the world • objects, events, properties, relationships • descriptions of the rules that allow us to reason about things in the world • Ask Questions about Knowledge • what are the facts? • what can be inferred from the facts? • what can be inferred from the inferences from facts? • Run Simulations in the World Described in the KB • what would be the state of objects if certain events took place?
Representing Knowledge • What are the kinds of things in the world? • Event, Object, Artifact, Device, Computer, … • What are the real individual things of each kind? • PartyAtMyHouseThisSaturday, ThePCInMyOffice • What properties hold for all individuals of some kind? • all events occur at some place and time • most computers have hard drives • What properties hold for specific individuals? • this lecture is at 3:30pm, your favorite color is green • How do kinds of things relate to each other? • all Computers are Devices, all People have a Mother • How do individuals relate to each other? • Thomas’ father is Bob, Texas is part of the USA
An Informal Knowledge Base • Classes • Event, Object, Artifact, Device, Computer, Person, Place, Hard-Drive are Classes • every Evente occurs at a time-of-occt and place-of-occp • The superclass of Computer is Device • every Personp1 has a motherp2 • most Computersc have a parth that is a Hard-Drive • Instances • PartyAtMyHouseThisSaturday is an instance of Event • ThePCInMyOffice is an instance of Computer • ThisLecture is an instance of Event with time-of-occ3:30pm • You is an instance of Person with favorite-colorgreen • Thomas is an instance of Person with father Bob (a Person) • Texas is an instance of Place with is-part-of USA (a Place)
KM for Representing Knowledge • English • ThePCInMyOffice is an instance of Computer • the superclass of Computer is Device • Is ThePCInMyOffice a Device? • FOPC • Computer(ThePCInMyOffice) • x Computer(x) Device(x) • Device(ThePCInMyOffice) • KM • (ThePCInMyOffice has (instance-of (Computer))) • (Computer has (superclasses (Device))) • (ThePCInMyOffice isa Device)(t)
This Tutorial • Overview of the KM language and inference • based on a detailed script of using KM • the examples are “toy” • Not a tutorial in how to do knowledge representation! • KM Manuals give the full reference* • how to download and run KM • details of the syntax • logical semantics for the expressions • Tutorial works best if you ask lots of questions!! * http://www.cs.utexas.edu/users/mfkb/km.html
Interaction with KM • Interaction with KM: read-eval-print • User gives KM an expression (assertion/query) • KM • “reads” the expression given by the user • evaluates it and • prints the result KM> 123 (123) KM> (1 + 1) (2) KM> (the count of MonteCristo) (Edmond) KM> _
The ‘*’ has no special meaning in KM; We use it as a handy visual indicator to distinguish instances and classes Expressions in KM • The basic expressions in KM are • assertions • facts and rules about the world • queries • expressions whose evaluation searches the facts and rules KM> (*Shiner has (instance-of (Cat))) (*Shiner) KM> (*Shiner has (brother (*Kashmir))) (*Shiner) KM> (the brother of *Shiner) (*Kashmir) KM> (the instance-of of *Kashmir) (Cat) How does it know?
Knowledge Bases in KM • KM expressions can be loaded from a file into KM • More KM expressions can be evaluated by the interpreter, possibly modifying the loaded KB • The modified KB can be saved from the KM environment into a file KM> (load-kb "a-family.km") Loading a-family.km... a-family.km loaded! KM> (the father of *Thomas) (*Bob) KM> (the mother of *Thomas) (*BettyJo) KM> (*Bryen has (father (*Thomas))) (*Bryen) KM> (the grandmother of *Bryen) (*BettyJo) KM> (save-kb "a-family2.km") a-family2.km saved!
‘;’ = comment to end of line Instances • Recall: • an instance denotes some object in the world • a class is a collection of instances1 • An instance (individual) evaluates to itself ;;; --- instances --- ;;; "Fred" KM> *Fred (*Fred) ;;; "1" KM> 1 (1) ;;; "1 + 1" KM> (1 + 1) (2) 1 or, more precisely, a class has a collection of instances associated with it (the class’s extension)
Guaranteed uniqueand fresh! An instance together with all the knowledge attached to that instance is called a “Frame” “Creating instances”(existential quantification) • We can “create” (assert the existence of) an instance of a particular class • Logic: x.<classname>(x) (e.g. x.Cat(x)) • KM: (a <classname>) ;;; "A cat." KM> (a Cat) (_Cat0) ; _Cat0 is a Skolem individual denoting that cat ;;; "A black cat with a head and a tail." KM> (a Cat with (color (*Black)) (parts ((a Head) (a Tail))) ) (_Cat1) KM> (showme _Cat1) (_Cat1 has (instance-of (Cat)) (color (*Black)) (parts ((a Head) (a Tail)))) (_Cat1)
Frame Slot Value Slots • Slots relate instances together • Two ways to specify slots: • on existing instances • (<instance> has (<slot> (<value>))) • directly on new instances when created • (a <classname> with (<slot> (<value>))) KM> (a Car) (_Car3) KM> (_Car3 has (color (*Silver))) (_Car3) KM> (showme _Car3) (_Car3 has (instance-of (Car)) (color (*Silver))) KM> (a Car with (color (*Silver))) (_Car4) KM> (showme _Car4) (_Car4 has (instance-of (Car)) (color (*Silver)))
Frame Value Slot Slots • Slots can relate instances together • Slots are themselves frames • can specify properties of the slot on these frames ;;; --- slot declarations (optional) --- ;;; "'age' is a slot relating physical objects (Physobj) to Numbers. ;;; A physical object has at most one age." KM> (age has (instance-of (Slot)) (domain (Physobj)) (range (Number)) (inverse (is-age-of)) ; name of the inverse slot (cardinality (N-to-1))) ; one age per thing
Superclasses • Slots can also give information about classes • The syntax is the same as for instances • (<class> has (<slot> (<value>))) KM> (Car has (superclasses (Vehicle)) (subclasses (Hybrid-Car Station-Wagon Sports-Car)) (instances (_Car3 _Car4))) (Car) KM> (the subclasses of Vehicle) (Car)
Thing Physobj Number Car Person …. The Taxonomy (Inheritance Hierarchy) • Classes can themselves be subclasses of (multiple) other classes • (taxonomy) shows the entire taxonomy KM> (*Fred has (instance-of (Person))) KM> (Person has (superclasses (Physobj))) KM> (Physobj has (superclasses (Thing))) ;;; "Show me the concept taxonomy." KM> (taxonomy) Thing Number Physobj Car Cat House Person I *Fred I *Joe …. etc…..
Frame (identifier) Slot Value Instance Frames and Simple Queries • Frames describe objects in the world • list its properties, and relationships to other objects • Queries: Form is (the <slot> of <object>) – an “access path” ;;; "Fred is a person." KM> (*Fred has (instance-of (Person))) ;;; "Fred's age is 32." KM> (*Fred has (age (32))) ;;; "Show me the frame representing Fred." KM> (showme *Fred) (*Fred has (instance-of (Person)) (age (32))) ;;; "What is the age of Fred?" KM> (the age of *Fred) (32)
Slot Inverses • KM keeps track of inverse relations alsoX→r→Y Y→r-1→X ;;; "Fred owns a car." KM> (*Fred has (owns ((a Car)))) ;;; "What does Fred own?" KM> (the owns of *Fred) (_Car1) ; (Skolem _Car1 created, denoting that car) ;;; "Show me the frame representing that last car." KM> (showme (thelast Car)) (_Car1 has (instance-of (Car)) (owns-of (*Fred))) ; note the inverse slot (default name is "<slot>-of")
evaluates to a Frame KM> (the owns of *Joe) ( ) KM> (the color of ) (*Red) Embedded Units and Access Paths • Frames can be embedded within frames • An access path may be embedded in an access path ;;; --- embedded units --- ;;; "Joe is a person, and owns a red car." KM> (*Joe has (instance-of (Person)) (owns ((a Car with (color (*Red)))))) ;;; --- access paths --- ;;; "What are the color(s) of the thing(s) which Joe owns?" KM> (the color of (the owns of *Joe)) (*Red)
Embedded Units and Access Paths (cont) • An access path will select all values on a slot. • (the <slot> of <instance>) • To select a subset • give a class name in the query • (the <class> <slot> of <instance>) • add a filter (“generate and test”), discussed later • (allof (the <slot> of <instance>) where <filter-condition>) ;;; "Joe is a person, and owns a car and a teddy bear." KM> (*Joe has (instance-of (Person)) (owns ((a Car) (a Teddy-Bear)))) ;;; "What does Joe own?" KM> (the owns of *Joe) (_Car1 _Teddy-Bear2) ;;; "What vehicles does Joe own?" KM> (the Vehicle owns of *Joe)) (_Car1)
Class Frames and Inheritance • Can state properties for all members of a class • (every <classname> has (<slot> (<value>))) • Each instance of that class acquires (inherits) those properties ;;; "People are physical objects" (property of the class) KM> (Person has (superclasses (Physobj))) ;;; "Every person lives in a house." (property of class members) KM> (every Person has (lives-in ((a House)))) ;;; "Every house has a door and a roof." KM> (every House has (parts ((a Door) (a Roof)))) ;;; "What does Joe live in?" KM> (the lives-in of *Joe) (_House3) ;;; "What are the parts of the thing which Joe lives in?" KM> (the parts of (the lives-in of *Joe)) (_Door4 _Roof5)
Self • Within a class frame, Self refers to the instance inheriting the information. ;;; "Every person likes him/herself" KM> (every Person has (likes (Self))) (Person) KM> (the likes of *Fred) (*Fred) KM> (the likes of *Joe) (*Joe) ;;; "Every person likes their favorite color(s)." KM> (every Person has (likes ((the favorite-color of Self)))) (Person) ;;; "Fred's favorite color is blue." KM> (*Fred has (instance-of (Person)) (favorite-color (*Blue))) (*Fred) KM> (the likes of *Fred) (*Fred *Blue) KM> (the Color likes of *Fred)(*Blue)
Exercises ;; "All professors have (at least) one car which is old, is ;; their favorite color, and was made in Sweden." (Professor has (superclasses (Person))) (every Professor has (owns ( (a Car with (age (*Old)) (color ((the favorite-color of Self))) (made-by ((a Manufacturer with (location (*Sweden))))))))) 1. Write the KM queries to find: - the vehicle that a professor owns - the age of a professor’s vehicle - the location of the manufacturer of the professor’s vehicle 2. Create a KM frame describing the concept of a Car (the Car class): (every Car has ...)
Answers to Exercises 1a. (the Car owns of (a Professor)) 1b. (the age of (the Car owns of (a Professor))) 1c. (the location of (the made-by of (the Car owns of (a Professor)))) 2. For example... (every Car has (color ((a Color))) (age ((a Age-Value))) (made-by ((a Manufacturer))) (parts ((a Chassis) (a Engine) (a Fuel-Tank with (supplies ((the Engine parts of Self)))))))
Exercise (Electrophoresis has (superclasses (Process))) (every Electrophoresis has (sample ((a Chemical))) (equipment ((a Separation-unit) (a Syringe))) (subevents ( (a Remove with (object ((the sample of Self))) (location ((the delivery-medium of (the sample of Self))))) (a Insert with (object ((the sample of Self))) (destination ((the Separation-unit equipment of Self))) (equipment ((the Syringe equipment of Self))))))) (Albumin has (superclasses (Chemical))) (every Albumin has (delivery-medium ((a Bottle))) (storage-medium ((a Fridge)))) 3. What is the result of the query: KM> (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin))))))
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin))))))
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) KM> (a Electrophoresis with (sample ((a Albumin)))) (_Electrophoresis5)
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of(the Remove subevents of _Electrophoresis5))
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of(the Remove subevents of _Electrophoresis5)) KM> (showme _Electrophresis5) (_Electrophoresis5 has (instance-of (Electrophoresis)) (sample ((a Albumin))) (subevents ((a Remove) (a Insert))))
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of (the Remove subevents of _Electrophoresis5)) (the location of _Remove6)
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of (the Remove subevents of _Electrophoresis5)) (the location of _Remove6) KM> (showme _Remove6) (_Remove6 has (instance-of (Remove)) (object ((the sample of _Electrophoresis5))) (location ((the delivery-medium of (the sample of _Electrophoresis5)))) (subevents-of (_Electrophoresis5)))
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of (the Remove subevents of _Electrophoresis5)) (the location of _Remove6) (the delivery-medium of (the sample of _Electrophoresis5))
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of (the Remove subevents of _Electrophoresis5)) (the location of _Remove6) (the delivery-medium of (the sample of _Electrophoresis5)) KM> (showme _Electrophresis5) (_Electrophoresis5 has (instance-of (Electrophoresis)) (sample ((a Albumin))) (subevents (_Remove6 (a Insert))))
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of (the Remove subevents of _Electrophoresis5)) (the location of _Remove6) (the delivery-medium of (the sample of _Electrophoresis5)) (the delivery-medium of _Albumin7)
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of (the Remove subevents of _Electrophoresis5)) (the location of _Remove6) (the delivery-medium of (the sample of _Electrophoresis5)) (the delivery-medium of _Albumin7) KM> (showme _Alubmin7) (_Albumin7 has (instance-of (Albumin)) (sample-of (_Electrophoresis5)) (delivery-medium ((a Bottle))) (storage-medium ((a Fridge))))
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of (the Remove subevents of _Electrophoresis5)) (the location of _Remove6) (the delivery-medium of (the sample of _Electrophoresis5)) (the delivery-medium of _Albumin7) (a Bottle)
Answers to Exercises 3. _Bottle8 (i.e., a Skolem instance of Bottle). (the location of (the Remove subevents of (a Electrophoresis with (sample ((a Albumin)))))) (the location of (the Remove subevents of _Electrophoresis5)) (the location of _Remove6) (the delivery-medium of (the sample of _Electrophoresis5)) (the delivery-medium of _Albumin7) (a Bottle) _Bottle8
mind the gap x = y if [x equals y +/- (0.0001 or 0.01% of max(x,y), whichever is smaller (so 1.0001 ≠ 1.0002))] Simple Data Types KM> (3.1 isa Number) (t) KM> (3 isa Integer)(t) KM> (3.1 isa Integer) NIL KM> (t = (not NIL))(t) KM> (f = NIL)NIL KM> ("apocope" isa String)(t) KM> (99 = 100 +/- 2)(t) KM> (99 = 100 +/- 1 %)(t) KM> (4.99999 = 5.00000)(t) • KM has numbers, integers, strings, booleans
Why? Rules • The expression for a slot-value may be a rule • The rule is evaluated when the slot-value is queried • The tests and result of the rule are themselves KM expressions ;;; "A person is a voter if he/she is older than 18." KM> (every Person has (is-voter ((if ((the age of Self) >= 18) then *Yes else (if ((the age of Self) < 18) then *No))))) ;;; "Is Fred a voter?" [Fred is 32, asserted earlier] KM> (the is-voter of *Fred) (*Yes) ;;; "Joe is 12 years old." KM> (*Joe has (instance-of (Person)) (age (12))) (*Joe) ;;; "Is Joe a voter?" KM> (the is-voter of *Joe) (*No) ;;; "Is some generic person a voter?" KM> (the is-voter of (a Person))NIL
Exercise 4. Complete the expression on the “brightness” slot for the concept of a Light, so that the result depends on the light’s switch position. (every Light has (part ((a Switch with (position ((a PositionValue))))) (brightness (...))) (*Up has (instance-of (PositionValue))) (*Down has (instance-of (PositionValue))) (*Bright has (instance-of (BrightnessValue))) (*Dark has (instance-of (BrightnessValue))))
Answers to Exercises 4. (every Light has (part ((a Switch with (position ((a PositionValue)))))) (brightness ( (if ((the position of (the Switch part of Self)) = *Up) then *Bright else (if ((the position of (the Switch part of Self)) = *Down) then *Dark))))) Note that if the position is unknown, then the brightness will be unknown also (which is what we want).
Complex Types ;;; "Jason has two favorite colors" KM> (*Jason has (favorite-color (*Blue *Green))) (*Jason) KM> (the favorite-color of *Jason)(*Blue *Green) ;;; "So does Mike"KM> (*Mike has (favorite-color ((:set *Blue *Green)))) (*Mike) KM> (the favorite-color of *Mike)(*Blue *Green) ;;; "Mike, John and Jason finished 1st, 2nd and 3rd"KM> (a Tournament with (finishers ((:seq *Mike *John *Jason))))(_Tournament6) ;;; "Mike had scores of 66, 67, 73 and 67KM> (*Mike has (round-score ((:bag 66 67 73 67))))(*Mike) • KM also has sets, sequences, bags
Arithmetic • KM has infix operators +, -, *, /, ^ • Also has arithmetic slots: (the sum of ….), etc. • sum, difference, product, quotient, and others ;;; "What is 1 + 1?" KM> (1 + 1) (2) ;;; "A person's age in days is their age [in years] times 365." KM> (every Person has (age-in-days (((the age of Self) * 365)))) (Person) ;;; "What is Fred's age in days?" KM> (the age-in-days of *Fred) (11680) ;;; "What was Mike's total score last week?“ KM> (*Mike has (round-score ((:bag 66 67 73 67))))(Mike) KM> (the sum of (the round-score of *Mike))(273)
Set Expressions • Procedurally, KM iterates over a set of values • Two main iterators • (allof <set> where <filter-condition>) • (forall <set> where <filter-condition> <expression>) • Keyword It denotes the value in each iteration ;;; "Show me every person [in the KB so far]?" KM> (every Person) (*Joe *Fred) ;;; or equivalently KM> (the all-instances of Person) (*Joe *Fred) ;;; "Which people are over 18?" KM> (allof (the all-instances of Person) where ((the age of It) > 18)) (*Fred) ;;; "What is the favorite color(s) of the people over 18?" KM> (forall (the all-instances of Person) where ((the age of It) > 18) (the favorite-color of It)) (*Blue)
Aside: Tracing in KM KM> (*Lily has (instance-of (Person)) (favorite-color (*Lilac)) (age (21)))(*Lily) KM> (trace)(Tracing of KM switched on) KM> (forall (the all-instances of Person) where ((the age of It) > 18) (the favorite-color of It)) 1 -> (forall (the all-instances of Person) where ((the age of It) > 18) (the favorite-color of It)) 2 -> (the all-instances of Person) 2 <- (*Joe *Fred *Lily) [(the all-instances of Person)] 2 -> ((the age of *Joe) > 18) 3 -> (the age of *Joe) 3 <- (12) [(the age of *Joe)] 2 <- FAIL! [((the age of *Joe) > 18)] 2 -> ((the age of *Fred) > 18) 3 -> (the age of *Fred) 3 <- (32) [(the age of *Fred)] 2 <- (t) [((the age of *Fred) > 18)] 2 -> (the favorite-color of *Fred) 2 <- (*Blue) [(the favorite-color of *Fred)] 2 -> ((the age of *Lily) > 18) 3 -> (the age of *Lily) 3 <- (21) [(the age of *Lily)] 2 <- (t) [((the age of *Lily) > 18)] 2 -> (the favorite-color of *Lily) 2 <- (*Lilac) [(the favorite-color of *Lily)] 1 <- (*Blue *Lilac) [(forall ... (*Blue *Lilac) KM> (untrace)(Tracing of KM switched off)
What’s Wrong with Set Expressions? • Two main iterators • (allof <set> where <filter-condition>) • (forall <set> where <filter-condition> <expression>) • Keyword It denotes the value in each iteration • Hint: • if <expression> can be any KM expression, could it be an iterator? • could <set> and <filter-condition> contain iterators?
Set Expressions with Variables • A slightly more general form allows for nestediterators • (allof <var> in <set> where <filter-condition>) • (forall <var> in <set> where <filter-condition> <expression>) ;;; "Which people are over 18?" KM> (allof ?x in (the all-instances of Person) where ((the age of ?x) > 18)) (*Fred *Lily) ;;; "What is the favorite color(s) of the people over 18?" KM> (forall ?y in (the all-instances of Person) where ((the age of ?y) > 18) (the favorite-color of ?y)) (*Blue *Lilac) ;;; "What things do people over 18 own that are their favorite colors?" KM> (forall ?p in (the all-instances of Person) where ((the age of ?p) > 18) (allof ?t in (the owns of ?p) where ((the color of ?t) = (the favorite-color of ?p)) ) ) (_Car11 _House13)
Unification ;;; "Johanne likes some cute puppy"KM> (*Johanne has (likes ((a Puppy with (trait (*cute)))))) (*Johanne) ;;; "What puppy does Johanne like?“ KM> (the likes of *Johanne) (_Puppy25) ;;; "Victor is a puppy" KM> (*Victor has (instance-of (Puppy)) (color (*brown))) (*Victor) • Is it possible that _Puppy25 and *Victor are the same puppy? • What would we then know about *Victor?
Unification • The assertion of equality of two objects • The merging of data structures associated with instances (_Person3 has (parts ((a Brain))) (name ("Albert"))) ) (_Student4 has (parts ((a Body))) (gpa (9.6))) ) (_Student4 has (parts ((a Brain) (a Body))) (name ("Albert"))) (gpa (9.6)) )
Vertical Unification (Inheritance) • Inheritance is a kind of unification • An instance of a class is the unification of anonymous instances of all its ancestor classes (every Living-Thing has (parts ((a Body))) ) _Living-Thing832 superclasses (every Person has (parts ((a Brain))) ) _Person834 superclasses (every Student has (gpa ((a Number))) ) _Student836 isa (_Student4 has (parts ((a Brain) (a Body))) (gpa ((a Number))) ) _Student4 = _Living-Thing832 & _Person834 & _Student836 & _Student4