CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    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. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: A Predator-Prey Simulation

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

  3. #3
    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. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: A Predator-Prey Simulation

    Quote Originally Posted by OReubens View Post
    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. #5
    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A Predator-Prey Simulation

    Quote Originally Posted by rockx View Post
    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

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: A Predator-Prey Simulation

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

    Regards,

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

  8. #8
    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

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: A Predator-Prey Simulation

    Quote Originally Posted by nuzzle View Post
    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.

  10. #10
    Join Date
    May 2009
    Posts
    2,413

    Re: A Predator-Prey Simulation

    Quote Originally Posted by OReubens View Post
    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.

  11. #11
    Join Date
    May 2004
    Posts
    249

    Re: A Predator-Prey Simulation

    Quote Originally Posted by OReubens View Post
    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. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: A Predator-Prey Simulation

    So what's the problem? What's not working as it should?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    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

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: A Predator-Prey Simulation

    Assuming the calculations are correct (Does the program produce the output expected?) the only small comments re the code is that

    Code:
    for( int m=0; m <(months+1); m++){
    could be written as

    Code:
    for( int m=0; m <= months; m++){
    Also, if the input is <= 0 or input is >= 1 then you get strange results. You might want to restrict input to just > 0 and < 1 (or fix the formula so that it works for all positive numbers!).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    May 2009
    Posts
    2,413

    Re: A Predator-Prey Simulation

    Quote Originally Posted by OReubens View Post
    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.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured