www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgtradeoffgui/@mmeditor/addToTradeoff.m

    function addToTradeoff(obj, pTO, DispWaitbar)
%ADDTOTRADEOFF Add the point-by-point models to a tradeoff
%
%  ADDTOTRADEOFF(OBJ, PTO) adds the point-by-point model setup from OBJ to the
%  specified tradeoff node.  This involves creating tables, adding models
%  and variables to the project and linking these tables, models and
%  variables correctly together in the tradeoff.
%
%  ADDTOTRADEOFF(OBJ, PTO, DisplayProgress) where DisplayProgress is a
%  boolean flag toggles the display of a progress dialog that indicates
%  what step of the import is occurring.

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

if ~any(obj.TableWillBeCreated) ...
        || ~obj.hasCorrectAxes(pTO) ...
    % No tables are to be added, so nothing needs to be done
    return
end

if length(obj.AxisOrder)~=2
    % Can only support 2D tables at the moment
    error(message('mbc:cgtradeoffgui:mmeditor:InvalidState'));
end

if pTO.numTables>0
    % Can only support 2D tables at the moment
    error(message('mbc:cgtradeoffgui:mmeditor:InvalidState1'));
end


if nargin<3
    DispWaitbar = false;
end

if DispWaitbar
    progressStartFcn = @i_createWait;
    progressIncrFcn = @i_incrementWait;
    progressEndFcn = @i_closeWait;
else
    progressStartFcn = @i_nullfunction;
    progressIncrFcn = @i_nullfunction;
    progressEndFcn = @i_nullfunction;
end

hWait = feval(progressStartFcn);

PROJ = pTO.project;
pPROJ = address(PROJ);

% create models
[pModelNodes,pInputs] = iModels(obj,pPROJ);
pInputs = unique(pInputs);
OriginalInputNames = getsymbols(obj.FullSwitchModels{1});
obj.InputNames = pveceval(pInputs,@getname);
% table names may have changed so remap to new names
[OK,loc]= ismember(OriginalInputNames,obj.TableNames);
obj.TableNames(loc(OK)) = obj.InputNames(OK); 

obj.SwitchInputs = obj.InputNames(end-1:end);

% Make the normalisers for the tables
numNorms = length(obj.AxisOrder);
pNorm = null(xregpointer, 1, numNorms);
for n = 1:numNorms
    inputname = obj.SwitchInputs{obj.AxisOrder(n)};
    feval(progressIncrFcn, hWait, 1+(n-1)/numNorms, ...
        sprintf('Creating normaliser %s_norm...', inputname));
    bp = obj.Breakpoints{obj.AxisOrder(n)};
    val = 0:length(bp)-1;
    pInput = i_findinput(obj, pInputs, inputname);
    pNorm(n) = cgnormaliser([inputname '_norm'], [bp(:) val(:)], pInput);
    pNorm(n).info = pNorm(n).set('bplocks' , ones(size(bp(:))));
end
feval(progressIncrFcn, hWait, 2);


% Make the tables and the models
numTables = sum(obj.TableWillBeCreated);
numModels = length(obj.FullSwitchModels);
pTables = null(xregpointer, 1, numTables);
pFill = pTables;
tablevals = zeros(cellfun('length', obj.Breakpoints(obj.AxisOrder)));
tableIdx = 0;

pModels = unique(parrayeval(pModelNodes,@getdata,{},@mbcpointer));
for n = find(obj.TableWillBeCreated)
    feval(progressIncrFcn, hWait, 2+(n-1)/numTables, ...
        sprintf('Creating table %s_Table and its fill item...', obj.TableNames{n}));
    
    tableIdx = tableIdx + 1;
    if n<=numModels
        % Table is filled with a model
        
        pFill(tableIdx) = pModels(n);
        
        % Ensure table is initialised to zero
        tablevals(:,:) = 0;
    else
        pFill(tableIdx) = i_findinput(obj, pInputs, obj.TableNames{n});
        
        % Ensure table is initialised to variable setpoint
        tablevals(:,:) = pFill(tableIdx).getnomvalue;
    end
    tblname = [obj.TableNames{n}, '_Table'];
    pTables(tableIdx) = cglookuptwo(tblname, tablevals, pNorm(2), pNorm(1));
end
feval(progressIncrFcn, hWait, 3);


