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

    function pUpdateValues(obj)
%PUPDATEVALUES Update expression evaluation values
%
%  PUPDATEVALUES(OBJ) updates the evaluation data in the data model

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

dm = obj.hDataModel;
ms = obj.MessageService;
if ~isempty(ms) ...
        && ~isempty(ms.CurrentTradeoff) ...
        && ~isempty(obj.OutputsCache) ...
        && ~isempty(ms.TradeoffServices.pInputs)
    
    % Set table and hidden inputs to their current saved value
    ms.TradeoffServices.setPointValues;
    % update ranges for point-by-point models
    iUpdateMMRanges(ms,dm);
    redoEvaluation = evaluateTradeoff(obj,obj.ZoomConstraints);
    if redoEvaluation
        evaluateTradeoff(obj,false);
    end

else
    dm = obj.hDataModel;
    dm.RowItemValues = [];
    dm.ColumnItemValues = [];
    dm.XValues = {}; 
    dm.ModelValues = {};
    dm.LowerModelPEBound = {};
    dm.UpperModelPEBound = {};
    dm.ConstraintEdges = {};
end


function iUpdateMMRanges(ms,dm)

pTO = ms.CurrentTradeoff;
[~, pMask] = getFillExpression(info(pTO), ms.CurrentTable);
mst = ms.TradeoffServices;
for i=1:length(mst.pBaseInputs)
    r = getrange(mst.pBaseInputs(i).info);
    % reset to model ranges
    dm.XLimits(i,:) = r;
end
if ~isnull(pMask) && pMask.isSwitchExpr
    pTOInputs = [mst.pBaseInputs,mst.pTableInputs];
    % values for tradeoff
    Vals = [mst.Values, parrayeval(mst.pTableInputs,@getvalue,{},@zeros)];
    
    [SwitchModel,pInputs] = getSwitchModel(info(pMask));
    if ~isempty(SwitchModel)
        mdl = get(SwitchModel,'model');
        [foundInTO,locSwitchInputs] = ismember(pInputs,pTOInputs);
        if all(foundInTO)
            % find operating point for model
            Xop = Vals(locSwitchInputs);
            Index = findOpPoints(mdl,Xop);
            if Index>0
                
                % get range for selected operating point
                mdl = get(SwitchModel,'model');
                [LB,UB] = modelranges(mdl);
                LB = LB(:,Index);
                UB = UB(:,Index);
                
                pSMInputs = getinputs(SwitchModel);
                % remove switch factors
                pSMInputs(getSwitchFactors(SwitchModel))= [];
                
                % update ranges for base inputs in common with switch model
                [InModel,locInModel]=ismember(mst.pBaseInputs,pSMInputs);
                for i=find(InModel)
                    % update limits for inputs to switch model
                    dm.XLimits(i,:) = [LB(locInModel(i)) UB(locInModel(i))];
                end
            end
        end
    end
end

function redoEvaluation = evaluateTradeoff(obj,ZoomConstraints)
ms = obj.MessageService;
dm = obj.hDataModel;



pInputs = ms.TradeoffServices.pInputs;
Values = ms.TradeoffServices.Values;
IsBase = ms.TradeoffServices.IsBaseInput;
BaseIndex = ms.TradeoffServices.BaseIndex;

nInputs = length(pInputs);
nOutputs = length(obj.OutputsCache);

% Create Xvalues for evaluating each model against.  These are a
% range between the limits on each input, with the specific candidate
% input value included.  The location of this input value is saved
% for later extraction of the specific output values.

hInputs = infoarray(pInputs);
xvals = cell(1, nInputs);
dm.ColumnItemValues = Values;
CandidateValueIndex = zeros(1, nInputs);

% Work out correct data for non-derived inputs and set this data
% into the objects.


redoEvaluation = false;
nPoints = 101;
for n = 1:nInputs
    if IsBase(n)
        % include actual value in evaluation points
        [xvals{n},idxvect] = ...
            sort([Values(n), linspace(dm.XLimits(n,1), dm.XLimits(n,2), nPoints)]);
        CandidateValueIndex(n) = find(idxvect==1);
        hInputs{n} = setvalue(hInputs{n}, xvals{n});
    end
end
passign(pInputs(IsBase), hInputs(IsBase));
if any(~IsBase)
    % Get the correct data for derived objects from their current
    % values
    hInputs = infoarray(pInputs);
    for n = find(~IsBase)
        xvals{n} = getvalue(hInputs{n});
        CandidateValueIndex(n) = CandidateValueIndex(BaseIndex(n));
    end
