[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Oct 17 22:58:10 2002 
Subject:Re: Copying Lists 
From:steve 
Volume-ID:1021017.01 

Hi Adam,
Cc'd to popforum

>I'm writing a genetic algorithm.  My population is a list of chromosomes
>in the variable 'population'.  I'm trying to overwrite one choromosome
>with a copy of another:
>
>population(20) -> population(19);

The key word is "copy".  Assignment in Pop-11 does not copy data.  I 
would paraphrase the above statement as: take the pointer in the 20th 
element of the population vector and store it into the 19th element 
of the same vector.  So the 32-bit pointer gets copied but the data 
doesn't.

>However I find that anything do to population(20) is automatically done to
>population(19).

Because they point to the same object in the heap which is, if I read 
your comments rightly, a vector of 0s and 1s.  If that guess is 
correct, all you have to do is use the copy procedure, see HELP 
*COPY.  In other words, you should write.

copy(population(20)) -> population(19);

The copy procedure only does a shallow copy i.e. non-recursive.  If 
you know Java the equivalent is "clone".  If you know C++, the 
equivalent is the normal bitwise copy.  (Watch out if you are using 
lists inside of vectors.  You need the copylist procedure for lists, 
see HELP *COPYLIST.)

Have you considered representing the bit-vector as an integer? 
Pop-11 integers are not limited to 32-bits but are of arbitrary size! 
A common trick is to use them to represent bit-vectors.  To do this 
you need to know the bitwise operators:

     &&  bitwise AND
     ||  bitwise OR
     ~~  bitwise NOT

There are some more which are documented in REF *NUMBERS.  You also 
need to set and test bits.  When you want to test the Nth bit BITNUM 
of a number NUMBER you use the testbit procedure and get back true or 
false.

     testbit( NUMBER, BITNUM ) -> bool

To set the Nth bit means generating a new number because, naturally 
enough, Pop-11 cannot update the bits of a number.  So you use 
testbit in a rather peculiar fashion.

     bool -> testbit( NUMBER, BITNUM ) -> NEW_NUMBER

Using this, your mutate routine would go from this ...

>define mutate(chom);
>lvars pos;
>     1 + random0( chom_length) -> pos;
>
>     if population(chom)(pos) == 1 then
>         0 -> population(chom)(pos);
>     else
>         1 -> population(chom)(pos);
>     endif;
>enddefine;

... to this ...

define mutate( chom );
     lvars pos = random( chom_length );   ;;; random = random0 + 1
     ;;; toggle the pos'th bit.
     not( testbit( population( chom ), pos ) ) -> testbit( population( 
chom ), pos )
enddefine;

It is kind of neat - although it is the kind of trick that won't 
appeal to everyone.

-- 
Steve