www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@xregdesign/gui_generateSpaceFilling.m

    function [dout,ok] = gui_generateSpaceFilling(d)
%gui_generateSpaceFilling GUI for choosing a pre-defined space filling design
%
%   [DOUT,OK]=gui_generateSpaceFilling(D) invokes a dialog editing or choosing a
%   pre-defined space-filling design. 

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


if npoints(d)
    % do an add/overwrite dialog
    [action,ok]=designAugmentReplaceGUI(d);
    if ~ok
        dout=d;
        return
    end
else
    action=1;
end

% Decide which option to go for
nm = 'Space-Filling Design Browser';
htag = 'xreg_desSpaceFill';
dlg = mbcgui.container.Dialog('Name', nm, ...
    'HelpCode', htag, ...
    'PersistPosition', true, ...
    'Tag', 'DesignBrowser', ...
    'ContentBorder', [7 0 7 10], ...
    'Size', [700 450]);
dlg.Figure.MinimumSize = [600 380];
fH = dlg.Figure;

p=xregpointer([]);
[lyt,GUI_STATE]=i_createlyt(fH,p,d);

if GUI_STATE
    EN_STATE = true;
else
    EN_STATE = false;
end
dlg.Enabled = EN_STATE;
dlg.Content = lyt;

Result = dlg.showDialog();

if strcmp(Result,'OK')
    ok=1;
    i_finalise(lyt);
    cs=p.info;
else
    ok=0;
end
freeptr(p);
delete(dlg);

% Use the new candidate set or do a no-op
if ~ok
    dout = d;
else
    ActionStrs = {'replace', 'add', 'replacefree'};
    dout = SpaceFillDesign(d,cs,ActionStrs{action},'constrain');
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function to create the browser GUI %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [lyt, GUI_STATE]=i_createlyt(fH, p, d)
ud.pointer = p;
ud.figure = fH;
ud.model = model(d);
c = constraints(d);
if isempty(c)
    ud.constraints = [];
else
    ud.constraints = reset(c);   % keep this constraints object lightweight
end
ud.design = d;
ud.autopreview = true;
ud.ispreviewed = false;

% Get list of candidateset objects
csI=csetinterface;
csI=set(csI,'typefilter',1);
csI=set(csI,'nffilter',nfactors(d));
viewlist=get(csI,'fullnames');
ud.createlist=get(csI,'classnames');
ud.cards=zeros(1,length(ud.createlist));

defaultOption = find(strcmp(ud.createlist, 'cset_spacefillsobol'));
if ~isempty(viewlist)
    % available designs to choose from
    GUI_STATE=true;
    ENABLE_SETTING='on';
    [s,info]=getstyle(d);
    if s-1==1
        % use the current settings as default but reset the candidateset
        ud.pointer.info=reset(info);
        ind=find( strcmp( class( info ),ud.createlist ) );
        if isempty(ind)
            ind=1;
        end
    else
        tgt=gettarget(ud.model);
        csP=candidateset(tgt);
        ud.pointer.info=feval(ud.createlist{defaultOption},csP);
        % default option
        ind=defaultOption;
    end
else
    % no available designs
    GUI_STATE=false;
    ENABLE_SETTING='off';
    ind=defaultOption;
    viewlist={''};
end

sc=xregGui.SystemColorsDbl;

ud.typepop=uicontrol('Parent',fH,...
    'Tag', 'DesignStylePopup',...
    'Style','popupmenu',...
    'Enable',ENABLE_SETTING,...
    'Value',ind,...
    'String',viewlist,...
    'Interruptible','off',...
    'BackgroundColor',sc.WINDOW_BG);
ud.typetxt = xregGui.labelcontrol('Parent', fH, ...
    'LabelSizeMode', 'absolute', ...
    'LabelSize', 70, ...
    'ControlSize', 180, ...
    'String', 'Design type:', ...
    'Control', ud.typepop, ...
    'Enable', ENABLE_SETTING);


ud.optsframe = mbcgui.container.layoutpanel(...
    'Parent', fH, ...
    'Title','Options',...
    'BorderType', 'etchedin', ...
    'LayoutBorder',[10 10 10 10]);
