CA元胞自动机优化模型原代码
- 格式:docx
- 大小:17.55 KB
- 文档页数:9
CA优化模型原代码:
M=load(‘d:\ca\jlwm’)
N=load(‘d:\ca\jlwn.asc’)
lindishy=load(‘d:\ca\ldfj3.asc’)
caodishy=load(‘d:\ca\cdfj3.asc’)
gengdishy=load(‘d:\ca\htfj3.asc’)
[m,n]=size(M);
Xr=[1 1 -1 1 1 1 -1 -1 1 1;1 1 1 1 -1 -1 1 1 1 -1;-1 1 1 1 -1 -1 -1 1 -1 -1;1 1 1 1 1 1 -1 1 1 I; l -1 -1 1 1 -1 -1 -1 1 1;1 -1 -1 1 -1 1 -1 1 -1 -1;-1 1 -1 -1 -1 -1 1 -1 -1 -1;-1 1 1 1 -1 1 -1 1 -1 -1;1 1 -1 1 1 -1 -1 -1 1 1;1 -1 -1 1 1 -1 -1 -1 1 1];
caodi=0;lindi=0;gengdi=0;
for i=1:m
forj=l:n
if M(i,j)==4
caodi=caodi+1;
elseif M(i,j)==3
lindi=lindi+1;
elseif M(i,j)==2
gengdi=gengdi+1;
end
end
end
for i=1:m
for j=1:n
if M(i,j)==4
if lindishy(i,j)>gengdishy(i,j)
if lindishy(i,j)>caodishy(i,j)
z=0;
for P=max(1,i-1):min(i+1,m)
for q=max(j-1,1):min(j+1,n)
if (M(p,q)~=0)&&xr(M(p,q),3)==-1
z=1;
end
end
end
if z== 0
caodi=eaodi-1;
M(i,j)=3;
lindi=lindi+1;
end
elseif lindishy(i,j)==caodishy(i,j)
caoditemp=0;
linditemp=0;
gengditemp=0;
for p=max(1,i-1):min(i+1,m)
for q=max(j-1,1):min(i+1,n)
if N(p,q)==4
caoditemp=caoditemp+1;
elseif N(p,q)==3
linditemp=linditemp+1;
elseif N(p,q)=2
gengditemp=gengditemp+1;
end
end
end
if linditemp>=max(caoditemp,gengditemp) z=0;
for p=max(1,j-1):min(i+1,m)
for q=max(j-1,1):min(j+1,n)
if(M(p,q)~=0)&&xr(M(p,q),3)==-1;
z=1;
end
end
end
if Z==0
caodi=caodi-1;
M(i,j)=3;
lindi=lindi+1;
end
end
end
elseif lindishy(i,j)==gengdishy(i,j)
if lindishy(i,j)>caodishy(i,j)
caoditemp=0:
linditemp=0;
gengditemp=0:
for p=max(1,i-1):min(i+1,m)
for q=max(j-1,1):min(j+1,n)
if N(p,q)==4
caoditemp=caoditemp+1;
elseif N(p,q)==3
linditemp=linditemp+1;
elseif N(p,q)==2
gengditemp=gengdltemp+1;
end
end
end
if linditemp>=gengditemp
for p=max(1,j-1):min(i+1,m)
for q=max(j-1,1):min(j+1,n)
if (M(p,q)~=0)&&xr(M(p,q),3)==-1
z=1;
end
end
end
if z==0
caodi=caodi-1;
M(i,j)=3;
lindi=lindi+1;
end
end
elseif lindishy(i,j)==caodishy(i,j) caoditemp=0;
linditemp=0;
gengditemp=0;
for p=max(i,i-1):min(i+1,m)
for q=max(j-1,1):min(j+1,n)
if N(p,q)==4
caoditemp=caoditemp+1;
elseif N(p,q)==3
linditemp=linditemp+1;
elseif N(p,q)==2
gengditemp=gengditemp+1;
end
end
end
if linditemp>=max(caoditemp,gengditemp) z=0;
for p=max(1,i-1):min(i+1,m)
for q=max(j-1,1):min(j+1,n)
if(M(p,q)~=0)&&xr(M(p,q),3)==-1
z=1;
end
end
end
if z==0
caodi=caodi-1;
M(i,j)=3;
lindi=lindi+1;
end
end