;;;; -*- Mode: LISP; Syntax: Common-Lisp; Base: 10 -*- ;;;; --------------------------------------------------------------------------- ;;;; File name: qr-bote-examples.lsp ;;;; System: FIRE/BotE ;;;; Author: Praveen Paritosh ;;;; Created: April 16, 2004 13:31:39 ;;;; Purpose: Examples for BotE via solve ;;;; --------------------------------------------------------------------------- ;;;; Modified: Sunday, May 2, 2004 at 19:56:02 by paritosh ;;;; --------------------------------------------------------------------------- (in-package :common-lisp-user) ;; How many popcorns would fill in the 1890 Maple big classroom? ;; To Run the example ;; (fire::setup-solve-test-case *popcorn-suggestions* *popcorn-facts* :KB) ;; (multiple-value-setq (ans tree) (fire::solve *popcorn-query* :response :bindings)) ;; For more solutions ;; (fire::get-solution tree) ;; Query (defparameter *popcorn-query* '(CountContained CS381ClassRoom Popcorn ?number)) (defparameter *popcorn-suggestions* '((defSuggestion VolumeStrategyForCount (CountContained ?container ?contained ?count) :subgoals ((volumeOfObject ?container ?v1) (volumeOfObject ?contained ?v2)) :result-step (evaluate ?count (QuotientFn ?v1 ?v2))) (defSuggestion VolumeFormulaForSphere (volumeOfObject ?obj ?vol) :test (shapeOfObject ?obj SphereShape) :subgoals ((radius ?obj ?radius)) :result-step (evaluate ?vol (TimesFn 4.187 ;;4/3*Pi (ExponentFn ?radius 3)))) (defSuggestion VolumeFormulaForRectangular3DShape (volumeOfObject ?obj ?vol) :test (shapeOfObject ?obj Rectangular3DShape) :subgoals ((heightOfObject ?obj ?h) (lengthOfObject ?obj ?l) (widthOfObject ?obj ?w)) :result-step (evaluate ?vol (TimesFn ?h ?l ?w))))) ;; Knowledge that must be around (defparameter *popcorn-facts* '((isa CS381ClassRoom ClassRoom) (shapeOfObject CS381ClassRoom Rectangular3DShape) (shapeOfObject Popcorn SphereShape) (heightOfObject CS381ClassRoom 3) (lengthOfObject CS381ClassRoom 15) (widthOfObject CS381ClassRoom 10) (radius Popcorn 0.02))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; How much money is spent on Newspapers in the US? ;; (annualSales NewspaperCopy UnitedStatesOfAmerica (YearFn 2003) ?money) ;; To Run the example ;; (fire::setup-solve-test-case *news-suggestions* *news-facts* :KB) ;; (multiple-value-setq (ans tree) (fire::solve *news-query* :response :bindings)) ;; For more solutions ;; (fire::get-solution tree) (defparameter *news-query* '(annualSales NewspaperCopy UnitedStatesOfAmerica (YearFn 2003) ?money)) (defparameter *news-suggestions* '((defSuggestion PerBuyerStrategy (annualSales ?obj ?place ?time ?money) :subgoals ((annualExpensePerBuyer ?obj ?place ?time ?money-per-buyer) (numberOfBuyers ?obj ?place ?time ?number)) :result-step (evaluate ?money (TimesFn ?money-per-buyer ?number))) (defSuggestion UnitaryStrategyForCost (annualExpensePerBuyer ?obj ?place ?time ?money-per-buyer) :subgoals ((annualUnitsBoughtPerBuyer ?obj ?place ?time ?units) (cost ?obj ?unit-cost)) :result-step (evaluate ?money-per-buyer (TimesFn ?units ?unit-cost))) ;;; (defSuggestion FrequencyStrategy ;;; (annualUnitsBoughtPerBuyer ?obj ?place ?time ?units) ;;; :subgoals (( (defSuggestion FractionOfTotalStrategy (numberOfBuyers ?obj ?place ?time ?number) :subgoals ((populationDuring ?place ?any-time ?total) ;; This is bad, it should be more clever than this (percentOfBuyers ?obj ?place ?percent)) :result-step (evaluate ?number (QuotientFn (TimesFn ?total ?percent) 100))) )) (defparameter *news-facts* '((cost NewspaperCopy 0.75) (percentOfBuyers NewspaperCopy UnitedStatesOfAmerica 30) (annualUnitsBoughtPerBuyer NewspaperCopy UnitedStatesOfAmerica (YearFn 2003) 365))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; How Many K-8 Teachers are there in the US? ;; (cardinality K-8SchoolTeacher ?numteachers) ;; (fire::setup-solve-test-case *teacher-suggestions* *teacher-facts* :KB) ;; (multiple-value-setq (ans tree) (fire::solve *teacher-query* :response :bindings)) ;; For more solutions ;; (fire::get-solution tree) (defparameter *teacher-query* '(cardinality K-8SchoolTeacher ?numteachers)) (defparameter *teacher-suggestions* '((defSuggestion StudentsPerTeacherStrategy (cardinality K-8SchoolTeacher ?numteachers) :subgoals ((cardinality K-8SchoolStudent ?numstudents) (studentsPerTeacher K-8School ?perteacher)) :result-step (evaluate ?numteachers (QuotientFn ?numstudents ?perteacher))) (defSuggestion UniformAgeDistributionStrategy (cardinality K-8SchoolStudent ?numstudents) :subgoals ((populationDuring UnitedStatesOfAmerica (YearFn 1997) ?population) (minimumAge K-8SchoolStudent ?min) (maximumAge K-8SchoolStudent ?max) (lifeExpectancyForGroupInRegion UnitedStatesOfAmerica Person (YearsDuration ?life))) :result-step (evaluate ?numstudents (TimesFn (QuotientFn (DifferenceFn ?max ?min) ?life) ?population))) )) (defparameter *teacher-facts* '((minimumAge K-8SchoolStudent 5) (maximumAge K-8SchoolStudent 14) (studentsPerTeacher K-8School 30))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; What is the annual cost of healthcare in the US? ;; (fire::setup-solve-test-case *health-suggestions* *health-facts* :KB) ;; (multiple-value-setq (ans tree) (fire::solve *health-query* :response :bindings)) ;; For more solutions ;; (fire::get-solution tree) (defparameter *health-query* '(annualSales HealthCare UnitedStatesOfAmerica (YearFn 1997) ?money)) (defparameter *health-suggestions* '(;; This one gets reused. (defSuggestion PerBuyerStrategy (annualSales ?obj ?place ?time ?money) :subgoals ((annualExpensePerBuyer ?obj ?place ?time ?money-per-buyer) (numberOfBuyers ?obj ?place ?time ?number)) :result-step (evaluate ?money (TimesFn ?money-per-buyer ?number))) ;; annual expense per buyer on a specific object. If we have discrete ;; units of that object that are bought separately, then it is the product ;; of number of units by cost per unit. on the other hand if it is something ;; that is paid for monthly or in installments, we want to add that all up ;; and find the annualExpense. For example, rent. In case of insurance, ;; its more sensible to assume we know the annual insurance premium ;; so we dont have to do much here. This example demonstrates the ;; issues in generality of suggestions. Suggestions have unique names, ;; so this one has to be named differently from UnitaryStrategyForCost (defSuggestion ExpenseOnHealthStrategy (annualExpensePerBuyer HealthCare ?place ?time ?money-per-buyer) :subgoals ((healthInsurancePremiumForGroupInRegion ?place Person (DollarsPerYear ?money-per-buyer))) :result-step (healthInsurancePremiumForGroupInRegion ?place Person (DollarsPerYear ?money-per-buyer))) (defSuggestion FractionOfTotalStrategy (numberOfBuyers ?obj ?place ?time ?number) :subgoals ((populationDuring ?place ?time ?total) ;; This is bad, it should be more clever than this (percentOfBuyers ?obj ?place ?percent)) :result-step (evaluate ?number (QuotientFn (TimesFn ?total ?percent) 100))))) (defparameter *health-facts* '((healthInsurancePremiumForGroupInRegion UnitedStatesOfAmerica Person (DollarsPerYear 3000)) (percentOfBuyers HealthCare UnitedStatesOfAmerica 100))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; How many cars are bought per year in the US? ;; (fire::setup-solve-test-case *cars-suggestions* *cars-facts* :KB) ;; (multiple-value-setq (ans tree) (fire::solve *cars-query* :response :bindings)) ;; For more solutions ;; (fire::get-solution tree) (defparameter *cars-query* '(unitsBoughtPerYear Automobile UnitedStatesOfAmerica (YearFn 1997) ?num)) (defparameter *cars-suggestions* ;; This strategy works like this. There is an OBJ that people possess, and to find ;; out how many of these are being bought per year, assuming mostly repeat ;; buyers, we can say they buy it once the old one is gone bad. So we divide the ;; total units of product already there, by the average lifetime of the product. '((defSuggestion AgeStrategyForUnitsBought (unitsBoughtPerYear ?obj ?place ?time ?units-per-year) :subgoals ((unitsTotal ?obj ?place ?time ?total-units) (age ?obj (YearsDuration ?life))) :result-step (evaluate ?units-per-year (QuotientFn ?total-units ?life))) ;; This strategy works for products that are owned by households usually. So ;; if we can find the total number of households, and multiply that by the ;; average number of units owned by each household, we get the total units. ;; Weirdness in cyc knowledge: ;; A notable specialization of CohabitationUnit is #$FamilyCohabitationUnit. ;; This is closest the idea of households but not yet defined in the KB. (defSuggestion HouseholdStrategyForCountingUnits (unitsTotal ?obj ?place ?time ?total-units) :subgoals ((numberOfHouseholds ?place ?time ?num-households) (unitsPerHousehold ?obj ?units-per-household)) :result-step (evaluate ?total-units (TimesFn ?num-households ?units-per-household))) (defSuggestion HouseholdViaPopulationStrategy (numberOfHouseholds ?place ?time ?num-households) :subgoals ((populationDuring UnitedStatesOfAmerica (YearFn 1997) ?population) (sizeOf CohabitationUnit UnitedStatesOfAmerica ?size)) :result-step (evaluate ?num-households (QuotientFn ?population ?size))))) (defparameter *cars-facts* '((sizeOf CohabitationUnit UnitedStatesOfAmerica 3) ;; Another example where we need feel for numbers -- in the KB it says ;; (YearsDuration 5 50). Now whats a typical value. We dont want to do ;; interval math with these things. (age Automobile (YearsDuration 10)) (unitsPerHousehold Automobile 1))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; What is the weight of solid garbage thrown away by American ;; families each year? ;; (fire::setup-solve-test-case *garbage-suggestions* *garbage-facts* :KB) ;; (multiple-value-setq (ans tree) (fire::solve *garbage-query* :response :bindings)) ;; For more solutions ;; (fire::get-solution tree) (defparameter *garbage-query* '(annualProduction Garbage-Generic UnitedStatesOfAmerica (YearFn 1997) ?garbage-mass)) (defparameter *garbage-suggestions* '((defSuggestion HouseholdGarbageStrategy (annualProduction Garbage-Generic ?region ?time ?total-garbage-mass) :subgoals ((numberOfHouseholds ?region ?time ?num-households) (amountProducedInEvent GarbageDisposalEvent Garbage-Generic (Pound-UnitOfMass ?garbage-per-house))) :result-step (evaluate ?total-garbage-mass (TimesFn ?num-households ?garbage-per-house))) ;; Reuse (defSuggestion HouseholdViaPopulationStrategy (numberOfHouseholds ?place ?time ?num-households) :subgoals ((populationDuring UnitedStatesOfAmerica (YearFn 1997) ?population) (sizeOf CohabitationUnit UnitedStatesOfAmerica ?size)) :result-step (evaluate ?num-households (QuotientFn ?population ?size))))) (defparameter *garbage-facts* '((amountProducedInEvent GarbageDisposalEvent Garbage-Generic (Pound-UnitOfMass 5)) ;; reuse (sizeOf CohabitationUnit UnitedStatesOfAmerica 3))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; How many hotdogs are sold in a baseball season in Wrigley Field? ;; (fire::setup-solve-test-case *hotdog-suggestions* *hotdog-facts* :KB) ;; (multiple-value-setq (ans tree) (fire::solve *hotdog-query* :response :bindings)) ;; For more solutions ;; (fire::get-solution tree) (defparameter *hotdog-query* '(unitsSold HotDogSandwich WrigleyField BaseballSeason ?num-dogs)) (defparameter *hotdog-suggestions* '(;; Number of hotdogs sold per game multiplied by number of games (defSuggestion PerEventStrategy (unitsSold ?obj ?place ?event ?total-units) :test (subEvents ?event ?subevent) :subgoals ((numberOfOccurancesIn ?event ?subevent ?num-subevents) (unitsSold ?obj ?place ?subevent ?units-per-subevent)) :result-step (evaluate ?total-units (TimesFn ?num-subevents ?units-per-subevent))) ;; This one is very similar to PerBuyerStrategy. Can we recast that into unitsSold? ;; The earlier one had annualSales and a whole annual thing about it which really ;; isnt necessary at all. (defSuggestion PerBuyerStrategy (unitsSold ?obj ?place ?event ?total-units-sold) :subgoals ((numberOfBuyers ?obj ?place ?event ?num-buyers) (unitsBoughtPerBuyer ?obj ?place ?event ?units-per-buyer)) :result-step (evaluate ?total-units-sold (TimesFn ?num-buyers ?units-per-buyer))) ;; Reuse (defSuggestion FractionOfTotalStrategy (numberOfBuyers ?obj ?place ?time ?number) :subgoals ((populationDuring ?place ?time ?total) (percentOfBuyers ?obj ?place ?percent)) :result-step (evaluate ?number (QuotientFn (TimesFn ?total ?percent) 100))) )) (defparameter *hotdog-facts* '((populationDuring WrigleyField BaseballHomeGame 40000) (subEvents BaseballSeason BaseballHomeGame) (numberOfOccurancesIn BaseballSeason BaseballHomeGame 80) (percentOfBuyers HotDogSandwich WrigleyField 50) (unitsBoughtPerBuyer HotDogSandwich WrigleyField BaseballHomeGame 1))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; What is the total amount of gasoline consumption by cars in the US? ;; (fire::setup-solve-test-case *gas-suggestions* *gas-facts* :KB) ;; (multiple-value-setq (ans tree) (fire::solve *gas-query* :response :bindings)) ;; For more solutions ;; (fire::get-solution tree) (defparameter *gas-query* '(annualAutomobileGasConsumption UnitedStatesOfAmerica (YearFn 1997) (Gallon-US ?oil-consumption))) (defparameter *gas-suggestions* '((defSuggestion MileageConsumptionStrategy (annualAutomobileGasConsumption ?place ?time (Gallon-US ?annual-consumption)) :subgoals (;; Per car, how much it is driven in the year (mileageDrivenPerYear Automobile ?place ?miles) (unitsTotal Automobile ?place ?time ?num-automobiles) ;; Miles per gallon a car gives (fuelConsumption Automobile (MilesPerGallon ?miles-per-gallon))) :result-step (evaluate ?annual-consumption (QuotientFn (TimesFn ?miles ?num-automobiles) ?miles-per-gallon))) (defSuggestion PerDayStrategyForMileage (mileageDrivenPerYear Automobile ?place ?miles-per-year) :subgoals ((mileageDrivenPerDay Automobile ?place ?miles-per-day)) :result-step (evaluate ?miles-per-year (TimesFn ?miles-per-day 365))) (defSuggestion HouseholdStrategyForCountingUnits (unitsTotal ?obj ?place ?time ?total-units) :subgoals ((numberOfHouseholds ?place ?time ?num-households) (unitsPerHousehold ?obj ?units-per-household)) :result-step (evaluate ?total-units (TimesFn ?num-households ?units-per-household))) (defSuggestion HouseholdViaPopulationStrategy (numberOfHouseholds ?place ?time ?num-households) :subgoals ((populationDuring UnitedStatesOfAmerica (YearFn 1997) ?population) (sizeOf CohabitationUnit UnitedStatesOfAmerica ?size)) :result-step (evaluate ?num-households (QuotientFn ?population ?size))))) (defparameter *gas-facts* '((sizeOf CohabitationUnit UnitedStatesOfAmerica 3) ;; Another example where we need feel for numbers -- in the KB it says ;; (YearsDuration 5 50). Now whats a typical value. We dont want to do ;; interval math with these things. (unitsPerHousehold Automobile 1) (mileageDrivenPerDay Automobile UnitedStatesOfAmerica 20) (fuelConsumption Automobile (MilesPerGallon 20)))) ;; How much time would be saved nationwide by increasing the speed ;; limit from 55 to 65 mph? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; End of Code