首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:'float‘和'function’的实例之间不支持'<‘

TypeError:'float‘和'function’的实例之间不支持'<‘
EN

Stack Overflow用户
提问于 2017-12-08 21:23:38
回答 1查看 8.7K关注 0票数 1

我正在尝试创建一个非常简单的病毒修复模拟。我创建了两个文件(infection.py和medicine.py),一个模拟病毒,另一个模拟非常简单的修复。第一个程序本身运行得很好。它返回一个列表,其中包含每个时间单位的病毒数量。第二个程序导入了第一个程序,并建议创建某种cure。我主要使用程序1中的预定义函数,并重新定义了simulate()

为了理解这个程序:“治疗”在timestep = 100之后开始,这是在病毒被诊断出来之后。病毒只有在对治疗有抵抗力的情况下才能复制,换句话说,如果病毒含有'AAA‘。病毒可以死亡、繁殖和变异。突变有时会导致抵抗力。

一切都应该正常工作(理论上?),但是在运行程序2时,我一次又一次地收到相同的错误消息:TypeError: '<' not supported between instances of 'float' and 'function'。我知道这是什么意思,但它说错误来自程序1中预定义的函数。但是程序1运行这个函数没有任何困难。我尝试存储random.random()的值,然后使用它,但这不起作用。这是我的代码:

计划1

代码语言:javascript
复制
import random

def generateVirus(length):
    return ''.join([random.choice(['A', 'G', 'T', 'C']) for i in range(length)])

def mutate(virus):
    rand = random.randint(0, len(virus)-1)
    return virus[:rand] + random.choice([i for i in 'AGTC' if i != virus[rand]]) + virus[rand+1:]

def kill(viruses, mortalityProb):
    return [survivors for survivors in viruses if random.random() > mortalityProb]

def reproduce(viruses, mutationProb, reproductionProb):
    nextgen = []
    for i in viruses:
        nextgen.append(i)
        if random.random() < reproductionProb:
            if random.random() < mutationProb:
                nextgen.append(mutate(i))
            else:
                nextgen.append(i)
    return nextgen

def reproductionProbability(viruses, maxReproductionProb, maxPopulation):
    return (1 - (len(viruses) / maxPopulation)) * maxReproductionProb

def simulate(viruses, mortalityProb, mutationProb, maxReproductionProb, 
    maxPopulation, timesteps = 500):
    pop_size = []
    while timesteps > -1:
        survivors = kill(viruses, mortalityProb)
        reproductionProb = reproductionProbability(survivors, maxReproductionProb, maxPopulation)
        viruses = reproduce(survivors, mutationProb, reproductionProb)
        pop_size.append(len(viruses))
        timesteps -= 1
    return pop_size

print(simulate(['GCTCC', 'CCGG', 'AACCGG', 'CCCTATAGG'], 0.05, 0.1, 0.07, 1000))

节目2

代码语言:javascript
复制
import infection

def isResistent(virus):
    if virus.find('AAA') > -1:
        return True
    else:
        return False

def simulate(viruses, mortalityProb, mutationProb, maxReproductionProb, maxPopulation, timesteps = 500):
    activation_cure = 400
    while timesteps > -1:
        survivors = infection.kill(viruses, mortalityProb)
        for virus in viruses:
            if timesteps < activation_cure and isResistent(virus):
                reproductionProb = infection.reproductionProbability
                infection.reproduce(viruses, mutationProb, reproductionProb)
        timesteps -= 1
    return len(viruses)

def experiment(numberOfPatients):
    cured = 0
    viruses = []
    for i in range(10):
        viruses.append(infection.generateVirus(16))
    for i in range(numberOfPatients):
        remaining_virus = simulate(viruses, 0.05, 0.1, 0.07, 1000)
        if remaining_virus[len(remaining_virus)-1] == 0:
            cured += 1
    return cured

print(experiment(5))

完整的错误消息

代码语言:javascript
复制
File "C:\something\workspace\infection.py", line 17, in reproduce
if random.random() < reproductionProb:
TypeError: '<' not supported between instances of 'float' and 'function'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-08 21:34:57

错误消息很清楚:这一行(第17行)是问题所在:

代码语言:javascript
复制
 if random.random() < reproductionProb

random.random()是一个浮点型,reproductionProb是一个函数句柄。不能将浮点数与函数进行比较。

代码语言:javascript
复制
reproductionProb = infection.reproductionProbability

是传递函数句柄而不是浮点数的调用。

此外,您还需要确保将main函数封装为:

代码语言:javascript
复制
if __name__=='__main__':

如果导入文件,这会阻止调用导入文件的main函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47715289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档