end
dm.XValues = xvals;


% Calculate output data for each model.  First initialise the
% datamodel arrays to the correct size.
dm.RowItemValues = zeros(1, nOutputs);
dm.ModelValues = cell(nOutputs, nInputs);
if obj.ShowErrorLines
    dm.LowerModelPEBound = cell(nOutputs, nInputs);
    dm.UpperModelPEBound = cell(nOutputs, nInputs);
else
    dm.LowerModelPEBound = {};
    dm.UpperModelPEBound = {};
end
if obj.ShowConstraints
    dm.ConstraintEdges = cell(nOutputs, nInputs);
    dm.ConstraintAvailable = false(nOutputs, 1);
else
    dm.ConstraintEdges = {};
    dm.ConstraintAvailable = [];
end

% Loop over the models  to evaluate them
hOutputs = infoarray(obj.OutputsCache);
for n = 1:nOutputs
    isSeqVar = ~dm.IsBlank(n, :) & IsBase;
    SeqVarIdx = find(isSeqVar);
    if isempty(SeqVarIdx)
        % No non-axis inputs for this model - just need to get the row value
        dm.RowItemValues(n) = evaluate(hOutputs{n});
    else
        % Evaluate model
        dm.ModelValues(n, isSeqVar) = ...
            evaluatesequential(hOutputs{n}, ...
            pInputs(isSeqVar), ...
            Values(isSeqVar));
        dm.RowItemValues(n) =  dm.ModelValues{n, SeqVarIdx(1)}(CandidateValueIndex(SeqVarIdx(1)));

        % Evaluate error lines if required
        if obj.ShowErrorLines && pevcheck(hOutputs{n})
            delta = evaluatesequential(hOutputs{n}, ...
                pInputs(isSeqVar), ...
                Values(isSeqVar), ...
                @evalCI);
            for m = 1:length(delta)
                if ~all(isnan(delta{m}))
                    dm.LowerModelPEBound{n, SeqVarIdx(m)} = dm.ModelValues{n, SeqVarIdx(m)} - delta{m};
                    dm.UpperModelPEBound{n, SeqVarIdx(m)} = dm.ModelValues{n, SeqVarIdx(m)} + delta{m};
                end
            end
        end

        % Evaluate constraints if required
        if (ZoomConstraints || obj.ShowConstraints) && concheck(hOutputs{n}) 
            dm.ConstraintAvailable(n) = true;
            cons = evaluatesequential(hOutputs{n}, ...
                pInputs(isSeqVar), ...
                Values(isSeqVar), ...
                'constraint');
            for m = 1:length(cons)
                if obj.ShowConstraints
                    % add constraint patches
                    dm.ConstraintEdges{n, SeqVarIdx(m)} = ...
                        xregGui.intervalPatch1D.convertConstraintVector( ...
                        xvals{SeqVarIdx(m)}, cons{m});
                end
                    
                if ZoomConstraints
                    % work out new limits for next evaluation
                    insideConstraint = cons{m}<1e-6;
                    st = max(find(insideConstraint,1,'first')-1,1);
                    fin = min(find(insideConstraint,1,'last')+1,nPoints);
                    if ~isempty(st) && ~isempty(fin)
                        % calculate new limits based on constraint
                        newLims = dm.XValues{SeqVarIdx(m)}([st fin]);
                        
                        dm.XLimits(SeqVarIdx(m),:) = [max(dm.XLimits(SeqVarIdx(m),1),newLims(1)),...
                            min(dm.XLimits(SeqVarIdx(m),2),newLims(2))];
                        % redo evaluation with new limits
                        
                        redoEvaluation = true;
                    end
                end
                
            end
        end

        % Fill in values for derived inports to this model
        isDerivedInput = ~dm.IsBlank(n, :) & ~IsBase;
        if any(isDerivedInput)
            for m = find(isDerivedInput)
                % Fill in the output values from the output of the base
                % input that we depend on
                dm.ModelValues{n, m} = dm.ModelValues{n, BaseIndex(m)};
                if obj.ShowErrorLines
                    dm.LowerModelPEBound{n, m} = dm.LowerModelPEBound{n, BaseIndex(m)};
                    dm.UpperModelPEBound{n, m} = dm.UpperModelPEBound{n, BaseIndex(m)};
                end
                if obj.ShowConstraints
                    dm.ConstraintEdges{n, m} = dm.ConstraintEdges{n, BaseIndex(m)};
                end
            end
        end
    end
end