www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgoptimnode/createPointOptimization.m
function pointNd = createPointOptimization(nd) %CREATEPOINTOPTIMIZATION Create a point optimization from a sum % % POINTNODE = CREATEPOINTOPTIMIZATION(SUMNODE) creates a new point % optimization from a sum optimization. All objectives are converted to % point objectives. Any sum constraints (currently table gradient and % sum) are deleted. If the sum optimization contains a single run % output, then the initial values for the point optimization are taken % from the output node. % % See also CREATESUMOPTIMIZATION % Copyright 2007-2011 The MathWorks, Inc. % Sum optimization pSumOptim = getdata(nd); % Do not allow if there is more than one run nRuns = getNumRuns(pSumOptim.info); if nRuns > 1 error(message('mbc:cgoptimnode:InvalidState')); end % Do not allow if the variables are not of the same length or scalar. Data = getinitialvaluedata(pSumOptim.info); DataSize = cellfun(@(x) size(x,2), Data); maxSize = max(DataSize); if ~all(DataSize == maxSize | DataSize == 1) % This method can only convert a point optimization error('mbc:cgoptimnode:InvalidState', ... 'A sum optimization can only be created from an optimization containing scalar input variables only.'); end % Duplicate supplied optimization and give it a sensible default name pointNd = duplicate(nd); sumName = getname(nd); pointName = sprintf('Point_%s', sumName); pointName = uniquename(project(pointNd), pointName); pointNd = setname(pointNd, pointName); % Get the optimizations from the nodes pPointOptim = getdata(pointNd); % Set the objectives to be point objectives objs = getObjectiveFunc(pPointOptim.info); nObj = length(objs); newObj = cell(1, nObj); for i = 1:nObj thisObj = objs{i}; name = getname(thisObj); type = getObjectiveType(thisObj); pExpr = getExpression(thisObj); newObj{i} = cgpointobjective(name, type, pExpr); end pPointOptim.info = setObjectiveFunc(pPointOptim.info, newObj); % Delete any "sum" type constraints. Currently, these are table gradient % and sum constraints. pointConstraints = getConstraint(pPointOptim.info); istabgrad = mbccellarrayeval(pointConstraints, ... @isa, {'cgtabgradconstraint'}, mbclogical); issumcon = mbccellarrayeval(pointConstraints, @isa, ... {'cgsumconstraint'}, mbclogical); idxDel = istabgrad | issumcon; optimSetup = getSetup(pPointOptim.info); conStruct = getConstraints(optimSetup); conStructToDelete = conStruct(idxDel); for i = 1:length(conStructToDelete) conLabel = conStructToDelete(i).label; pPointOptim.info = deleteConstraint(pPointOptim.info, conLabel); end % Import initial values from the results of the sum optimization nSumOutputs = numOutputs(nd); if nSumOutputs % Get active output sumOutput = info(getOutput(nd, nSumOutputs)); % Only use results with one run which are acceptable nRunSum = getNumRuns(sumOutput); if nObj > 1 && hasSelectedSolution(sumOutput) solInd = -1; else solInd = 1; end if nRunSum == 1 && isAcceptable(sumOutput, 1, solInd); [unused, pInp] = getinitialvaluedata(pPointOptim.info); pointInputNames = pveceval(pInp, @getname); % Only set values for variables that can be found in the output OutputNames = getColumnNames(sumOutput, 'OutputFormat', 'cell', ... 'OutputContents', {'FreeVars', 'FixedVars', 'Objectives', 'Constraints'}); ism = ismember(pointInputNames, OutputNames); pPointOptim.info = setinitialvaluedatafromoutput(pPointOptim.info, ... sumOutput, pInp(ism), pointInputNames(ism), 1, solInd); else % Initial values from sum optimization will be used. end else % Initial values from the sum optimization will be used. end % Convert the optimization to a single run pPointOptim.info = convertinitialvaluedata(pPointOptim.info, 'multirun');