root/trunk/openopt/scikits/openopt/examples/checkDerivatives.py

Revision 915, 2.6 kB (checked in by dmitrey.kroshko, 4 months ago)

--

Line 
1 from scikits.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] += 8 #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 h1 = lambda x: 1e1*(x[-1]-1)**4
26 h2 = lambda x: (x[-2]-1.5)**4
27 p.h = (h1, h2)
28
29 def dh(x):
30     r = zeros((2, p.n))
31     r[0,-1] = 1e1*4*(x[-1]-1)**3
32     r[1,-2] = 4*(x[-2]-1.5)**3 + 15 #incorrect derivative
33     return r
34 p.dh = dh
35
36 p.checkdf()
37 p.checkdc()
38 p.checkdh()
39 """
40 you can use p.checkdF(x) for other point than x0 (F is f, c or h)
41 p.checkdc(myX)
42 or
43 p.checkdc(x=myX)
44 values with difference greater than
45 maxViolation (default 1e-5)
46 will be shown
47 p.checkdh(maxViolation=1e-4)
48 p.checkdh(myX, maxViolation=1e-4)
49 p.checkdh(x=myX, maxViolation=1e-4)
50 """
51
52
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:
60
61 OpenOpt checks user-supplied gradient df (shape: (30,) )
62 according 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
68 max(abs(df_user - df_numerical)) = 14.9999995251
69 (is registered in df number 0)
70 sum(abs(df_user - df_numerical)) = 23.0000210402
71 ========================
72 OpenOpt checks user-supplied gradient dc (shape: (2, 30) )
73 according 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
78 max(abs(dc_user - dc_numerical)) = 14.9999999032
79 (is registered in dc number 32)
80 sum(abs(dc_user - dc_numerical)) = 15.0000011942
81 ========================
82 OpenOpt checks user-supplied gradient dh (shape: (2, 30) )
83 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
89 max(abs(dh_user - dh_numerical)) = 14.9999962441
90 (is registered in dh number 58)
91 sum(abs(dh_user - dh_numerical)) = 15.0000144464
92 ========================
93 """
Note: See TracBrowser for help on using the browser.