www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/@modeldev/CreateAlternativeModels.m

    function [mdev,OK] = CreateAlternativeModels(mdev,mlist,Criteria,GList,Gcrit)
%CREATEALTERNATIVEMODELS Make a series of alternative models and select the best
%
%   mdev= CREATEALTERNATIVEMODELS(mdev,Models,Criteria);
%    Models   list of  models (from model template)
%    Criteria Selection criteria for best model
%
% This option is only available for the two-stage response node
%   mdev= CreateAlternativeModels(mdev,LocalModels,LocalCriteria,GlobalModels,GlobalCriteria);
%    LocalModels    list of local models
%    LocalCriteria  'Two-Stage RMSE'
%    GlobalModels   list of global models (from model template)
%    GlobalCriteria Selection criteria for best model

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


OK = true;
if nargin==3 && ischar( mlist )
    mlist = iGetModelListFromTemplate( mlist );
end
if ~iscell(mlist)
    error(message('mbc:modeldev:InvalidArgument'))
end

m= model(mdev);
if strcmp(guid(mdev),'global')
    [dum,choices] = summary(m);
else
    choices = colhead(mdev);
end
if ~any(strcmp(Criteria,choices)) 
    choices = sprintf('''%s'',',choices{:});
    error(message('mbc:modeldev:InvalidArgument1', choices( 1:end - 1 )));
end
if isa(m,'xregtwostage');
    % Make up a series of two-stage models

    % use the default global model
    G= model( mdevtestplan(mdev) );
    if nargin<5
        % don't try any alternatives global models
        GList= {};
        Gcrit = 'RMSE';
    end
    Lbase= get(m,'local');
    DatumType= get(Lbase,'DatumType');

    for i= 1:length(mlist)
        % mlist is a list of localmods
        L= mlist{i};
        if isa(L,'mbcmodel.localmodel')
           L = L.Object; 
        elseif ~isa(L,'localmod')
            error(message('mbc:modeldev:InvalidArgument2'))
        end
        L= copymodel(Lbase,L);
        if DatumType && ~permitsDatum(L)
            error(message('mbc:modeldev:InvalidArgument3'))
        end
        set(L,'DatumType',DatumType);
        TS= xregtwostage(L,G);
        mdev.Model= TS;
        NewL= makechildren(mdev);
        if ~isempty(GList)
            % build alternative global models
            NewL.children(@CreateAlternativeModels,GList,Gcrit);
        end
        if canGTS(NewL.model)
            NewL.MakeTwoStage;
        end
        mdev= info(mdev);
    end
    OK= ChooseBest(mdev,Criteria);
    mdev= info(mdev);
else
    if nargin>3
        error(message('mbc:modeldev:InvalidArgument4'));
    end
    nf = nfactors(m);
    % one-stage or global models
    if ~isempty(mlist)
        % build alternative models for all response features
        % mlist from model template
        if all(cellfun(@(m) isa(m,'mbcmodel.model'),mlist)) 
            % turn models into underlying objects
            mlist = cellfun(@getObject,mlist,'UniformOutput',false);
        end
        
        if ~all(cellfun(@(m) isa(m,'xregmodel') && nfactors(m)==nf && ~isa(m,'localmod') && ~isa(m,'xregtwostage') ,mlist))
            error(message('mbc:modeldev:InvalidArgument'))
        end
        
        OK = buildmodels(mdev,mlist,Criteria);
    end
    mdev= info(mdev);
end

% ---------------------------------------------------------------
function mlist = iGetModelListFromTemplate( TemplateName )

[p,f,e] = fileparts(TemplateName);

if isempty(e)
    % default extension
    e = '.mbm';
end
if isempty(p) && exist([f,e], 'file') ~= 2
    % default directory - templates are stored in the "Designs" file pref.
    % Dont ask.
    p = mbcGetPath('mbcmodel', 'Designs');
end

TemplateName = fullfile(p,[f,e]);
% Need to check that the requested file exists on the local machine
if exist(TemplateName, 'file') ~= 2
    error(message('mbc:modeldev:InvalidArgument6'));
end

% load from file
template = load('-mat',TemplateName);
if ~isfield( template, 'mlist') || ~iscell(template.mlist) || ~all( cellfun( @(m)isa(m, 'xregmodel'), template.mlist ) )
    error(message('mbc:modeldev:InvalidArgument7'));
end
mlist = template.mlist;