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

Revision 971, 1.1 KB (checked in by dmitrey.kroshko, 2 years ago)

some code cleanup + some changes

Line 
1"""
2Example of using additional parameters for user f, c, h functions
3"""
4
5from scikits.openopt import NLP
6from numpy import asfarray
7
8f = lambda x, a: (x**2).sum() + a * x[0]**4
9x0 = [8, 15, 80]
10p = NLP(f, x0)
11
12
13#using c(x)<=0 constraints
14p.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
17p.h = lambda x, d: (x[2]-4)**2 + d*x[2]**4 - 15
18   
19p.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
22p.args.c = (1,2)
23
24p.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
39r = p.solve('ralg')
40"""
41If you will encounter any problems with additional args implementation,
42you can use the simple python trick
43p.f = lambda x: other_f(x, <your_args>)
44same to c, h, df, etc
45"""
Note: See TracBrowser for help on using the browser.