ud.previewframe = mbcgui.container.layoutpanel(...
    'Parent', fH, ...
    'Title','Preview',...
    'BorderType', 'etchedin', ...
    'LayoutBorder',[10 10 10 5]);

ud.PreviewCheck = uicontrol('Parent',ud.previewframe,...
    'Style','checkbox',...
    'String','Automatically update preview',...
    'Enable',ENABLE_SETTING,...
    'Value', 1, ...
    'Tag', 'AutoUpdate');
ud.PreviewButton = uicontrol('Parent',ud.previewframe,...
    'Style','pushbutton',...
    'String','Generate');


ud.NoPreviewText = uicontrol('Parent',ud.previewframe,...
    'Style','text',...
    'HorizontalAlignment','center', ...
    'String', 'Press Generate to create a preview of the currently selected design.');
NoPreviewLyt = xreglayerlayout(ud.previewframe, ...
    'elements', {ud.NoPreviewText}, ...
    'border', [50 40 50 40]);
udh=ud.typetxt;


if GUI_STATE
    proplyt=propertypage(p.info,'layout',ud.optsframe,'callback',{@i_optschange,ud.typetxt}, 'editablePoints', false);
    crd=xregcardlayout(ud.optsframe,'numcards',1, 'packstatus', 'off');
    attach(crd,proplyt,1);
else
    crd=uicontrol('Parent',ud.optsframe,...
        'Visible','off',...
        'Style','text',...
        'Enable','inactive',...
        'HorizontalAlignment','left',...
        'String',['There are no design styles of this type available that are compatible with',...
        ' your current settings.  Please try a different type of design, or change the number',...
        ' of input factors in your experiment.']);
end
ud.cardlay=crd;
set(ud.optsframe, 'LayoutComponent', {crd})

ud.tabbedPreview = mbcgui.graph.TabbedGraphs(ud.previewframe, d.nfactors);
ud.previewcards = xregcardlayout(ud.previewframe, 'numcards', 2);
attach(ud.previewcards,ud.tabbedPreview,1);
attach(ud.previewcards,NoPreviewLyt,2);

GenGrid = xreggridbaglayout(ud.previewframe, ...
    'dimension', [3 2], ...
    'gapx', 15, ...
    'colsizes', [65 200], ...
    'rowsizes', [5 15 5], ...
    'mergeblock', {[1 3],[1 1]}, ...
    'elements', {ud.PreviewButton, [], [], [],ud.PreviewCheck}); 

% constrained points spinner
ud.constrainedpoints = mbcgui.widget.Spinner('Parent', fH,...
    'Min',2,...
    'Max',2^52,...
    'Visible','off',...
    'ClickIncrement',10,...
    'Rule','int', ...
    'Tag', 'ConstrainedPointsSpinner');
ud.constrainedtxt = xregGui.labelcontrol('Parent', fH, ...
    'LabelSizeMode', 'absolute', ...
    'LabelSize', 120, ...
    'ControlSize', 50, ...
    'String', 'Number of points:', ...
    'Control', ud.constrainedpoints, ...
    'Enable','on');
ud.pointInformationTxt = uicontrol('Parent', fH, ...
    'Style', 'text', ...
    'String', '', ...
    'HorizontalAlignment','left');
set(ud.constrainedpoints, 'Callback', {@i_optschange, udh});
% options layout
optsLayout=xreggridbaglayout(fH, ...
'dimension',[2 2],...
'mergeblock', {[2 2],[1 2]}, ...
'rowsizes',[20 -1], ...
'colsizes',[170 -1], ...
'gap',10, ...
'elements',{ud.constrainedtxt, ud.optsframe, ud.pointInformationTxt, []});

% layout preview frame
grd=xreggridbaglayout(ud.previewframe, ...
    'dimension',[5 2],...
    'rowsizes',[25 10 2 -1 25], ...
    'colsizes', [222 -1], ...
    'mergeblock', {[1 1], [1 2]}, ...
    'mergeblock', {[3 5], [1 2]}, ...
    'elements',{GenGrid, [], ud.previewcards, [], [], ...
    [], [], [], [], []});
