- Timestamp:
- 12/11/12 08:23:06 (3 years ago)
- Location:
- branches/decorate
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/constraints.py
r608 r609 5 5 from wrapper import * 6 6 from mystic.math import almostEqual 7 8 def with_constraint(ctype, *args, **kwds): 9 """convert a set transformation to a constraints solver of the chosen type 10 11 transformation f(x) is a mapping between x and x', where x' = f(x). 12 ctype is a mystic.constraints type [inner, outer, inner_proxy, outer_proxy]. 13 """ 14 def dec(condition): 15 @ctype(condition, *args, **kwds) 16 def constraint(x): 17 return x 18 return constraint 19 return dec 20 21 22 def outer(outer=lambda x:x, args=None, kwds=None): 23 """wrap a function around another function: convert y = f(x) to y' = c(f(x)) 24 25 This is useful, for example, in nesting one constraint in another constraint. 26 """ 27 if args is None: args=() 28 if kwds is None: kwds={} 29 def dec(f): 30 def func(x, *argz, **kwdz): 31 return outer(f(x, *argz, **kwdz), *args, **kwds) 32 return func 33 return dec 34 35 36 def inner(inner=lambda x:x, args=None, kwds=None): 37 """nest a function within another function: convert y = f(x) to y' = f(c(x)) 38 39 This is useful, for example, in nesting constraints in a cost function; 40 thus, the constraints will be enforced at every cost function evaluation. 41 """ 42 if args is None: args=() 43 if kwds is None: kwds={} 44 def dec(f): 45 def func(x, *argz, **kwdz): 46 return f(inner(x, *args, **kwds), *argz, **kwdz) 47 return func 48 return dec 49 50 51 def inner_proxy(inner=lambda x:x, args=None, kwds=None): 52 """nest a function within another function: convert y = f(x) to y' = f(c(x)) 53 54 This is useful, for example, in nesting constraints in a cost function; 55 thus, the constraints will be enforced at every cost function evaluation. 56 57 This function does not preserve decorated function signature, but passes args 58 and kwds to the inner function. 59 """ 60 if args is None: args=() 61 if kwds is None: kwds={} 62 def dec(f): 63 def func(*argz, **kwdz): 64 return f(inner(*argz, **kwdz), *args, **kwds) 65 return func 66 return dec 67 68 69 def outer_proxy(outer=lambda x:x, args=None, kwds=None): 70 """wrap a function around another function: convert y = f(x) to y' = c(f(x)) 71 72 This is useful, for example, in nesting one constraint in another constraint. 73 74 This function does not preserve decorated function signature, but passes args 75 and kwds to the outer function. 76 """ 77 if args is None: args=() 78 if kwds is None: kwds={} 79 def dec(f): 80 def func(x, *argz, **kwdz): 81 return outer(f(x, *args, **kwds), *argz, **kwdz) 82 return func 83 return dec 84 7 85 8 86 ''' -
branches/decorate/penalty.py
r608 r609 1 2 def with_penalty(ptype, *args, **kwds): 3 """convert a condition to a penalty function of the chosen type 4 5 condition f(x) is satisfied when f(x) == 0.0 for equality constraints 6 and f(x) <= 0.0 for inequality constraints. ptype is a mystic.penalty type. 7 """ 8 def dec(condition): 9 @ptype(condition, *args, **kwds) 10 def penalty(x): 11 return 0.0 12 return penalty 13 return dec 14 15 1 16 from numpy import inf, log 2 17 def quadratic_equality(condition=lambda x:0., args=None, kwds=None, k=100, h=5): … … 383 398 384 399 #################################################### 385 def proxify(penalty=lambda x:0.0 ):400 def proxify(penalty=lambda x:0.0, args=None, kwds=None): 386 401 """penalize a function with another function: y = f(x) to y' = f(x) + p(x) 387 402 … … 391 406 392 407 This function does not preserve decorated function signature, but passes args 393 and kwds to the inner function. The decorated function should require a 394 single parameter as input. 395 """ 396 def dec(f): 397 def func(x, *args, **kwds): 398 return f(x) + penalty(x, *args, **kwds) 399 return func 400 return dec 401 402 def penalize(penalty=lambda x:0.0, args=None, kwds=None): #XXX: *args, **kwds ? 408 and kwds to the penalty function. 409 """ 410 if args is None: args=() 411 if kwds is None: kwds={} 412 def dec(f): 413 def func(x, *argz, **kwdz): 414 return f(x, *args, **kwds) + penalty(x, *argz, **kwdz) 415 return func 416 return dec 417 418 def penalize(penalty=lambda x:0.0, args=None, kwds=None): 403 419 """penalize a function with another function: y = f(x) to y' = f(x) + p(x) 404 420 -
branches/decorate/test_constraints.py
r603 r609 2 2 from mystic.math import almostEqual 3 3 4 def test_with_constraint(): 4 def test_outer(): 5 6 def squared(x): 7 return x**2 8 9 @outer(outer=squared) 10 def plus_one_squared(x): 11 return x+1 12 13 from numpy import array 14 x = array([1,2,3,4,5]) 15 assert all(plus_one_squared(x)) == all((x+1)**2) 16 17 18 def test_inner(): 19 20 def squared(x): 21 return x**2 22 23 @inner(inner=squared) 24 def squared_plus_one(x): 25 return x+1 26 27 from numpy import array 28 x = array([1,2,3,4,5]) 29 assert all(squared_plus_one(x)) == all(x**2 + 1) 30 31 32 def test_outer_constraint(): 33 34 from mystic.math.measures import impose_mean, mean 35 36 def impose_constraints(x, mean, weights=None): 37 return impose_mean(mean, x, weights) 38 39 @outer(outer=impose_constraints, kwds={'mean':5.0}) 40 def mean_of_squared(x): 41 return [i**2 for i in x] 42 43 from numpy import array 44 x = array([1,2,3,4,5]) 45 y = impose_mean(5, [i**2 for i in x]) 46 assert mean(y) == 5.0 47 assert mean_of_squared(x) == y 48 49 50 def test_inner_constraint(): 51 52 from mystic.math.measures import impose_mean 53 54 def impose_constraints(x, mean, weights=None): 55 return impose_mean(mean, x, weights) 56 57 @inner(inner=impose_constraints, kwds={'mean':5.0}) 58 def mean_then_squared(x): 59 return [i**2 for i in x] 60 61 from numpy import array 62 x = array([1,2,3,4,5]) 63 assert mean_then_squared(x) == [i**2 for i in impose_mean(5,x)] 64 65 66 def test_proxified_constraint(): 67 68 from mystic.math.measures import impose_mean 69 70 @inner_proxy(inner=impose_mean) 71 def mean_then_squared(x): #XXX: proxy doesn't preserve function signature 72 return [i**2 for i in x] 73 74 from numpy import array 75 x = array([1,2,3,4,5]) 76 assert mean_then_squared(5,x) == [i**2 for i in impose_mean(5,x)] 77 78 79 def test_inner_constraints(): 80 81 from mystic.math.measures import impose_mean, impose_spread 82 83 def impose_constraints(x, mean=0.0, spread=1.0): 84 x = impose_mean(mean, x) 85 x = impose_spread(spread, x) 86 return x 87 88 @inner(inner=impose_constraints, kwds={'mean':5.0, 'spread':50.0}) 89 def constrained_squared(x): 90 return [i**2 for i in x] 91 92 from numpy import array 93 x = array([1,2,3,4,5]) 94 y = impose_spread(50.0, impose_mean(5.0,x)) 95 assert constrained_squared(x) == [i**2 for i in y] 96 97 98 def test_proxified_constraints(): 99 100 from mystic.math.measures import impose_mean, impose_spread 101 102 def impose_constraints(x, mean=0.0, spread=1.0): 103 x = impose_mean(mean, x) 104 x = impose_spread(spread, x) 105 return x 106 107 @inner_proxy(inner=impose_constraints) 108 def constrained_squared(x): #XXX: proxy doesn't preserve function signature 109 return [i**2 for i in x] 110 111 from numpy import array 112 x = array([1,2,3,4,5]) 113 y = impose_spread(50.0, impose_mean(5.0,x)) 114 assert constrained_squared(x, 5.0, 50.0) == [i**2 for i in y] 115 116 117 def test_with_mean(): 5 118 6 119 from mystic.math.measures import impose_mean … … 17 130 18 131 19 def test_with_ constraints():132 def test_with_mean_spread(): 20 133 21 134 from mystic.math.measures import impose_mean, impose_spread … … 34 147 35 148 36 def test_constrain ():149 def test_constrained_solve(): 37 150 38 151 @with_spread(5.0) … … 54 167 55 168 169 def test_constrain(): 170 171 from mystic.math.measures import mean, spread 172 from mystic.math.measures import impose_mean, impose_spread 173 def mean_constraint(x, mean=0.0): 174 return impose_mean(mean, x) 175 176 def range_constraint(x, spread=1.0): 177 return impose_spread(spread, x) 178 179 @inner(inner=range_constraint, kwds={'spread':5.0}) 180 @inner(inner=mean_constraint, kwds={'mean':5.0}) 181 def constraints(x): 182 return x 183 184 def cost(x): 185 return abs(sum(x) - 5.0) 186 187 from mystic.solvers import fmin_powell 188 from numpy import array 189 x = array([1,2,3,4,5]) 190 y = fmin_powell(cost, x, constraints=constraints, disp=False) 191 192 assert mean(y) == 5.0 193 assert spread(y) == 5.0 194 assert almostEqual(cost(y), 4*(5.0)) 195 196 197 def test_with_constraint(): 198 199 from mystic.math.measures import impose_mean 200 201 @with_constraint(inner, kwds={'target':5.0}) 202 def mean_of_squared(x, target): 203 return impose_mean(target, [i**2 for i in x]) 204 205 from numpy import array 206 x = array([1,2,3,4,5]) 207 y = impose_mean(5, [i**2 for i in x]) 208 assert mean(y) == 5.0 209 assert mean_of_squared(x) == y 210 56 211 57 212 if __name__ == '__main__': 213 test_outer() 214 test_inner() 215 test_outer_constraint() 216 test_inner_constraint() 217 test_proxified_constraint() 218 test_inner_constraints() 219 test_proxified_constraints() 220 test_with_mean() 221 test_with_mean_spread() 222 test_constrained_solve() 223 test_constrain() 58 224 test_with_constraint() 59 test_with_constraints()60 test_constrain()61 225 62 226 -
branches/decorate/test_penalty.py
r608 r609 130 130 131 131 132 def test_with_penalty(): 133 134 from mystic.math.measures import mean, spread 135 @with_penalty(quadratic_equality, kwds={'target':5.0}) 136 def penalty(x, target): 137 return mean(x) - target 138 139 def cost(x): 140 return abs(sum(x) - 5.0) 141 142 from mystic.solvers import fmin_powell 143 from numpy import array 144 x = array([1,2,3,4,5]) 145 y = fmin_powell(cost, x, penalty=penalty, disp=False) 146 147 assert round(mean(y)) == 5.0 148 assert round(cost(y)) == 4*(5.0) 149 132 150 133 151 if __name__ == '__main__': … … 137 155 test_as_constraint() 138 156 test_as_penalty() 157 test_with_penalty() 139 158 140 159 -
branches/decorate/test_wrapper.py
r604 r609 1 1 from wrapper import * 2 2 from mystic.math import almostEqual 3 4 def test_wrap():5 6 def squared(x):7 return x**28 9 @wrap(outer=squared)10 def plus_one_squared(x):11 return x+112 13 from numpy import array14 x = array([1,2,3,4,5])15 assert all(plus_one_squared(x)) == all((x+1)**2)16 17 18 def test_nested():19 20 def squared(x):21 return x**222 23 @nested(inner=squared)24 def squared_plus_one(x):25 return x+126 27 from numpy import array28 x = array([1,2,3,4,5])29 assert all(squared_plus_one(x)) == all(x**2 + 1)30 31 32 def test_wrap_constraint():33 34 from mystic.math.measures import impose_mean, mean35 36 def impose_constraints(x, mean, weights=None):37 return impose_mean(mean, x, weights)38 39 @wrap(outer=impose_constraints, kwds={'mean':5.0})40 def mean_of_squared(x):41 return [i**2 for i in x]42 43 from numpy import array44 x = array([1,2,3,4,5])45 y = impose_mean(5, [i**2 for i in x])46 assert mean(y) == 5.047 assert mean_of_squared(x) == y48 49 50 def test_nested_constraint():51 52 from mystic.math.measures import impose_mean53 54 def impose_constraints(x, mean, weights=None):55 return impose_mean(mean, x, weights)56 57 @nested(inner=impose_constraints, kwds={'mean':5.0})58 def mean_then_squared(x):59 return [i**2 for i in x]60 61 from numpy import array62 x = array([1,2,3,4,5])63 assert mean_then_squared(x) == [i**2 for i in impose_mean(5,x)]64 65 66 def test_proxified_constraint():67 68 from mystic.math.measures import impose_mean69 70 @proxify(inner=impose_mean)71 def mean_then_squared(x): #XXX: proxy doesn't preserve function signature72 return [i**2 for i in x]73 74 from numpy import array75 x = array([1,2,3,4,5])76 assert mean_then_squared(5,x) == [i**2 for i in impose_mean(5,x)]77 78 79 def test_nested_constraints():80 81 from mystic.math.measures import impose_mean, impose_spread82 83 def impose_constraints(x, mean=0.0, spread=1.0):84 x = impose_mean(mean, x)85 x = impose_spread(spread, x)86 return x87 88 @nested(inner=impose_constraints, kwds={'mean':5.0, 'spread':50.0})89 def constrained_squared(x):90 return [i**2 for i in x]91 92 from numpy import array93 x = array([1,2,3,4,5])94 y = impose_spread(50.0, impose_mean(5.0,x))95 assert constrained_squared(x) == [i**2 for i in y]96 97 98 def test_proxified_constraints():99 100 from mystic.math.measures import impose_mean, impose_spread101 102 def impose_constraints(x, mean=0.0, spread=1.0):103 x = impose_mean(mean, x)104 x = impose_spread(spread, x)105 return x106 107 @proxify(inner=impose_constraints)108 def constrained_squared(x): #XXX: proxy doesn't preserve function signature109 return [i**2 for i in x]110 111 from numpy import array112 x = array([1,2,3,4,5])113 y = impose_spread(50.0, impose_mean(5.0,x))114 assert constrained_squared(x, 5.0, 50.0) == [i**2 for i in y]115 116 3 117 4 def test_monitored(): … … 251 138 252 139 253 def test_constrain():254 255 from mystic.math.measures import mean, spread256 from mystic.math.measures import impose_mean, impose_spread257 def mean_constraint(x, mean=0.0):258 return impose_mean(mean, x)259 260 def range_constraint(x, spread=1.0):261 return impose_spread(spread, x)262 263 @nested(inner=range_constraint, kwds={'spread':5.0})264 @nested(inner=mean_constraint, kwds={'mean':5.0})265 def constraints(x):266 return x267 268 def cost(x):269 return abs(sum(x) - 5.0)270 271 from mystic.solvers import fmin_powell272 from numpy import array273 x = array([1,2,3,4,5])274 y = fmin_powell(cost, x, constraints=constraints, disp=False)275 276 assert mean(y) == 5.0277 assert spread(y) == 5.0278 assert almostEqual(cost(y), 4*(5.0))279 280 281 282 140 if __name__ == '__main__': 283 test_wrap()284 test_nested()285 test_wrap_constraint()286 test_nested_constraint()287 test_proxified_constraint()288 test_nested_constraints()289 test_proxified_constraints()290 141 test_monitored() 291 142 test_counted() … … 297 148 test_target_bounded() 298 149 test_bounce_bounded() 299 test_constrain()300 150 301 151 -
branches/decorate/wrapper.py
r608 r609 3 3 4 4 #XXX: when_decorated registers methods to 'populate up' when is decorated ? 5 6 7 def wrap(outer=lambda x:x, args=None, kwds=None): #XXX: *args, **kwds ?8 """wrap a function around another function: convert y = f(x) to y' = c(f(x))9 10 This is useful, for example, in nesting one constraint in another constraint.11 """12 if args is None: args=()13 if kwds is None: kwds={}14 def dec(f):15 def func(x, *argz, **kwdz):16 return outer(f(x, *argz, **kwdz), *args, **kwds)17 return func18 return dec19 20 21 def nested(inner=lambda x:x, args=None, kwds=None): #XXX: *args, **kwds ?22 """nest a function within another function: convert y = f(x) to y' = f(c(x))23 24 This is useful, for example, in nesting constraints in a cost function;25 thus, the constraints will be enforced at every cost function evaluation.26 """27 if args is None: args=()28 if kwds is None: kwds={}29 def dec(f):30 def func(x, *argz, **kwdz):31 return f(inner(x, *args, **kwds), *argz, **kwdz)32 return func33 return dec34 35 36 def proxify(inner=lambda x:x):37 """nest a function within another function: convert y = f(x) to y' = f(c(x))38 39 This is useful, for example, in nesting constraints in a cost function;40 thus, the constraints will be enforced at every cost function evaluation.41 42 This function does not preserve decorated function signature, but passes args43 and kwds to the inner function. The decorated function should require a44 single parameter as input.45 """46 def dec(f):47 def func(*args, **kwds):48 return f(inner(*args, **kwds))49 return func50 return dec51 5 52 6
Note: See TracChangeset
for help on using the changeset viewer.