📄 ga_ex21_demo.m
字号:
echo on
% GA_ex21_demo.m file
% Fin minimization of a function of 2 varaibles
% Minimize f(x,y)=x*sin(4*x)+1.1*y*sin(2*y)
% 0<=x,y<=10
% ANSWER should like this
% x1 = 9.03899200
% x2 = 8.66818900
%==>Best fitness for MIN. problem is (-18.55472108)
% PRESS <Enter> key to continue
pause
%
% PenChen Chou, 7- 1-2001
% Written on 11-22-2001
% This is a Genetic Algorithm based Optimization problem, we will
% use GA_genetic function file to find global minimum
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
% Clear the workspace
clear all
ans=1;
% You will see that there is only ans in WORKSPACE
who
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
% Executing figure(1), we will have a figure #1 window now, pull
% this window to the upper right of your screen. Make it smaller so you
% can see the session on in the MATLAB command window.
figure(1)
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
% For the same reason, you do the above procedure once more
figure(2)
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
% For the same reason, you do the above procedure once more
figure(3)
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
% Start the program execution now
fprintf('>>>Start this program [GA_ex21.m] now. Please wait!\n');
% Define global variables
% MIN_offset>0 for minimization problem. If it is a maximization
% problem, let MIN_offset=0
% MUL_factor=1 for the mose time
global MIN_offset MUL_factor LOCUS x_data y_data
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
%**********************************************************/
% User can modify the following in blocks
%**********************************************************/
% Set initial values
% LOCUS=1 means we will draw a contour for this problem in which
% we can see the path of search and initial and final points
MIN_offset=25; % 25 is the supremum of this f(x,y)
LOCUS=1; x_data=[]; y_data=[]; MUL_factor=1;
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
% GA_f21.m is the function file to calculate FITNESS of OBJECTIVE
% function
obj_fcn = 'GA_f21'; % Objective function
% See ranges of x and y. The upper row is LOW BOUND, the lower
% is HIGH BOUND
range = [0 0
10 10]; % Range of the input variables
IC=[]; elite=1; % Set elite=1 to keep the best-fitness gene
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
% Set # of generations, population size(genes) and bit length
gen_no=800; popuSize=50; bit_n=60;
% Set crossover and muatation rate
xover_rate=0.8; mutate_rate=0.05;
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
% Draw contour and mesh plots of this function
x=0:0.1:10; y=x; [X,Y]=meshgrid(x,y);
Z=X.*sin(4*X)+1.1*Y.*sin(2*Y);
figure(2);contour(X,Y,Z)
% The peak is the location where the min. ocurred
figure(3);meshc(X,Y,-Z)
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
%************************************************************
% Everthing is ready, let us go!
tic % Remember time to start
% call GA_genetic
echo off
[popu, popu_real, fcn_value, upper, average,...
lower, BEST_popu, popuSize, gen_no, para, best_pi,...
bit_n, xover_rate, mutate_rate]=...
GA_genetic(obj_fcn, range, IC, elite, gen_no, popuSize,...
bit_n, xover_rate, mutate_rate);
echo on
% PRESS <Enter> key to continue
pause
%-------------------------------------------------------------------
t=toc/60; % Mark the ending time and display the elapsed time in minutes
fprintf('==> Computation time is (%.2f) minutes.\n\n',t);
% Plot locus of optimization process
LAST=length(x_data);
figure(2); hold on
plot(x_data(1),y_data(1),'x');
plot(x_data,y_data);
plot(x_data(LAST),y_data(LAST),'o');
%plot(0,0,'*'); % The best position
hold off
title('*=BEST, x=Starting point, o=Ending point')
% END of this program
%
% END of this demonstration
echo off
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -