www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptim/setConstraintEnabled.m

    function optim = setConstraintEnabled(optim, label, enstate)
%SETCONSTRAINTENABLED Set whether constraints are enabled or not.
%
%   OPTIM = SETCONSTRAINTENABLED(OPTIM, LABEL, ENSTATE) toggles constraints
%   from being enabled to disabled.  Disabling constraints behaves very
%   similarly to deleting them, except the constraint object is kept in
%   existence for future reinstatement.
%
%   OPTIM = SETCONSTRAINTENABLED(OPTIM, ENSTATE) sets the enable flags for
%   all of the constraints.

%   Copyright 2006 The MathWorks, Inc.


if nargin==3
    if ischar(label) || iscellstr(label)
        ind = getConstraintIndex(optim.OptimSetup, label);
    elseif isnumeric(label) && label>0 && label<=numConstraints(optim.OptimSetup)
        ind = label;
    else
        ind = 0;
    end
    if ind==0
        error(message('mbc:cgoptim:InvalidLabel'));
    end
else
    if numel(label)~=numel(optim.Constraints)
        error(message('mbc:cgoptim:InvalidArgument18'));
    end
    enstate = label;
    ind = 1:numel(optim.Constraints);
end

% Check whether the operation will change the number of enabled
% constraints, and whether this will be allowed
Nchange = sum(enstate) - sum(optim.ConstraintEnabled(ind));
if Nchange<0 && ~canRemoveConstraint(optim.OptimSetup)
    error(message('mbc:cgoptim:InvalidState16'));
elseif Nchange>0 && ~canAddConstraint(optim.OptimSetup)
    error(message('mbc:cgoptim:InvalidState'));
end

optim.ConstraintEnabled(ind) = enstate;