NetLogo banner

 NetLogo Publications
 Contact Us

 Modeling Commons

 User Manuals:
 Farsi / Persian


NetLogo User Community Models

(back to the NetLogo User Community Models)

[screen shot]

If clicking does not initiate a download, try right clicking or control clicking and choosing "Save" or "Download".

Try It in NetLogo Web


This simulation demonstrates the haystacks model developed by John Maynard Smith (1964), used in evolutionary biology and economics to investigate the conditions under which altruism can develop in groups over time despite the personal cost (and hence evolutionary disadvantage) to the altruist. The model implemented is the version described in Theodore Bergstrom's (2002) overview paper from the economics perspective.


John Maynard Smith argued that altruism could be maintained (and eventually fixed) in a population under specific conditions.
1/ the altruists initially make up a high proportion of an isolated subpopulation;
2/ the benefit arising from the altruism ensures the isolated subpopulation grows much faster than the remaining population;
3/ periodically, the population mixes (allowing the altruists to 'infect' the other haystacks) and then re-establishes new isolated subpopulations.
The hypothesis is that the faster growth of the high altruism subpopulation leads to an increasing proportion of altruists in the whole population.


There are several haystacks. A founding population of mice starts in each haystack. Each haystack has the same sized founding population but with (potentially) different distributions of altruists and egoists. An altruist is willing to incur a personal cost so as to improve the reproductive rate of all the mice in their own haystack. An egoist is not willing to do so, but benefits from the sacrifice of the altruists.

For the purposes of the model, mice breed asexually; that is, altruists breed altruists and egoists breed egoists, and there is no need to track males and females. The success of the group is operationalised as reproduction. After some generations where each haystack is isolated, all the mice from the various haystacks intermingle and new founding populations for each haystack are drawn from the entire mouse population. This reproduction and mixing continues until the whole population is of only one type, altruists or egoists.

The only agents are haystacks. The key properties of the haystack are the number of altruists and the number of egoists. These are updated each timestep based on reproduction rates. Mice are implicit rather than modelled.

Additional haystack properties are for visualisation purposes and have no behavioural influence. The total number of mice (that is, the sum of the number of altruists and the number of egoists) governs the size, so that the differential impact of breeding can be seen.

There is no meaningful geographic or network relationship. The patches have no impact. Each haystack has a location only for visualisation purposes.


The world view shows several haystacks (number given by 'num-haystacks' slider). The colour of the haystack represents the mix of altruists and egoists in the haystack: blue for primarily (>65%) altruists, red for primarily (>65%) egoists, and green if roughly balanced. The size is determined by the number of mice.

There are three input boxes for the reproduction rates to update the number of altruists and egoists in each haystack.
> 'reproduction' is the additional mice in the next generation per existing mouse
> 'altruism-group-benefit-b' is the group benefit of altruism
> total additional mice in the next generation per altruist in the haystack
> allocated randomly over all mice in the haystack
> 'altruism-personal-cost-c' is the decrease in reproduction for each altruist
> incurred by the altruists only

Mathematically (following Bergstrom), if N is the number of existing mice in the haystack, x is the proportion of altruists, r is the base reproduction, b is the group benefit (additional mice) and c is the personal cost (to each altruist):
> Base reproduction: Each altruist in the group allows an additional b mice across the group, so the base reproduction rate is increased by b/N per altruist. Overall, there are xN altruists, so the additional reproduction rate is (bx).
> Egoist reproduction: Each egoist produces (r+bx) new mice in the next generation because they receive the group benefit at no cost plus the base reproduction.
> Altruist reproduction: Each altruist would have produced (r+bx) new mice, but gave up c mice for the good of the group, so their reproduction rate is (r+bx-c).

The 'Population Counts' monitor displays the combined haystack total number of altruists (blue) and egoists (red). Total is displayed in the 'Population' output. The 'Altruist Proportion' monitor displays the number of altruists as a proportion of the total altruist and egoist population.

There are two sliders to control the iterative isolation and mixing process. The 'founding-popn-N' slider sets the total number of mice in each haystack at the beginning of the isolation phase. The 'generations-T' slider sets the number of generations for which mice will reproduce within their haystack during the isolation phase. At the end of the isolation phase, new founding populations are established for each haystack. The mix of altruists and egoists is proportional to the entire mouse population. Conceptually, this is equivalent to drawing these founding populations randomly from the mice in every haystack and any mice left over die.

The cycle of reproduction and mixing continues until an evolutionary stable equilibrium has been achieved. At this point the entire mouse population is altruistic (in which natural selection at the group level has occurred) or egoistic (in which natural selection at the individual level has occurred).

