;;;; 2007-03-20 22:35:43 ;;;; This is your lisp file. There are many like it, but this one is yours. ;;;; Iterative Improvement Search Algorithms (defun between (min max) (+ min (* (random 1.0) (- max min)))) (defun mutate (current) (mapcar #'(lambda (part) (let* ((old (cdr part)) (new (+ old (between -0.5 0.5)))) (cons (car part) new))) current)) (defmacro at (key l) `(cdr (assoc ,key ,l))) (setf *cache* (make-hash-table)) (setf *things* (make-hash-table)) (defstruct thing name generator) (defmacro def (name generator) `(setf (gethash ',name *things*) (make-thing :name ',name :generator #'(lambda () ,generator)))) (def a (between 2 3)) (defmacro ? (key) (let* ((default :sOmCrazyMizSpeltSymBBol) (tmp (gethash ',key *memos* default))) (if (eq tmp default) (funcall (gethash ',key *things* 'missing)) tmp))) (register 'a (between 10 20)) (defun start () `((a . 1) (b . 1))) (defun cost (l) (let ((a (at 'a l)) (b (at 'b l))) (abs (+ (* 3 a ) (* -1 b) 2)))) (defun metropolis (&key (steps 10000) (cost 'cost) (next 'mutate) (first 'start) (near 0.001)) (let* (best (time 1) (bestS (expt 10 32)) new newS (current (funcall first)) (currentS (funcall cost current))) (loop (if (<= time 0 ) (return (values best bestS))) (setf new (funcall next current) newS (funcall cost new)) (if (< newS bestS) (setf best (copy-list new) bestS newS)) (if (accept (- newS currentS) time) (setf current (copy-list new) currentS newS)) (setf time (- time (/ 1.0 steps))) ))) (defun accept(delta time) (or (<= delta 0) (< (random 1.0) (expt 2.781281828 (* -1.0 (/ delta time))))))