function [BestMSE,Bestc,Bestg,ga_option] = gaSVMcgForRegress(train_label,train_data,ga_option)
%% 参数初始化
if nargin == 2
ga_option = struct('maxgen',200,'sizepop',20,'ggap',0.9,...
'cbound',[0,100],'gbound',[0,1000],'v',5);
end
% maxgen:最大的进化代数,默认为200,一般取值范围为[100,500]
% sizepop:种群最大数量,默认为20,一般取值范围为[20,100]
% cbound = [cmin,cmax],参数c的变化范围,默认为(0,100]
% gbound = [gmin,gmax],参数g的变化范围,默认为[0,1000]
% v:SVM Cross Validation参数,默认为5
%%
MAXGEN = ga_option.maxgen;
NIND = ga_option.sizepop;
NVAR = 2;
PRECI = 20;
GGAP = ga_option.ggap;
trace = zeros(MAXGEN,2);
FieldID = ...
[rep([PRECI],[1,NVAR]);[ga_option.cbound(1),ga_option.gbound(1);ga_option.cbound(2),ga_option.gbound(2)];...
[1,1;0,0;0,1;1,1]];
Chrom = crtbp(NIND,NVAR*PRECI);
gen = 1;
v = ga_option.v;
BestMSE = inf;
Bestc = 0;
Bestg = 0;
%%
cg = bs2rv(Chrom,FieldID);
for nind = 1:NIND
cmd = ['-v ',num2str(v),' -c ',num2str(cg(nind,1)),' -g ',num2str(cg(nind,2)),' -s 3 -p 0.01'];
ObjV(nind,1) = svmtrain(train_label,train_data,cmd);
end
[BestMSE,I] = min(ObjV);
Bestc = cg(I,1);
Bestg = cg(I,2);
%%
while 1
FitnV = ranking(ObjV);
SelCh = select('sus',Chrom,FitnV,GGAP);
SelCh = recombin('xovsp',SelCh,0.7);
SelCh = mut(SelCh);
cg = bs2rv(SelCh,FieldID);
for nind = 1:size(SelCh,1)
cmd = ['-v ',num2str(v),' -c ',num2str(cg(nind,1)),' -g ',num2str(cg(nind,2)),' -s 3 -p 0.01'];
ObjVSel(nind,1) = svmtrain(train_label,train_data,cmd);
end
[Chrom,ObjV] = reins(Chrom,SelCh,1,1,ObjV,ObjVSel);
[NewBestCVaccuracy,I] = min(ObjV);
cg_temp = bs2rv(Chrom,FieldID);
temp_NewBestCVaccuracy = NewBestCVaccuracy;
if NewBestCVaccuracy < BestMSE
BestMSE = NewBestCVaccuracy;
Bestc = cg_temp(I,1);
Bestg = cg_temp(I,2);
end
if abs( NewBestCVaccuracy-BestMSE ) <= 10^(-2) && ...
cg_temp(I,1) < Bestc
BestMSE = NewBestCVaccuracy;
Bestc = cg_temp(I,1);
Bestg = cg_temp(I,2);
end
trace(gen,1) = min(ObjV);
trace(gen,2) = sum(ObjV)/length(ObjV);
if gen >= MAXGEN/2 && ...
( temp_NewBestCVaccuracy-BestMSE ) <= 10^(-2)
break;
end
if gen == MAXGEN
break;
end
gen = gen + 1;
end
%%
figure;
hold on;
trace = round(trace*10000)/10000;
plot(trace(1:gen,1),'r*-','LineWidth',1.5);
plot(trace(1:gen,2),'o-','LineWidth',1.5);
legend('最佳适应度','平均适应度');
xlabel('进化代数','FontSize',12);
ylabel('适应度','FontSize',12);
grid on;
axis auto;
line1 = '适应度曲线MSE[GAmethod]';
line2 = ['(终止代数=', ...
num2str(gen),',种群数量pop=', ...
num2str(NIND),')'];
line3 = ['Best c=',num2str(Bestc),' g=',num2str(Bestg), ...
' MSE=',num2str(BestMSE)];
title({line1;line2;line3},'FontSize',12);
- 1
- 2
- 3
- 4
- 5
- 6
前往页