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

Revision 1542, 2.8 KB (checked in by dmitrey.kroshko, 9 months ago)

minor doc update

Line 
1"""
2This example illustrates a great speedup that can be achieved
3via using oofun and oovar vs "classic" style,
4even for unconstrained functions,
5provided the solver deals with at least 1st derivatives,
6so scipy_cobyla, goldenSection, scipy_fminbound, scipy_powell
7or GLP solvers are inappropriate
8
9The speedup is due to changes in derivatives numerical approximation approach:
10instead of handling whole dF/dx = d(g(f))/dx
11we find dg/df, df/dx and
12dF/dx = dg/df * df/dx
13
14Here's example of unconstrained problem, but constrained ones can be used as well
15for NLP, NSP, NLSP, LSP classes
16
17Concider the NL problem
18g(f(x)) -> min
19g is costly and g derivative are not available
20f(x) = (x[0]-0)^2 + (x[1]-1)^2 + ... + (x[N-1]-(N-1))^2
21see below for definition of g
22here I have chosed g: R -> R for the sake of simplicity,
23but R^m -> R^k can be handled as well
24"""
25def CostlyFunction(z):
26    counter['g'] += 1
27    r = z
28    for k in xrange(1, K+2):
29        r += z ** (1 / k**1.5)
30    return r
31
32def f(z):
33    counter['f'] += 1
34    return ((z-aN)**2).sum()
35
36solver = 'scipy_ncg'# try also scipy_cg, scipy_ncg, ralg, algencan etc
37N, K = 150, 500
38ftol, xtol, gtol = 1e-6, 1e-6, 1e-6
39iprint = 5
40
41from scikits.openopt import NLP,  oofun,  oovar
42from numpy import arange, zeros
43aN = arange(N)
44
45"""                      1: using oovar & oofun                      """
46counter = {'f':0, 'g':0}
47v = oovar('v', size = N) # start value will be zeros(N)
48ff = oofun(f, input = v)
49g = oofun(CostlyFunction, input = ff)
50p = NLP(g, maxIter=1e4, iprint=iprint, ftol=ftol, xtol=xtol, gtol=gtol)
51print 'using oofun:'
52r = p.solve(solver)
53print 'evals f:', counter['f'], '  evals of costly func g:', counter['g']
54"""                               2: classic                                  """
55counter = {'f':0, 'g':0}
56g = CostlyFunction
57p = NLP(lambda x: g(f(x)), x0=zeros(N), maxIter=1e4, ftol=ftol, xtol=xtol, gtol=gtol, iprint=iprint)
58print '\nwithout oofun:'
59r = p.solve(solver)
60print 'evals f:', counter['f'], '  evals of costly func g:', counter['g']
61"""
62using oofun:
63-----------------------------------------------------
64solver: scipy_ncg   problem: unnamed   goal: minimum
65 iter    objFunVal
66    0  2.228e+06
67    5  4.876e+04
68   10  4.953e+02
69   15  4.924e+02
70   20  4.901e+02
71   25  4.881e+02
72   30  4.862e+02
73   31  4.862e+02
74istop:  1000
75Solver:   Time Elapsed = 1.51   CPU Time Elapsed = 1.49
76objFunValue: 486.20891
77evals f: 17887   evals of costly func g: 305
78
79without oofun:
80-----------------------------------------------------
81solver: scipy_ncg   problem: unnamed   goal: minimum
82 iter    objFunVal
83    0  2.228e+06
84    5  4.957e+02
85   10  4.926e+02
86   15  4.903e+02
87   20  4.882e+02
88   25  4.864e+02
89   27  4.861e+02
90istop:  1000
91Solver:   Time Elapsed = 15.09  CPU Time Elapsed = 14.29
92objFunValue: 486.07635
93evals f: 13660   evals of costly func g: 13660
94"""
Note: See TracBrowser for help on using the browser.