Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.4遗传算法,除掉余数_Python_Genetic Algorithm - Fatal编程技术网

Python 3.4遗传算法,除掉余数

Python 3.4遗传算法,除掉余数,python,genetic-algorithm,Python,Genetic Algorithm,我正在用python编写一个简单的遗传算法,当我试图找到一组单倍体的平均适应度时,它给了我剩下的部分,我不明白它为什么这样做。以下是完整的来源 import random def generate_random_haploid(haploid_length): haploid = [] for x in range(haploid_length): haploid.append(random.randint(0,1)) return haploid

我正在用python编写一个简单的遗传算法,当我试图找到一组单倍体的平均适应度时,它给了我剩下的部分,我不明白它为什么这样做。以下是完整的来源

import random

def generate_random_haploid(haploid_length):
    haploid = []
    for x in range(haploid_length):
        haploid.append(random.randint(0,1))

    return haploid

def crossover_haploid(haploid_1, haploid_2):
    locus = random.randint(1, len(haploid_1))

    for x in range(locus - 1):
        haploid_1[x] = haploid_2[x]

    for x in range(len(haploid_2) - locus):
        haploid_2[x + locus] = haploid_1[x + locus]

    return [haploid_1, haploid_2]

def crossover_diploid(diploid_1, diploid_2):
    children_1, children_2 = crossover_haploid(diploid_1[0], diploid_1[1]), crossover_haploid(diploid_2[0], diploid_2[1])
    return crossover_haploid(children_1[0], children_2[1])

def flipbit(bit):
    if bit == 1:
        bit = 0
    elif bit == 0:
        bit = 1
    return bit

def mutate_haploid(haploid, mutate_prob):
    for x in haploid:
        if random.randint(0, mutate_prob) <= mutate_prob:
            haploid[x] = flipbit(haploid[x])

    return haploid

def average_fitness(haploid_list):
    return sum(haploid_list[0]) / len(haploid_list)

def fitness(haploid):
    fitness = 0
    for x in range(len(haploid)):
        if haploid[x] == 1:
            fitness += 1
    return fitness

def print_haploid(haploid):
    print(haploid, "Fitness: ", fitness(haploid))

x = generate_random_haploid(4)
y = generate_random_haploid(4)
print_haploid(x)
print_haploid(y)
print("-------------------------------")
children = crossover_haploid(x, y)
print_haploid(children[0])
print_haploid(children[1])
print("-------------------------------")
print("Parent Fitness: ", average_fitness([x, y]) )
print("-------------------------------")
print("Children Fitness: ", average_fitness([children[0], children[1]]) )
随机导入
def生成随机单倍体(单倍体长度):
单倍体=[]
对于范围内的x(单倍体长度):
单倍体附加(随机随机随机(0,1))
返回单倍体
def交叉_单倍体(单倍体_1,单倍体_2):
位点=random.randint(1,len(单倍体_1))
对于范围内的x(轨迹-1):
单倍体_1[x]=单倍体_2[x]
对于范围内的x(len(单倍体_2)-基因座):
单倍体_2[x+位点]=单倍体_1[x+位点]
return[haploid_1,haploid_2]
def交叉_二倍体(二倍体_1,二倍体_2):
儿童_1,儿童_2=交叉_单倍体(二倍体_1[0],二倍体_1[1]),交叉_单倍体(二倍体_2[0],二倍体_2[1])
返回交叉_单倍体(子_1[0],子_2[1])
def翻转位(位):
如果位==1:
位=0
elif位==0:
位=1
返回位
def突变单倍体(单倍体,突变探针):
对于单倍体中的x:

如果random.randint(0,mutate_prob)这是因为python integer要向下截断到最低值。你有很多选择来解决这个问题:

>>> 5 / 2
2
选择1 将其中一个值强制转换为float,python将自动向上转换所有其他int类型

>>> 5 / 2
2

>>> float(5) / 2
2.5
对代码的更正:

def average_fitness(haploid_list):
    return float(sum(haploid_list[0])) / len(haploid_list)
def average_fitness(haploid_list):
    return float(sum(haploid_list[0])) // len(haploid_list)
选择2 将此添加到脚本的顶部

from __future__ import division
现在,
5/2
将产生
2.5
,您无需更改
平均适应度
方法,如选项1所示。
\uuuuuuuuuuuuuuuuuuu
是指python3,默认情况下,
/
操作符执行浮点除法。通过导入该功能,您现在将在代码中的任何地方使用
浮点
除法运算符
/
,而不是python2的
int
除法运算符

选择3 您可以用
/
运算符替换
/

>>> 5 / 2
2

>>> 5 // 2
2.5
对代码的更正:

def average_fitness(haploid_list):
    return float(sum(haploid_list[0])) / len(haploid_list)
def average_fitness(haploid_list):
    return float(sum(haploid_list[0])) // len(haploid_list)

您确定使用的是Python 3吗?因为在Python2中,
5/2==2
但在Python3中,
5/2==2.5
。这解决了您的问题吗?我可以在空闲环境中进行浮点除法,但不能在命令提示符下进行,我安装了多个版本的Python,并且我认为它默认为系统路径中的最后一个版本,即Python2.7。6@SchighSchagh对我认为他假设了错误的版本。@SchighSchagh如果我使用Python2,打印语句还能工作吗?@Cayle我认为你的python解释器在回答这个问题方面应该比我或这里的任何人都要好得多。