1

我正在使用 pyevolve,我想根据整个人群给出一个健身分数。然而,在评估函数中需要为一个人定义,例如:

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

但我想立即为整个人口定义一个函数。

 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

因为评估函数是在 GSimpleGA.evolve 和 GSimpleGA.step 函数中评估的,所以我认为使用我自己的评估函数创建一个新的 GSimpleGA 类是一种选择,例如:

 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)

这实际上似乎有效,但我想知道是否有更直接的选择。

4

0 回答 0