Publicado el 1 de Septiembre del 2019
1.248 visualizaciones desde el 1 de Septiembre del 2019
617,3 KB
171 paginas
Creado hace 13a (11/08/2010)
ANEXO G
CÓDIGOS DE LOS PROGRAMAS DE MATLAB
function [Kp,tao,to]=ajuste_fit3(c,t,dm)
% Ajuste de una curva por el mètodo del FIT3
% A la función se le pasan los valores del tiempo, la salida y el cambio en
% el set point
span_C=100; %Rango de la variable de salida proceso
span_m=100; %Rango de la variable de entrada proceso(salida controlador)
dC=c(length(c))-c(1); % Para calcular el cambio total de la señal se resta
% el primer valor con el ùltimo.
Ct1=c(1)+dC*0.283; % Se calcula el valor de la señal cuando a llegado
Ct2=c(1)+dC*0.632; % al 28.3% y 63.2% de su de su cambio total.
it1=min(find(c==Ct1)); % se halla el indice en el cual se encuentra el
it2=min(find(c==Ct2)); % t1 y t2.
% En caso de que no se encuentre un valor igual en la tabla se procede a
% realizar una interpolacion lineal para hallar t1 y t2. Verifica si el
% camio en la variable fua ascendente o descendente de modo que se
% modifiquen las desigualdades para encontrar el tiempo anterior al tiempo
% no encontrado t1 o t2.
if(it1)
t1=t(it1);
else
if dC < 0
itl=max(find(c>Ct1));
ith=itl+1;
else
itl=max(find(c<Ct1));
ith=itl+1;
end
t1=((Ct1-c(itl))*t(ith)+(c(ith)-Ct1)*t(itl))/(c(ith)-c(itl));
end
clear itl ith %se limpian las variables para su reuso
if(it2)
t2=t(it2);
else
if dC < 0
itl=max(find(c>Ct2));
ith=itl+1;
else
itl=max(find(c<Ct2));
ith=itl+1;
end
t2=((Ct2-c(itl))*t(ith)+(c(ith)-Ct2)*t(itl))/(c(ith)-c(itl));;
end
%Se halla la constante de tiempo del sistema
tao=1.5*(t2-t1);
%Se halla el tiempo muerto del sistema
to=t2-tao;
%Se halla la ganancia del sistema en %T0/%CO
Kp=(dC/span_C)/(dm/span_m);
function [child,bestmember]=cruzamiento4(trait,fitness,CROSS_PROB,POP_SIZE,...
NUM_TRAITS,ELITISM,bestmember,HIGHTRAIT,LOWTRAIT)
% Esta función realiza el crossover heurístico de la siguiente manera:
% hijo=random*(padre1 -padre2)+padre1, teniendo en cuenta que padre1 es
% mejor que dos y de que además es para real coded es decir tal y como son.
for pop_member=1:POP_SIZE
if ELITISM==1 && pop_member==bestmember
child(:,pop_member)=trait(:,pop_member);
bestmember=pop_member;
else
if CROSS_PROB > rand
for num_trait=1:NUM_TRAITS
parent1=trait(num_trait,pop_member);
pop_member2=ceil(rand*POP_SIZE);
% se verifica que no sea el mismo miembro de la poblacion y
% además que su fitness no sea menor que el fitness actual
while pop_member2==pop_member && ...
fitness(pop_member2) < fitness(pop_member)
pop_member2=ceil(rand*POP_SIZE);
end
parent2=trait(num_trait,pop_member2);
% se realiza el cruzamiento para real coded de acuerdo a la
% regla.
child(num_trait,pop_member)=rand*(parent1-parent2)+parent1;
% se verifica si se paso del límite par no generar miembros de
% la población no factibles.
if child(num_trait,pop_member) > HIGHTRAIT(num_trait)
child(num_trait,pop_member)=HIGHTRAIT(num_trait);
elseif child(num_trait,pop_member) < LOWTRAIT(num_trait)
child(num_trait,pop_member)=LOWTRAIT(num_trait);
end
end
else
child(:,pop_member)=trait(:,pop_member);
end
end
end
function [child,bestmember]=cruzamiento5(trait,CROSS_PROB,POP_SIZE,...
NUM_TRAITS,ELITISM,bestmember,HIGHTRAIT,LOWTRAIT)
% Esta función realiza el cruzamiento atirmetico de la siguiente forma
% hijo=a*padre2*(1-a)*padre1, siendo "a" una constante o un número aleatorio
% que para nuestro caso fue un número aleatorio por haberse demostrado que
% tiene mejores resultados.
for pop_member=1:POP_SIZE
if ELITISM==1 && pop_member==bestmember
child(:,pop_member)=trait(:,pop_member);
bestmember=pop_member;
else
if CROSS_PROB > rand
for num_trait=1:NUM_TRAITS
parent1=trait(num_trait,pop_member);
pop_member2=ceil(rand*POP_SIZE);
% se verifica que no sea el mismo miembro de la poblacion
while pop_member2==pop_member
pop_member2=ceil(rand*POP_SIZE);
end
parent2=trait(num_trait,pop_member2);
% se realiza el cruzamiento para real coded de acuerdo a la
% regla.
a=rand;
child(num_trait,pop_member)=a*parent2+(1-a)*parent1;
% se verifica si se paso del límite par no generar miembros de
% la población no factibles.
if child(num_trait,pop_member) > HIGHTRAIT(num_trait)
child(num_trait,pop_member)=HIGHTRAIT(num_trait);
elseif child(num_trait,pop_member) < LOWTRAIT(num_trait)
child(num_trait,pop_member)=LOWTRAIT(num_trait);
end
end
else
child(:,pop_member)=trait(:,pop_member);
end
end
end
function m = dmc2(r,c2,time);
global m mbar predvect T lambda np nu G i gamma1
% inicializacion
if time==0
load parametros_dmc;
mbar=50;
%lambda=0.2;
m(1)=mbar;
m(2)=0;
predvect=c2*ones(np,1);
i=time;
end
if rem(time,T)==0 && time > i
em=c2-predvect(1);
predvect_u=[predvect(2:np);predvect(np)]+em;
E=r*ones(np,1)-predvect_u;
gamma2=gamma1*eye(np);
Kc=inv(G'*gamma2'*gamma2*G+lambda*eye(nu))*G'*gamma2'*gamma2;
delta_u=Kc*E;
delta_u=delta_u(1);
if (m(1) + delta_u) > 100,
delta_u=100-m;
elseif (m(1) + delta_u) < 0,
delta_u = -m(1);
end
m(1)=m(1)+delta_u;
predvect=G*[delta_u;zeros(nu-1,1)]+predvect_u;
i=time;
m(2)=em;
else
m=m;
end
%ie+1 input elements
%identifies training set dimension
%identifies number of input elements
%Program for training a RAWN network
%This function finds the weights for the hidden and the output layer, that produce the
%minimum SSE (Y-Ypred).
%The inputs and outputs for training, and the number of hidden nodes must be provided.
%Weights are stored in a file called weights.
function
[Wh,Bh,Wo,Bo,Ymin,SSEtrain]=entrenamiento2(inputs,outputs,nodes,iter,inputs_2,outputs
_2);
ie=size(inputs,2);
l=size(inputs,1);
%Include bias input
inputs(:,ie+1)=ones(l,1);
%Initializing variables
SSEflag=0;
Y=outputs;
%Finding weights for the minimum SSE of Y-Ypredicted
h = waitbar(0,'Entrenando RAWN...');
for j=1:iter
set
W=zeros(ie+1,nodes); %Initializing the weights of the hidden layer
for q=1:nodes
R=randn(ie+1,1);
numbers
W(:,q)=sqrt(2/(max(sum(inputs.^2,2))))*R;
end
Z=inputs*W; %net for each node in the hidden layer
V=tansig(Z); %output for each node in the hidden layer
%Include bias input
V(:,nodes+1)=ones(l,1); %nodes+1 input elements
Wout=inv(V'*V)*V'*Y; %Least squares method ojo!!!!!!!!!
%validacion cruzada
Wh_2=W(1:ie,:);
Bh_2=W(ie+1,:);
Wo_2=Wout(1:nodes,:);
Bo_2=Wout(nodes+1,:);
[Ypred]=nn_sim_val(inputs_2,Wh_2,Bh_2,Wo_2,Bo_2);
%generates a file of normally distributed random
%iter is the number of trials per node
%Picking the minimum SSE
%Initializing SSEmin
%blocking
%Ypred=V_2*Wout;
SSE=sumsqr(outputs_2-Ypred);
if SSEflag==0
SSEtrain=SSE;
SSEflag=1;
end
%Separating weights and biases
if SSE<= SSEtrain
SSEtrain=SSE;
Ymin=Ypred;
Wh=W(1:ie,:);
Bh=W(ie+1,:);
Wo=Wout(1:nodes,:);
Bo=Wout(nodes+1,:);
if SSEtrain<0.1
break
end
end
waitbar(j/iter)
end
close(h)
fprintf('SSEtrain= %g\n',SSEtrain)
function nuevos=evr_selec(F,D,POP_SIZE2);
% Esta función realiza la seleccion de los individuos que se encontrarán en
% el archivo es decir hace la selección de Pt+1.
nuevos=find(F < 1);
nuevos=nuevos';
N=length(nuevos);
if N < POP_SIZE2
[F2,nuevos2]=sort(F);
nuevos=nuevos2(1:POP_SIZE2)';
else
% se inicializa el contador para los miebros de la poblacion Pt+1
% quienes son "nuevos"
pop_member=1;
% se inicializa la cuenta para ir desde la distancia minima hasta la
% máxima en caso de que no se reduzcan los miembros con la primera. se
% empieza en tres porque la primera distancia mínima es la de el mismo y
% siempre es cero
dist_min=3;
% Se hace un ciclo infinito que se rompe internamente solo cuando se
% llega hasta N2
while 1
% Se busca si el miembro hace parte de la primera distancia y
% debido a que se encuentran ordenadas ascendentemente esta es la
% menor. En caso de que se encuentre que ese miembro es el miembro
% con distancia mínima a otro se le asigna al indice que se
% encuentra en el vector "nuevos" de manera que se borre de la
% nueva población.
dist=find(D(:,dist_min)==nuevos(1,pop_member));
if dist
nuevos(pop_member)=0;
end
N2=nnz(nuevos);
% Se pregunta si ya se llegó al limite deseado, en caso que no sea
% asi se pregunta si ya se recorrio tod
Comentarios de: Anexo G - Códigos de los programas de Matlab (0)
No hay comentarios