www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/+cgcalsetup/OptimPage.m
classdef OptimPage < cgcalsetup.WizardPage %OptimPage wizard page for setting up an optimization from a model % % cgcalsetup.OptimPage methods: % updatePage - update wizard page with data % updateData - update data from wizard page % createPage - create page for wizard (Static) % getNextPage - function handle for next page % Copyright 2009-2015 The MathWorks, Inc. and Ford Global Technologies, Inc. properties (Access=protected) %hObjectivePopup 'Minimize' or 'Maximize' popup control hObjectivePopup %hObjectiveSum 'Point' or 'Sum' pop up control hObjectiveSum %hAlgorithmPopup popup to select optimization algorithm hAlgorithmPopup %hDataType popup to select data type % % DataType is {'Set point','Table grid','Data set'} % or 'Model operating points' for point-by-point models hDataType %hDataList popup to select data source for table grid or data sets hDataList %hFreeVarLabel label for free variable table. This label also %displays the number of free variables selected hFreeVarLabel %hFreeVarTable java table with check boxes to select free variables hFreeVarTable %hConstraintCheck check box to add a boundary constraint hConstraintCheck %hListeners store for listeners hListeners end methods function updateData(OPT) %UPDATEDATA update OptimData from OptimPage wizard C = OPT.Data; ObjIdx = get(OPT.hObjectivePopup, 'Value'); ObjectiveTypes = get(OPT.hObjectivePopup, 'String'); idxFree = OPT.hFreeVarTable.getChecks; AlgName = get(OPT.hAlgorithmPopup, 'Value'); AlgorithmOptions = get(OPT.hAlgorithmPopup, 'String'); AddBoundary = get(OPT.hConstraintCheck, 'Value'); IsSumObj = get(OPT.hObjectiveSum,'Value')==2; C.IsFree = idxFree; C.ObjectiveType = ObjectiveTypes{ObjIdx}; C.Algorithm = AlgorithmOptions{AlgName}; C.IsSumOptim = IsSumObj; C.BoundaryConstraint = AddBoundary; % data type C.DataType = C.DataTypeList{get(OPT.hDataType,'Value')}; switch C.DataType case 'Table grid' C.DataSource = C.Tables( get(OPT.hDataList,'Value') ); case 'Data set' C.DataSource = C.Datasets( get(OPT.hDataList,'Value') ); end end function updatePage(OPT,iFace) %UPDATEPAGE update wizard page from OptimData C = OPT.Data; pInputs = C.pInputs; % table for free variables % varNames = pveceval(pInputs, @getname); OPT.hFreeVarTable.Items = pInputs; OPT.hFreeVarTable.setChecks(C.IsFree); checkFreeVariables(OPT,iFace) set(OPT.hFreeVarLabel,'String', sprintf('Free variables:\n%d selected',sum(C.IsFree))) set(OPT.hObjectiveSum,'Value',C.IsSumOptim+1); if concheck(C.Model) set(OPT.hConstraintCheck, 'Value', C.BoundaryConstraint, 'Enable', 'on'); else set(OPT.hConstraintCheck, 'Value', 0, 'Enable', 'off'); end %data source setDataOptions(OPT) InfoStr = sprintf('Choose optimization type and select free variables to optimize %s.',getname(C.Model)); OPT.hInfoPane.InfoString = InfoStr; end end methods (Access=protected) function OPT = OptimPage(fh,iFace,C) OPT.Data = C; OPT.Layout = createLayout(OPT,fh,iFace); updatePage(OPT,iFace) end function MainLyt= createLayout(OPT,hFig,iFace) % Standard colors SC = xregGui.SystemColorsDbl; editbg = SC.WINDOW_BG; % Selecting algorithm AlgorithmOptions = cgcalsetup.OptimData.getOptimAlgorithms; OPT.hAlgorithmPopup = uicontrol('Parent', hFig, ... 'Style', 'popupmenu', ... 'String', AlgorithmOptions, ... 'BackgroundColor', editbg, ... 'Tag', 'algorithmPopup', ... 'HorizontalAlignment', 'left',... 'Callback',{@cbkAlgorithm,OPT,iFace}); hAlgorithmLabel = xregGui.labelcontrol('parent', hFig, ... 'String', 'Algorithm:', ... 'LabelSizeMode', 'absolute', ... 'ControlSizeMode', 'absolute', ... 'LabelSize', 100, ... 'ControlSize', 150, ... 'Control', OPT.hAlgorithmPopup); % Selecting objective type ObjectiveTypes = {'Minimize','Maximize'}; OPT.hObjectivePopup = uicontrol('Parent', hFig, ... 'Style', 'popup', ... 'String', ObjectiveTypes, ... 'BackgroundColor', editbg, ... 'Tag', 'objType1', ... 'HorizontalAlignment', 'left'); hObjectiveLabel = xregGui.labelcontrol('parent', hFig, ... 'String', 'Objective type:', ... 'LabelSizeMode', 'absolute', ... 'ControlSizeMode', 'absolute', ... 'LabelSize', 100, ... 'ControlSize', 150, ... 'Control', OPT.hObjectivePopup); OPT.hObjectiveSum = uicontrol('Parent', hFig, ... 'Style', 'popup', ... 'String', {'Point','Sum'}, ... 'BackgroundColor', editbg, ... 'Tag', 'objType2', ... 'HorizontalAlignment', 'left'); OPT.hDataType = uicontrol('Parent', hFig, ... 'Style', 'popup', ... 'String', {'temp'}, ... 'BackgroundColor', editbg, ... 'HorizontalAlignment', 'left',... 'Tag', 'dataSource', ... 'Callback',{@cbkDataType,OPT,iFace}); hDataLabel = xregGui.labelcontrol('parent', hFig, ... 'String', 'Data source:', ... 'LabelSizeMode', 'absolute', ... 'ControlSizeMode', 'absolute', ... 'LabelSize', 100, ... 'ControlSize', 150, ... 'Control', OPT.hDataType); OPT.hDataList = uicontrol('Parent', hFig, ... 'Style', 'popup', ... 'String', {'temp'}, ... 'BackgroundColor', editbg, ... 'Tag', 'dataList', ... 'HorizontalAlignment', 'left',... 'Callback',{@cbkSelectData,OPT,iFace}); % Selecting free variables OPT.hFreeVarTable = cgtools.exprList('Parent', hFig,... 'DisplayChecks',true,... 'DisplayTypeColumn',false,... 'SelectionStyle','None',... 'ItemHeaderText','Variable'); OPT.hFreeVarLabel = uicontrol('Parent', hFig, ... 'Style', 'text', ... 'String', 'Free variables:', ... 'HorizontalAlignment', 'left'); tabLyt = xreggridbaglayout(hFig, ... 'dimension', [1 2], ... 'colsizes', [100 -1], ... 'gapx',2,... 'elements', {OPT.hFreeVarLabel, OPT.hFreeVarTable}); % Create boundary constraint from model OPT.hConstraintCheck = uicontrol('Parent', hFig, ... 'Style', 'checkbox', ... 'String', 'Add a model boundary constraint', ... 'Tag','BoundaryConstraint',... 'HorizontalAlignment', 'left'); MainLyt = xreggridbaglayout(hFig, ... 'dimension', [5 3], ... 'gapy', 5, ... 'rowsizes', [20 20 20 -1 20], ... 'colsizes', [250 150 -1], ... 'mergeblock', {[4 4], [1 2]}, ... 'mergeblock', {[5 5], [1 3]}, ... 'border', [7 2 7 4], ... 'elements', {hAlgorithmLabel, hObjectiveLabel, hDataLabel, tabLyt OPT.hConstraintCheck, ... [], OPT.hObjectiveSum, OPT.hDataList, [], [],... [],[],[],[],[]}); OPT.hInfoPane = mbcgui.container.InfoPane('Parent', hFig, ... 'InfoHeight', 35, ... 'Title','Optimization',... 'Center', MainLyt); MainLyt = xreglayerlayout(hFig, 'Elements', {OPT.hInfoPane}); OPT.hListeners = handle.listener(OPT.hFreeVarTable.Display, 'ValueChanged',{@iEditVariables,OPT,iFace}); end function setDataList(OPT) %setDataList set data type and source controls for optimization %page C = OPT.Data; val = get(OPT.hDataType,'Value'); Types= get(OPT.hDataType,'String'); List = {''}; Value = 1; Enable = 'off'; switch Types{val} case 'Data set' D = C.Datasets; if ~isempty(D) List = pveceval(D,@getname); if isempty(C.DataSource) || ~any(C.DataSource==D) Value = 1; else Value = find(C.DataSource==D); end Enable = 'on'; end case 'Table grid' T = C.Tables; if ~isempty(T) List = cell(size(T)); for i=1:length(T) Inputs = mbcListString(pveceval(T(i).getinports,@getname)); List{i} = sprintf('%s(%s)',T(i).getname,Inputs); end if isempty(C.DataSource) || ~any(C.DataSource==T) Value = 1; else Value = find(C.DataSource==T); end Enable = 'on'; end end set(OPT.hDataList,... 'String',List,... 'Value',Value,... 'Enable',Enable) end function setDataOptions(OPT) %set up data type popup menu DataTypes = OPT.Data.DataTypeList; DataValue = find(strcmp(OPT.Data.DataType,DataTypes)); set(OPT.hDataType,'String',DataTypes,'Value',DataValue); setDataList(OPT); end function checkFreeVariables(OPT,iFace) %checkFreeVariables check the number of free variables idxFree = OPT.hFreeVarTable.getChecks; set(OPT.hFreeVarLabel,... 'String', sprintf('Free variables:\n%d selected',sum(idxFree))) if nargin>1 % can't create an optimization with no free variables. feval(iFace.setFinishButton, any(idxFree)); end end end methods (Static) function layout = createPage( fh, iFace,C ) %createPage create optimization wizard page % % layout = createPage( fh, iFace,C ) if isa(fh, 'xregcontainer') OPT = get(fh,'UserData'); updatePage(OPT,iFace) else OPT = cgcalsetup.OptimPage(fh,iFace,C); end feval(iFace.setNextButton, 0); %enable finish if there are any free variables set feval(iFace.setFinishButton, any(C.IsFree)); layout = OPT.Layout; end end end function cbkDataType(~,~,OPT,iFace) %select data source from popup menu setDataList(OPT) cbkSelectData([],[],OPT,iFace); end function cbkSelectData(~,~,OPT,iFace) %select data set or table from popup menu val = get(OPT.hDataType,'Value'); Types= get(OPT.hDataType,'String'); switch Types{val} case 'Table grid' %remove table inputs from free variables val = get(OPT.hDataList,'Value'); T = OPT.Data.Tables(val); iSelectTable(OPT,T) end checkFreeVariables(OPT,iFace); end function iSelectTable(OPT,pT) % turn off table inputs C = OPT.Data; idxFree = OPT.hFreeVarTable.getChecks; pTabInputs = getinports(pT.info); idxFree(ismember(C.pInputs,pTabInputs))= false; OPT.hFreeVarTable.setChecks(idxFree); end function iEditVariables(~,~,OPT,iFace) %free variable check callback checkFreeVariables(OPT,iFace); end function cbkAlgorithm(h,~,OPT,iFace) options = get(h,'String'); C = OPT.Data; if isMultiModal(C.Model) idxFree = OPT.hFreeVarTable.getChecks; [~,pInp]= getSwitchModel(C.Model); if strcmp(options{get(h,'Value')},'Modal optimization') % select mode variable idxFree(ismember(C.pInputs,pInp(end)))= true; if length(pInp)>1 % deselect other switch factors idxFree(ismember(C.pInputs,pInp(1:end-1)))= false; end % modal optimization only works for point optimizations set(OPT.hObjectiveSum,'Value',1); else % deselect mode variable idxFree(ismember(C.pInputs,pInp(end)))= false; end OPT.hFreeVarTable.setChecks(idxFree); checkFreeVariables(OPT,iFace); end end