;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- ;;; Copyright 2007 by Robert St. Amant. ;;; To download the code for simple-POP and simple-GP, visit ;;; . ;;; This software is released under the license described by Peter ;;; Norvig: . If you find this ;;; software useful, please send email to . ;;; About aima-compatibility.lisp: ;;; The code in this file provides a set of quick-and-dirty ;;; conditionalizations to allow simple-POP and simple-GP to use the ;;; PAIP *or* AIMA code bases. The conditionalizations are fragile. ;;; The symbol PAIP is pushed onto the *features* list in ;;; load-simple-planners.lisp. If it's not there, we assume that ;;; we're using AIMA instead. ;;; ============================== ;;; PAIP overrides and extensions ;;; See PAIP/auxfns.lisp #+PAIP (defconstant +no-bindings+ no-bindings) (defvar *variable-prefix-char* #-PAIP #\$ #+PAIP #\?) #+PAIP (defun variable-p (x) "Is x a variable (a symbol beginning with `?')?" (and (symbolp x) (equal (elt (symbol-name x) 0) *variable-prefix-char*))) ;;; ============================== ;;; AIMA extensions to handle PAIP functions #-PAIP (defun variable? (x) "Is x a variable (a symbol starting with $)?" (and (symbolp x) (eql (char (symbol-name x) 0) *variable-prefix-char*))) #-PAIP (setf (symbol-function 'find-all-if) #'remove-if-not) #-PAIP (defun find-all (item sequence &rest keyword-args &key (test #'eql) test-not &allow-other-keys) "Find all those elements of sequence that match item, according to the keywords. Doesn't alter sequence." (if test-not (apply #'remove item sequence :test-not (complement test-not) keyword-args) (apply #'remove item sequence :test (complement test) keyword-args))) ;;; See PAIP/patmatch.lisp #-PAIP (defun variable-p (x) "Is x a variable (a symbol beginning with `?')?" (and (symbolp x) (equal (elt (symbol-name x) 0) #\?))) ;;; See PAIP/search.lisp #-PAIP (defun sorter (cost-fn) "Return a combiner function that sorts according to cost-fn." #'(lambda (new old) (sort (append new old) #'< :key cost-fn))) #-PAIP (defstructure (planning-problem (:include problem))) #-PAIP (defmethod goal-test ((problem planning-problem) node) (plan-solution-p (state-if-node node))) #-PAIP (defmethod successors ((problem planning-problem) node) (mapcar #'(lambda (successor) (cons 'action successor)) (plan-successors (state-if-node node)))) ;;; Utility #-PAIP (defun state-if-node (node-or-state) (if (node-p node-or-state) (node-state node-or-state) node-or-state)) ;;; ============================== ;;; Common function (defun simple-search (start goal-p successors cost-fn) (declare (ignorable goal-p successors)) #+PAIP (best-first-search start goal-p successors cost-fn) #-PAIP (let* ((*variable-prefix-char* #\?) (result (best-first-search (make-planning-problem :initial-state start) #'(lambda (node-or-state) (funcall cost-fn (state-if-node node-or-state)))))) (when result (node-state result)))) #+ignore (defun node-state (object) object)