声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1666|回复: 3

[工具箱] 发现工具箱一源程序有错,改正后请高手审批

[复制链接]
发表于 2006-9-14 10:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
本人在用遗传算法工具箱中的crtrp函数时发现错误,经调试改正,已通过运行。现告知大家,并请专家给与审批!
crtrp源程序如下:
% CRTRP.M        (CReaTe an initial (Real-value) Population)
%
% This function creates a population of given size of random real-values.
%
% Syntax:       Chrom = crtrp(Nind,FieldDR);
%
% Input parameters:
%    Nind      - A scalar containing the number of individuals in the new
%                population.
%
%    FieldDR   - A matrix of size 2 by number of variables describing the
%                boundaries of each variable. It has the following structure:
%                [lower_bound;   (vector with lower bound for each veriable)
%                 upper_bound]   (vector with upper bound for each veriable)
%                [lower_bound_var_1  lower_bound_var_2 ... lower_bound_var_Nvar;
%                 upper_bound_var_1  upper_bound_var_2 ... upper_bound_var_Nvar]
%                example - each individuals consists of 4 variables:
%                FieldDR = [-100 -50 -30 -20;   % lower bound
%                            100  50  30  20]   % upper bound
%              
% Output parameter:
%    Chrom     - A matrix containing the random valued individuals of the
%                new population of size Nind by number of variables.

% Author:     Hartmut Pohlheim
% History:    23.11.93     file created
%             25.02.94     clean up, check parameter consistency


function Chrom = crtrp(Nind,FieldDR);

% Check parameter consistency
   if nargin < 2, error('parameter FieldDR missing'); end
   if nargin > 2, nargin = 2; end

   [mN, nN] = size(Nind);
   [mF, Nvar] = size(FieldDR);

   if (mN ~= 1 & nN ~= 1), error('Nind has to be a scalar'); end
   if mF ~= 2, error('FieldDR must be a matrix with 2 rows'); end

% Compute Matrix with Range of variables and Matrix with Lower value
   Range = rep((FieldDR(2,:)-FieldDR(1,:)),[Nind 1]);
   Lower = rep(FieldDR(1,:), [Nind 1]);

% Create initial population
% Each row contains one individual, the values of each variable uniformly
% distributed between lower and upper bound (given by FieldDR)
   Chrom = rand(Nind,Nvar) .* Range + Lower;


% End of function
在运行时一直出现这样的错误提示:(提示在“ if nargin < 2, error('parameter FieldDR missing'); end
这一行”)
"nargin" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable.
A possible cause of this error is that you forgot to initialize the
variable, or you have initialized it implicitly using load or eval.
本人改正如下:
% CRTRP.M        (CReaTe an initial (Real-value) Population)
%
% This function creates a population of given size of random real-values.
%
% Syntax:       Chrom = crtrp(Nind,FieldDR);
%
% Input parameters:
%    Nind      - A scalar containing the number of individuals in the new
%                population.
%
%    FieldDR   - A matrix of size 2 by number of variables describing the
%                boundaries of each variable. It has the following structure:
%                [lower_bound;   (vector with lower bound for each veriable)
%                 upper_bound]   (vector with upper bound for each veriable)
%                [lower_bound_var_1  lower_bound_var_2 ... lower_bound_var_Nvar;
%                 upper_bound_var_1  upper_bound_var_2 ... upper_bound_var_Nvar]
%                example - each individuals consists of 4 variables:
%                FieldDR = [-100 -50 -30 -20;   % lower bound
%                            100  50  30  20]   % upper bound
%              
% Output parameter:
%    Chrom     - A matrix containing the random valued individuals of the
%                new population of size Nind by number of variables.

% Author:     Hartmut Pohlheim
% History:    23.11.93     file created
%             25.02.94     clean up, check parameter consistency


function Chrom = crtrp(Nind,FieldDR);

% Check parameter consistency
   [mN, nN] = size(Nind);
   [mF, Nvar] = size(FieldDR);
   if (nN+Nvar) < 2, error('parameter FieldDR missing'); end
   if (nN+Nvar) >= 2, end
   if (mN ~= 1 & nN ~= 1), error('Nind has to be a scalar'); end
   if mF ~= 2, error('FieldDR must be a matrix with 2 rows'); end

% Compute Matrix with Range of variables and Matrix with Lower value
   Range = rep((FieldDR(2,:)-FieldDR(1,:)),[Nind 1]);
   Lower = rep(FieldDR(1,:), [Nind 1]);

% Create initial population
% Each row contains one individual, the values of each variable uniformly
% distributed between lower and upper bound (given by FieldDR)
   Chrom = rand(Nind,Nvar) .* Range + Lower;


% End of function
程序通过运行。
请专家审批,如正确,则希望能改正工具箱中的这一程序。以让使用者收益!

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2006-9-14 17:23 | 显示全部楼层
原来的程序没什么问题
发表于 2006-10-14 15:29 | 显示全部楼层
% Check parameter consistency
   if nargin < 2, error('parameter FieldDR missing'); end
   if nargin > 2, nargin = 2; end

我的也和楼主样的,用起来就报错。
我看书上说nargin是输入函数,不可以再赋值。

不知道LS的说没问题是不是真的?
发表于 2006-10-14 20:26 | 显示全部楼层
牛人是
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-25 05:22 , Processed in 0.056900 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表