Guest User

Untitled

a guest
Jul 13th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.99 KB | None | 0 0
  1. ;;;;;;;facts;;;;;;;
  2. ;(deffacts categories ;unused, would be used later in the program
  3. ;   (category hatchback Alfa_Romeo_Giulietta Volkswagen_Golf Ford_Fiesta)
  4. ;   (category coupe Jaguar_XKR Mazda_MX5 Audi_TT)
  5. ;   (category supercar Aston_Martin_DB9 Ferrari_458_Italia Audi_R8)
  6. ;   (category saloon BMW_3_Series Mercedes_C_Class Jaguar_XF)
  7. ;   (category estate Mercedes_E_Class_Estate Subaru_Legacy ;BMW_5_Series_Touring)
  8. ;   (category hot_hatch Citroen_DS3 Volkswagen_Golf_GTI Ford_Focus_ST )
  9. ;   (category suv Range_Rover BMW_X5 Volvo_XC90)
  10. ;)
  11.  
  12. (deftemplate category
  13.     (slot type)
  14.     (slot value)
  15. )
  16.  
  17. (deffacts types
  18.     (category (type hatchback)(value 0))
  19.     (category (type coupe)(value 0))
  20.     (category (type supercar)(value 0))
  21.     (category (type saloon)(value 0))
  22.     (category (type estate)(value 0))
  23.     (category (type hot_hatch)(value 0))
  24.     (category (type suv)(value 0))
  25. )
  26.  
  27.  
  28. ;;;;;;;question fuctions;;;;;;;
  29. (deffunction ask-question (?question $?allowed-values)
  30.    (printout t ?question)
  31.    (bind ?answer (read))
  32.    (if (lexemep ?answer)
  33.        then (bind ?answer (lowcase ?answer)))
  34.    (while (not (member ?answer ?allowed-values)) do
  35.       (printout t ?question)
  36.       (bind ?answer (read))
  37.       (if (lexemep ?answer)
  38.           then (bind ?answer (lowcase ?answer))))
  39.    ?answer
  40. )
  41.  
  42. (deffunction yes-or-no-p (?question)
  43.    (bind ?response (ask-question ?question yes no y n))
  44.    (if (or (eq ?response yes) (eq ?response y))
  45.        then TRUE
  46.        else FALSE)
  47. )
  48.  
  49.  
  50. ;;;;;;;probability function;;;;;;;
  51. (deffunction calc(?b ?type)
  52.     ?fact <- (category (type ?type)(value ?a)) ;retrieve the matching fact index
  53.     (bind ?probability (/ (* ?a ?b) (+ (* ?a ?b) (* (- 1 ?a) (- 1 ?b)))))
  54.     (bind ?prob (/ (round (* ?probability 100)) 100)) ;round to 2 decimal places
  55.     (modify ?fact (value (?prob))) ;modify the value of the fact at that index
  56. )
  57.  
  58.  
  59. ;;;;;;;questions;;;;;;;
  60. ;As this is the first question, we don't need to work out the probability,
  61. ;just set the facts to their respective values, as they are 0 by default, which will not work in the equation.
  62. (defrule carry_multiple_passengers ""
  63.     (declare (salience 10))
  64.     (not (multiple_passengers ?))
  65.     ?h <- (category (type hatchback)) ;get the fact indices
  66.     ?c <- (category (type coupe))
  67.     ?sc <- (category (type supercar))
  68.     ?s <- (category (type saloon))
  69.     ?e <- (category (type estate))
  70.     ?hh <- (category (type hot_hatch))
  71.     ?suv <- (category (type suv))
  72.    =>
  73.    (if (yes-or-no-p "Do you have to carry multiple passengers (yes/no)?")
  74.        then
  75.     (assert (multiple_passengers yes))
  76.     (modify ?h (value 1)) ;modify the facts
  77.     (modify ?c (value 0.3))
  78.     (modify ?sc (value 0.3))
  79.     (modify ?s (value 0.9))
  80.     (modify ?e (value 1))
  81.     (modify ?hh (value 0.6))
  82.     (modify ?suv (value 0.8))
  83.    else
  84.     (assert (multiple_passengers no))
  85.    )
  86. )
  87.  
  88. (defrule carry_child_passengers ""
  89.     (declare (salience 9))
  90.     (not (child_passengers ?))
  91.    =>
  92.    (if (yes-or-no-p "Do you have to carry child passengers (yes/no)?")
  93.        then
  94.     (assert (child_passengers yes))
  95.     (calc 1 hatchback) ;call the calc function, with the P(b) probability, and the type
  96.     (calc 0.2 coupe)
  97.     (calc 0.1 supercar)
  98.     (calc 0.7 saloon)
  99.     (calc 1 estate)
  100.     (calc 0.5 hot_hatch)
  101.     (calc 0.6 suv)
  102.    else
  103.     (assert (child_passengers no))
  104.    )
  105. )
  106.  
  107. (defrule drive_on_circuit ""
  108.     (declare (salience 8))
  109.     (not (circuit_drive ?))
  110.    =>
  111.    (if (yes-or-no-p "Do you want to drive on a circuit? (yes/no)?")
  112.        then
  113.     (calc 0.4 hatchback)
  114.     (calc 1 coupe)
  115.     (calc 1 supercar)
  116.     (calc 0.6 saloon)
  117.     (calc 0.4 estate)
  118.     (calc 0.8 hot_hatch)
  119.     (calc 0.4 suv)
  120.    else
  121.     (assert (circuit_drive no))
  122.    )
  123. )
  124.  
  125. (defrule drive_off_road ""
  126.     (declare (salience 7))
  127.     (not (offroad_drive ?))
  128.    =>
  129.    (if (yes-or-no-p "Do you want to drive off-road (yes/no)?")
  130.        then
  131.     (calc 0.3 hatchback)
  132.     (calc 0.1 coupe)
  133.     (calc 0.1 supercar)
  134.     (calc 0.3 saloon)
  135.     (calc 0.5 estate)
  136.     (calc 0.3 hot_hatch)
  137.     (calc 1 suv)
  138.    else
  139.     (assert (offroad_drive no))
  140.    )
  141. )
  142.  
  143. (defrule drive_congested_areas ""
  144.     (declare (salience 6))
  145.     (not (drive_congested ?))
  146.    =>
  147.    (if (yes-or-no-p "Do you drive in congested areas (yes/no)?")
  148.        then
  149.     (calc 1 hatchback)
  150.     (calc 0.5 coupe)
  151.     (calc 0.3 supercar)
  152.     (calc 0.8 saloon)
  153.     (calc 0.8 estate)
  154.     (calc 0.7 hot_hatch)
  155.     (calc 0.6 suv)
  156.    else
  157.     (assert (drive_congested no))
  158.    )
  159. )
  160.  
  161. (defrule drive_rush_hour ""
  162.     (declare (salience 5))
  163.     (not (rush_hour_drive ?))
  164.    =>
  165.    (if (yes-or-no-p "Do you frequently drive in rush hour traffic (yes/no)?")
  166.        then
  167.     (calc 1 hatchback)
  168.     (calc 0.5 coupe)
  169.     (calc 0.3 supercar)
  170.     (calc 0.9 saloon)
  171.     (calc 0.9 estate)
  172.     (calc 0.7 hot_hatch)
  173.     (calc 0.6 suv)
  174.    else
  175.     (assert (rush_hour_drive no))
  176.    )
  177. )
  178.  
  179. (defrule park_on_road ""
  180.     (declare (salience 4))
  181.     (not (road_park ?))
  182.    =>
  183.    (if (yes-or-no-p "Can you only park on the road (yes/no)?")
  184.        then
  185.     (calc 1 hatchback)
  186.     (calc 0.5 coupe)
  187.     (calc 0.3 supercar)
  188.     (calc 0.9 saloon)
  189.     (calc 0.9 estate)
  190.     (calc 0.6 hot_hatch)
  191.     (calc 0.7 suv)
  192.    else
  193.     (assert (road_park no))
  194.    )
  195. )
Add Comment
Please, Sign In to add comment