⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fitness_function.asv

📁 MATLAB版本的遗传算法程序
💻 ASV
字号:
function [fitness]=fitness_function(Kp,Ki,Kd)
%Fitness_function Function is an example function Of Simple Genetic Algorithm Program (Version 1.0.0.1 )
%Support multi-dimesion parameters
%By chen yi ,CQU .QQ:2376635  Email:cdey@10mail.net  (April,19th,2002)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Attention :fitness_function(x) is a single value function
%and it is a standard function of m-function-file
%
% this is only an example of fitness_function ,you can give your own
% fitness_function as you wish.
%
% See Also DECODING ,CODING,SELECTION ,CROSSOVER,MUTATION,FITNESS,
%          SGA
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
fid20001=fopen('error_acc1.txt','r');%open file for reading
error_acc1=fscanf(fid20001,'%g');

fid20002=fopen('error_acc2.txt','r');
error_acc2=fscanf(fid20002,'%g');

fid20003=fopen('w1.txt','r');
w1=fscanf(fid20003,'%g');

fid20004=fopen('w2.txt','r');
w2=fscanf(fid20004,'%g');

fid20005=fopen('w3.txt','r');
w3=fscanf(fid20005,'%g');

fid20006=fopen('h.txt','r');
h=fscanf(fid20006,'%g');

fid20007=fopen('umax.txt','r');
umax=fscanf(fid20007,'%g');

fid20008=fopen('umin.txt','r');
umin=fscanf(fid20008,'%g');

fid20009=fopen('u_now.txt','r');
u_now=fscanf(fid20009,'%g');

fid1001=fopen('n0.txt','r');
n0=fscanf(fid1001,'%g');

fid1008=fopen('Gqn0.txt','r');
Gqn0=fscanf(fid1008,'%g');

fid1009=fopen('Vehicle_Velocity.txt','r');
Vehicle_Velocity=fscanf(fid1009,'%g');

fid2001=fopen('Spectrum_Types.txt','r');
fid2002=fopen('Parameter1.txt','r');
fid2003=fopen('Parameter2.txt','r');
fid2004=fopen('Parameter3.txt','r');
fid2005=fopen('Parameter4.txt','r');
Spectrum_Types=fscanf(fid2001,'%c');
Parameter1=fscanf(fid2002,'%g');
Parameter2=fscanf(fid2003,'%g');
Parameter3=fscanf(fid2004,'%g');
Parameter4=fscanf(fid2005,'%g');

fid1003=fopen('m1.txt','r');
fid1004=fopen('m2.txt','r');
fid1005=fopen('k1.txt','r');
fid1006=fopen('k2.txt','r');
fid1010=fopen('c2semi.txt','r');

m1=fscanf(fid1003,'%g');
m2=fscanf(fid1004,'%g');
k1=fscanf(fid1005,'%g');
k2=fscanf(fid1006,'%g');
c2semi=fscanf(fid1010,'%g');

fid200010=fopen('XX.txt','r');
XX=fscanf(fid200010,'%g');

fid200011=fopen('timer.txt','r');
timer=fscanf(fid200011,'%g');

%fid200012=fopen('YY.txt','r');
%YY=fscanf(fid200012,'%g');

fid200013=fopen('Y11.txt','r');
Y11=fscanf(fid200013,'%g');
Y11=Y11';

fid200014=fopen('Y21.txt','r');
Y21=fscanf(fid200014,'%g');
Y21=Y21';

fid200015=fopen('Y31.txt','r');
Y31=fscanf(fid200015,'%g');
Y31=Y31';


fid200016=fopen('Road_Velocity.txt','r');
Road_Velocity=fscanf(fid200016,'%g');

%fid200017=fopen('J.txt','r');
%J=fscanf(fid200017,'%g');

%fid200018=fopen('JJ.txt','w');

x=[0 0 0]';
J1=0;
J2=0;
J3=0;

%u
   x(1)=error_acc2;                         % Calculating P
   x(2)=(error_acc2-error_acc1)/h;   % Calculating D
   x(3)=x(3)+error_acc2*h;                % Calculating I
   u_now=u_now+Kp*x(1)+Ki*x(2)+Kd*x(3);  
     
     if u_now>=umax
      u_now=umax;
      end
     if  u_now<=umin
      u_now=umin;
       end
% connect u with J
[XX,YY]=QVSemiModel(Road_Velocity,u_now,m1,m2,k1,k2,c2semi,XX,h);

Y=[ [Y11;Y21;Y31;] YY];
J1=var(Y(1,:));
J2=var(Y(2,:));
J3=var(Y(3,:));
J=(sqrt((w1*J1^2+w2*J2^2+w3*J3^2)/(w1+w2+w3)));
%JJ=(sqrt((w1*J1^2+w2*J2^2+w3*J3^2)/(w1+w2+w3)));
%J=[J JJ];
%J=sum(J);
%fprintf(fid200018,'%g',J);

%J=fscanf(fid200017,'%g');
%J=J';
fitness=1/(J+1);
%fitness=1/(J+10000);

%status=fclose('all');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -