feature selection 心得。PSO based

function [Selection , SelectionValue]=psoSelection(Data,nOfSelection,population,itrationMax,fun)
% PSO SELECTION ALGORITHM
% INPUTS
% Data=Data to be minimize selection 
% nOfSelection=number of selection
% population=number of population
% itrationMax=maximum itration
% fun=fitness function
% OUTPUTS
% Selection=Selected Positions
% SelectionValue=fitness value of selected
% Developer Er.Abbas Manthiri S
% EMail abbasmanthiribe@gmail.com
% Date 23-03-2017

% Boundary
cloumn=size(Data,2);
if cloumn<=nOfSelection
    error('Number of Selection must be positive and less than Selection boundary')
end
Boundary=[1,cloumn];
% Initialize Constants
weight=0.2;
constant1=2;
constant2=2;
% Initialize Pariticles
Positions=zeros(population,nOfSelection);
for i=1:population
    tempVar = randperm(Boundary(2));
    Positions(i,:) = tempVar(1:nOfSelection);%%%%形成一个nOfSelection维的子矩阵,
end
% velocity initialization
Velocity = ones(population,nOfSelection);
% Initialize Global best
itration = 1;
globalBest.value(itration) = inf;
% fun=@distkm;
%% Optimizing
Particles = struct;

while(1)
    % Calculating Fitness Values
    for i=1:population
        Particles(itration).position(i,:) = Positions(i,:);
        Particles(itration).value(i) = fun(Data(:,Positions(i,:)));%这里就是取Data的Positions(i,:)组合列的子集算fitness
    end
    % Update Position Best
    [Particlesbest.value(itration),ind] = min(Particles(itration).value);
    Particlesbest.position(itration,:) = Particles(itration).position(ind,:);
    % Update Global Best
    if globalBest.value(itration) > Particlesbest.value(itration)
        [globalBest.value(itration),globalBest.value(itration+1)] = deal(Particlesbest.value(itration));%%重复赋值
        globalBest.position(itration,:) = Positions(ind(1),:);
    else
        globalBest.value(itration+1) = globalBest.value(itration);
        globalBest.position(itration,:) = globalBest.position(itration-1,:);
    end
    fprintf('\nIteration %d gbest value of  fitness= %f',itration,globalBest.value(itration))
    % Velocity Update
    Velocity = (weight*Velocity) + (constant1*rand(1)*(repmat(Particlesbest.position(itration,:),population,1)-Positions)) + ...
        (constant2*rand(1)*(repmat(globalBest.position(itration,:),population,1)-Positions));
    % Postion Update
    Positions = Positions+round(Velocity);
    % Boundary Checking for Position   下面这个骚操作是如果越界则被重新赋值
    Positions(Positions>Boundary(2)) = round(rand(1)*(Boundary(2)-1))+1;
    Positions(Positions<Boundary(1)) = round(rand(1)*(Boundary(2)-1))+1;
    for i=1:population%%%有重复列也要被重置
        if length(unique( Positions(i,:) )) ~= nOfSelection
        tempVar = randperm(Boundary(2));
        Positions(i,:) = tempVar(1:nOfSelection);
        end
    end
    % Loop Breaking is Positions all are same or itertion acheived maximum
    % itertion
    count=0;
    for i=1:population-1;
        if Positions(i,:) == Positions(i+1,:)
            count = count+1;
        end
    end
    if (count == population-1) || (itration >= itrationMax)
        fprintf('\n******Iteration completed******\n')
        break
    end
    itration = itration+1;
end
Selection=globalBest.position(itration,:);
SelectionValue=globalBest.value(itration)

本代码节选自mathwork。非原创作品

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Matlab中好用的数据降维和特征选择工具包 Copyright (c) 2018, Giorgio Roffo All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution * Neither the name of University of Glasgow nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值