Copying Lists
Author Message
Copying Lists

Hi everyone, I've got a problem.

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);

However I find that anything do to population(20) is automatically done to
population(19).  This means that I'm ending up with most of the population
becoming the same, but not because its found a good solution.

I'd be very greatful for any suggestions,

Thanks

Adam Knowles
MSc Natural Computation

P.S. A copy of the mutation function I'm using is below (the chromosomes
are binary 0s and 1s)

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;

Mon, 04 Apr 2005 23:43:13 GMT
Copying Lists

Quote:

> I'm trying to overwrite one choromosome
> with a copy of another:

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

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

Try copy(population(20)) -> population(19);
See HELP COPY.

Stephen Isard

Tue, 05 Apr 2005 00:40:27 GMT
Copying Lists
Hi Adam,
Cc'd to popforum

Quote:
>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.

Quote:
>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 ...

Quote:
>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

Tue, 05 Apr 2005 06:58:10 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages

Powered by phpBB® Forum Software