initial population

791 views
Skip to first unread message

David

unread,
Dec 29, 2009, 5:54:56 PM12/29/09
to pyevolve
Hi,
I'm new to pyevolve and it looks really nice.
I'm trying to start with a given population which I want to specify,
instead of a random one. Is that possible with the API or should I
change the source code?
Thanks!!
Dav

Christian S. Perone

unread,
Dec 29, 2009, 7:41:10 PM12/29/09
to pyev...@googlegroups.com
Hello David,
you can use a custom initialization routine, like this used for a TSP problem:

def G1DListTSPInitializator(genome, **args):
   """ The initializator for the TSP """
   lst = [i for i in xrange(genome.getListSize())]
   random.shuffle(lst)
   genome.setInternalList(lst)

and then put it in the genome:

genome.initializator.set(G1DListTSPInitializator)

Yours,
- Christian S. Perone
--
"Forgive, O Lord, my little jokes on Thee, and I'll forgive Thy great big joke on me."

David

unread,
Dec 31, 2009, 4:21:49 PM12/31/09
to pyevolve
Thanks for your quick answer! Unfortunately, it doesn't work in that I
get the following error when the genome.setInternalList(lst) is
executed:

AttributeError: G1DList instance has no attribute 'setInternalList'

Interactive example:

In [1]: from pyevolve import G1DList
In [2]: gen= G1DList.G1DList(4)
In [3]: gen.setInternalList([1,2,3,4])
---------------------------------------------------------------------------
<type 'exceptions.AttributeError'> Traceback (most recent call
last)
/home/davide/<ipython console> in <module>()
<type 'exceptions.AttributeError'>: G1DList instance has no attribute
'setInternalList'

Happy New Year everybody!

;Dav

On Dec 29, 5:41 pm, "Christian S. Perone" <christian.per...@gmail.com>
wrote:


> Hello David,
> you can use a custom initialization routine, like this used for a TSP
> problem:
>
> def G1DListTSPInitializator(genome, **args):
>    """ The initializator for the TSP """
>    lst = [i for i in xrange(genome.getListSize())]
>    random.shuffle(lst)
>    genome.setInternalList(lst)
>
> and then put it in the genome:
>
> genome.initializator.set(G1DListTSPInitializator)
>
> Yours,
> - Christian S. Perone
>

David

unread,
Dec 31, 2009, 6:38:37 PM12/31/09
to pyevolve
Another thing, mumbling before the year ends: if I understand
correctly, this would initialize the genome with a particular list of
genes, which is not what I want: I want to initialize the population,
with particular list of individuals (each with its own value for the
genome).
Happy New Year again!
;David

Amit Saha

unread,
Jan 1, 2010, 12:53:56 AM1/1/10
to pyev...@googlegroups.com
Hello David:

On Fri, Jan 1, 2010 at 5:08 AM, David <davide.d...@gmail.com> wrote:
> Another thing, mumbling before the year ends: if I understand
> correctly, this would initialize the genome with a particular list of
> genes, which is not what I want: I want to initialize the population,
> with particular list of individuals (each with its own value for the
> genome).

I do not think that is possible in any population based approach as
such. The individuals in the population are chosen at random. You can
regulate what the individuals are composed of, but probably not what
your population is composed of. Just curious as what problem you are
trying to solve?

I may be wrong.

Best,
Amit

--
Journal: http://amitksaha.wordpress.com,
µ-blog: http://twitter.com/amitsaha

Freenode: cornucopic in #scheme, #lisp, #math,#linux, #python

David

unread,
Jan 1, 2010, 1:12:56 PM1/1/10
to pyevolve
Thanks your message.

> On Fri, Jan 1, 2010 at 5:08 AM, David <davide.del.ve...@gmail.com> wrote:
> > Another thing, mumbling before the year ends: if I understand
> > correctly, this would initialize the genome with a particular list of
> > genes, which is not what I want: I want to initialize the population,
> > with particular list of individuals (each with its own value for the
> > genome).
>
> I do not think that is possible in any population based approach as
> such.

Why not? I mean, are you talking about GA in general, or pyevolve API?

> The individuals in the population are chosen at random.

I know, but why? I mean, if you don't have anything, than random is
ok, but if you have something to start from....

> You can
> regulate what the individuals are composed of, but probably  not what
> your population is composed of.  Just curious as what problem you are
> trying to solve?

I have a heuristic model (actually three different models, among which
I should find the best) that interacts with stochastic data. I have a
natural score for "how well" the model is doing. Each model has 4-10
parameters, which I want to optimize with genetic algorithms. Now, if
I do that naively (just the default in pyevolve) I start with bad
parameters and evolve just marginally better. Increasing the lenght of
the evolution (even a lot) helps very little. Increasing the size of
the population helps more (especially if I choose a huge population),
but just because there are better individuals at the beginning - the
evolution itself slows down a lot and almost stops (as it is common
with large individuals count).

