www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/mbcOSpsearch.m
function out = mbcOSpsearch(action, in) %MBCOSPSEARCH CAGE Optimization library function % % MBCOSPSEARCH contains the configuration and evaluation routines for the % CAGE Optimization, PATTERNSEARCH. This function is called by CAGE using % the syntaxes below. % % OPTOBJ = MBCOSPSEARCH('OPTIONS', OPTOBJ) is called by CAGE when a % PATTERNSEARCH optimization is created in CAGE. This call defines the % structure of the optimization problem, using a CGOPTIMOPTIONS object. % The completed configuration for the Optimization is returned to CAGE % via OPTOBJ. % % OPTIMSTORE = MBCOSPSEARCH('EVALUATE', OPTIMSTORE) is called by CAGE % when a user runs a PATTERNSEARCH optimization. Optimization parameters % are passed to the evaluation routine through OPTIMSTORE. The results of % the optimization are stored in the OPTIMSTORE object which is returned % to CAGE for further processing. % % See also MBCOSNBI, MBCOSFMINCON, MBCOSGA % Copyright 2006-2012 The MathWorks, Inc. % Deal with the action inputs if strcmp(action, 'options') options = in; % Add a name options = setName(options, 'patternsearch'); % Add a description options = setDescription(options, ... 'Single objective optimization using a pattern search algorithm'); % Allow objectives and constraints to be renamed options = setRenameMode(options, true); % Set up the operating point sets %%%% Any number of operating point sets are allowed %%%% options = setOperatingPointsMode(options, 'any'); % Determine whether Global Optimization is available hasGADSToolbox = mbcCheckForToolbox(... 'Global Optimization', ... 'gads_toolbox', 'globaloptim'); options = setEnabled(options, hasGADSToolbox); % Set up the optimization parameters options = addParameter(options, 'Display', ... {'list', {'none', 'final', 'iter', 'diagnose'}}, 'none'); options = addParameter(options, 'TimeLimit', ... {'number', 'positive'}, Inf, 'Time limit'); options = addParameter(options, 'MaxIter', {'integer', 'positive'}, ... 100, 'Maximum number of iterations'); options = addParameter(options, 'MaxFunEvals', {'integer', 'positive'}, ... 100000, 'Maximum function evaluations'); options = addParameter(options, 'TolX', {'number', 'positive'}, ... 1e-6, 'Variable tolerance'); options = addParameter(options, 'TolFun', {'number', 'positive'}, ... 1e-6, 'Function tolerance'); options = addParameter(options, 'TolCon', {'number', 'positive'}, ... 1e-6, 'Constraint tolerance'); options = addParameter(options, 'TolMesh', {'number', 'positive'}, ... 1e-6, 'Mesh size tolerance'); options = addParameter(options, 'InitialMeshSize', ... {'number', 'positive'}, 1, 'Initial mesh size'); options = addParameter(options, 'PollMethod', {'list', ... {'GPSPositiveBasis2N', 'GPSPositiveBasisNp1', ... 'MADSPositiveBasis2N', 'MADSPositiveBasisNp1'}}, ... 'GPSPositiveBasis2N', 'Poll method'); options = addParameter(options, 'SearchMethod', {'list', ... {'none', 'MADSPositiveBasisNp1', 'MADSPositiveBasis2N', ... 'GPSPositiveBasisNp1', 'GPSPositiveBasis2N', ... 'searchlhs', 'searchneldermead', 'searchga'}},... 'none', 'Search method'); out = options; elseif strcmp(action, 'evaluate') optimstore = in; optimstore = i_Evaluate(optimstore); out = optimstore; else error(message('mbc:mbcOSpsearch:InvalidArgument')); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function optimstore = i_Evaluate(optimstore) % Ensure that Global Optimization is available [canCheckOutTlbx, errmsg] = mbcCheckForToolbox(... 'Global Optimization', ... 'gads_toolbox', 'globaloptim', true); if ~canCheckOutTlbx error('mbc:mbcOSpsearch:InvalidState', errmsg); end % Set the functions PollMethod = getParam(optimstore, 'PollMethod'); SearchMethodStr = getParam(optimstore, 'SearchMethod'); if strcmp(SearchMethodStr, 'none') SearchMethod = []; else SearchMethod = str2func(SearchMethodStr); end % Set the other variables Display = getParam(optimstore, 'Display'); TimeLimit = getParam(optimstore, 'TimeLimit'); MaxFunEvals = getParam(optimstore, 'MaxFunEvals'); TolX = getParam(optimstore, 'TolX'); TolFun = getParam(optimstore, 'TolFun'); TolCon = getParam(optimstore, 'TolCon'); TolMesh = getParam(optimstore, 'TolMesh'); MaxIter = getParam(optimstore, 'MaxIter'); InitialMeshSize = getParam(optimstore, 'InitialMeshSize'); % Get linear constraints A = getA(optimstore); B = getB(optimstore); % Get the bounds and initial conditions. Transpose all vectors to make them % consistent for scalar variables with patternsearch. lb = getLB(optimstore); ub = getUB(optimstore); x0 = getInitFreeVal(optimstore); % Put them all into a psoptions structure options = psoptimset('Display', Display, 'TolFun', TolFun, ... 'TolCon', TolCon, 'TolMesh', TolMesh, 'MaxIter', MaxIter, ... 'InitialMeshSize', InitialMeshSize, 'PollMethod', PollMethod, ... 'SearchMethod', SearchMethod, 'CompletePoll', 'off', ... 'CompleteSearch', 'off', 'Cache', 'on', ... 'MeshAccelerator', 'on', 'Vectorized', 'on', ... 'PollingOrder', 'success', ... 'TimeLimit', TimeLimit, 'TolX', TolX, ... 'MaxFunEvals', MaxFunEvals, 'OutputFcns', @i_Output); % Workaround issue in patternsearch. If all the bounds are infinite % and A, B are empty, patternsearch will still pass to the bound % constrained algorithm. In our view, it should pass to the % unconstrained algorithm in this case. if all(lb == -Inf) lb = []; end if all(ub == Inf) ub = []; end % Call optimization algorithm. Workaround issue in patternsearch. If a % function handle to nonlinear constraints is specified then patternsearch % will pass to the nonlinear constrained algorithm, even if the nonlinear % constraint function returns no constraints. It should not pass to the % nonlinear contrained algorithm in this case (it is currently much slower % than the other algorithms). [c, ceq] = i_evalCon(lb); if isempty(c) && isempty(ceq) [bestx, bestfun, exitflag, output] = patternsearch(@i_evalObj, x0, ... A, B, [], [], lb, ub, [], options); else [bestx, bestfun, exitflag, output] = patternsearch(@i_evalObj, x0, ... A, B, [], [], lb, ub, @i_evalCon, options); end % Write the results to optimstore. Transpose the free variables back to % 1-by-NFREEVAR vector, optimstore = setFreeVariables(optimstore, bestx); % Set all the information in optimstore optimstore = setExitStatus(optimstore, exitflag, output.message); output = rmfield(output, 'message'); output = rmfield(output, 'function'); output = rmfield(output, 'searchmethod'); optimstore = setOutput(optimstore, output); function y = i_evalObj(x) % Evaluate the objective function. Transpose x as it is provided in % the form NFREEVAR-by-NPTS for pattern search. y = evaluateObjective(optimstore, x); ObjectiveFuncTypes = getObjectiveType(optimstore); switch ObjectiveFuncTypes{1} case 'max' y = -y; case 'min' otherwise error('mbc:mbcOSpsearch:InvalidState', ... strcat('This optimization routine can only deal with',... 'objective functions to be minimized or maximized')); end end function [c, ceq] = i_evalCon(x) % Nonlinear inequality constraints. Transpose x as it is provided in % the form NFREEVAR-by-NPTS for pattern search. c = evaluateNonlcon(optimstore, x); % Nonlinear equality constraints currently not supported ceq = []; end function [stop, options, optchanged] = i_Output(unused1, options, ... unused2, unused3) stop = getStopState(optimstore); optchanged = false; end end