Psychic bug

J

Jennifer Hallinan

Hi,

I have a mutate function for a genetic algorithm which is giving me
odd results. I suspect I'm missing somthing really simple, so I'd be
grateful for any suggestions. Basically, when I comment out the line
which is commented out below, it works fine (although of course it
doesn't change the genome). When I uncomment it gen[letter]
automagically gets the value base in the print statements, before the
assignment has been made. And it still doesn't update the genome.
Genome is a string of integers in the range 0- 3, hence the
conversion.

def Mutate(self, mrate):
g = Random(rseed)
# If no mutation rate specified, use 1 per genome
if mrate == -1:
mrate = 1.0/len(self.genome)
print "mrate is ", mrate

print "original genome: "
print self.genome
# convert genome to a list
gen = [ord(letter)-48 for letter in self.genome]

for letter in range(len(gen)):
rnum = g.random()
if rnum < mrate:
base = g.randint(0,3)
print "base is ", base
print "pos ", letter, gen[letter], " to ", base
# gen[letter] = base

# convert list back to a string
self.genome = ''.join([str(x) for x in gen])
print "final genome"
print self.genome


Output with line commented out:
base is 1
pos 40 0 to 1
base is 0
pos 185 2 to 0
base is 3
pos 223 0 to 3

....etc

Output with line included:
base is 1
pos 40 1 to 1
base is 0
pos 185 0 to 0
base is 3
pos 223 3 to 3

Thanks,

Jen
 
A

Alan Franzoni

Il Wed, 22 Feb 2006 19:04:48 +0000, Jennifer Hallinan ha scritto:
Genome is a string of integers in the range 0- 3, hence the
conversion.

Genome is a *class*, i suppose from this code, and Mutate is... an instance
method, I think. And self.genome is a string like composed of integers in
the 0-3 range.

I would advise you to use a list in first place, and define a method (maybe
__str__ or __repr__ ) in your class in order to output it as a string - I
think it may make your work easier.

Your code is a little unpythonic-inside. I think you're coding from a very
programmer-unfriendly language :)

I don't understand your specific problem (it may be class-related but we
can't know if you don't post your full code) because the code looks fine to
me, e.g. it works as it should, with that line commented out.

For printing, try this:

print "pos:%s , old value: %s, new value: %s " % (
letter, gen[letter], base)

in place of your two prints in the if block, it should help you.

--
Alan Franzoni <[email protected]>
-
Togli .xyz dalla mia email per contattarmi.
To contact me, remove .xyz from my email address.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
 
R

Raymond Hettinger

a mutate function for a genetic algorithm which is giving me
odd results. I suspect I'm missing somthing really simple, so I'd be
grateful for any suggestions. Basically, when I comment out the line
which is commented out below, it works fine (although of course it
doesn't change the genome). When I uncomment it gen[letter]
automagically gets the value base in the print statements, before the
assignment has been made. And it still doesn't update the genome.

The code looks fine to me and your description suggests that the
problem lies elsewhere -- the genome instance is getting updated
but the tool to look at it is working on an unmutated copy.

When you see the "automagic" value assignment appearing before
the assignment is even made, it is a cue that the assignment got
made on a previous run and was esssentially already done when
the function was called a second time.

An easy way to see this in action is to add print statements to the
beginning and end of the mutate function:

print self.genome[:70]

So, I think the error is likely in the calling code and not in the
mutate function.


Raymond
 
G

Gerard Flanagan

Jennifer said:
Hi,

I have a mutate function for a genetic algorithm which is giving me
odd results. I suspect I'm missing somthing really simple, so I'd be
grateful for any suggestions. Basically, when I comment out the line
which is commented out below, it works fine (although of course it
doesn't change the genome). When I uncomment it gen[letter]
automagically gets the value base in the print statements, before the
assignment has been made. And it still doesn't update the genome.
Genome is a string of integers in the range 0- 3, hence the
conversion.

def Mutate(self, mrate):
g = Random(rseed)
# If no mutation rate specified, use 1 per genome
if mrate == -1:
mrate = 1.0/len(self.genome)
print "mrate is ", mrate

print "original genome: "
print self.genome
# convert genome to a list
gen = [ord(letter)-48 for letter in self.genome]

for letter in range(len(gen)):
rnum = g.random()
if rnum < mrate:
base = g.randint(0,3)
print "base is ", base
print "pos ", letter, gen[letter], " to ", base
# gen[letter] = base

# convert list back to a string
self.genome = ''.join([str(x) for x in gen])
print "final genome"
print self.genome
[...]

Thanks,

Jen


Jen

how about storing the genome data as a list, then converting to a
string if needs be?


import random

class Genome(object):

def __init__(self, genome):
self.genome = list(genome)
self.mrate = 1.0/len(genome)

def __str__(self):
return ''.join(self.genome)

def Mutate(self):
for i in range(len(self.genome)):
if random.random() < self.mrate:
self.genome = str(random.randint(0,2))


print
G = Genome( '10021010212021110' )
print G
G.Mutate()
print G
G.mrate = 0.8
G.Mutate()
print G


Gerard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top