root/trunk/openopt/scikits/openopt/examples/oofun/fixedOOVars.py

Revision 1975, 1.2 KB (checked in by dmitrey.kroshko, 14 months ago)

major changes for oofun-oovar

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