reins matlab,遗传算法为什么要使用重插入reins?不使用可以吗?

function [Chrom, ObjVCh] = reins(Chrom, SelCh, SUBPOP, InsOpt, ObjVCh, ObjVSel);

% Check parameter consistency

if nargin < 2, error('Not enough input parameter'); end

if (nargout == 2 & nargin < 6), error('Input parameter missing: ObjVCh and/or ObjVSel'); end

[NindP, NvarP] = size(Chrom);

[NindO, NvarO] = size(SelCh);

if nargin == 2, SUBPOP = 1; end

if nargin > 2,

if isempty(SUBPOP), SUBPOP = 1;

elseif isnan(SUBPOP), SUBPOP = 1;

elseif length(SUBPOP) ~= 1, error('SUBPOP must be a scalar'); end

end

if (NindP/SUBPOP) ~= fix(NindP/SUBPOP), error('Chrom and SUBPOP disagree'); end

if (NindO/SUBPOP) ~= fix(NindO/SUBPOP), error('SelCh and SUBPOP disagree'); end

NIND = NindP/SUBPOP;  % Compute number of individuals per subpopulation

NSEL = NindO/SUBPOP;  % Compute number of offspring per subpopulation

IsObjVCh = 0; IsObjVSel = 0;

if nargin > 4,

[mO, nO] = size(ObjVCh);

if nO ~= 1, error('ObjVCh must be a column vector'); end

if NindP ~= mO, error('Chrom and ObjVCh disagree'); end

IsObjVCh = 1;

end

if nargin > 5,

[mO, nO] = size(ObjVSel);

if nO ~= 1, error('ObjVSel must be a column vector'); end

if NindO ~= mO, error('SelCh and ObjVSel disagree'); end

IsObjVSel = 1;

end

if nargin < 4, INSR = 1.0; Select = 0; end

if nargin >= 4,

if isempty(InsOpt), INSR = 1.0; Select = 0;

elseif isnan(InsOpt), INSR = 1.0; Select = 0;

else

INSR = NaN; Select = NaN;

if (length(InsOpt) > 2), error('Parameter InsOpt too long'); end

if (length(InsOpt) >= 1), Select = InsOpt(1); end

if (length(InsOpt) >= 2), INSR = InsOpt(2); end

if isnan(Select), Select = 0; end

if isnan(INSR), INSR =1.0; end

end

end

if (INSR < 0 | INSR > 1), error('Parameter for insertion rate must be a scalar in [0, 1]'); end

if (INSR < 1 & IsObjVSel ~= 1), error('For selection of offspring ObjVSel is needed'); end

if (Select ~= 0 & Select ~= 1), error('Parameter for selection method must be 0 or 1'); end

if (Select == 1 & IsObjVCh == 0), error('ObjVCh for fitness-based exchange needed'); end

if INSR == 0, return; end

NIns = min(max(floor(INSR*NSEL+.5),1),NIND);   % Number of offspring to insert

% perform insertion for each subpopulation

for irun = 1:SUBPOP,

% Calculate positions in old subpopulation, where offspring are inserted

if Select == 1,    % fitness-based reinsertion

[Dummy, ChIx] = sort(-ObjVCh((irun-1)*NIND+1:irun*NIND));

else               % uniform reinsertion

[Dummy, ChIx] = sort(rand(NIND,1));

end

PopIx = ChIx((1:NIns)')+ (irun-1)*NIND;

% Calculate position of Nins-% best offspring

if (NIns < NSEL),  % select best offspring

[Dummy,OffIx] = sort(ObjVSel((irun-1)*NSEL+1:irun*NSEL));

else

OffIx = (1:NIns)';

end

SelIx = OffIx((1:NIns)')+(irun-1)*NSEL;

% Insert offspring in subpopulation -> new subpopulation

Chrom(PopIx,:) = SelCh(SelIx,:);

if (IsObjVCh == 1 & IsObjVSel == 1), ObjVCh(PopIx) = ObjVSel(SelIx); end

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值