Changeset 1116

Show
Ignore:
Timestamp:
07/12/08 04:00:27 (5 months ago)
Author:
dmitrey.kroshko
Message:

some changes in check user-supplied derivatives

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openopt/scikits/openopt/Kernel/BaseProblem.py

    r1115 r1116  
    238238 
    239239 
    240 class Check: 
    241     def __init__(self): 
    242         self.df = 0# check numerical & 
    243         self.dh = 0# user-supplied gradients 
    244         self.dc = 0 
    245         # lines with difference less than maxViolation will be not shown 
    246         self.maxViolation = 1e-5 
    247  
    248240class Parallel: 
    249241    def __init__(self): 
     
    262254    def __init__(self): 
    263255        BaseProblem.__init__(self) 
    264         self.check = Check() 
     256        #self.check = Check() 
    265257        self.args = args() 
    266258        self.consMode = 'all' # TODO: remove it? 
     
    281273        self.c = None # c(x)<=0 
    282274        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 
    283278 
    284279#        self.fPattern = None 
  • trunk/openopt/scikits/openopt/Kernel/ooCheckGradient.py

    r960 r1116  
    66 
    77#def ooCheckGradient(p, fun_, xCheck=None, separator='========================'): 
    8 def ooCheckGradient(p, fun_, *args, **kwargs):     
     8def ooCheckGradient(p, fun_, *args, **kwargs): 
    99    """ 
    1010    fun_ is string name like 'df', 'dc', 'dh' 
     
    1515        p.warn('you must provide gradient for check ' + fun_+'. Turning the option off') 
    1616        return 
    17          
     17 
    1818    x0 = p.x0 
    1919    p.x0 = asfarray(p.x0) 
    20      
     20 
    2121    if len(args)>0: 
    22         if len(args)>1 or kwargs.has_key('x'):  
     22        if len(args)>1 or kwargs.has_key('x'): 
    2323            p.err('checkd<func> funcs can have single argument x only (then x should be absent in kwargs )') 
    2424        xCheck = asfarray(args[0]) 
    2525    elif kwargs.has_key('x'): 
    2626        xCheck = asfarray(kwargs['x']) 
    27     else:  
     27    else: 
    2828        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 
    3336    separator='========================' 
    34      
     37 
    3538    if p.isObjFunValueASingleNumber: singleColumn, doubleColumn = ['df'] , ['dc', 'dh'] 
    3639    else: singleColumn, doubleColumn = [], ['df', 'dc', 'dh'] 
    37      
    38      
     40 
     41 
    3942    genericUserFunc1 = getattr(p, fun_) # df, dc, dh 
    4043    genericUserFunc2 = getattr(p, fun_[1:]) # f, c, h 
    41      
     44 
    4245    p.nEvals = {} 
    4346    p.nEvals[fun_[1:]] = 0 
    4447    p.nEvals[fun_] = 0 
    45      
     48 
    4649    #for genericUserFunc in [genericUserFunc1, genericUserFunc2]: 
    4750    for fn in [fun_[1:], fun_]: 
     
    5255            setattr(p.user, fn, [genericUserFunc]) 
    5356        setattr(p, fn, getattr(p, 'user_' + fn)) 
    54      
     57 
    5558    diffInt = p.diffInt 
    5659    if type(p.diffInt) not in [list, tuple]: 
    5760        p.diffInt = [p.diffInt] 
    58      
     61 
    5962    p.__makeCorrectArgs__() 
    60      
     63 
    6164    setattr(p.userProvided, fun_, False) 
    62     info_numerical = getattr(p, fun_)(xCheck, None, True)  
     65    info_numerical = getattr(p, fun_)(xCheck, None, True) 
    6366    setattr(p.userProvided, fun_, True) 
    64      
     67 
    6568    info_user = getattr(p, fun_)(xCheck, ignorePrev=True) 
    6669 
     
    6972        str(info_numerical.shape) + ' expected, ' + str(info_user.shape) + ' obtained') 
    7073        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 
    7582    #nskiplines = sum(abs(Diff.flatten()) < p.check.maxViolation) 
    76      
     83 
    7784    print('OpenOpt checks user-supplied gradient ' + fun_ + ' (shape: ' + str(info_user.shape) + ' )') 
    7885    print('according to:') 
    7986    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(): 
    8390        ss = '    ' 
    8491        if fun_ in doubleColumn: 
    8592            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
    8895        print(s) 
    89      
     96 
    9097    ns = ceil(log10(d.shape[0])) 
    9198    counter = 0 
    9299    fl_info_user = info_user.flatten() 
    93100    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) 
    95104    for i in xrange(Diff.shape[0]): 
    96105        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 
    99107            counter += 1 
    100108            k = Diff.shape[1]*i+j 
     
    102110            if fun_ in doubleColumn:  ss = str(i) + ' / ' + str(j) 
    103111            else: ss = '' 
    104              
     112 
    105113            if len(Diff.shape) == 1 or Diff.shape[1] == 1: n2 = 0 
    106114            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) 
    108116            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 
    115125    print(separator) 
    116      
     126 
    117127    delattr(p.user, fun_) 
    118128    delattr(p.user, fun_[1:]) 
  • trunk/openopt/scikits/openopt/examples/checkDerivatives.py

    r915 r1116  
    2020    r[1,1] = 2 * x[1] 
    2121    r[1,2] = 2 * x[2] + 15 #incorrect derivative 
    22     return r     
     22    return r 
    2323p.dc = dc 
    2424 
     
    4242or 
    4343p.checkdc(x=myX) 
    44 values with difference greater than  
     44values with difference greater than 
    4545maxViolation (default 1e-5) 
    4646will be shown 
     
    4848p.checkdh(myX, maxViolation=1e-4) 
    4949p.checkdh(x=myX, maxViolation=1e-4) 
    50 """ 
    5150 
     51Note: 
     52RD is log10 of "relative difference" and is defined as int(ceil(log10( |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15)))) 
    5253 
    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################################################################################# 
     55Typical output (unfortunately, in terminal or other IDEs the blank space used in strings separation can have other lengths): 
    6056 
    6157OpenOpt checks user-supplied gradient df (shape: (30,) ) 
    6258according to: 
    63 prob.diffInt = 1e-07 
    64 maxViolation = 1e-05 
    65 df num         user-supplied     numerical         difference 
    66     0             +7.000e+00     -8.000e+00     +1.500e+01 
    67     8             -2.291e+00     -1.029e+01     +8.000e+00 
     59prob.diffInt = [9.9999999999999995e-08] 
     60lines with |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15) greater than maxViolation = 0.01 will be shown 
     61df num         user-supplied     numerical               RD 
     62    0             +7.000e+00     -8.000e+00              2 
     63    8             -2.291e+00     -1.029e+01              2 
    6864max(abs(df_user - df_numerical)) = 14.9999995251 
    6965(is registered in df number 0) 
    70 sum(abs(df_user - df_numerical)) = 23.0000210402 
    7166======================== 
    7267OpenOpt checks user-supplied gradient dc (shape: (2, 30) ) 
    7368according to: 
    74 prob.diffInt = 1e-07 
    75 maxViolation = 1e-05 
    76 dc num   i,j:dc[i]/dx[j]   user-supplied     numerical         difference 
    77     32             1 / 2         +1.417e+01     -8.323e-01     +1.500e+01 
     69prob.diffInt = [9.9999999999999995e-08] 
     70lines with |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15) greater than maxViolation = 0.01 will be shown 
     71dc num   i,j:dc[i]/dx[j]   user-supplied     numerical               RD 
     72    32             1 / 2         +1.417e+01     -8.323e-01              2 
    7873max(abs(dc_user - dc_numerical)) = 14.9999999032 
    7974(is registered in dc number 32) 
    80 sum(abs(dc_user - dc_numerical)) = 15.0000011942 
    8175======================== 
    8276OpenOpt checks user-supplied gradient dh (shape: (2, 30) ) 
    8377according 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 
     78prob.diffInt = [9.9999999999999995e-08] 
     79lines with |info_user-info_numerical| / (|info_user|+|info_numerical+1e-15) greater than maxViolation = 0.01 will be shown 
     80dh num   i,j:dh[i]/dx[j]   user-supplied     numerical               RD 
     81    58            1 / 28         -4.474e+01     -5.974e+01              2 
    8982max(abs(dh_user - dh_numerical)) = 14.9999962441 
    9083(is registered in dh number 58) 
    91 sum(abs(dh_user - dh_numerical)) = 15.0000144464 
    9284======================== 
    9385"""