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

Revision 971, 1.1 kB (checked in by dmitrey.kroshko, 7 months ago)

some code cleanup + some changes

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