元胞自動機——雄安新區城市規劃模擬
5 人贊了文章
首先把圖7的像素調低一點,便於模擬模擬(畫圖工具)
程序1
運行環境:Matlab2011a/2014a
clear;close all
I=imread(city.png);
level=graythresh(I); % 圖像灰度處理
bw=im2bw(I,level); % 圖像二值化處理
I=bw;
cells=double(I);
%定義 button
plotbutton=uicontrol(style,pushbutton,...
string,Run,...
fontsize,12,...
position,[100,400,50,20],...
callback,run=1;);
erasebutton=uicontrol(style,pushbutton,...
string,Stop,...
fontsize,12,...
position,[300,400,50,20],...
callback,freeze=1;);
number=uicontrol(style,text,...
string,1,...
fontsize,12,...
position,[20,400,50,20]);
%初始化
cells(33,44)=2;
cells(88,31)=2;
cells(33,80)=2;
%參數定義
kuosan=0.2;fanzhi=0.2;chuanbo=0.2;chengshihua=0.0004;sch=[];skz=[];t=1;
[x,y]=size(cells);
run=0;
freeze=0;
stop=0;
while (stop==0)
if(run==1)
for i=1:x
for j=1:y
if(cells(i,j)~=1)
if(cells(i,j)==0) %自然增長
if(rand<chengshihua)
cells(i,j)=2;
end
if(aroundcenter(i,j,cells))
if(rand<chuanbo)
cells(i,j)=2;
end
end
end
if(cells(i,j)==2 && rand<kuosan) %邊界增長
if(exist1(i,j,cells))
m=1+3*rand;
switch m
case 1
ii=i-1;jj=j;
case 2
ii=i;jj=j-1;
case 3
ii=i;jj=j+1;
otherwise 4
ii=i+1;jj=j;
end
if(cancity(ii,jj,cells))
cells(ii,jj)=2;
end
end
end
if(cells(i,j)==2 && exist1(i,j,cells)) %新擴展中心型
if(rand<fanzhi)
if(cancity(i,j,cells))
cells(i,j)=3;
end
end
end
end
end
end
ch=0;kzch=0;
for i=1:x
for j=1:y
if(cells(i,j)==2) ch=ch+1;end
if(cells(i,j)==3) kzch=kzch+1;end
end
end
sch(t)=ch;skz(t)=kzch;
t=t+1;
[A,B]=size(cells);
Area(1:A,1:B,1)=zeros(A,B);
Area(1:A,1:B,2)=zeros(A,B);
Area(1:A,1:B,3)=zeros(A,B);
for i=1:A
for j=1:B
if cells(i,j)==1
Area(i,j,:)=[1,1,1];
elseif cells(i,j)==0
Area(i,j,:)=[255,255,255];
elseif cells(i,j)==3
Area(i,j,:)=[255,0,0];
elseif cells(i,j)==2
Area(i,j,:)=[255,177,0];
end
end
end
pause(0.0005);%速度
Area=uint8(Area);
Area=imagesc(Area);
axis equal;
axis tight;
%計步
stepnumber=1+str2num(get(number,string));
set(number,string,num2str(stepnumber));
end
if freeze==1
run=0;
freeze=0;
end
drawnow
end
程序2
運行環境:Matlab2011a/2014a
function a=aroundcenter(i,j,cells)
a=0;
if(cells(i-1,j)==3) a=1; end
if(cells(i,j-1)==3) a=1;end
if(cells(i,j+1)==3) a=1;end
if(cells(i+1,j)==3) a=1;end
end
程序3
運行環境:Matlab2011a/2014a
function result= exist1(i,j,cells)
a=0;
if(cells(i-1,j)==2) a=a+1; end
if(cells(i,j-1)==2) a=a+1;end
if(cells(i,j+1)==2) a=a+1;end
if(cells(i+1,j)==2) a=a+1;end
if(a>=2)
result=1;
else
result=0;
end
end
程序4
運行環境:Matlab2011a/2014a
function result=cancity(i,j,cells)
s=0
if(cells(i-1,j-1)==1) s=s+1;end
if(cells(i-1,j)==1) s=s+1;end
if(cells(i-1,j+1)==1) s=s+1;end
if(cells(i,j-1)==1) s=s+1;end
if(cells(i,j+1)==1) s=s+1;end
if(cells(i+1,j-1)==1) s=s+1;end
if(cells(i+1,j)==1) s=s+1;end
if(cells(i+1,j+1)==1) s=s+1;end
if(s>=4)
result=0;
else
result=1;
end
end
找了些資料分享給大家
(百度雲)鏈接:http://pan.baidu.com/s/1o8qiyfw 密碼:54xa
推薦閱讀:
TAG:數學建模 |