A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Simulating a population sample growth/decay

  1. #1
    Senior Member
    Join Date
    Nov 2001
    Posts
    174

    Simulating a population sample growth/decay

    OK, heres some theory that I need to improve on.

    I have a population of worms, and select 20 of em to display on the stage. They have 2 colours, green and pink.

    At the start there will be 10 pink and 10 green displayed.

    I can click on a worm to 'eat' it, and have to get as many as i can before a timer runs out. One colour is easy to see, while the other is much more difficult, the point being that it emulates nature : the easy to see worms get eaten and their population declines.

    Once the timer runs out, worms are added to teh population to replace the ones eaten in that 'round' and thats where the theory comes in. I need to replace worms representative of the total population.

    For example, had the population been 100 worms, and i had eaten 10 pink worms (all those on display first round), then the population of worms would be 40 pink, 50 green. I need to replace 10 worms, but there is less pink than greens, so they arent going to make so much, i do a percentage 4/9 and 5/9.

    So i make 4 pinks, and 6 greens, and start round 2, selecting 20 worms as a percentage of the total pop.

    This approach works great to start, but as the pink pop declines -(gets closer to zero) it takes longer and longer (more and more rounds) to finally eliminate the pink pop, which is the opposite to how it should go : a pop should dwindle faster and faster.

    Is there a diff approach i can take to this problem? I like maths but this is doing my head in a bit

    Cheerz
    Dwayne

  2. #2
    Viral tick lordofduct's Avatar
    Join Date
    May 2008
    Location
    South Florida
    Posts
    159
    exponential growth and decay!

    It's a generic method used to guess the population of a city depending on some rate of growth/decay and a period of time. You basically decide on the rate of growth between each "level" of the game and pass the number of each type of worms to get a new value of worms.

    N(t) = Ne^(kt)

    N is the starting number of worms (the number at the time you run this)
    e is the constant value e (2.71828183)
    k is the constant of decay/growth
    t is the amount of time passed (use 1)

    the thing is we're going to have to do some tweaking to ensure that N(t)pink plus N(t)green == 100 (I'm assuming you require the same number of worms over and over).

    SO, seeing as Ngreen and Npink is always going to be a defined value each update. e is a constant value, we're going to just leave t as one because we are always updating 1 interval. So that leaves "k" to be a value we can alter each update.

    SO, let's solve this function to find out how to alter k each time so that way we can exponentialy decay a population over time and always have the same number of worms:

    TotWrms = N1e^k + N2e^k
    TotWrms / e^k = N1 + N2
    1 / e^k = (N1 + N2) / TotWrms
    e ^ -k = ...
    ln( e ^ -k ) = ln( ... )
    -k = ln( (N1 + N2) / TotWrms )

    k = -ln( (N1 + N2) / TotWrms )

    We have it.

    So every time you want to replenish your worms you have to first solve for k with the current number of worms for each race.

    Then you pass these values to the original decay formula at the top.

    And bam that's your new population forcing it to stay with in some constant bounds.


    What happens is the fewer of one race there are, the slower it increases it's population. But it assumes that when there are VERY few worms of a race, but a lot of worms in total were eaten, the fact it survived this it must not have been the one eaten and will actually grow faster then if a few amount of worms were eaten that round.

    I love math!


    Oh and if you want more then 2 races of worms, it's easy to put in... it's the sum of all worms right now.

    N1 + N2 + N3 + N... + Nn

    The only thing is this method allows for fractional amounts of worms and will require rounding. Which gives the possibility of like +- 1 or 2 worms then the intended population. So remember to check for this and just make sure you give the larger population the worm, and not the weaker population.
    Last edited by lordofduct; 02-11-2009 at 04:20 PM.
    I do not vow to know everything, and I can not guarantee what I say is correct. We are all learning, and I actually thank you the one with the questions for asking and stimulating my mind.

    www.lordofduct.com - check out my blog, or contact me through my website. I'm always available for freelance work in Flex3/Actionscript 3.

    how many times must I say it... the livedocs are your friend!
    http://livedocs.adobe.com/flash/9.0/...l-classes.html

  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    174
    Wow dood that is an impressive formula

    I came up with a solution before I got your post, ended up squaring the numbers, so if the user has eaten noticeably more worms of a certain colour, squaring it and finding the proportion of the squares has a stronger impact : faster population decline/growth.

    But now the client is saying they have their own formula so i might have to swicth to that anyway :/ Maybe ill get to use your formula!

    Thanx heaps
    Dwayne

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

HTML5 Development Center