www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimrunner/private/pRunv3DCT.m
function [mainOK, mainmsg, OK, freevals, msg, stats,selectedsol] = ... pRunv3DCT(obj, DOWAITBAR, waitH) %PRUNV3DCT Run the optimization in DCT mode using version 3 interface. % % [MAINOK, MAINMSG, OK, FREEVALS, MSG, STATS] = PRUNV3DCT(OBJ) runs the % optimization assuming the user-function is written for MBC version 3 % optimization interfaces. The optimization uses the DCT option to % distribute runs to a cluster of computers. % Copyright 2006-2015 The MathWorks, Inc. NRuns = length(obj.RunIndices); mainOK = true; mainmsg = ''; OK = cell(1, NRuns); msg = cell(1, NRuns); % Set up waitbar correctly if DOWAITBAR if NRuns>1 waitH.waitbar.min = 0; waitH.waitbar.max = NRuns; else % turn off waitbar waitH.waitbar.visible = 'off'; end end % Get the DCT manager used for MBC and make sure it is connected to the % scheduler if DOWAITBAR waitH.Message = 'Creating parallel tasks...'; end % store optimrunner on workers OptimRunnerOnWorkers = parallel.pool.Constant(obj); for n = 1:NRuns % set up future jobs on workers f(n) = parfeval(@iPCTrun,7,obj.FunctionName,n,OptimRunnerOnWorkers ); %#ok<AGROW> end % Retrieve the results from all of the successful tasks freevalcell = cell(1, NRuns); statcell = cell(1, NRuns); selectedsol = cell(1, NRuns); if DOWAITBAR waitH.waitbar.value = 0; end DefaultFreeVals = getInitialFreeValues(obj, 'all'); stopped = false; hasRun = false(1,NRuns); if DOWAITBAR waitH.Message = 'Running optimizations in parallel...'; end for n=1:NRuns % get results from workers try [idx,Data{1:7}] = fetchNext(f); catch E mainOK = false; mainmsg = E.message; break end mainOK = Data{1}; mainmsg = Data{2}; OK{idx} = Data{3}; msg{idx} = Data{4}; statcell{idx} = Data{5}; freevalcell{idx} = Data{6}; selectedsol{idx} = Data{7}; if DOWAITBAR waitH.waitbar.value = n; drawnow update stopped = waitH.StopState || waitH.CancelState; end if ~mainOK || stopped % cancel parfeval job cancel(f); break end hasRun(idx) = true; end if stopped for idx=find(~hasRun); OK{idx} = -1; msg{idx} = 'Optimization not run'; freevalcell{idx} = DefaultFreeVals(idx,:); end end freevals = cell(1, length(obj.FreeVariableIndices)); stats = []; if mainOK % expand solutions to the same size [freevalcell,OK,msg]= pSquareUpMultiSolns(freevalcell,OK,msg); stats = pStatsCell2Struct(statcell); freevals = pRunCell2VarCell(obj, freevalcell); end function [OK, ErrMsg, exitflag, exitmsg, stats, freevalmatrix,selectedsol] = iPCTrun(FunctionName, n,OptimRunnerOnWorkers ) % Evaluate the optimization routine and extract the results for return. try % get optimrunner from parallel.pool.Constant obj = OptimRunnerOnWorkers.Value; thisObj = nextRun(obj,n); cos = cgoptimstore(thisObj); % run optimisation cos = feval(FunctionName, 'evaluate', cos); OK = true; ErrMsg = ''; [exitflag, exitmsg, stats] = getOutputInfo(cos); [freevalmatrix,selectedsol] = getFreeVariables(cos); catch E OK = false; exitflag = 0; exitmsg = ''; ErrMsg = sprintf('%s\n\n%s\nat %s(%d)', ... 'An error occurred while running the optimization function:', ... E.message,E.stack(1).file,E.stack(1).line); E.getReport stats = []; freevalmatrix = []; selectedsol = []; end