set(ud.previewframe, 'LayoutComponent', {grd});

lyt=xreggridbaglayout(fH, ...
    'dimension',[2 3],...
    'rowsizes',[20 -1], ...
    'colsizes', [350 300 -1], ...
    'gap',10, ...
    'mergeblock', {[1 1], [1 3]}, ...
    'mergeblock', {[2 2], [2 3]}, ...
    'elements',{ud.typetxt, optsLayout, [], ud.previewframe});

set(ud.typepop,'Callback',{@i_typechange,udh});
set(ud.PreviewCheck,'Callback',{@i_prevchange,udh});
set(ud.PreviewButton, 'Callback', {@i_doprev, udh});

if GUI_STATE
    ud.cards(ind) = 1;
end
ud.GUI_STATE=GUI_STATE;

% if candidate set has not been generated then generate it
cs = ud.pointer.info;
if ~isGenerated(cs)
    cs = generate(cs);
    ud.pointer.info = cs;
end
% calculate number of constrained points
data = constrainCandidateSet(cs, ud);
numPoints = size(data,1);

ud=i_setvalsinit(ud);
ud=i_setvalues(ud, numPoints, true);
set(udh,'UserData',ud);



function ud=i_setvalsinit(ud)

% Check whether to start with the preview pane active
ud.autopreview = ud.pointer.checkPreview;
set(ud.PreviewCheck, 'Value', ud.autopreview);
ud = i_switchpreview(ud);

if ud.GUI_STATE
    nf=ud.pointer.nfactors;
    if nf>=2
        % Start on the 2D tab
        ud.tabbedPreview.selectTab(2);
        if ud.autopreview
            set(ud.previewcards, 'CurrentCard', 1);
        end
    end
end

% initialise factor names in graph objects
if ud.GUI_STATE
    symb=get(ud.model,'symbol');
    ud.tabbedPreview.setFactors(symb);
end


function ud=i_setvalues(ud, numPoints, useCS)
if nargin < 3
    useCS = false;
end

val=get(ud.typepop,'Value');
if val>0 && ud.GUI_STATE
    % check and create property page
    if ~ud.cards(val)
        nc=get(ud.cardlay,'numcards');
        set(ud.cardlay,'numcards',nc+1);
        proplyt=propertypage(ud.pointer.info,'layout',ud.optsframe,'callback',{@i_optschange,ud.typetxt}, 'editablePoints', false);
        attach(ud.cardlay,proplyt,nc+1);
        ud.cards(val)=nc+1;
    else
        proplyt=getcard(ud.cardlay,ud.cards(val));
        proplyt=proplyt{1};
    end
    % update propertypage
    propertypage(ud.pointer.info,'update',proplyt,ud.pointer,ud.model);
    set(proplyt,'packstatus','on');
    set(ud.cardlay,'currentcard',ud.cards(val));
    % update constrained points spinner
    ud.constrainedpoints.Value = numPoints;
end

if ud.autopreview
    % update preview
    if ud.GUI_STATE
        ud=i_dopreview(ud, false, useCS);
    end
elseif ud.ispreviewed
    % Turn off preview as it is now assumed to be invalid
    ud.ispreviewed = false;
    ud = i_switchpreview(ud);
end


% Redraw the preview panel
function ud = i_dopreview(ud, Regen, useCS)
if nargin<2
    Regen = false;
end
if nargin<3
    useCS = false;
end

% Apply the current design settings
proplyt=getcard(ud.cardlay);
propertypage(ud.pointer.info,'apply',proplyt{1});
if Regen
    ud.pointer.info = reset(ud.pointer.info);
end

% Call generate on the candidateset if required.
fig = ud.figure;
oldpointer = get(fig, 'pointer');
set(fig, 'pointer', 'watch')
resetPointer = onCleanup(@()set(fig, 'pointer', oldpointer));
drawnow;
if ~useCS
    ud = generateCS(ud);
