;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: learning/algorithms/knowledge-learning.lisp

;;;; Knowledge in Learning: Logical Hypotheses (Chapter 20 of the 3rd edition)

;;; The examples are those of a learning problem with a single goal; an
;;; example is positive when its goal has the first of the goal's values.

(defun positive-example? (e goal)
  (eq (attribute-value goal e) (first (attribute-values goal))))

;;;; Current-Best-Hypothesis Search

;;; A hypothesis is a disjunction of conjunctions, a list of disjuncts each
;;; of which is a list of (attribute-name . value) tests.

(defun conjunction-covers? (conjunction e)
  (every #'(lambda (test) (eq (cdr (assoc (car test) e)) (cdr test))) conjunction))

(defun hypothesis-covers? (h e)
  "Does the hypothesis H classify the example E as positive?"
  (some #'(lambda (conjunction) (conjunction-covers? conjunction e)) h))

(defun hypothesis-consistent? (h examples goal)
  (every #'(lambda (e) (eq (hypothesis-covers? h e) (positive-example? e goal)))
         examples))

(defun current-best-learning (examples h attributes goal &optional seen)
  "Search for a hypothesis consistent with EXAMPLES, starting from H and
  specializing it on false positives and generalizing it on false
  negatives, backtracking when no consistent one is found.  SEEN are the
  examples processed so far.  Returns :FAIL when there is none."
  (if (null examples)
      h
    (let* ((e (first examples))
           (seen (cons e seen)))
      (cond ((eq (hypothesis-covers? h e) (positive-example? e goal))
             (current-best-learning (rest examples) h attributes goal seen))
            (t (dolist (h2 (if (positive-example? e goal)
                               (generalizations h e attributes)
                             (specializations h e attributes))
                           :fail)
                 (when (hypothesis-consistent? h2 seen goal)
                   (let ((h3 (current-best-learning (rest examples) h2 attributes goal seen)))
                     (unless (eq h3 :fail) (return h3))))))))))

(defun specializations (h e attributes)
  "The hypotheses that exclude the false positive E: every disjunct that
  covers E is either dropped or given one more test that E fails."
  (let ((choices
          (mapcar #'(lambda (conjunction)
                      (if (conjunction-covers? conjunction e)
                          (cons :drop
                                (mapcan #'(lambda (a)
                                            (unless (assoc (attribute-name a) conjunction)
                                              (mapcar #'(lambda (v)
                                                          (append conjunction
                                                                  (list (cons (attribute-name a) v))))
                                                      (remove (attribute-value a e)
                                                              (attribute-values a)))))
                                        attributes))
                        (list conjunction)))
                  h)))
    (mapcar #'(lambda (disjuncts) (remove :drop disjuncts))
            (cartesian-product choices))))

(defun generalizations (h e attributes)
  "The hypotheses that include the false negative E: some disjunct loses
  the tests that E fails, or the description of E becomes a new disjunct."
  (append
   (loop for conjunction in h
         for i from 0
         collect (append (subseq h 0 i)
                         (list (remove-if-not #'(lambda (test) (conjunction-covers? (list test) e))
                                              conjunction))
                         (subseq h (+ i 1))))
   (list (append h (list (mapcar #'(lambda (a)
                                     (cons (attribute-name a) (attribute-value a e)))
                                 attributes))))))

;;;; Version-Space Learning

;;; The hypotheses are the conjunctions with at most one test of the form
;;; attribute = value for each attribute (the empty one covers everything).

(defun all-conjunctive-hypotheses (attributes)
  (mapcar #'(lambda (choice) (remove nil choice))
          (cartesian-product
           (mapcar #'(lambda (a)
                       (cons nil (mapcar #'(lambda (v) (cons (attribute-name a) v))
                                         (attribute-values a))))
                   attributes))))

(defun version-space-learning (examples attributes goal)
  "The version space: the conjunctive hypotheses consistent with all the
  EXAMPLES, found by removing the inconsistent ones example by example."
  (let ((v (all-conjunctive-hypotheses attributes)))
    (dolist (e examples v)
      (when v
        (setf v (version-space-update v e goal))))))

(defun version-space-update (v e goal)
  (remove-if-not #'(lambda (h)
                     (eq (conjunction-covers? h e) (positive-example? e goal)))
                 v))

;;;; Minimal Consistent Determinations

(defun subsets-of-size (list k)
  "All the subsets of LIST with K elements, keeping the order of LIST."
  (cond ((= k 0) (list nil))
        ((null list) nil)
        (t (append (mapcar #'(lambda (s) (cons (first list) s))
                           (subsets-of-size (rest list) (- k 1)))
                   (subsets-of-size (rest list) k)))))

(defun consistent-determination? (attribute-names examples goal)
  "Do the values of ATTRIBUTE-NAMES determine the goal on EXAMPLES?"
  (let ((table (make-hash-table :test #'equal)))
    (dolist (e examples t)
      (let ((key (mapcar #'(lambda (a) (cdr (assoc a e))) attribute-names))
            (class (attribute-value goal e)))
        (multiple-value-bind (old found) (gethash key table)
          (when (and found (not (equal old class)))
            (return nil))
          (setf (gethash key table) class))))))

(defun minimal-consistent-det (examples attribute-names goal)
  "A smallest set of attributes that determines the goal on EXAMPLES."
  (loop for i from 0 to (length attribute-names)
        do (dolist (subset (subsets-of-size attribute-names i))
             (when (consistent-determination? subset examples goal)
               (return-from minimal-consistent-det subset)))))
