2015-02-06 22 views
1

我正在做一個捕食獵物模擬,打印在一定時期內的獵物和捕食者的數量。使用Python捕食者 - 獵物模擬。 Python閱讀方程有問題嗎?

這是我第一次發佈,所以如果有任何問題,請讓我知道。

我在執行測試的信息是:

a= .1 
b= .01 
c= .01 
d= .00002 
prey_population = 1000 
predator_population = 20 
periods = 10 

a = float(input("Enter the rate at which prey birth exceeds natural death: ")) 
b = float(input("Enter the rate of predation: ")) 
c = float(input("Enter the rate at which predator deaths exceed births without food: ")) 
d = float(input("Predator increase with the presence of food: ")) 

prey_population = int(input("Enter prey population: ")) 
predator_population = int(input("Enter predator population: ")) 

periods = int(input("Enter the number of periods: ")) 

for i in range(1, periods + 1): 
    prey_population = int(prey_population * (1 + a - b * predator_population)) 
    predator_population = int(predator_population * (1 - c + d * prey_population)) 

    print("After period", i, "there are", predator_population, "predators") 
    print("After period", i, "there are", prey_population, "prey") 

我的信息是準確的獵物,直到第6期,我的捕食者輸出只精確到年第3期。

我的輸出是:

After period 1 there are 20 predators 
After period 1 there are 900 prey 

After period 2 there are 20 predators 
After period 2 there are 810 prey 

After period 3 there are 20 predators 
After period 3 there are 729 prey 

After period 4 there are 20 predators 
After period 4 there are 656 prey 

After period 5 there are 20 predators 
After period 5 there are 590 prey 

After period 6 there are 20 predators 
After period 6 there are 531 prey 

After period 7 there are 19 predators 
After period 7 there are 477 prey 

After period 8 there are 18 predators 
After period 8 there are 434 prey 

After period 9 there are 17 predators 
After period 9 there are 399 prey 

After period 10 there are 16 predators 
After period 10 there are 371 prey 

它應該顯示的數字是:

After period 1 there are 20 predators 
After period 1 there are 900 prey 

After period 2 there are 20 predators 
After period 2 there are 808 prey 

After period 3 there are 20 predators 
After period 3 there are 724 prey 

After period 4 there are 21 predators 
After period 4 there are 648 prey 

After period 5 there are 21 predators 
After period 5 there are 580 prey 

After period 6 there are 21 predators 
After period 6 there are 518 prey 

After period 7 there are 21 predators 
After period 7 there are 463 prey 

After period 8 there are 21 predators 
After period 8 there are 413 prey 

After period 9 there are 21 predators 
After period 9 there are 369 prey 

After period 10 there are 21 predators 
After period 10 there are 330 prey 
+1

我不確定你打算如何使用你的所有數字,但是我的頭頂是你的(1 + a - b * predator_population)中的舍入問題或操作順序問題,以及這樣。 – thumbtackthief 2015-02-06 22:46:23

+0

清理你的代碼,任何可能有人會很高興檢查你的算法。但是你不能期望在這裏獲得幫助。 *你認爲什麼是錯誤的?期望輸出和實際輸出之間的差異會隨着迭代次數的增加而增加,所以您在算法中犯了一個錯誤。 – 2015-02-06 22:46:29

+2

當您更新'predator_population'的值時,您是否打算使用'prey_population'的新值而不是舊值?因爲這就是發生了什麼事。 – jwodder 2015-02-06 22:48:08

回答

3

每回合你使用新的值來計算predator_population之前更新的prey_population值。這是拋棄結果。

+0

確實,OP是這樣做的,但我們確定修復它會恢復「預期」結果嗎? – DSM 2015-02-06 22:58:00

+0

我只是檢查它(畢竟這是一個古老的,已知的陷阱),但是這並沒有提出OP尋找的數字。 – Roberto 2015-02-06 23:00:31

+1

如果我正確理解這一點,我應該在方程前面做一個新變量來保存新信息,然後將它帶回到原來的獵物/捕食者種羣中,以便在下一回閤中用於方程中? – Djb14 2015-02-06 23:04:28

0

我不認爲這有資格作爲一個答案,但它太長時間放在註釋,這樣"practicality beats purity"

你問你的代碼做什麼,對於動輒就是:

獵物新號碼等於:

  • 老字號的獵物
    • 加這個回合的新胎(出生率*當前的獵物)(1)
    • 減去這個回合的殺死獵物[(捕食率*當前predato RS)*當前獵物] (1)
    • 圓這一切下來。

大鱷新號碼等於:

  • 老字號大鱷
    • 減去死亡(死亡率*當前的掠食者)的固定利率( 1)
    • 加上「食品胎」 [(「食生」率*新號碼的獵物)*當前的掠食者] (1)
    • 圍繞這一切。

這是它應該做的?

(1)在這一點上新的號碼不被圓下來了

+0

是的,如果我正確理解它,聽起來相當準確。我需要採取新的獵物和掠食者數量(第一次跑完900只獵物和20只掠食者)。然後打印獵物和掠食者,然後將這個新數字(900個獵物和20個掠食者)第二次帶入方程。我把這個方程式放在計算器裏,我已經得到了正確的數字。這是與我的等式,我會繼續嘗試和解決。 – Djb14 2015-02-06 23:42:38

+0

我在計算器中輸入的是 – Djb14 2015-02-06 23:48:01

+0

對不起,按回車。這是我做的。 什麼我把計算器是 食餌: 1000 *(1 + 0.1-0.01 * 20)= 900 900 *(1 + 0.1-0.01 * 20.2 = 808.2 808.2 *(1 + 0.1-0.01 * 20.3616)= 724.4575488 這是捕食者 20 *(1 - 0.01 + 0.00002 * 1000)= 20.2 20.2 *(1-0.01 + 0.00002 * 900)= 20.3616 我認爲公式中的舍入可能會擺脫量。 – Djb14 2015-02-06 23:55:34

1

我發現這個問題,是的,它是四捨五入。方程中的整數是四捨五入的答案。我將其更改爲浮點數並在打印語句中四捨五入。我非常感謝大家的幫助。

+0

你仍然需要做兩件事情,使結果預期:(1)遵循@HughBothwell的建議,使用另一個變量記住獵物的當前數量,更新前,並使用此計算新的掠食者; (2)不只是本輪下跌與詮釋,但圍捕,如果高於0.5(你可以使它在打印語句來詮釋前添加0.5)。 – Roberto 2015-02-07 00:07:15