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

    function obj = copyFromOptim(obj, optim, RunCopy)
%COPYFROMOPTIM Copy static data from an optimization
%
%  OBJ = COPYFROMOPTIM(OBJ, OPTIM) extracts all of the data required for
%  running an optimization and stores it as static data within this object.
%
%  OBJ = COPYFROMOPTIM(OBJ, OPTIM, false) creates a cgoptimrunner that only
%  holds the data required for using the object in a cgoptimoutput.  This
%  object will not be able to provide all of the services that a full
%  version can.

%  Copyright 2005-2015 The MathWorks, Inc.

if nargin<3
    RunCopy = true;
end

% ----- Function to run
obj.FunctionName = getfunctionfile(optim);


% ----- Interface version to use.  Checks are performed later
obj.RunAPIVersion = getRunAPIVersion(optim);


% ----- Name<-->Item mapping information
obj.Setup = getEnabledSetup(optim);


% ----- Parameters
obj.Parameters = getParameters(optim);

% ----- Data sets


% ----- Input data
[obj.InputData, pInputFactors] = getinitialvaluedata(optim);

obj.InputDataLengths = getvaluedatasizes(optim, pInputFactors);
obj.InputDataNames = pveceval(pInputFactors, @getname);

pFree = getfreevalues(optim);
[~, obj.FreeVariableIndices] = ismember(pFree, pInputFactors);

obj.FreeVariableRanges = getFreeValueRange(optim).';

% get model ranges for point-by-point models
obj.ModelRangeInfo = getModelRangeInfo(optim,pInputFactors);

if obj.RunAPIVersion==2
    % Lengths must all be scalar
    if any(obj.InputDataLengths>1)
        error(message('mbc:cgoptimrunner:InvalidInterfaceState'));
    end
end

% Set run indices to run them all by default
obj.RunIndices = 1:size(obj.InputData{1},1);
obj.CurrentRun = 0;

DS = getOptimDatasets(optim);
obj.DataSetData = convertToStatic(DS, optim );
if obj.RunAPIVersion==2
    if RunCopy
        obj.DataSetData = i_AddV2InterfaceData(obj, obj.DataSetData);
    else
        % Do not take any datasets into the output
        obj.DataSetData = struct('Data', {},...
            'InputVariableIdx', {},...
            'MatchedColumnIdx', {},...
            'DSName',{},...
            'InterpMatrix',{});
    end
end


% ----- Objectives
if RunCopy
    EvalTypes = {'Evaluate', 'NaturalEvaluate'};
else
    EvalTypes = {'NaturalEvaluate'};
end
obj.Objectives = pCopyOptimItems(obj, getObjectiveFunc(optim), ...
    pInputFactors, obj.InputDataLengths, EvalTypes, DS.ObjectiveIndices,RunCopy);


% ----- Constraints
if RunCopy
    EvalTypes = {'Evaluate', 'NaturalEvaluate'};
else
    EvalTypes = {'NaturalEvaluate', 'LHSEvaluate', 'RHSEvaluate'};
end
[constraints,Enabled] = getEnabledConstraints(optim);
DSindices = DS.ConstraintIndices(Enabled);
obj.Constraints = pCopyOptimItems(obj, constraints, ...
    pInputFactors, obj.InputDataLengths, EvalTypes,DSindices ,RunCopy);

% make linear constraints
obj.LinearConstraints = pCreateLinearConstraintData(obj, obj.Constraints, RunCopy);
if obj.RunAPIVersion==2
    % Check that linear constraint data is OK for old interface
    if any(obj.LinearConstraints.A2(:)~=0)
        error(message('mbc:cgoptimrunner:InvalidInterfaceState1'));
    end
end



% ----- Save whether a full copy was done
obj.HasRunData = RunCopy;

% ----- Set default evaluation mode
if RunCopy
    obj.DefaultEvaluationType = 'Evaluate';
else
    obj.DefaultEvaluationType = 'NaturalEvaluate';
end



% Add in fixed variable data to the first dataset.  This is only used when
% the user requests to run in v2 interface mode
function DSData = i_AddV2InterfaceData(obj, DSData)

if isempty(DSData)
    % Add a dummy data set
    DSData = cgstaticdataset;
    DSData.Data = zeros(length(obj.RunIndices), 0);
else
    % Check that the primary dataset has same number of rows as
    % there are runs
    if length(obj.RunIndices)~=size(DSData(1).Data,1)
        error(message('mbc:cgoptimrunner:InvalidInterfaceState2'));
    end
end

% Add fixed variable data into the first data set
DSDataOne = DSData(1);
for n = 1:length(obj.InputDataNames)
    if ~ismember(n, obj.FreeVariableIndices)
        IsInDataset = (n==DSDataOne.InputVariableIdx);
        if ~any(IsInDataset)
            % Add to the data set
            DSDataOne.Data = [DSDataOne.Data, obj.InputData{n}];
            DSDataOne.InputVariableIdx = [DSDataOne.InputVariableIdx, n];
        else
            % Copy data into the data set location
            DSDataOne.Data(:, IsInDataset) = obj.InputData{n};
        end
    end
end
DSData(1) = DSDataOne;