www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/mbcOSNBI.m
function out = mbcOSNBI(action, in) % MBCOSNBI CAGE Optimization library function % % MBCOSNBI contains the configuration and evaluation routines for the % CAGE Optimization, NBI. This function is called by CAGE using the % syntaxes below. % % OPTOBJ = MBCOSNBI('OPTIONS', OPTOBJ) is called by CAGE when an % NBI 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 = MBCOSNBI('EVALUATE', OPTIMSTORE) is called by CAGE % when a user runs an NBI 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 MBCOSFMINCON % Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Deal with the two action cases %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% switch lower(action) case 'options' % Return the updated options object out = i_Options(in); case 'evaluate' % Return a handle to the main evaluation routine out = i_Evaluate(in); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % options Function %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function opt = i_Options(opt) % Set the evaluation function interface to always be the latest one opt = setRunInterfaceVersion(opt, 0); % Add a name opt = setName(opt, 'NBI'); % Add a description opt = setDescription(opt, 'Normal Boundary Intersection Algorithm'); % Set up the free variables %%%% Any number are allowed %%%% % Set up the objective functions opt = setObjectivesMode(opt, 'multiple'); % Set up the constraints %%%% Any number of constraints are allowed %%%% % Allow objectives and constraints to be renamed opt = setRenameMode(opt, true); % allow equality constraints opt = setEqConMode(opt,'on'); % Set up the operating point sets %%%% Any number of operating point sets are allowed %%%% opt = setOperatingPointsMode(opt, 'any'); % Set up the optimization parameters options = xregoptmgr(@omnbi,cgoptimstore); opt = addParameter(opt, options); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Evaluation Function %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function optimstore = i_Evaluate(optimstore) % Optimization routine must conform to the following API % optimstore = <OPTIMIZATION_FUNCTION>(optimstore) % % Inputs: optimstore: store of all the CAGE data needed % Outputs: optimstore: updated object containing all the CAGE data, including outputs % % Number of objectives csum = cumsum(getNumObjectives(optimstore)); csum = [0, csum]; % Get inputs for NBI x0 = getInitFreeVal(optimstore); NumberOfObjectives = sum(getNumObjectives(optimstore)); NumberOfPoints = getParam(optimstore, 'NumberOfPoints'); A = getA(optimstore); B = getB(optimstore); lb = getLB(optimstore); ub = getUB(optimstore); GRADREQ = ~isScalarFreeVariables(optimstore); NBIoptions = i_NBI_om2options; % Display run number if ismember(NBIoptions.ShadowOptions.Display, {'final', 'iter'}) || ... ismember(NBIoptions.NBISubproblemOptions.Display, {'final', 'iter'}) runNo = getRunIndex(optimstore); fprintf('\n\nRun %d\n', runNo); end % Call NBI [bestx, unused, exitFlag, OUTPUT] = cgnbi(@i_evalObj, x0', NumberOfObjectives, ... NumberOfPoints, A, B, [], [], lb', ub', @i_evalCon, NBIoptions); % Save the results optimstore = setFreeVariables(optimstore, bestx'); optimstore = setExitStatus(optimstore, exitFlag, OUTPUT.message); OUTPUT = rmfield(OUTPUT, 'message'); optimstore = setOutput(optimstore, OUTPUT); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Options plus objective and constraint evaluation % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [y, yg] = i_evalObj(x, funcindex) yg = []; objectives = getObjectives(optimstore); % Decide which objective labels need to be evaluated allobjlabinds = zeros(1, length(funcindex)); for i = 1:length(funcindex) allobjlabinds(i) = find(funcindex(i) <= csum, 1) - 1; end objlabinds = unique(allobjlabinds); objectives = objectives(objlabinds); % Which objectives will be evaluated, given the selected objective % labels objinds = []; for ct = 1:length(objlabinds) objinds = [objinds, csum(objlabinds(ct))+1:csum(objlabinds(ct)+1)]; end % Evaluate the objectives if nargout>1 [y, yg] = evaluateObjective(optimstore, x', objectives); % NBI expects objective gradient to be NOBJ-by-NFREE yg = yg'; else y = evaluateObjective(optimstore, x', objectives); end % One objective label may produce more than one objective. Pull off % the required objective values and gradients [unused, inds] = ismember(funcindex, objinds); y = y(:, inds); if ~isempty(yg) yg = yg(inds, :); end % Objective vector needs to be transposed y = y'; ObjectiveFuncTypes = getObjectiveType(optimstore); for j = 1:length(funcindex) switch ObjectiveFuncTypes{allobjlabinds(j)} case 'max' % use negative of the function y(j,:) = -y(j,:); if nargout>1 yg(j, :, :) = -yg(j, :, :); end case 'min' % no change otherwise error(message('mbc:mbcOSNBI:InvalidState', 'This optimization routine can only deal', 'with objective functions to be maximized or minimized')); end end end function [c, ceq, cg, cgeq] = i_evalCon(x, funcindex) % Nonlinear inequality constraints. Note funcindex not used for % constraints, but passed to both objective and constraint % functions. cg = []; cgeq = []; if nargout>2 % evaluate nonlinear inequality constraints [c, cg] = evaluateIneqCon(optimstore, x'); % evaluate nonlinear equality constraints [ceq, cgeq] = evaluateEqCon(optimstore, x'); else c = evaluateIneqCon(optimstore, x'); ceq = evaluateEqCon(optimstore, x'); end end function NBIoptions = i_NBI_om2options omNBI = getParam(optimstore, 'Options'); if GRADREQ set(omNBI,'ShadowOptions.gradobj','on'); set(omNBI,'ShadowOptions.gradconstr', 'on'); set(omNBI,'NBISubproblemOptions.gradconstr', 'on'); end NBIoptions = cgnbiom2options(omNBI); NBIoptions.ShadowOptions = optimset(NBIoptions.ShadowOptions,'OutputFcn',@i_Output); NBIoptions.NBISubproblemOptions = optimset(NBIoptions.NBISubproblemOptions,'OutputFcn',@i_Output); end function stop= i_Output(varargin) stop= getStopState(optimstore); end end