minn算法matlab,MATLAB代做-PSO工具箱的函数寻优算法matlab源码程序

% INITIALIZE END INITIALIZE END INITIALIZE END INITIALIZE END

rstflg = 0; % for dynamic environment checking

% start PSO iterative procedures

cnt    = 0; % counter used for updating display according to df in the options

cnt2   = 0; % counter used for the stopping subroutine based on error convergence

iwt(1) = iw1;

for i=1:me  % start epoch loop (iterations)

out        = feval(functname,[pos;gbest]);

outbestval = out(end,:);

out        = out(1:end-1,:);

tr(i+1)          = gbestval; % keep track of global best val

te               = i; % returns epoch number to calling program when done

bestpos(i,1:D+1) = [gbest,gbestval];

%assignin('base','bestpos',bestpos(i,1:D+1));

%------------------------------------------------------------------------

% this section does the plots during iterations

if plotflg==1

if (rem(i,df) == 0 ) | (i==me) | (i==1)

fprintf(message,i,gbestval);

cnt = cnt+1; % count how many times we display (useful for movies)

eval(plotfcn); % defined at top of script

end  % end update display every df if statement

end % end plotflg if statement

% check for an error space that changes wrt time/iter

% threshold value that determines dynamic environment

% sees if the value of gbest changes more than some threshold value

% for the same location

chkdyn = 1;

rstflg = 0; % for dynamic environment checking

if chkdyn==1

threshld = 0.05;  % percent current best is allowed to change, .05 = 5% etc

letiter  = 5; % # of iterations before checking environment, leave at least 3 so PSO has time to converge

outorng  = abs( 1- (outbestval/gbestval) ) >= threshld;

samepos  = (max( sentry == gbest ));

if (outorng & samepos) & rem(i,letiter)==0

rstflg=1;

% disp('New Environment: reset pbest, gbest, and vel');

%% reset pbest and pbestval if warranted

%        outpbestval = feval( functname,[pbest] );

%        Poutorng    = abs( 1-(outpbestval./pbestval) ) > threshld;

%        pbestval    = pbestval.*~Poutorng + outpbestval.*Poutorng;

%        pbest       = pbest.*repmat(~Poutorng,1,D) + pos.*repmat(Poutorng,1,D);

pbest     = pos; % reset personal bests to current positions

pbestval  = out;

vel       = vel*10; % agitate particles a little (or a lot)

% recalculate best vals

if minmax == 1

[gbestval,idx1] = max(pbestval);

elseif minmax==0

[gbestval,idx1] = min(pbestval);

elseif minmax==2 % this section needs work

[temp,idx1] = min((pbestval-ones(size(pbestval))*errgoal).^2);

gbestval    = pbestval(idx1);

end

gbest  = pbest(idx1,:);

% used with trainpso, for neural net training

% assign gbest to net at each iteration, these interim assignments

% are for plotting mostly

if strcmp(functname,'pso_neteval')

net=setx(net,gbest);

end

end  % end if outorng

sentryval = gbestval;

sentry    = gbest;

end % end if chkdyn

% find particles where we have new pbest, depending on minmax choice

% then find gbest and gbestval

%[size(out),size(pbestval)]

if rstflg == 0

if minmax == 0

[tempi]            = find(pbestval>=out); % new min pbestvals

pbestval(tempi,1)  = out(tempi);   % update pbestvals

pbest(tempi,:)     = pos(tempi,:); % update pbest positions

[iterbestval,idx1] = min(pbestval);

if gbestval >= iterbestval

gbestval = iterbestval;

gbest    = pbest(idx1,:);

% used with trainpso, for neural net training

% assign gbest to net at each iteration, these interim assignments

% are for plotting mostly

if strcmp(functname,'pso_neteval')

net=setx(net,gbest);

end

end

elseif minmax == 1

[tempi,dum]        = find(pbestval<=out); % new max pbestvals

pbestval(tempi,1)  = out(tempi,1); % update pbestvals

