-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdfovec.py
More file actions
336 lines (322 loc) · 11 KB
/
Copy pathdfovec.py
File metadata and controls
336 lines (322 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import numpy as np
def set_constants():
c13 = 1.3e1
c14 = 1.4e1
c29 = 2.9e1
c45 = 4.5e1
v = [4.0e0, 2.0e0, 1.0e0, 5.0e-1, 2.5e-1, 1.67e-1, 1.25e-1, 1.0e-1, 8.33e-2, 7.14e-2, 6.25e-2]
y1 = [1.4e-1, 1.8e-1, 2.2e-1, 2.5e-1, 2.9e-1, 3.2e-1, 3.5e-1, 3.9e-1, 3.7e-1, 5.8e-1, 7.3e-1, 9.6e-1, 1.34e0, 2.1e0, 4.39e0]
y2 = [1.957e-1, 1.947e-1, 1.735e-1, 1.6e-1, 8.44e-2, 6.27e-2, 4.56e-2, 3.42e-2, 3.23e-2, 2.35e-2, 2.46e-2]
y3 = [3.478e4, 2.861e4, 2.365e4, 1.963e4, 1.637e4, 1.372e4, 1.154e4, 9.744e3, 8.261e3, 7.03e3, 6.005e3, 5.147e3, 4.427e3, 3.82e3, 3.307e3, 2.872e3]
y4 = [
8.44e-1,
9.08e-1,
9.32e-1,
9.36e-1,
9.25e-1,
9.08e-1,
8.81e-1,
8.5e-1,
8.18e-1,
7.84e-1,
7.51e-1,
7.18e-1,
6.85e-1,
6.58e-1,
6.28e-1,
6.03e-1,
5.8e-1,
5.58e-1,
5.38e-1,
5.22e-1,
5.06e-1,
4.9e-1,
4.78e-1,
4.67e-1,
4.57e-1,
4.48e-1,
4.38e-1,
4.31e-1,
4.24e-1,
4.2e-1,
4.14e-1,
4.11e-1,
4.06e-1,
]
y5 = [
1.366e0,
1.191e0,
1.112e0,
1.013e0,
9.91e-1,
8.85e-1,
8.31e-1,
8.47e-1,
7.86e-1,
7.25e-1,
7.46e-1,
6.79e-1,
6.08e-1,
6.55e-1,
6.16e-1,
6.06e-1,
6.02e-1,
6.26e-1,
6.51e-1,
7.24e-1,
6.49e-1,
6.49e-1,
6.94e-1,
6.44e-1,
6.24e-1,
6.61e-1,
6.12e-1,
5.58e-1,
5.33e-1,
4.95e-1,
5.0e-1,
4.23e-1,
3.95e-1,
3.75e-1,
3.72e-1,
3.91e-1,
3.96e-1,
4.05e-1,
4.28e-1,
4.29e-1,
5.23e-1,
5.62e-1,
6.07e-1,
6.53e-1,
6.72e-1,
7.08e-1,
6.33e-1,
6.68e-1,
6.45e-1,
6.32e-1,
5.91e-1,
5.59e-1,
5.97e-1,
6.25e-1,
7.39e-1,
7.1e-1,
7.29e-1,
7.2e-1,
6.36e-1,
5.81e-1,
4.28e-1,
2.92e-1,
1.62e-1,
9.8e-2,
5.4e-2,
]
return c13, c14, c29, c45, v, y1, y2, y3, y4, y5
def dfovec(m, n, x, nprob):
"""
This is a Python translation of the Matlab version of the subroutine dfovec.f
This subroutine specifies the nonlinear benchmark problems in
Benchmarking Derivative-Free Optimization Algorithms
Jorge J. More' and Stefan M. Wild
SIAM J. Optimization, Vol. 20 (1), pp.172-191, 2009.
The latest version of this subroutine is always available at
https://github.com/POptUS/BenDFO/
The authors would appreciate feedback and experiences from numerical
studies conducted using this subroutine.
The data file dfo.dat defines suitable values of m and n
for each problem number nprob.
This subroutine defines the functions of 22 nonlinear
least squares problems. The allowable values of (m,n) for
functions 1,2 and 3 are variable but with m .ge. n.
For functions 4,5,6,7,8,9 and 10 the values of (m,n) are
(2,2),(3,3),(4,4),(2,2),(15,3),(11,4) and (16,3), respectively.
Function 11 (Watson) has m = 31 with n usually 6 or 9.
However, any n, n = 2,...,31, is permitted.
Functions 12,13 and 14 have n = 3,2 and 4, respectively, but
allow any m .ge. n, with the usual choices being 10,10 and 20.
Function 15 (Chebyquad) allows m and n variable with m .ge. n.
Function 16 (Brown) allows n variable with m = n.
For functions 17 and 18, the values of (m,n) are
(33,5) and (65,11), respectively.
fvec = dfovec(m, n, x, nprob)
fvec is an output array of length m which contains the nprob
function evaluated at x.
m and n are positive integer input variables. n must not
exceed m.
x is an input array of length n.
nprob is a positive integer input variable which defines the
number of the problem. nprob must not exceed 22.
Argonne National Laboratory
Jorge More' and Stefan Wild. January 2008.
"""
[c13, c14, c29, c45, v, y1, y2, y3, y4, y5] = set_constants()
# Initialize things
fvec = np.zeros(m)
total = 0
if nprob == 1: # Linear function - full rank.
for j in range(n):
total = total + x[j]
temp = 2 * total / m + 1
for i in range(m):
fvec[i] = -temp
if i < n:
fvec[i] = fvec[i] + x[i]
elif nprob == 2: # Linear function - rank 1.
for j in range(n):
total = total + (j + 1) * x[j]
for i in range(m):
fvec[i] = (i + 1) * total - 1
elif nprob == 3: # Linear function - rank 1 with zero columns and rows.
for j in range(1, n - 1):
total = total + (j + 1) * x[j]
for i in range(m - 1):
fvec[i] = i * total - 1
fvec[m - 1] = -1
elif nprob == 4: # Rosenbrock function.
fvec[0] = 10 * (x[1] - x[0] * x[0])
fvec[1] = 1 - x[0]
elif nprob == 5: # Helical valley function.
if x[0] > 0:
th = np.arctan(x[1] / x[0]) / (2 * np.pi)
elif x[0] < 0:
th = np.arctan(x[1] / x[0]) / (2 * np.pi) + 0.5
elif x[0] == x[1] and x[1] == 0:
th = 0.0
else:
th = 0.25
r = np.sqrt(x[0] * x[0] + x[1] * x[1])
fvec[0] = 10 * (x[2] - 10 * th)
fvec[1] = 10 * (r - 1)
fvec[2] = x[2]
elif nprob == 6: # Powell singular function.
fvec[0] = x[0] + 10 * x[1]
fvec[1] = np.sqrt(5) * (x[2] - x[3])
fvec[2] = (x[1] - 2 * x[2]) ** 2
fvec[3] = np.sqrt(10) * (x[0] - x[3]) ** 2
elif nprob == 7: # Freudenstein and Roth function.
fvec[0] = -c13 + x[0] + ((5 - x[1]) * x[1] - 2) * x[1]
fvec[1] = -c29 + x[0] + ((1 + x[1]) * x[1] - c14) * x[1]
elif nprob == 8: # Bard function.
for i in range(15):
tmp1 = i + 1
tmp2 = 15 - i
tmp3 = tmp1
if i > 7:
tmp3 = tmp2
fvec[i] = y1[i] - (x[0] + tmp1 / (x[1] * tmp2 + x[2] * tmp3))
elif nprob == 9: # Kowalik and Osborne function.
for i in range(11):
tmp1 = v[i] * (v[i] + x[1])
tmp2 = v[i] * (v[i] + x[2]) + x[3]
fvec[i] = y2[i] - x[0] * tmp1 / tmp2
elif nprob == 10: # Meyer function.
for i in range(16):
temp = 5 * (i + 1) + c45 + x[2]
tmp1 = x[1] / temp
tmp2 = np.exp(tmp1)
fvec[i] = x[0] * tmp2 - y3[i]
elif nprob == 11: # Watson function.
for i in range(29):
div = (i + 1) / c29
s1 = 0
dx = 1
for j in range(1, n):
s1 = s1 + j * dx * x[j]
dx = div * dx
s2 = 0
dx = 1
for j in range(n):
s2 = s2 + dx * x[j]
dx = div * dx
fvec[i] = s1 - s2 * s2 - 1
fvec[29] = x[0]
fvec[30] = x[1] - x[0] * x[0] - 1
elif nprob == 12: # Box 3-dimensional function.
for i in range(m):
temp = i + 1
tmp1 = temp / 10
fvec[i] = np.exp(-tmp1 * x[0]) - np.exp(-tmp1 * x[1]) + (np.exp(-temp) - np.exp(-tmp1)) * x[2]
elif nprob == 13: # Jennrich and Sampson function.
for i in range(m):
temp = i + 1
fvec[i] = 2 + 2 * temp - np.exp(temp * x[0]) - np.exp(temp * x[1])
elif nprob == 14: # Brown and Dennis function.
for i in range(m):
temp = (i + 1) / 5
tmp1 = x[0] + temp * x[1] - np.exp(temp)
tmp2 = x[2] + np.sin(temp) * x[3] - np.cos(temp)
fvec[i] = tmp1 * tmp1 + tmp2 * tmp2
elif nprob == 15: # Chebyquad function.
for j in range(n):
t1 = 1
t2 = 2 * x[j] - 1
t = 2 * t2
for i in range(m):
fvec[i] = fvec[i] + t2
th = t * t2 - t1
t1 = t2
t2 = th
iev = -1
for i in range(m):
fvec[i] = fvec[i] / n
if iev > 0:
fvec[i] = fvec[i] + 1 / ((i + 1) ** 2 - 1)
iev = -iev
elif nprob == 16: # Brown almost-linear function.
total1 = -(n + 1)
prod1 = 1
for j in range(n):
total1 = total1 + x[j]
prod1 = x[j] * prod1
for i in range(n - 1):
fvec[i] = x[i] + total1
fvec[n - 1] = prod1 - 1
elif nprob == 17: # Osborne 1 function.
for i in range(33):
temp = 10 * i
tmp1 = np.exp(-x[3] * temp)
tmp2 = np.exp(-x[4] * temp)
fvec[i] = y4[i] - (x[0] + x[1] * tmp1 + x[2] * tmp2)
elif nprob == 18: # Osborne 2 function.
for i in range(65):
temp = i / 10
tmp1 = np.exp(-x[4] * temp)
tmp2 = np.exp(-x[5] * (temp - x[8]) ** 2)
tmp3 = np.exp(-x[6] * (temp - x[9]) ** 2)
tmp4 = np.exp(-x[7] * (temp - x[10]) ** 2)
fvec[i] = y5[i] - (x[0] * tmp1 + x[1] * tmp2 + x[2] * tmp3 + x[3] * tmp4) # noqa
elif nprob == 19: # Bdqrtic
# n >= 5, m = (n-4)*2
for i in range(n - 4):
fvec[i] = -4 * x[i] + 3.0
fvec[n - 4 + i] = x[i] ** 2 + 2 * x[i + 1] ** 2 + 3 * x[i + 2] ** 2 + 4 * x[i + 3] ** 2 + 5 * x[n - 1] ** 2
elif nprob == 20: # Cube
# n = 2, m = n
fvec[0] = x[0] - 1.0
for i in range(1, n):
fvec[i] = 10 * (x[i] - x[i - 1] ** 3)
elif nprob == 21: # Mancino
# n = 2, m = n
for i in range(n):
ss = 0
for j in range(n):
v2 = np.sqrt(x[i] ** 2 + (i + 1) / (j + 1))
ss = ss + v2 * ((np.sin(np.log(v2))) ** 5 + (np.cos(np.log(v2))) ** 5) # noqa
fvec[i] = 1400 * x[i] + (i - 49) ** 3 + ss
elif nprob == 22: # Heart8ls
# m = n = 8
fvec[0] = x[0] + x[1] + 0.69
fvec[1] = x[2] + x[3] + 0.044
fvec[2] = x[4] * x[0] + x[5] * x[1] - x[6] * x[2] - x[7] * x[3] + 1.57
fvec[3] = x[6] * x[0] + x[7] * x[1] + x[4] * x[2] + x[5] * x[3] + 1.31
fvec[4] = x[0] * (x[4] ** 2 - x[6] ** 2) - 2.0 * x[2] * x[4] * x[6] + x[1] * (x[5] ** 2 - x[7] ** 2) - 2.0 * x[3] * x[5] * x[7] + 2.65
fvec[5] = x[2] * (x[4] ** 2 - x[6] ** 2) + 2.0 * x[0] * x[4] * x[6] + x[3] * (x[5] ** 2 - x[7] ** 2) + 2.0 * x[1] * x[5] * x[7] - 2.0
fvec[6] = (
x[0] * x[4] * (x[4] ** 2 - 3.0 * x[6] ** 2) + x[2] * x[6] * (x[6] ** 2 - 3.0 * x[4] ** 2) + x[1] * x[5] * (x[5] ** 2 - 3.0 * x[7] ** 2) + x[3] * x[7] * (x[7] ** 2 - 3.0 * x[5] ** 2) + 12.6
)
fvec[7] = (
x[2] * x[4] * (x[4] ** 2 - 3.0 * x[6] ** 2) - x[0] * x[6] * (x[6] ** 2 - 3.0 * x[4] ** 2) + x[3] * x[5] * (x[5] ** 2 - 3.0 * x[7] ** 2) - x[1] * x[7] * (x[7] ** 2 - 3.0 * x[5] ** 2) - 9.48
)
else:
print(f"unrecognized function number {nprob}")
return None
return fvec