
ECOLOGICAL EXPERIMENTS (aka. Ecological Modelling) by William Masefield
from Your Computer, August 1982

William Masefield's ZX-81 program effectively simulates the spread of
disease and the predator-prey relationship.


--

Ive noticed TTFn has a few ZX80/81 programs that arent in the standard .O
and .P formats, so I thought I would convert them as appropriate so that
they can be used with the widest range of emulators. Ive attached new
versions of each program, although for some it was only necessary to rename
the file extension. [ PF ]

.Z81
Ive converted snapshot files in .Z81 format to .P format:
- YC8208w208EcologicalModelling.z81
- SP8209w003014TreasureHunt.z81
- HW83073020025Dodgem.z81

The .z81 snapshot format saves a dump of the entire ZX81 memory, which is
quite wasteful (resulting in larger files) and would only be useful if the
program held data in RAM that SAVE would not access, e.g. data above
RAMTOP. However, none of these programs do that and so there is no
advantage using .Z81 over .P format. In fact, it could prove a disadvantage
since the snapshots includes the stack and these might contain return
addresses into the BASIC interpreter in the ROM. Since there were 2
editions of the ZX81 ROM (let alone clones machines of the ZX81), such
addresses could differ between them and so the snapshots might only work
with one ROM version or the other (I havent tested for this issue). The .P
format does not suffer from this problem and so is more portable. [ PF ]

--


A number of programs have been published, based on Life, in which a colony
of cells is made to proliferate and ultimately die off. None that I have
seen does much more than this. Yet with a little extra programming, it is
possible to use the basic concept of Life to create a series of ecological
and epidemiological models which, while not being perfect, can be used in a
classroom to illustrate some of the elementary concepts in ecology.

The first model deals with the spread of a disease in, say, a woodland
plantation, and the second models the predator-prey relationship, with
spiders and flies as the protagonists.

Taking the first model, the program asks for the initial number of normal,
healthy trees in the plantation  a maximum of 100  then the number of
trees that may be immune to the disease  a maximum of 100 less the number
of healthy normal trees  and finally the number of diseased trees  100
less the sum of the other trees.

The program then plants these trees randomly in a 10-by-10 matrix, using a
different symbol for each kind of individual. The matrix is then displayed
within a black border and the numbers of each kind printed alongside,
together with the cycle number, which will be zero at the start  see
figure 1.

Figure 1

On keying Newline, the first cycle is initiated. What the program does now
is to interrogate each cell of the matrix in turn to find out what kind of
tree, if any, occupies it, and what its neighbours are on the north, south,
east and west sides.

For our purposes, the diagonal neighbours are not considered near enough to
have any influence.

This is where the ideas borrowed from Life play a role: if the number of
neighbours of a normal or immune tree are three or four, then the tree is
overcrowded and dies; its matrix cell is reset to zero; if the number of
neighbours is less than two, then the normal and immune trees reproduce 
that is, a new tree of the same kind is placed at random in an empty cell
of the matrix.

However, if the cell being interrogated is occupied by a diseased tree, the
disease is spread to a random matrix cell. Should that cell be empty or
occupied by an immune tree, nothing happens, but if the cell has a normal
tree, then that tree becomes diseased. Finally, diseased trees have a
limited life-span, dying off after four cycles, whether overcrowded or not.

After interrogation of all the cells of the field, the new state of affairs
is displayed, together with the counts of normal, immune and diseased trees
in the field. These numbers are also stored for later display in histogram
and tabular forms. The cycle is now repeated as many times as necessary
until the disease dies out  which it will not do if there are no immune
trees to start with, in which case the disease becomes endemic.

Figures 2 and 3 show the position after 10 and 20 cycles and figure 4 the
final field for a run which started with 50 normal, 10 immune and five
diseased trees. Obviously, the number of cycles will depend on the starting
conditions, and on the randomness of the propagation process, but the
general trend is always the same.

Figure 2

Figure 3

