Advertisement
Francoo

Autômato Celular Elementar

Sep 10th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.97 KB | None | 0 0
  1. % Simulates any elementary automaton rule.
  2. %Rev1 speed is ~20k cells/s, rev2 ~200k cells/s, rev3 ~1000k cells/s, rev4 ~2M cells/s on AMD Phenom II X4 955 at 3.2GHz.
  3. %There are multiple versions here, please select one (I recommend rev5).
  4. % It runs a cellular automata with the specified rule (int 1~255), for a specified amount of steps (int > 1) and a initial pattern (line vector).
  5.  
  6. ---------------------------------------------------------------------------------
  7.  
  8. %Rev 1.0 (reconstruction, for comparison):
  9.  
  10. function a = cellsim(rule,steps,initial)
  11.  
  12. cells=[0,0,initial(1,:),0,0]; % Bidimensional array for cells with zeros border.
  13. cells(2,:)=zeros();
  14. rule=de2bi(rule,8,'right-msb'); % Transform decimal rule into binary matrix.
  15. state=0;
  16.  
  17. for cstep = 2:steps+1
  18.     for k = 2:size(cells,2)-1
  19.         state=bi2de(cells(cstep-1,k-1:k+1),'left-msb')+1;
  20.         % Cell/state check: (checks state of neighbors cells and applies rule)
  21.         if rule(1,state) == 1
  22.             cells(cstep,k)=1;
  23.         else
  24.             cells(cstep,k)=0;
  25.         end
  26.     end
  27.     % Boundaries checks: (resize matrix so it won't overflow)
  28.     if cells(cstep,2) == 1
  29.         cells(:,2:end+1)=cells(:,:); % Move matrix right and fill cells with zeros.
  30.     end
  31.     if cells(cstep,end-1) == 1
  32.         cells(:,end+1)=zeros(); % Add right column filled with zeros.
  33.     end
  34. end
  35.  
  36. cells=cells(:,3:end-2);
  37.  
  38. a=cells;
  39.  
  40. ---------------------------------------------------------------------------------
  41.  
  42. %Rev 2.0: Changed function "bi2de" to a matrix multiplication, made it ~10x faster (from 19k points/s to >200k points/s).
  43.  
  44. function a = cellsim(rule,steps,initial)
  45.  
  46. cells=[0,0,initial(1,:),0,0]; % Bidimensional array for cells with zeros border.
  47. cells(2,:)=zeros();
  48. rule=de2bi(rule,8,'left-msb'); % Transform decimal rule into binary matrix.
  49.  
  50. for cstep = 2:steps+1
  51.     for k = 2:size(cells,2)-1
  52.         % Cell/state check: (checks state of neighbors cells and applies rule)
  53.         if rule(1,8-cells(cstep-1,k-1:k+1)*[4;2;1]) == 1
  54.             cells(cstep,k)=1;
  55.         else
  56.             cells(cstep,k)=0;
  57.         end
  58.     end
  59.     % Boundaries checks: (resize matrix so it won't overflow)
  60.     if cells(cstep,2) == 1
  61.         cells(:,2:end+1)=cells(:,:); % Move matrix right and fill cells with zeros.
  62.     end
  63.     if cells(cstep,end-1) == 1
  64.         cells(:,end+1)=zeros(); % Add right column filled with zeros.
  65.     end
  66. end
  67.  
  68. cells=cells(:,3:end-2);
  69.  
  70. a=cells;
  71.  
  72. ---------------------------------------------------------------------------------
  73.  
  74. %Rev 3.0: Changed loop for determining the value of past cells to a matrix manipulation, on a single turn. About 4x faster. (~1M points/s).
  75.  
  76. function a = cellsim(rule,steps,initial)
  77.  
  78. cells=[0,initial(1,:),0]; % Bidimensional array for cells with zeros border.
  79. cells(2,:)=zeros();
  80. rule=de2bi(rule,8,'right-msb'); % Transform decimal rule into binary matrix.
  81.  
  82. for cstep = 2:steps+1
  83.     past=[cells(cstep-1,:),0,0;0,cells(cstep-1,:),0;0,0,cells(cstep-1,:)]'*[4;2;1]+1;
  84.     for k = 2:size(past,1)-1
  85.         % Cell/state check: (checks state of neighbors cells and applies rule)
  86.         if rule(1,past(k,1)) == 1
  87.             cells(cstep,k-1)=1;
  88.         else
  89.             cells(cstep,k-1)=0;
  90.         end
  91.     end
  92.     % Boundaries checks: (resize matrix so it won't overflow)
  93.     if cells(cstep,1) == 1
  94.         cells(:,2:end+1)=cells(:,:); % Move matrix right and fill cells with zeros.
  95.     end
  96.     if cells(cstep,end) == 1
  97.         cells(:,end+1)=zeros(); % Add right column filled with zeros.
  98.     end
  99. end
  100.  
  101. cells=cells(:,3:end-2);
  102.  
  103. a=cells;
  104.  
  105. ---------------------------------------------------------------------------------
  106.  
  107. %Rev 4.0: Matrix preallocation on both axes.
  108.  
  109. function a = cellsim(rule,steps,initial)
  110.  
  111. rule=de2bi(rule,8,'right-msb'); % Transform decimal rule into binary matrix.
  112.  
  113. cells(steps,size(initial,2)+2*steps)=zeros();
  114. cells(1,steps+1:steps+size(initial,2))=initial(1,:); % Preallocating matrix
  115. start=steps; % Virtual limits
  116. final=size(initial,2)+steps;
  117.  
  118. for cstep = 2:steps+1
  119.     past=[cells(cstep-1,2:end),0;cells(cstep-1,:);0,cells(cstep-1,1:end-1)]'*[4;2;1]+1;
  120.     for k = start:final
  121.         % Cell/state check: (checks state of neighbors cells and applies rule)
  122.         if rule(1,past(k,1)) == 1
  123.             cells(cstep,k)=1;
  124.         else
  125.             cells(cstep,k)=0;
  126.         end
  127.     end
  128.     if cells(cstep,start) == 1
  129.         start=start-1;
  130.     end
  131.     if cells(cstep,final) == 1
  132.         final=final+1;
  133.     end
  134. end
  135.  
  136. a=cells(:,start+1:final-1);
  137.  
  138. ---------------------------------------------------------------------------------
  139.  
  140. %Rev 4.1: Order selection (elementary or second order).
  141.  
  142. function a = cellsim(rule,order,steps,initial)
  143.  
  144. rule=de2bi(rule,8,'right-msb'); % Transform decimal rule into binary matrix.
  145.  
  146. cells(steps,size(initial,2)+2*steps)=zeros();
  147. cells(1,steps+1:steps+size(initial,2))=initial(1,:); % Preallocating matrix
  148. start=steps; % Virtual limits.
  149. final=size(initial,2)+steps;
  150.  
  151. for cstep = 2:steps
  152.     past=[cells(cstep-1,2:end),0;cells(cstep-1,:);0,cells(cstep-1,1:end-1)]'*[4;2;1]+1;
  153.     for k = start:final+1
  154.         % Cell/state check: (checks state of neighbors cells and applies rule)
  155.         if xor(rule(1,past(k,1)) == 1, (order-1)*cells(max(cstep-2,1),k))
  156.             cells(cstep,k)=1;
  157.         else
  158.             cells(cstep,k)=0;
  159.         end
  160.     end
  161.     if cells(cstep,start) == 1 % Modifying "virtual" limits if necessary.
  162.         start=start-1;
  163.     end
  164.     if cells(cstep,final+1) == 1
  165.         final=final+1;
  166.     end
  167. end
  168.  
  169. a=cells(:,start+1:final-1); % Cropping matrix.
  170.  
  171. ---------------------------------------------------------------------------------
  172.  
  173. %Rev 5: Parallel method, very simple & fast. Completely "manual".
  174.  
  175. function a = cellsim(rule,steps,initial)
  176.  
  177. rule=de2bi(rule,8,'right-msb'); % Transform decimal rule into binary matrix.
  178.  
  179. cells=zeros(steps,size(initial,2));
  180. cells(1,:)=initial;
  181.  
  182. for cstep = 2:steps+1
  183.     past=([cells(cstep-1,2:end),0;cells(cstep-1,1:end);0,cells(cstep-1,1:end-1)]'*[4;2;1]+1)';
  184.     for k = 1:8
  185.         past(past==k)=rule(1,k);
  186.         cells(cstep,:)=past;
  187.     end
  188. end
  189.  
  190. a=cells;
  191.  
  192. ----------------------------------
  193.  
  194. %Rev 6:
  195.  
  196. function a = cellsim(rule,rule2,order,steps,initial)
  197.  
  198. rule=de2bi(rule,8,'right-msb'); % Transform decimal rule into binary matrix.
  199. rule(2,1:8)=de2bi(rule2,8,'right-msb');
  200.  
  201. cells=zeros(steps,size(initial,2));
  202. cells(1,:)=initial;
  203.  
  204. for cstep = 2:steps+1
  205.     past=([cells(cstep-1,2:end),0;cells(cstep-1,1:end);0,cells(cstep-1,1:end-1)]'*[4;2;1]+1)';
  206.     for k = 1:8
  207.         past(past==k)=rule(1,k);
  208.     end
  209.     if order==1 || cstep==2
  210.         cells(cstep,:)=past;
  211.     elseif order==2
  212.         cells(cstep,:)=bitxor(past,cells(cstep-2,:));
  213.     elseif order==3
  214.         tpast=([cells(cstep-2,2:end),0;cells(cstep-2,1:end);0,cells(cstep-2,1:end-1)]'*[4;2;1]+1)';
  215.         for k = 1:8
  216.             tpast(tpast==k)=rule(2,k);
  217.         end
  218.         cells(cstep,:)=bitxor(past,tpast);
  219.     end
  220. end
  221.  
  222. a=cells;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement