------------------------------------------------------------------------- PHYS210 CELLULAR AUTOMATA LAB SUMMARY 1) Octave scripts to play Conway's Game of Life (2-dimensional, 2-state cellular automaton) life.m -> version that uses for loops lifea.m -> version that uses whole array operations 2) Demonstration of xflat2d_rgb, utility for visualization of cellular automata (CA) results 3) Implementation suggestions for CA programs, and use of latout function to write CA data to a file in the format needed by xflat2d_rgb ------------------------------------------------------------------------- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1) Scripts to play Conway's Game of Life +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - DEFINITION OF life (uses for loops) >> type life life is the user-defined function defined from: /home/phys210/octave/life.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Plays Conway's game of life: % % 2D 2-state cellular automata using 8 cell neighbourhood and % the following update rule (states are 0 ("dead") and 1 ("live")): % % S_{n} N-live S_{n+1} . . : % Update the cell state according to the rules if c(i,j,n) == 1 if (count == 2) | (count == 3) c(i,j,n+1) = 1; else c(i,j,n+1) = 0; end else if count == 3 c(i,j,n+1) = 1; else c(i,j,n+1) = 0; end end end end end % Define colours for latout ... rgb = [[0.3 0.3 0.3]; [1.0 0.0 0.0]]; % Output the entire evolution of the arena states latout('life.dat', c, rgb, 10); - DEFINITION OF lifea (uses whole array operations) >> type lifea lifea is the user-defined function defined from: /home/phys210/octave/lifea.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Plays Conway's game of life: % % 2D 2-state cellular automata using 8 cell neighbourhood and % the following update rule (states are 0 ("dead") and 1 ("live")): % % S_{n} N-live S_{n+1} . . . cn = c(x0:x1, y0:y1, n); % ... and compute the new states using the rules of the game. cnp1 = (~cn .* (count == 3)) | (cn .* ((count == 2) | (count ==3))); c(x0:x1, y0:y1, n+1) = cnp1; end % Define colours for latout_rgb ... rgb = [[0.3 0.3 0.3]; [0.0 1.0 0.0]]; % Output the entire evolution of the arena states ... latout('lifea.dat', c, rgb, 10); - Both life and lifea use the function 'latout', which outputs CA data in form expected by xflat2d_rgb - EXECUTE life >> life id_type = 0 nx = 50 ny = 50 nt = 200 life: Step 1 of 200 life: Step 2 of 200 life: Step 3 of 200 life: Step 4 of 200 life: Step 5 of 200 life: Step 6 of 200 life: Step 7 of 200 life: Step 8 of 200 life: Step 9 of 200 . . . latout: Wrote 140 of 200 latout: Wrote 150 of 200 latout: Wrote 160 of 200 latout: Wrote 170 of 200 latout: Wrote 180 of 200 latout: Wrote 190 of 200 latout: Wrote 200 of 200 latout: Wrote nx=50 ny=50 nt=200 to file=life.dat - EXECUTE lifea --- note how it runs much faster than life (due to its use of whole-array operations, rather than loops over individual array operations) >> lifea id_type = 0 nx = 50 ny = 50 nt = 200 lifea: Step 1 of 200 lifea: Step 2 of 200 lifea: Step 3 of 200 lifea: Step 4 of 200 lifea: Step 5 of 200 lifea: Step 6 of 200 lifea: Step 7 of 200 lifea: Step 8 of 200 lifea: Step 9 of 200 . . . latout: Wrote 140 of 200 latout: Wrote 150 of 200 latout: Wrote 160 of 200 latout: Wrote 170 of 200 latout: Wrote 180 of 200 latout: Wrote 190 of 200 latout: Wrote 200 of 200 latout: Wrote nx=50 ny=50 nt=200 to file=life.dat - NOTE: You can experiment with the lifea script (recommended, since it runs faster), by copying it to your octave directory % cp ~phys210/octave/lifea.m . and then modifying parameters in the script such as the lattice dimensions (nx, ny), number of generations (time steps), initial data type (id_type = id_random for random initial conditions, id_type = id_glider for a single "glider") +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2) Demonstration of xflat2d_rgb, utility for visualization of cellular automata results +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - IMPORTANT: xflat2d_rgb must be executed from the shell, NOT in octave - In a terminal window, change to your octave directory % cd % cd octave - Ensure that you have previously run life and lifea from an octave session that was started from your octave directory - You should see 'life.dat' and 'life.dat' in a listing of that directory - FIRST, get help for the application, using the -h option % xflat2d_rgb -h usage: xflat2d_rgb [-h] Displays animations of two dimensional binary lattices colored with arbitrary RGB values. xflat2d_rgb expects the following formatted input on standard input: ! Dimensions of lattice ! FIRST display time ! Lat, RGB values for site (1,1) ! Lat, RGB values for site (2,1) . . ! Lat, RGB values for site (nx,1) ! Lat, RGB values for site (1,2) . . ! Lat, RGB values for site (nx,2) o o o Lattice positions (1,1) and (nx,ny) correspond to the upper left and lower right positions of the display area respectively. Input is stream oriented so carriage returns are effectively ignored. Thus for example, the lattice values at a given time could appear on a single line, one per line, one row per line etc. - NOW USE xflat2d_rgb TO VISUALIZE THE OUTPUT FROM life AND lifea - IMPORTANT!! As with xfpp3d, xflat2d_rgb only takes input from the standard input, so USE INPUT REDIRECTION ('<') % xflat2d_rgb < life.dat % xflat2d_rgb < lifea.dat - I will demonstrate the usage of 'xflat2d_rgb', but its controls (including the mpeg generation panel) are very similar to those of xfpp3d +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3) Implementation suggestions for CA programs, and use of latout to write CA data to a file in the format needed by xflat2d_rgb +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ I) If your CA has 'nstate' states, use consecutive integers to represent them, i.e. 0, 1, ..., nstate - 1 or 1, 2, ..., nstate II) Use a 3-dimensional array (called 'c' here) to store your CA data. Define/initialize the array as follows: c = zeros(nx, ny, nt) so that c has dimensions nx x ny x nt, where nx, ny, nt are nx: size of lattice in x-direction nz: size of lattice in y-direction nt: number of time steps in simulation MAKE SURE THAT THE TIME DIMENSION IS THE LAST ONE, SO THAT YOU CAN USE latout (see below) DIRECTLY FOR EFFICIENCY, YOU SHOULD *DEFINITELY* USE zeros(...) AS ABOVE TO CREATE THE 3D LATTICE BEFORE THE SIMULATION PER SE IS STARTED III) Define an RGB array that gives the colors which will be used by xflat2d_rgb for each state value. For example, if your model has four states -- 1, 2, 3 and 4 -- and you want to use the colors red, green, yellow and white for them, then define rgb = [ [1.0 0.0 0.0]; [0.0 1.0 0.0]; [1.0 1.0 0.0]; [1.0 1.0 1.0] ]; i.e. rgb is an nstate x 3 array, where each row is an RGB (red-green-blue) triple defining a color (each R, G or B value must be in the range 0.0 to 1.0) IV) Assuming that you have followed/implemented these "hints" (and you should!), then once your script has completed the simulation, so that each element of the array has been defined (i.e. all nx x ny x nt elements have been assigned a state value), use latout('ca.dat', c, rgb); to write the CA data to a file (called ca.dat here, you can use whatever name you wish), that you can then visualize via % xflat2d_rgb < ca.dat REMEMBER to execute xflat2d_rgb from the shell, not octave, and use input redirection (<) ALSO NOTE THAT latout WILL OVERWRITE THE OUTPUT FILE (ca.dat in this example) IF IT EXISTS, SO BE SURE TO RENAME/MOVE THE EXISTING FILE, SHOULD YOU WANT TO PRESERVE ITS CONTENTS V) UNLESS YOU ARE CONFIDENT IN YOUR ABILITY TO PROGRAM IN OCTAVE USING WHOLE-ARRAY OPERATIONS, YOU SHOULD FIRST WRITE YOUR PROJECT CODE WITH LOOPS. THEN WRITE ANOTHER VERSION USING ARRAY OPERATIONS, SHOULD YOU HAVE THE TIME (AND ONLY IF YOU FIND THAT YOUR LOOP VERSION IS RUNNING TOO SLOWLY FOR YOUR PURPOSES). VI) In an octave session, use >> help latout for additional usage information for latout, and >> type latout to see the function definition VII) AS ALWAYS, contact me should you have any questions about the scripts and functions discussed here, or for other help with your projects