Figure 4

After cycling for the required time, the results of the experiment can be
displayed as a histogram  figure 5  or as a table of counts for pupils to
make their own graphs  figure 6.

Figure 5

Figure 6

It is possible to elaborate this program so that the disease is spread only
to immediate neighbours, for instance, or to prolong the lifespan of
diseased trees, and so on.

The second model concerns spiders and flies, and the prey and predator
relationship. The basic idea is the same: the program asks for the initial
number of flies  a maximum of 50  and spiders  a maximum of 20  and
these are scattered at random in the 10-by-10 field. This initial field and
the counts are displayed  figure 7.

Figure 7

The same kind of interrogation of each cell is then carried out, but the
manipulation is different: if a fly is unfortunate enough to be adjacent to
a spider  north, south, east or west but not diagonally  then it is
eaten. Otherwise, it is moved to another empty cell at random. A fly not
adjacent to a spider is allowed to reproduce.

A constraint limits the number of new flies in each generation to half the
number of parent flies, that is, a reproduction factor of 1.5, so that if
there are 30 flies in one field, there will be 45 in the next.

Another limit ensures that there are not more flies than there are spaces
in the matrix for them. All the flies are redistributed randomly, as they
would be in life were they flying about.

The spiders, on the other hand, do not move, as they are supposed to be
sitting in their webs. Each spider, at the start of its life, is given a
store of food, valued at three units, but this is depleted by one unit per
cycle, and a spider with zero units dies. However, each fly it consumes
increments its food store by one unit.

If a spider reaches a value greater than six, it reproduces, the offspring
being set at random in the field, and both it and its parent are given the
starting value of three units.

At the end of each cycle the field and counts are displayed, and the
process repeated for as many cycles as required. Figure 8 shows the 10th
field and figure 9 shows the histograms. In this particular run, starting
with 50 flies and five spiders, there has been a cyclic fluctuation in the
populations and the peaks are staggered as they would be in nature.

Figure 8

Figure 9

Other starting figures will give different degrees of fluctuation and if
there are too few flies or too many spiders at the start, the spiders can
actually die out. It is assumed that the flies' food source is
inexhaustible.

Although the displayed field is a 10-by-10 matrix, the actual field is
10-by-12. The reason for this is as follows: in the interrogation of, say,
the jth cell, we have to look at the adjacent cells, j-10, j+10, j-1 and
j+1  the diagonal cells are not considered.

Now, if we wanted to interrogate cell 4, say, of a 10-by-10 matrix, we
should have to look at cell 4-10, which does not exist and would crash the
program. Similarly, cell 93+10, say, does not exist; so we have two rows of
guard cells  1 to 10 and 111 to 120  which are not interrogated and are
cleared on each cycle in lines 300 to 350, leaving the interrogated cells
as 11 to 110. Note that the left and right sides of the field are, in
effect, contiguous, so that the field is really cylindrical. This does not
invalidate the models in any way.

In the woodland program, normal, immune and diseased trees are
distinguished by giving the cells values 11, 12 and 13 respectively  lines
1260 to 1420  and the number of neighbours is determined by the sum of the
tens digits in the adjacent cells  line 1760. The various operations on
the cells are carried out in lines 1680 to 1790.

In the spiders and flies program, spiders are given an initial value of
three  line 3350  and this is decremented on each cycle by one, and
incremented by one for each fly consumed  line 3710. Reproduction of
spiders is carried out in lines 3740 to 3760. Reproduction is considered
asexual in this model, but a sexual method could be incorporated.

Matrix cells with flies are given a value of 100  line 3290  which is a
higher value than any spider is likely to reach. Flies' reproduction is
carried out at line 3610 where the number of flies is increased by a factor
of 1.5. It is interesting to experiment with other reproductive factors.

For both parts of the program an index is provided at the start, lines 40
to 170, and instructions for proceeding are displayed at each stage. Each
cycle takes about 15 seconds in fast mode.


