Changeset 1116
- Timestamp:
- 07/12/08 04:00:27 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openopt/scikits/openopt/Kernel/BaseProblem.py
r1115 r1116 238 238 239 239 240 class Check:241 def __init__(self):242 self.df = 0# check numerical &243 self.dh = 0# user-supplied gradients244 self.dc = 0245 # lines with difference less than maxViolation will be not shown246 self.maxViolation = 1e-5247 248 240 class Parallel: 249 241 def __init__(self): … … 262 254 def __init__(self): 263 255 BaseProblem.__init__(self) 264 self.check = Check()256 #self.check = Check() 265 257 self.args = args() 266 258 self.consMode = 'all' # TODO: remove it? … … 281 273 self.c = None # c(x)<=0 282 274 self.h = None # h(x)=0 275 276 #lines with |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15) greater than maxViolation will be shown 277 self.maxViolation = 1e-2 283 278 284 279 # self.fPattern = None trunk/openopt/scikits/openopt/Kernel/ooCheckGradient.py
r960 r1116 6 6 7 7 #def ooCheckGradient(p, fun_, xCheck=None, separator='========================'): 8 def ooCheckGradient(p, fun_, *args, **kwargs): 8 def ooCheckGradient(p, fun_, *args, **kwargs): 9 9 """ 10 10 fun_ is string name like 'df', 'dc', 'dh' … … 15 15 p.warn('you must provide gradient for check ' + fun_+'. Turning the option off') 16 16 return 17 17 18 18 x0 = p.x0 19 19 p.x0 = asfarray(p.x0) 20 20 21 21 if len(args)>0: 22 if len(args)>1 or kwargs.has_key('x'): 22 if len(args)>1 or kwargs.has_key('x'): 23 23 p.err('checkd<func> funcs can have single argument x only (then x should be absent in kwargs )') 24 24 xCheck = asfarray(args[0]) 25 25 elif kwargs.has_key('x'): 26 26 xCheck = asfarray(kwargs['x']) 27 else: 27 else: 28 28 xCheck = asfarray(p.x0) 29 30 for fn in kwargs.keys(): 31 setattr(p.check, fn, kwargs[fn]) 32 29 30 if kwargs.has_key('maxViolation'): 31 setattr(p, 'maxViolation', kwargs['maxViolation']) 32 33 # for fn in kwargs.keys(): 34 # setattr(p, fn, kwargs[fn]) 35 33 36 separator='========================' 34 37 35 38 if p.isObjFunValueASingleNumber: singleColumn, doubleColumn = ['df'] , ['dc', 'dh'] 36 39 else: singleColumn, doubleColumn = [], ['df', 'dc', 'dh'] 37 38 40 41 39 42 genericUserFunc1 = getattr(p, fun_) # df, dc, dh 40 43 genericUserFunc2 = getattr(p, fun_[1:]) # f, c, h 41 44 42 45 p.nEvals = {} 43 46 p.nEvals[fun_[1:]] = 0 44 47 p.nEvals[fun_] = 0 45 48 46 49 #for genericUserFunc in [genericUserFunc1, genericUserFunc2]: 47 50 for fn in [fun_[1:], fun_]: … … 52 55 setattr(p.user, fn, [genericUserFunc]) 53 56 setattr(p, fn, getattr(p, 'user_' + fn)) 54 57 55 58 diffInt = p.diffInt 56 59 if type(p.diffInt) not in [list, tuple]: 57 60 p.diffInt = [p.diffInt] 58 61 59 62 p.__makeCorrectArgs__() 60 63 61 64 setattr(p.userProvided, fun_, False) 62 info_numerical = getattr(p, fun_)(xCheck, None, True) 65 info_numerical = getattr(p, fun_)(xCheck, None, True) 63 66 setattr(p.userProvided, fun_, True) 64 67 65 68 info_user = getattr(p, fun_)(xCheck, ignorePrev=True) 66 69 … … 69 72 str(info_numerical.shape) + ' expected, ' + str(info_user.shape) + ' obtained') 70 73 return inf 71 72 Diff = info_user-info_numerical 73 d = hstack((info_user.reshape(-1,1), info_numerical.reshape(-1,1), Diff.reshape(-1,1))) 74 74 75 S = (abs(info_user) +abs(info_numerical)) + 1e-15 76 Diff = abs(info_user-info_numerical) / S 77 78 log10_RD = log10(Diff / p.maxViolation) 79 80 d = hstack((info_user.reshape(-1,1), info_numerical.reshape(-1,1), log10_RD.reshape(-1,1))) 81 75 82 #nskiplines = sum(abs(Diff.flatten()) < p.check.maxViolation) 76 83 77 84 print('OpenOpt checks user-supplied gradient ' + fun_ + ' (shape: ' + str(info_user.shape) + ' )') 78 85 print('according to:') 79 86 print('prob.diffInt = ' + str(p.diffInt))#TODO: ADD other parameters: allowed epsilon, maxDiffLines etc 80 print(' values greater than maxViolation = '+ str(p.check.maxViolation) + ' will be shown')81 82 if (abs(Diff) >= p. check.maxViolation).any():87 print('lines with |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15) greater than maxViolation = '+ str(p.maxViolation) + ' will be shown') 88 89 if (abs(Diff) >= p.maxViolation).any(): 83 90 ss = ' ' 84 91 if fun_ in doubleColumn: 85 92 ss = ' i,j:' + fun_ + '[i]/dx[j]' 86 87 s = fun_ + ' num ' + ss + ' user-supplied numerical difference'93 94 s = fun_ + ' num ' + ss + ' user-supplied numerical RD' 88 95 print(s) 89 96 90 97 ns = ceil(log10(d.shape[0])) 91 98 counter = 0 92 99 fl_info_user = info_user.flatten() 93 100 fl_info_numerical = info_numerical.flatten() 94 if len(Diff.shape) == 1: Diff = Diff.reshape(-1,1) 101 if len(Diff.shape) == 1: 102 Diff = Diff.reshape(-1,1) 103 log10_RD = log10_RD.reshape(-1,1) 95 104 for i in xrange(Diff.shape[0]): 96 105 for j in xrange(Diff.shape[1]): 97 #TODO: if maxLineNumber is exeeded - break 98 if abs(Diff[i,j]) < p.check.maxViolation: continue 106 if abs(Diff[i,j]) < p.maxViolation: continue 99 107 counter += 1 100 108 k = Diff.shape[1]*i+j … … 102 110 if fun_ in doubleColumn: ss = str(i) + ' / ' + str(j) 103 111 else: ss = '' 104 112 105 113 if len(Diff.shape) == 1 or Diff.shape[1] == 1: n2 = 0 106 114 else: n2 = 15 107 s = ' ' + ljust('%d' % k,5) + rjust(ss, n2) + rjust('%+0.3e' % fl_info_user[k],19) + rjust('%+0.3e' % fl_info_numerical[k], 15) + rjust('%+0.3e' % Diff[i,j], 15)115 s = ' ' + ljust('%d' % k,5) + rjust(ss, n2) + rjust('%+0.3e' % fl_info_user[k],19) + rjust('%+0.3e' % fl_info_numerical[k], 15) + rjust('%d' % int(ceil(log10_RD[i,j])), 15) 108 116 print(s) 109 110 111 print('max(abs(' + fun_ + '_user - ' + fun_ + '_numerical)) = ' + str(p.norm(d[:,2],inf))) 112 print('(is registered in '+ fun_+ ' number ' + str(argmax(abs(d[:,2]))) + ')') 113 print('sum(abs(' + fun_ + '_user - ' + fun_ + '_numerical)) = ' + str(p.norm(d[:,2],1))) 114 117 118 diff_d = abs(d[:,0]-d[:,1]) 119 ind_max = argmax(diff_d) 120 val_max = diff_d[ind_max] 121 print('max(abs(' + fun_ + '_user - ' + fun_ + '_numerical)) = ' + str(val_max)) 122 print('(is registered in '+ fun_+ ' number ' + str(ind_max) + ')') 123 #print('sum(abs(' + fun_ + '_user - ' + fun_ + '_numerical)) = ' + str(p.norm(d[:,2],1))) 124 115 125 print(separator) 116 126 117 127 delattr(p.user, fun_) 118 128 delattr(p.user, fun_[1:]) trunk/openopt/scikits/openopt/examples/checkDerivatives.py
r915 r1116 20 20 r[1,1] = 2 * x[1] 21 21 r[1,2] = 2 * x[2] + 15 #incorrect derivative 22 return r 22 return r 23 23 p.dc = dc 24 24 … … 42 42 or 43 43 p.checkdc(x=myX) 44 values with difference greater than 44 values with difference greater than 45 45 maxViolation (default 1e-5) 46 46 will be shown … … 48 48 p.checkdh(myX, maxViolation=1e-4) 49 49 p.checkdh(x=myX, maxViolation=1e-4) 50 """51 50 51 Note: 52 RD is log10 of "relative difference" and is defined as int(ceil(log10( |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15)))) 52 53 53 # if you'll use solvers that use derivatives, 54 # they will hardly converge to feasible point 55 # because of the bugs in calculating derivatives made: 56 # r = p.solve('ralg') 57 58 """ 59 Typical output: 54 ################################################################################# 55 Typical output (unfortunately, in terminal or other IDEs the blank space used in strings separation can have other lengths): 60 56 61 57 OpenOpt checks user-supplied gradient df (shape: (30,) ) 62 58 according to: 63 prob.diffInt = 1e-0764 maxViolation = 1e-05 65 df num user-supplied numerical difference66 0 +7.000e+00 -8.000e+00 +1.500e+0167 8 -2.291e+00 -1.029e+01 +8.000e+0059 prob.diffInt = [9.9999999999999995e-08] 60 lines with |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15) greater than maxViolation = 0.01 will be shown 61 df num user-supplied numerical RD 62 0 +7.000e+00 -8.000e+00 2 63 8 -2.291e+00 -1.029e+01 2 68 64 max(abs(df_user - df_numerical)) = 14.9999995251 69 65 (is registered in df number 0) 70 sum(abs(df_user - df_numerical)) = 23.000021040271 66 ======================== 72 67 OpenOpt checks user-supplied gradient dc (shape: (2, 30) ) 73 68 according to: 74 prob.diffInt = 1e-0775 maxViolation = 1e-05 76 dc num i,j:dc[i]/dx[j] user-supplied numerical difference77 32 1 / 2 +1.417e+01 -8.323e-01 +1.500e+0169 prob.diffInt = [9.9999999999999995e-08] 70 lines with |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15) greater than maxViolation = 0.01 will be shown 71 dc num i,j:dc[i]/dx[j] user-supplied numerical RD 72 32 1 / 2 +1.417e+01 -8.323e-01 2 78 73 max(abs(dc_user - dc_numerical)) = 14.9999999032 79 74 (is registered in dc number 32) 80 sum(abs(dc_user - dc_numerical)) = 15.000001194281 75 ======================== 82 76 OpenOpt checks user-supplied gradient dh (shape: (2, 30) ) 83 77 according to: 84 prob.diffInt = 1e-07 85 maxViolation = 1e-05 86 dh num i,j:dh[i]/dx[j] user-supplied numerical difference 87 29 0 / 29 -2.137e+02 -2.137e+02 -1.820e-05 88 58 1 / 28 -4.474e+01 -5.974e+01 +1.500e+01 78 prob.diffInt = [9.9999999999999995e-08] 79 lines with |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15) greater than maxViolation = 0.01 will be shown 80 dh num i,j:dh[i]/dx[j] user-supplied numerical RD 81 58 1 / 28 -4.474e+01 -5.974e+01 2 89 82 max(abs(dh_user - dh_numerical)) = 14.9999962441 90 83 (is registered in dh number 58) 91 sum(abs(dh_user - dh_numerical)) = 15.000014446492 84 ======================== 93 85 """
