Package snobfit :: Package tests :: Module test_Sigma

Source Code for Module snobfit.tests.test_Sigma

  1  """ Unit tests for Snobfit for data with noise 
  2  Author: Ziwen Fu 
  3  Nov 2007 
  4  """ 
  5   
  6  import unittest 
  7  import numpy 
  8  try: 
  9      from snobfit.snobfit import snobfit 
 10  except: 
 11      from snobfit import snobfit 
 12   
13 -def ros(x):
14 f = 100*( x[0]**2 - x[1] ) **2 + (x[0] - 1) **2 15 return f
16 17
18 -def bra(x):
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
35 -def cam(x):
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
41 -def gpr(x,y=None):
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
56 -def sh5(x):
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
72 -def sh7(x):
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
88 -def sh10(x):
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
105 -def hm3(x):
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
124 -def hm6(x):
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
149 -def shu(x):
150 # Shubert function 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 rtol = 1.e-2 162 163 # ---------------------------------------------------------------------
164 -class test_Sigma_Case1(unittest.TestCase):
165
166 - def setUp(self):
167 self.factor=0.0 168 print "Sigma=0.0"
169 170
171 - def test_bra(self):
172 u = numpy.array( [-5, 0]) 173 v = numpy.array( [10, 15] ) 174 x0 = numpy.array( [2,3] ) 175 fglob = 0.397887357729739 176 xglob = numpy.array( [[9.42477796, -3.14159265, 3.14159265], 177 [2.47499998, 12.27500000, 2.27500000]] ) 178 179 xbest, fbest, ncall = snobfit(bra, x0, (u, v), 180 fglob=fglob, 181 rtol=rtol, 182 fac = self.factor 183 ) 184 assert abs( (fbest-fglob)/fglob) < rtol 185 print "branin", xbest, fbest, ncall
186 187
188 - def test_ros(self):
189 """ Rosenbrock """ 190 x0 = numpy.array([2, 3]) 191 u = -5.12*numpy.ones(2) 192 v = -u 193 fglob = 0 194 xglob = numpy.array([1, 1]) 195 xbest, fbest, ncall = snobfit(ros, x0, (u, v), 196 fac = self.factor) 197 198 assert abs(fbest) < 1.0e-6 199 print "ros: ", xbest, fbest, ncall
200 201
202 - def test_cam(self):
203 # six-hump camel 204 u = numpy.array([-3, -2]) 205 v = numpy.array([3, 2] ) 206 x0 = numpy.array([1, 1]) 207 fglob = -1.0316284535 208 xglob = numpy.array([ 0.08984201, -0.08984201, 209 -0.71265640, 0.71265640]) 210 xbest, fbest, ncall = snobfit(cam, x0, (u, v), fglob=fglob,rtol=rtol, 211 fac = self.factor) 212 213 assert abs((fbest-fglob)/fglob) < rtol 214 print "cam", xbest, fbest, ncall
215
216 - def test_sh5(self):
217 # Shekel 5 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.1531996790582 222 xglob = numpy.array([4, 4, 4, 4]) 223 xbest, fbest, ncall = snobfit(sh5, x0, (u, v), 224 fglob=fglob,rtol=rtol, 225 fac = self.factor) 226 227 assert abs( (fbest-fglob)/fglob ) < rtol 228 print "sh5", xbest, fbest, ncall
229 230
231 - def test_sh7(self):
232 #Shekel 7 233 u = numpy.array([0, 0, 0, 0]) 234 v = numpy.array([10, 10, 10, 10]) 235 x0 = numpy.array([5, 5,5,5]) 236 fglob = -10.4029405668187 237 xglob = numpy.array([4, 4, 4, 4]) 238 xbest, fbest, ncall = snobfit(sh7, x0, (u, v), 239 fglob=fglob,rtol=rtol,fac = self.factor) 240 241 assert abs( (fbest-fglob)/fglob ) < rtol 242 print "sh7", xbest, fbest, ncall
243 244
245 - def test_sh10(self):
246 # Shekel 10 247 u = numpy.array([0, 0, 0, 0]) 248 v = numpy.array([10, 10, 10, 10]) 249 x0 = numpy.array([5, 5,5,5]) 250 fglob = -10.5364098166920 251 xglob = numpy.array([4, 4, 4, 4]) 252 xbest, fbest, ncall = snobfit(sh10, x0, (u, v), fglob=fglob,rtol=rtol, 253 fac = self.factor) 254 255 assert abs( (fbest-fglob)/fglob ) < rtol 256 print "sh10", xbest, fbest, ncall
257 258
259 - def test_hm3(self):
260 # Hartman 3 261 u = numpy.array([0, 0, 0]) 262 v = numpy.array([1, 1, 1]) 263 x0 = numpy.array([0.5, 0.5,0.5]) 264 fglob = -3.86278214782076 265 xglob = numpy.array([0.1, 0.55592003, 0.85218259]) 266 xbest, fbest, ncall = snobfit(hm3, x0, (u, v), fglob=fglob,rtol=rtol, 267 fac = self.factor) 268 269 assert abs(( fbest-fglob)/fglob ) < rtol 270 print "hm3", xbest, fbest, ncall
271 272
273 - def test_hm6(self):
274 # Hartman 6 275 u = numpy.array([0, 0, 0, 0, 0, 0]) 276 v = numpy.array([1, 1, 1, 1, 1, 1]) 277 x0 = numpy.array([0.5, 0.5,0.5,0.5,0.5,0.5]) 278 fglob = -3.32236801141551 279 xglob = numpy.array([0.20168952, 0.15001069, 0.47687398, 280 0.27533243, 0.31165162, 0.65730054]) 281 xbest, fbest, ncall = snobfit(hm6, x0, (u, v), fglob=fglob,rtol=rtol, 282 fac = self.factor) 283 284 assert abs( (fbest-fglob)/fglob ) < rtol 285 print "hm6", xbest, fbest, ncall
286 287
288 - def test_shu(self):
289 # Shubert 290 u = numpy.array([-10, -10]) 291 v = numpy.array([10, 10]) 292 x0 = numpy.array([1, 1]) 293 fglob = -186.730908831024 294 xglob = numpy.array([[ -7.08350658, 5.48286415, 4.85805691, 295 4.85805691, -7.08350658, -7.70831382, 296 -1.42512845, -0.80032121, -1.42512844, 297 -7.08350639, -7.70831354, 5.48286415, 298 5.48286415, 4.85805691, -7.70831354, 299 -0.80032121, -1.42512845, -0.80032121], 300 [4.85805691, 4.85805681, -7.08350658, 301 5.48286415, -7.70831382, -7.08350658, 302 -0.80032121, -1.42512845, -7.08350639, 303 -1.42512844, 5.48286415, -7.70831354, 304 4.85805691, 5.48286415, -0.80032121, 305 -7.70831354, -0.80032121, -1.42512845]]) 306 307 xbest, fbest, ncall = snobfit(shu, x0, (u, v), fglob=fglob, 308 fac = self.factor,rtol=rtol 309 ) 310 assert abs( (fbest-fglob)/fglob ) < rtol 311 print "shu", xbest, fbest, ncall
312 313 314 315 # ---------------------------------------------------------------------
316 -class test_Sigma_Case2(test_Sigma_Case1):
317 """ Test case for a simple constrained entropy maximization problem 318 (the machine translation example of Berger et al in 319 Computational Linguistics, vol 22, num 1, pp 39--72, 1996.) 320 """
321 - def setUp(self):
322 self.factor=0.01 323 print "Sigma=0.01"
324 325 326 # ---------------------------------------------------------------------
327 -class test_Sigma_Case3(test_Sigma_Case1):
328 """ Test case for a simple constrained entropy maximization problem 329 (the machine translation example of Berger et al in 330 Computational Linguistics, vol 22, num 1, pp 39--72, 1996.) 331 """
332 - def setUp(self):
333 self.factor=0.1 334 print "Sigma=0.1"
335 336 337 #--------------------------------------------- 338 if __name__ == '__main__': 339 unittest.main() 340