Package snobfit :: Package tests :: Module test_soft

Source Code for Module snobfit.tests.test_soft

 1  """ Unit tests for Snobfit for a soft constraint 
 2   
 3  Author: Ziwen Fu 
 4  Nov 2008 
 5  """ 
 6   
 7  import unittest 
 8  import numpy 
 9  try: 
10      from snobfit.snobfit import snobfit 
11  except: 
12      from snobfit import snobfit 
13   
14   
15 -def big20(x):
16 f = 0.0 17 for i in xrange(20): 18 f += (x[i]-i*0.5)**2 19 20 return f
21
22 -def hsf18(x):
23 f = 0.01*x[0]**2 + x[1]**2 24 return f
25
26 -def hsc18(x):
27 F = numpy.zeros(2) 28 F[0] = x[0]*x[1] - 25 29 F[1] = x[0]**2 + x[1]**2 - 25 30 F1 = numpy.array( [0, 0] ) 31 F2 = numpy.array( [numpy.inf, numpy.inf] ) 32 return F, F1, F2
33 34 35 36 # ---------------------------------------------------------------------
37 -class test_Snobfit(unittest.TestCase):
38 - def setUp(self):
39 pass
40
41 - def test_hsf18(self):
42 x0 = numpy.array([20, 2]) 43 u = numpy.array( [2,0] ) 44 v = numpy.array( [50,50] ) 45 fglob = 5 46 xbest, fbest, ncall = snobfit(hsf18, x0, (u, v), 47 constraintFunc=hsc18, 48 retall=0, 49 disp=0, 50 fglob=fglob 51 ) 52 53 54 assert abs( (fbest-fglob)/fglob ) < 1.0e-2
55 56 57 58 #--------------------------------------------- 59 if __name__ == '__main__': 60 unittest.main() 61