www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimrunner/private/pCopyOptimItems.m
function ItemData = pCopyOptimItems(obj, optimitems, pAllInp, InLen, EvalTypes,DatasetIndices, RunCopy) %PCOPYOPTIMITEMS Copy optim item data to static storage % % ITEMDATA = PCOPYOPTIMITEMS(OBJ, OPTIMITEMS, ALLINPUTS, INLENGTHS, EVALTYPES) % makes copies of all of the cgoptimitem objects in the cell array % OPTIMITEMS in a way that makes them statically storable. A structure % of information is returned that has the following fields: % % Items : Cell array of statically-converted items. % InputIndices : The inputs that each item requires. % OutputLengths : The number of outputs from each item. % EvaluationData : Structure of data that is specific to each % specified EVALTYPE. This structure contains: % % EvaluationFunction : Function handle to staticXYZ method that % should be used in the optim item. % ExpressionStore : cgoptimexprgroup that contains static copies % of all the expressions for the items. % ExpressionDeps : Cell array of sparse matrices indicating % which input data affects each expression output. % ExpressionGradFcn : Empty. % % ITEMDATA = PCOPYOPTIMITEMS(..., false) makes a copy of the optim items % that only contains the data required for supporting the cgoptimoutput % object. % Copyright 2005-2009 The MathWorks, Inc. % Create an empty output data structure ItemData = pCreateItemDataStruct; % Get data that is shared between all eval types staticitems = optimitems; pInputs = cell(size(staticitems)); InputIdx = cell(size(staticitems)); OutputLen = zeros(size(staticitems)); for n = 1:numel(staticitems) pInputs{n} = getInputs(staticitems{n}); InputIdx{n} = findptrs(pInputs{n}, pAllInp); Len = InLen(InputIdx{n}); if DatasetIndices(n) % replace inputs with dataset length DS = obj.DataSetData(DatasetIndices(n)); Len(Len~=1)= size(DS.Data,1); staticitems{n} = convertToStatic(staticitems{n},DS); else staticitems{n} = convertToStatic(staticitems{n},[]); end OutputLen(n) = getNumOutputs(staticitems{n}, Len); end ItemData.Items = staticitems; ItemData.InputIndices = InputIdx; ItemData.OutputLengths = OutputLen; ItemData.Datasets = DatasetIndices; % Loop over eval types and create sub-structure for each one. The % expressions and the expression dependency matrices are collected into % single objects and the item offset index into these for each evaluation % type is saved. NumEvalTypes = length(EvalTypes); pExpr = cell(1,NumEvalTypes*length(staticitems)); Funcs = cell(1,NumEvalTypes*length(staticitems)); for k = 1:NumEvalTypes ItemOffset = (k-1)*length(staticitems); for n = 1:length(optimitems) [pExpr{ItemOffset+n}, Funcs{ItemOffset+n}] = ... getStaticExpressions(optimitems{n}, EvalTypes{k}); end SubData.EvaluationFunction = str2func(sprintf('static%s', EvalTypes{k})); SubData.ItemDataOffset = ItemOffset; SubData.ExpressionGradFcn = []; ItemData.EvaluationData.(EvalTypes{k}) = SubData; end if ~isempty(optimitems) staticgroup = cgoptimexprgroup(pExpr, Funcs, pAllInp); else staticgroup = cgoptimexprgroup({}, {}, mbcpointer(1,0)); end ItemData.ExpressionStore = staticgroup; % Create dependency matrices DepMatrix = {}; if numel(optimitems)>0 && RunCopy % Create a free variable dependency matrix for each group of % expressions. This is used for numjac calls. % Get the inputs needed for each output expression InputDep = getRequiredInputs(staticgroup); FreeVarInd = obj.FreeVariableIndices; NfreeVar = length(FreeVarInd); NFreeVarVals = sum(InLen(FreeVarInd)); % Form one big dependency matrix and then split into smaller ones for % each group NExpr = numOutputs(staticgroup); OutLen = zeros(1, NExpr); for n = 1:length(OutLen) OutLen(n) = max(InLen(InputDep{n})); end BigDep = zeros(sum(OutLen), NFreeVarVals); % Loop over the expressions and their inputs to decide how each % expression value depends on each free variable value pStart = 0; pEnd = 0; qStart = [1, cumsum(InLen(FreeVarInd))+1]; qEnd = cumsum(InLen(FreeVarInd)); for p = 1:NExpr pStart = pEnd + 1; pEnd = pStart + OutLen(p) -1; for q = 1:NfreeVar if any(FreeVarInd(q)==InputDep{p}) if InLen(FreeVarInd(q))==OutLen(p) % Vector input: each output depends on one of the % inputs BigDep(pStart:pEnd, qStart(q):qEnd(q)) = eye(OutLen(p)); else % Scalar input: every output depends on it BigDep(pStart:pEnd, qStart(q):qEnd(q)) = 1; end end end end % Split into groups DepMatrix = cell(size(pExpr)); pStart = 0; pEnd = 0; ExprStart = 0; ExprEnd = 0; ExprIdx = 0; for p = 1:length(DepMatrix) ExprStart = ExprEnd + 1; ExprEnd = ExprStart + length(pExpr{p}) - 1; pStart = pEnd + 1; pEnd = pStart + sum(OutLen(ExprStart:ExprEnd)) - 1; DepMatrix{p} = sparse(BigDep(pStart:pEnd, :)); end end ItemData.ExpressionDeps = DepMatrix;