Package snobfit :: Package tests :: Module testNewClass

Source Code for Module snobfit.tests.testNewClass

  1  import numpy 
  2  import pylab 
  3  from snobfit.snobfit import snobfit 
  4   
5 -def ortho_r(n, seed=0):
6 numpy.random.seed( [seed] ) 7 q,r = numpy.linalg.qr( numpy.random.rand( n,n ) ) 8 return q
9 10
11 -class Fm:
12 """ 13 A new class of test functions for global optimization 14 15 Bernardetta Addis, Marco Locatelli 16 17 Equation 6 18 """
19 - def __init__(self, 20 n = 2, 21 m = 1, 22 K = 10.0, 23 H = 10.0, 24 c1 = -3.0, 25 c2 = 3.0, 26 fac= 0.0, 27 seed=0 28 ):
29 self.n=n 30 self.m=m 31 self.K=K 32 self.H=H 33 self.c1=c1 34 self.c2=c2 35 self.c2_c1 = c2 - c1 36 self.fac = fac 37 self.p=numpy.ones(n) 38 self.o = ortho_r(n)
39
40 - def set_fac(self, fac):
41 self.fac = fac
42
43 - def set_o(self, m):
44 self.o=m
45
46 - def set_p(self, i):
47 if self.p[i]: self.p[i] = 0 48 else: self.p[i] = 1
49 50
51 - def oscillation(self, x):
52 """ 53 Implement Equation(1) 54 """ 55 t = 2.0*numpy.pi * numpy.ceil( (self.K*(self.c2_c1))/10.0 ) 56 return -self.H*numpy.cos( t * ( (x-self.c1)/(self.c2_c1) ) ) + self.H
57 58
59 - def gamma_p(self, p, x):
60 """ 61 Implement Equation(3) 62 """ 63 if p==0: return 0.5*(x-self.c2)**2 + 2 64 else: return 0.5*(x-self.c1)**2 + 2
65 66
67 - def getInvA(self, c):
68 A = numpy.array( [ [1, 0, 0, 0 ], 69 [1, -c, c*c, -c*c*c], 70 [0, 1, 0, 0 ], 71 [0, 1, -2*c, 3*c*c ]] ) 72 return numpy.linalg.inv(A)
73 74
75 - def getAlphaBeta(self, p):
76 A = self.getInvA(self.c1) 77 b = numpy.array( [ p, 5,0,0] ) 78 alpha = numpy.dot( A, b) 79 80 A = self.getInvA(self.c2) 81 b = numpy.array( [ 1-p, 5,0,0] ) 82 beta = numpy.dot( A, b) 83 84 return ( alpha, beta )
85 86
87 - def beta_p(self, p, x):
88 alpha, beta=self.getAlphaBeta( p ) 89 90 y = numpy.zeros( len(x) ) 91 for i in xrange(len(x)): 92 if x[i]<0: 93 y[i] = alpha[0] + \ 94 alpha[1]*( x[i] - self.c1) + \ 95 alpha[2]*( x[i] - self.c1)**2 + \ 96 alpha[3]*( x[i] - self.c1)**3 97 else: 98 y[i] = beta[0] + \ 99 beta[1]*( x[i] - self.c2) + \ 100 beta[2]*( x[i] - self.c2)**2 + \ 101 beta[3]*( x[i] - self.c2)**3 102 return y
103 104
105 - def s_pK(self, p, x):
106 """ 107 Implement Equation(2) 108 """ 109 return self.oscillation(x) + self.gamma_p(p, x)
110 111
112 - def d_pK(self, p, x):
113 """ 114 Implement Equation(4) 115 """ 116 return self.oscillation( x) + self.beta_p( p, x)
117 118
119 - def get_wx(self):
120 wx = numpy.zeros(self.n) 121 for i in xrange(self.n): 122 if (self.p[i]==0 and i< self.m) or \ 123 (self.p[i]==1 and i>= self.m) : 124 wx[i] = self.c1 125 else: 126 wx[i] = self.c2 127 128 return wx
129 130
131 - def getGlobalMinimizer(self):
132 return numpy.dot( numpy.linalg.inv(self.o), self.get_wx() )
133 134
135 - def getGlobalFunc(self):
136 return 2*(self.n-self.m)
137 138 139
140 - def __call__(self, x):
141 wx = numpy.dot(self.o, x) 142 _sum = 0.0 143 for i in xrange(self.m): 144 _sum += self.d_pK( self.p[i], numpy.array([wx[i]]) ) 145 for i in xrange(self.m,self.n): 146 _sum += self.s_pK( self.p[i], numpy.array([wx[i]]) ) 147 148 149 return _sum + self.fac*numpy.random.rand()
150 151 152 153 154 #==================================================================
155 -def testsnob4():
156 157 u = -5.0 * numpy.ones( 4 ) 158 v = 5.0 * numpy.ones( 4 ) 159 x0 = numpy.array( [0.7, 4.5, 2.8, -2.4] ) 160 fglob = 0 161 f = Fm(n=4,m=2) 162 o = ortho_r(4) 163 f.set_o(o) 164 165 print "The global minimizer", f.getGlobalMinimizer() 166 print "The global function value", f.getGlobalFunc() 167 xbest, fbest, ncall = snobfit(f, 168 x0, 169 (u, v), 170 dn=12, 171 fglob=fglob, 172 retall=1 173 ) 174 175 print "The global minimizer", f.getGlobalMinimizer() 176 print "The global function value", f.getGlobalFunc() 177 print "The best solution", xbest,fbest, ncall
178 179
180 -def testsnob16():
181 182 u = -6.0 * numpy.ones( 16 ) 183 v = 6.0 * numpy.ones( 16 ) 184 x0 = numpy.array( [0.6, -0.5, 2.8, 0, -0.58, -5.1, -3.3, 4.0, 185 6, 4.3, 2.58, 0.53, -1.6, 1.3, -0.6, -4.3] ) 186 fglob = 0 187 f = Fm(n=16,m=8) 188 o = ortho_r(16) 189 f.set_o(o) 190 191 print "The global minimizer", f.getGlobalMinimizer() 192 print "The global function value", f.getGlobalFunc() 193 xbest, fbest, ncall = snobfit(f, 194 x0, 195 (u, v), 196 dn=16, 197 fglob=fglob, 198 retall=1 199 ) 200 201 print "The global minimizer", f.getGlobalMinimizer() 202 print "The global function value", f.getGlobalFunc() 203 print "The best solution", xbest,fbest, ncall
204 205 #================================================
206 -def test():
207 u = -5.0 * numpy.ones( 2 ) 208 v = 5.0 * numpy.ones( 2 ) 209 x0 = numpy.array( [0, 1] ) 210 fglob = 0 211 f = Fm() 212 f.set_o( -numpy.array( [ [-0.7880367, -0.61562826], 213 [-0.61562826, 0.7880367 ] ] ) ) 214 xbest, fbest, ncall = snobfit(f, 215 x0, 216 (u, v), 217 fglob=fglob, 218 retall=1 219 ) 220 221 print "The global minimizer", f.getGlobalMinimizer() 222 print "The global function value", f.getGlobalFunc() 223 print "The best solution", xbest,fbest, ncall
224 225
226 -def testnoise():
227 u = -5.0 * numpy.ones( 2 ) 228 v = 5.0 * numpy.ones( 2 ) 229 x0 = numpy.array( [0, 1] ) 230 fglob = 0 231 f = Fm() 232 f.set_o( -numpy.array( [ [-0.7880367, -0.61562826], 233 [-0.61562826, 0.7880367 ] ] ) ) 234 f.set_fac(0.001) 235 236 xbest, fbest, ncall = snobfit(f, 237 x0, 238 (u, v), 239 fglob=fglob, 240 retall=1 241 ) 242 243 print "The global minimizer", f.getGlobalMinimizer() 244 print "The global function value", f.getGlobalFunc() 245 print "The best solution", xbest,fbest, ncall
246 247 248 249 if __name__ == '__main__': 250 #test() 251 testnoise() 252