function [pop] = initializega(num, bounds, evalFN,evalOps,options)
% function [pop]=initializega(populationSize, variableBounds,evalFN,
% evalOps,options)
% initializega creates a matrix of random numbers with
% a number of rows equal to the populationSize and a number
% columns equal to the number of rows in bounds plus 1 for
% the f(x) value which is found by applying the evalFN.
% This is used by the ga to create the population if it
% is not supplied.
%
% pop - the initial, evaluated, random population
% populatoinSize - the size of the population, i.e. the number to create
% variableBounds - a matrix which contains the bounds of each variable, i.e.
% [var1_high var1_low; var2_high var2_low; ....]
% evalFN - the evaluation fn, usually the name of the .m file for
% evaluation
% evalOps - any options to be passed to the eval function defaults []
% options - options to the initialize function, ie.
% [type prec] where eps is the epsilon value
% and the second option is 1 for float and 0 for binary,
% prec is the precision of the variables defaults [1e-6 1]
% Binary and Real-Valued Simulation Evolution for Matlab GAOT V2
% Copyright (C) 1998 C.R. Houck, J.A. Joines, M.G. Kay
if nargin<5
options=[1e-6 1];
end
if nargin<4
evalOps=[];
end
if any(evalFN<48) % Not a .m file
if options(2)==1 % Float GA
estr=['x=pop(i,1); pop(i,xZomeLength)=', evalFN ';'];
else % Binary GA
estr=['x=b2f(pop(i,:),bounds,bits); pop(i,xZomeLength)=', evalFN ';'];
end
else % A .m file
if options(2)==1 % Float GA
estr=['[ pop(i,:) pop(i,xZomeLength)]=' evalFN '(pop(i,:),[0 evalOps]);']
else % Binary GA
estr=['x=b2f(pop(i,:),bounds,bits);[x v]=' evalFN ...
'(x,[0 evalOps]); pop(i,:)=[f2b(x,bounds,bits) v];']
end
end
numVars = size(bounds,1); % Number of variables, size(bounds,1)return the number of 返回矩阵bounds的行数
rng = (bounds(:,2)-bounds(:,1))'; % The variable ranges'
if options(2)==1 % Float GA
xZomeLength = numVars+1; % Length of string is numVar + fit 染色体长度 = 变量数 + 适应度变量
pop = zeros(num,xZomeLength); % Allocate the new population
pop(:,1:numVars)=(ones(num,numVars)*rng).*(rand(num,numVars))+...
(ones(num,1)*bounds(:,1)');
else % Binary GA
bits=calcbits(bounds,options(1));
xZomeLength = sum(bits)+1; % Length of string is numVar + fit
pop = round(rand(num,sum(bits)+1));
end
for i=1:num
eval(estr)
end
很急啊
matlab中if nargin<5 options=[1e-6 1];end if nargin<4 evalOps=[];什么意思?还有这个代码作用?
答案:4 悬赏:0 手机版
解决时间 2021-01-29 12:24
- 提问者网友:半生酒醒
- 2021-01-28 21:52
最佳答案
- 五星知识达人网友:思契十里
- 2021-01-28 22:48
把这几句注释下:
if nargin<5 %如果输入参数少于5个时
options=[1e-6 1];%赋值
end %条件判断结束
if nargin<4 %如果输入参数少于4个时
evalOps=[];%赋空值
另外,这整段程序是用于初始化遗传算法的,就是遗传算法优化程序的预处理子程序,为后面的主程序打基础。
全部回答
- 1楼网友:怙棘
- 2021-01-29 01:52
你好!
nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能。通常可以用他来设定一些默认值,如你上面的函数。
当nargin=5时,options=[10^(-6) 1]
当nargin=4时,evalOps=[];
如果对你有帮助,望采纳。
- 2楼网友:一袍清酒付
- 2021-01-29 00:34
这段代码是用来初始化遗传算法的种群
- 3楼网友:污到你湿
- 2021-01-29 00:19
nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能。通常可以用他来设定一些默认值,如你上面的函数。
当nargin=5时,options=[10^(-6) 1]
当nargin=4时,evalOps=[];
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