1 """ Unit tests for Snobfit
2 Author: Ziwen Fu
3 Nov 2008
4 """
5
6 import unittest
7 import numpy
8 try:
9 from snobfit.snobfit import snobfit
10 except:
11 from snobfit import snobfit
12
14 f = 100*( x[0]**2 - x[1] ) **2 + (x[0] - 1) **2
15 return f
16
17
19
20 a=1
21 b=5.1/(4*numpy.pi*numpy.pi)
22 c=5/numpy.pi
23 d=6
24 h=10
25 ff=1/(8*numpy.pi)
26
27 x1 = x[0]
28 x2 = x[1]
29
30 f = a * (x2-b * x1 **2 + c*x1-d )**2 + \
31 h* (1-ff) * numpy.cos(x1) + h
32
33 return f
34
36 f = ( 4- 2.1 * x[0]**2 + x[0]**4 /3 ) * x[0]**2 + \
37 x[0] * x[1] + \
38 (-4 + 4.0 * x[1]**2 ) * x[1]**2
39 return f
40
42 if y == None:
43 x1 = x[0]
44 x2 = x[1]
45 else:
46 x1 = x
47 x2 = y
48
49 f =( 1+ (x1+x2+1)**2 *(19-14*x1+3*x1**2 - 14*x2 + 6*x1*x2 + 3*x2**22) ) * \
50 ( 30+(2*x1-3*x2)**2 * (18-32*x1 + 12*x1**2 + 48*x2 - \
51 36*x1*x2 + 27*x2**2) )
52
53 return f
54
55
57 a = numpy.array( [[4.0, 1.0, 8.0, 6.0, 3.0],
58 [4.0, 1.0, 8.0, 6.0, 7.0],
59 [4.0, 1.0, 8.0, 6.0, 3.0],
60 [4.0, 1.0, 8.0, 6.0, 7.0]])
61 c = numpy.array( [0.1, 0.2, 0.2, 0.4, 0.4] )
62 d = numpy.zeros(5)
63 for i in xrange(5):
64 b = ( x - a[:,i] ) ** 2
65 d[i] = sum(b)
66
67 f = -sum( (c+d) ** (-1) )
68
69 return f
70
71
73 a = numpy.array( [[4, 1, 8, 6, 3, 2, 5],
74 [4, 1, 8, 6, 7, 9, 5],
75 [4, 1, 8, 6, 3, 2, 3],
76 [4, 1, 8, 6, 7, 9, 3]]
77 )
78 c = numpy.array([0.1, 0.2, 0.2, 0.4, 0.4, 0.6, 0.3])
79 d = numpy.zeros(a.shape[1])
80 for i in xrange(7):
81 b = ( x - a[:,i] ) **2
82 d[i] = sum(b)
83
84 f = -sum( (c+d)**(-1) )
85 return f
86
87
89 a = numpy.array([[4, 1, 8, 6, 3, 2, 5, 8, 6, 7],
90 [4, 1, 8, 6, 7, 9, 5, 1, 2, 3.6],
91 [4, 1, 8, 6, 3, 2, 3, 8, 6, 7],
92 [4, 1, 8, 6, 7, 9, 3, 1, 2, 3.6]])
93 c = numpy.array( [0.1, 0.2, 0.2, 0.4, 0.4, 0.6, 0.3, 0.7, 0.5, 0.5] )
94
95 d = numpy.zeros(10)
96 for i in xrange(10):
97 b = ( x - a[:,i] ) ** 2
98 d[i] = numpy.sum(b)
99
100 f = -numpy.sum( (c+d)**(-1) )
101
102 return f
103
104
106 a = numpy.array([[3.0, 0.1, 3.0, 0.1],
107 [10.0, 10.0, 10.0, 10.0],
108 [30.0, 35.0, 30.0, 35.0]])
109
110 p = numpy.array( [[ 0.36890, 0.46990, 0.10910, 0.03815],
111 [ 0.11700, 0.43870, 0.87320, 0.57430],
112 [ 0.26730, 0.74700, 0.55470, 0.88280]])
113
114 c = numpy.array( [1.0, 1.2, 3.0, 3.2] )
115 d = numpy.zeros(4)
116 for i in xrange(4):
117 d[i] = sum( a[:,i] * ( x - p[:,i]) **2 )
118
119 f = -sum( c * numpy.exp( -d ) )
120
121 return f
122
123
125 a = numpy.array([[ 10.00, 0.05, 3.00, 17.00],
126 [ 3.00, 10.00, 3.50, 8.00],
127 [ 17.00, 17.00, 1.70, 0.05],
128 [ 3.50, 0.10, 10.00, 10.00],
129 [ 1.70, 8.00, 17.00, 0.10],
130 [ 8.00, 14.00, 8.00, 14.00]])
131
132 p = numpy.array([[ 0.1312, 0.2329, 0.2348, 0.4047],
133 [ 0.1696, 0.4135, 0.1451, 0.8828],
134 [ 0.5569, 0.8307, 0.3522, 0.8732],
135 [ 0.0124, 0.3736, 0.2883, 0.5743],
136 [ 0.8283, 0.1004, 0.3047, 0.1091],
137 [ 0.5886, 0.9991, 0.6650, 0.0381]])
138
139 c = numpy.array( [1.0, 1.2, 3.0, 3.2] )
140
141 d = numpy.zeros(4)
142 for i in xrange(4):
143 d[i] = numpy.sum( a[:,i] * ( x - p[:,i] ) **2 )
144
145 f = -numpy.sum( c*numpy.exp(-d));
146
147 return f
148
150
151 sum1 = 0
152 sum2 = 0
153 for i in range(1,6):
154 sum1 += i * numpy.cos( (i+1) * x[0] + i )
155 sum2 += i * numpy.cos( (i+1) * x[1] + i );
156
157 f = sum1*sum2
158 return f
159
160
161
163
166
168 u = numpy.array( [-5, 0])
169 v = numpy.array( [10, 15] )
170 x0 = numpy.array( [2,3] )
171 fglob = 0.397887357729739
172 xglob = numpy.array( [[9.42477796, -3.14159265, 3.14159265],
173 [2.47499998, 12.27500000, 2.27500000]] )
174
175 xbest, fbest, ncall = snobfit(bra, x0, (u, v), fglob=fglob)
176 assert abs(fbest-fglob) < 1.0e-6
177
179 """ Rosenbrock """
180 x0 = numpy.array([2, 3])
181 u = -5.12*numpy.ones(2)
182 v = -u
183 fglob = 0
184 xglob = numpy.array([1, 1])
185 xbest, fbest, ncall = snobfit(ros, x0, (u, v))
186
187
188 assert abs(fbest) < 1.0e-6
189
191
192 u = numpy.array([-3, -2])
193 v = numpy.array([3, 2] )
194 x0 = numpy.array([1, 1])
195 fglob = -1.0316284535
196 xglob = numpy.array([ 0.08984201, -0.08984201,
197 -0.71265640, 0.71265640])
198 xbest, fbest, ncall = snobfit(cam, x0, (u, v), fglob=fglob)
199
200 assert abs(fbest-fglob) < 1.0e-6
201
202
204
205 u = numpy.array([0, 0, 0, 0])
206 v = numpy.array([10, 10, 10, 10] )
207 x0 = numpy.array([5, 5,5,5])
208 fglob = -10.1531996790582
209 xglob = numpy.array([4, 4, 4, 4])
210 xbest, fbest, ncall = snobfit(sh5, x0, (u, v), fglob=fglob)
211
212 assert abs(fbest-fglob) < 1.0e-6
213
214
215
217
218 u = numpy.array([0, 0, 0, 0])
219 v = numpy.array([10, 10, 10, 10])
220 x0 = numpy.array([5, 5,5,5])
221 fglob = -10.4029405668187
222 xglob = numpy.array([4, 4, 4, 4])
223 xbest, fbest, ncall = snobfit(sh7, x0, (u, v), fglob=fglob)
224
225 assert abs(fbest-fglob) < 1.0e-6
226
227
229
230 u = numpy.array([0, 0, 0, 0])
231 v = numpy.array([10, 10, 10, 10])
232 x0 = numpy.array([5, 5,5,5])
233 fglob = -10.5364098166920
234 xglob = numpy.array([4, 4, 4, 4])
235 xbest, fbest, ncall = snobfit(sh10, x0, (u, v), fglob=fglob)
236
237 assert abs(fbest-fglob) < 1.0e-6
238
239
241
242 u = numpy.array([0, 0, 0])
243 v = numpy.array([1, 1, 1])
244 x0 = numpy.array([0.5, 0.5,0.5])
245 fglob = -3.86278214782076
246 xglob = numpy.array([0.1, 0.55592003, 0.85218259])
247 xbest, fbest, ncall = snobfit(hm3, x0, (u, v), fglob=fglob)
248
249 assert abs(fbest-fglob) < 1.0e-6
250
251
253
254 u = numpy.array([0, 0, 0, 0, 0, 0])
255 v = numpy.array([1, 1, 1, 1, 1, 1])
256 x0 = numpy.array([0.5, 0.5,0.5,0.5,0.5,0.5])
257 fglob = -3.32236801141551
258 xglob = numpy.array([0.20168952, 0.15001069, 0.47687398,
259 0.27533243, 0.31165162, 0.65730054])
260 xbest, fbest, ncall = snobfit(hm6, x0, (u, v), fglob=fglob)
261
262 assert abs( (fbest-fglob)/fglob ) < 1.0e-6
263
265
266 u = numpy.array([-10, -10])
267 v = numpy.array([10, 10])
268 x0 = numpy.array([1, 1])
269 fglob = -186.730908831024
270 xglob = numpy.array([[ -7.08350658, 5.48286415, 4.85805691,
271 4.85805691, -7.08350658, -7.70831382,
272 -1.42512845, -0.80032121, -1.42512844,
273 -7.08350639, -7.70831354, 5.48286415,
274 5.48286415, 4.85805691, -7.70831354,
275 -0.80032121, -1.42512845, -0.80032121],
276 [4.85805691, 4.85805681, -7.08350658,
277 5.48286415, -7.70831382, -7.08350658,
278 -0.80032121, -1.42512845, -7.08350639,
279 -1.42512844, 5.48286415, -7.70831354,
280 4.85805691, 5.48286415, -0.80032121,
281 -7.70831354, -0.80032121, -1.42512845]])
282
283 xbest, fbest, ncall = snobfit(shu, x0, (u, v), fglob=fglob)
284 assert abs( (fbest-fglob)/fglob ) < 1.0e-6
285
286
287
288
289 if __name__ == '__main__':
290 unittest.main()
291