% Add the items to the project
pAddItems = [pNorm, pTables];
hAddItems = infoarray(pAddItems);
ndP = null(xregpointer, 1, length(pAddItems));
for n = 1:length(ndP)
    feval(progressIncrFcn, hWait, 3+(n-1)/(length(ndP)+1), ...
        sprintf('Adding item %s to the project...', getname(hAddItems{n})));
    ndP(n)=cgnode(hAddItems{n},[],pAddItems(n),1);
end
feval(progressIncrFcn, hWait, 3+length(ndP)/(length(ndP)+1), ...
        sprintf('Adding items to the project...'));
pPROJ.addnodestoproject(ndP);
feval(progressIncrFcn, hWait, 4);


% Add the tables to the tradeoff and associate them with the correct
% filling models/inputs
hTO = pTO.info;
if ~isempty(pModels)
    % The first model provides the multimodel mask for all tables
    pMask = pModels(1);
else
    % No switching models are available in the session - use the first
    % input
    pMask = pInputs(1);
end
for n = 1:numTables
    feval(progressIncrFcn, hWait, 4+(n-1)/numTables, ...
        sprintf('Adding table %s to the tradeoff...', pTables(n).getname));
    hTO = addTable(hTO, pTables(n), true);
    hTO = setFillExpression(hTO, pTables(n), pFill(n), pMask);
end
feval(progressIncrFcn, hWait, 5, 'Initializing tradeoff data...');

% Initialise the table inputs' saved values to the list of model sites.
initTableInputsForValidCells(hTO, pTables(1));

% use multimodel settings to initialize tradeoff
mdl = pModels(1).get('model');
pInputs = pModels(1).getinports;

% table might be transposed.
pTableInputs=pTables(1).getinports;
[OK,loc] = ismember(pTableInputs,pInputs);

% get initial values to initialise tradeoff. The initial values are the
% mid-points of each operating point plus the operating point
data = InitialValues(mdl,pveceval(pInputs(end-1:end),@getvalue));
% Sort the data based on the order of the table inputs.
% This is the order assumed in initTableInputsForValidCells.
data = sortrows(data,loc([2 1]));

% fill tradeoff using initial values
pInputs = pInputs(1:end-2);
pFillTradeOff(hTO,data,pInputs);

% Close the progress monitor
feval(progressEndFcn, hWait);

% Function to find the input pointer(s) for the named input(s) from the
% list of previously created inputs.
function ptr = i_findinput(obj, pInputs, name)
[unused, idx] = ismember(name, obj.InputNames);
ptr = pInputs(idx);



% Progress waitbar functions
function h = i_nullfunction(varargin)
if nargout
    h =[];
end

function h = i_createWait
h = xregGui.waitdlg('Message', 'Creating point-by-point model tradeoff...');
h.Waitbar.Max = 5;

function i_incrementWait(h, val, messageString)
h.Waitbar.Value = val;
if nargin>2
    h.Message = messageString;
end

function i_closeWait(h)
delete(h);


function [pModels,pInputs] = iModels(obj,pPROJ)
if isempty(obj.CageModels)
    numModels = length(obj.FullSwitchModels);
    pModels = mbcpointer(1,numModels);
    % variables for model inputs - don't use models
    pInputs = ModelInputs(pPROJ.info,obj.FullSwitchModels{1}, true,false);

    mdl = obj.FullSwitchModels{1};
    data = InitialValues(mdl);
    for i=1:length(pInputs);
        % set nominal values for operating points to be the first operating
        % point
        pInputs(i).info = pInputs(i).setnomvalue(data(1,i));
    end
    
    for i=1:numModels
        m = iPrepareModel(obj,obj.FullSwitchModels{i});
        % make model with replacement
        pModels(i) = ImportMBCModel(pPROJ.info,m,getname(m),false,true,pInputs);
    end
else
    pModels = obj.CageModels;
    for i=1:length(pModels)
        % model needs to be updated for grid etc.
        p = pModels(i).getdata;
        m = p.get('model');
        m = iPrepareModel(obj,m);
        p.info = p.set('model',m);
    end
end
pInputs = getinports( info(pModels(1).getdata) );

function mdl = iPrepareModel(obj,mdl)
mdl = setTolerance(mdl, obj.Tolerance);
mdl = selectModels(mdl, obj.IsSiteIncluded);
mdl = SnapToGrid(mdl,obj.Breakpoints);