SVM三種尋優方法matlab程式碼 grid search、GA、PSO
阿新 • • 發佈:2018-12-31
- function [bestCVaccuarcy,bestc,bestg,pso_option] = psoSVMcgForClass(train_label,train,pso_option)
- % psoSVMcgForClass
- %%
- % by faruto
- %Email:[email protected] QQ:516667408 http://blog.sina.com.cn/faruto BNU
- %last modified 2010.01.17
- %% 若轉載請註明:
- % faruto and liyang , LIBSVM-farutoUltimateVersion
- % a toolbox with implements for support vector machines based on libsvm, 2009.
- %
- % Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for
- % support vector machines, 2001. Software available at
- % http://www.csie.ntu.edu.tw/~cjlin/libsvm
- %% 引數初始化
- if nargin == 2
- pso_option = struct('c1',1.5,'c2',1.7,'maxgen',200,'sizepop',20, ...
- 'k',0.6,'wV',1,'wP',1,'v',5, ...
- 'popcmax',10^2,'popcmin',10^(-1),'popgmax',10^3,'popgmin',10^(-2));
- end
- % c1:初始為1.5,pso引數區域性搜尋能力
- % c2:初始為1.7,pso引數全域性搜尋能力
- % maxgen:初始為200,最大進化數量
- % sizepop:初始為20,種群最大數量
- % k:初始為0.6(k belongs to [0.1,1.0]),速率和x的關係(V = kX)
- % wV:初始為1(wV best belongs to [0.8,1.2]),速率更新公式中速度前面的彈性係數
- % wP:初始為1,種群更新公式中速度前面的彈性係數
- % v:初始為3,SVM Cross Validation引數
- % popcmax:初始為100,SVM 引數c的變化的最大值.
- % popcmin:初始為0.1,SVM 引數c的變化的最小值.
- % popgmax:初始為1000,SVM 引數g的變化的最大值.
- % popgmin:初始為0.01,SVM 引數c的變化的最小值.
- Vcmax = pso_option.k*pso_option.popcmax;
- Vcmin = -Vcmax ;
- Vgmax = pso_option.k*pso_option.popgmax;
- Vgmin = -Vgmax ;
- eps = 10^(-3);
- %% 產生初始粒子和速度
- for i=1:pso_option.sizepop
- % 隨機產生種群和速度
- pop(i,1) = (pso_option.popcmax-pso_option.popcmin)*rand+pso_option.popcmin;
- pop(i,2) = (pso_option.popgmax-pso_option.popgmin)*rand+pso_option.popgmin;
- V(i,1)=Vcmax*rands(1,1);
- V(i,2)=Vgmax*rands(1,1);
- % 計算初始適應度
- cmd = ['-v ',num2str(pso_option.v),' -c ',num2str( pop(i,1) ),' -g ',num2str( pop(i,2) )];
- fitness(i) = svmtrain(train_label, train, cmd);
- fitness(i) = -fitness(i);
- end
- % 找極值和極值點
- [global_fitness bestindex]=min(fitness); % 全域性極值
- local_fitness=fitness; % 個體極值初始化
- global_x=pop(bestindex,:); % 全域性極值點
- local_x=pop; % 個體極值點初始化
- % 每一代種群的平均適應度
- avgfitness_gen = zeros(1,pso_option.maxgen);
- %% 迭代尋優
- for i=1:pso_option.maxgen
- for j=1:pso_option.sizepop
- %速度更新
- V(j,:) = pso_option.wV*V(j,:) + pso_option.c1*rand*(local_x(j,:) - pop(j,:)) + pso_option.c2*rand*(global_x - pop(j,:));
- if V(j,1) > Vcmax
- V(j,1) = Vcmax;
- end
- if V(j,1) < Vcmin
- V(j,1) = Vcmin;
- end
- if V(j,2) > Vgmax
- V(j,2) = Vgmax;
- end
- if V(j,2) < Vgmin
- V(j,2) = Vgmin;
- end
- %種群更新
- pop(j,:)=pop(j,:) + pso_option.wP*V(j,:);
- if pop(j,1) > pso_option.popcmax
- pop(j,1) = pso_option.popcmax;
- end
- if pop(j,1) < pso_option.popcmin
- pop(j,1) = pso_option.popcmin;
- end
- if pop(j,2) > pso_option.popgmax
- pop(j,2) = pso_option.popgmax;
- end
- if pop(j,2) < pso_option.popgmin
- pop(j,2) = pso_option.popgmin;
- end
- % 自適應粒子變異
- if rand>0.5
- k=ceil(2*rand);
- if k == 1
- pop(j,k) = (20-1)*rand+1;
- end
- if k == 2
- pop(j,k) = (pso_option.popgmax-pso_option.popgmin)*rand + pso_option.popgmin;
- end
- end
- %適應度值
- cmd = ['-v ',num2str(pso_option.v),' -c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];
- fitness(j) = svmtrain(train_label, train, cmd);
- fitness(j) = -fitness(j);
- cmd_temp = ['-c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];
- model = svmtrain(train_label, train, cmd_temp);
- if fitness(j) >= -65
- continue;
- end
- %個體最優更新
- if fitness(j) < local_fitness(j)
- local_x(j,:) = pop(j,:);
- local_fitness(j) = fitness(j);
- end
- if abs( fitness(j)-local_fitness(j) )<=eps && pop(j,1) < local_x(j,1)
- local_x(j,:) = pop(j,:);
- local_fitness(j) = fitness(j);
- end
- %群體最優更新
- if fitness(j) < global_fitness
- global_x = pop(j,:);
- global_fitness = fitness(j);
- end
- if abs( fitness(j)-global_fitness )<=eps && pop(j,1) < global_x(1)
- global_x = pop(j,:);
- global_fitness = fitness(j);
- end
- end
- fit_gen(i) = global_fitness;
- avgfitness_gen(i) = sum(fitness)/pso_option.sizepop;
- end
- %% 結果分析
- figure;
- hold on;
- plot(-fit_gen,'r*-','LineWidth',1.5);
- plot(-avgfitness_gen,'o-','LineWidth',1.5);
- legend('最佳適應度','平均適應度',3);
- xlabel('進化代數','FontSize',12);
- ylabel('適應度','FontSize',12);
- grid on;
- % print -dtiff -r600 pso
- bestc = global_x(1);
- bestg = global_x(2);
- bestCVaccuarcy = -fit_gen(pso_option.maxgen);
- line1 = '適應度曲線Accuracy[PSOmethod]';
- line2 = ['(引數c1=',num2str(pso_option.c1), ...
- ',c2=',num2str(pso_option.c2),',終止代數=', ...
- num2str(pso_option.maxgen),',種群數量pop=', ...
- num2str(pso_option.sizepop),')'];
- % line3 = ['Best c=',num2str(bestc),' g=',num2str(bestg), ...
- % ' CVAccuracy=',num2str(bestCVaccuarcy),'%'];
- % title({line1;line2;line3},'FontSize',12);
- title({line1;line2},'FontSize',12);