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

Revision 1387, 2.7 KB (checked in by dmitrey.kroshko, 10 months ago)

some changes

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