CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
May 2004
Posts
249

## A Predator-Prey Simulation

Let there be two fishes named Kernighans and Ritchies. The only fish know to prey on Ritchies and to be able to digest them is the colour-blind Kernighan. The Kernighan has adapted to a diet of Ritchies, and won't prey on any other species. No other fish or sea animal could prey on the Ritchies.

So with he above information, could a simulation be created to calculate the two population at different time intervals. I have done something but i belive that i could be on the wrong track.

Some other information to follow would be:

The population of Kernighans and Ritchies can be fully determined by the population size of Kernighans and Ritchies. Suppose that in a given month the number of Kernighan is pop_k, and the population of Ritchies is pop_r. The population of Kernighans and Ritchies is measured in multiples of 1000. This means a population size of 1.0 corresponds to 1000 fish. For the number of Kernighans in the next month we have:

 The population pop_k decreases by alpha_k  pop_k. This is the number of Kernighans that would starve if there were no Ritchies to eat.
The population pop_k increases by beta_k  pop_k  pop_r. This is the number of new Kernighans because they can feed on Ritchies.
The population pop_k decreases by gamma_k  pop_k2. This decrease in the number of Kernighans is due to competition between Kernighans.

Hence, given a population pop_k, the population in the next month is
pop_k - alpha_k  pop_k + beta_k  pop_k  pop_r - gamma_k.pop_k2
If the result of this computation is smaller than 0.001, then there are no Kernighan fish left

For the number of Ritchies in the next month we have

The population pop_r increases by alpha_r  pop_r. This is the number of new Ritchies, if there were no Kernighan to eat them..
 The population pop_r decreases by beta_r  pop_k  pop_r. This is the number of Ritcchies eaten by Kernighans.
 The population pop_r decreases by gamma_r  pop_r2. This decrease in the number of Ritcchies tis due to competition between Ritchies.

Hence, given a population pop_r the population in the next month is

 pop_r + alpha_r.pop_r - beta_r.pop_k.pop_r - gamma_r.pop_r 2
 If the result of this computation is smaller than 0.001, then there are no Ritchie fish left.

Measurements have shown that the constants have the following values:
 alpha_k = 0.4;
 beta_k = 0.4
 gamma_k=0.036
 alpha_r = 0.2;
 beta_r = 0.2
 gamma_r=0.036

2. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: A Predator-Prey Simulation

What's your C or C++ question? Seems like this belongs in the Algorithms forum.

3. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: A Predator-Prey Simulation

Actually, this is a math problem. This is't a simulation all the information is static so for a given input there will be a given output.

A simulation would be about having to deal with things like... even if there are enough fish to eat, does a particular predator manage to catch enough of those prey fish to stay alive. Maybe it can't because none of the prey are nearby enough, or maybe it's not fast enough. in other words, some random effect.

4. Elite Member
Join Date
May 2009
Posts
2,413

## Re: A Predator-Prey Simulation

Originally Posted by OReubens
Actually, this is a math problem. This is't a simulation all the information is static so for a given input there will be a given output.
In fact predator-prey simulation is one of the cornerstones of mathematical biology. It gives rise to a system of two differential equations and to study how models like this evolve over time is called a dynamic simulation.

I suggest the OP uses the Lotka-Volterra equations and tries to identify the parameters with the ones of his problem,

http://en.wikipedia.org/wiki/Lotka%E...terra_equation
Last edited by nuzzle; May 15th, 2013 at 12:11 PM.

5. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: A Predator-Prey Simulation

Originally Posted by nuzzle
In fact predator-prey simulation is one of the cornerstones of mathematical biology. It gives rise to a system of two differential equations and to study how models like this evolve over time is called a dynamic simulation.

I suggest the OP uses the Lotka-Volterra equations and tries to identify the parameters with the ones of his problem,

http://en.wikipedia.org/wiki/Lotka%E...terra_equation
picking nits... it's not a simulation
if you run the math for a different range of times, It's a mathematical progression.

