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

📄 dp22a_passino.m

📁 其中提到遺傳學的程式碼與應用提供給次淚相向的研究者參考下載
💻 M
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DP2.2(a)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fuzzy Cruise Controller. dp22a_passino.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% By: Kevin Passino
% Version: 2/6/97
% (uses some code fragments developed earlier
%  by B. Klinehoffer and K. Passino)
%
% This program is used to simulate the PI fuzzy controller for
% the automobile cruise control problem in Design Problem 2.2(a).
% The variable names are chosen accordingly.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear		% Clear all variables in memory

% Initialize vehicle parameters

m=1300;   % Mass of the vehicle
Ar=0.3;   % Aerodynamic drag
tau=0.2;  % Engine/brake time constant
d=100;    % Constant frictional force

% Initialize parameters for the fuzzy controller

nume=11; 	% Number of input membership functions for the e
			% universe of discourse
numie=11; 	% Number of input membership functions for the integral
			% of e universe of discourse

g1=1;,g2=.01;,g0=1000;
		% Scaling gains for tuning membership functions for
		% universes of discourse for e, integral of e (what we
		% sometimes call "int e" below), and u respectively
		% These can be tuned to try to improve the performance.
we=0.2*(1/g1);
	% we is half the width of the triangular input membership
	% function bases (note that if you change g0, the base width
	% will correspondingly change so that we always end
	% up with uniformly distributed input membership functions)
	% Note that if you change nume you will need to adjust the
	% "0.2" factor if you want membership functions that
	% overlap in the same way.
wie=0.2*(1/g2);
	% Similar to we but for the int e universe of discourse
base=0.4*g0;
	% Base width of output membership fuctions of the fuzzy
	% controller

% Place centers of membership functions of the fuzzy controller:

%  Centers of input membership functions for the e universe of
% discourse of fuzzy controller (a vector of centers)
ce=[-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1]*(1/g1);

% Centers of input membership functions for the int e universe of
% discourse of fuzzy controller (a vector of centers)
cie=[-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1]*(1/g2);

% This next matrix specifies the rules of the fuzzy controller.
% The entries are the centers of the output membership functions.
% This choice represents just one guess on how to synthesize
% the fuzzy controller.  Notice the regularity
% of the pattern of rules (basiscally we are using a type of
% saturated index adding).  Notice that it is scaled by g0, the
% output scaling factor, since it is a normalized rule-base.
% The rule-base can be tuned to try to improve performance.

rules=[-1    -1     -1     -1    -1     -1    -0.8  -0.6   -0.4   -0.2    0;
  	   -1    -1     -1     -1    -1   -0.8    -0.6   -0.4   -0.2   0    0.2;
	   -1    -1     -1     -1   -0.8  -0.6    -0.4   -0.2   0    0.2    0.4;
	   -1    -1     -1   -0.8   -0.6  -0.4    -0.2    0    0.2   0.4    0.6;
	   -1    -1   -0.8   -0.6   -0.4  -0.2     0     0.2   0.4   0.6    0.8;
	   -1  -0.8   -0.6   -0.4   -0.2   0      0.2    0.4   0.6   0.8     1;
	 -0.8  -0.6   -0.4   -0.2     0   0.2     0.4    0.6   0.8   1       1;
	 -0.6  -0.4   -0.2    0      0.2  0.4     0.6    0.8   1     1       1;
	 -0.4  -0.2    0     0.2     0.4  0.6     0.8     1    1     1       1;
	 -0.2   0      0.2   0.4     0.6  0.8     1       1    1     1       1;
	    0  0.2     0.4   0.6     0.8  1       1       1    1     1       1]*g0;

% Next, we initialize the simulation:

t=0; 		% Reset time to zero
index=1;	% This is time's index (not time, its index).
tstop=30;	% Stopping time for the simulation (in seconds)
%tstop=60;	% Stopping time for the simulation (in seconds), last part
            % of problem (a), i.e., test input 3
