📄 psoptimset.m
字号:
varargin{1} = options;
varargin{2} = optionsfcn;
numberargs = 2;
end
Names = fieldnames(options);
m = size(Names,1);
names = lower(Names);
i = 1;
while i <= numberargs
arg = varargin{i};
if ischar(arg) % arg is an option name
break;
end
if ~isempty(arg) % [] is a valid options argument
if ~isa(arg,'struct')
error('gads:PSOPTIMSET:invalidArgument',['Expected argument %d to be a string parameter name ' ...
'or an options structure\ncreated with PSOPTIMSET.'], i);
end
for j = 1:m
if any(strcmp(fieldnames(arg),Names{j,:}))
val = arg.(Names{j,:});
else
val = [];
end
if ~isempty(val)
if ischar(val)
val = lower(deblank(val));
end
[valid, errmsg] = checkfield(Names{j,:},val);
if valid
options.(Names{j,:}) = val;
else
error('gads:PSOPTIMSET:invalidOptionField',errmsg);
end
end
end
end
i = i + 1;
end
% A finite state machine to parse name-value pairs.
if rem(numberargs-i+1,2) ~= 0
error('gads:PSOPTIMSET:invalidArgPair','Arguments must occur in name-value pairs.');
end
expectval = 0; % start expecting a name, not a value
while i <= numberargs
arg = varargin{i};
if ~expectval
if ~ischar(arg)
error('gads:PSOPTIMSET:invalidArgFormat','Expected argument %d to be a string parameter name.', i);
end
lowArg = lower(arg);
j = strmatch(lowArg,names);
if isempty(j) % if no matches
error('gads:PSOPTIMSET:invalidParamName','Unrecognized parameter name ''%s''.', arg);
elseif length(j) > 1 % if more than one match
% Check for any exact matches (in case any names are subsets of others)
k = strmatch(lowArg,names,'exact');
if length(k) == 1
j = k;
else
msg = sprintf('Ambiguous parameter name ''%s'' ', arg);
msg = [msg '(' Names{j(1),:}];
for k = j(2:length(j))'
msg = [msg ', ' Names{k,:}];
end
msg = sprintf('%s).', msg);
error('gads:PSOPTIMSET:AmbiguousParamName',msg);
end
end
expectval = 1; % we expect a value next
else
if ischar(arg)
arg = lower(deblank(arg));
end
[valid, errmsg] = checkfield(Names{j,:},arg);
if valid
options.(Names{j,:}) = arg;
else
error('gads:PSOPTIMSET:invalidParamVal',errmsg);
end
expectval = 0;
end
i = i + 1;
end
if expectval
error('gads:PSOPTIMSET:invalidParamVal','Expected value for parameter ''%s''.', arg);
end
%-------------------------------------------------
function [valid, errmsg] = checkfield(field,value)
%CHECKFIELD Check validity of structure field contents.
% [VALID, MSG] = CHECKFIELD('field',V) checks the contents of the specified
% value V to be valid for the field 'field'.
%
valid = 1;
errmsg = '';
% empty matrix is always valid
if isempty(value)
return
end
switch field
case {'SearchMethod'}
if iscell(value) || isa(value,'function_handle')
valid=1;
elseif isa(value,'char') && any(strcmp(value,{'positivebasisnp1','positivebasis2n','searchlhs','searchga','searchneldermead'}))
valid = 1;
else
valid = 0;
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be ''@PositiveBasisNp1'',''@PositiveBasis2N'',''@searchlhs'',\n''@searchga'',''@searchneldermead'' or a function_handle.',field);
end
case {'PollMethod'}
if ~isa(value,'char') || ~any(strcmp(value,{'positivebasisnp1', 'positivebasis2n'}))
valid=0;
if length(strmatch(value,{'positivebasisnp1','positivebasis2n'})) >1
valid =0;
errmsg = sprintf('Ambiguous value for OPTIONS parameter %s: No match found''.',field);
else
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be ''PositiveBasisNp1'', or , ''PositiveBasis2N''.',field);
end
end
case {'PollingOrder'}
if ~isa(value,'char') || ~any(strcmp(value,{'random','success','consecutive'}))
valid=0;
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be ''Random'',''Success'', or ,''Consecutive''.',field);
end
case {'Display'}
if ~isa(value,'char') || ~any(strcmpi(value,{'off','iter','none', ...
'diagnose','final'}))
valid=0;
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be ''off'',''iter'', ''diagnose'',''final'', or ,''none''.',field);
end
case {'OutputFcns','PlotFcns'}
if iscell(value) || isa(value,'function_handle')
valid = 1;
else
valid = 0;
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be a function handle or cell array of function handles.',field);
end
case {'InitialMeshSize','MeshContraction','MeshExpansion','MaxMeshSize', ...
'TolMesh','TolBind','TolX','TolFun','CacheTol','CacheSize'}
if ~(isa(value,'double') && value > 0)
valid = 0;
if ischar(value)
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be a real positive scalar (not a string).',field);
else
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be a real positive scalar.',field);
end
end
case {'MaxIteration'} % integer including inf or default string
if ~(isa(value,'double') && value >= 0) ...
&& ~isequal(value, '100*numberofvariables') ...
&& ~isequal(value, '200*numberofvariables')
valid = 0;
if ischar(value)
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be a positive numeric (not a string).',field);
else
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be a positive numeric.',field);
end
end
case {'MaxFunEvals'} % integer including inf or default string
if ~(isa(value,'double') && value >= 0) ...
&& ~isequal(value,'1000*numberofvariables') ...
&& ~isequal(value, '2000*numberofvariables')
valid = 0;
if ischar(value)
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be a positive numeric (not a string).',field);
else
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be a positive numeric.',field);
end
end
case {'ScaleMesh','CompletePoll','CompleteSearch','MeshAccelerator','MeshRotate','Vectorized','Cache'} %off, on
if ~isa(value,'char') || ~any(strcmp(value,{'on','off'}))
valid = 0;
errmsg = sprintf('Invalid value for OPTIONS parameter %s: must be ''off'' or ''on''.',field);
end
case 'PlotInterval'
valid = isnumeric(value) && (1 == length(value)) && (value > 0) && (value == floor(value));
if(~valid)
errmsg = sprintf('The field ''%s'' must be a positive integer.',field);
end
otherwise
error('gads:PSOPTIMSET:unknownOptionsField','Unknown field name for Options structure.')
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -