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

    function optim = scaleOptimItems(optim, method, varargin)
%SCALEOPTIMITEMS Set the scaling flag for objectives/constraints
%
%  OPTIM = SCALEOPTIMITEMS(OPTIM, 'natural') sets all the optimization
%  objectives and constraints to evaluate in natural units.
%
%  OPTIM = SCALEOPTIMITEMS(OPTIM, 'scaled') sets all the optimization
%  objectives and constraints to evaluate in scaled units.
%
%  OPTIM = SCALEOPTIMITEMS(OPTIM, 'automatic') sets each of the
%  optimization objectives/constraints to evaluate in scaled units only if
%  the maximum absolute value of the range is outside [1, 10^4].
%
%  OPTIM = SCALEOPTIMITEMS(OPTIM, 'user', OBJLABELS, OBJSTATUS, CONLABELS,
%  CONSTATUS) sets the specified optimization objectives/constraints to
%  evaluate in scaled or natural units. OBJLABELS (CONLABELS) is a cell
%  array of objective (constraint) labels, OBJSTATUS (CONSTATUS) is a
%  logical array indicating whether the item is scaled (TRUE) or
%  otherwise (FALSE).

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


% Get objectives and constraints
objs = getObjectiveFunc(optim);
NOBJS = length(objs);
objlabels = getObjectiveLabels(optim.OptimSetup);
cons = getConstraint(optim);
NCONS = length(cons);
conlabels = getConstraintLabels(optim.OptimSetup);

% Determine the scaling flag based on the supplied method
switch lower(method)
    case 'natural'
        objflag = repmat(false, 1, NOBJS);
        conflag = repmat(false, 1, NCONS);
    case 'scaled'
        objflag = repmat(true, 1, NOBJS);
        conflag = repmat(true, 1, NCONS);
    case 'automatic'
        objflag = i_AutomaticScaling(objs);
        conflag = i_AutomaticScaling(cons);
    case 'user'
        objlabels = varargin{1};
        objs = getObjectiveFunc(optim, objlabels); 
        if ~iscell(objs)
            objs = {objs};
        end
        objflag = varargin{2};
        conlabels = varargin{3};
        cons = getConstraint(optim, conlabels); 
        if ~iscell(cons)
            cons = {cons};
        end
        conflag = varargin{4};
    otherwise
        error(message('mbc:cgoptim:InvalidArgument16'));
end

% Set the scaling flag in the selected items
for i = 1:length(objs)
    objs{i} = setOutputScaled(objs{i}, objflag(i));
end
for i = 1:length(cons)
    cons{i} = setOutputScaled(cons{i}, conflag(i));
end

% Set the modified items back in the optimization
for i = 1:length(objlabels)
    optim = setObjectiveFunc(optim, objlabels{i}, objs{i});
end
for i = 1:length(conlabels)
    optim = setConstraint(optim, conlabels{i}, cons{i});
end

%--------------------------------------------------------------------------
function flag = i_AutomaticScaling(items) 
%--------------------------------------------------------------------------

NITEMS = length(items);
flag = true(1, NITEMS);
for i = 1:NITEMS
    rng = getDistanceScale(items{i});
    Rm = floor(0.25*log10(max(abs(rng))));
    if Rm
        flag(i) = true;
    else
        flag(i) = false;
    end
end