pbest(tempi,:)     = pos(tempi,:); % update pbest positions

[iterbestval,idx1] = max(pbestval);

if gbestval <= iterbestval

gbestval = iterbestval;

gbest    = pbest(idx1,:);

% used with trainpso, for neural net training

% assign gbest to net at each iteration, these interim assignments

% are for plotting mostly

if strcmp(functname,'pso_neteval')

net=setx(net,gbest);

end

end

elseif minmax == 2  % this won't work as it is, fix it later

egones            = errgoal*ones(ps,1); % vector of errgoals

sqrerr2           = ((pbestval-egones).^2);

sqrerr1           = ((out-egones).^2);

[tempi,dum]       = find(sqerr1 <= sqrerr2); % find particles closest to targ

pbestval(tempi,1) = out(tempi,1); % update pbestvals

pbest(tempi,:)    = pos(tempi,:); % update pbest positions

sqrerr            = ((pbestval-egones).^2); % need to do this to reflect new pbests

[temp,idx1]       = min(sqrerr);

iterbestval       = pbestval(idx1);

if (iterbestval-errgoal)^2 <= (gbestval-errgoal)^2

gbestval = iterbestval;

gbest    = pbest(idx1,:);

% used with trainpso, for neural net training

% assign gbest to net at each iteration, these interim assignments

% are for plotting mostly

if strcmp(functname,'pso_neteval')

net=setx(net,gbest);

end

end

end

end

%   % build a simple predictor 10th order, for gbest trajectory

%   if i>500

%    for dimcnt=1:D

%      pred_coef  = polyfit(i-250:i,(bestpos(i-250:i,dimcnt))',20);

%     % pred_coef  = polyfit(200:i,(bestpos(200:i,dimcnt))',20);

%      gbest_pred(i,dimcnt) = polyval(pred_coef,i+1);

%    end

%    else

%       gbest_pred(i,:) = zeros(size(gbest));

%    end

%gbest_pred(i,:)=gbest;

%assignin('base','gbest_pred',gbest_pred);

%   % convert to non-inertial frame

%    gbestoffset = gbest - gbest_pred(i,:);

%    gbest = gbest - gbestoffset;

%    pos   = pos + repmat(gbestoffset,ps,1);

%    pbest = pbest + repmat(gbestoffset,ps,1);

%PSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSO

% get new velocities, positions (this is the heart of the PSO algorithm)

% each epoch get new set of random numbers

rannum1 = rand([ps,D]); % for Trelea and Clerc types

rannum2 = rand([ps,D]);

if     trelea == 2

% from Trelea's paper, parameter set 2

vel = 0.729.*vel...                              % prev vel

+1.494.*rannum1.*(pbest-pos)...            % independent

+1.494.*rannum2.*(repmat(gbest,ps,1)-pos); % social

elseif trelea == 1

% from Trelea's paper, parameter set 1

vel = 0.600.*vel...                              % prev vel

+1.700.*rannum1.*(pbest-pos)...            % independent

+1.700.*rannum2.*(repmat(gbest,ps,1)-pos); % social

elseif trelea ==3

% Clerc's Type 1" PSO

vel = chi*(vel...                                % prev vel

+ac1.*rannum1.*(pbest-pos)...              % independent

+ac2.*rannum2.*(repmat(gbest,ps,1)-pos)) ; % social

else

% common PSO algo with inertia wt

% get inertia weight, just a linear funct w.r.t. epoch parameter iwe

if i<=iwe

iwt(i) = ((iw2-iw1)/(iwe-1))*(i-1)+iw1;

else

iwt(i) = iw2;

end

% random number including acceleration constants

ac11 = rannum1.*ac1;    % for common PSO w/inertia

ac22 = rannum2.*ac2;

vel = iwt(i).*vel...                             % prev vel

+ac11.*(pbest-pos)...                      % independent

+ac22.*(repmat(gbest,ps,1)-pos);           % social

end

% limit velocities here using masking

vel = ( (vel <= velmaskmin).*velmaskmin ) + ( (vel > velmaskmin).*vel );

vel = ( (vel >= velmaskmax).*velmaskmax ) + ( (vel < velmaskmax).*vel );

% update new position (PSO algo)

pos = pos + vel;

% position masking, limits positions to desired search space

% method: 0) no position limiting, 1) saturation at limit,

%         2) wraparound at limit , 3) bounce off limit

minposmask_throwaway = pos <= posmaskmin;  % these are psXD matrices

minposmask_keep      = pos >  posmaskmin;

maxposmask_throwaway = pos >= posmaskmax;

maxposmask_keep      = pos

if     posmaskmeth == 1

% this is the saturation method

pos = ( minposmask_throwaway.*posmaskmin ) + ( minposmask_keep.*pos );

pos = ( maxposmask_throwaway.*posmaskmax ) + ( maxposmask_keep.*pos );

elseif posmaskmeth == 2

% this is the wraparound method

pos = ( minposmask_throwaway.*posmaskmax ) + ( minposmask_keep.*pos );

pos = ( maxposmask_throwaway.*posmaskmin ) + ( maxposmask_keep.*pos );

elseif posmaskmeth == 3

% this is the bounce method, particles bounce off the boundaries with -vel

pos = ( minposmask_throwaway.*posmaskmin ) + ( minposmask_keep.*pos );

pos = ( maxposmask_throwaway.*posmaskmax ) + ( maxposmask_keep.*pos );

vel = (vel.*minposmask_keep) + (-vel.*minposmask_throwaway);

vel = (vel.*maxposmask_keep) + (-vel.*maxposmask_throwaway);

else

% no change, this is the original Eberhart, Kennedy method,

% it lets the particles grow beyond bounds if psoparams (P)

% especially Vmax, aren't set correctly, see the literature

end

%PSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSOPSO

% check for stopping criterion based on speed of convergence to desired

% error

tmp1 = abs(tr(i) - gbestval);

if tmp1 > ergrd

cnt2 = 0;

elseif tmp1 <= ergrd

cnt2 = cnt2+1;

if cnt2 >= ergrdep

if plotflg == 1

fprintf(message,i,gbestval);

disp(' ');

disp(['--> Solution likely, GBest hasn''t changed by at least ',...

num2str(ergrd),' for ',...

num2str(cnt2),' epochs.']);

eval(plotfcn);

end

break

end

end

% this stops if using constrained optimization and goal is reached

if ~isnan(errgoal)

if ((gbestval<=errgoal) & (minmax==0)) | ((gbestval>=errgoal) & (minmax==1))

if plotflg == 1

fprintf(message,i,gbestval);

disp(' ');

disp(['--> Error Goal reached, successful termination!']);

eval(plotfcn);

end

break

end

% this is stopping criterion for constrained from both sides

if minmax == 2

if ((tr(i)=errgoal)) | ((tr(i)>errgoal) ...

& (gbestval <= errgoal))

if plotflg == 1

fprintf(message,i,gbestval);

disp(' ');

disp(['--> Error Goal reached, successful termination!']);

eval(plotfcn);

end

break

end

end % end if minmax==2

end  % end ~isnan if

%    % convert back to inertial frame

%     pos = pos - repmat(gbestoffset,ps,1);

%     pbest = pbest - repmat(gbestoffset,ps,1);

%     gbest = gbest + gbestoffset;

end  % end epoch loop

