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

    function obj = loadobj(obj)
%LOADOBJ Load-time object actions
%
%   OBJ = LOADOBJ(OBJ)

%   Copyright 2007 The MathWorks, Inc.

if obj.Version < 2
    if isStaticMode(obj.cgoptimconstraint)
        % Upgrade the static data to add the constraint summary template
        % string
        StaticData.char_return = ...
            getStaticData(obj.cgoptimconstraint, 'char_return');
        StaticData.IsLinear = ...
            getStaticData(obj.cgoptimconstraint, 'IsLinear');
        StaticData.InfBounds = ...
            getStaticData(obj.cgoptimconstraint, 'InfBounds');       
        StaticData.charForSummary_return = i_getCharForSummary(obj, ...
            StaticData.InfBounds);
        obj.cgoptimconstraint = pSetStaticData(...
            obj.cgoptimconstraint, StaticData);
    end
end

% Current version number
obj.Version = 2;

% Convert to object if structure
if isstruct(obj)
    obj = cgrangeconstraint(obj);
end

%--------------------------------------------------------------------------
function charForSummary_return = i_getCharForSummary(obj, InfBounds)
%--------------------------------------------------------------------------

% Get statically cached char
objChar = getStaticData(obj.cgoptimconstraint, 'char_return');

% Replace <varname)> and <varname, >. We're assuming here that the strings
% found here are names of fixed/free variables in the optimization.
inputNames = regexp(objChar, '(\w+)\)|(\w+),', 'tokens');
inputNames = [inputNames{:}];
[uInputNames, unused, loc] = i_uniqueNoSort(inputNames);
for i = 1:length(inputNames)
    repStr = sprintf('%s%d%s', 'CONSUMMARYINPUT', loc(i), '$2');
    objChar = regexprep(objChar, '(\w+)(\))|(\w+)(,)', repStr, i);
end
inputNames = uInputNames;

% Check for range constraints that are bounded below by a variable
lowerBoundName = regexp(objChar, '([A-z]\w*) <=', 'tokens');
lowerBoundName = [lowerBoundName{:}];
if ~isempty(lowerBoundName) && InfBounds(1) 
    lowerBoundName(1) = [];
end
nBound = length(lowerBoundName);
if nBound == 2
    % Constraint of the form, ECP_Lower <= ECP <= something
    [repStr, inputNames] = i_genTemplateInputString(lowerBoundName, inputNames);
    repStr = [repStr{1}, ' <= ', repStr{2}, ' <='];
    objChar = regexprep(objChar, '([A-z]\w*) <= ([A-z]\w*) <=', repStr);
elseif nBound == 1 
    [repStr, inputNames] = i_genTemplateInputString(lowerBoundName, inputNames);
    objChar = regexprep(objChar, '([A-z]\w*) <=', [repStr{1}, ' <=']);
end

% Check for range constraints that are bounded above by a variable
upperBoundName = regexp(objChar, '<= ([A-z]\w*)$', 'tokens');
if ~isempty(upperBoundName) && InfBounds(2) 
    upperBoundName(1) = [];
end
if ~isempty(upperBoundName)
    upperBoundName = upperBoundName{1};
    [repStr, inputNames] = i_genTemplateInputString(upperBoundName, inputNames);
    objChar = regexprep(objChar, '<= ([A-z]\w*)$', ['<= ', repStr{1}]);
end

charForSummary_return{1} = objChar;
charForSummary_return{2} = inputNames;

%--------------------------------------------------------------------------
function [repStr, inputNames] = i_genTemplateInputString(testInputs, inputNames)
%--------------------------------------------------------------------------

% Number of test inputs to generate template string for
nInp = length(testInputs);

% Initialise replacement string
repStr = cell(1, nInp);

% Add any new inputs to input names
inputNames = [inputNames, setdiff(testInputs, inputNames,'legacy')];

% Find location of test inputs in newly augmented inputNames
[unused, loc] = ismember(testInputs, inputNames,'legacy');

% Generate the replacement string for the specified inputs
for i = 1:nInp
    repStr{i} = sprintf('%s%d', 'CONSUMMARYINPUT', loc(i));
end

%--------------------------------------------------------------------------
function [B, I, J] = i_uniqueNoSort(A)
%--------------------------------------------------------------------------

[uA, uloc, loc] = unique(A, 'first','legacy');
[I, ulocIdx] = sort(uloc);
B = uA(ulocIdx);
[unused, J] = ismember(loc, ulocIdx,'legacy');