Search Unity

Math for simulating city population/birth rate

Discussion in 'Scripting' started by FisherM, Apr 25, 2015.

  1. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I have a set of game objects positioned around a map with city models attached to them.
    Each has a class Settlement attached, on a timer I execute a function in every settlement function.
    Code (CSharp):
    1.  
    2. public float population_growth{
    3.         get{
    4.             float housing_avalibility = Mathf.Clamp(1 - ((float)population_count/(float)housing_capacity), 0.0f, 1.0f);
    5.             float food_avalibility = Mathf.Clamp(1 - ((float)food_consumed/(float)food_produced), 0.0f, 1.0f);
    6.             float growth = ((0.5f * housing_avalibility) + (0.5f * food_avalibility)) * 0.5f;
    7.             growth = Mathf.Clamp(growth, 0.0f, 0.5f);
    8.             Debug.Log ("growth = " + growth + ", with housing at " + housing_avalibility*100 + "%, with food at "+ food_avalibility*100 + "%");
    9.             return growth;
    10.         }
    11.     }
    12. public void ReproductionCycleUpdate(){
    13.         //need to add immigration
    14.         population_count += Mathf.RoundToInt(n_children);
    15.  
    16.         n_children = Mathf.RoundToInt(population_growth * population_count);
    17.  
    18.         eligable_recruits += Mathf.RoundToInt(population_count * recruits_rate);
    19.  
    20.         Debug.Log ("population = " + population_count);
    21.         Debug.Log ("children = " + n_children);
    22.         Debug.Log ("eligable recruits = " + eligable_recruits);
    23.     }
    24.  
    I currently use population growth to calculate the growth rate, however I am not sure the best approach but I want to be able to base the growth rate of a number of variables such as how much housing is available and how much food surplus is available.

    The growth cannot be lower than 0 or greater than 0.5 (presumes ruffly that 50% are females that can reproduce 1 child and that any anomalies to this balance)
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  3. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    How would I go about applying various values such as food surplus or housing available (if no living space is available will reduce reproduction rate and lots of empty living space will increase reproduction rate)
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Logistical growth rate looks like this.

    Code (CSharp):
    1. noOfNewIndividuals = currentPopulation * maxGrowthRate  / (1 - currentPopulation / maxPopulation);
    In most cases the max population is calculated based on the limiting factor. In your case I'd calculate a theoretical max population for each factor, then use the lowest. This equation will also start killing people if resources disappear or the population suddenly spikes.

    Something like this

    Code (CSharp):
    1. float[] maxPopulationSize;
    2. foreach (factor that can affect growth){
    3.     // Calculate a maxPopulationSize and add it to the array
    4. }
    5. maxPolulation = Mathf.Min(maxPopulationSize);
    6.