5 { Found this in the book *The Computational Beauty of Nature*. --drummyfish }
7 Resnick's termite is a simple [cellular automaton](cellular_automaton.md) simulating behavior of ants, demonstrating how even a very dumb behavior of a single agent can lead to higher collective intelligence once we increase the number of the agents. The simulation was made by Mitchel Resnick, the theme is similar to that of [Langton's ant](langtons_ant.md) but Resnick's termites are [stochastic](stochasticism.md), [nondeterministic](determinism.md), they rather show how statistics/[randomness](randomness.md) in behavior help many ants build tunnels in sand. The game demonstrates how randomly scattered chips start getting chunked together and form tunnels once we let ants with extremely simple behavior work together on moving the chips. Besides this demonstration however there doesn't seem to be anything more interesting going on (at least until we start to modify and tweak the thing somehow).
9 The system is defined quite simply: we have a world made of cells, each cell can be either empty or have a wooden chip on it. In this world we have a number of ants, each of which behaves by the following [algorithm](algorithm.md):
11 1. Randomly walk around until you bump into a chip.
12 2. If you are not carrying a chip, pick up the one you bumped into, otherwise drop the chip you are carrying. Go to step 1.
14 The original implementation had ants who had direction (up, right, down, left) and on each step could make a random turn to the right or left. If an ant bumped into a chip it turned 180 degrees. These things prevented some annoying patterns like an ant just picking up a chip and immediately dropping it etc. Some further modifications were suggested like giving the ants some simple sense of sight or teleporting them randomly after dropping the chip.
18 ----------------------------------------------------------------
19 | , , ' ' , ' , ; ; ' ',,'' ', ' ' |
20 | ' , , ' ' ' ',, ;' ,,, ,,, ; ' ;, |
21 |, , ', ; ' ' ', ' , ' ',' ' ,, '' , ',|
22 | ' ,;'' , ,', , , ' , '',' '',; ' , ,, ', |
23 | , ',, ,,', , , ; ;', ,';' ,', ' , ' ;; ', |
24 | ', ' ' ; ,, , , , ' , , ' , , , ' |
25 | , ,', ,' ' '' ' ,' ' ; , ' ' ; , , ' ,, , , '|
26 | , '' '' ' , ; ; ,;' '' ; ; ' '|
27 | , ,, ;'' ', ; ' ' ' ,' ,,,, , , ', ,',,'; |
28 | ' '',,' , ' ' , '',, ,, ,',' ' ; ' ' ,; |
29 |',, ' , , , ' , , ' ;,, ' ' ,, ,';, , ; ;, ' ;|
30 |, ' ' ' ' ;, ,,,; ', ; ' , ' '; , ' ; , ';, , |
31 | ' ,' ', ' , , ' ', '' ' , ; ; ,, ,,, ;, ',' ', ' |
32 |',, ' , ''' ' ,, ',' ' ' '' ,, , ', ' ',''|
33 | , , , ,,';,;,, , , ' ,' ', ' ; ' |
34 | , ' ,' , ; ' , , , , ' , '; ,, , ','', |
35 | ,', ,' ' ,, ''' , ' ' ', ', ,,, ',|
36 | ,',, ,, '; ,' ' ' ', ' , ' , ' ,; ; ' |
37 |'' ',' ' , ' ,, , ' , ; ' ,'' , ,' ; ,', |
38 | , ' ; , ' '''; , '',' , ' , '' ' ', |
39 | ' , ' ' ' , , ' ,' '' ,', , ;,',,', '|
40 | ' ', ''''; ''' , ' ,', ,'' ;' , , ' , , ;|
41 |,, , ', ' , ;'' ' ' ,', ' , ,' ,, ' , |
42 | ',', ' , ',' ,;, ,; ',,, ' ', ' ; |
43 |' , ' , ' , ' ' , ; ' ' , ; ,; '' ' ,'' |
44 | ; , ,;,; ' , ' '' , , , , ,,,' ' ,,' ,|
45 | ' ,' '' ',, ' ', ' ; ; , ,, ' , |
46 | , ; , ,;' , ' , '' ' '', , ; , , ,''' ' '|
47 |, ;, , ' ,, ; ',,;,;'; ; ; , '' , ', |
48 |,' '; , , ,, ' ,' '' ' ,' , '' ' ,, , ', , |
49 |; ' ''' ' , , , ' ' , ,, ,' |
50 |; , ' ' ' ' ', ''',, ', , ' , ,'|
51 ----------------------------------------------------------------
54 ----------------------------------------------------------------
55 | , ;;'; ' ;'; ';,, , ' , , ;' ,''; |
56 | ';, ; ,,, , ,, ;,,'', ,,, ,; ; |
57 | , ' ,' ,, '';' ' ' , ' ' '';|
58 |, ,, , ,, ''; , ; ,';;,; |
59 |' , ,,,'' '' ' ' ' ' '' ;' ,'''|
60 |, ''; ,, ,'; , ;, ,, ; ; ,, ; |
61 | ' , '' ' ';, ' ',; ; '';, ,' |
62 | '', ' ', , ; '', '' ' , '',,, ' ,'|
63 |, ; , ;' ' ; , ; ;; ,, ;|
64 |, ; '; , ' ,;' ;;' ,''; |
66 |; ',' ,';,;' ' ,, , ' ; ', |
67 |;, ,, ' ',;, , ;,,' |
68 | ,';' ;'; ' ,,, '; '|
69 |,, , ' , ' ;'''', ' ; , ' |
70 |' ;,,;' '' ';,, ' ,, ; ';' ''' ,, ,,, ,|
71 |,, ;, , ';; , ; ;, ', ,, ;' ,, ;;|
72 | ;, ,;,;', ' ;;, '' ', ; |
73 | ; , ' ,'' ,,' ,, ;' ;;,;; ' ',,;'' , |
74 | ,; , , '' ' ,;, ,, ''' ' ;' |
75 | ; ,;, , '' ' ;; ;; ; ' ' ,,' '' ,,; ,' ,|
76 | , '' ';; ''' ,,;' ''' ' ' ; ;;' |
77 | ,; ' ;;; ,, ,,''; ; ;', ;,,; , |
79 |'' ,;,, ''; ' '' '' ,, |
80 |'' ;, ,,, ;' '';,, ;' , , |
84 | , ', '' , ; , ;, ;, |
85 | '',,'' '' ,' ' ;, '' |
86 | ,,'' ,, ' ; '' ' ; ';' ' ' ' ,, , ; ' |
87 ----------------------------------------------------------------
90 Here is an extremely basic implementation in [C](c.md) (without the fancy behavior improvements mentioned above, to keep the code short):
98 #define CHIP_DENSITY 5
100 unsigned char world[WORLD_SIZE * WORLD_SIZE]; // 0: empty, 1: chip
110 void printHBorder(void)
112 for (int i = 0; i < WORLD_SIZE + 2; ++i)
113 putchar((i != 0 && i != WORLD_SIZE + 1) ? '-' : ' ');
118 void printWorld(void)
122 for (int y = 0; y < WORLD_SIZE; y += 2)
125 for (int x = 0; x < WORLD_SIZE; ++x)
127 int n = y * WORLD_SIZE + x;
129 switch ((world[n] << 1) | (world[n + WORLD_SIZE]))
131 case 1: putchar('\''); break;
132 case 2: putchar(','); break;
133 case 3: putchar(';'); break;
134 default: putchar(' '); break;
145 void updateAnts(void)
147 for (int i = 0; i < ANTS; ++i)
149 int newPos = // this just randomly moves in one direction
150 (WORLD_SIZE * WORLD_SIZE +
152 ((rand() % 2) ? ((rand() % 2) ? -1 : 1) :
153 ((rand() % 2) ? -1 * WORLD_SIZE : WORLD_SIZE)))
154 % (WORLD_SIZE * WORLD_SIZE);
156 if (world[newPos]) // stepped on a chip?
159 { // has chip; drop the chip
160 if (!world[ants[i].pos])
163 world[ants[i].pos] = 1;
167 { // no chip; pick up the chip
173 ants[i].pos = newPos;
181 for (int i = 0; i < WORLD_SIZE * WORLD_SIZE; ++i)
182 world[i] = (rand() % CHIP_DENSITY) == 0;
184 for (int i = 0; i < ANTS; ++i)
186 ants[i].pos = rand() % (WORLD_SIZE * WORLD_SIZE);
196 printf("iteration %d:\n",i);