The mix of altruists and egoists in each haystack initially is controlled by two sliders. The 'initial-prop-altruists-x' slider sets the proportion of altruists in the population at the start of the simulation. This proportion changes over time due to differential reproduction rates.

The 'assortativity-a' slider affects how the founding mice are distributed between haystacks at the start of each isolation phase. A value of 0 means that all haystacks have a mix of altruist and egoist mice in the same proportion. A value of 1 means that some haystacks have only altruistic mice and others have only egoistic mice, but no haystack has a mixed population. This is operationalised using the subgroup matching method: if (a) is the index of assortativity, then the first proportion (a) of the haystack's founding population is of one type (with the type is selected randomly using the population proportion). The remainder of the founder population is generated with the type selected randomly at the individual level.


The default settings have been selected to demonstrate that egoists win in normal evolutionary arrangements. That is, egoists do better than altruists and eventually comprise the whole population despite an equal mix at the start.

The settings are:
1 base reproduction
4 additional reproduction for haystack per altruist
1 personal cost of altruism
That is, altruists give up their base personal reproduction to provide a group benefit of additional reproduction that is shared between all the mice in their haystack.
10 haystacks
10 founding population per haystack
3 generations between mixing
0.5 initial proportion altruists
0 assortativity


To demonstrate Maynard Smith's idea, changes are only required in 3 of the settings. The other settings are available for further exploration. Use the same settings for reproduction rates, initial proportion of altruists and number of haystacks. Only the altruism in isolated populations will be changed.

Variation 1: small founding population
3 founding population per haystack
3 generations between mixing
0 assortativity
The small founding population size per haystack produces greater variation in the proportion of altruists within each haystack. Thus, there are some haystacks with much higher initial proportions of altruists and hence much higher reproduction rates. Altruists will (usually) win.

Variation 2: moderate founding population, longer isolation
5 founding population per haystack
25 generations between mixing
0 assortativity
With founding populations of 5 mice per haystack, there is variation in the altruist proportions, but 3 generations is insufficient for those higher altruist haystacks to develop much larger mice numbers. However, extending the isolation phase will (usually) allow altruists to win.

Variation 3: positive assortativity
10 founding population per haystack
3 generations between mixing
0.5 assortativity
With the default settings of 10 founding mice per haystack, variation in altruist proportions is small. However, higher assortativity increases this variation by making it more likely that altruist mice will stick together. Altruists will (usually) win, even with a short isolation phase.


There are two types of games used extensively in evolutionary biology: the conflict game of Prisoners' Dilemma, and the cooperation game of Stag Hunt.

PRISONERS' DILEMMA: Consider the situation where two people commit a crime. The police have enough evidence to convict the offenders of a lesser crime, but need the testimony of either one to convict the other of the more serious crime. The police offer a deal: confess and testify to receive a reduced sentence. Each offender has to choose whether to confess without knowledge of the choice of the other offender.

If they both confess, they each receive sentences of 5 years. If neither confesses, they are sentenced to 2 years. If only one confesses, that offender receives no sentence and the other receives 10 years. It is clearly in the interest of each to confess, but that leads to a worse outcome than if neither confesses.

STAG HUNT: Consider the situation where two (or more) people are required to successfully hunt a stag. Alternatively, each person could decide to hunt a rabbit. The amount of food available per person from a successful stag hunt is more than he or she would obtain from a successful rabbit hunt. However, the stag hunt is only successful if all the hunters participate.

The benefits of participating in the stag hunt are higher than not participating, so participants are not being altruistic. The best response for a hunter is to participate in the stag hunt if all others participate, but to hunt rabbits if any of the others decides to do so. Therefore, the stag hunt requires a level of cooperation and trust.

Within a haystack, the egoists always reproduce faster than the altruists, so the haystacks model is usually set up with the same incentives as a Prisoners' Dilemma. However, if there is a very high proportion of altruists and their share of additional reproduction is greater than their personal costs to obtain that reproduction, then the game is of Stag Hunt form. However, Maynard Smith's thesis does not rely on this Stag Hunt solution. Instead, altruist populations can emerge even within a Prisoners' Dilemma game if subpopulations with higher levels of altruism remain isolated for some time.


This model was developed by Jennifer Badham ( in September 2011.

Maynard Smith J 1964, "Group selection and kin selection" in Nature, No. 4924 (March 14, 1964), pp 1145-1147.

Bergstrom T 2002, "Evolution of Social Behavior: Individual and Group Selection" in Journal of Economic Perspectives, vol 16(2), pp 67-88.

(back to the NetLogo User Community Models)