This project is inspired by two simpler models: one of termites gathering wood
chips into piles and one of moving sheep.  In this project, sheep wander randomly
while shepherds circulate trying to herd them.  The shepherds follow a set of
simple rules.  Each shepherd starts wandering randomly.  If it bumps into a
sheep, it picks the sheep up, and continues to wander randomly. When it bumps
into another sheep, it finds a nearby empty space, puts its sheep down, and looks
for another one.  Whether or not the sheep eventually end up herded and in a
single pile depends on the number of shepherds and how fast they move compared to
the sheep.

Click the SETUP button to set up the shepherds (brown) and  sheep (white).  Click
the GO button to start the simulation.   A shepherd turns blue when it is
carrying a sheep.

There are four sliders.  NSHEEP and NSHEPHERDS control the numbers of sheep and
shepherds, respectively.   Changes in these sliders do not take effect until the 
next setup.  The SPEED-RATIO slider controls the speed of the sheep relative to
the shepherds.  The SIM-DELAY slider can be used to slow down the speed of the
simulation.  These two sliders can be changed while the model is running.

Click the GRAPH button (after you press GO) to see a dynamic graph of the
shepherds' herding efficiency.  Herding efficiency is measured here by counting
the number of patches that have no sheep in their neighborhood, compared to the
initial number of such patches.   Note that the efficiency should be zero to
start with.

Herding Efficiency = (sheepless neighborhoods  - initial sheepless neighborhoods)
/ initial sheepless patches

As the shepherds herd the sheep, more of the neighborhoods should be empty. The
scale of efficiency is somewhat arbitrary.

As small herds of sheep begin to form, the herds are not "protected" in any way. 
That is, shepherds sometimes take chips away from existing herds.  That strategy
might seem counter-productive. But if the herds were "protected", you would end
up with lots of little herds, not several big ones.   Why is this?

In general, if there are enough shepherds and/or the shepherds move much faster
than the sheep, the number of herds decreases with time. Why?  One explanation is
as follows: some herds disappear, when shepherds carry away all of the sheep.  If
sheep never moved, it would not be possible for a new herd to start from scratch,
since  shepherds always put their sheep near other sheep.  So the number of herds
would necessarily decrease over time.  (The only way a "new" herd would start is
when an existing herd splits into two.) However, since sheep move, they can form
new herds. If they move too fast relative to the shepherds, the herding will
break down.

If there are not enough shepherds, or if the sheep move fast enough relative to
the shepherds, the shepherds cannot keep up with the wanderings of their sheep,
and the sheep will disperse.

Are the final herds are roughly round?  What other physical situations also
produce round things?

This project is a good example of a probabilistic and decentralized strategy.
There is no shepherd in charge, and no special pre-designated  site for the
herds. The movement of the shepherds and sheep and thus their behavior is
probabilistic. Each shepherd follows a set of simple rules, but the group as a
whole accomplishes a rather sophisticated task.

Can you find the minimum number of shepherds needed to herd a given number of
sheep?  Which helps more, doubling the number of shepherds or doubling the speed
of the existing shepherds (by cutting the SPEED-RATIO in half)?

How many sheep can one individual shepherd keep in a group?

Start with a SPEED RATIO of zero (the sheep stay put), let the shepherd gather
them into herds, and then slowly increase the SPEED RATIO.   How is the herding
efficiency affected?   How big does the SPEED-RATIO need to be for the shepherds
to useless, namely, the herding efficiency returns to its initial value of zero? 
This is the same as saying that the distribution of sheep is no better than

When there are just two or three herds left, which of them is most  likely to
"win" as the single, final herd?  How often does  the larger of the two herds
win?  If one herd has only a  single sheep, and the other herd has the rest of
the sheep, what are the chances that the first herd will win?

Compare this model to "Termites".  It's slower, but aside from that are the
results the same?

It was noticed in both the Termites and the Shepherds models that if the turtles
don't jump away from the piles they make, herding happens more slowly and to a
lesser extent.  Does this make sense?  Experiment with different search commands
that you might give the shepherds besides "fd 1".

Can you find other ways than that given to measure herding efficiency?

Can you change the model so the shepherds actually herd the sheep rather than
killing them and recreating them?

Can you extend the model so that sheep follow each other, tending to cluster?

Can you extend the model to have the shepherds sort white sheep from black sheep?

The way this program is currently written, multiple sheep are allowed to occupy
the same physical location.  And, since all shepherds search for a sheep to pick
up before any of them actually take their sheep away, a shepherd may come to a
location with several sheep and, examining one at random, find that another
shepherd has already laid ahold of that sheep. Currently shepherds give up on all
sheep at that location when this happens, rather than seeing if there are other
sheep there which are still unattended. (If shepherds did not check to see
whether a sheep was attended, multiple shepherds might each pick up the same
sheep and take it away, thereby cloning it!)  Can you find a way to make
shepherds check all sheep at a location before leaving?

Can you change the model so that there's only one sheep on a patch?  Does it
change the behavior of the model?

Real shepherds often use sheepdogs to help them with their herding. A sheepdog in
this context might put down some chemical which "scares" the sheep, i.e.,
wandering sheep try to avoid it and move down gradient. Can you implement
sheepdogs and see how helpful they are?  Can you come up with a rough equivalence
of how many shepherds a sheepdog can replace (to maintain the same herding
efficiency), or how many sheepdogs are needed to replace a single shepherd?

Compare this code to that of "Termites".  It's very similar, except that sheep
and shepherds are both turtles here, and in Termites the wood chips are patches. 
  As a result, the Termites model runs much faster.

Since it would be difficult to force sheep-turtles to follow shepherd-turtles
which have "picked them up", the mechanics of picking a sheep up actually involve
"killing" the sheep and creating a sheep-shepherd collective which wanders around
following shepherd rules until it finds a place to put the sheep down, at which
point another sheep is "created" at that location, and the collective reverts to
being a normal shepherd again.

Both Termites a Shepherds use a loops-within-GO structure that is worth noting. 
GO calls up a series of loops, each of which is executed by every turtle until a
certain condition is satisfied.  Then each turtle goes on to the next loop.  Each
turtle runs as fast as it can.   To make SIM-DELAY look realistic, a "wait"
command is put after every "forward" or "jump" in all of these loops.

Note that the GRAPH button is separated from the GO button. If the call to GRAPH
were inside GO, then GO would become an observer procedure and thus all turtles
would synchronize, giving their movements an unnatural appearance (Try it).  Note
that AGE is updated only when the GRAPH button is down.  For some reason, the
GRAPH button must be pushed after the GO button is down.  This is a bug in

Painted Desert Challenge