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

    function optim = convertToSum(optim, pointOutput )
%CONVERTTOSUM Convert a point optimization to a sum.
%
%   OPTIM = CONVERTTOSUM(OPTIM, POINTOUTPUT) converts a point optimization
%   to a sum. The following actions are performed:
%
%   1. Non-helper objectives are converted to sum objectives.
%   2. Initial values are imported from the supplied output. Note if the
%   output is not from a point optimization, then no data is imported.
%   Furthermore only acceptable runs and data from free/fixed variables in
%   the output that match those in the optimization (by name) are imported.
%   No warning is given if no data is imported.
%   3. The optimization is converted to a single run.  
%
%   See also CGOPTIMNODE/CREATESUMOPTIMIZATION

%   Copyright 2007-2014 The MathWorks, Inc.

if nargin < 2
    pointOutput = [];
end


% Set non-helper objectives to be sum objectives
objs = getObjectiveFunc(optim);
nObj = length(objs);
newObj = cell(1, nObj);
for i = 1:nObj    
    thisObj = objs{i};    
    type = getObjectiveType(thisObj);
    if any(strcmpi(type, {'minimize', 'maximize'})) && ...
            ~isa(objs{i}, 'cgsumobjective')
        name = getName(thisObj);
        pExpr = getExpression(thisObj);
        newObj{i} = cgsumobjective(name, type, pExpr);
        newObj{i} = setAllowTypeChange(newObj{i}, true);
    else
        newObj{i} = thisObj;
    end
end
optim = setObjectiveFunc(optim, newObj);

% Import initial values from the results of the point optimization 
if i_outputIsPoint(pointOutput)

    [RunSel, SolInd] = i_acceptableRunsSols(pointOutput);
    nRunSel = length(RunSel);
    if nRunSel
        [pCommonFacs, matchedSumNames] = i_findMatchedFactors(optim, pointOutput);
        optim = setNumRuns(optim, nRunSel);
        optim = setinitialvaluedatafromoutput(optim, ...
            pointOutput, pCommonFacs, matchedSumNames, RunSel, SolInd);
    end

else
    % Initial values will be unaltered. 
end

% Convert the optimization to a single run
optim = convertinitialvaluedata(optim, 'multivalue');

function [RunSel, SolInd] = i_acceptableRunsSols(pointOutput)

% Return the indices of the acceptable runs and solutions
if hasSelectedSolution(pointOutput)
    SolInd = -1;
else
    SolInd = 1;
end
if getNumSolutions(pointOutput)>1
    % multiple solutions - select run if there are any acceptable solutions
    idxSel = any(isAcceptable(pointOutput, 1:getNumRuns(pointOutput), ':'),2);
    RunSel = find(idxSel);
else
    idxSel = isAcceptable(pointOutput, 1:getNumRuns(pointOutput), SolInd);
    RunSel = find(idxSel);
end

function [pCommonFacs, matchedSumNames] = i_findMatchedFactors(optim, pointOutput)

% Return the common input factors between the new sum optimization and the
% specified point optimization output
[~, pInp] = getinitialvaluedata(optim);
sumInputNames = pveceval(pInp, @getname);
OutputNames = getColumnNames(pointOutput, 'OutputFormat', 'cell', ...
    'OutputContents', {'FreeVars', 'FixedVars', 'Objectives', 'Constraints'});
ism = ismember(sumInputNames, OutputNames);
pCommonFacs = pInp(ism);
matchedSumNames = sumInputNames(ism);

function stat = i_outputIsPoint(output)

if isempty(output)
    stat = false;
else
    nFreeOut = getNumFreeVariables(output);
    nFixedOut = getNumFixedVariables(output);
    stat = all([nFreeOut, nFixedOut] == 1);
end