www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@xregdesign/EvalConstraints.m

    function d = EvalConstraints(d,checkUpdateRequired)
%EVALCONSTRAINTS Evaluate constraints for design over candidate set 
%  D=EVALCONSTRAINTS(D) evaluates the constraints over the candidate set
%  and store results in object
%   D=EVALCONSTRAINTS(D,true) only evaluates the constraints if candidate
%   set and constraints are out-of-sync.

%  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.

if nargin>1 && checkUpdateRequired
    % evaluate constraints if candidate set and constraints are
    % out-of-sync.
    updateConstraints = d.candstate~=d.constraintsflag;
else
    % always update constraints
    updateConstraints = true;
end

if updateConstraints && ~isempty(d.constraints) && isoptimcapable(d)
    % EvalConstraints only required for optimal designs
    
    % size of candidate set - 
    c = reset(d.constraints);
    nc= ncand(d,'unconstrained');
    if isfinite(nc)
        % number of points to process at one time
        n= 1e6;
        % number of sections in full candidate set
        nd= fix(nc/n);
        ind= 1:n;
        
        usewait=waitbars(d) & (nd>1);
        if usewait
            % prevent any nested waitbars
            d=waitbars(d,0);
            % gui feedback
            h=xregGui.waitdlg('title','MBC Toolbox', ...
                'message',sprintf('Evaluating constraints (%5.3g elements). ',nc));
            h.waitbar.max=nd+1;
        else
            h=[];
        end
        
        if nc>1e8 && mbcfoundation.DCTManager.isParpoolOpen()
            in = iEvalinParallel(d,h,nc,nd,n,ind);
        else
            in = iEvalInSeries(d,nd,n,ind,usewait,h);
        end
        st = nd*n;
        
        Xc= indexcand(d,nd*n+1:nc,'unconstrained');
        in{end} = iEval(d,Xc,st);
        % check size of constraints
        if nd>0
            tc= sum(cellfun('prodofsize',in));
            [~,ms]=computer;
            if tc>ms
                error(message('mbc:xregdesign:TooManyFeasiblePoints'))
            end
            % concatenate interior points
            d.constraints = interiorPoints(c,cat(1,in{:}));
        else
            d.constraints = interiorPoints(c,in{1});
        end
        
        if usewait
            h.waitbar.value=nd+1;
        end
        if usewait
            delete(h);
            % turn waitbars back on
            d=waitbars(d,1);
        end
    else
        error(message('mbc:xregdesign:InvalidState'))
    end
    
    % set candstate and constraintsflag to same state
    d.candstate=d.candstate+1;
    d.constraintsflag=d.candstate;
end

end


function out = iEval(d, Xc, st )
% Use natural values for points (not coded)
Xc = invcode( d.model , Xc );
% find feasible points
[~, out] = isInside( d.constraints , Xc, st );
% store indices 
out = st+find(out);
end

function updateWait(h,i)
h.waitbar.value=i;
end

function in = iEvalinParallel(d,h,nc,nd,n,ind)
% use parfor to evaluate in parallel
in = cell(1,nd+1);
if waitbars(d)
    h.Message = sprintf('Evaluating constraints (%5.3g elements) in parallel. ',nc);
end
parfor i=1:nd
    st = (i-1)*n;
    Xc= indexcand(d,ind+st,'unconstrained');
    in{i} = iEval(d,Xc,st);
end
end

function in = iEvalInSeries(d,nd,n,ind,usewait,h)
in = cell(1,nd+1);
for i=1:nd
    % index to starting point for current section
    st = (i-1)*n;
    % get candidate set points for current section
    Xc= indexcand(d,ind+st,'unconstrained');
    % evaluate constraint for current section and store
    in{i} = iEval(d ,Xc,st);
    if usewait
        updateWait(h,i)
    end
end
end