www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimrunner/run.m

    function [mainOK, mainmsg, OK, freevals, msg, stats,selectedsol] = run(obj, DOWAITBAR)
%RUN Run the optimization
%
%  [MAINOK, MAINMSG, OK, VALS, MSG, STATS] = RUN(OBJ) runs the optimization
%  over all of the run indices that have been specified and returns the
%  results.  MAINOK is a boolean flag indicating whether the optimization
%  ran without error. If MAINOK is false, MAINMSG returns an error message.
%  OK is a (NRUNS-by-1) vector of integers, indicating the termination
%  status of each run. VALS is a (NRUNS-by-NFREEVAR) cell array with each
%  cell containing the set of solutions that were found for the free
%  variable at that run.  MSG is an (NRUNS-by-1) cell array of strings that
%  can be used to indicate problems.  STATS is a (NRUNS-by-1) structure
%  array containing statistics for each optimization run.
%
%  [MAINOK, MAINMSG, OK, VALS, MSG, STATS] = RUN(OBJ, DOWAITBAR) runs the
%  optimization with or without a waitbar.  The default setting is false.

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


if nargin<2
    DOWAITBAR = false;
end

% Create the waitbar if required
if DOWAITBAR
    DLGTITLE = 'Running Optimization';
    waitH = xregGui.waitdlg('title',DLGTITLE,...
        'message','Optimizing... Please wait.',...
        'ShowCancelButton',true,...
        'ShowStopButton',true);
    
    % add listener to stop button so we can react
    hStopList = handle.listener(waitH,waitH.findprop('StopState'),'PropertyPostSet',@i_Stop);
    hCancelList = handle.listener(waitH,waitH.findprop('CancelState'),'PropertyPostSet',@i_Stop);

    drawnow;
else
    waitH= [];
end

% Set up the object to run from the beginning
obj.CurrentRun = 0;
setStopState(cgoptimstore,false);

% Decide which private function to call to execute the optimization run
RunVer = getRunAPIVersion(obj);
selectedsol = [];
if RunVer==2
    [mainOK, mainmsg, OK, freevals, msg, stats] = pRunv2(obj, DOWAITBAR, waitH);
else
    NRuns = length(obj.RunIndices);
    if NRuns>1 && mbcfoundation.DCTManager.isParpoolOpen
        % always run in parallel if there are more than 1 run and their is
        % a valid parpool (PCT available and windows)
        [mainOK, mainmsg, OK, freevals, msg, stats,selectedsol] = pRunv3DCT(obj, DOWAITBAR, waitH);
    else
        [mainOK, mainmsg, OK, freevals, msg, stats,selectedsol] = pRunv3(obj, DOWAITBAR, waitH);
    end
end

% Delete the waitbar
if DOWAITBAR
    if waitH.CancelState
       mainOK = false;
       mainmsg = '';
    end
    
    delete(hStopList)
    delete(waitH);
    delete(hCancelList);
end


%--------------------------------------------------------------------------
function i_Stop(~,~)
% set stop state for optimstore. Individual scripts can query this state
% during operation (e.g. in an output function) and react appropriately
setStopState(cgoptimstore,true);