home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / fractal / progs / fractal4 / arcaut / Info_Info < prev    next >
Encoding:
Text File  |  1992-01-15  |  19.5 KB  |  334 lines

  1.  
  2.   Most readers of this magazine will, at one time or another, have come
  3. across a cellular automaton. The most famous of these is John Conway`s
  4. 'Life' created way back in the early 70`s; more recently Archimedes World
  5. carried a version of 'RugWorld', another & quite different cellular
  6. automaton.
  7.  
  8.   To the uninitiated, a cellular automaton (CA) is a simulation. The space
  9. in which this takes place is called the CA universe; this is frequently a
  10. rectangular grid, like a chess board. Each position in the grid is called a
  11. cell. An individual cell can exist in any of a number of predefined states,
  12. the total number of which depends on the particular CA in question. For
  13. example, in 'Life' there are only two states called dead & alive, while in
  14. 'Rugworld' there can be up to 256 distinct states.
  15.   At any moment in time, the configuration of cells in the universe is
  16. called a generation. Usually the screen is used to display the current
  17. generation, with each cell being represented by a single pixel; different
  18. colours corresponding to different states.
  19.   The CA consists of a collection of rules which tell you & the computer how
  20. to calculate what the next generation will look like; this depends only on
  21. the current generation. The simulation simply involves setting up an initial
  22. configuration of cells and then iterating or repeating the application of
  23. these rules to create successive generations; in practice, if we can do this
  24. fast enough, the cell states can be seen to evolve in what looks like a
  25. continuous manner, in the same way that by moving a sprite one pixel across
  26. the screen at a time, we can make it appear to move smoothly. This can
  27. produce some quite stunning results.
  28.  
  29.   Normally certain restrictions are imposed on the rules we are allowed to
  30. use to define a CA. They must be both local and translation invariant. The
  31. former means that the new state of each cell is only allowed to depend on
  32. the prior state of cells nearby, while the latter says that the way each
  33. cell develops does not depend on where that cell is, so if we have the same
  34. pattern of cells at two different locations on screen, then they will both
  35. behave in the same manner.
  36.  
  37.   In order to permit the investigation of general automata, I have written
  38. ArcAut. It is a cellular automaton laboratory, which allows you to experiment
  39. with virtually any rules you can think of, quickly & easily.
  40.   In ArcAut, each automaton is defined in a single text file: this contains
  41. the name & if desired, a description of the automaton; the Basic routine
  42. used to set up any parameters needed by the automaton (this may involve
  43. getting the user to input some values); the routine needed to set up the
  44. screen for the first generation; & finally the code which actually defines
  45. the rules governing the automaton.
  46.   An automaton can be treated as an ordinary text file, for viewing &
  47. editing purposes, using for instance !Edit. To execute the automaton it is
  48. only necessary to install !ArcAut on the icon bar by double clicking on it's
  49. filer icon, & then to drag the textfile onto the !ArcAut icon, either from a
  50. filer window, or from the save dialogue box in say, !Edit. You can
  51. immediately run any of the automatons provided, in this manner.
  52.  
  53.   ArcAut currently allows two different ways of referring to neighbours,
  54. take a look at table 3; for the moment you need only consider the Moore
  55. neighbourhood scheme - if you are viewing these instructions as text within
  56. the desktop environment, then I would recommend you use Mode 16 to read the
  57. tables as they need more than 80 columns.
  58.  
  59.   To clarify the ideas so far covered, let`s consider the classic example of
  60. Life. In this CA, we have only two states, dead & alive. The rules governing
  61. growth are:
  62.  If a cell is alive and has two or three living neighbours then it remains
  63. alive, else it dies;
  64.  If a cell is dead and has precisely three living neighbours then a new
  65. living cell is born, else it remains dead.
  66.   And that is all there is to it. Extremely simple, yet the resulting
  67. behaviour can be very complex. In fact, for the benefit of any doubters
  68. reading this, A CA universe running Life is capable of solving any
  69. computable problem! you could 'easily' simulate the entire operations of
  70. your Archimedes within such an environment (although you would need a pretty
  71. big grid - possibly bigger than any which would fit into the biggest
  72. computer currently in existence). This is only of theoretical importance,
  73. nevertheless, the pictures which you can obtain from a simulation running on
  74. an Archimedes do exhibit astonishing complexity.
  75.  
  76.   If you haven't ever seen 'Life' or are curious about the possibilities for
  77. other CA, then I suggest you take a look at ArcAut now, if you haven't
  78. already done so; if you aren't curious, then why are you reading this?
  79.   As Life is so familiar I would advise you take a look at the text file
  80. which implements it, to get some idea of how an automaton definition is put
  81. together, & perhaps try running several of the automatons. 
  82.   The rules are specified in a reasonably easily readable format which I
  83. will explain later, so you don't need to know any assembly programming. When
  84. an automaton is executed, ArcAut compiles the rules into assembly. This
  85. ensures that the CA will work at a good speed; often this is comparable with
  86. a purpose written assembly equivalent. ArcAut then takes over the full
  87. screen; this may not keep to the spirit of Risc OS, but is quite adequate
  88. when you want to use the full power of your Arc to work with automata;
  89. terminating the run of a particular automaton will take you back to the
  90. desktop at the point that you left it. This allows you to try out automatons
  91. while you are in the process of editing them.
  92.  
  93.   When an automaton is run, you are prompted for a series of values
  94. including the size of the window within which the CA will run; default
  95. values are indicated within { } brackets, & can be selected by pressing just
  96. return. The smaller the window, the faster the CA will progress; although
  97. you sometimes need windows of a certain minimum size in order to observe all
  98. the properties of that automaton. Pressing Menu while the automaton is
  99. running will give you a list of options. This will give you the chance to
  100. freeze the action. The current automaton can be restarted (Again), or the
  101. present generation can be saved as a screen (Save) or replaced with one on
  102. disc (Load); (Next) lets you advance a single generation, while (Cont.)
  103. allows you to resume the run where you left off. (Phase) is described in
  104. table 3. Finally, (Quit) returns control, as previously described, to the
  105. desktop.
  106.  
  107.   To give you some idea of what to expect & what is going on, I have
  108. included a brief description of each automaton provided here (there are
  109. about 24!), within the automaton files. You might care to do this yourself,
  110. with any automatons you devise, if you intend passing them on to anyone
  111. else.
  112.  
  113.   If you have already examined any of the automatons then you will have some
  114. idea of the format in which they are stored.
  115.   Each automaton has the following syntax:
  116.  
  117.  AUTOMATON*
  118.  <name>
  119.  INITIALISATION*
  120.  <basic1>
  121.  SCREEN*
  122.  <basic2>
  123.  CODE*
  124.  <code>
  125.  END*
  126.  
  127.   <name> must contain no spaces, if it is made from several words these
  128. should be separated by '_' characters; each word should contain no more than
  129. 7 characters. Any text following <name> is ignored so you may include a
  130. description of the automaton here.
  131.  
  132.   <basic1> & <basic2> both have the same format. They are both Basic
  133. routines, stored in text form. Line numbers must be used, & should start
  134. with a number no smaller than 10. The main routine to be executed in each
  135. case should be enclosed within the definition of a procedure called 'do'.
  136.   The first is executed at the start of an automaton run, & should set up
  137. any parameters & input any data required from the user. Data can be passed
  138. to the other routine & automaton code, using system variables. Certain
  139. system variables used by ArcAut, need to be set up - see table 4 & take a
  140. look at some examples.
  141.   The purpose of the second is to set up the screen for the first
  142. generation. A graphics window has already been created limiting commands to
  143. the region of the CA universe. A number of useful library routines are
  144. provided & can be used in both sections of Basic code, though they are
  145. probably of most use when setting up the screen (there is no need to load
  146. the library - it will be present); see table 5. Notice that the numerical
  147. values of cell states are in fact the colour values as stored in screen
  148. memory, & not the GCOL values. Thus if you wish to draw a circle on screen
  149. corresponding to cells of state 45, you need to first convert this to a GCOL
  150. number & then select this colour. A function to do this is provided; it is
  151. described in table 5.
  152.  
  153.   The last section, <code>, defines the rules governing the automaton. These
  154. are enclosed within a pair of ( ) brackets. You are free to add as many
  155. spaces & carriage returns between instructions to aid legibility; surplus
  156. separators will be ignored by ArcAut.
  157.   The code consists of a series of commands & a few elementary arithmetic &
  158. logic operators, together with a single simple means of conditional
  159. execution of parts of the code.
  160.   Before describing this you need to understand the principal underlying the
  161. passing of data to operators. The system known as Reverse Polish Notation is
  162. used; this may sound complex & at first seem unnatural but it is quite easy
  163. to get used to. It is the system that most calculators use. Consider the
  164. following simple example.
  165.  To add two numbers together in Basic you would use something like: 2+3
  166.  In RPN this becomes: 2 3 +
  167.  If you want to do 2*(3+5) in Basic you need the brackets to select the
  168. correct order of precedence for the calculations.
  169.  In RPN all you need to do is: 3 5 + 2 *
  170.              or alternatively: 2 3 5 + *
  171.  It is much easier to understand how this works if you know how the computer
  172. goes about evaluating an expression. RPN relies on a stack. A stack is
  173. simply a long list or pile of items. This could be a stack of plates or
  174. books. The main feature of a stack is that you can only access the item at
  175. the top. Similarly items can only be put onto the top of the stack; you
  176. can't insert an item part way down. In this context we are dealing with a
  177. stack of numbers.
  178.   Let us consider the expression '2 3 5 + *' again.
  179.   When the computer comes across the number 2 it places this onto the top of
  180. the stack (which begins empty); the same thing happens as it gets to the
  181. numbers 3 & 5. So by the time we reach the + sign the stack has three items
  182. on it. 5 at the top, then 3, & 2 at the bottom. Now + has the effect of
  183. making the computer take the top two items off the stack, add them & then
  184. place the result on top of the stack. Hence after the + has been executed we
  185. have two items on the stack: 8 at the top & 2 beneath. * works in a similar
  186. way, so at the end we have a single number stored on the stack, 16, the
  187. correct answer.
  188.   This may seem like a very drawn out & unnecessary explanation. If you
  189. already know about RPN then all well & good. It is however worthwhile to
  190. give an elementary explanation as it is impossible to write the code for
  191. CA's if you don't understand these principles. All calculations are done
  192. with RPN. Parameters are passed & returned from commands in the same way.
  193.   In ArcAut you are subject to the rather unusual restriction of only being
  194. allowed upto 3 items on the stack at any one time. This is because the stack
  195. is not stored in memory, but directly in three registers; as such the stack
  196. doesn't actually exist. The reason for this is simple: speed; using a real
  197. stack would slow down the program by a large factor. Also of course, this
  198. has only been done because being restricted to three items is still
  199. practical; you can still write the code for most automatons that you care to
  200. think of, as the examples have already demonstrated. If this is beginning to
  201. confuse you (it confuses me), don't think about it. You will be okay, as
  202. long as you behave as if you were using a stack, but one which can only hold
  203. at most three numbers.
  204.   As the rules for a CA don't depend on where the cell is, & do depend on no
  205. more than the current cell state & those of it's immediate neighbours, all
  206. we need to do is describe a procedure which calculates a cell state in terms
  207. of up to these 9 parameters. To do this you need to be able to refer to
  208. these values.
  209.   The current cell state is referred to by the name CELL.
  210.   All commands & values should be separated by spaces or carriage returns.
  211.   Before you can access the values of the neighbouring cells, or you use any
  212. command which uses these values, you must issue the command READ_NEIG.
  213.   The positions & names of the neighbouring cells depends on which of the
  214. two neighbourhoods has been selected in the initialisation procedure. Table
  215. 3 describes both possibilities.
  216.   You can't assign values to these names (other than by READ_NEIG), they may
  217. only be read for the purpose of calculations.
  218.   A list of operators is given in table 1 together with their meaning & the
  219. number of stack items they require & return.
  220.   A similar list of commands with their purpose is given in table 2.
  221.   All arithmetic is limited to dealing with integers. There are however
  222. further restrictions on the numbers you are allowed to specify in
  223. instructions. Numbers greater than 256 must be a multiple of 4, those
  224. greater than 1024 must be a multiple of 16 & so on. So if you want to add
  225. 257 to the top stack item you would need to do '256 + 1 +'. If you only need
  226. a number of roughly a certain size then FNround(x) can be used to round down
  227. the number x, to one which meets these restrictions. To make the code easier
  228. to read you can embed conventional basic expressions which evaluate to give
  229. a numerical result, in the code using [ ] brackets. System variables can be
  230. accessed simply by enclosing them within < > signs; for example if you
  231. wanted to multiply the top stack item by the value of x^2 where x is a
  232. system variable you could use '[<x>^2] *'. Numbers may also be specified
  233. using system  variables without the [ ]; so to subtract x from the top item
  234. '<x> -' is all that is necessary. The same restrictions on the values of
  235. variables & [ ] expressions exist as for numbers directly specified. This
  236. isn't usually a problem as you are normally working with values that lie in
  237. the range -255 to 255
  238.   When your calculation has finally come up with the value that is to be
  239. used for the new cell state the command == terminates the code. This has the
  240. effect of returning the top stack item as the new cell state. Hence once
  241. this is encountered, subsequent instructions in your code have no effect.
  242.   The last thing you need to know about is the IF statement.
  243.   This is the only control structure provided & is the only means of making
  244. parts of your code conditional. It takes one of two forms.
  245.  'IF (code A)'  and 'IF (code A) ELSE (code B)'
  246.   The condition is provided in the state of the top stack item. 'IF' removes
  247. the top stack item, & proceeds to execute 'code A' if this value is non zero
  248. or 'code B' if present & the value is zero. 'code A' & 'code B' may be any
  249. collection of automaton code including further, nested IF clauses. Usually
  250. the code in an IF clause will eventually terminate with ==, but occasionally
  251. you may want the instructions to alter the state of the stack & then resume
  252. execution after the conditional section. In this case there is a further
  253. restriction. You must ensure that the number of items on the stack after the
  254. conditional code has executed & returned control to the following code, is
  255. the same as if it had not been executed.
  256.  
  257.   If all this is becoming confusing don't worry. In practice the rules for
  258. CA's can be expressed very simply & you won't have the chance to get bogged
  259. down as a result of any of these restrictions & complications, & in a moment
  260. I'll go step by step through an example. If you can follow that, you'll be
  261. able to implement code for your own automatons.
  262.  
  263.   If you make any mistakes then something unpleasant may go wrong. Errors
  264. due to commands/operators not finding enough data on the stack, or running
  265. out of room on the stack for an answer, should be correctly reported. Any
  266. other errors, however, are likely to create an error somewhere else in the
  267. program, & this won't give you much idea what is wrong with your code.
  268. ArcAut has been well tested (over several months) so you can be reasonably
  269. sure that if any strange errors appear they are due to your automaton code,
  270. especially if that is the first time you've attempted to try it out.
  271.  
  272.   Now for the example I promised. We shall go back to 'Life' for this.
  273.   Here is a description of the rules again, followed by some suitable code:
  274.  
  275.  If a cell is alive and has two or three living neighbours then it remains
  276. alive, else it dies; If a cell is dead and has precisely three living
  277. neighbours then a new living cell is born, else it remains dead.
  278.  
  279.  alive always has the value 255 (white)
  280.  dead always has the value 0 (black)
  281.   
  282.  READ_NEIG             get the state of the neighbouring cells
  283.  alive SCOUNT_NEIG     count how many are alive & leave on stack
  284.  CELL IF (DUP          If cell<>0 (cell is alive) then DUP # of living neig.
  285.  2 = IF (alive ==)                if also # living=2 then leave cell alive
  286.  3 = IF (alive ==)             or if also # living=3 then leave cell alive
  287.  dead ==)                    else doesn't have 2 or 3 live neig. so kill it
  288.  ELSE (                else cell=0 (cell is dead)
  289.  3 = IF (alive ==)                if also # living=3 then new live cell born
  290.  ELSE (dead ==)              else doesn't have 3 live neig. so remains dead
  291.  )                                                                         
  292.  
  293.   Now consider what the stack contents look like as this is executed.
  294.  alive SCOUNT_NEIG leaves the number of living neigbours on the top of the
  295. stack. CELL IF puts one value on the stack & then takes it off, so the top
  296. stack item is still the number of living neighbours, regardless of whether
  297. the cell is alive or not.
  298.   If the cell is alive then the number of living neigbours is duplicated;
  299. this is so we can make a comparison with both the values 2 and 3. If the
  300. number of living neighbours is 2 we return with the new cell state of a
  301. living cell; if not then the first of our two copies has been removed & we
  302. are left with one. If this value is 3 then the new cell is alive, otherwise
  303. we know cell is alive & doesn't have 2 or 3 living neighbours so we kill it.
  304.   On the other hand, if the original cell was dead we need only compare the
  305. number of living neighbours (which is still on the top of the stack,
  306. remember) with 3. If it is equal to 3 then the new cell is brought to life,
  307. otherwise it remains dead, & we have accounted for all the possible cases,
  308. so are finished.
  309.  
  310.   Think about that for a moment if it isn't clear. Believe me, if you have
  311. understood that then you will have no problems writing your own code. When
  312. thinking about the state of the stack take another look at tables 1 & 2 to
  313. remind yourself about how many operands each of the operators/procedures use
  314. & leave.
  315.  
  316.   It might be a good idea now to see if you can figure out what the code
  317. defining the other automatons does. The descriptions of several of the
  318. automatons incorporate a wordy explanation of the action of the rules; see
  319. if you can work out how the code executes the algorithm as described in
  320. English.
  321.  
  322.   Hopefully, if you've got this far, you'll soon be writing your own
  323. automatons. ArcAut gives you the flexibility to try out various ideas
  324. quickly, but you'll need some inspiration or imagination to devise something
  325. original. I'm sure that if you do come up with anything exciting, Archimedes
  326. World would be interested to see it & perhaps pass it on to the rest of us.
  327. Also should you find any bugs (I'm sure there aren't any?), then please let
  328. me know.
  329.  
  330.   Good Bye & Good Luck.
  331.  
  332. Bibliography:    Cellular Automata Machines
  333.               by Tommaso Toffoli & Norman Margolus.
  334.