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

Revision 1, 1.5 kB (checked in by dmitrey, 1 month ago)

init commit

Line  
1 from openopt import NLP,  oofun,  oovar
2
3 v0 = oovar('v0', [-4.0, -15.0]) # 2nd arg (if provided) is start value
4 """
5 1st arg is name
6 r.ff will be dictionary with keys = these names
7 and values = optim point coords
8
9 alternatively you can set
10 v0 = oovar('my_var_0', size = 2)
11 or
12 v0 = oovar('my_var_0', lb=[-15, -80])
13 and size will be inhereted from lb (or ub)
14
15 setting i-th coord of the oovar start value (if that one is absent)
16 will be performed via rule:
17 coords i of lb and ub are
18 finite   finite -> (lb[i] + ub[i]) / 2
19 -inf      finite -> ub[i]
20 finite   +inf  -> lb[i]
21 -inf      +inf -> 0
22 """
23 v1 = oovar('v1', [1, 2], ub=[0.1, 0.2]) # ub is upper bound (optional)
24 v2 = oovar('v2', (3, 4), lb=[-2, 0.0], ub=[-1.5, 1]) # lb is lower bound (optional)
25 # also, Python tuple or numpy ndarray or matrix can be used instead of Python list
26
27 f0 = oofun(lambda z: z[0]**2 + z[0]  + 2*z[1]**2 , input = v0)
28 """However, indexing (like above) is not recommended.
29 Try to create separate oovars instead:
30 v0_1 = oovar(-4, lb=...), v0_2 = oovar(-15, ub=...)
31 f1 = oofun(lambda y, z: y**2 + y  + 2*z , input = v0_1, v0_2)
32 """
33
34 f1 = oofun(lambda z: (z-1).sum() ** 2, input = v1)
35 f2 = oofun(lambda z: z.sum() ** 2, input = v2)
36 f3 = oofun(lambda y, z, t: y.sum() ** 2 + 2*z.sum() + 4 * t, input = [f1, f2, f0])
37
38 p = NLP(f3, gtol=1e-7)
39 r = p.solve('ralg')
40
41 print 'solution:', r.xf
42 print 'optim value:', r.ff
43 """
44 solution: {'v0': array([ -5.00002645e-01,   3.52068631e-06]), 'v1': array([ 0.10000075,  0.20000047]), 'v2': array([-1.50000237,  0.9999997 ])}
45 optim value: 7.85208138965
46 """
Note: See TracBrowser for help on using the browser.