【问题标题】:Creating a very simple 'evolutionary' algorithm in python在 python 中创建一个非常简单的“进化”算法
【发布时间】:2018-04-28 09:07:58
【问题描述】:

我正在尝试在 python 中创建一个非常简单的“进化”算法。

我最初想创建一个大约 100 个具有四个数字属性 (a1-4) 的个体,使用一个函数从这些属性中获取分数,然后删除得分最差的 20 个个体。

这是我目前所拥有的

import random
population = 100

class Individual(object):
    def __init__(self, a1, a2, a3, a4):
        self.a1 = a1
        self.a2 = a2
        self.a3 = a3
        self.a4 = a4

starting_population = list()

for i in range (population):
    a1 = random.randint(1,10)
    a2 = random.randint(1,10)
    a3 = random.randint(1,10)
    a4 = random.randint(1,10)
    starting_population.append(Individual(a1,a2,a3,a4))

def fitness(x):
    fitness = a1*a2/a3*a4
    return fitness

我不知道如何将函数应用于人口列表的成员?

另外,我对 Python 很陌生,而且我确信我做的一些事情很糟糕,所以非常感谢任何提示!

谢谢

【问题讨论】:

  • 注意random.seed(somenumber) 通常在随机调用之前调用。另外,您想要一个新的fitness 列表,还是添加一个属性?

标签: python python-3.x list evolutionary-algorithm


【解决方案1】:

循环有什么问题?

for person in starting_population:
    person.fitness = person.a1*person.a2/person.a3*person.a4 #Add fitness to object

还要注意,操作顺序是:

((a1*a2)/a3)*a4)

如果你的意思不同。您可以考虑将健身作为个人的方法:

class Individual(object):
    def __init__(self, a1, a2, a3, a4):
        self.a1 = a1
        self.a2 = a2
        self.a3 = a3
        self.a4 = a4

    def fitness(self,x):
        fitness = self.a1*self.a2/self.a3*self.a4
        return fitness

starting_population = list()   
for i in range (population):
    a1 = random.randint(1,10)
    a2 = random.randint(1,10)
    a3 = random.randint(1,10)
    a4 = random.randint(1,10)
    starting_population.append(Individual(a1,a2,a3,a4))

这样您就可以立即调用starting_population[i].fitness(),或者计算__init__ 中的值并使其成为一个字段。

另一种解决方案,放弃代码的对象清晰度以支持numpy 数组以提高速度:

import numpy.random as rnd
rnd.seed(78943598743)
starting_population=rnd.randint(1,10,size=100*4).reshape(100,4) #100 rows, 4 columns, each row a person
fitness_vector = starting_population[:,0]*starting_population[:,1]/starting_population[:,2]*starting_population[:,3]

【讨论】:

    【解决方案2】:

    首先,您应该将fitness 设为Individual 的方法:

    import random
    population = 100
    
    class Individual(object):
        def __init__(self, a1, a2, a3, a4):
            self.a1 = a1
            self.a2 = a2
            self.a3 = a3
            self.a4 = a4
    
        def fitness(self):
            fitness = self.a1*self.a2/self.a3*self.a4
            return fitness
    
    starting_population = list()
    
    for i in range (population):
        a1 = random.randint(1,10)
        a2 = random.randint(1,10)
        a3 = random.randint(1,10)
        a4 = random.randint(1,10)
        starting_population.append(Individual(a1,a2,a3,a4))
    

    如果要删除得分最低的 20 个,首先按适应度对它们进行排序,然后从列表中抽取一部分:

    sorted_people = sorted(starting_population, key=lambda i:i.fitness())
    fit_people = sorted_people[20:]
    

    您还可以根据它们的适应度值过滤它们,就像这样,使用列表推导:

    fit_people = [i for i in starting_population if i.fitness() > 0.5]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2011-07-27
      • 2016-07-16
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      相关资源
      最近更新 更多