# Population dynamics/Spatial simulation

This is another kind of simulation aiming at finding the equilibrium (if any) in the population dynamics. Contrary to the other method which tried to find an analytical formula for the MS/witch population, this "new" method is entirely based on simulation. This method is based on a finite population, and is inspired by reinforcement learning.

## Method

Here is how it works: "entities" are placed on a grid (lattice) of a certain size. Each entity takes exactly one cell of the grid, or put differently, each cell can hold exactly one entity. The different entities are Human, MS, Witch and Dead (this last one representing an empty cell). The simulation is discrete, meaning that the time advances in "steps". At each step, each cell is updated following certain rules. The update rules for the different cells are:

• The "Kyubey Factor": At each turn, Kyubey contracts with a certain amount of new Magical Girls. In other words: at each iteration, K random cells are turned into MSes. Ideally, only Human entities should turn into MSes, but in the current version, for optimization purposes, any type of cell can become a MS thanks to Kyubey.
• Update rules for humans:
• A human can die of natural death, or become too old to become a MS. Therefore, we define a "maturity threshold": once a Human entity has survived for a given number of iterations (in the tests: 15), it has a 50% of "dying" at each new iteration.
• A human can also die because of a witch (eg by comitting suicide). So, when a witch is in one of the 8 surrounding cells, the human has a 10% chance of dying because of it.
• A human can occasionally be born or become eligible to become a MS. This is represented in the model by the empty cells having a low probability (1%) of becoming Humans
• Update rules for MSes:
• Despair rule: MSes become witches if they haven't cleaned their grief seed enough. This is modeled in our simulation by a "despair threshold": MSes become witches with a probability of 100% if they haven't beaten a single witch for a certain amount of iterations (in the experiment: 10).
• Combat against witch: If there is a witch in one of the 8 cells surrounding the MS, they start fighting with probability 10%. If the combat starts, the MS has a probability of losing (and dying) inversely proportional to her experience (the amount of iterations she has lived as a MS). Otherwise, the witch dies and the MS's grief seed is cleaned, meaning that her "countdown to despair" is reset.
• Update rule for witches:
• The only thing a witch can do now is engage combat with a MS as explained above.

In addition to those "update rules", at each turn, all entities have the ability to move to an adjacent space. Right now, all entities have the same speed, but it could be an interesting improvement to give less mobility to witches than to MSes.

## Results

The script is available as a Netbeans Java project here. It is interesting to run because it shows a visual (spatial) representation of the population: humans are shown in white, witches in red, MSes in blue and empty cells in grey. A graph of the population is also saved as ./population.jpg. Note: you will need JFreeChart to save the statistics plot.

The script has been run with the following parameters:

This is an example
 Grid size 50 x 50 Population size approx. 1250 Kyubey factor 5 Maturity threshold 15 Despair threshold 10

The result for this setup can be seen on the right side, and also here. Interestingly enough, the number of witches does not grow exponentially like in the analytical case. This is probably due to the limited size of the population, and the more realistic rules. Changing the above parameters does not seem to change the overall shape of the result; only the proportion of each population changes. Therefore, even though there are always more witches than MSes, the system seems to reach a stable equilibrium -and the population of humans doesn't go extinct, which is rather a good news for us.

## Planned refinements

The model presented above is really simple but could be easily expanded. Some improvements that have been though of include:

• Adding a "natural death" factor for MSes, just like humans. Indeed, the fact that they have become MSes does not automatically involve them becoming immortal (unless they wished for immortality). Maybe the threshold should be put higher than for normal humans though
• MSes sometimes fight each other. Hence, it might be interesting to add a (really small) probability that two MSes would engage combat when they are next to each other.
• The same goes for witch. Nothing says witches can't compete for the same area and kill each other.
• We know witches spawn minions, and we know minions that reach maturity become witches themselves. This could be modeled in two ways:
• The simple way: if there is an empty space around a witch, this witch has a (really small) probability of spawning another witch there
• The accurate way: define a new entity type, "Minion". A witch has a certain probability of spawning minions at each iterations. Minions who survive long enough turn into witches. Could be achieved easily with the code provided and the explanations above, but it does need some coding.
• We could also add the possibility of a combat ending in a tie. Thus, both opponents would survive and nothing special would happen.
• Finally, we know that most MSes try their hardest to save humans. Hence, we could model some complex behaviors representing that. For instance, we could introduce "proxy fighting": if a MS is near a human that is herself near a witch, the MS would engage combat against the witch, even though they are not directly adjacent. This would also require some additional coding.

All those improvements should only be added in as minor variations of the simple case though; the mechanics that have been implemented should remain the most important ones.