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

    function [dout,ok]=gui_optimal(d)
%GUI_OPTIMAL  Gui for creating an optimal design
%
%  D=GUI_OPTIMAL(D) creates a gui for creating an optimal design.
 
%  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
 
 
[dout,ok]= i_createfig(d);
 
 
function [d,ok]=i_createfig(d)
 
dlg = mbcgui.container.Dialog('Name','Optimal Design',...
    'Size',[385 530],...
    'HelpCode','xreg_desOptimal',...
    'Resize','off');

fig = dlg.Figure;

ptr=xregGui.RunTimePointer(d);
saved_c=constraints(d);
% remove constraints when layout is created
newDesign=constraints(d,[],0);
ptr.info = newDesign;
dlg.Content = i_createlyt(fig,ptr,saved_c);

dlg.ValidationFcn = @() validateOptimal(dlg,ptr,saved_c);

% store original design with constraints
ptr.info = d;
newDesign=ptr.info;
newDesign=constraints(newDesign,[],0);
ptr.info=newDesign;
tg=dlg.showDialog();
if strcmpi(tg,'ok')
    % push constraints in first to ensure initial design is correct
    newDesign=ptr.info;
    ok = true;
else
    newDesign = d;
    ok = false;
end

delete(dlg);
 
if ok
    try
        % invoke optimisation with a dialog
        [newDesign,ok]=optimisedialog(newDesign);
    catch %#ok<CTCH>
        h = errordlg('Optimization failed.  Check that the initial design is not rank-deficient.', ...
            'Error','modal');
        waitfor(h);
        ok = 0;
    end
    % return newDesign whatever - this keeps settings if they press OK, then Cancel
    if ok
        d=newDesign;
    end
end
delete(ptr);
 
function OK = validateOptimal(dlg,ptr,saved_c)

