www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimoutput/private/pFindDefaultConTol.m

    function Tol = pFindDefaultConTol(or)
%PFINDDEFAULTCONTOL Generate a suitable default constraint tolerance.
%
%   TOL = PFINDDEFAULTCONTOL(OR) creates an appropriate constraint
%   tolerance given the optimization problem that is encapsulated in the
%   optimrunner object OR.

%   Copyright 2006 The MathWorks, Inc.


% Default value to use if all else fails
Tol = 1e-6;

% Will get here if the empty cgoptimoutput constructor is called
if isempty(or)
    return
end

% Constraint tolerance is defined as a parameter for many algorithms so we
% first look in the parameters for likely candidates
P = getParameters(or);

params = {'Top_Level_Options';{P}};
Valid = false;

% Keep looping over options, recursing into xregoptmgr's
n = 1;
while n<=size(params,2)
    val = params{2,n}{1};
    if isa(val, 'xregoptmgr')
        % Append the parameters from this xregoptmgr to the end of the list
        % to look at
        params = [params, cellopts(val)];
        Valid(size(params,2)) = false;
    elseif isnumeric(val) && isscalar(val)  && val>=0
        % This parameter is the right type to be a constraint tolerance
        Valid(n) = true;
    end
    n = n+1;
end

% Look for "Constraint" or "Con" in a valid parameter.  Preference is given
% to some guesses that include Tol in the name.  TolCon is top of the list
% since this is used by Optimization Toolbox functions.  Matching is done
% at "MatchLevel 2": the full string must exist in a parameter.
Constraint_Names = {'TolCon', 'ConTol', 'ConstraintTol', 'Constraint', 'Con'};
ValidIdx = find(Valid);
MatchInd = mbcMatchNames(Constraint_Names, params(1, ValidIdx), 'MatchLevel', 2);

for n = 1:length(MatchInd)
    if MatchInd(n)>0
        Tol = params{2, ValidIdx(MatchInd(n))}{1};
        break
    end
end