Re: Roulette wheel

2009-03-19 Thread Gabriel Genellina
En Thu, 19 Mar 2009 08:06:35 -0200, mattia escribió: OK, understood. Now, as a general rule, is it correct to say: - use generator expression when I just need to iterate over the list or call a function that involve an iterator (e.g. sum) and get the result, so the list is not necessary anymore

Re: Roulette wheel

2009-03-19 Thread mattia
Il Wed, 18 Mar 2009 23:31:09 -0200, Gabriel Genellina ha scritto: > En Wed, 18 Mar 2009 18:49:19 -0200, mattia escribió: >> Il Wed, 18 Mar 2009 13:20:14 -0700, Aahz ha scritto: >>> In article <49c1562a$0$1115$4fafb...@reader1.news.tin.it>, mattia >>> wrote: Yeah, and I believe that we

Re: Roulette wheel

2009-03-18 Thread Gabriel Genellina
En Wed, 18 Mar 2009 18:49:19 -0200, mattia escribió: Il Wed, 18 Mar 2009 13:20:14 -0700, Aahz ha scritto: In article <49c1562a$0$1115$4fafb...@reader1.news.tin.it>, mattia wrote: Yeah, and I believe that we can say the same for: 1 - t = [x*2 for x in range(10)] 2 - t = list(x*2 for x in rang

Re: Roulette wheel

2009-03-18 Thread mattia
Il Wed, 18 Mar 2009 13:20:14 -0700, Aahz ha scritto: > In article <49c1562a$0$1115$4fafb...@reader1.news.tin.it>, mattia > wrote: >> >>Yeah, and I believe that we can say the same for: 1 - t = [x*2 for x in >>range(10)] >>2 - t = list(x*2 for x in range(10)) >>or not? > > The latter requires ge

Re: Roulette wheel

2009-03-18 Thread Aahz
In article <49c1562a$0$1115$4fafb...@reader1.news.tin.it>, mattia wrote: > >Yeah, and I believe that we can say the same for: >1 - t = [x*2 for x in range(10)] >2 - t = list(x*2 for x in range(10)) >or not? The latter requires generator expressions, which means it only works with Python 2.4 or h

Re: Roulette wheel

2009-03-18 Thread mattia
Il Wed, 18 Mar 2009 09:34:57 -0700, Aahz ha scritto: > In article , Peter Otten > <__pete...@web.de> wrote: >>mattia wrote: >>> >>> cpop += [nchromosome1] + [nchromosome2] >> >>I'd write that as >> >>cpop.append(nchromosome1) >>cpop.append(nchromosome2) >> >>thus avoiding the intermediate

Re: Roulette wheel

2009-03-18 Thread Aahz
In article , Peter Otten <__pete...@web.de> wrote: >mattia wrote: >> >> cpop += [nchromosome1] + [nchromosome2] > >I'd write that as > >cpop.append(nchromosome1) >cpop.append(nchromosome2) > >thus avoiding the intermediate lists. You could also write it as cpop += [nchromosome1, nchromo

Re: Roulette wheel

2009-03-05 Thread Peter Pearson
On 05 Mar 2009 20:43:41 GMT, mattia wrote: >> for a, b in zip(*[iter(pop)]*2): > In the python documentation there is a similar example, well, the obscure > thing here is the usage of *[iter(pop)]! Then I believe that I can safely > say that you iterate over the values 0 and 1, 2 and 3 etc.

Re: Roulette wheel

2009-03-05 Thread mattia
Il Thu, 05 Mar 2009 18:07:29 +0100, Peter Otten ha scritto: > mattia wrote: > >> The last question: how can I improve readability in this piece of code? >> >> def crossover(pop, prob=0.6): >> """ >> With a crossover probability cross over the parents to form new >> offspring. If no c

Re: Roulette wheel

2009-03-05 Thread mattia
> for a, b in zip(*[iter(pop)]*2): In the python documentation there is a similar example, well, the obscure thing here is the usage of *[iter(pop)]! Then I believe that I can safely say that you iterate over the values 0 and 1, 2 and 3 etc. because the zip automatically updates the index, i

Re: Roulette wheel

2009-03-05 Thread mattia
ssical genetic algorithm example: given a >>> population of chromosomes, encoded using 1 and 0, find the chromosome >>> with the maximum number of 1s. Now, despite all the code used to >>> implement the solution, I'm wondering if there is a better way to use >>> the s

Re: Roulette wheel

2009-03-05 Thread Peter Pearson
On Thu, 05 Mar 2009 18:07:29 +0100, Peter Otten <__pete...@web.de> wrote: [snip] > > Just for fun here's an alternative version of your function > > def generate_crossover(pop, prob): > for a, b in zip(*[iter(pop)]*2): . . . Good grief! That's devilishly obscure. -- To email me, substitute

Re: Roulette wheel

2009-03-05 Thread Scott David Daniels
mattia wrote: ... The last question: how can I improve readability in this piece of code? def crossover(pop, prob=0.6): """ With a crossover probability cross over the parents to form new offspring. If no crossover was performed, offspring is the exact copy of parents. """ c

Re: Roulette wheel

2009-03-05 Thread Peter Otten
mattia wrote: > The last question: how can I improve readability in this piece of code? > > def crossover(pop, prob=0.6): > """ > With a crossover probability cross over the parents to form new > offspring. If no crossover was performed, offspring is the exact copy > of parents. "

Re: Roulette wheel

2009-03-05 Thread mattia
1 and 0, find the chromosome >> with the maximum number of 1s. Now, despite all the code used to >> implement the solution, I'm wondering if there is a better way to use >> the so-called roulette wheel selection in this problem. Here I paste >> the code of my solution, a

Re: Roulette wheel

2009-03-05 Thread mattia
Il Thu, 05 Mar 2009 12:54:39 +0100, Peter Otten ha scritto: > mattia wrote: > >> Il Thu, 05 Mar 2009 10:46:58 +0100, Peter Otten ha scritto: >> >>> mattia wrote: >>> > Note how get_roulette_wheel() is now completeley independent of the > concrete problem you are using it for.

Re: Roulette wheel

2009-03-05 Thread Peter Otten
mattia wrote: > Il Thu, 05 Mar 2009 10:46:58 +0100, Peter Otten ha scritto: > >> mattia wrote: >> Note how get_roulette_wheel() is now completeley independent of the concrete problem you are using it for. >>> >>> Ok, but also a lot more memory consuming ;-) >> >> I don't think so. Py

Re: Roulette wheel

2009-03-05 Thread mattia
Il Thu, 05 Mar 2009 10:46:58 +0100, Peter Otten ha scritto: > mattia wrote: > >>> Note how get_roulette_wheel() is now completeley independent of the >>> concrete problem you are using it for. >> >> Ok, but also a lot more memory consuming ;-) > > I don't think so. Python references objects; th

Re: Roulette wheel

2009-03-05 Thread Peter Otten
mattia wrote: >> Note how get_roulette_wheel() is now completeley independent of the >> concrete problem you are using it for. > > Ok, but also a lot more memory consuming ;-) I don't think so. Python references objects; therefore the list [tiny_little_thing]*N does not consume more memory th

Re: Roulette wheel

2009-03-04 Thread mattia
> Note how get_roulette_wheel() is now completeley independent of the > concrete problem you are using it for. Ok, but also a lot more memory consuming ;-) -- http://mail.python.org/mailman/listinfo/python-list

Re: Roulette wheel

2009-03-04 Thread mattia
1 and 0, find the chromosome >> with the maximum number of 1s. Now, despite all the code used to >> implement the solution, I'm wondering if there is a better way to use >> the so-called roulette wheel selection in this problem. Here I paste >> the code of my solution, a

Re: Roulette wheel

2009-03-04 Thread Peter Otten
the code used to implement the > solution, I'm wondering if there is a better way to use the so-called > roulette wheel selection in this problem. Here I paste the code of my > solution, any advice will be helpful: Your code looks good to me. > from random import randint, random &

Re: Roulette wheel

2009-03-04 Thread Tim Wintle
On Wed, 2009-03-04 at 18:02 +, mattia wrote: > ri = randint(0, len(rw) - 1) > print("Random index:", rw[ri], ", value:", pop[rw[ri]]) you probably want random.choice(rw) -- http://mail.python.org/mailman/listinfo/python-list

Roulette wheel

2009-03-04 Thread mattia
ution, I'm wondering if there is a better way to use the so-called roulette wheel selection in this problem. Here I paste the code of my solution, any advice will be helpful: from random import randint, random def create_chromosome(min, max, length): chromosome = [] for i in ra