function [Ab,x,y,fx,vfx,vmfit] = opt(ts,N,Nc,beta,gen)
% function [M] = opt(ts,N,Nc,beta,gen)
% M -> matrix of memory cells
% ts -> suppression threshold
% N -> clone number multiplier
% Nc -> no. of clones to be generated
% beta -> decay of the inverse exponential function
% gen -> maximum number of generations
%L -> Length of antibodies
%
%Author :: Jerry
%Build date :: 3/4/2011c
%Time ::
%Version 1.01
% Function to be Optimized
f = '1 * x .* sin(4 * pi .* x) - 1 * y.* sin(4 * pi .* y + pi) + 1';
%f = '20 + x.^2 + y.^2 - 10.*(cos(2*pi.*x) + cos(2*pi.*y))';%Rastrigin's
%Function
%f = '0.5-((sin(sqrt(x.^2+y.^2)).^2-0.5)./(1+0.001.*(x.^2+y.^2)).^2)';%Schaffer`s Function
% Parameters for Ploting the Affinity Lansdscape
xmin = -2; xmax = 2 ; ymin = -2; ymax = 2;
[x,y] = meshgrid(-1:0.01:1,-1:0.01:1); vxp = x; vyp = y;
vzp = eval(f);
% Initial Random Population Within the Intervals (xmin/xmax; ymin/ymax)
Ab1 = (xmax + rand(N,1).*(xmin-xmax))./2; //the rand(N,1) function is needed to be called
Ab2 = (ymax + rand(N,1).*(ymin-ymax))./2;
% [Ab1,Ab2] = cadeia(100,40,0,0,0)
Ab = [Ab1,Ab2];
x = Ab(:,1); y = Ab(:,2);
fit = eval(f);
imprime(1,vxp,vyp,vzp,x,y,fit,1,1); %title('Initial Population');
disp('Press any key to continue...'); pause;
it = 0; Nold = N + 1; Nsup = N;
FLAG = 0; FLAGERROR = 0;
avfitold = mean(fit); //the mean(fit) function is needed to be called
avfit = avfitold-1;
vavfit = [];
vfx = []; vmfit = []; valfx = 1;
% Main Loop where the data acquisation is process
while it <= gen & FLAG == 0,
a = x;%Ab(:,1); //the Ab(:,1) function is needed to be called
b = y;%Ab(:,2);C=[];
fit = eval(f); ////the eval(f) function is needed to be called
clear memory
if Nc~=N, %~= represents not equal to
[a,ind] = sort(fit);%sorts the vector for fitness
else, a = fit; ind = 1:1:N;
end;
valx = x(ind(end-N+1:end)); valy = y(ind(end-N+1:end)); //the ind() function is needed to be called
% a = fit; ind = 0.01:0.01:N;
fx = a(end-N+1:end);
% Reproduction (Cloning), Affinity Maturation, and Selection Within Each Clone
[Ab] = clone_mut_select(Ab,Nc,beta,norma(fit),xmin,xmax,ymin,ymax,f);%can be used in pattern recognition
% Immune Network Interactions After a Number of Iterations
if rem(it,5) == 0, //the rem() function is needed to be called
if abs(1-avfitold/avfit) < .001,
[Ab] = suppress(Ab,ts);
FLAGERROR = 1;
Nsupold = Nsup; Nsup = size(Ab,1);
% Convergence Criterion
if (Nsupold-Nsup) == 0, % & rem(it,20) == 0,
FLAG = 1; FLAGERROR = 0;
end;
end;
end;
% Insert randomly generated individuals
if FLAGERROR == 1,
% pause;
d = round(.4*N); ////the round()function is needed to be called
Ab1 = (xmax + rand(d,1).*(xmin-xmax))./2;
Ab2 = (ymax + rand(d,1).*(ymin-ymax))./2;
Ab = [Ab;Ab1,Ab2];
FLAGERROR = 0;
end;
% % Evaluating Fitness
x = Ab(:,1); y = Ab(:,2);
z = complex(x,y);
fit = eval(f); avfitold = avfit;
avfit = mean(fit);
% Ploting Results
imprime(1,vxp,vyp,vzp,x,y,fit,it,10);
N = size(Ab,1);
valfx = min(fx);////the min() function is needed to be called
vfx = [vfx,valfx];
it = it + 1; vmfit = [vmfit,avfit]; % vN = [vN,N]
%valx = min(x);valy = min(y);
disp(sprintf('It: %d x: %2.10f y: %2.10f Av: %2.10f f(x,y) = %2.10f',it,valx(end),valy(end),avfit(end),valfx));
end;
x = valx(end); y = valy(end); fx = min(fx);
imprime(1,vxp,vyp,vzp,x,y,fit,1,1);
figure(2); //the figure() function is needed to be called
plot(vfx); //the plot() function is needed to be called
%set(get(get(p,'Annotation'),'LegendInformation'),'IconDisplayStyle','on');
hold on;
q = plot(vmfit,'-.'); set(q,'Color','red','LineWidth',2); //the set() function is needed to be called
xlabel('Generations');ylabel('Fitness/Mean');
%set(get(get(q,'Annotation'),'LegendInformation'),'IconDisplayStyle','on');
legend('Fitness','Mean');
title('Fitness/Mean Population vs Generations'); hold off;
%figure(3); plot(vN); title('N');
% ------------------- %
% SECONDARY FUNCTIONS %
% ------------------- %
function [C] = clone_mut_select(Ab,Nc,beta,fitin,xmin,xmax,ymin,ymax,f);
% C -> matrix of clones
% g -> vector with Gaussian mutation/affinity
% Ab -> matrix of antibodies
% N -> cardinality of Ab
% Nc -> number of clones for each candidate
[N,L] = size(Ab);
C = [];
for i=1:N,
vones = ones(Nc,1); //the ones() function is needed to be called
Cc = vones * Ab(i,:);
% g = (randn(Nc,L)./beta) .* exp(-beta.*fitin(i));
g = (randn(Nc,L)./beta) .* exp(-fitin(i));%hypermutation rate
g(1,:) = zeros(1,L); //the zeros() function is needed to be called
% Keep one previous individual for each clone unmutated
c = Cc + g;
% Keeps all elements of the population within the allowed bounds
Ixmin = find(c(:,1) < xmin); Ixmax = find(c(:,1) > xmax); //the find() function is needed to be called
Iymin = find(c(:,2) < ymin); Iymax = find(c(:,2) > ymax);
if ~isempty(Ixmin),
c(Ixmin,1) = Cc(length(Ixmin),1); //the length() function is needed to be called
end;
if ~isempty(Ixmax),
c(Ixmax,1) = Cc(length(Ixmax),1);
end;
if ~isempty(Iymin),
c(Iymin,2) = Cc(length(Iymin),2);
end;
if ~isempty(Iymax),
c(Iymax,2) = Cc(length(Iymax),2);
end;
x = c(:,1); y = c(:,2);
fit = eval(f);
[out,I] = min(fit);
C = [C;c(I,:)]; % C contains only the best individuals of each clone
end;
% Function suppress self-recognizing and non-stimulated Ab from Memory (M)
function [M] = suppress(M,ts);
% M -> memory matrix
% D1 -> idiotypic affinity matrix
D1 = dist(M,M'); //the dist() function is needed to be called
%using Euclidean distance
aux = triu(D1,1); //the triu() function is needed to be called
[Is,Js] = find(aux>0 & aux<ts);
if ~isempty(Is),
Is = ver_eq(Is);
M = extract(M,Is);
% D1 = extract(D1,Is);
end;
% D1 = dist(M,M');
% Search for repeated indexes
function [Is] = ver_eq(I);
l = length(I); Is = [];
if l > 1,
for i=1:l-1,
aux = I(i);
auxI = I(i+1:end);
el = find(auxI == aux);
if isempty(el),
Is = [Is,aux];
end;
end;
Is = [Is,I(end)];
else,
Is = I;
end;
% Function Extracts lines from M indexed by I
function [M] = extract(M,I);
Maux = zeros(size(M));
Maux(I,:) = M(I,:);
M = M - Maux;
[I] = find(M(:,1)~=0);
M = M(I,:);
% Function normalizes matrix over [0,1]
function [Dn] = norma(D);
% Dn -> normalized vector over [0,1]
[np,ni] = size(D);
if ni == 1,
Dn = (D - min(D))./(max(D)-min(D));
else,
vmaxD = max(D); vminD = min(D);
for i=1:ni,
Dn(:,i) = (D(:,i) - vminD(i))./(vmaxD(i)-vminD(i));
end;
end;
% End Function NORMA
% Print Affinity Landscape and Population of Individuals
function [] = imprime(PRINT,vx,vy,vz,x,y,fx,it,mit);
% x,fx -> current values
% vxplot, vplot -> original (base) function
if PRINT == 1,
if rem(it,mit) == 0,
mesh(vx,vy,vz); //the mesh() function is needed to be called
hold on;
axis([-1 1 -1 1 -2 2]);
%axis([-2 2 -2 2 0 30]); %Rastrigin's Function
xlabel('x'); ylabel('y'); zlabel('f(x,y)');
plot3(x,y,fx,'k*'); drawnow; hold on; hidden on;
%pause;
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;
function [Ab1,Ab2] = cadeia(n1,s1,n2,s2,bip)
if nargin == 2,
n2 = n1; s2 = s1; bip = 2;
elseif nargin == 4,
bip = 1;
end;
% Antibody (Ab1) chains
Ab1 = 2 .* rand(n1,s1) - 1; %n1 = rows, s1 = columns
% Converting bipolar (-1,+1) strings to binary ones (0,+1)
if bip == 1,
Ab1 = hardlims(Ab1) ;//the hardlims() function is needed to be called
else,
Ab1 = hardlim(Ab1); //the hardlim() function is needed to be called
end;
% Antigen (Ag) chains
Ab2 = 2 .* rand(n2,s2) - 1;
if bip == 1,
Ab2 = hardlims(Ab2)
else,
Ab2 = hardlim(Ab2);
end;
Hi,
I'm having trouble including .lib files into my coding. And I want to know how to use this in mBed compiler #pragma comment(lib, "libmat.lib")