- Timestamp:
- 12/06/12 13:58:33 (3 years ago)
- Location:
- branches/decorate
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/penalty.py
r602 r605 10 10 if args is None: args=() 11 11 if kwds is None: kwds={} 12 _n = [0] 13 def iter(i=None): 14 if i is None: _n[0] += 1 15 else: _n[0] = i 16 return 17 def get_iter(): 18 return _n[0] 19 def dec(f): 12 _n = [0] # current penalty iteration 13 _f = [lambda x:0.] # decorated function 14 _y = [] # stored results 15 def iter(i=None): 16 if i is None: _n[0] += 1 17 else: _n[0] = i 18 if hasattr(_f[0], 'iter'): _f[0].iter(i) 19 return 20 def iteration(): 21 return _n[0] 22 def store(x,i=None): #XXX: couple to 'iter' as {n:y} ? 23 if hasattr(_f[0], 'store'): _f[0].store(x,i) 24 return 25 def stored(i=None): # can take a slice 26 if i is None: return _y[:] 27 try: return _y[i] 28 except IndexError: return 0.0 29 def clear(): 30 _n[0] = 0 31 [_y.pop() for i in range(len(_y))] 32 if hasattr(_f[0], 'clear'): _f[0].clear() 33 return 34 def dec(f): 35 _f[0] = f 20 36 def func(x, *argz, **kwdz): 21 37 pf = condition(x, *args, **kwds) … … 23 39 return float(_k)*pf**2 + f(x, *argz, **kwdz) 24 40 func.iter = iter 25 func._get_iter = get_iter 41 func.iteration = iteration 42 func.store = store 43 func.clear = clear 44 func.stored = stored 45 return func 46 return dec 47 48 def uniform_equality(condition=lambda x:0., args=None, kwds=None, k=inf, h=5): 49 """apply a uniform penalty if the given equality constraint is violated 50 51 penalty is p(x) = pk, with pk = k*pow(h,n) and n=0 52 where f.iter() can be used to increment n = n+1 53 54 the condition f(x) is satisfied when f(x) == 0.0 55 """ #XXX: this is a rather special case penalty 56 if args is None: args=() 57 if kwds is None: kwds={} 58 _n = [0] # current penalty iteration 59 _f = [lambda x:0.] # decorated function 60 _y = [] # stored results 61 def iter(i=None): 62 if i is None: _n[0] += 1 63 else: _n[0] = i 64 if hasattr(_f[0], 'iter'): _f[0].iter(i) 65 return 66 def iteration(): 67 return _n[0] 68 def store(x,i=None): #XXX: couple to 'iter' as {n:y} ? 69 if hasattr(_f[0], 'store'): _f[0].store(x,i) 70 return 71 def stored(i=None): # can take a slice 72 if i is None: return _y[:] 73 try: return _y[i] 74 except IndexError: return 0.0 75 def clear(): 76 _n[0] = 0 77 [_y.pop() for i in range(len(_y))] 78 if hasattr(_f[0], 'clear'): _f[0].clear() 79 return 80 def dec(f): 81 _f[0] = f 82 def func(x, *argz, **kwdz): 83 pf = condition(x, *args, **kwds) 84 _k = float(k) * pow(h,_n[0]) if pf else 0.0 85 return _k + f(x, *argz, **kwdz) 86 func.iter = iter 87 func.iteration = iteration 88 func.store = store 89 func.clear = clear 90 func.stored = stored 91 return func 92 return dec 93 94 def uniform_inequality(condition=lambda x:0., args=None, kwds=None, k=inf, h=5): 95 """apply a uniform penalty if the given inequality constraint is violated 96 97 penalty is p(x) = pk, with pk = k*pow(h,n) and n=0 98 where f.iter() can be used to increment n = n+1 99 100 the condition f(x) is satisfied when f(x) <= 0.0 101 """ 102 if args is None: args=() 103 if kwds is None: kwds={} 104 _n = [0] # current penalty iteration 105 _f = [lambda x:0.] # decorated function 106 _y = [] # stored results 107 def iter(i=None): 108 if i is None: _n[0] += 1 109 else: _n[0] = i 110 if hasattr(_f[0], 'iter'): _f[0].iter(i) 111 return 112 def iteration(): 113 return _n[0] 114 def store(x,i=None): #XXX: couple to 'iter' as {n:y} ? 115 if hasattr(_f[0], 'store'): _f[0].store(x,i) 116 return 117 def stored(i=None): # can take a slice 118 if i is None: return _y[:] 119 try: return _y[i] 120 except IndexError: return 0.0 121 def clear(): 122 _n[0] = 0 123 [_y.pop() for i in range(len(_y))] 124 if hasattr(_f[0], 'clear'): _f[0].clear() 125 return 126 def dec(f): 127 _f[0] = f 128 def func(x, *argz, **kwdz): 129 pf = condition(x, *args, **kwds) 130 _k = float(k) * pow(h,_n[0]) if pf > 0 else 0.0 131 return _k + f(x, *argz, **kwdz) 132 func.iter = iter 133 func.iteration = iteration 134 func.store = store 135 func.clear = clear 136 func.stored = stored 26 137 return func 27 138 return dec … … 39 150 if args is None: args=() 40 151 if kwds is None: kwds={} 41 _n = [0] 42 def iter(i=None): 43 if i is None: _n[0] += 1 44 else: _n[0] = i 45 return 46 def get_iter(): 47 return _n[0] 48 def dec(f): 152 _n = [0] # current penalty iteration 153 _f = [lambda x:0.] # decorated function 154 _y = [] # stored results 155 def iter(i=None): 156 if i is None: _n[0] += 1 157 else: _n[0] = i 158 if hasattr(_f[0], 'iter'): _f[0].iter(i) 159 return 160 def iteration(): 161 return _n[0] 162 def store(x,i=None): #XXX: couple to 'iter' as {n:y} ? 163 if hasattr(_f[0], 'store'): _f[0].store(x,i) 164 return 165 def stored(i=None): # can take a slice 166 if i is None: return _y[:] 167 try: return _y[i] 168 except IndexError: return 0.0 169 def clear(): 170 _n[0] = 0 171 [_y.pop() for i in range(len(_y))] 172 if hasattr(_f[0], 'clear'): _f[0].clear() 173 return 174 def dec(f): 175 _f[0] = f 49 176 def func(x, *argz, **kwdz): 50 177 pf = condition(x, *args, **kwds) … … 55 182 return -.5/_k*log(-pf) + f(x, *argz, **kwdz) #XXX: use 2*k or k=200? 56 183 func.iter = iter 57 func._get_iter = get_iter 184 func.iteration = iteration 185 func.store = store 186 func.clear = clear 187 func.stored = stored 58 188 return func 59 189 return dec … … 69 199 if args is None: args=() 70 200 if kwds is None: kwds={} 71 _n = [0] 72 def iter(i=None): 73 if i is None: _n[0] += 1 74 else: _n[0] = i 75 return 76 def get_iter(): 77 return _n[0] 78 def dec(f): 201 _n = [0] # current penalty iteration 202 _f = [lambda x:0.] # decorated function 203 _y = [] # stored results 204 def iter(i=None): 205 if i is None: _n[0] += 1 206 else: _n[0] = i 207 if hasattr(_f[0], 'iter'): _f[0].iter(i) 208 return 209 def iteration(): 210 return _n[0] 211 def store(x,i=None): #XXX: couple to 'iter' as {n:y} ? 212 if hasattr(_f[0], 'store'): _f[0].store(x,i) 213 return 214 def stored(i=None): # can take a slice 215 if i is None: return _y[:] 216 try: return _y[i] 217 except IndexError: return 0.0 218 def clear(): 219 _n[0] = 0 220 [_y.pop() for i in range(len(_y))] 221 if hasattr(_f[0], 'clear'): _f[0].clear() 222 return 223 def dec(f): 224 _f[0] = f 79 225 def func(x, *argz, **kwdz): 80 226 pf = condition(x, *args, **kwds) … … 82 228 return float(2*_k)*max(0., pf)**2 + f(x, *argz, **kwdz) #XXX: use 2*k or k=200? 83 229 func.iter = iter 84 func._get_iter = get_iter 230 func.iteration = iteration 231 func.store = store 232 func.clear = clear 233 func.stored = stored 85 234 return func 86 235 return dec … … 97 246 if args is None: args=() 98 247 if kwds is None: kwds={} 99 _n = [0] 100 def iter(i=None): 101 if i is None: _n[0] += 1 102 else: _n[0] = i 103 return 104 def get_iter(): 105 return _n[0] 106 def dec(f): 248 _n = [0] # current penalty iteration 249 _f = [lambda x:0.] # decorated function 250 _y = [] # stored results 251 def iter(i=None): 252 if i is None: _n[0] += 1 253 else: _n[0] = i 254 if hasattr(_f[0], 'iter'): _f[0].iter(i) 255 return 256 def iteration(): 257 return _n[0] 258 def store(x,i=None): #XXX: couple to 'iter' as {n:y} ? 259 y = condition(x, *args, **kwds) 260 l = len(_y) 261 if i is None: i = iteration() 262 if i >= l: _y.extend([0.]*(i-l) + [y]) 263 else: _y[i] = y 264 if hasattr(_f[0], 'store'): _f[0].store(x,i) 265 return 266 def stored(i=None): # can take a slice 267 if i is None: return _y[:] 268 try: return _y[i] 269 except IndexError: return 0.0 270 def clear(): 271 _n[0] = 0 272 [_y.pop() for i in range(len(_y))] 273 if hasattr(_f[0], 'clear'): _f[0].clear() 274 return 275 def dec(f): 276 _f[0] = f 107 277 def func(x, *argz, **kwdz): 108 278 pf = condition(x, *args, **kwds) 109 279 beta = 0.; _k = k 110 280 for i in range(_n[0]): 111 beta += 2.*_k*max(-beta/(2.*_k), pf)281 beta += 2.*_k*max(-beta/(2.*_k), stored(i)) 112 282 _k *= h 113 283 mpf = max(-beta/(2.*_k), pf) 114 284 return float(_k)*mpf**2 + beta*mpf + f(x, *argz, **kwdz) 115 285 func.iter = iter 116 func._get_iter = get_iter 286 func.iteration = iteration 287 func.store = store 288 func.clear = clear 289 func.stored = stored 117 290 return func 118 291 return dec … … 129 302 if args is None: args=() 130 303 if kwds is None: kwds={} 131 _n = [0] 132 def iter(i=None): 133 if i is None: _n[0] += 1 134 else: _n[0] = i 135 return 136 def get_iter(): 137 return _n[0] 138 def dec(f): 304 _n = [0] # current penalty iteration 305 _f = [lambda x:0.] # decorated function 306 _y = [] # stored results 307 def iter(i=None): 308 if i is None: _n[0] += 1 309 else: _n[0] = i 310 if hasattr(_f[0], 'iter'): _f[0].iter(i) 311 return 312 def iteration(): 313 return _n[0] 314 def store(x,i=None): #XXX: couple to 'iter' as {n:y} ? 315 y = condition(x, *args, **kwds) 316 l = len(_y) 317 if i is None: i = iteration() 318 if i >= l: _y.extend([0.]*(i-l) + [y]) 319 else: _y[i] = y 320 if hasattr(_f[0], 'store'): _f[0].store(x,i) 321 return 322 def stored(i=None): # can take a slice 323 if i is None: return _y[:] 324 try: return _y[i] 325 except IndexError: return 0.0 326 def clear(): 327 _n[0] = 0 328 [_y.pop() for i in range(len(_y))] 329 if hasattr(_f[0], 'clear'): _f[0].clear() 330 return 331 def dec(f): 332 _f[0] = f 139 333 def func(x, *argz, **kwdz): 140 334 pf = condition(x, *args, **kwds) 141 335 lam = 0.; _k = k 142 336 for i in range(_n[0]): 143 lam += 2.*_k* pf337 lam += 2.*_k*stored(i) 144 338 _k *= h 145 339 return float(_k)*pf**2 + lam*pf + f(x, *argz, **kwdz) 146 340 func.iter = iter 147 func._get_iter = get_iter 341 func.iteration = iteration 342 func.store = store 343 func.clear = clear 344 func.stored = stored 148 345 return func 149 346 return dec
Note: See TracChangeset
for help on using the changeset viewer.