📄 p_fdmax_x2_schema01.m
字号:
% p_fdmax_x2.m file
% Fin max of x^2 between 0 to 4096
%**********************************************************/
% User can modify the following in blocks
%**********************************************************/
%obj_fcn = 'xpeaksfcn'; % Objective function: 'peaksfcn'
%range = [-3, 3; -3, 3]; % Range of the input variables
%bit_n = 18; % Bit number for each input variable
%obj_fcn = 'xfcn1'; % Objective function
%range = [0, 10; 0, 10]; % Range of the input variables
clear ALL;
obj_fcn = 'func_x2'; % Objective function
range = [0
127]; % Range of the input variables
%bit_n = 30; % Bit number for each input variable
bit_n = 8; % Bit number for each input variable
total_gen=10;
generation_n = total_gen-1; % Number of generations
%popuSize = 20; % Population size
%IF POPUSIZE IS LESS THAN 11, BINARY BITS AS WELL AS CHROMOSOMES ARE
%PRINTED. NO PRINT OUT IF >=11.
popuSize = 8; % Population size
%xover_rate = 1.0; % Crossover rate
%mutate_rate = 0.01; % Mutation rate
xover_rate = 0.7; % Crossover rate (BETTER)
mutate_rate = 0.08; % Mutation rate (BETTER)
elite=1;
mode=0; %BGA
%mode=1; %RGA
if mode==0,
%generation_n = 600; % Number of generations
else
%generation_n = 600; % Number of generations
end
%**********************************************************/
%figure(1);
%xpeaks;
%************************************************************
% call GA written by Chang
tic
%[popu, fcn_value, upper, average, lower]=xgenetic(obj_fcn, range,1);
[popu, popu_real, fcn_value, upper, average, lower, popuSize]=xgenetic(...
obj_fcn, range, mode, [ ], 0, elite, generation_n , ...
popuSize, bit_n, xover_rate, mutate_rate);
t=toc/60;
fprintf('==> Computation time is (%.2f) minutes.\n',t);
figure(2);
xWhitebg;
x = (1:generation_n)';
plot(x, upper, 'o', x, average, 'x', x, lower, '*');
hold on;
plot(x, [upper average lower]);
hold off;
legend('Best', 'Average', 'Poorest',4);
xlabel('Generations'); ylabel('Fitness');
[junk, inx1]=max(upper);
fprintf('The best generation occured at generation #%i.\n',inx1);
[junk, inx1]=max(fcn_value);
bx=popu_real(inx1);
fprintf('==>The best x=%6.2f, the fitness=%10.2f\n',bx,bx*bx);
[fcn_value, popu_real] = xevalpopu(mode, popu, bit_n, range, obj_fcn);
cnt=0;
avg=0;
for(i=1:1:popuSize)
if( popu(i,1)==1 & popu(i,2)==1)
avg=avg+fcn_value(i);
cnt=cnt+1;
fprintf('The popu of schema 11###### is %i %i %i %i %i %i %i %i ,',popu(i,:)');
fprintf(' finess value is %10.2f\n',fcn_value(i));
end;
end;
if cnt>0
avg=avg/cnt;
end
fprintf('The avg of schema 11###### is %10.2f\n\n',avg);
cnt=0;
avg=0;
for(i=1:1:popuSize)
if( popu(i,1)==1 )
avg=avg+fcn_value(i);
cnt=cnt+1;
fprintf('The popu of schema 1####### is %i %i %i %i %i %i %i %i ,',popu(i,:)');
fprintf(' finess value is %10.2f\n',fcn_value(i));
end;
end;
if cnt>0
avg=avg/cnt;
end
fprintf('The avg of schema 1####### is %10.2f\n\n',avg);
cnt=0;
avg=0;
for(i=1:1:popuSize)
if( popu(i,1)==0 )
avg=avg+fcn_value(i);
cnt=cnt+1;
fprintf('The popu of schema 0####### is %i %i %i %i %i %i %i %i ,',popu(i,:)');
fprintf(' finess value is %10.2f\n',fcn_value(i));
end;
end;
if cnt>0
avg=avg/cnt;
end
fprintf('The avg of schema 0####### is %10.2f\n',avg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -