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

📄 ga_fit.m

📁 遗传算法插值程序
💻 M
字号:
clear; 
% Generate a set of points in the space 
source_point = -10+20*rand(2,200); 

% Classified boundary is y = sin(x)+2*cos^2(x) 
points_c = zeros(1,200) 
for i = 1:200 
point_c(i) = source_point(2,i)>(sin(source_point(1,i))+2*(cos(source_point(1,i))^2)); 
end 

save training_data 
% Plot the result 
figure(); 
hold on; 
for i = 1:200 
if point_c(i) == 1 
plot(source_point(1,i),source_point(2,i),'*'); 
else 
plot(source_point(1,i),source_point(2,i),'+'); 
end 
end 
%plot the boundary 
x = -10:0.01:10; 
y = sin(x)+2.*cos(x).^2; 
plot(x,y,'r'); 
hold off; 

% The training progress 
% Defined the lower boundary 
LB = zeros(1,25); 
LB(1:18) = -1*ones(1,18); 
LB(19:25) = -10*ones(1,7); 

% Defined the upper boundary 
UB = zeros(1,25); 
UB(1:18) = 1*ones(1,18); 
UB(19:25) = 10*ones(1,7); 

% Set the training option 
options=gaoptimset; 
options.PopulationSize=20; 
options.Generations=100; 
options.StallGenLimit=Inf; 
options.StallTimeLimit=Inf; 
options = gaoptimset(options,'PlotFcns',{@gaplotbestf}') 
[par,fval] = ga(@fitness_fuction,25,[],[],[],[],LB,UB,[],options); 

% Generate 50 points to test the parameter 
exam_point = -10+20*rand(2,50); 
output = exam_function(par,exam_point); 

% Plot the result 
figure(); 
hold on; 
for i = 1:50 
if output(i) == 1 
plot(exam_point(1,i),exam_point(2,i),'*'); 
else 
plot(exam_point(1,i),exam_point(2,i),'+'); 
end 
end 
%plot the boundary 
x = -10:0.01:10; 
y = sin(x)+2.*cos(x).^2; 
plot(x,y,'r'); 
hold off; 
 

⌨️ 快捷键说明

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