Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;;facts;;;;;;;
- ;(deffacts categories ;unused, would be used later in the program
- ; (category hatchback Alfa_Romeo_Giulietta Volkswagen_Golf Ford_Fiesta)
- ; (category coupe Jaguar_XKR Mazda_MX5 Audi_TT)
- ; (category supercar Aston_Martin_DB9 Ferrari_458_Italia Audi_R8)
- ; (category saloon BMW_3_Series Mercedes_C_Class Jaguar_XF)
- ; (category estate Mercedes_E_Class_Estate Subaru_Legacy ;BMW_5_Series_Touring)
- ; (category hot_hatch Citroen_DS3 Volkswagen_Golf_GTI Ford_Focus_ST )
- ; (category suv Range_Rover BMW_X5 Volvo_XC90)
- ;)
- (deftemplate category
- (slot type)
- (slot value)
- )
- (deffacts types
- (category (type hatchback)(value 0))
- (category (type coupe)(value 0))
- (category (type supercar)(value 0))
- (category (type saloon)(value 0))
- (category (type estate)(value 0))
- (category (type hot_hatch)(value 0))
- (category (type suv)(value 0))
- )
- ;;;;;;;question fuctions;;;;;;;
- (deffunction ask-question (?question $?allowed-values)
- (printout t ?question)
- (bind ?answer (read))
- (if (lexemep ?answer)
- then (bind ?answer (lowcase ?answer)))
- (while (not (member ?answer ?allowed-values)) do
- (printout t ?question)
- (bind ?answer (read))
- (if (lexemep ?answer)
- then (bind ?answer (lowcase ?answer))))
- ?answer
- )
- (deffunction yes-or-no-p (?question)
- (bind ?response (ask-question ?question yes no y n))
- (if (or (eq ?response yes) (eq ?response y))
- then TRUE
- else FALSE)
- )
- ;;;;;;;probability function;;;;;;;
- (deffunction calc(?b ?type)
- ?fact <- (category (type ?type)(value ?a)) ;retrieve the matching fact index
- (bind ?probability (/ (* ?a ?b) (+ (* ?a ?b) (* (- 1 ?a) (- 1 ?b)))))
- (bind ?prob (/ (round (* ?probability 100)) 100)) ;round to 2 decimal places
- (modify ?fact (value (?prob))) ;modify the value of the fact at that index
- )
- ;;;;;;;questions;;;;;;;
- ;As this is the first question, we don't need to work out the probability,
- ;just set the facts to their respective values, as they are 0 by default, which will not work in the equation.
- (defrule carry_multiple_passengers ""
- (declare (salience 10))
- (not (multiple_passengers ?))
- ?h <- (category (type hatchback)) ;get the fact indices
- ?c <- (category (type coupe))
- ?sc <- (category (type supercar))
- ?s <- (category (type saloon))
- ?e <- (category (type estate))
- ?hh <- (category (type hot_hatch))
- ?suv <- (category (type suv))
- =>
- (if (yes-or-no-p "Do you have to carry multiple passengers (yes/no)?")
- then
- (assert (multiple_passengers yes))
- (modify ?h (value 1)) ;modify the facts
- (modify ?c (value 0.3))
- (modify ?sc (value 0.3))
- (modify ?s (value 0.9))
- (modify ?e (value 1))
- (modify ?hh (value 0.6))
- (modify ?suv (value 0.8))
- else
- (assert (multiple_passengers no))
- )
- )
- (defrule carry_child_passengers ""
- (declare (salience 9))
- (not (child_passengers ?))
- =>
- (if (yes-or-no-p "Do you have to carry child passengers (yes/no)?")
- then
- (assert (child_passengers yes))
- (calc 1 hatchback) ;call the calc function, with the P(b) probability, and the type
- (calc 0.2 coupe)
- (calc 0.1 supercar)
- (calc 0.7 saloon)
- (calc 1 estate)
- (calc 0.5 hot_hatch)
- (calc 0.6 suv)
- else
- (assert (child_passengers no))
- )
- )
- (defrule drive_on_circuit ""
- (declare (salience 8))
- (not (circuit_drive ?))
- =>
- (if (yes-or-no-p "Do you want to drive on a circuit? (yes/no)?")
- then
- (calc 0.4 hatchback)
- (calc 1 coupe)
- (calc 1 supercar)
- (calc 0.6 saloon)
- (calc 0.4 estate)
- (calc 0.8 hot_hatch)
- (calc 0.4 suv)
- else
- (assert (circuit_drive no))
- )
- )
- (defrule drive_off_road ""
- (declare (salience 7))
- (not (offroad_drive ?))
- =>
- (if (yes-or-no-p "Do you want to drive off-road (yes/no)?")
- then
- (calc 0.3 hatchback)
- (calc 0.1 coupe)
- (calc 0.1 supercar)
- (calc 0.3 saloon)
- (calc 0.5 estate)
- (calc 0.3 hot_hatch)
- (calc 1 suv)
- else
- (assert (offroad_drive no))
- )
- )
- (defrule drive_congested_areas ""
- (declare (salience 6))
- (not (drive_congested ?))
- =>
- (if (yes-or-no-p "Do you drive in congested areas (yes/no)?")
- then
- (calc 1 hatchback)
- (calc 0.5 coupe)
- (calc 0.3 supercar)
- (calc 0.8 saloon)
- (calc 0.8 estate)
- (calc 0.7 hot_hatch)
- (calc 0.6 suv)
- else
- (assert (drive_congested no))
- )
- )
- (defrule drive_rush_hour ""
- (declare (salience 5))
- (not (rush_hour_drive ?))
- =>
- (if (yes-or-no-p "Do you frequently drive in rush hour traffic (yes/no)?")
- then
- (calc 1 hatchback)
- (calc 0.5 coupe)
- (calc 0.3 supercar)
- (calc 0.9 saloon)
- (calc 0.9 estate)
- (calc 0.7 hot_hatch)
- (calc 0.6 suv)
- else
- (assert (rush_hour_drive no))
- )
- )
- (defrule park_on_road ""
- (declare (salience 4))
- (not (road_park ?))
- =>
- (if (yes-or-no-p "Can you only park on the road (yes/no)?")
- then
- (calc 1 hatchback)
- (calc 0.5 coupe)
- (calc 0.3 supercar)
- (calc 0.9 saloon)
- (calc 0.9 estate)
- (calc 0.6 hot_hatch)
- (calc 0.7 suv)
- else
- (assert (road_park no))
- )
- )
Add Comment
Please, Sign In to add comment