matlab涡流matlab邻域搜索算法法程序

2954人阅读
Machine learning(19)
文章转自:/thread-.html
基于GridSearch的svm参数寻优
/thread-.html
基于GA的svm参数寻优
/thread-.html
====================================================
在这里使用启发式算法PSO来进行参数寻优,用网格划分(grid search)来寻找最佳的参数c和g,虽然采用网格搜索能够找到在CV意义下的最高的分类准确率,即全局最优解,但有时候如果想在更大的范围内寻找最佳的参数c和g会很费时,采用启发式算法就可以不必遍历网格内的所有的参数点,也能找到全局最优解。
关于粒子群优化算法这里不打算过多介绍,想要学习的朋友可以自己查看相关资料。
使用PSO来进行参数寻优在在libsvm-mat-2.89-3[FarutoUltimate3.0]工具箱中已经实现psoSVMcgForClass.m(分类问题参数寻优)、psoSVMcgForRegress.m(回归问题参数寻优)。
函数使用接口:
利用PSO参数寻优函数(分类问题):psoSVMcgForClass
[bestCVaccuracy,bestc,bestg,pso_option]=&
psoSVMcgForClass(train_label,train,pso_option)
train_label:训练集的标签,格式要求与svmtrain相同。
train:训练集,格式要求与svmtrain相同。
pso_option:PSO中的一些参数设置,可不输入,有默认值,详细请看代码的帮助说明。
bestCVaccuracy:最终CV意义下的最佳分类准确率。
bestc:最佳的参数c。
bestg:最佳的参数g。
pso_option:记录PSO中的一些参数。
==========================================================
利用PSO参数寻优函数(回归问题):psoSVMcgForRegress
[bestCVmse,bestc,bestg,pso_option]=&
psoSVMcgForRegress(train_label,train,pso_option)
其输入输出与psoSVMcgForClass类似,这里不再赘述。
psoSVMcgForClass源代码:
function [bestCVaccuarcy,bestc,bestg,pso_option] = psoSVMcgForClass(train_label,train,pso_option)
% psoSVMcgForClass
% by faruto
%Email:patrick. QQ: .cn/faruto BNU
%last modified
%% 若转载请注明:
% 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));
% 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.
Vcmin = -V
Vgmax = pso_option.k*pso_option.
Vgmin = -V
eps = 10^(-3);
%% 产生初始粒子和速度
for i=1:pso_option.sizepop
& & % 随机产生种群和速度
& & pop(i,1) = (pso_option.popcmax-pso_option.popcmin)*rand+pso_option.
& & pop(i,2) = (pso_option.popgmax-pso_option.popgmin)*rand+pso_option.
& & 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);
% 找极值和极值点
[global_fitness bestindex]=min(fitness); % 全局极值
local_fitness=& &% 个体极值初始化
global_x=pop(bestindex,:);& &% 全局极值点
local_x=& & % 个体极值点初始化
% 每一代种群的平均适应度
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) = V
& && &&&end
& && &&&if V(j,1) & Vcmin
& && && && &V(j,1) = V
& && &&&end
& && &&&if V(j,2) & Vgmax
& && && && &V(j,2) = V
& && &&&end
& && &&&if V(j,2) & Vgmin
& && && && &V(j,2) = V
& && &&&end
& && &&&%种群更新
& && &&&pop(j,:)=pop(j,:) + pso_option.wP*V(j,:);
& && &&&if pop(j,1) & pso_option.popcmax
& && && && &pop(j,1) = pso_option.
& && &&&end
& && &&&if pop(j,1) & pso_option.popcmin
& && && && &pop(j,1) = pso_option.
& && &&&end
& && &&&if pop(j,2) & pso_option.popgmax
& && && && &pop(j,2) = pso_option.
& && &&&end
& && &&&if pop(j,2) & pso_option.popgmin
& && && && &pop(j,2) = pso_option.
& && &&&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.
& && && && &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
& && && && &
& && &&&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
& & fit_gen(i) = global_
& & avgfitness_gen(i) = sum(fitness)/pso_option.
%% 结果分析
plot(-fit_gen,'r*-','LineWidth',1.5);
plot(-avgfitness_gen,'o-','LineWidth',1.5);
legend('最佳适应度','平均适应度',3);
xlabel('进化代数','FontSize',12);
ylabel('适应度','FontSize',12);
% 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);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:96555次
积分:1786
积分:1786
排名:第19789名
原创:68篇
转载:114篇
评论:11条
(2)(9)(5)(5)(1)(2)(3)(7)(7)(20)(16)(7)(7)(4)(3)(3)(8)(8)(12)(5)(6)(12)(11)(8)(10)%%%模拟退火算法源程序;%此题以中国31省会城市的最短旅行路径为例:;%;function[MinD,BestPath]=;%CityPosition存储的为每个城市的二维;CityPosition=[;7;;2545235
模拟退火算法源程序
此题以中国31省会城市的最短旅行路径为例:
function [MinD,BestPath]=MainAneal(pn)
% CityPosition存储的为每个城市的二维坐标x和y;
CityPosition=[39 44;88 56;;...
790;07 56;81 1676;...
15 79;80 78;;...
29 76;39 40;;...
figure(1);
plot(CityPosition(:,1),CityPosition(:,2),'o')
m=size(CityPosition,1);%城市的数目
D = sqrt((CityPosition(:,ones(1,m)) - CityPosition(:,ones(1,m))').^2 + ...
(CityPosition(:,2*ones(1,m)) - CityPosition(:,2*ones(1,m))').^2);
path=zeros(pn,m);
for i=1:pn
path(i,:)=randperm(m);
iter_max=100;%i
Len1=zeros(1,pn);Len2=zeros(1,pn);path2=zeros(pn,m);
t=zeros(1,pn);
T=1e5; tau=1e-5;
while T&=tau
iter_num=1;
while m_num&m_max && iter_num&iter_max
for i=1:pn
Len1(i)=sum([D(path(i,1:m-1)+m*(path(i,2:m)-1))
D(path(i,m)+m*(path(i,1)-1))]);
path2(i,:)=ChangePath2(path(i,:),m);
Len2(i)=sum([D(path2(i,1:m-1)+m*(path2(i,2:m)-1))
D(path2(i,m)+m*(path2(i,1)-1))]);
R=rand(1,pn);
if find((Len2-Len1&t&exp((Len1-Len2)/T)&R)~=0)
path(find((Len2-Len1&t&exp((Len1-Len2)/T)&R)~=0),:)=path2(find((Len2-Len1&t&exp((Len1-Len2)/T)&R)~=0),:); %#ok&FNDSB&
Len1(find((Len2-Len1&t&exp((Len1-Len2)/T)&R)~=0))=Len2(find((Len2-Len1&t&exp((Len1-Len2)/T)&R)~=0));
[TempMinD,TempIndex]=min(Len1);
TracePath(N,:)=path(TempIndex,:); %#ok&AGROW&
Distance(N)=TempMinD; %#ok&AGROW&
m_num=m_num+1;
iter_num=iter_num+1;
[MinD,Index]=min(Distance);
BestPath=TracePath(Index,:);%disp(MinD)
%画出路线图
figure(2);
plot(CityPosition(BestPath(1:end-1),1),CityPosition(BestPath(1:end-1),2),'r*-');
function p2=ChangePath2(p1,CityNum)
R=unidrnd(CityNum,1,2);
if abs(R(1)-R(2)) & 0
I=R(1);J=R(2);
p2(1:I)=p1(1:I);
p2(I+1:J)=p1(J:-1:I+1);
p2(J+1:CityNum)=p1(J+1:CityNum);
p2(1:J-1)=p1(1:J-1);
p2(J:I+1)=p1(I+1:-1:J);
p2(I:CityNum)=p1(I:CityNum);
禁忌搜索算法解决TSP问题
%此题以中国31省会城市的最短旅行路径为例:
%禁忌搜索是对局部领域搜索的一种扩展,是一种全局逐步寻优算法,搜索过程可以接受劣解,有较强的爬山能力.领域结构对收敛性有很大影响。
function [BestShortcut,theMinDistance]=TabuSearch
Clist=[39 44;88 56;;...
790;07 56;81 1676;...
15 79;80 78;;...
29 76;39 40;;...
CityNum=size(Clist,1);%TSP问题的规模,即城市数目
dislist=zeros(CityNum);
for i=1:CityNum
for j=1:CityNum
dislist(i,j)=((Clist(i,1)-Clist(j,1))^2+(Clist(i,2)-Clist(j,2))^2)^0.5;
TabuList=zeros(CityNum);% (tabu list)
TabuLength=round((CityNum*(CityNum-1)/2)^0.5);%禁忌长度(tabu length)
Candidates=200;%候选集的个数 (全部领域解个数)
CandidateNum=zeros(Candidates,CityNum);%候选解集合
S0=randperm(CityNum);%随机产生初始解
figure(1);
stop = uicontrol('style','toggle','string'…
,'stop','background','white');
StopL=80*CityN
while p&StopL
if Candidates&CityNum*(CityNum-1)/2
disp('候选解个数不大于n*(n-1)/2!');
ALong(p)=Fun(dislist,S0);
A=zeros(Candidates,2);
while i&=Candidates
M=CityNum*rand(1,2);
M=ceil(M);
if M(1)~=M(2)
A(i,1)=max(M(1),M(2));
A(i,2)=min(M(1),M(2));
for j=1:i-1
if A(i,1)==A(j,1) && A(i,2)==A(j,2)
BestCandidateNum=100;%保留前BestCandidateNum个最好候选解
BestCandidate=Inf*ones(BestCandidateNum,4);
F=zeros(1,Candidates);
for i=1:Candidates
CandidateNum(i,:)=S0;
CandidateNum(i,[A(i,2),A(i,1)])=S0([A(i,1),A(i,2)]);
F(i)=Fun(dislist,CandidateNum(i,:));
if i&=BestCandidateNum
BestCandidate(i,2)=F(i);
BestCandidate(i,1)=i;
BestCandidate(i,3)=S0(A(i,1));
BestCandidate(i,4)=S0(A(i,2));
for j=1:BestCandidateNum
if F(i)&BestCandidate(j,2)
BestCandidate(j,2)=F(i);
BestCandidate(j,1)=i;
BestCandidate(j,3)=S0(A(i,1));
BestCandidate(j,4)=S0(A(i,2));
%对BestCandidate
[JL,Index]=sort(BestCandidate(:,2));
SBest=BestCandidate(Index,:);
BestCandidate=SB
if BestCandidate(1,2)&BestL
BestL=BestCandidate(1,2);
S0=CandidateNum(BestCandidate(1,1),:);
for m=1:CityNum
for n=1:CityNum
if TabuList(m,n)~=0
TabuList(m,n)=TabuList(m,n)-1;
TabuList(BestCandidate(1,3),BestCandidate(1,4))=TabuL
i=1:BestCandidateNum
TabuList(BestCandidate(i,3),BestCandidate(i,4))==0
S0=CandidateNum(BestCandidate(i,1),:);
for m=1:CityNum
for n=1:CityNum
if TabuList(m,n)~=0
TabuList(m,n)=TabuList(m,n)-1;
TabuList(BestCandidate(i,3),BestCandidate(i,4))=TabuL
ArrBestL(p)=BestL; %#ok&AGROW&
for i=1:CityNum-1
plot([Clist(BSF(i),1),Clist(BSF(i+1),1)],[Clist(BSF(i),2),Clist(BSF(i+1),2)],'bo-');
三亿文库包含各类专业文献、生活休闲娱乐、行业资料、外语学习资料、中学教育、专业论文、文学作品欣赏、幼儿教育、小学教育、96模拟退火算法及禁忌搜索算法的matlab源程序等内容。 
 模拟退火算法摘要: 阐述了模拟退火算法的基本原理及...关键词:模拟退火算法,全局寻优,搜索策略 simulated...结论利用 MATLAB 语言实现的模拟退火程序,能够找到...  模拟退火算法求解TSP问题Matlab源码_理学_高等教育_教育专区。模拟退火算法求解TSP...基于模拟退火算法的TSP问... 13页 1下载券
模拟退火算法及禁忌搜索... 3...  爬山算法 介绍模拟退火前,先介绍爬山算法。爬山算法是一种简单的贪心 搜索算法,...代码: %%读入城市经纬度数据 datas=xlsread('34个城市经纬度.xlsx'); n=...  (6) 如果满足终止条件则输出当前解作为最优解,结 束程序. 终止条件通常取为...基于MATLAB的模拟退火算... 4页 免费 模拟退火算法及禁忌搜索... 6页 5下载...  模拟退火算法及禁忌搜索算... 6页 2财富值 完全图哈密尔顿圈的遗传模... 4...模拟退火算法(MATLAB实现) 4页 免费 基于模拟退火算法的旅行商... 15页 免费...  蚁群算法与模拟退火算法对旅游路线问题的探究(附matlab程序)_理学_高等教育_教育...禁忌搜索算法等理论相比还远不 成熟,实际应用也远未挖掘出其真正潜力还有许多...  优化算法的MATLAB实现技巧(单)_计算机软件及应用_IT...比较常见的有禁忌搜 索、模拟退火、遗传算法、蚁群...选取算法实现 中重要部分的MATLAB代码进行详细分析。...  文章重 点阐述了车辆路线问题的模拟退火算法的设计思路,详细分析和编制了求解程序...在启发式算法中,遗传算法 “爬山”能力差,容易出现“早熟”现象,禁忌搜索算法...  优化算法,如蚁群算法、遗传算法、模拟退火、禁忌搜索...结束程序.终止条件通常取为连续 若干个新解都没有被...5.6 仿真分析与评价为了验证用 MATLAB 实现的模拟...

我要回帖

更多关于 网格搜索算法 matlab 的文章

 

随机推荐