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

Revision 1, 1.2 kB (checked in by dmitrey, 8 months ago)

init commit

Line  
1 """
2 the example illustrates
3 how you can declare and use
4 fixed oovars
5
6 here it is used for objFunc only
7 but of course it can be used
8 for non-linear equality and inequality constraints as well
9 """
10
11 n = 5
12
13 from openopt import NLP,  oofun,  oovar
14 from numpy import inf
15
16 v0 = oovar('v0', [-4.0, -15.0]) # 2nd arg (if provided) is start value
17 v1 = oovar('v1', range(n), fixed=True)  # range(n) is [0, 1, ..., n-1], see also: numpy.arange
18
19 #set objFunc
20 f = oofun(lambda z0, z1: (z0[0]-15)**4 + (z0[1]-80)**4 + (z1**2).sum(), input = [v0, v1])
21
22 # assign prob
23 p = NLP(f)
24
25 # solve
26 r = p.solve('ralg')
27
28 print 'solution:', r.xf
29 print 'optim value:', r.ff
30
31 """
32 using oofun-style for fixed vars is more effective than same problem in classic style fix via lb=ub
33
34 some mature solvers (but not ralg) have efficient handling of fixed vars,
35 but they can't take advantage from deeply nested parts of code fixed due to all used variables in that parts are fixed.
36 so using oovars + oofuns can yield serious benefites even for those ones.
37 """
38 lb = [-inf, -inf] + range(n)
39 ub = [inf, inf] + range(n)
40 p2 = NLP(lambda x: (x[0]-15) **4 + (x[1]-80)**4 + (x[2:] ** 2).sum(), [-4.0, -15.0] + range(n), lb=lb, ub=ub)
41 r2 = p2.solve('ralg')
Note: See TracBrowser for help on using the browser.