;;; learning/domains/restaurant-multivalued.lisp
;;; Restaurant example from chapter 18, encoded
;;; using multivalued input attributes suitable for
;;; decision-tree learning.

(defvar *restaurant-multivalued* 
  '((patrons none some full)
    (none . no)
    (some . yes)
    (full 
     (waitestimate 60 30 10 0)
     (60 . no)
     (30
      (alternate yes no)
      (yes
       (fri/sat yes no)
       (no . no)
       (yes . yes))
      (no
       (reservation yes no)
       (no 
        (bar yes no)
        (no . no)
        (yes . yes))
       (yes . yes)))
     (10
      (hungry yes no)
      (no . yes)
      (yes
       (alternate yes no)
       (no . yes)
       (yes
        (raining yes no)
        (no . no)
        (yes . yes))))
     (0 . yes))))

;;; The 12 examples of Figure 19.2 are read from restaurant.csv of the
;;; aima-data submodule, whose columns are the attributes below and the goal.

(defparameter *restaurant-columns*
  '(alternate bar fri/sat hungry patrons price raining reservation type
    waitestimate willwait)
  "The columns of restaurant.csv.")

(defun read-restaurant-examples (encode)
  "The examples of restaurant.csv, as alists that start with the goal
  willwait; (ENCODE attribute field) is the value of the string FIELD."
  (mapcar #'(lambda (row)
              (let ((pairs (mapcar #'(lambda (attribute field)
                                       (cons attribute (funcall encode attribute field)))
                                   *restaurant-columns* row)))
                (cons (car (last pairs)) (butlast pairs))))
          (read-aima-data-csv "restaurant.csv")))

(defun restaurant-multivalued-value (attribute field)
  "The symbolic value of FIELD: the price $ is D, and the wait estimate is
  the lower end, in minutes, of its interval."
  (case attribute
    (price (cdr (assoc field '(("$" . D) ("$$" . DD) ("$$$" . DDD))
                       :test #'string=)))
    (waitestimate (cdr (assoc field '(("0-10" . 0) ("10-30" . 10) ("30-60" . 30)
                                      (">60" . 60))
                              :test #'string=)))
    (t (intern (string-upcase field)))))

(defvar *restaurant-multivalued-problem*)

(setq *restaurant-multivalued-problem*
      (make-learning-problem
       :attributes '((alternate yes no)
                     (bar yes no)
                     (fri/sat yes no)
                     (hungry yes no)
                     (patrons None Some Full)
                     (price D DD DDD)
                     (raining yes no)
                     (reservation yes no)
                     (type French Italian Thai Burger)
                     (waitestimate 0 10 30 60))
       :goals      '((willwait yes no))
       :examples   (read-restaurant-examples #'restaurant-multivalued-value)))




