Допис з розмови
Newby(ish)
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.cis.ohio-state.edu!news.dfci.harvard.edu!news.ccs.neu.edu!not-for-mail
From: Joe Marshall <j...@ccs.neu.edu>
Newsgroups: comp.lang.lisp
Subject: Re: Newby(ish)
Date: 27 Jun 2003 09:18:37 -0400
Organization: Northeastern University, College of Computer Science
Lines: 40
Message-ID: <7k774q1e.fsf@ccs.neu.edu>
References: <Pine.LNX.4.44.0306271132520.26288-100000@avenger.inf.ed.ac.uk>
NNTP-Posting-Host: jmarshall.ccs.neu.edu
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: camelot.ccs.neu.edu 1056719916 28753 129.10.115.140 (27 Jun 2003 13:18:36 GMT)
X-Complaints-To: news@news.ccs.neu.edu
NNTP-Posting-Date: Fri, 27 Jun 2003 13:18:36 +0000 (UTC)
User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Native Windows TTY Support (Windows))
Gregory Rickeard <s9905...@sms.ed.ac.uk> writes:
> (defun crossover (tree &optional (subst nil) (prob-no-recursion .75))
> "args: tree &optional (subst nil) (prob-no-recursion .75)
>
> 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)))))
Instead of RPLACA and RPLACD, you should use SETF
(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.