www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgoptimnode/pConvertGamultiobjPointToSum.m

    function pConvertGamultiobjPointToSum(cgoptimnode, nd, pOut)
%pConvertGamultiobjPointToSum Convert a gamultiobj point optim to a sum
% Finds results from point runs and adds them as the 'InitPop' option in
% the optimization.

%   Copyright 2014 The MathWorks, Inc.

pOpt = getdata(nd);

pointOutput = pOut.getdata;

optim = info(pOpt);
optimParams = getParameters(optim);
currPopSize = get(optimParams, 'PopulationSize');
popSize = max(currPopSize,200);

% display an information dialog explaining that 
message = sprintf(['CAGE will use your point optimization output to generate the initial population for the sum optimization. \n\n', ...
    'If CAGE does not find a feasible solution, try setting the Creation function to  揻easible in the Optimization ', ...
    'Parameters dialog box. If the optimization does not converge on a good solution, try increasing the population ', ...
    'or number of generations.'],popSize);
uiwait(msgbox(message, 'Sum Optimization Population', 'help'))

% collect point values and sort them by objective value
numRuns = getNumRuns(pointOutput.info);
objTypes = getObjectiveTypes(getOptimRunner(pointOutput.info));
numObjectives = numel(objTypes);
flipObjectiveSign = ones(1,numObjectives);
for i=1:numObjectives
    switch objTypes{i}
        case 'Minimize'
            % do nothing
        case 'Maximize'
            flipObjectiveSign(i) = -1;
    end
end

% only use data from runs that were acceptable
idxSel = any(isAcceptable(pointOutput.info, 1:numRuns, ':'),2);
RunSel = find(idxSel);
nRunSel = length(RunSel);
for i=1:nRunSel
    sortedSolution = generateSolution(pointOutput.info,RunSel(i),flipObjectiveSign);
    pointSortedParetoInput(:,:,i) = sortedSolution;
end

% set optimization parameters
iSetOptimOptions(pOpt,pointSortedParetoInput, popSize);
end


function iSetOptimOptions(pOpt,sumInitialPoints,popSize)
optim = info(pOpt);
optimParams = getParameters(optim);
optimParams = set(optimParams, 'InitPop', sumInitialPoints);

optimParams = set(optimParams, 'PopulationSize', popSize);

optim = setParameters(optim, optimParams);
pOpt.info = optim;
end

function sortedSolution = generateSolution(info,i,flipObjectiveSign)
% get objective output
paretoOutput = getParetoSolution(info,i,'OutputContents', 'Objectives','OutputFormat','matrix');
% flip sign of the column if a maximization
paretoOutput = bsxfun(@times, flipObjectiveSign, paretoOutput);
% sort rows and find indexes
[~, objSortedInds] = sortrows(paretoOutput);
% get the free variable values that correspond to objectives
selSol = getParetoSolution(info,i,'OutputContents', 'FreeVars', 'OutputFormat','matrix');
% sort free variables rows based on sort of objective 
sortedSolution = selSol(objSortedInds,:);
end