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

    function obj = cgoptimoutput(optim, outputData, LastError, exitFlag, output,selectedsol)
%CGOPTIMOUTPUT Constructor for optimization output object.
%
%  OBJ = CGOPTIMOUTPUT constructs an object to hold the results of an
%  optimization.
%
%  OBJ = CGOPTIMOUTPUT(OPTIM, OUTDATA, ERRS, FLAGS, STATS)
%  constructs an object to hold the specified output data.

%  Copyright 2005-2009 The MathWorks, Inc. and Ford Global Technologies, Inc.


if nargin==1 && isstruct(optim)
    % Upgrade old object structure
    R = optim.mbcreference;
    s = rmfield(optim, 'mbcreference');
    obj = class(s, 'cgoptimoutput', R);
else
    % Create structure
    s.outputData = {{}};
    s.outputWeights = [];
    s.outputSelection = cgoptcsol;
    s.runErrors = {{}};
    s.runFlags = [];
    s.runStats = struct([]);
    s.optimRunner = [];
    s.name = 'New_Output';
    s.version = 3;
    s.solutionFlags = uint8([]);
    s.displayConTol = 1e-6;
    s.tableFiller = [];

    if nargin
        % Initialise from the optimization data

        % Copy relevant fields
        s.outputData = outputData;
        
        % Initialise the output weights to unity
        if ~isempty(outputData)
            szout = size(outputData{1});
            if length(szout)<3
                szout = [szout 1];
            end
        else
            szout = [0 0 0];
        end

        % Create runner from the optimization
        s.optimRunner = pCreateOptimRunner(optim);

        % Create output weights
        [unused, Nobj] = numObjectives(s.optimRunner);
        s.outputWeights = ones(szout(1), Nobj);

        
        % Update solution flags to match output data size
        s.solutionFlags = zeros( szout(1),  szout(3), 'uint8');

        % Check output info is all correct
        s.runErrors = i_checkerr(LastError, szout);
        s.runFlags = i_checkflags(exitFlag, szout);
        s.runStats = i_checkstats(output, szout(1));

        % Set name from the optimization's name
        s.name = [getname(optim), '_Output'];
    end
    
    R = mbcreference;
    obj = class(s, 'cgoptimoutput', R);
    
    % Initialise acceptable flags
    obj = setAcceptableToExitFlag(obj, ':', ':');
    
    % Initialise constraint tolerance
    obj = setConstraintTolFromSetup(obj);
    if nargin>5 && ~isempty(selectedsol)
        for i=1:length(selectedsol)
            if ~isempty(selectedsol{i})
                obj = setSelectedSolutionNumber(obj,i,selectedsol{i});
            else
                obj = setSelectedSolutionNumber(obj,i,1);
            end
        end
    end
    
end




function PropVal = i_checkerr(msg, nVals)
if isempty(msg)
    PropVal = repmat({''}, nVals(1), 1);
elseif ischar(msg)
    PropVal = repmat({msg}, nVals(1), 1);
elseif iscell(msg)
    IsChar = cellfun('isclass',msg,'char');
    if isscalar(msg) && ischar(msg{1})
        PropVal = repmat(msg, nVals(1), 1);
    elseif ~all(IsChar)
        try
            NumSolutions = max(cellfun('size',msg(~IsChar),2));
            for i=find(IsChar)
                msg{i} = repmat(msg(i),1,NumSolutions);
            end
            
            PropVal = cat(1,msg{:});
        catch ME
            error(message('mbc:cgoptimoutput:InvalidArgument'));
        end
        if size(PropVal,2)~=nVals(3)
            error(message('mbc:cgoptimoutput:InvalidArgument1'));
        end
    elseif length(msg)==nVals(1)
        PropVal = msg(:);
    else
        error(message('mbc:cgoptimoutput:InvalidArgument2'));
    end
else
    error(message('mbc:cgoptimoutput:InvalidArgument3'));
end


function PropVal = i_checkflags(flags, nVals)

if iscell(flags)
    Nsolflags = max(cellfun('size',flags,2));
    if all(cellfun('size',flags,2) == Nsolflags)
        flags = cat(1,flags{:});
    else
        NRuns = nVals(1);
        OKmatrix = zeros(NRuns,Nsolflags);
        for i=1:NRuns
            if ~isempty(flags{i})
                OKmatrix(i,:) = flags{i};
            else
                OKmatrix(i,:) = -1;
            end
        end
        flags = OKmatrix;
    end
end
if islogical(flags)
    flags = double(flags);
end
if isempty(flags)
    PropVal = zeros(nVals(1), 1);
elseif isnumeric(flags)
    if isscalar(flags)
        PropVal = repmat(flags, nVals(1), 1);
    elseif all(size(flags)==nVals([1 3]))
        PropVal = flags;
    elseif length(flags)==nVals(1)
        PropVal = flags(:);
    else
        error(message('mbc:cgoptimoutput:InvalidArgument4'));
    end
else
    error(message('mbc:cgoptimoutput:InvalidArgument5'));
end


function PropVal = i_checkstats(stats, nVals)
if isempty(stats)
    PropVal = repmat(struct, nVals, 1);
elseif isstruct(stats)
    if isscalar(stats)
        PropVal = repmat(stats, nVals, 1);
    elseif length(stats)==nVals
        PropVal = stats(:);
    else
        error(message('mbc:cgoptimoutput:InvalidArgument6'));
    end
else
    error(message('mbc:cgoptimoutput:InvalidArgument7'));
end