Python 用遗传算法打印获奖者?

Python 用遗传算法打印获奖者?,python,algorithm,genetic,Python,Algorithm,Genetic,我正在查看我找到的这段代码,但我不知道如何打印获奖者。 从本质上说,我试图产生一种遗传算法,它将产生一个N个数字的个体,其总和为X。 这个问题是在勒钦身上提出来的:但我似乎不知道如何打印出进化的结果 下面是一些示例代码: from random import randint, random from operator import add target = 5 #this defines the target number we are trying to reach

我正在查看我找到的这段代码,但我不知道如何打印获奖者。 从本质上说,我试图产生一种遗传算法,它将产生一个N个数字的个体,其总和为X。 这个问题是在勒钦身上提出来的:但我似乎不知道如何打印出进化的结果

下面是一些示例代码:

    from random import randint, random
    from operator import add
    target = 5 #this defines the target number we are trying to reach
    p_count = 100 #this defines the number of individuals available to                                        mutate for the desired result
    i_length = 6 #this defines the length of each individual, the number of intergers in it
    i_min = 0 #this defines the minimum value of an integer in the individual
    i_max = 100 #this defines the maximum value of an integer in the individual
    def individual(length, min, max):
        return [ randint(min,max) for x in xrange(length) ] #Creates an individual. Defines an individual by the number of numbers contained in it and their min/max
    def population(count, length, min, max):
        return [ individual(length, min, max) for x in xrange(count) ] #Creates population from individuals. This sets the number of individuals in the    population (count), the number of numbers in each individual (length), and the min and max numbers for the individual        
    def fitness(individual, target):
        sum = reduce(add, individual, 0)
        return abs(target-sum) #determines the fitness. does this by adding the indivduals numbers together, and taking that value from the target value 
    def grade(pop, target):
        summed = reduce(add, (fitness(x, target) for x in pop))
        return summed / (len(pop) * 1.0) #This returns an average fitness to compare populations to.
    def evolve(pop, target, retain=0.2, random_select=0.05, mutate=0.01): #This evolves a population, retaining 20%, randomly selecting 5% so it doesnt get stuck/lose variance, and mutates 1%.
        graded = [ (fitness(x, target), x) for x in pop]
        graded = [ x[1] for x in sorted(graded)]
        retain_length = int(len(graded)*retain)
        parents = graded[:retain_length]
        for individual in graded[retain_length:]:
            if random_select > random():
                parents.append(individual)
        for individual in parents:
            if mutate > random():
                pos_to_mutate = randint(0, len(individual)-1)
                individual[pos_to_mutate] = randint(
                    min(individual), max(individual))
        parents_length = len(parents)
        desired_length = len(pop) - parents_length
        children = []
        while len(children) < desired_length:
            male = randint(0, parents_length-1)
            female = randint(0, parents_length-1)
            if male != female:
                male = parents[male]
                female = parents[female]
                half = int(len(male) / 2)
                child = male[:half] + female[half:]
                children.append(child)
        parents.extend(children)
        return parents
从随机导入randint,随机
从操作员导入添加
目标=5#这定义了我们试图达到的目标数量
p_count=100#这定义了可用于突变以获得所需结果的个体数量
i_length=6#这定义了每个个体的长度以及其中的整数数量
i_min=0#这定义了个体中整数的最小值
i_max=100#这定义了个体中整数的最大值
def单个(长度、最小值、最大值):
返回[x范围(长度)中x的randint(最小值,最大值)]#创建一个个体。根据其中包含的数字数量及其最小/最大值定义个人
def填充(计数、长度、最小值、最大值):
return[x范围内x的个体(长度、最小值、最大值)(计数)]#从个体创建总体。这将设置总体中的个体数量(计数)、每个个体中的个体数量(长度)以及个体的最小和最大数量
def适合度(个人、目标):
总和=减少(相加,单个,0)
返回abs(目标和)#确定适合度。通过将独立数字相加,并从目标值中获取该值来实现此目的
def等级(pop、目标):
求和=减少(添加,(pop中x的适合度(x,目标))
return summated/(len(pop)*1.0)#这将返回一个平均适合度来比较种群。
def进化(pop,target,retain=0.2,random_select=0.05,mutate=0.01):#这进化出一个种群,保留20%,随机选择5%,这样它就不会卡住/丢失方差,并变异1%。
分级=[(适用性(x,目标),x)适用于pop中的x]
分级=[x[1]表示已排序(分级)中的x]
保留长度=整数(长度(分级)*保留)
父母=分级[:保留长度]
对于分级的个人[保留长度:]:
如果随机选择>随机()
父母。附加(个人)
对于父母中的个人:
如果mutate>random():
pos_to_mutate=randint(0,len(个体)-1)
个体[pos_to_mutate]=randint(
最小值(单个)、最大值(单个))
父项长度=len(父项)
所需长度=长度(pop)-父项长度
儿童=[]
而len(儿童)<所需长度:
男性=randint(0,父母长度-1)
女性=randint(0,双亲长度-1)
如果是男性!=女性:
男=父母[男]
女性=父母[女性]
一半=整数(长(公)/2)
儿童=男性[一半]+女性[一半]
children.append(child)
父母(子女)
回归父母

任何帮助都将不胜感激

如果我正确理解您,您希望打印符合您标准的候选解决方案(如高于特定适用性) 如果是的话 你可以:


算法进化并没有给你一个个体,它给你一个填充。你的代码片段只包含辅助函数,算法缺失。。。你可以从一个群体开始(通过调用
群体
),然后多次进化(通过调用
进化
)。在某个时候,你可能会选择群体中最好的个体。看起来你必须反复调用
evolve()
,然后检查群体中是否有个体达到了目标
   def fitness(individual, target):
    sum = reduce(add, individual, 0)
    if sum >= target:
      print individual
    return abs(target-sum)