📄 untitled2.asv
字号:
function [] = CSA
disp(sprintf('\n** PART III - CLONALG (The CLONal Selection ALGorithm) **'));
% figure(1); clf;
v = cadeia(100,44,0,0,0);
disp(sprintf('Available Demo Tasks:'));
disp(sprintf('1) GA (GLOBAL SEARCH)'));
disp(sprintf('2) CLONALG (MULTI-MODAL OPTIMIZATION)'));
disp(sprintf('3) CHARACTER (KNOWLEDGE ACQUISITION)'));
ga_cga = input('Type the desired demo number (1) ,(2) or (3): ');
switch ga_cga,
case 1,
disp(sprintf('** Standard Genetic Algorithm - GA **'));
figure(1); clf;
[x,y,fx,vx,vmfit,P] = ga3d(v);
disp(sprintf('Maximum found [x,y,f(x,y)]: [%.2f,%.2f,%.2f]',x,y,fx));
figure(2); plot(vx); title('f(x,y) x Mean'); xlabel('Generations'); ylabel('f(x)');
hold on; plot(vmfit,'r'); hold off;
case 2,
disp(sprintf('** Clonal Selection Algorithm - CLONALG **'));
figure(3); clf; %v = v(1:50,:);
[x,y,fx,vfx,vmfit,P,vpm] = imalg3d(v);
save data x y fx vfx;
disp(sprintf('Maximum found [x,y,f(x,y)]: [%.2f,%.2f,%.2f]',x,y,fx));
figure(4); clf; plot(vfx); title('f(x,y) x Mean'); xlabel('Generations'); ylabel('f(x)');
hold on; plot(vmfit,'r'); hold off;
case 3,
disp(sprintf('** CLONALG - Knowledge Acquisition**'));
load num8_12x10; X = v(1,:); P = cadeia(10,120,0,0,0);
M = imalgchar(P,X);
otherwise,
display('Accepted values are (1), (2) or (3)');
end; % End Switch ga_cga
end;
%GA3D
function [x,y,fx,vx,vmfit,P] = ga3d(v,ger,pc,pm);
%
% Ph.D. Thesis
% Leandro Nunes de Castro
% November, 1999.
% ENHANCED GENETIC ALGORITHM - Bi-classist Selection
%
% Secondary functions: DECODE & IMPRIME(internal)
%
% function [x,y,fx,vx,vmfit,P] = ga3d(v,ger,N,pc,pm);
% x,y,fx -> f(x,y) is the maximal value of the function
% vfx -> vector of best fitness of population through generations
% vmfit -> vector of mean fitness of population through generations
% v -> population of size N x 2.L (L = 22 [-1,1])
% ger -> number of generations
% N -> population size
% pc -> cross-over probability
% pm -> mutation probability
%
% Definitions
% f -> evaluation function
% v -> population
% fit -> fitness vector
% x,y -> decodified coordinates
% Maximum found: [x,y,f] = [1.76,1.27,4.0172];
%
% Default Parameters
if nargin == 1,
[N,L] = size(v); ger = 250; pc = 0.5; pm = 0.01;
end;
disp(sprintf('Number of generations: %d',ger));
disp(sprintf('Population size: %d',N));
disp(sprintf('Crossover probability: %.3f',pc));
disp(sprintf('Mutation probability: %.3f',pm));
%f = '-1 * x .* sin(2 * pi .* x) + y.* sin(2 * pi .* y) + 1';
f = '1 * x .* sin(4 * pi .* x) - 1 * y.* sin(4 * pi .* y + pi) + 1';
% Plotting parameters
% [x,y] = meshgrid(-1:0.05:2,-1:0.05:2); vxp = x; vyp = y;
[x,y] = meshgrid(-1:0.05:1,-1:0.05:1); vxp = x; vyp = y;
vzp = eval(f); PRINT = 1;
% General parameters & Initial operations
sol = 1; vmfit = []; it = 1; vx = []; C = [];
x = decode(v(:,1:22));
y = decode(v(:,23:end)); fit = eval(f);
imprime(PRINT,vxp,vyp,vzp,x,y,fit,1,1); title('Initial Population');
disp('Press any key to continue...'); pause;
pb = 0.5; pw = 0.1; pr = 1 - (pb + pw);
nb = round(pb * N); nw = round(pw * N); nr = round(pr * N);
if (nb + nw + nr) ~= N,
dif = N - (nb + nw + nr);
nr = nr + dif;
end;
% Generations
t0 = clock; f0 = flops;
while it <= ger & sol <= 2.26,
% Reproduction (Bi-classist Selection)
[rw,ind] = sort(fit); ind = fliplr(ind);
vtemp = [v(ind(1:nb),:); v(ind(end-nw+1:end),:); v(2:nr+1,:)];
% Crossover
C(:,1) = rand(N,1) <= pc; C(:,2) = round(19.*rand(N,1))+1;
I = find(C(:,1) == 1); IP = [I,C(I,2)];
for i = 1:size(IP,1),
v(IP(i,1),:) = [vtemp(IP(i,1),1:IP(i,2)) vtemp(1,IP(i,2)+1:end)];
end;
% Mutation
M = rand(N,L) <= pm; M(1,:) = zeros(1,L);
v = v - 2 .* (v.*M) + M;
% Results
% x = decode(v); fit = eval(f);
x = decode(v(:,1:22)); y = decode(v(:,23:end)); fit = eval(f);
imprime(PRINT,vxp,vyp,vzp,x,y,fit,it,5);
[sol,indb] = max(fit);
v(1,:) = v(indb,:); media = mean(fit);
vx = [vx sol]; vmfit = [vmfit media];
if rem(it,1) == 0 | it == 10,
disp(sprintf('Gen.: %d x: %2.2f y: %2.2f Av: %2.2f f(x,y): %2.3f',it,x(indb),y(indb),media,sol));
end;
it = it + 1;
end;
T = etime(clock,t0); F = flops - f0;
x = x(indb); y = y(indb); fx = sol; P = v;
% Results
% clf; plot(vx); title('f(x) evolution'); xlabel('Generations'); ylabel('f(x)');
% hold on; plot(vmfit); hold off;
% --------------------- %
% INTERNAL SUBFUNCTIONS
% --------------------- %
% Print Surface
function [] = imprime(PRINT,vx,vy,vz,x,y,fx,it,mit);
% x,fx -> actual values
% vxplot, vplot -> original (base) function
if PRINT == 1,
if rem(it,mit) == 0,
mesh(vx,vy,vz); hold on; axis([-1 1 -1 1 -1 2.5]);
xlabel('x'); ylabel('y'); zlabel('f(x,y)');
plot3(x,y,fx,'k*'); drawnow; hold off;
end;
end;
% Decodify bitstrings
function x = decode(v);
% x -> real value (precision: 6)
% v -> binary string (length: 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);
%CSA
function [x,y,fx,vfx,vmfit,P,vpm] = imalg3d(P,gen,n,pm,per);
%
% Ph.D. Thesis
% Leandro Nunes de Castro
% November, 1999.
% Immune Algorithm - New evolutionary strategy inspired in the Immune System
% Operations: Hypermutation, Editing, Selection
% Each clone has size proportional to its affinity
% IMALG3D: MULTI-PEAK Solution (3D Function)
%
% function [x,y,fx,vfx,vmfit,P] = imalg3d(P,gen,n,pm,per);
% x,y,fx -> f(x,y) is the maximal value of the function
% vfx -> vector of best fitness of population through generations
% vmfit -> vector of mean fitness of population through generations
% P -> population of size N x 2.L
% gen -> generation number
% n -> number of clones
% pm -> hypermutation probability
% per -> percentile of the population to suffer random reshuffle
%
% T -> temporary population
% Maximum found: [x,y,f] = [1.76,1.27,4.0172];
%
if nargin == 1,
% gen = 200; n = round(size(P,1)/2); pm = 0.0005; per = 0.0; fat = 10;
%gen = 250; n = size(P,1); pm = 0.01; per = 0.0; fat = .1;
gen = 100; n = size(P,1); pm = 0.01; per = 0.0; fat = 0.1;
end;
while n <= 0,
n = input('n has to be at least one. Type a new value for n: ');
end;
% Printing parameters
%f = '-1 * x .* sin(2 * pi .* x) + y.* sin(2 * pi .* y) + 1';
f = '1 * x .* sin(4 * pi .* x) - 1 * y.* sin(4 * pi .* y + pi) + 1';
%f='sin(x) + sin(y)';
%f = '0.5+(sin(sqrt((x .* x+y.*y))).*sin(sqrt((x.* x+y.*y)))-0.5)./(1+0.001.*(x .* x+y.*y))';
% [x,y] = meshgrid(-1:0.05:2,-1:0.05:2); vxp = x; vyp = y;
[x,y] = meshgrid(-1:0.05:1,-1:0.05:1); vxp = x; vyp = y;
%[x,y] = meshgrid(-10:0.5:10,-10:0.5:10); vxp = x; vyp = y;
vzp = eval(f);
x = decode(P(:,1:22));
y = decode(P(:,23:end));
fit = eval(f);
imprime(1,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; PRINT = 1;
% 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(PRINT,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);
% --------------------- %
% INTERNAL SUBFUNCTIONS
% --------------------- %
% Print
function [] = imprime(PRINT,vx,vy,vz,x,y,fx,it,mit);
% x,fx -> actual values
% vxplot, vplot -> original (base) function
if PRINT == 1,
if rem(it,mit) == 0,
mesh(vx,vy,vz); hold on; axis([-1 1 -1 1 -1 2.5]);
xlabel('x'); ylabel('y'); zlabel('f(x,y)');
plot3(x,y,fx,'k*'); drawnow; hold off;
end;
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)
% v -> binary string (length: 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
%zifushibie
function [M] = imalgchar(P,X,gen,n,pm,per);
%
% Ph.D. Thesis
% Leandro Nunes de Castro
% November, 1999.
% Immune Algorithm - New evolutionary strategy inspired in the Immune System
% Operations: Hypermutation, Editing, Selection
% Each clone has size proportional to its affinity
%
% function [M] = pattern(P,X,gen,n,pm,per);
% M -> memory matrix
% P -> population of size N x L
% X -> patterns to be recognized np x L
% gen -> number of generations
% n -> number of clones
% pm -> hypermutation probability
% per -> percentile of the population to suffer random reshuffle
%
% T -> temporary population
% M -> Memory matrix (functionally disconnected)
% Tips: proportional clone sizes with very high sizes, e.g., fat = L
%
if nargin == 2,
gen = 250; n = round(size(P,1)/2); pm = 0.05; per = 0.0; fat = 10;
% gen = 200; n = size(P,1); pm = 0.1; per = 0.0; fat = 35;
end;
while n <= 0,
n = input('n has to be at least one. Type a new value for n: ');
end;
[N,L] = size(P); it = 0;
np = size(X,1); PRINT = 1;
% Hypermutation controlling parameters
pma = pm; itpm = 10; pmr = 0.8;
mfit = []; vpm = []; menor = 1;
M = cadeia(np,L+1);
disp(sprintf('Population size: %d',N));
disp(sprintf('Memory matrix size: [%d,%d]',np,L));
disp(sprintf('Maximum number of generations: %d',gen));
imprime(PRINT,12,10,X,it,1,1); title('Pattern to be recognized');
imprime(PRINT,12,10,M(1,2:end),it,1,2); title('Initial memory matrix');
disp('Press any key to continue...'); pause;
M = hardlim(M); M(1,1) = L; % Transform into a binary matrix
% Generations
while it < gen & menor > 0,
T = []; k = 0; vet = randperm(np); vfit = []; vind = []; % Assincronous
while k < np,
k = k+1; fit = []; % i = vet(k);
[fit,mXOR] = match(P,X(k,:),0);
[v(k,:),ind(k,:)] = sort(fit);
% Reproduction
[T,pcs] = reprod(n,fat,N,ind(k,:),P,T);
% Hypermutation
Ta = rand(size(T,1),L) <= pm;
T = T - 2 .* (T.*Ta) + Ta; % 0,1 mutation
T(pcs,:) = P(ind(k,1:n),:); % keep the previous best individuals
% Re-selection
[fit,mXOR] = match(T,X(k,:),0);
pcs = [0 pcs];
for i=1:n,
[out(i),bcs(i)] = min(fit(pcs(i)+1:pcs(i+1))); % Minimization problem
bcs(i) = bcs(i) + pcs(i);
end;
P(ind(k,1:n),:) = T(bcs,:);
% Memory Assignment & Evaluation
[b,indb] = min(fit);
if b < M(k,1),
M(k,1) = b; M(k,2:end) = T(indb,:);
menor(k) = b;
else,
menor(k) = M(k,1);
end;
% Editing (Repertoire shift)
nedit = round(per*N);
if nedit > 0,
P(ind(k,N-nedit-np+1:N-np),:) = cadeia(nedit,L,0,0,0);
end;
end;
P(ind(:,1),:) = M(:,2:end);
% Hypermutation control
[pm] = pmcont(pm,pma,pmr,it,itpm);
menor = sum(menor); vfit = [vfit,menor]; it = it + 1;
disp(sprintf('It.: %d pm: %.3f F: %2.4f',it,pm,menor));
Mem = hardlims(M - 0.1);
imprime(PRINT,12,10,Mem(:,2:end),it,5,2);
end; % end while
imprime(PRINT,12,10,M(:,2:end),it,1,2);
% --------------------- %
% INTERNAL SUBFUNCTIONS
% --------------------- %
% Function plot pictures
function [] = imprime(PRINT,res_lin,res_col,P,it,mit,fn);function [] = imprime(PRINT,vx,vy,vz,x,y,fx,it,mit);
if PRINT == 1,
if rem(it,mit) == 0,
fig(res_lin,res_col,0,fn,P);
end;
end;
function [mat] = fig(L,C,div,fn,X)
figure(fn); clf; hold on;
for i = 1:size(X,1),
mat = reshape(X(i,:),C,L)';
if (div == 1),
subplot(2,4,i);
end;
image(mat*15);axis('square');axis('off');
end;
drawnow; hold off;
% Function match bipolar strings
function [ms,mXOR] = match(ab,ag,comp)
if nargin == 2,
comp = 0; % Hamming distance
end;
msc = []; % ms complement
if min(min(ag)) == -1,
ag = hardlim(ag);
end;
% Using the XOR operator for calculating the match score
[n1,s1] = size(ab);
ag = ones(n1,1) * ag; % Multiply the Antigen
mXOR = xor(ab,ag);
ms = sum(mXOR');
msc = 1 - ms;
if comp == 1,
ms = msc;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -