刚用IDL实现的Conway生命游戏的元胞自动机模型,看到了不可思议的图案....

 

;输入参数时,dimI=dimJ

;dimI,二维元胞空间的行数

;dimJ,二维元胞空间的列数

;IterateTimes,本次迭代的数据

PRO CONWAY_LIFE_GAME,dimI,dimJ,IterateTimes

 

;变量定义区

 

;元胞空间,初始值时让对角线为生存。

cellSpace=indgen(dimI+2,dimJ+2)

 

;临时元胞空间,每次计算时临时保存值,每次完成计算后,将值给正式的元胞空间。

tempCellSpace=cellSpace

 

 

 

;用一个100X100的数组表示元胞空间,

;数组元素中的值如果为0,则表示该元胞是死亡状态,在屏幕上用一个白点表示。

;数据元素中的值如果为1,则表示该元胞是生存状态,在屏幕上用一个黑点表示。

;(1)Conway生命游戏是一个二维元胞自动机

;(2)元胞有两种状态,0代表死亡,1代表生存

;(3)元胞的邻居采用Moore型邻居形式。

; (4) 元胞的边界采用"固定边界"的形式进行处理,即边界之外的元胞的状态统一默认为0。

 

;####初始值时让对角线为生存,同时第一、二列为生存。####

 

for subi=0,(dimI+1) do begin

for subj=0,(dimJ+1) do begin

if (subi eq subj) then begin

cellSpace[subi,subj]=1

endif else begin

cellSpace[subi,subj]=0

endelse

endfor

endfor

 

for subi=1,(dimJ) do begin

cellSpace[subi,1]=1

cellSpace[subi,2]=1

endfor

 

cellSpace[0,0]=0

cellSpace[dimI+1,dimI+1]=0

 

;####打印初始元胞空间####

;用RGB方式表操作屏幕的像素颜色,黑色代表生存

data = BytArr(3,dimI,dimI)

;先将屏幕的一个指定区域(100X100变为白色)

for i=0,(dimI-1) do begin

for j=0,(dimI-1) do begin

data[*, i, j] = [255,255,255]

endfor

endfor

 

for i=1,(dimI) do begin

for j=1,(dimJ) do begin

 

if (cellSpace[i,j] eq 1) then begin

data[*,i-1,j-1] = [0,0,0]

endif

endfor

endfor

;        data[*,0, 9] = [255,255,255]

 

        Window,0, XSize = 100, YSize = 100

        Tv, data, /True

 

 

;*****初始化临时元胞空间*******

for subi=0,(dimI+1) do begin

for subj=0,(dimJ+1) do begin

tempCellSpace[subi,subj]=0

endfor

endfor

 

print,cellSpace

 

;for 迭代次数

for iTs=0,(IterateTimes) do begin

 

;对元胞空间中的每一个值,并结合其邻居进行分析,并按照演化规则进行操作

for subi=1,(dimI) do begin

for subj=1,(dimJ) do begin

 

;计算其周围邻居的数量

nNums=0

nNums=nNums+cellSpace[subi-1,subj-1]+cellSpace[subi-1,subj]+cellSpace[subi-1,subj+1]+ $

cellSpace[subi,subj-1]+cellSpace[subi+1,subj-1]+cellSpace[subi+1,subj]+cellSpace[subi+1,subj+1]+ $

cellSpace[subi,subj+1]

 

 

 

;如果该元胞为死亡状态....

if (cellSpace[subi,subj] eq 0) then begin

 

;判断其周围邻居的数量,如果邻居数量为=3,则该元胞为生存状态

if (nNums eq 3) then begin

tempCellSpace[subi,subj]=1

endif

 

;判断其周围邻居的数量,如果邻居的数量不<>3,则该元胞为死亡状态

if (nNums ne 3) then begin

tempCellSpace[subi,subj]=0

endif

endif

 

;如果该元胞为生存状态...

if(cellSpace[subi,subj] eq 1) then begin

;判断其周围邻居的数量,如果邻居数量为=3 or =2,则该元胞为生存状态

 

if(nNums eq 3) or (nNums eq 2) then begin

tempCellSpace[subi,subj]=1

endif

 

;判断其周围邻居的数量,如果邻居数量为>3或<2,则该元胞为死亡状态

if(nNums gt 3) or (nNums lt 2) then begin

tempCellSpace[subi,subj]=0

endif

 

endif

 

endfor

endfor

 

;将临时元胞空间的值赋给正式的元胞空间

cellSpace=tempCellSpace

print,"Afterward"

;print,cellSpace

 

 

 

;用RGB方式表操作屏幕的像素颜色,黑色代表生存

data = BytArr(3,dimI,dimI)

 

;先将屏幕的一个指定区域(100X100变为白色)

for i=0,(dimI-1) do begin

for j=0,(dimI-1) do begin

data[*, i, j] = [255,255,255]

endfor

endfor

 

for i=1,(dimI) do begin

for j=1,(dimJ) do begin

 

if (cellSpace[i,j] eq 1) then begin

data[*,i-1,j-1] = [0,0,0]

endif

endfor

endfor

;        data[*,0, 9] = [255,255,255]

 

;its为iterateTimes,代表演化的步数

        Window ,its+1, XSize = 100, YSize = 100

        Tv, data, /True

 

;endfor 迭代次数

endfor

 

 

 

END


posted on 2009-09-23 02:12  Hi Jew  阅读(833)  评论(0编辑  收藏  举报