| |
comp.lang.lisp |
> finds a random subtree (not including the entire tree) of tree and returns
> it, possibly
> after replacing it with subst if subst is non nil.."
> (if (< (random 1.0) (expt prob-no-recursion (1- (max-depth tree))))
> ;; swap with a sub tree
> (let ((args (cdr tree))
> (rnd (random 2))
> (tmp nil))
> (if (null subst)
> (cond ((= rnd 0) (car args))
> (t (second args)))
> (cond ((= rnd 0) (setf tmp (car args))
> (rplaca args subst)
> tmp)
> (t (setf tmp (second args))
> (rplacd args (list subst))
> tmp))))
> ;; swap with another sub-tree
> (let* ((dl (max-depth (second tree)))
> (dr (max-depth (third tree)))
> (rnd (random (+ dl dr))))
> (if (< rnd dl)
> (crossover (second tree) subst)
> (crossover (third tree) subst)))))
(setf (car args) subst) instead of (rplaca args subst)
and
(setf (cadr args) subst) instead of (rplacd args (list subst))
You should consider representing the nodes in your tree as defstructs.