Hello all,
I'm programming a genetic algorithm, and am having difficulties with the
hashtables. The following is the code and a snippet of the output.

(defun crossover (lengthofchromosome sizeofpopulation
hashofpopulation)
"Take a pruned hashtable. Effect crossover. The resulting hash
does not have to be fullsized."
(format t "~&Size of pruned population : ~A" (hashtablecount
hashofpopulation))
(let* ((vectorofparents (makearray (hashtablecount
hashofpopulation)
:initialcontents (loop for k being the
hashkeys in hashofpopulation
collect k))) ;Easy to access vector of keys
(newhash (makehashtable :test 'equalp)))
(loop for k being the hashkeys in hashofpopulation using
(hashvalue v) do
(setf (gethash k newhash)
v))
(loop repeat 20 ;Should find some way of making this not
arbitrary, but also leave some space for mutations.
for i = (random (length vectorofparents)) then (random (length
vectorofparents))
and j = (random (length vectorofparents)) then (random (length
vectorofparents))
and crossoverpoint = (random lengthofchromosome) then (random
lengthofchromosome)
with firstparent = () and secondparent = ()
while (< (hashtablecount newhash)
sizeofpopulation)
finally (let ((returnlist (loop for k being the hashkeys in
newhash collect k)))
(format t "~&Crossover returns list: ~A" returnlist)
(return newhash))
do
(setf firstparent (aref vectorofparents i)
secondparent (aref vectorofparents j)) ;Pick 2 to make
parents off
(rotatef (subseq firstparent crossoverpoint) (subseq
secondparent crossoverpoint))
(setf (gethash firstparent newhash) 1
(gethash secondparent newhash) 1)
)))
Crossover returns list: ((1 0 0 0 1 1 1) (1 0 1 0 1 0 1) (0 1 0 1 0 0 0)
(1 1 0 1 1 1 0) (1 0 0 1 1 0 0) (1 1 0 1 0 0 0)
(1 0 0 1 0 1 0) (1 1 0 1 1 1 0) (1 0 0 0 1 1 1)
(1 0 1 0 1 0 1) (1 0 0 1 0 1 0))

As one can see, the list '(1 0 0 0 1 1 1) occurs 2 times. I've made the
NEWHASH hashtable using :test 'EQUAL as well as 'EQUALP, and I get
similar results.
I was earlier using only the CHROMOSOMEHASH hashtable with the same
results, and then decided to see if I was getting the same thing if I
created a new hashtable, hence NEWHASH.
I don't know what is going on. I thought that if you do a SETF and
GETHASH on an existing key, then you just change the value. If you are
not dealing with the value, as I am in this function, then nothing
should happen. Only unique keys (chromosomes, in the case of this
function) should be added to the hashtable. Am I missing something?
Please let me know if I'm doing something wrong here.
I'm using sbcl 1.0.50 in archlinux 64bit.
Thank you
