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

    function [Y, XSweep, DepMatrix] = evaluateSweep(obj, X, itemType, itemNames, varargin)
%EVALUATESWEEP Evaluate items over sweeps of the free values.
%
%   Y = EVALUATESWEEP(OBJ, X, ITEMTYPE, ITEMNAMES) evaluates the specified
%   items as each of the free values is swept across its optimization
%   range.  The item outputs are returned as a cell array of evaluations.
%   Each cell corresponds to one of the free values being swept across its
%   range.
%
%   Y = EVALUATESWEEP(..., PROP, VALUE, ...) allows specification of
%   additional arguments.  Valiid properties and their values are given in
%   the table below.
%
%      PROPERTY          |  DESCRIPTION
%      ------------------+----------------------------------------------
%      EvaluationType    | Specifies the quantity to evaluate.  The
%                        | available quantities will differ depending on
%                        | how the cgoptimrunner was constructed.
%      FixedValueRun     | Specify which run the fixed values should be
%                        | taken from.
%      NumSweepPoints    | Number of points to evaluate for each sweep.
%      SweepValues       | Indices of the values that should be swept.
%                        | These are indices into the expanded list of all
%                        | free values, not indices into the list of free
%                        | variables.
%      SweepRanges       | Ranges that each sweep value should be swept
%                        | over.  A range must be provided for each value
%                        | being swept.
%      InsertEvalPoint   | Specifies whether to force one of the sweep
%                        | point values to be exactly the free value
%                        | specified in X.  the default setting for this is
%                        | true.
%
%   [Y, X2] = EVALUATESWEEP(...) also returns a cell array the same size as
%   Y that contains the values that each variable was set to as it was
%   swept.
%
%   [Y, X2, DEPS] = EVALUATESWEEP(...) also returns 2-D logical array of
%   size (NumOutVals-by-NumFreeVals) that indicates which free values each
%   output depends on and which have no effect on its output.

%   Copyright 2005-2012 The MathWorks, Inc.

[~, nVals] = numFreeValues(obj);

% Parse optional arguments
FixedValRun = [];
EvalType = obj.DefaultEvaluationType;
NumSweepPts = 101;
SweepVals = [];
SweepRng = [];
InsertEvalPoint = true;
for n = 1:2:length(varargin)
    switch lower(varargin{n})
        case 'evaluationtype'
            EvalType = varargin{n+1};
        case 'fixedvaluerun'
            FixedValRun = varargin{n+1};   
        case 'numsweeppoints'
            NumSweepPts = varargin{n+1};
        case 'sweepvalues'
            SweepVals = varargin{n+1};
        case 'sweepranges'
            SweepRng = varargin{n+1};
        case 'insertevalpoint'
            InsertEvalPoint = varargin{n+1};
    end
end

if isempty(FixedValRun)
    FixedValRun = obj.RunIndices(obj.CurrentRun);
end

if isempty(SweepVals)
    SweepVals = 1:nVals;
end

if isempty(SweepRng)
    [lb, ub] = getFreeVariableRanges(obj,FixedValRun);
    SweepRng = [lb(SweepVals); ub(SweepVals)];
elseif size(SweepRng, 2)~=length(SweepVals)
    error(message('mbc:cgoptimrunner:InvalidArgument1'));
end


XSweep = cell(1, length(SweepVals));
Y = cell(1, length(SweepVals));

if NumSweepPts*length(SweepVals)*length(X)>1e6
    % Loop over sweep variables (sweep each one separately) to avoid taking
    % up all the memory
    
    switch lower(itemType)
        case 'objective'
            ItemInd = getObjectiveIndex(obj.Setup, itemNames);
            ItemData = pGetItemDataFor(obj.Objectives, EvalType);
        case 'constraint'
            ItemInd = getConstraintIndex(obj.Setup, itemNames);
            ItemData = pGetItemDataFor(obj.Constraints, EvalType);
    end
    
    XFree = repmat(X, NumSweepPts, 1);
    for n = 1:length(SweepVals)
        if n>1
            % Reset variable from last sweep
            XFree(:, SweepVals(n-1)) = X(SweepVals(n-1));
        end

        XSweep{n} = linspace(SweepRng(1, n), SweepRng(2, n), NumSweepPts);
        if InsertEvalPoint
            % Replace one of the regular gridded points with the evaluation
            % value for this variable
            XSweep{n} = i_replacenearest(XSweep{n}, X(SweepVals(n)));
        end
        XFree(:, SweepVals(n)) = XSweep{n};

        % Perform evaluation of items
        Y{n} = pEvaluateItems(obj, ItemData, XFree, ItemInd, 'runs', FixedValRun);
    end

else
    % Evaluate all sweeps at once
    
    % Make a big free variable matrix for the evaluation
    XFree = repmat(X, NumSweepPts*length(SweepVals), 1);

    % Insert sweep values for each sweep variable
    SweepIdx = 1:NumSweepPts;
    for n = 1:length(SweepVals)
        XSweep{n} = linspace(SweepRng(1, n), SweepRng(2, n), NumSweepPts);
        if InsertEvalPoint
            % Replace one of the regular gridded points with the evaluation
            % value for this variable
            XSweep{n} = i_replacenearest(XSweep{n}, X(SweepVals(n)));
        end
        XFree(SweepIdx, SweepVals(n)) = XSweep{n};
        SweepIdx = SweepIdx+NumSweepPts;
    end

    % Perform evaluation of items
    switch lower(itemType)
        case 'objective'
            ItemInd = getObjectiveIndex(obj.Setup, itemNames);
            ItemData = pGetItemDataFor(obj.Objectives, EvalType);
        case 'constraint'
            ItemInd = getConstraintIndex(obj.Setup, itemNames);
            ItemData = pGetItemDataFor(obj.Constraints, EvalType);
    end
    
    ItemData.ExpressionStore = setCompressInputData(ItemData.ExpressionStore, true);
    YAll = pEvaluateItems(obj, ItemData, XFree, ItemInd, 'runs', FixedValRun);
    ItemData.ExpressionStore = setCompressInputData(ItemData.ExpressionStore, false);

    % Split values into a cell for each sweep variable
    SweepIdx = 1:NumSweepPts;
    for n = 1:length(SweepVals)
        Y{n} = YAll(SweepIdx, :);
        SweepIdx = SweepIdx+NumSweepPts;
    end
end

% Create a mini-dependency chart.  We cannot use any of the saved
% dependency data as that might not be available in the output node version
% of the object.
if nargout>2
    DepMatrix = getDependencyMatrix(obj, itemType, itemNames);
end



% Function that replaces one of the values in a vector with a new value
function X = i_replacenearest(X, newVal)

if ~isempty(X)
    [~, idx] = min(abs(X-newVal));
    X(idx) = newVal;
end