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

Revision 170, 1.3 kB (checked in by dmitrey, 2 years ago)

move some files

Line 
1 """
2 Example of using additional parameters for user f, c, h functions
3 Note! For oofun handling user parameters is performed
4 in the same way:
5 my_oofun.args = (...)
6 they will be passed to derivative function as well (if you have supplied it)
7 """
8
9 from openopt import NLP
10 from numpy import asfarray
11
12 f = lambda x, a: (x**2).sum() + a * x[0]**4
13 x0 = [8, 15, 80]
14 p = NLP(f, x0)
15
16
17 #using c(x)<=0 constraints
18 p.c = lambda x, b, c: (x[0]-4)**2 - 1 + b*x[1]**4 + c*x[2]**4
19
20 #using h(x)=0 constraints
21 p.h = lambda x, d: (x[2]-4)**2 + d*x[2]**4 - 15
22    
23 p.args.f = 4 # i.e. here we use a=4
24 # so it's the same to "a = 4; p.args.f = a" or just "p.args.f = a = 4"
25
26 p.args.c = (1,2)
27
28 p.args.h = 15
29
30 # Note 1: using tuple p.args.h = (15,) is valid as well
31
32 # Note 2: if all your funcs use same args, you can just use
33 # p.args = (your args)
34
35 # Note 3: you could use f = lambda x, a: (...); c = lambda x, a, b: (...); h = lambda x, a: (...)
36
37 # Note 4: if you use df or d2f, they should handle same additional arguments;
38 # same to c - dc - d2c, h - dh - d2h
39
40 # Note 5: instead of myfun = lambda x, a, b: ...
41 # you can use def myfun(x, a, b): ...
42
43 r = p.solve('ralg')
44 """
45 If you will encounter any problems with additional args implementation,
46 you can use the simple python trick
47 p.f = lambda x: other_f(x, <your_args>)
48 same to c, h, df, etc
49 """
Note: See TracBrowser for help on using the browser.