end
cs = ud.pointer.info;
requestedPoints = ud.constrainedpoints.Value;

propertypage(ud.pointer.info,'update',proplyt{1},ud.pointer,ud.model);

% get design points
data = constrainCandidateSet(cs, ud);
lims=limits(cs);

if ~isempty(data)
    % check data isn't outside limits and adjust limits if necessary - CCD
    % designs do this
    lims(:,1)=min(lims(:,1),min(data,[],1)');
    lims(:,2)=max(lims(:,2),max(data,[],1)');
end

% invcode data
data=invcode(ud.model,data);
lims=num2cell(invcode(ud.model,lims')',2);
npCon=size(data,1);

if npCon == requestedPoints
    if isempty(ud.constraints)
        ud.pointInformationTxt.String = '';
    else
        ud.pointInformationTxt.String = 'Constraints present';
    end
else
    ud.pointInformationTxt.String = sprintf('Found %d constrained points', npCon);
end

ud.tabbedPreview.setData(data, lims);
ud.ispreviewed = true;


function data = constrainCandidateSet(cs, ud)
data=fullset(cs);
if ~isempty(ud.constraints)
    % apply constraints to points
    [~, in] = isInside( ud.constraints, invcode( ud.model, data ) );
    data=data(in,:);
end


% Enable/Disable the preview panel
function ud = i_switchpreview(ud, TurnOn)
if nargin<2
    TurnOn = ud.autopreview;
end
if TurnOn
    set(ud.previewcards, 'CurrentCard', 1);
else
    set(ud.previewcards, 'CurrentCard', 2);
end


function i_finalise(lyt)
% finalise the cset selection.
% get handle to ud
udh=lyt2udh(lyt);
ud=get(udh,'UserData');
crd=getcard(ud.cardlay);
propertypage(ud.pointer.info,'finalise',crd{1});

% Ensure point set is generated
if ~isGenerated(ud.pointer.info)
    ud = generateCS(ud);
end
set(udh,'UserData', ud);



function h=lyt2udh(lyt)
% return udh for a lyt
el=get(lyt,'elements');
h=el{1};



%%%%%%%%%%%%%
% Callbacks %
%%%%%%%%%%%%%
function i_typechange(~,~,udh)
ud=get(udh,'UserData');
val=get(ud.typepop,'Value');
if ud.cards(val)~=get(ud.cardlay,'currentcard')
    ud.pointer.info=feval(ud.createlist{val},toCandidateSet(ud.pointer.info));
    ud=i_setvalues(ud, npoints(ud.pointer.info));
    set(udh,'UserData',ud);
end


function i_optschange(~,~,udh)
ud=get(udh,'UserData');
if ud.autopreview
    ud = i_dopreview(ud);
elseif ud.ispreviewed
    % Turn off preview
    ud.ispreviewed = false;
    ud = i_switchpreview(ud);
end
set(udh,'UserData',ud);


function i_prevchange(~, ~, udh)
ud=get(udh,'UserData');
DoPrev = get(ud.PreviewCheck, 'Value');
if DoPrev
    ud.autopreview = true;
    ud = i_dopreview(ud, false);
    ud = i_switchpreview(ud);
else
    % Toggle auto-previewing off but leave the current preview still
    % visible
    ud.autopreview = false;
end
set(udh,'UserData',ud);


function i_doprev(~, ~, udh)
ud=get(udh,'UserData');
ud = i_dopreview(ud, true);
ud = i_switchpreview(ud, true);
set(udh,'UserData',ud);


function ud = generateCS(ud)
cs = ud.pointer.info;
requestedPoints = ud.constrainedpoints.Value;
if isempty(ud.constraints)
    cs = set(cs,'N',requestedPoints);
    cs = generate(cs);
else
    try
        [~, cs]=generateConstrainedSpaceFill(ud.design, cs, requestedPoints);
    catch ME
        if ~strcmp(ME.identifier, 'mbc:xregdesign:ExceededMaxIter')
            rethrow(ME)
        end
    end
end
% update the property page
ud.pointer.info = cs;