下面一段程序卡在了第三个 fit = eval(f)处,这是怎么回事?
function [] = AISMAIN
v = cadeia(100,44,0,0,0);
figure(1);
clf;
P=v;
% Default Parameters默认参数
gen = 100; n = size(P,1); pm = 0.01; per = 0.0; fat = 0.1;
%待优化的函数
a=0.3;b=0.7
f='a.*[1+x.*sin(4*pi.*x)-y.*sin(4*pi.*y+pi)]+b.*[-1 * x .* sin(2 * pi .* x) + y.* sin(2 * pi .* y) + 1]';
% Plotting parameters绘图参数
[x,y] = meshgrid(-1:0.05:1,-1:0.05:1);
vxp = x;
vyp = y;
vzp = eval(f);
x = decode(P(:,1:22)); y = decode(P(:,23:end)); fit = eval(f);
imprime(vxp,vyp,vzp,x,y,fit,1,1); title('Initial Population');
disp(sprintf('Number of generations: %d',gen));
disp(sprintf('Population size: %d',n));
disp(sprintf('Mutation probability: %.3f',pm));
disp(sprintf('Number of clones per candidate: %d',fat*n));
disp(sprintf('Press any key to continue...')); pause;
% Hypermutation controlling parameters
pma = pm; itpm = gen; pmr = 0.8;
% General defintions
vpm = []; vfx = []; vmfit = []; valfx = 1;
[N,L] = size(P); it = 0;
% Generations
while it <= gen & valfx <= 2.26,
x = decode(P(:,1:22)); y = decode(P(:,23:end)); T = []; cs = [];
fit = eval(f); [a,ind] = sort(fit);
valx = x(ind(end-n+1:end)); valy = y(ind(end-n+1:end));
fx = a(end-n+1:end); % n best individuals (maximization)
imprime(vxp,vyp,vzp,x,y,fit,it,5);
% Reproduction
[T,pcs] = reprod(n,fat,N,ind,P,T);
% Hypermutation
M = rand(size(T,1),L) <= pm;
T = T - 2 .* (T.*M) + M;
T(pcs,:) = P(fliplr(ind(end-n+1:end)),:);
% New Re-Selection (Multi-peak solution)
x = decode(T(:,1:22));
y = decode(T(:,23:end));
fit = eval(f);
pcs = [0 pcs];
for i=1:n,
[out(i),bcs(i)] = max(fit(pcs(i)+1:pcs(i+1))); % Maximizationn problem
bcs(i) = bcs(i) + pcs(i);
end;
P(fliplr(ind(end-n+1:end)),:) = T(bcs,:);
% Editing (Repertoire shift)
nedit = round(per*N); it = it + 1;
P(ind(1:nedit),:) = cadeia(nedit,L,0,0,0);
pm = pmcont(pm,pma,pmr,it,itpm); valfx = max(fx);
vpm = [vpm pm]; vfx = [vfx valfx]; vmfit = [vmfit mean(fit)];
disp(sprintf('It.: %d pm: %.4f x: %2.2f y: %2.2f Av.: %2.2f f(x,y): %2.3f',it,pm,valx(end),valy(end),vmfit(end),valfx));
end; % end while
%imprime(PRINT,vxp,vyp,vzp,x,y,fit,it,1);
x = valx(end); y = valy(end); fx = max(fx);
% x = P(ind(end),1:22); y = P(ind(end),23:44); fx = max(fx);
save data x y fx vfx;
disp(sprintf('Maximum found [x,y,f(x,y)]: [%.2f,%.2f,%.2f]',x,y,fx));
figure(2); clf; plot(vfx); title('f(x,y) x Mean'); xlabel('Generations'); ylabel('f(x)');
hold on; plot(vmfit,'r'); hold off;
% --------------------- %
% INTERNAL SUBFUNCTIONS
% --------------------- %
function [] = imprime(vxp,vyp,vzp,x,y,fit,it,mit);
% x,fx -> actual values
% vxplot, vplot -> original (base) function
if rem(it,mit) == 0,
mesh(vxp,vyp,vzp);
hold on; %图形保持
%axis([-4 4 -4 4 0 4000]);%控制轴刻度
%axis([-10 10 -10 10 0 3500]);
axis([-1 1 -1 1 -1 2.5]);
xlabel('x'); ylabel('y'); zlabel('f(x,y)');
plot3(x,y,fit,'k*');
drawnow;%更新事件队列强迫Matlab刷新屏幕
hold off;%图形取消
end;
% Reproduction
function [T,pcs] = reprod(n,fat,N,ind,P,T);
% n -> number of clones
% fat -> multiplying factor
% ind -> best individuals
% T -> temporary population
% pcs -> final position of each clone
if n == 1,
cs = N;
T = ones(N,1) * P(ind(1),:);
else,
for i=1:n,
% cs(i) = round(fat*N/i);
cs(i) = round(fat*N);
pcs(i) = sum(cs);
T = [T; ones(cs(i),1) * P(ind(end-i+1),:)];
end;
end;
% Control of pm
function [pm] = pmcont(pm,pma,pmr,it,itpm);
% pma -> initial value
% pmr -> control rate
% itpm -> iterations for restoring
if rem(it,itpm) == 0,
pm = pm * pmr;
if rem(it,10*itpm) == 0,
pm = pma;
end;
end;
% Decodify bitstrings解码操作函数
function x = decode(v);
% x -> real value (precision: 6)十进制数(精度为6位小数)
% v -> binary string (length: 22)二进制字符串(长度为22位)
v = fliplr(v); s = size(v);
aux = 0:1:21; aux = ones(s(1),1)*aux;
x1 = sum((v.*2.^aux)');
x = -1 + x1 .* (2 / 4194303);
% Function CADEIA
function [ab,ag] = cadeia(n1,s1,n2,s2,bip)
if nargin == 2,
n2 = n1; s2 = s1; bip = 1;
elseif nargin == 4,
bip = 1;
end;
% Antibody (Ab) chains
ab = 2 .* rand(n1,s1) - 1;
if bip == 1,
ab = hardlims(ab);
else,
ab = hardlim(ab);
end;
% Antigen (Ag) chains
ag = 2 .* rand(n2,s2) - 1;
if bip == 1,
ag = hardlims(ag);
else,
ag = hardlim(ag);
end;
% End Function CADEIA
[本帖最后由 mooni 于 2009-5-6 21:34 编辑]