OK = true;
originalDesign=ptr.info;
newDesign = originalDesign;
newDesign=constraints(newDesign,saved_c,0);
nc = ncand(newDesign,'unconstrained');
if nc>=flintmax
    % Canidate set is too large for indexing
    hError=errordlg(sprintf('The candidate set size is greater than the maximum size of %10.5g points.', flintmax'),...
        'Candidate Set Too Large','modal');
    waitfor(hError);
    OK = false;
elseif nc>1e8 && numConstraints(newDesign)>0
    % warn user that candidate set is very large and it will take a long
    % time to evaluate constraints.
    btn=questdlg(sprintf('Your settings define a large candidate set (%10.5g points). Evaluating constraints over this candidate set may take a long time and could lead to an out of memory error. Do you want to continue?', nc),...
        'Large Candidate Set','Yes','No','No');
    if strcmp(btn,'No')
        OK = false;
    end
end

if OK
    
    % turn on progress bar before evaluating constraints
    usewait = waitbars(newDesign);
    newDesign = waitbars(newDesign,1);
    newDesign = updatestores(newDesign);
    newDesign = waitbars(newDesign,usewait);
    
    % finalise reset gui - check for initial design
    pUD=get(dlg.Content,'UserData');
    ud=pUD.info;    
    %gui_reset needs design stored in runtime pointer
    ptr.info=newDesign;
    OK = gui_reset(newDesign,'finalise',ud.reset);
    if ~OK
        msg=['Not enough feasible points in the candidate set to generate a design to support your model. ',sprintf('\n'),...
            '\bullet Increase the resolution of the candidate set by increasing the number of values for each input. '...
            'You can do this efficiently with a nonregular candidate set, by changing the generation algorithm to a Sobol or Halton sequence.',sprintf('\n'),...
            '\bullet Check that you have not over constrained the design.'];
        h = errordlg(msg,'Optimal Design',struct('WindowStyle','modal','Interpreter','tex'));
        waitfor(h);
        % restore initial design in runtime pointer
        ptr.info = originalDesign;
    end
end 
        
function lyt=i_createlyt(fig,ptr,opt_con)
if nargin<3
    opt_con=des_constraints;
end
 
udptr=xregGui.RunTimePointer;
 
ud.pointer=ptr;
ud.figure=fig;
des=ptr.info;
 
txt(1)=uicontrol('Parent',fig,'Visible','off',...
    'Style','text',...
    'String','Optimality criteria:',...
    'Enable','inactive',...
    'HorizontalAlignment','left');
ud.optpop=uicontrol('Parent',fig,...
    'Visible','off',...
    'Style','popupmenu',...
    'BackgroundColor','w',...
    'String',{'D-Optimal','V-Optimal','A-Optimal'},...
    'Callback',{@i_critchng,udptr});
 
% create tab group
tablyt = uitabgroup(fig, 'Units', 'pixels', 'Tag', 'TabGroup');
tabInitDesign = mbcgui.container.uitab('Parent', tablyt, ...
    'Title', 'Initial Design', ...
    'LayoutBorder', [10,10,10,10],...
    'Tag', 'initialDesign');
tabCandSet = mbcgui.container.uitab('Parent', tablyt, ...
    'Title', 'Candidate Set', ...
    'LayoutBorder', [10,10,10,10],...
    'Tag', 'candidateSet');
tabAlg = mbcgui.container.uitab('Parent', tablyt, ...
    'Title', 'Algorithm', ...
    'LayoutBorder', [10,10,10,10],...
    'Tag', 'algorithm');

% Initial design tab
ud.reset=gui_reset(des,'layout',tabInitDesign,ptr);
set(ud.reset, 'Visible', 'on');
tabInitDesign.LayoutComponent = ud.reset;
% candidate set tab
ud.cand=gui_candspace(des,'layout',tabCandSet,ptr,'callback',{@i_candchange,udptr});
% algorithm tab
ud.alg=gui_optimset(des,'layout',tabAlg,ptr);
set(ud.alg, 'Visible', 'on');
tabAlg.LayoutComponent = ud.alg;
 
% candidate set preview buttons
frm = mbcgui.container.layoutpanel( ...
    'Parent', tabCandSet, ...
    'Title','Display',...
    'Visible','off',...
    'BorderType', 'etchedin', ...
    'LayoutBorder',[10 0 10 5]);
txt(2)=uicontrol('Parent',frm,...
    'Style','text',...
    'String','Display a maximum of',...
    'Enable','inactive',...
    'HorizontalAlignment','left');
txt(3)=uicontrol('Parent',frm,...
    'Style','text',...
    'String','points.',...
    'Enable','inactive',...
    'HorizontalAlignment','left');
ud.Npoints=mbcgui.widget.Spinner('Parent',frm,...
    'Min',0,...
    'Rule','int',...
    'ClickIncrement',50,...
    'Value',2500,...
    'Callback',{@i_NPmaxchange,udptr});
sc=xregGui.SystemColors;
ud.view(1)=uicontrol('Parent',frm,...
    'Style','togglebutton',...
    'CData',replacecolor(xregresload('1dgraph.bmp','bmp'),[0 255 0],sc.CTRL_BACK),...
    'Callback',{@i_vieweronoff,udptr,1},...
    'DeleteFcn',{@i_layoutdel,udptr},...
    'TooltipString','1D projection', ...
    'Tag', '1D projection');
ud.view(2)=uicontrol('Parent',frm,...
    'Style','togglebutton',...
    'CData',replacecolor(xregresload('2dgraph.bmp','bmp'),[0 255 0],sc.CTRL_BACK),...
    'Callback',{@i_vieweronoff,udptr,2},...
    'TooltipString','2D projection', ...
    'Tag', '2D projection');
ud.view(3)=uicontrol('Parent',frm,...
    'Style','togglebutton',...
    'CData',replacecolor(xregresload('3dgraph.bmp','bmp'),[0 255 0],sc.CTRL_BACK),...
    'Callback',{@i_vieweronoff,udptr,3},...
    'TooltipString','3D projection', ...
    'Tag', '3D projection');
ud.view(4)=uicontrol('Parent',frm,...
    'Style','togglebutton',...
    'CData',replacecolor(xregresload('4dgraph.bmp','bmp'),[0 255 0],sc.CTRL_BACK),...
    'Callback',{@i_vieweronoff,udptr,4},...
    'TooltipString','4D projection', ...
    'Tag', '4D projection');
 
ud.candviewfigs=[0 0 0 0];
 
% constraints information
txt(4)=uicontrol('Parent',tabCandSet,...
    'Style','text',...
    'Enable','inactive',...
    'HorizontalAlignment','left',...
    'Visible','off');
NCons=length(opt_con);
if NCons==1
    set(txt(4),'String',sprintf('%d constraint will be applied to this candidate set.',NCons));
else
    set(txt(4),'String',sprintf('%d constraints will be applied to this candidate set.',NCons));
end
 
grd = xreggridbaglayout(frm,'dimension',[5 9],...
    'packstatus','off',...
    'rowsizes',[4 3 15 2 3],...
    'colsizes',[29 29 29 29 15 110 60 5 30],...
    'mergeblock',{[1 5],[1 1]},...
    'mergeblock',{[1 5],[2 2]},...
    'mergeblock',{[1 5],[3 3]},...
    'mergeblock',{[1 5],[4 4]},...
    'mergeblock',{[2 4],[7 7]},...
    'elements',{ud.view(1),ud.view(2),ud.view(3),ud.view(4),[],[],[],[],[];...
    [],[],[],[],[],[],ud.Npoints,[],[];...
    [],[],[],[],[],txt(2),[],[],txt(3);...
    [],[],[],[],[],[],[],[],[];...
    [],[],[],[],[],[],[],[],[]});
set(frm, 'LayoutComponent', {grd});
 
candidateLyt = xreggridlayout(tabCandSet,'correctalg','on',...
    'dimension',[3 1],...
    'elements',{ud.cand,frm,txt(4)},...
    'gapy',10,...
    'rowsizes',[-1 56 15]);
set(candidateLyt, 'Visible', 'on');
tabCandSet.LayoutComponent = candidateLyt;
 
lyt = xreggridbaglayout(fig,'dimension',[5 3],...
    'colratios',[0 2 1],...
    'colsizes',[90 -1 -1],...
    'rowsizes',[3 15 2 10 -1],...
    'gapx',10,...
    'mergeblock',{[1 3],[2 2]},...
    'mergeblock',{[5 5],[1 3]},...
    'elements',{[],txt(1),[],[],tablyt,ud.optpop},...
    'userdata',udptr);
udptr.info = ud;
i_setvalues(udptr);
 
 
function i_setvalues(ptr)
ud=ptr.info;
p=ud.pointer;
optset=getoptimiser(p.info);
val=find(strcmpi(optset,{'d-optimal','v-optimal','a-optimal'}));
set(ud.optpop,'Value',val)
 
 
function i_critchng(~,~,ptr)
ud=ptr.info;
val=get(ud.optpop,'Value');
str=get(ud.optpop,'String');
str=lower(str{val});
ud.pointer.info = setoptimiser(ud.pointer.info,str);
 
 
function i_candchange(~,~,ptr)
ud=ptr.info;
% affects the info display on alg page
gui_optimset(ud.pointer.info,'layout',ud.alg,ud.pointer);
% need to update open viewers
for n=1:length(ud.candviewfigs)
    if ud.candviewfigs(n)
        set(ud.figure,'Pointer','watch');
        previewcand(ud.pointer.info,n,get(ud.Npoints,'Value'),'update',ud.candviewfigs(n));
        set(ud.figure,'Pointer','arrow');
    end
end
 
function i_vieweronoff(~,~,ptr,N)
ud=ptr.info;
if ud.candviewfigs(N)
    %close figure
    delete(ud.candviewfigs(N));
    ud.candviewfigs(N)=0;
else
    %create figure
    set(ud.figure,'Pointer','watch');
    ud.candviewfigs(N)=previewcand(ud.pointer.info,N,get(ud.Npoints,'Value'));
    set(ud.figure,'Pointer','arrow');
    % attach listener for figure deletion.
    set(ud.candviewfigs(N),'DeleteFcn',{@i_delfig,ptr,N});
end
ptr.info=ud;
 
 
function i_delfig(~,~,ptr,N)
ud=ptr.info;
if ~mbcgui.util.isBeingDestroyed(ud.figure)
    ud.candviewfigs(N)=0;
    set(ud.view(N),'Value',0);
    ptr.info=ud;
end
 
 
function i_layoutdel(~,~,ptr)
ud=ptr.info;
% close all open viewers
for n=1:length(ud.candviewfigs)
    if ud.candviewfigs(n)
        delete(ud.candviewfigs(n));
    end
end
 
 
function i_NPmaxchange(~,~,ptr)
ud=ptr.info;
% need to update open viewers
for n=1:length(ud.candviewfigs)
    if ud.candviewfigs(n)
        set(ud.figure,'Pointer','watch');
        previewcand(ud.pointer.info,n,get(ud.Npoints,'Value'),'update',ud.candviewfigs(n));
        set(ud.figure,'Pointer','arrow');
    end
end