简体   繁体   English

Python:循环未得到期望的结果

[英]Python: Loop not giving desired result

So I'm trying to write a code that simulates a predator and prey situation where it starts off with a low population of predators and high population of prey. 因此,我正在尝试编写代码来模拟捕食者和被捕食者的情况,该情况以低掠食者和高捕食者开始。 Over time the predator population grows while the prey population shrinks until the prey population is too little to sustain the predator population. 随着时间的推移,捕食者的数量会增加,而猎物的数量则会减少,直到猎物的数量变得不足以维持捕食者的数量。 The predator population dies off and then the prey population is able to repopulate. 捕食者的种群死亡,然后猎物种群能够重新繁殖。 The simulation is supposed to stop whenever one of the two populations reach 0, in this case the predator population will and plot two populations over time of the simulation until it stopped. 每当两个种群中的一个种群达到0时,模拟都将停止,在这种情况下,捕食者种群将随着模拟的时间绘制两个种群,直到停止为止。 This is my code so far: 到目前为止,这是我的代码:

import matplotlib.pyplot as plt

def simulate(initialPred, initialPrey, preyGrowth, predationRate, predShrink, predFedBirthRate):
    preyCounts = []
    predatorCounts = []
    predatorI = initialPred
    preyI = initialPrey
    predator = predatorI
    prey = preyI



    while predator > 0 and prey > 0:

            predator = predatorI * (1 - predShrink + predFedBirthRate * preyI)
            prey = preyI * (1 + preyGrowth - predationRate * predatorI)
            predatorCounts.append(predator)
            preyCounts.append(prey)
            predatorI = predator
            preyI = prey


    plt.plot(predatorCounts, 'r', preyCounts, 'b')
    plt.show()      

    return preyCounts, predatorCounts

simulate(50,1000,0.25,0.01,0.05,0.00002)

Its output is this : 其输出是这样的: 在此处输入图片说明

But it's supposed to come out like this: 但是应该这样出来: 在此处输入图片说明

Can someone help me please? 有人能帮助我吗?

*Also aside from this whenever I put my plotting code outside of the function after the function line with values inside like this: *此外,每当我将绘图代码放在函数行后的函数行中,并且内部值像这样时,还可以:

simulate(50,1000,0.25,0.01,0.05,0.00002) 
plt.plot(predatorCounts, 'r', preyCounts, 'b')
plt.show() 

It doesn't plot the values from the function and says predatorCounts and preyCounts are undefined. 它没有绘制函数中的值,并说predatorCountspreyCounts是未定义的。

So you looked at your process/calcs and it seems correct but you look at your result and it's funny. 因此,您查看了您的过程/计算,它看起来是正确的,但是您查看了结果,这很有趣。 One thing you notice when you print the counts... 当您打印计数时,您会注意到一件事...

print predatorI, preyI

is that there are fractions of predators and prey which, in the real world, doesn't make sense. 在现实世界中,有一部分掠食者和猎物没有任何意义。 You are trying to simulate the real world. 您正在尝试模拟现实世界。 All your rate parameters are probably based on whole things, not fractional things. 您所有的费率参数都可能基于整体,而不是小数。 So you decide that there can't be any fractional beings in your simulation and you only deal with whole beings (ints) after the population growth calculations ... 因此,您确定模拟中不能存在任何分数众生,并且仅在人口增长计算之后处理整个众生(整数)... 在此处输入图片说明


Your function returns the count vectors. 您的函数返回计数向量。 If you want to move the plotting statements outside of the function you need to assign the function's return value(s) to a name and then use them for plotting. 如果要将绘图语句移到函数之外,则需要将函数的返回值分配给一个名称,然后使用它们进行绘图。

prey, predator, = simulate(50,1000,0.25,0.01,0.05,0.00002) 
plt.plot(predator, 'r', prey, 'b')
plt.show() 

Here are some things to read from the docs concerning names, scope, namespaces https://docs.python.org/3/tutorial/classes.html#a-word-about-names-and-objects https://docs.python.org/3/reference/executionmodel.html#naming-and-binding 这是从文档中读取的有关名称,范围和名称空间https://docs.python.org/3/tutorial/classes.html#a-word-about-names-and-objects https:// docs的一些信息。 python.org/3/reference/executionmodel.html#naming-and-binding

You might need to read them periodically as you use the language more. 您可能会在使用更多语言时需要定期阅读它们。

If I initialize your plot data with the starting populations and use int() truncation for the populations, I get the plot you say you're supposed to see: 如果我与起始群体使用初始化绘图数据int()截断的人群中,我给你说,你应该看到的情节:

import matplotlib.pyplot as plt

def simulate(initialPred, initialPrey, preyGrowth, predationRate, predShrink, predFedBirthRate):
    preyCounts = [initialPrey]
    predatorCounts = [initialPred]
    predator = initialPred
    prey = initialPrey

    while predator > 0 and prey > 0:
        predatorScaleFactor = 1.0 - predShrink + predFedBirthRate * prey
        preyScaleFactor = 1.0 + preyGrowth - predationRate * predator
        predator = int(predator * predatorScaleFactor)
        prey = int(prey * preyScaleFactor)
        predatorCounts.append(predator)
        preyCounts.append(prey)

    plt.plot(predatorCounts, 'r', preyCounts, 'b')
    plt.show()

    return preyCounts, predatorCounts

simulate(50, 1000, 0.25, 0.01, 0.05, 0.00002)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM