www.gusucode.com > sloptim工具箱matlab源码程序 > sloptim/sloptguis/@srocsdgui/@sropnl/optimize.m

    function optimize(this,Action)
% OPTIMIZE  method to run optimization defined by GUI
%

% Author(s): A. Stothert 08-Sep-2005
% Copyright 2005-2011 The MathWorks, Inc.

%Get progress and display handles
hOptimJava = this.hOptimization.getJavaHandle;

if strcmpi(Action,'start')
    %Update GUI to reflect running status
    this.updateStatus(true)
    
    if ~isempty(this.hProject) && ishandle(this.hProject)
        this.hProject.delete
        this.hProject = [];
    end
    %Construct the optimization problem
    %REVISIT: Work around JEditorPanel font bug
    hOptimJava.getEditor.setText(sprintf('<font face=''sans serif'' size=2>'))
    hOptimJava.getEditor.append(sprintf('%s<BR>',getString(message('Sloptim:srosisotool:lblConstructingProblem'))))
    awtinvoke( hOptimJava.getIterationTable.getModel, 'setRowCount(I)', 0 )
    try
        this.hProject = this.evalForm;
    catch E
        if ishandle(this)
            this.updateStatus(false);
            com.mathworks.mwswing.MJOptionPane.showMessageDialog(slctrlexplorer,...
                sroengine.ut_stripErrorString(E.message),...
                getString(message('Sloptim:srosisotool:lblResponseOptimization')),com.mathworks.mwswing.MJOptionPane.ERROR_MESSAGE);
        end
        return
    end
    
    %Create locally scoped listener to update editors/ analysis plots
    L = handle.listener(this.hProject.Spec(1),'OptimUpdate',@(hSrc,hData) localUpdate(this)); %#ok<NASGU>
    
    %Turn off any Tuned parameter listeners
    this.hParameters.disableListeners(true);
    
    %Turn off Simulink auto update
    SimulinkAutoUpdate = false;
    if isa(this.up,'ControlDesignNodes.SISODesignConfiguration')
        if strcmp(this.up.getAutoUpdateListenerEnabled,'on')
            this.up.setAutoUpdateListenerEnabled('off')
            SimulinkAutoUpdate = true;
        end
    end
    
    %Create transaction to record optimization
    this.Transaction = controllib.app.managers.eventmanager.internal.FunctionTransaction(getString(message('Sloptim:srosisotool:lblOptimizeCompensators')));
    %Get current parameter values
    OriginalValues = copy(this.hProject.Specs(1).getModel.getValue(this.hProject.Parameters));
    %Create undo and redo functions for the transaction
    this.Transaction.UndoFcn = {@localUndo this OriginalValues};
    this.Transaction.RedoFcn = {@localRedo this};
    
    %Bring design plots to forefront
    localShowPlots(this)
    
    %Run optimization problem
    try
        this.hProject.optimize(this.hOptimization);
    catch E
        if ishandle(this)
            % Return to idle (to re-enable Start button)
            this.hProject.OptimStatus = 'idle';
            % Post error
            if ~strcmp(E.identifier,'MATLAB:interrupt')
                com.mathworks.mwswing.MJOptionPane.showMessageDialog(slctrlexplorer,...
                    sroengine.ut_stripErrorString(E.message),...
                    getString(message('Sloptim:srosisotool:lblResponseOptimization')),com.mathworks.mwswing.MJOptionPane.ERROR_MESSAGE);
            end
        else
            return;
        end
    end
    
    % Commit and stack transaction
    if ~isempty(this.Transaction)
        EventMgr = this.EventManager;
        EventMgr.record(this.Transaction);
        this.Transaction = [];
    end
    
    %REVISIT: work around JEditorPane font bug
    %Stop end font setting in progress window
    hOptimJava.getEditor.append(sprintf('</font>'))
    
    %Turn on any Tuned parameter listeners
    this.hParameters.disableListeners(false);
    
    %Turn on Simulink auto update if it was previously on
    if SimulinkAutoUpdate
        this.up.setAutoUpdateListenerEnabled('on')
        %Push final results to Simulink
        this.up.WriteToSimulinkModel;
    end
    
    %Perform any post optimization GUI synchronization
    this.postOptimSync;
    
    %Update GUI to reflect stopped status
    this.updateStatus(false)
    
    % Post status message
    this.EventManager.postActionStatus('off', getString(message('Control:designerapp:notifyOptimizationTuning')));
    
    %Force tunedparameters panel update
    this.hParameters.updateTable;
    
end
if strcmpi(Action,'stop')
    % Commit and stack transaction
    if ~isempty(this.Transaction)
        EventMgr = this.EventManager;
        EventMgr.record(this.Transaction);
        this.Transaction = [];
    end
    
    %Stop optimization problem
    this.hProject.OptimStatus = 'stop';  %will cause running optimization to stop, line 62 above
end

%--------------------------------------------------------------------------
function localUpdate(this)
%Callback to update display during optimization

this.updateDiagrams;

%--------------------------------------------------------------------------
function localUndo(this,OriginalValues)
%Undo operation to restore original parameter values

if ishandle(this)
    this.EventManager.postActionStatus('off',getString(message('Controllib:gui:strReverted',...
        getString(message('Control:designerapp:notifyOptimizationTuning')))));
    this.undoOptimize(OriginalValues)
else
    warnStr = ctrlMsgUtils.message('Sloptim:srosisotool:warnUndoDeletedProject');
    com.mathworks.mwswing.MJOptionPane.showMessageDialog(slctrlexplorer,warnStr,...
        getString(message('Sloptim:srosisotool:lblResponseOptimization')),com.mathworks.mwswing.MJOptionPane.WARNING_MESSAGE);
end

%--------------------------------------------------------------------------
function localRedo(this)
%Redo operation to perform optimization

if ishandle(this)
    %Switch to Response optimization node
    this.EventManager.postActionStatus('off',getString(message('Control:designerapp:notifyOptimizationTuning')));
    openTool(this.ToolsManager, 'SRO', [])
    updateStatus(this,true)
    if this.getStatus
        %Start optimization
        optimize(this,'start')
    end
end

%--------------------------------------------------------------------------
function localShowPlots(this)
%Local function to bring Design plots to the foreground

% sisodb = this.hsisodb;
% if ishghandle(sisodb.Figure,'figure')
%     figure(double(sisodb.Figure));
% end
% if ~isempty(sisodb.DesignTask.AnalysisPlotConfig)
%    sisodb.DesignTask.AnalysisPlotConfig.showViewer(false);
% end
%
% %Force figures to update
% drawnow