[Tutor] Problems with a Class
thomas coopman
thomas.coopman at gmail.com
Sat Jan 13 13:12:51 CET 2007
On Sat, 13 Jan 2007 01:08:53 +0100
Carlos <carloslara at web.de> wrote:
> Hello,
>
> I'm just about to finish my project, but something is causing me
> trouble. I need to find out how to get some information out of the
> genetic algorithm that Im using. This algorithm is generating some
> coordinates for a collection of objects, until now I needed only the
> final result of the process. But I discovered that the final
> information is not enough, I need to get some intermediate data to
> actualize the positions of the objects and then evaluate those
> positions.
>
> This is the area of the algorith that I think holds the info that I
> need:
>
> def evolve(self, generations = 100):
>
> self.halt_reached = 0
>
> for gen in range(generations):
>
> #Changed.
> print 'GEN:',gen, self.entities
>
> self.do_mutation()
> self.do_crossover()
> print "Average fitness generation " + str(gen) + ": " +
> str(self.avg_fitness())
> if self.debug == 1:
> self.echo_fitness()
> if self.halt >= 0:
> max_entity = self.get_max_fitness()
> fit = self.calc_fitness(max_entity)
> if fit >= halt:
> self.halt_reached = 1
> return [max_entity]
>
> The line marked as #Changed is what I changed, with this I was able
> to print the info, but I'm sorry to say that I have no clue at how to
> apply something like return() that would give me this self.entities
> list. As you can see there is a for loop and I need the info every
> time that it gets updated by the loop.
>
> Hopefully I'm making myself clear and the info is enough.
>
> Thanks for your help in advance
> Carlos
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
Hi,
when you call return, you return from the function so
when you have something like this:
def loop():
for i in range(10):
return i
print loop()
This will print just 0.
when you want to get all the numbers, you can't use return in the loop
you could save them in a list or something, and after the loop return
the list.
def loop():
x = []
for i in range(10):
x.append(i)
return x
print loop()
this will print a list with all the numbers.
I hope this is what you mean.
Thomas
More information about the Tutor
mailing list