I doubt this is called a "dynamic simulation" because 1) it's not a simulation and 2) it's not dynamic, the input is entirely static, so the output is as well.

If I'd have to guess, I guess you're fumbling up some of the words here and it should really have been something more akin to:
...and to study how models like this evolve over time is called the dynamics of a (biological) system
dynamics != dynamic
even a static mathimatical progression has evolution dynamics

system != simulation.

I've seen the term "simulation" used incorrectly many times, even by big names, but essentially, if you can do the math in a spreadsheet (like you can here), it's a system, not a simulation.

Simulations are really about systems that are so dynamic that they branch off into 2 or more different possible states every so often, so much so that it is impossible to track EVERY state, so you end up randomly picking one of the states and go with that. You run the sim several times with different branches taken, and then try to infer your solutions/results based out of a small set of possible outcomes.

You could do the above predator/prey with an actual similation having, "playing out the lives of individual predators and prey". But the OP's quesion is really about a static system where you take statistical input about how the populations as a whole behave rather than individuals.

6. Elite Member
Join Date
May 2009
Posts
2,413

## Re: A Predator-Prey Simulation

Originally Posted by OReubens
I've seen the term "simulation" used incorrectly many times, even by big names, but essentially, if you can do the math in a spreadsheet (like you can here), it's a system, not a simulation.
I must comment on this because it looks like the worst besserwisser approach I've seen in a long time.

So you've come up with your own oddball definition of what constitutes a simulation. And since you're the centre of the world yours is now the correct definition of simulation. And from this follows that everybody else must be wrong! All off a sudden everybody who are sticking to the standard or orthodox or established or timetested definition of simulation, are wrong!

Before we continue this discussion I would like you to present some support of your view of what is a simulation. Is it something you've cooked up or do you have references in support of it?
Last edited by nuzzle; May 17th, 2013 at 06:22 PM.

7. Member
Join Date
May 2004
Posts
249

## Re: A Predator-Prey Simulation

a program that asks the user for the current population of Kernighans and Ritchies, and to compute the population size for the next month. It should take the following into account.
 Use the above formulas to compute the population size for the next month.
 If the computed population size is smaller than 0.001, the result should be 0.

8. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: A Predator-Prey Simulation

Originally Posted by rockx
a program that asks the user for the current population of Kernighans and Ritchies, and to compute the population size for the next month. It should take the following into account.
• Use the above formulas to compute the population size for the next month.
• If the computed population size is smaller than 0.001, the result should be 0.
Is that a C++ question?

Regards,

Paul McKenzie

9. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: A Predator-Prey Simulation

Originally Posted by Paul McKenzie
Is that a C++ question?

Regards,

Paul McKenzie
Doesn't look like any kind of question.

10. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: A Predator-Prey Simulation

I think it's a "can someone do my homework" question

11. Member
Join Date
May 2004
Posts
249

## Re: A Predator-Prey Simulation

Originally Posted by OReubens
I think it's a "can someone do my homework" question
Well yes it was my homework question and ive done it, as i said earlier and i quote
I have done something but i belive that i could be on the wrong track.
Here is my code.

Code:
```#include <iostream>
#include <stdlib.h>

using namespace std;

int main(){

//Variable Declaration

const float Alpha_k = 0.4;
const float Beta_k  = 0.4;
const float Gamma_k = 0.036;

const float Alpha_r = 0.2;
const float Beta_r  = 0.2;
const float Gamma_r =0.036;

float pop_k = 0;
float pop_r = 0;

float new_k = 0;
float new_r = 0;

cout << " ***** Kernighan and Ritchie fish simulator ****\n\n";

cout << "Enter population size of Kernighans:\t\t";
cin >> pop_k;

cout << "Enter population size of Ritchies:\t\t";
cin >> pop_r;

//Computes the population size of Kernighans for the next month
new_k = pop_k - (Alpha_k * pop_k) + (Beta_k * pop_k * pop_r) - (Gamma_k * pop_k * pop_k);

//Computes the population size of Ritchies for the next month
new_r = pop_r + (Alpha_r * pop_r) - (Beta_r * pop_k * pop_r) - (Gamma_r * pop_r * pop_r);

cout << endl << endl;

cout << "The population of Kernighan one month later is\t:" << new_k << ".\n";
cout << "The population of Ritchie one month later is\t:" << new_r << ".\n\n";

system("pause");
return 0;
}```
And

