473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Psychic bug

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

Feb 22 '06 #1
3 1095
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 <al***************@gmail.com>
-
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
Feb 22 '06 #2
> 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

Feb 23 '06 #3
Jennifer Hallinan wrote:
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[i] = str(random.randint(0,2))
print
G = Genome( '10021010212021110' )
print G
G.Mutate()
print G
G.mrate = 0.8
G.Mutate()
print G
Gerard

Feb 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

467
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
188
by: Ilias Lazaridis | last post by:
I'm a newcomer to python: - E01: The Java Failure - May Python Helps? http://groups-beta.google.com/group/comp.lang.python/msg/75f0c5c35374f553 - I've download (as suggested) the python...
13
by: owen_meany | last post by:
I just installed SP2. now my .asp scripts won't run. .aspx scripts run fine. It's just .asp for some reason. I keep getting the following error: "Either a required impersonation level was...
40
by: Seth Brundle | last post by:
I am surprised that no one has yet developed an HTML variant which uses abbreviated tagging. Maybe they have, I dunno. HTML is very verbose - this was important to its adoption, as reading...
0
by: uncle jenny | last post by:
something right out of a box +++ eminem 'lose yourself' +++ "thanks paulie" yur welcome
4
by: William Sullivan | last post by:
I have an extremely weird problem that I have no idea how to approach. I have a simple page with a search textbox and a search button. The button causes a postback, where I perform the search and...
0
by: baseballswim | last post by:
Friend, I'm always looking for good and intelligent individuals like you to visit my website, www.ChezBrandon.com , and it has pictures of beautiful women, information about aged clones, and a...
0
by: yuying611 | last post by:
Here you can see a lot of incredible psychic picture! Click here: http://www.flixya.com/photos/u/superphoto
1
by: Barfy the Wonder Camel | last post by:
OK, here it is. I wanted to add a DateTimePicker column to the DataGridView at design time, which means it would be available as a column type when laying out a dgv. I got some source code from...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.