 2015-06-02 08:50

# 请大家帮忙看看这段代码如何写才好？

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.

This Predator-Prey model defines:

 H[n] as the snowshoe hare population (being n a specific moment of time)

 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]

 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]

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:

After 1 period the population of snowshoe hares will be:

H = H + a1 * H - a2*H*L = H * (1 + a1 - a2*L) = 300 *(1 + 0.1 - 0.01*20) = 270
In turn, the population of lynxes will be:

L = L - b1 * L + b2*H*L = L * (1 - b1 +b2*H) = 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 = H * (1 + a1 - a2*L) = 270 * (1 + 0.1 - 0.01*19.92) = 243.216

And the population of lynxes will be:
L = L * (1 - b1 +b2*H) = 19.92 * (1 - 0.01 + 0.00002*270) = 19.828368

If we continue, we can guess the population after 20 periods:
H=47.15 snowshoe hares
L=17.28 lynxes

Or after 100 periods:
H=8.44 snowshoe hares
L=7.89 lynxes

Or even after 200 periods:
H=903.17 snowshoe hares
L=3.91 lynxes

``````
``````
• 写回答
• 好问题 提建议
• 关注问题
• 收藏
• 邀请回答

#### 3条回答默认 最新

• JonsonJiao 2015-06-02 09:44
已采纳

开始的时候我认为这个就是一个递归调用的过程，也写好了代码，计算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 = POP_H0;
popL = 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));
}
}
``````

非递归调用，计算时间秒级啊。

已采纳该答案
评论
解决 无用
打赏 举报
• 逆-风 2015-06-02 08:55

能说我看着捉这么长的题目我就头疼吗

评论
解决 无用
打赏 举报
• threenewbee 2015-06-02 09:05
评论
解决 无用
打赏 举报