程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 開發-請大家幫忙看看這段代碼如何寫才好?

開發-請大家幫忙看看這段代碼如何寫才好?

編輯:編程綜合問答
請大家幫忙看看這段代碼如何寫才好?

如果題目的意思是:
方法頭必須是double[] preyPredLV(double[] preyPred, double[] a, double[] b, int n};
然後,題目的要求如下,誰能給出一段代碼,符合題目的要求?
The dynamics between predators and preys in a given ecosystem attracts a lot of attention from researchers. Different scientists have developed Predator-Prey models to try to understand the interactions and foresee the evolution of the populations.
在給定的生態系統裡,食肉動物和獵物之間的動態吸引了許多來自調研人員的注意力。不同的科學家開發了食肉動物-獵物模型,試圖理解和預測人口進化的交互。

One of the first analyzed Predator-Prey ecosystems was the “Lynx-Snowshoe hare” and one of the first Predator-Prey models defined was the one developed by Lotka and Volterra.
其中,第一個分析食肉動物-獵物生態系統的是”Lynx-Snow雪地靴野兔”,以及第一個定義的食肉動物-獵物模型是由Lotka和Volterrra開發的。

This Predator-Prey model defines:
這個食肉動物-獵物模型定義了:

 H[n] as the snowshoe hare population (being n a specific moment of time)
是雪地靴野兔的人口(n是一個特定時刻 )
 L[n] as the lynx population
是猞猁的人口
 It assumes that the primary growth of the hare population in the absence of lynx is a1*H[n] and that the lynx population in the absence of hares declines –b1*L[n]
假設,當猞猁缺乏的時候,野兔人口的主要增長是a1*H[n],並且,野兔缺乏的時候,猞猁的人口下降是-b1*L[n].
 It also assumes that the primary loss of snowshoe hares is due to predating a2*H[n]*L[n] and the growth of the lynx population is from the energy derived from eating snowshoe hares b2*H[n]*L[n]
它還假設雪地靴野兔的主要減少是因為捕食a2*H[n]*L[n],且猞猁的人口增長是因為來源於吃雪地靴野兔得到的能量b2*H[n]*L[n]

The Lotka-Volterra model is defined by the following formula:

H[n+1] = H[n] + a1 * H[n] - a2*H[n]*L[n] = H[n] * (1 + a1 - a2*L[n])
L[n+1] = L[n] - b1 * L[n] + b2*H[n]*L[n] = L[n] * (1 - b1 +b2*H[n])

For instance, let’s assume that the initial population of snowshoe hares is 300 and the initial population of lynxes is 20, and the values of the constants that regulate the model are a1=0.1, a2=0.01, b1=0.01 and b2=0.00002. The previous formula can be used to calculate the population of both lynxes and snowshoe hares after 2 periods:
舉個例子,讓我們假設,雪地靴野兔的初始人口是300個,猞猁的初始人口是20個,約束這個模型的常量的值是a1=0.1, a2=0.01, b1=0.01 and b2=0.00002。前一個公式能被用來計算猞猁和雪地靴野兔的人口,在期間2以後。

After 1 period the population of snowshoe hares will be:
在期間1以後,雪地靴野兔的人口將會是:
H[1] = H[0] + a1 * H[0] - a2*H[0]*L[0] = H[0] * (1 + a1 - a2*L[0]) = 300 *(1 + 0.1 - 0.01*20) = 270
In turn, the population of lynxes will be:
相應地,猞猁的人口將會是:
L[1] = L[0] - b1 * L[0] + b2*H[0]*L[0] = L[0] * (1 - b1 +b2*H[0]) = 20 * (1 - 0.01 + 0.00002*300) = 19.92

Notice that we keep the decimals for the following loop in the formula.
注意,我們保持小數點給公式裡的下面的循環

After 2 periods, the population of snowshoe hares will be:
H[2] = H[1] * (1 + a1 - a2*L[1]) = 270 * (1 + 0.1 - 0.01*19.92) = 243.216
經過2個期間,雪地靴野兔的人口將會是如上面的公式

And the population of lynxes will be:
L[2] = L[1] * (1 - b1 +b2*H[1]) = 19.92 * (1 - 0.01 + 0.00002*270) = 19.828368
且猞猁的人口將會是以上的只數

If we continue, we can guess the population after 20 periods:
H[20]=47.15 snowshoe hares
L[20]=17.28 lynxes
如果我們繼續,我們能夠猜測20個期間以後的人口數:

Or after 100 periods:
H[100]=8.44 snowshoe hares
L[100]=7.89 lynxes
經過100期間:

Or even after 200 periods:
H[200]=903.17 snowshoe hares
L[200]=3.91 lynxes
或者甚至經過200個期間以後:


最佳回答:


開始的時候我認為這個就是一個遞歸調用的過程,也寫好了代碼,計算1、2、20都沒有問題,等到100的時候就受不了,計算時間太長了,後來我改成了這個樣子,Java代碼如下:

  private static final int POP_H0 = 300;
    private static final int POP_L0 = 20;

    private float[] popH;
    private float[] popL;

    public static void main(String[] args) {

        EveryThingTest ett = new EveryThingTest();
        int period = 200;

        ett.printPop(period);

    }

    /**
     * 輸出period時的兩種動物數量
     * 
     * @param period
     */
    private void printPop(int period) {
        // TODO Auto-generated method stub
        popH = new float[period + 1];
        popL = new float[period + 1];
        popH[0] = POP_H0;
        popL[0] = POP_L0;
        for (int i = 1; i < period + 1; i++) {
            popH[i] = getPopH(popH[i - 1], popL[i - 1]);    //非遞歸調用
            popL[i] = getPopL(popH[i - 1], popL[i - 1]);    //非遞歸調用
        }
        // float popH = getPopH(period);    //遞歸調用
        // float popL = getPopL(period);    //遞歸調用
        System.out.println("經過" + period + "期間之後:");
        System.out.println("-->野兔數據量為" + popH[period]);
        System.out.println("-->猞猁數據量為" + popL[period]);
    }

    /**
     * 非遞歸方式,根據傳入的前一個時間的野兔和猞猁值計算野兔數量
     * @param preH
     * @param preL
     * @return
     */
    private float getPopH(float preH, float preL) {
        // TODO Auto-generated method stub
        return preH * (1 + 0.1f - 0.01f * preL);
    }

    /**
     * 非遞歸方式,根據傳入的前一個時間的野兔和猞猁值計算猞猁數量
     * @param preH
     * @param preL
     * @return
     */
    private float getPopL(float preH, float preL) {
        // TODO Auto-generated method stub
        return preL * (1 - 0.01f + 0.00002f * preH);
    }

    /**
     * 遞歸計算野兔的數量,period為0時返回300
     * 
     * @param period
     * @return
     */
    private float getPopH(int period) {
        // TODO Auto-generated method stub
        System.out.println("計算" + period);
        if (period == 0) {
            return POP_H0;
        } else {
            return getPopH(period - 1)
                    * (1 + 0.1f - 0.01f * getPopL(period - 1));
        }
    }

    /**
     * 遞歸計算period時的猞猁數量,period為0時返回20
     * 
     * @param period
     * @return
     */
    private float getPopL(int period) {
        // TODO Auto-generated method stub
        if (period == 0) {
            return POP_L0;
        } else {
            return getPopL(period - 1)
                    * (1 - 0.01f + 0.00002f * getPopH(period - 1));
        }
    }

非遞歸調用,計算時間秒級啊。

tongyi55555
FaradayZhangChina
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved