- Timestamp:
- 12/06/12 17:42:32 (3 years ago)
- Location:
- branches/decorate
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/penalty.py
r605 r606 13 13 _f = [lambda x:0.] # decorated function 14 14 _y = [] # stored results 15 def error(x): 16 rms = condition(x, *args, **kwds)**2 17 if hasattr(_f[0], 'error'): rms += _f[0].error(x)**2 18 return rms**0.5 15 19 def iter(i=None): 16 20 if i is None: _n[0] += 1 … … 43 47 func.clear = clear 44 48 func.stored = stored 49 func.error = error 45 50 return func 46 51 return dec … … 59 64 _f = [lambda x:0.] # decorated function 60 65 _y = [] # stored results 66 def error(x): 67 rms = condition(x, *args, **kwds)**2 68 if hasattr(_f[0], 'error'): rms += _f[0].error(x)**2 69 return rms**0.5 61 70 def iter(i=None): 62 71 if i is None: _n[0] += 1 … … 89 98 func.clear = clear 90 99 func.stored = stored 100 func.error = error 91 101 return func 92 102 return dec … … 105 115 _f = [lambda x:0.] # decorated function 106 116 _y = [] # stored results 117 def error(x): 118 rms = max(0., condition(x, *args, **kwds))**2 119 if hasattr(_f[0], 'error'): rms += _f[0].error(x)**2 120 return rms**0.5 107 121 def iter(i=None): 108 122 if i is None: _n[0] += 1 … … 135 149 func.clear = clear 136 150 func.stored = stored 151 func.error = error 137 152 return func 138 153 return dec … … 153 168 _f = [lambda x:0.] # decorated function 154 169 _y = [] # stored results 170 def error(x): 171 rms = max(0., condition(x, *args, **kwds))**2 172 if hasattr(_f[0], 'error'): rms += _f[0].error(x)**2 173 return rms**0.5 155 174 def iter(i=None): 156 175 if i is None: _n[0] += 1 … … 186 205 func.clear = clear 187 206 func.stored = stored 207 func.error = error 188 208 return func 189 209 return dec … … 202 222 _f = [lambda x:0.] # decorated function 203 223 _y = [] # stored results 224 def error(x): 225 rms = max(0., condition(x, *args, **kwds))**2 226 if hasattr(_f[0], 'error'): rms += _f[0].error(x)**2 227 return rms**0.5 204 228 def iter(i=None): 205 229 if i is None: _n[0] += 1 … … 232 256 func.clear = clear 233 257 func.stored = stored 258 func.error = error 234 259 return func 235 260 return dec … … 249 274 _f = [lambda x:0.] # decorated function 250 275 _y = [] # stored results 276 def error(x): 277 rms = max(0., condition(x, *args, **kwds))**2 278 if hasattr(_f[0], 'error'): rms += _f[0].error(x)**2 279 return rms**0.5 251 280 def iter(i=None): 252 281 if i is None: _n[0] += 1 … … 288 317 func.clear = clear 289 318 func.stored = stored 319 func.error = error 290 320 return func 291 321 return dec … … 305 335 _f = [lambda x:0.] # decorated function 306 336 _y = [] # stored results 337 def error(x): 338 rms = condition(x, *args, **kwds)**2 339 if hasattr(_f[0], 'error'): rms += _f[0].error(x)**2 340 return rms**0.5 307 341 def iter(i=None): 308 342 if i is None: _n[0] += 1 … … 343 377 func.clear = clear 344 378 func.stored = stored 379 func.error = error 345 380 return func 346 381 return dec … … 384 419 385 420 421 def issolution(penalty, candidate, tol=1e-3): 422 """check if the candidate is a solution to penalty constraints 423 424 penalty: a penalty function 425 candidate: a candidate solution 426 """ 427 if penalty.error(candidate) <= tol: return True 428 return False 429 430 #XXX: nice if penalty.error could give error for each condition... or total 431 386 432 387 433 # EOF -
branches/decorate/test_restarts.py
r605 r606 1 1 #!/usr/bin/env python 2 from mystic.constraints import issolution3 2 from restarts import sumt 4 3 from penalty import * … … 6 5 random_seed(123) 7 6 8 def test_sumt1( ):7 def test_sumt1(verbose=False): 9 8 10 9 def constraint1(x): … … 25 24 0.25*x1**2 + 0.75*x2**2 - 1. <= 0. 26 25 """ 27 print "constraints equations:%s" % (constraints_string.rstrip(),) 26 if verbose: 27 print "constraints equations:%s" % (constraints_string.rstrip(),) 28 28 29 29 def costfunc(x): … … 43 43 solver.SetPenalty(penalty) 44 44 term = VTR() 45 end_solver = sumt(costfunc, solver, term, disp= True)45 end_solver = sumt(costfunc, solver, term, disp=verbose) 46 46 soln = end_solver.Solution() 47 print "final answer:", soln 48 print "constraints satisfied:", issolution(constraints_string, soln) 49 print "expected: [1., 1.]", "\n" 47 satisfied = issolution(penalty, soln) 48 if verbose: 49 print "final answer:", soln 50 print "constraints satisfied:", satisfied 51 print "expected: [1., 1.]", "\n" 52 else: assert satisfied 50 53 51 def test_sumt2(): 54 55 def test_sumt2(verbose=False): 52 56 def constraint1(x): 53 57 x1,x2,x3 = x[0],x[1],x[2] … … 73 77 (x1-10.)**2 + (x2+1.)**2 < 50. 74 78 """ 75 print "constraints equations:%s" % (constraints_string.rstrip(),) 79 if verbose: 80 print "constraints equations:%s" % (constraints_string.rstrip(),) 76 81 77 82 def costfunc(x): … … 88 93 solver.SetPenalty(penalty) 89 94 term = VTR() 90 end_solver = sumt(costfunc, solver, term, disp= True)95 end_solver = sumt(costfunc, solver, term, disp=verbose) 91 96 soln = end_solver.Solution() 92 print "final answer:", soln 93 print "constraints satisfied:", issolution(constraints_string, soln) 94 print "expected: [ 6.25827968 4.999961 5.20662288]", "\n" 97 satisfied = issolution(penalty, soln) 98 if verbose: 99 print "final answer:", soln 100 print "constraints satisfied:", satisfied 101 print "expected: [ 6.25827968 4.999961 5.20662288]", "\n" 102 else: assert satisfied 103 95 104 96 105 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.