1.利用PSO参数寻优函数(分类问题):psoSVMcgForClass2.[bestCVaccuracy,bestc,bestg,pso_option]=3.psoSVMcgForClass(train_label,train,pso_option)4.输入:5.train_label:训练集的标签,格式要求与svmtrain相同。
6.train:训练集,格式要求与svmtrain相同。
7.pso_option:PSO中的一些参数设置,可不输入,有默认值,详细请看代码的帮助说明。
8.输出:9.bestCVaccuracy:最终CV意义下的最佳分类准确率。
10.bestc:最佳的参数c。
11.bestg:最佳的参数g。
12.pso_option:记录PSO中的一些参数。
13.==========================================================14.利用PSO参数寻优函数(回归问题):psoSVMcgForRegress15.[bestCVmse,bestc,bestg,pso_option]=16.psoSVMcgForRegress(train_label,train,pso_option)17.其输入输出与psoSVMcgForClass类似,这里不再赘述。
复制代码psoSVMcgForClass源代码:1.function [bestCVaccuarcy,bestc,bestg,pso_option] =psoSVMcgForClass(train_label,train,pso_option)2.% psoSVMcgForClass3.4.%%5.% by faruto6.%Email:patrick.lee@ QQ:516667408/faruto BNU7.%last modified 2010.01.178.9.%% 若转载请注明:10.% faruto and liyang , LIBSVM-farutoUltimateVersion11.% a toolbox with implements for support vector machines based on libsvm,2009.12.%13.% Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for14.% support vector machines, 2001. Software available at15.% .tw/~cjlin/libsvm16.%% 参数初始化17.if nargin == 218. pso_option =struct('c1',1.5,'c2',1.7,'maxgen',200,'sizepop',20, ...19. 'k',0.6,'wV',1,'wP',1,'v',5, ...20. 'popcmax',10^2,'popcmin',10^(-1),'popgmax',10^3,'popgmin',10^(-2));21.end22.% c1:初始为1.5,pso参数局部搜索能力23.% c2:初始为1.7,pso参数全局搜索能力24.% maxgen:初始为200,最大进化数量25.% sizepop:初始为20,种群最大数量26.% k:初始为0.6(k belongs to [0.1,1.0]),速率和x的关系(V = kX)27.% wV:初始为1(wV best belongs to [0.8,1.2]),速率更新公式中速度前面的弹性系数28.% wP:初始为1,种群更新公式中速度前面的弹性系数29.% v:初始为3,SVM Cross Validation参数30.% popcmax:初始为100,SVM 参数c的变化的最大值.31.% popcmin:初始为0.1,SVM 参数c的变化的最小值.32.% popgmax:初始为1000,SVM 参数g的变化的最大值.33.% popgmin:初始为0.01,SVM 参数c的变化的最小值.34.35.Vcmax = pso_option.k*pso_option.popcmax;36.Vcmin = -Vcmax ;37.Vgmax = pso_option.k*pso_option.popgmax;38.Vgmin = -Vgmax ;39.40.eps = 10^(-3);41.42.%% 产生初始粒子和速度43.for i=1:pso_option.sizepop44.45. % 随机产生种群和速度46. pop(i,1) =(pso_option.popcmax-pso_option.popcmin)*rand+pso_option.popcmin; 47. pop(i,2) =(pso_option.popgmax-pso_option.popgmin)*rand+pso_option.popgmin;48. V(i,1)=Vcmax*rands(1,1);49. V(i,2)=Vgmax*rands(1,1);50.51. % 计算初始适应度52. cmd = ['-v ',num2str(pso_option.v),' -c ',num2str( pop(i,1) ),' -g',num2str( pop(i,2) )];53. fitness(i) = svmtrain(train_label, train, cmd);54. fitness(i) = -fitness(i);55.end56.57.% 找极值和极值点58.[global_fitness bestindex]=min(fitness); % 全局极值59.local_fitness=fitness; % 个体极值初始化60.61.global_x=pop(bestindex,:); % 全局极值点62.local_x=pop; % 个体极值点初始化63.64.% 每一代种群的平均适应度65.avgfitness_gen = zeros(1,pso_option.maxgen);66.67.%% 迭代寻优68.for i=1:pso_option.maxgen69.70. for j=1:pso_option.sizepop71.72. %速度更新73. V(j,:) = pso_option.wV*V(j,:) +pso_option.c1*rand*(local_x(j,:) - pop(j,:)) +pso_option.c2*rand*(global_x - pop(j,:));74. if V(j,1) > Vcmax75. V(j,1) = Vcmax;76. end77. if V(j,1) < Vcmin78. V(j,1) = Vcmin;79. end80. if V(j,2) > Vgmax81. V(j,2) = Vgmax;82. end83. if V(j,2) < Vgmin84. V(j,2) = Vgmin;85. end86.87. %种群更新88. pop(j,:)=pop(j,:) + pso_option.wP*V(j,:);89. if pop(j,1) > pso_option.popcmax90. pop(j,1) = pso_option.popcmax;91. end92. if pop(j,1) < pso_option.popcmin93. pop(j,1) = pso_option.popcmin;94. end95. if pop(j,2) > pso_option.popgmax96. pop(j,2) = pso_option.popgmax;97. end98. if pop(j,2) < pso_option.popgmin99. pop(j,2) = pso_option.popgmin;100. end102. % 自适应粒子变异103. if rand>0.5104. k=ceil(2*rand);105. if k == 1106. pop(j,k) = (20-1)*rand+1;107. end108. if k == 2109. pop(j,k) =(pso_option.popgmax-pso_option.popgmin)*rand + pso_option.popgmin; 110. end111. end112.113. %适应度值114. cmd = ['-v ',num2str(pso_option.v),' -c',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];115. fitness(j) = svmtrain(train_label, train, cmd);116. fitness(j) = -fitness(j);117.118. cmd_temp = ['-c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];119. model = svmtrain(train_label, train, cmd_temp);120.121. if fitness(j) >= -65122. continue;123. end124.125. %个体最优更新126. if fitness(j) < local_fitness(j)127. local_x(j,:) = pop(j,:);128. local_fitness(j) = fitness(j);129. end130.131. if abs( fitness(j)-local_fitness(j) )<=eps && pop(j,1) < local_x(j,1)132. local_x(j,:) = pop(j,:);133. local_fitness(j) = fitness(j);134. end135.136. %群体最优更新137. if fitness(j) < global_fitness138. global_x = pop(j,:);139. global_fitness = fitness(j);140. end142. if abs( fitness(j)-global_fitness )<=eps && pop(j,1) < global_x(1)143. global_x = pop(j,:);144. global_fitness = fitness(j);145. end146.147. end148.149. fit_gen(i) = global_fitness;150. avgfitness_gen(i) = sum(fitness)/pso_option.sizepop;151.end152.153.%% 结果分析154.figure;155.hold on;156.plot(-fit_gen,'r*-','LineWidth',1.5);157.plot(-avgfitness_gen,'o-','LineWidth',1.5);158.legend('最佳适应度','平均适应度',3);159.xlabel('进化代数','FontSize',12);160.ylabel('适应度','FontSize',12);161.grid on;162.163.% print -dtiff -r600 pso164.165.bestc = global_x(1);166.bestg = global_x(2);167.bestCVaccuarcy = -fit_gen(pso_option.maxgen);168.169.line1 = '适应度曲线Accuracy[PSOmethod]';170.line2 = ['(参数c1=',num2str(pso_option.c1), ...171. ',c2=',num2str(pso_option.c2),',终止代数=', ...172. num2str(pso_option.maxgen),',种群数量pop=', ...173. num2str(pso_option.sizepop),')'];174.% line3 = ['Best c=',num2str(bestc),' g=',num2str(bestg), ... 175.% ' CVAccuracy=',num2str(bestCVaccuarcy),'%'];176.% title({line1;line2;line3},'FontSize',12);177.title({line1;line2},'FontSize',12);。