Simultaneous evaluation of fitness function with pyevolve

171 views Asked by At

I'm am using pyevolve and I would like to give a fitness score based on the whole population. Nevertheless in the evaluation function needs to be defined for one individual like:

 def eval_func(ind):
     score = 0.0
     for x in xrange(0,len(ind)):
     if ind[x] <= 0.0: score += 0.1
     return score

But I would like to have a function defined for the whole population at once.

 def eval_func_total_population(population):
     # evaluation score depends on whole population
     pop_sort = sorted(population)
     for ind in population:
         ind.score = pop_sort.index(ind)
     return

Because the evaluation function is evaluated in the GSimpleGA.evolve and GSimpleGA.step function I thought it would be an option to make a new GSimpleGA class using my own evaluation function like:

 class My_GSimpleGA(GSimpleGA.GSimpleGA): 
    def __init__(self,genome):
        GSimpleGA.GSimpleGA.__init__(self,genome)

    def evolve(self, freq_stats=0):
        (...)
        # change this line:
        self.internalPop.evaluate()
        # to this line:
        eval_func_total_population(self.internalPop)

This actually seems to work, but I am wondering if a more straightforward options would be possible.

0

There are 0 answers