www.gusucode.com > sloptim工具箱matlab源码程序 > sloptim/sloptim/@ResponseOptimizer/@Optimizer/numgrad.m

    function G = numgrad(this,fcn,x,Info)
% Computes gradient wrt decision variables using
% two simulations of the original model and finite 
% centered differences.

% Copyright 1986-2013 The MathWorks, Inc.

% min/max and scale data
xMin = Info.xMin;
xMax = Info.xMax;
xScale = Info.xScale;
xScale(xScale==0) = 1;

if this.fUseParallel
   
   %Ensure workers are in unique directory, can change from
   %this.initParallel call as model may rebuild on nominal simulation
   %causing cd change, see g469102
   nWorkers = parallelsim.getNumWorkers();
   parfor ct=1:nWorkers
      localConfigWorkers;
   end
   
   %Cache and remove some properties from 'this', so that it can be
   %saved and reloaded on the workers
   L                     = this.Listeners;
   this.Listeners        = [];
   isStopOnSimError      = this.isStopOnSimError;
   this.isStopOnSimError = false;
   %Wrap 'this' in an MCOS object so that it can be passed into the
   %parfor loop
   mcosObj        = parallelsim.uddWrapper;
   mcosObj.uddObj = this;
   xLOrig         = x;
   xROrig         = x;
   parfor j=1:length(x)-1
      % Compute perturbation size
      dx = 0.1 * abs(x(j)) + 0.01 * xScale(j);
      xjL = max(xMin(j),x(j)-dx);
      xjR = min(xMax(j),x(j)+dx);
      % Evaluate constraints at x-dx, and x+dx
      xL = xLOrig;  xL(j) = xjL;
      xR = xROrig;  xR(j) = xjR;
      [FL,FR] = localGetGrad(mcosObj,fcn,xL,xR,j);
      % Estimate gradient wrt xj
      dF = FR - FL;
      dF(imag(FR)~=0 | imag(FL)~=0) = 0; % complex -> sim failure
      G(j,:) = dF.'/(xjR - xjL);         % Don't know column size
   end
   %Restore cached properties
   this.Listeners        = L;
   this.isStopOnSimError = isStopOnSimError;
   
else
   %Serial calculation
   for j=1:length(x)-1
      % Compute perturbation size
      dx = 0.1 * abs(x(j)) + 0.01 * xScale(j);
      xjL = max(xMin(j),x(j)-dx);
      xjR = min(xMax(j),x(j)+dx);
      % Evaluate constraints at x-dx
      xL = x;  xL(j) = xjL;
      if strcmp(this.Project.OptimStatus,'run')
         FL = feval(fcn,this,xL,-j);
      else
         % Interrupted or error -> return as soon as simulation ends
         G = [];  return
      end
      % Evaluate constraints at x+dx
      xR = x;  xR(j) = xjR;
      if strcmp(this.Project.OptimStatus,'run')
         FR = feval(fcn,this,xR,j);
      else
         G = [];  return
      end
      % Estimate gradient wrt xj
      dF = FR - FL;
      dF(imag(FR)~=0 | imag(FL)~=0) = 0; % complex -> sim failure
      G(j,:) = dF.'/(xjR - xjL);         %#ok<AGROW> % Don't know column size
   end
end
end %numgrad

%% Local function for gradient calculation, called when running in parallel
function [FL,FR] = localGetGrad(mcosObj,fcn,xL,xR,j)

prob = mcosObj.uddObj; %Retrieve the optimization problem

%Compute left and right perturbations
FL = feval(fcn,prob,xL,-j);
FR = feval(fcn,prob,xR,j);
end

%% Local wrapper function for package method used in parfor loop
function localConfigWorkers

parallelsim.configureWorkerPathState;
end