checkGradFun

PURPOSE ^

% Method of optiGTest class

SYNOPSIS ^

function isOk=checkGradFun(obj,funCheck,funName)

DESCRIPTION ^

% Method of optiGTest class
 L. LAURENT --  15/04/2018 -- luc.laurent@lecnam.net

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %% Method of optiGTest class
0002 % L. LAURENT --  15/04/2018 -- luc.laurent@lecnam.net
0003 
0004 % sources available here:
0005 % https://bitbucket.org/luclaurent/optigtest/
0006 % https://github.com/luclaurent/optigtest/
0007 
0008 % optiGTest - set of testing functions    A toolbox to easy manipulate functions.
0009 % Copyright (C) 2018  Luc LAURENT <luc.laurent@lecnam.net>
0010 %
0011 % This program is free software: you can redistribute it and/or modify
0012 % it under the terms of the GNU General Public License as published by
0013 % the Free Software Foundation, either version 3 of the License, or
0014 % (at your option) any later version.
0015 %
0016 % This program is distributed in the hope that it will be useful,
0017 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0018 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0019 % GNU General Public License for more details.
0020 %
0021 % You should have received a copy of the GNU General Public License
0022 % along with this program.  If not, see <http://www.gnu.org/licenses/>.
0023 
0024 %% check gradients of any function
0025 % INPUTS:
0026 %  - funCheck: handle of checked function (@())
0027 %  - funName: name of the function [optional]
0028 % OUTPUT:
0029 %  - isOk: status of the checking
0030 
0031 function isOk=checkGradFun(obj,funCheck,funName)
0032 %default
0033 isOk=true;
0034 if nargin<3;funName='';end
0035 %threshold
0036 lim=1e-5;
0037 %space
0038 XminSpace=obj.xMin;
0039 XmaxSpace=obj.xMax;
0040 %build sampling points
0041 if exist('lhsdesign','file')
0042     Xsample=lhsdesign(obj.nSCheck,obj.dim);
0043 else
0044     Xsample=rand(obj.nSCheck,obj.dim);
0045 end
0046 %rescale the samples
0047 Xsample=Xsample.*repmat(XmaxSpace-XminSpace,obj.nSCheck,1)+repmat(XminSpace,obj.nSCheck,1);
0048 %evaluate function at the sample
0049 [~,~,GZactual]=funCheck(Xsample);
0050 %compute approximate gradients using finite differences
0051 if exist('gradFD','class')
0052     FDclass=gradFD(obj.FDtype,Xsample,obj.FDstep,funCheck);
0053     GZapprox=FDclass.GZeval;
0054     %compare results
0055     diffG=abs((GZactual-GZapprox)./GZactual);
0056     diffGB=abs((GZactual-GZapprox)./(1+abs(GZactual)));
0057     %remove bad terms
0058     diffGtest=diffG(~isinf(diffG));
0059     %
0060     critCheckA=(any(diffGtest(:)>lim)...
0061         ||any(diffGB(:)>lim))...
0062         &&obj.paranoidCheck;
0063     critCheckB=(sum(diffGtest(:)>lim)>floor(numel(diffGtest(:))/3)...
0064         ||sum(diffGB(:)>lim)>floor(numel(diffGB(:))/3))...
0065         &&~obj.paranoidCheck;
0066     if critCheckA||critCheckB
0067         Ofprintf('Issue with the gradients of the %s function\n',funName);
0068         isOk=false;
0069     end
0070     if any(diffGtest(:)>lim)||any(diffGB(:)>lim)||obj.forceDisplayGrad
0071         Ofprintf(' >> %s function\n',funName);
0072         Ofprintf('Exact\n');
0073         for it=1:obj.nSCheck
0074             Ofprintf('%+7.3e ',GZactual(it,:));
0075             fprintf('\n');
0076         end
0077         fprintf('\n');
0078         Ofprintf('Finite Difference (%s, %7.3e)\n',obj.FDtype,obj.FDstep);
0079         for it=1:obj.nSCheck
0080             Ofprintf('%+7.3e ',GZapprox(it,:));
0081             fprintf('\n');
0082         end
0083         fprintf('\n');
0084         Ofprintf('Difference\n');
0085         for it=1:obj.nSCheck
0086             Ofprintf('%+7.3e ',diffG(it,:));
0087             fprintf('\n');
0088         end
0089         fprintf('\n');
0090         Ofprintf('DifferenceB\n');
0091         for it=1:obj.nSCheck
0092             Ofprintf('%+7.3e ',diffGB(it,:));
0093             fprintf('\n');
0094         end
0095         fprintf('\n');
0096         Ofprintf('Checking points\n');
0097         for it=1:obj.nSCheck
0098             Ofprintf('%+7.3e ',Xsample(it,:));
0099             fprintf('\n');
0100         end
0101     end
0102 else
0103     Ofprintf(' Unable to check the gradients because the class gradFD is not available\n');
0104 end
0105 end

Generated on Tue 28-May-2019 16:00:34 by m2html © 2005