1+ """
2+ Demonstration of using database of existing evaluations to speed up DFO-LS
3+
4+ Test problem is the 'Watson function': for details, see
5+ J. J. More, B. S. Garbow, K. E. Hillstrom. Testing Unconstrained Optimization Software.
6+ ACM Transactions on Mathematical Software, 7:1 (1981), pp. 17-41.
7+ """
8+ import numpy as np
9+ import dfols
10+
11+ # Define the objective function
12+ def watson (x ):
13+ n = len (x )
14+ m = 31
15+ fvec = np .zeros ((m ,), dtype = float )
16+
17+ for i in range (1 , 30 ): # i=1,...,29
18+ div = float (i ) / 29.0
19+ s1 = 0.0
20+ dx = 1.0
21+ for j in range (2 , n + 1 ): # j = 2,...,n
22+ s1 = s1 + (j - 1 ) * dx * x [j - 1 ]
23+ dx = div * dx
24+ s2 = 0.0
25+ dx = 1.0
26+ for j in range (1 , n + 1 ): # j = 1,...,n
27+ s2 = s2 + dx * x [j - 1 ]
28+ dx = div * dx
29+ fvec [i - 1 ] = s1 - s2 ** 2 - 1.0
30+
31+ fvec [29 ] = x [0 ]
32+ fvec [30 ] = x [1 ] - x [0 ] ** 2 - 1.0
33+
34+ return fvec
35+
36+ # Define the starting point
37+ n = 6
38+ x0 = 0.5 * np .ones ((n ,), dtype = float )
39+
40+ # When n=6, we expect f(x0) ~ 16.4308 and f(xmin) ~ 0.00228767 at xmin ~ [ -0.0157, 1.0124, 1.2604, -1.5137, 0.992996]
41+
42+ # For optional extra output details
43+ import logging
44+ logging .basicConfig (level = logging .INFO , format = '%(message)s' )
45+
46+ # Now build a database of evaluations
47+ eval_db = dfols .EvaluationDatabase ()
48+ eval_db .append (x0 , watson (x0 ), make_starting_eval = True ) # make x0 the starting point
49+
50+ # Note: x0, x1 and x2 are colinear, so at least one of x1 and x2 shouldn't be included in the initial model
51+ x1 = np .ones ((n ,), dtype = float )
52+ x2 = np .zeros ((n ,), dtype = float )
53+ x3 = np .arange (n ).astype (float )
54+ eval_db .append (x1 , watson (x1 ))
55+ eval_db .append (x2 , watson (x2 ))
56+ eval_db .append (x3 , watson (x3 ))
57+
58+ soln = dfols .solve (watson , eval_db ) # replace x0 with eval_db
59+
60+ print (soln )
0 commit comments