www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgpointconstraint/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 for the constraint summary template
        % string
        StaticData.char_return = ...
            getStaticData(obj.cgoptimconstraint, 'char_return');
        StaticData.ComparisonOperator = ...
            getStaticData(obj.cgoptimconstraint, 'ComparisonOperator');
        StaticData.IsLinear = ...
            getStaticData(obj.cgoptimconstraint, 'IsLinear');
        StaticData.charForSummary_return = i_getCharForSummary(obj);
        obj.cgoptimconstraint = pSetStaticData(...
            obj.cgoptimconstraint, StaticData);
    end
end

% Current version number
obj.Version = 2;

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

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

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

% Pattern strings for 1 and 2-d tables
patstr1d = '(\w+)\((\w+)\) (<=|>=) \1(max|min)';
patstr2d = '(\w+)\((\w+),(\w+)\) (<=|>=) \1(max|min)';

% Replace the j-th variable name with CONSUMMARYINPUTj and store the
% variable names
if ~isempty(strfind(objChar, '*'))
    % Linear constraint
    inputNames = regexp(objChar, '[0-9]+\*(\w+)', 'tokens');
    inputNames = [inputNames{:}];
    inputNames = i_uniqueNoSort(inputNames);
    for i = 1:length(inputNames)
        repStr = sprintf('%s%d', '$1CONSUMMARYINPUT', i);
        pat = ['([0-9]+\*)', inputNames{i}];
        objChar = regexprep(objChar, pat, repStr);
    end
elseif ~isempty(regexp(objChar, '^Ellipsoid at \(', 'once' ))
    % Ellipsoid constraint
    inputNames = regexp(objChar, '([A-z]\w*)=', 'tokens');
    inputNames = [inputNames{:}];
    inputNames = i_uniqueNoSort(inputNames);
    for i = 1:length(inputNames)
        repStr = sprintf('%s%d%s', 'CONSUMMARYINPUT', i, '=');
        pat = [inputNames{i}, '='];
        objChar = regexprep(objChar, pat, repStr);
    end
elseif ~isempty(regexp(objChar, patstr1d, 'once'))
    % 1-d Table Constraints
    inputNames = regexp(objChar, patstr1d, 'tokens');
    % 1-d Table string is Y(X) <= Ymax. Inputs in object are in order, X,
    % Y.
    inputNames = inputNames{1}([2 1]);   
    [inputNames, unused, idx] = i_uniqueNoSort(inputNames);
    repStr = sprintf('CONSUMMARYINPUT%d(CONSUMMARYINPUT%d) $3 CONSUMMARYINPUT%d$4', idx([2 1 2]));
    objChar = regexprep( objChar, patstr1d, repStr);
elseif ~isempty(regexp(objChar, patstr2d, 'once'))
    % 2-d Table constraints
    inputNames = regexp(objChar, patstr2d, 'tokens');
    % 2-d Table string is Z(X, Y) <= Zmax. Inputs in object are in order, X,
    % Y, Z
    inputNames = inputNames{1}([2 3 1]);  
    [inputNames, unused, idx] = i_uniqueNoSort(inputNames);   
    repStr = sprintf('CONSUMMARYINPUT%d(CONSUMMARYINPUT%d,CONSUMMARYINPUT%d) $4 CONSUMMARYINPUT%d$5', idx([3 1 2 3]));
    objChar = regexprep(objChar, patstr2d, repStr);
else 
    % Model constraint 
    % Replace <varname)> and <varname, > 
    inputNames = regexp(objChar, '(\w+)\)|(\w+),', 'tokens');
    inputNames = [inputNames{:}];
    % Want inputNames to be unique, but in the order we encounter them, not
    % sorted
    [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 model constraints that are bounded by a variable
    varBoundName = regexp(objChar, '(<=|>=) ([A-z]\w*)$', 'tokens');
    if ~isempty(varBoundName) 
        [ism, loc] = ismember(varBoundName{1}(2), inputNames);
        if ism
            repStr = sprintf('%s%d', '$1 CONSUMMARYINPUT', loc);
        else
            inputNames = [inputNames, varBoundName{1}(2)];
            repStr = sprintf('%s%d', '$1 CONSUMMARYINPUT', length(inputNames));
        end
        objChar = regexprep(objChar, '(<=|>=) ([A-z]\w*)$', repStr);
    end
end
charForSummary_return{1} = objChar;
charForSummary_return{2} = inputNames;
 
%--------------------------------------------------------------------------
function [B, I, J] = i_uniqueNoSort(A)
%--------------------------------------------------------------------------

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