So, my situation now is that I can easily generate (basically at
random) lot of individuals that are fair/good. I think they can be
improved, and I'd like to improve with a population composed of them
(and possibly some additional random ones), rather than from a
completely random population (and my starting individuals are pretty
diverse, so GA should work fine on them)

I think it makes sense, and it could be useful to others, so if it's
not part of pyevolve API I'm eager to modify it and send a patch, if
Christian want.

Happy New year!
Davide

Amit Saha

unread,
Jan 2, 2010, 3:13:38 AM1/2/10
to pyev...@googlegroups.com
Hello David:

Thanks for the explanation. Now I understand better. Before I try to
give my opinion on your above approach, I am curious: Since you
already know a good starting point, why don't you think of making use
of a local search method such as a Hooke-Jeeve's pattern search- in a
recent work I found it very useful, of course I had coupled it with a
GA. May be you could take a look..

Now, let's do a bit of brainstorming as to how we can generate a
population to our liking: (This is interesting)

I think we can consider a "neighborhood" of arbitrary shape and size
around our starting point. And make sure we get as many individuals,
depending on our desired population size. We can use one of the random
sampling methods, such as a Latin Hypercube Sampling, or similar. I
think that way we can "start off" with a population to our liking.

Does that make any sense? (I hope so:))

Would be interested to know what you think!

Best Regards,
Amit

>
> I think it makes sense, and it could be useful to others, so if it's
> not part of pyevolve API I'm eager to modify it and send a patch, if
> Christian want.
>
> Happy New year!
> Davide
>

--

David

unread,
Jan 3, 2010, 5:46:53 PM1/3/10
to pyevolve
Hi.

> Thanks for the explanation. Now I understand better. Before I try to
> give my opinion on your above approach, I am curious: Since you
> already know a good starting point, why don't you think of making use
> of a local search method such as a Hooke-Jeeve's pattern search- in a
> recent work I found it very useful, of course I had coupled it with a
> GA. May be you could take a look..

Well, I'm not sure that local search is ok. Surely, I'll find the
local minima close to my starting points, but I'd like to better
explore more space for a "global" minimum, possibly better than the
local one(s).

> Now, let's do a bit of brainstorming as to how we can generate a
> population to our liking: (This is interesting)
>
> I think we can consider a "neighborhood" of arbitrary shape and size
> around our starting point. And make sure we get as many individuals,
> depending on our desired population size. We can use one of the random
> sampling methods, such as a Latin Hypercube Sampling, or similar. I
> think that way we can "start off" with a population to our liking.
>
> Does that make any sense? (I hope so:))
> Would be interested to know what you think!

Yes that makes sense. I do not know if it will help or not for my
particular problem, but it surely is what I'd like to have (and more):
having a starting population that is not just random.
Are you just thinking about what would be nice to have, or do you have
in mind any implementation detail?

Thanks and TTYL,
;Dav

Amit Saha

unread,
Jan 3, 2010, 11:59:45 PM1/3/10
to pyev...@googlegroups.com

No, I was just thinking may be that is one way we could approach your
particular requirement. How are you thinking of doing this?

Best,
Amit
>
> Thanks and TTYL,
> ;Dav

Amit Saha

unread,
Jan 6, 2010, 3:06:53 AM1/6/10
to pyev...@googlegroups.com
Hi David:

I just tried the idea of starting the initial population around a set
of known values and it seems to work pretty nicely for some 2 variable
test problems.

I tried it in a RGA code in C, which my laboratory has.

-Amit

Christian S. Perone

unread,
Jan 6, 2010, 11:55:26 AM1/6/10
to pyev...@googlegroups.com
Hello folks,
there is some question about something like this in this thread:
http://groups.google.com/group/pyevolve/browse_thread/thread/2dd03c1a5250a80c/2aa89a97c1b851ba

