root/OOPy/openopt/examples/oofun/checkDerivatives.py

Revision 90, 1.2 kB (checked in by dmitrey, 6 months ago)

some changes for examples

Line  
1 """
2 the example illustrates how to perform check for oofun derivatives
3 """
4 from openopt import NLP,  oofun,  oovar
5
6 v0 = oovar('v0', [-4.0, -15.0]) # 2nd arg (if provided) is start value
7 v1 = oovar('v2', (3, 4), lb=[-2, 0.0], ub=[-1.5, 1]) # lb, ub are upper / lower bounds (optional)
8 # also, Python tuple or numpy ndarray or matrix can be used instead of Python list
9
10 f0 = oofun(lambda y, z: [y[0]**2 + 4 * y[1]**2 + z[0]**2, y[0]**2 + 2 * y[1]**2 + z[1]**2], input = [v0, v1])
11 f0.d = lambda y, z: [[2*y[0], 8*y[1], 2*z[0], 0], [2*y[0], 4*y[1], 0, 15+2*z[1]]]
12 # incorrect derivative 15+2*z[1], should be 2*z[1]
13
14 # objective function
15 F = oofun(lambda t: 2*t.sum()+1, input = f0)
16
17 p = NLP(F)
18
19 # let's check f0 derivatives
20 p.checkOOFunDerivatives(f0) 
21 # also, you could check some oofun instances:
22 # p.checkOOFunDerivatives(f0, f1,f2 ,,, fk)
23
24 """
25 output:
26 f num   i,j: df[i]/dx[j]   user-supplied     numerical               RD
27     7              1 / 3         +2.300e+01     +8.000e+00              3
28 max(abs(f_user - f_numerical)) = 15.0000012329
29 (is registered in f number 7)
30 """
31
32 # optional code for the example: solve the problem
33 f0.d = lambda y, z: [[2*y[0], 8*y[1], 2*z[0], 0], [2*y[0], 4*y[1], 0, 2*z[1]]]
34 r = p.solve('ralg')
Note: See TracBrowser for help on using the browser.