step=0.01;  % Integration step size
x=[18;197.2;20];	% Intial condition on state	of the car
%x=0*x;             % Use a zero initial condition for the last part
                    % of problem (a) (in conjunction with test input 3).

%
% Next, we start the simulation of the system.  This is the main
% loop for the simulation of fuzzy cruise control system.
%
while t <= tstop
	v(index)=x(1);     % Output of the car (velocity)

% Next, we define the reference input vd  (desired speed as
% specified in the problem).

if t<=10, vd(index)=18; end    % First, define "test input 1"
if t>10, vd(index)=22; end

%if t<=10, vd(index)=18; end     % This is "test input 2" (ramp)
%if t>10, vd(index)=vd(index-1)+(4/1500); end % Ramp up 4 in 15 sec.
%if t>25, vd(index)=22; end

%vd(index)=22;      % Use this input for the last part of problem (a)
                    % This is "test input 3."

% Fuzzy controller calculations:
% First, for the given fuzzy controller inputs we determine
% the extent at which the error membership functions
% of the fuzzy controller are on (this is the fuzzification part).

ie_count=0;,e_count=0;   % These are used to count the number of
	% non-zero mf certainities of e and int e
e(index)=vd(index)-v(index);
			% Calculates the error input for the fuzzy controller

b(index)=x(3); % Sets the value of the integral of e

% The following if-then structure fills the vector mfe
% with the certainty of each membership fucntion of e for the
% current input e.  We use triangular membership functions.

	if e(index)<=ce(1)	% Takes care of saturation of the left-most
					    % membership function
         mfe=[1 0 0 0 0 0 0 0 0 0 0]; % i.e., the only one on is the
         	  %left-most one
	 e_count=e_count+1;,e_int=1; 	  %  One mf on, it is the
	 	      %left-most one.
	elseif e(index)>=ce(nume)	  % Takes care of saturation
			%of the right-most mf
	 mfe=[0 0 0 0 0 0 0 0 0 0 1];
	 e_count=e_count+1;,e_int=nume; % One mf on, it is the
	 								%right-most one
	else      % In this case the input is on the middle part of the
			  % universe of discourse for e
			  % Next, we are going to cycle through the mfs to
			  % find all that are on
	   for i=1:nume
		 if e(index)<=ce(i)
		  mfe(i)=max([0 1+(e(index)-ce(i))/we]);
		  				% In this case the input is to the
		  				% left of the center ce(i) and we compute
						% the value of the mfcentered at ce(i)
						% for this input e
			if mfe(i)~=0
				% If the certainty is not equal to zero then say
				% that have one mf on by incrementing our count
			 e_count=e_count+1;
			 e_int=i;	% This term holds the index last entry
			 			% with a non-zero term
			end
		 else
		  mfe(i)=max([0,1+(ce(i)-e(index))/we]);
		  	% In this case the input is to the
		 	% right of the center ce(i)
			if mfe(i)~=0
			 e_count=e_count+1;
			 e_int=i;  % This term holds the index of the
			 		   % last entry with a non-zero term
			end
		 end
		end
	end

% The following if-then structure fills the vector mfie with the
% certainty of each membership fucntion of the integral of e
% for its current value (to understand this part of the code see the above
% similar code for computing mfe).  Clearly, it could be more efficient to
% make a subroutine that performs these computations for each of
% the fuzzy system inputs.

	if b(index)<=cie(1)	% Takes care of saturation of left-most mf
         mfie=[1 0 0 0 0 0 0 0 0 0 0];
	 ie_count=ie_count+1;
	 ie_int=1;
	elseif b(index)>=cie(numie)
				% Takes care of saturation of the right-most mf
	 mfie=[0 0 0 0 0 0 0 0 0 0 1];
	 ie_count=ie_count+1;
	 ie_int=numie;
	else
		for i=1:numie
		 if b(index)<=cie(i)
		  mfie(i)=max([0,1+(b(index)-cie(i))/wie]);
			if mfie(i)~=0
			 ie_count=ie_count+1;
			 ie_int=i;  	% This term holds last entry
			 		     	% with a non-zero term
			end
		 else
		  mfie(i)=max([0,1+(cie(i)-b(index))/wie]);
			if mfie(i)~=0
			 ie_count=ie_count+1;
			 ie_int=i;	% This term holds last entry
			 			% with a non-zero term
			end
		 end
		end
	end

