www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimrunner/evaluateGrid.m
function [Y, XGrids] = evaluateGrid(obj, X, itemType, itemNames, varargin) %EVALUATEGRID Evaluate items over grids of the free values. % % Y = EVALUATEGRID(OBJ, X, ITEMTYPE, ITEMNAMES) evaluates the specified % items as pairs of the free values is swept across their optimization % ranges. The default settings will only grid the first two free values. % The item outputs are returned as a cell array of evaluations. Each cell % corresponds to two of the free values being swept across their ranges % and will contain a 3d matrix that comprises the 2d results grids for % each output stacked on top of each other, i.e. the 3rd dimension % corresponds to the output names. Grid dimensions are aligned with the % standard MATLAB notation, ie the rows correspond to the first free % variable in a grid pair. % % Y = EVALUATEGRID(..., PROP, VALUE, ...) allows specification of % additional arguments. Valid properties and their values are given in % the table below. % % PROPERTY | DESCRIPTION % ------------------+---------------------------------------------- % EvaluationType | Specifies the quantity to evaluate. The % | available quantities will differ depending on % | how the cgoptimrunner was constructed. % FixedValueRun | Specify which run the fixed values should be % | taken from. % NumGridPoints | Number of points for each grid dimension. % GridValuePairs | Indices of the values that should be used for % | each grid. This is a cell array of 2-element % | vectors that contain indices into the expanded % | list of all free values, not indices into the % | list of free variables. % GridRanges | Ranges that each grid value should be swept % | over. A range must be provided for every free % | value. % InsertEvalPoint | Specifies whether to force one of the grid % | point values to be exactly the free value % | specified in X. the default setting for this is % | true. % % [Y, X2] = EVALUATEGRID(...) also returns a cell array that contains the % values that each variable was set to to form a grid. X2 will be a % (2-by-length(Y)) cell array with the first row containing the grid % values for the first grid input and the second row the second grid % input values. % Copyright 2006-2012 The MathWorks, Inc. [~, nVals] = numFreeValues(obj); if nVals<2 % No one should be gridding in this case error(message('mbc:cgoptimrunner:InvalidState')); end % Parse optional arguments FixedValRun = []; EvalType = obj.DefaultEvaluationType; NumSweepPts = 51; SweepRng = []; GridPairs = {[1 2]}; InsertEvalPoint = true; for n = 1:2:length(varargin) switch lower(varargin{n}) case 'evaluationtype' EvalType = varargin{n+1}; case 'fixedvaluerun' FixedValRun = varargin{n+1}; case 'numgridpoints' NumSweepPts = varargin{n+1}; case 'gridvaluepairs' GridPairs = varargin{n+1}; case 'sweepranges' SweepRng = varargin{n+1}; case 'insertevalpoint' InsertEvalPoint = varargin{n+1}; end end if isempty(FixedValRun) FixedValRun = obj.RunIndices(obj.CurrentRun); end if isempty(SweepRng) [lb, ub] = getFreeVariableRanges(obj,FixedValRun); SweepRng = [lb; ub]; elseif size(SweepRng, 2)~=nVals error(message('mbc:cgoptimrunner:InvalidArgument')); end XGrids = cell(2, length(GridPairs)); Y = cell(1, length(GridPairs)); % Loop over grid pairs to avoid taking up all the memory switch lower(itemType) case 'objective' ItemInd = getObjectiveIndex(obj.Setup, itemNames); ItemData = pGetItemDataFor(obj.Objectives, EvalType); case 'constraint' ItemInd = getConstraintIndex(obj.Setup, itemNames); ItemData = pGetItemDataFor(obj.Constraints, EvalType); end ItemData.ExpressionStore = setCompressInputData(ItemData.ExpressionStore, true); XFree = repmat(X, NumSweepPts*NumSweepPts, 1); for n = 1:length(GridPairs) if n>1 % Reset variables from last sweep XFree(:, GridPairs{n-1}(1)) = X(GridPairs{n-1}(1)); XFree(:, GridPairs{n-1}(2)) = X(GridPairs{n-1}(2)); end R = SweepRng(:, GridPairs{n}(1)); XGrids{1, n} = linspace(R(1), R(2), NumSweepPts); if InsertEvalPoint % Replace one of the regular gridded points with the evaluation % value for this variable XGrids{1, n} = i_replacenearest(XGrids{1, n}, X(GridPairs{n}(1))); end R = SweepRng(:, GridPairs{n}(2)); XGrids{2, n} = linspace(R(1), R(2), NumSweepPts); if InsertEvalPoint % Replace one of the regular gridded points with the evaluation % value for this variable XGrids{2, n} = i_replacenearest(XGrids{2, n}, X(GridPairs{n}(2))); end [Xin, Yin] = ndgrid(XGrids{1, n}, XGrids{2, n}); XFree(:, GridPairs{n}(1)) = Xin(:); XFree(:, GridPairs{n}(2)) = Yin(:); % Perform evaluation of items ThisY = pEvaluateItems(obj, ItemData, XFree, ItemInd, 'runs', FixedValRun); Y{n} = reshape(ThisY, NumSweepPts, NumSweepPts, size(ThisY, 2)); end ItemData.ExpressionStore = setCompressInputData(ItemData.ExpressionStore, false); % Function that replaces one of the values in a vector with a new value function X = i_replacenearest(X, newVal) if ~isempty(X) [~, idx] = min(abs(X-newVal)); X(idx) = newVal; end