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

    function [mainOK, mainMsg, optimoutput] = run(optim, DOWAITBAR)
%RUN Run the optimization
%
%  [MAINOK, MAINMSG, OPTIMOUTPUT] = RUN(OPTIM) runs the optimization. If no
%  error is caught the results are exported to a CGOPTIMOUTPUT object and
%  MAINOK returns TRUE with empty MAINMSG. If an error is caught, MAINOK is
%  false, MAINMSG is a suitable error message and OPTIMOUTPUT is empty.
%
%  [MAINOK, MAINMSG, OPTIMOUTPUT] = RUN(OPTIM, DOWAITBAR) runs the
%  optimization with or without a waitbar.  The default value for DOWAITBAR
%  is false.
%

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


% Initialise return
optimoutput = [];

% perform some pre-run checks
[mainOK, mainMsg] = checkrun(optim, 'fullcheck');
if mainOK
    
    % Decide what scaling paradigm to use
    if optim.Scaled
        optim = scaleOptimItems(optim, 'scaled');
    else
        optim = scaleOptimItems(optim, 'natural');
    end
    
    try
        % wrap cgoptim
        optimrun = cgoptimrunner(optim);
    catch E
        if strcmp(E.identifier, 'mbc:cgoptimrunner:InvalidInterfaceState')
            mainOK = false;
            mainMsg = E.message;
            return
        else
            rethrow(E)
        end  
    end

    if nargin<2
        DOWAITBAR = false;
    end
    [mainOK, mainMsg, OK, freevals, msg, output,selectedsol] = run(optimrun, DOWAITBAR);
    
    % Always reset the scaling back to natural after optim
    optim = scaleOptimItems(optim, 'natural');
    
    if mainOK
        % Save output data
        optimoutput = cgoptimoutput(optim, freevals, msg, OK, output,selectedsol);
    end
end