% The next loops calculate the crisp output using only the non-
% zero premise of error,e, and integral of the error b.  This cuts down
% computation time since we will only compute
% the contribution from the rules that are on (i.e., a maximum of
% four rules for our case).  Minimum is used for the premise
% and implication.

num=0;
den=0;
	for k=(e_int-e_count+1):e_int
		% Scan over e indices of mfs that are on
		for l=(ie_int-ie_count+1):ie_int
		% Scan over int e indices of mfs that are on
		  prem=min([mfe(k) mfie(l)]);
		% Value of premise membership function
 % This next calculation of num adds up the numerator for the
 % defuzzification formula. rules(k,l) is the output center
 % for the rule.base*(prem-(prem)^2/2 is the area of a symmetric
 % triangle with base width "base" and that is chopped off at
 % a height of prem (since we use minimum to represent the
 % implication). Computation of den is similar but without
 % rules(k,l).
		  num=num+rules(k,l)*base*(prem-(prem)^2/2);
		  den=den+base*(prem-(prem)^2/2);
		end
	end

   u(index)=num/den;
   			% Crisp output of fuzzy controller that is the input
   			% to the vehicle.

% Next, the Runge-Kutta equations are used to find the next state.
% Clearly, it would be better to use a Matlab "function" for
% F (but here we do not so we can have only one program).
% (This does not implement the exact equations in the book but
% if you think about the problem a bit you will see that these
% really should be accurate enough. Here, we are not calculating
% several intermediate values of the controller output, only
% one per integration step; we do this simply to save some
% computations.)

	time(index)=t;

F=[(1/m)*(-Ar*x(1)^2 - d + x(2)) ;
   (1/tau)*(-x(2)+u(index))    ;
   vd(index)-x(1)              ];

	k1=step*F;
	xnew=x+k1/2;

F=[(1/m)*(-Ar*xnew(1)^2 - d + xnew(2)) ;
   (1/tau)*(-xnew(2)+u(index))    ;
   vd(index)-xnew(1)              ];

	k2=step*F;
	xnew=x+k2/2;

F=[(1/m)*(-Ar*xnew(1)^2 - d + xnew(2)) ;
   (1/tau)*(-xnew(2)+u(index))    ;
   vd(index)-xnew(1)              ];

	k3=step*F;
	xnew=x+k3;

F=[(1/m)*(-Ar*xnew(1)^2 - d + xnew(2)) ;
   (1/tau)*(-xnew(2)+u(index))    ;
   vd(index)-xnew(1)              ];

	k4=step*F;
	x=x+(1/6)*(k1+2*k2+2*k3+k4); % Calculated next state

t=t+step;  			% Increments time
index=index+1;	 	% Increments the indexing term so that
					% index=1 corresponds to time t=0.

end % This end statement goes with the first "while" statement
    % in the program

%
% Next, we provide plots of the input and output of the vehicle
% along with the reference speed that we want to track.
% It is also easy to plot the two inputs to the fuzzy controller,
% the integral of e (b(t)) and e(t) if you would like since these
% values are also saved by the program.
%

figure(1)
subplot(211)
plot(time,v,'-',time,vd,'--')
grid on
xlabel('Time (sec)')
title('Vehicle speed (solid) and desired speed (dashed)')
subplot(212)
plot(time,u)
grid on
xlabel('Time (sec)')
title('Output of fuzzy controller (input to the vehicle)')
disp('End of this program execution');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End of program %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

⌨️ 快捷键说明

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