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

    function [out, grad] = staticEvaluate(obj, InputValues, ExprVals, GradVals, GradVars,D)
%STATICEVALUATE Evaluate the item at a set of inputs and expression values.
%
%   OUT = STATICEVALUATE(OBJ, INPUTVALUES, EXPRVALS) evaluates the
%   optimization item at a set of input values given the set of expression
%   output values EXPRVALS and returns outputs for each input set.
%   INPUTVALUES must be a cell array with the same number of elements as
%   the item has inputs.  Each cell contains a matrix of data for that
%   input.  EXPRVALS must be a cell array with the same number of elements
%   as there are expressions in the expression/function list provided by
%   the GETSTATICEXPRESSIONS method.  The output is a matrix of evaluation
%   values. These values should match the values that are produced by the
%   call EVALUATE(OBJ, INPUTVALUES).
%
%   [OUT, GRAD] = STATICEVALUATE(OBJ, INPUTS, EXPRVALS, GRADVALS, GRADVARS)
%   also calculates the gradient of the objective, given the gradients of
%   the expression values.  GRAVALS is a cell array the same size as
%   EXPRVALS that contains a 3D matrix for each 2D matrix of expression
%   evaluations in EXPRVALS.  The 3rd dimension contains the gradients with
%   respect to each free variable value.  GRADVARS is a vector of input
%   indices that indicates which inputs the expression gradients have been
%   calculated for.  GRAD should be a 3D matrix with the first two
%   dimensions the same size as OUT, and the 3rd dimension the same size as
%   the 3rd dimension of the expression gradients, i.e. a gradient should
%   be produced for each variable value GRADVARS specifies.
%   [OUT, GRAD] = STATICEVALUATE(OBJ, INPUTS, EXPRVALS, GRADVALS, GRADVARS,DATASET)
%   DATASET is a static data set object (cgstaticdataset) and includes any
%   interpolation required for use as an application point set.

%   Copyright 2005-2009 The MathWorks, Inc.

Sc = cellfun('size', InputValues, 2);
Sr = cellfun('size', InputValues, 1);
grad = [];
if isempty(ExprVals)
    out = zeros(max(Sr), 0);
    if nargin>3
        grad = zeros(max(Sr),0,sum(Sc(GradVars)));
    end
else
    out = ExprVals{1};
    wts = InputValues{1};
    [out, wts] = pCalculateSum(out, wts, Sc);
    if nargout>1
        grad = GradVals{1};
        if all(Sc(2:end) == 1) && Sc(1) > 1
            % Need to check for vector weights, scalar variables case.
            grad = grad(:, ones(1, Sc(1)), :);
        end
        grad = grad.*wts(:,:, ones(1, size(grad,3)));
        grad = sum(grad, 2);
    end

    if isOutputScaled(obj)
        % Scale the evaluation to be on [-1 1]
        Np = max(Sc);
        out = pEvaluateScale(obj, Np, 'sum', out, wts);
        if nargout>1
            grad = pEvaluateScale(obj, Np, 'gradsum', grad, wts);
        end
    end
end