### 回答1: SC算法(Synchronous Counting)、MINN算法(Modified Ideal Nearest Neighbor)、PARK算法(Parallel Randomized K-residue Algorithm)都是用于同步性能分析的一类算法。下面我将对这三个算法进行对比和分析。 首先,SC算法是一种时间同步算法,它通过在系统中引入一个全局时钟来实现设备间的同步。这种算法的优点是对系统中的每个设备都可以提供同步的精确性和一致性。然而,由于SC算法需要一个全局时钟,因此对于系统的可伸缩性和容错性存在一定的限制。 其次,MINN算法是一种基于邻居节点相距时间来实现同步的算法,它主要利用了节点之间相对距离的稳定性。它通过选择邻居节点中相距时间最近的节点作为参考来进行同步。这种算法的优点是简单、容易实现,并且不需要全局时钟。然而,相对于SC算法来说,MINN算法的同步精度会有一定的降低。 最后,PARK算法是一种基于随机选取邻居节点的算法,它通过选择邻居节点中随机的一个节点作为参考来进行同步。这种算法的优点是能够在一定程度上提高系统的可伸缩性和容错性。然而,由于是随机选择邻居节点,因此PARK算法的同步精度相对较低。 综上所述,三种算法各有其优缺点。如果对同步的精度要求较高,并且系统规模较小,可以选择SC算法;如果对同步精度要求适中,并且系统规模较大,可以选择MINN算法;如果对同步精度要求相对较低,并且系统规模非常大,可以选择PARK算法。当然,在实际应用中,还需考虑系统的具体需求和资源限制,选择合适的同步算法。 ### 回答2: SC算法(Selective Control)是一种分布式时钟同步算法,通过选择具有最小时间差异的参考时钟进行同步。MINN算法Minnimum Interval Navigation Network)是一种基于导航系统的分布式时钟同步算法,它使用导航数据来提高同步精度。PARK算法(Parallelized Reference-Synchronize Kernel)是一种并行化的参考时钟同步算法,它能够提高同步的性能和精度。 在进行MATLAB仿真分析时,我们可以比较这三种算法的同步性能包括同步精度、同步速度和系统负载。 首先,对于同步精度方面,SC算法MINN算法都能够实现较高的同步精度,因为它们都采用了选择最接近参考时钟的方法。PARK算法通过并行化处理能够提高同步的精度,但可能会因此牺牲一部分同步速度。 其次,对于同步速度方面,MINN算法由于使用导航数据,能够更快地收敛到同步状态,而SC算法和PARK算法则需要通过周期性的通信进行同步,速度相对较慢。 最后,对于系统负载方面,SC算法和PARK算法相对较轻,因为它们不需要额外的导航数据。而MINN算法需要不断接收和处理导航数据,可能会增加系统的负载。 综上所述,通过MATLAB仿真分析,可以发现不同算法在同步精度、同步速度和系统负载方面存在差异。在选择时钟同步算法时,可以根据应用的需求和系统的特点,选择合适的算法来进行同步。 ### 回答3: SC算法MINN算法和Park算法是用于同步性能分析的几种常见方法。 SC算法是最简单的同步性能分析方法,通过计算信号的周期,将同步性能指标表达为时间的函数,并得出最佳同步时间。 MINN算法是一种改进的同步性能分析方法,它利用FFT快速算法提取信号谐波分量,并根据谐波分量计算最佳同步时间。 Park算法是一种基于Park变换的同步性能分析方法,它将三相旋转坐标系下的信号变换到静止坐标系下,并通过计算旋转坐标系和静止坐标系中的误差角度来评估同步性能。 这三种方法在MATLAB仿真中的表现如下: 1. SC算法的仿真分析: SC算法MATLAB中的实现相对简单,通过对信号进行周期性分析,可以得到同步性能随时间变化的结果。该算法具有较低的计算复杂度和较好的实时性能。 2. MINN算法的仿真分析: MINN算法MATLAB中使用FFT算法提取信号的谐波分量,然后根据谐波分量计算最佳同步时间。该算法可以准确地评估同步精度,并具有较高的计算精度和较好的抗噪声性能。 3. Park算法的仿真分析: Park算法MATLAB中通过Park变换将信号转换到静止坐标系下,并计算旋转坐标系和静止坐标系中的误差角度。该算法可以准确地评估同步误差,并具有较高的稳定性和抗干扰性能。 综上所述,这三种算法MATLAB仿真中都能够进行同步性能分析,但各自有不同的特点和适用场景。根据具体的应用需求和实际情况,选择合适的算法进行同步性能分析是十分重要的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值