root/PythonPackages/OpenOpt/openopt/doc/checkDerivatives.py

Revision 340, 2.6 kB (checked in by dmitrey, 2 years ago)

minor update in doc

Line 
1 from openopt import NLP
2 from numpy import cos, arange, ones, asarray, abs, zeros
3 N = 30
4 M = 5
5 ff = lambda x: ((x-M)**2).sum()
6 p = NLP(ff, cos(arange(N)))
7
8 def df(x):
9     r = 2*(x-M)
10     r[0] += 15 #incorrect derivative
11     r[8] += 80 #incorrect derivative
12     return r
13 p.df =  df
14
15 p.c = lambda x: [2* x[0] **4-32, x[1]**2+x[2]**2 - 8]
16
17 def dc(x):
18     r = zeros((2, p.n))
19     r[0,0] = 2 * 4 * x[0]**3
20     r[1,1] = 2 * x[1]
21     r[1,2] = 2 * x[2] + 15 #incorrect derivative
22     return r
23 p.dc = dc
24
25 p.h = lambda x: (1e1*(x[-1]-1)**4, (x[-2]-1.5)**4)
26
27 def dh(x):
28     r = zeros((2, p.n))
29     r[0,-1] = 1e1*4*(x[-1]-1)**3
30     r[1,-2] = 4*(x[-2]-1.5)**3 + 15 #incorrect derivative
31     return r
32 p.dh = dh
33
34 p.checkdf()
35 p.checkdc()
36 p.checkdh()
37 """
38 you can use p.checkdF(x) for other point than x0 (F is f, c or h)
39 p.checkdc(myX)
40 or
41 p.checkdc(x=myX)
42 values with difference greater than
43 maxViolation (default 1e-5)
44 will be shown
45 p.checkdh(maxViolation=1e-4)
46 p.checkdh(myX, maxViolation=1e-4)
47 p.checkdh(x=myX, maxViolation=1e-4)
48
49 #################################################################################
50 Typical output (unfortunately, in terminal or other IDEs the blank space used in strings separation can have other lengths):
51 Note that RD (relative difference) is defined as
52 int(ceil(log10(abs(Diff) / maxViolation + 1e-150)))
53 where
54 Diff = 1 - (info_user+1e-8)/(info_numerical + 1e-8)
55
56 OpenOpt checks user-supplied gradient df (shape: (30,) )
57 according to:
58     prob.diffInt = [  1.00000000e-07]
59     |1 - info_user/info_numerical| <= prob.maxViolation = 0.01
60 df num         user-supplied     numerical               RD
61     0             +7.000e+00     -8.000e+00              3
62     8             -2.291e+00     -1.029e+01              2
63 max(abs(df_user - df_numerical)) = 14.9999995251
64 (is registered in df number 0)
65 ========================
66 OpenOpt checks user-supplied gradient dc (shape: (2, 30) )
67 according to:
68     prob.diffInt = [  1.00000000e-07]
69     |1 - info_user/info_numerical| <= prob.maxViolation = 0.01
70 dc num   i,j:dc[i]/dx[j]   user-supplied     numerical               RD
71     32             1 / 2         +1.417e+01     -8.323e-01              4
72 max(abs(dc_user - dc_numerical)) = 14.9999999032
73 (is registered in dc number 32)
74 ========================
75 OpenOpt checks user-supplied gradient dh (shape: (2, 30) )
76 according to:
77     prob.diffInt = [  1.00000000e-07]
78     |1 - info_user/info_numerical| <= prob.maxViolation = 0.01
79 dh num   i,j:dh[i]/dx[j]   user-supplied     numerical               RD
80     58            1 / 28         -4.474e+01     -5.974e+01              2
81 max(abs(dh_user - dh_numerical)) = 14.9999962441
82 (is registered in dh number 58)
83 ========================
84
85 """
Note: See TracBrowser for help on using the browser.