By the way, Pyevolve really doesn't have a API for doing this (it's planned), but you can do some easy workarounds like using the "stepCallback" function slot to replace the entire population of the GA engine with your own, check the Example 5 on how to use the stepCallback (http://sourceforge.net/apps/trac/pyevolve/browser/pyevolve_ex5_callback.py#L7)

You just need to change it from example to check if the generation is the first generation and then if it is, replace the population by doing:

def evolve_callback(ga_engine):
    generation = ga_engine.getCurrentGeneration()
    if generation == 0:
        pop = ga_engine.getPopulation()
        # CHANGE THE ENTIRE POPULATION "pop"
        newPop.evaluate()
        newPop.sort()
    return False


Yours,
- Christian S. Perone

On Wed, Jan 6, 2010 at 06:06, Amit Saha <amitsaha.in@gmail.com> wrote:
Hi David:

I just tried the idea of starting the initial population around a set
of known values and it seems to work pretty nicely for some 2 variable
test problems.

I tried it in a RGA code in C, which my laboratory has.

-Amit

Amit Saha

unread,
Jan 6, 2010, 12:23:05 PM1/6/10
to pyev...@googlegroups.com
On Wed, Jan 6, 2010 at 1:36 PM, Amit Saha <amits...@gmail.com> wrote:
> Hi David:
>
> I just tried the idea of starting the initial population around a set
> of known values and it seems to work pretty nicely for some 2 variable
> test problems.

Blog post on the experiment:
http://amitksaha.wordpress.com/2010/01/06/genetic-algorithms-localsearch/

-Amit

Christian S. Perone

unread,
Jan 6, 2010, 12:45:56 PM1/6/10
to pyev...@googlegroups.com
Cool, thanks for that Amit.

- Chris

On Wed, Jan 6, 2010 at 15:23, Amit Saha <amitsaha.in@gmail.com> wrote:
On Wed, Jan 6, 2010 at 1:36 PM, Amit Saha <amitsaha.in@gmail.com> wrote:
> Hi David:
>
> I just tried the idea of starting the initial population around a set
> of known values and it seems to work pretty nicely for some 2 variable
> test problems.

Blog post on the experiment:
http://amitksaha.wordpress.com/2010/01/06/genetic-algorithms-localsearch/

-Amit

>
> I tried it in a RGA code in C, which my laboratory has.
>
> -Amit
>

David

unread,
Jan 11, 2010, 12:47:48 PM1/11/10
to pyevolve
Hi Amit,
I am proceeding pretty slowly on this, since I have a couple of other
issues that are higher priority, which arose on Jan 5th.
But I will eventually do it and let you know on this thread.
Thanks for your test!
Davide

On Jan 6, 1:06 am, Amit Saha <amitsaha...@gmail.com> wrote:
> Hi David:
>
> I just tried the idea of starting the initial population around a set
> of known values and it seems to work pretty nicely for some 2 variable
> test problems.
>
> I tried it in a RGA code in C, which my laboratory has.
>
> -Amit
>
>
>

> On Mon, Jan 4, 2010 at 10:29 AM, Amit Saha <amitsaha...@gmail.com> wrote:

David

unread,
Jan 11, 2010, 12:48:08 PM1/11/10
to pyevolve
Hi Amit,
I am proceeding pretty slowly on this, since I have a couple of other
issues that are higher priority, which arose on Jan 5th.
But I will eventually do it and let you know on this thread.
Thanks for your test!
Davide

On Jan 6, 1:06 am, Amit Saha <amitsaha...@gmail.com> wrote:

> Hi David:
>
> I just tried the idea of starting the initial population around a set
> of known values and it seems to work pretty nicely for some 2 variable
> test problems.
>
> I tried it in a RGA code in C, which my laboratory has.
>
> -Amit
>
>
>

> On Mon, Jan 4, 2010 at 10:29 AM, Amit Saha <amitsaha...@gmail.com> wrote:

Yibam

unread,
Jan 13, 2010, 12:51:20 PM1/13/10
to pyevolve
Hi, my name is Yibam.

Im a spanish man. Sorry by my bad english.

I need for my research, begin with a population controled.

the idea with set_callback is good but the code has a mistake in the
line

if generation == 0:

Genetation 0 dont exits, je je, you must write:

if generation == 1:

By the oder site, could you show me, how i can put en newpop the news
individuals ???

newpop[0] = [ 1, 2, 3, 4] ???

or i need use copy() ??

Thanks a lot.

Christian S. Perone

unread,
Jan 27, 2010, 6:59:31 PM1/27/10
to pyev...@googlegroups.com
Hello Yibam, sorry for the long delay to answer your question.
To add new individuals in the population you must do something like this:

newpop[0] = individual

the individual object must be a valid individual instance, you can use the population.oneSelfGenome.clone() to get one for example, the oneSelfGenome is the sample genome. Or you can alternatively instantiate and initialize a new individual.

Yours,
- Chris
--
"Forgive, O Lord, my little jokes on Thee, and I'll forgive Thy great big joke on me."
http://pyevolve.sourceforge.net/wordpress/

Paulo Almeida

unread,
Feb 3, 2014, 12:37:45 AM2/3/14
to pyev...@googlegroups.com
Just trying to help, after reading this thread and having had difficulties to figure out how to initialize my population, I eventually found out a way to do it in my case, and I am showing below to ilustrate:

# Below is how I implemented code for "CHANGE THE ENTIRE POPULATION 'pop'", please see posts above.
# OBS: in my case I was using G1DBinaryString with size 8, but the same methods will work for other kinds of lists

pop = ga_engine.getPopulation()

pop[0].setInternalList([1,0,1,1,1,1,0,1])
pop[1].setInternalList([0,0,1,0,0,1,0,0])
...

Hope this helps someone, I took some time to figure this out.
Cheers,
  Paulo
Reply all
Reply to author
Forward
0 new messages