Code:
```#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main(){

//Variable Declaration

const float Alpha_k  = 0.4;
const float Beta_k   = 0.4;
const float Gamma_k  = 0.036;

const float Alpha_r  = 0.2;
const float Beta_r   = 0.2;
const float Gamma_r  = 0.036;

float pop_k = 0;
float pop_r = 0;

float current_k = 0;
float current_r = 0;

int months = 0;

cout << " ***** Kerninghan and Ritchie fish simulator ****\n\n";

cout << "Enter population size of Kerninghans:\t";
cin >> pop_k;

cout << "Enter population size of Ritchies:\t";
cin >> pop_r;

cout << "Enter months you want to forecast:\t";
cin >> months;

cout << "\n\n\nMonth\tKerninghans\tRitchies\n";

for( int m=0; m <(months+1); m++){

cout << m << '\t' << pop_k << "       " << '\t' << pop_r << endl;

//Computes the population size of Kernighans for the next month
current_k = pop_k - (Alpha_k * pop_k) + (Beta_k * pop_k * pop_r) - (Gamma_k * pop_k * pop_k);

//Computes the population size of Ritchies for the next month
current_r = pop_r + (Alpha_r * pop_r) - (Beta_r * pop_k * pop_r) - (Gamma_r * pop_r * pop_r);

pop_k = current_k;// Update the population size of Kernighans
pop_r = current_r;// Update the population size of Ritchies
}

cout << endl;

system("pause");
return 0;
}```

This was due in checked in April. I just wish to get to the correct coding so i could know where i went wrong

12. ## Re: A Predator-Prey Simulation

Originally Posted by rockx
This was due in checked in April. I just wish to get to the correct coding so i could know where i went wrong
Did you not get any comments/feedback from your teacher? If not, then check your results with another student or with another implementation, e.g. in a spreadsheet. Find out where any difference originates and then analyze which implementation is correct.

So far, you haven't done more than post your code and ask people to correct it. Programming doesn't work that way; you need to be able to verify your own code and, if not correct, hunt down and fix any errors. Whenever you get stuck, we can help you, but you'll need to show us all the steps you've taken. Otherwise, we cannot help you; we can only do the work for you (which is pointless).

13. Elite Member
Join Date
May 2009
Posts
2,413

## Re: A Predator-Prey Simulation

Originally Posted by OReubens
picking nits... it's not a simulation
if you run the math for a different range of times, It's a mathematical progression.

I doubt this is called a "dynamic simulation" because 1) it's not a simulation and 2) it's not dynamic, the input is entirely static, so the output is as well.
You're wrong.

To model a system (any system, not just biological) using a set of differential (or difference) equations and then follow how it evolves and behaves over time on a computer is called a dynamic simulation. It's an established term.

Simulations are really about systems that are so dynamic that they branch off into 2 or more different possible states every so often, so much so that it is impossible to track EVERY state
This is also wrong.

Dynamic simply means "varying over time" and the opposite is static. The dynamics may vary. It could be simple, complex and even chaotic but a dynamic system is a dynamic system and there is no such thing as more or less dynamic.

You seem to be confusing simulation with the concept of a statistical ensemble which is a theory to describe the overall behaviour of a system from its micro-states. To deal with it on a computer two simulation approaches are commonly used, dynamic simulation and (Markov Chain) Monte Carlo simulation. They're different but equivalent.
Last edited by nuzzle; May 16th, 2013 at 01:18 PM.

14. ## Re: A Predator-Prey Simulation

So what's the problem? What's not working as it should?

15. Member
Join Date
May 2004
Posts
249

## Re: A Predator-Prey Simulation

I just wish to know if my coding is correct or have i made errors in my coding

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•