% REINS.M (RE-INSertion of offspring in population replacing parents)
%
% This function reinserts offspring in the population.
%
% Syntax: [Chrom, ObjVCh] = reins(Chrom, SelCh, SUBPOP, InsOpt, ObjVCh, ObjVSel)
%
% Input parameters:
% Chrom - Matrix containing the individuals (parents) of the current
% population. Each row corresponds to one individual.
% SelCh - Matrix containing the offspring of the current
% population. Each row corresponds to one individual.
% SUBPOP - (optional) Number of subpopulations
% if omitted or NaN, 1 subpopulation is assumed
% InsOpt - (optional) Vector containing the insertion method parameters
% ExOpt(1): Select - number indicating kind of insertion
% 0 - uniform insertion
% 1 - fitness-based insertion
% if omitted or NaN, 0 is assumed
% ExOpt(2): INSR - Rate of offspring to be inserted per
% subpopulation (% of subpopulation)
% if omitted or NaN, 1.0 (100%) is assumed
% ObjVCh - (optional) Column vector containing the objective values
% of the individuals (parents - Chrom) in the current
% population, needed for fitness-based insertion
% saves recalculation of objective values for population
% ObjVSel - (optional) Column vector containing the objective values
% of the offspring (SelCh) in the current population, needed for
% partial insertion of offspring,
% saves recalculation of objective values for population
%
% Output parameters:
% Chrom - Matrix containing the individuals of the current
% population after reinsertion.
% ObjVCh - if ObjVCh and ObjVSel are input parameter, than column
% vector containing the objective values of the individuals
% of the current generation after reinsertion.
% Author: Hartmut Pohlheim
% History: 10.03.94 file created
% 19.03.94 parameter checking improved
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
% End of function
没有合适的资源?快使用搜索试试~ 我知道了~
遗传算法(GA)解决数据拟合和拟合方程中未知参数代码
共19个文件
m:13个
mat:4个
xlsx:2个
2 下载量 191 浏览量
2024-02-11
20:51:56
上传
评论
收藏 24KB RAR 举报
温馨提示
本代码为遗传算法求解模型未知参数的程序,在建立模型的过程中,模型的待定系数通常是未知的,而通过数据拟合出来的模型无法求解,遗传算法通过以下步骤: (1)初始化:设置进化代数计数器t=0,设置最大进化代数T,随机生成M个个体作为初始群体。 (2)个体评价:计算群体P(t)中各个个体的适应度。 (3)选择运算:将选择算子作用于群体。选择的目的是把优化的个体直接遗传到下一代或通过配对交叉产生新的个体再遗传到下一代。选择操作是建立在群体中个体的适应度评估基础上的。 (4)交叉运算:将交叉算子作用于群体。遗传算法中起核心作用的就是交叉算子。 (5)变异运算:将变异算子作用于群体。即是对群体中的个体串的某些基因座上的基因值作变动。群体P(t)经过选择、交叉、变异运算之后得到下一代群体P(t+1)。 (6)终止条件判断:若t=T,则以进化过程中所得到的具有最大适应度个体作为最优解输出,终止计算。 对未知参数进行求解,当最优个体的适应度达到给定的阈值,或者最优个体的适应度和群体适应度不再上升时,或者迭代次数达到预设的代数时,算法终止。预设的代数一般设置为100-500代。
资源推荐
资源详情
资源评论
收起资源包目录
遗传代码.rar (19个子文件)
遗传代码
代码医学
ObjV1.m 292B
select.m 2KB
crtbp.m 2KB
y.mat 184B
bs2rv.m 3KB
xovmp.m 3KB
mut.m 2KB
main.m 5KB
ranking.m 5KB
sus.m 1KB
crtbase.m 1KB
x.mat 252B
recombin.m 2KB
医学数据.xlsx 10KB
reins.m 5KB
x1.mat 252B
y1.mat 252B
~$数据.xlsx 165B
xovsp.m 1KB
共 19 条
- 1
资源评论
算法小新.
- 粉丝: 7
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功