Changeset 609 for branches


Ignore:
Timestamp:
12/11/12 08:23:06 (3 years ago)
Author:
mmckerns
Message:

rename and migrate wrap, nested, and proxify from wrapper to constraints
add outer_proxy, wrap_constraints, and wrap_penalty methods
proxify methods now take args and kwds

Location:
branches/decorate
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/constraints.py

    r608 r609  
    55from wrapper import * 
    66from mystic.math import almostEqual 
     7 
     8def with_constraint(ctype, *args, **kwds): 
     9    """convert a set transformation to a constraints solver of the chosen type 
     10 
     11transformation f(x) is a mapping between x and x', where x' = f(x). 
     12ctype 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 
     22def 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 
     25This 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 
     36def 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 
     39This is useful, for example, in nesting constraints in a cost function; 
     40thus, 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 
     51def 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 
     54This is useful, for example, in nesting constraints in a cost function; 
     55thus, the constraints will be enforced at every cost function evaluation. 
     56 
     57This function does not preserve decorated function signature, but passes args 
     58and 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 
     69def 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 
     72This is useful, for example, in nesting one constraint in another constraint. 
     73  
     74This function does not preserve decorated function signature, but passes args 
     75and 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 
    785 
    886''' 
  • branches/decorate/penalty.py

    r608 r609  
     1 
     2def with_penalty(ptype, *args, **kwds): 
     3    """convert a condition to a penalty function of the chosen type 
     4 
     5condition f(x) is satisfied when f(x) == 0.0 for equality constraints 
     6and 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 
    116from numpy import inf, log 
    217def quadratic_equality(condition=lambda x:0., args=None, kwds=None, k=100, h=5): 
     
    383398 
    384399#################################################### 
    385 def proxify(penalty=lambda x:0.0): 
     400def proxify(penalty=lambda x:0.0, args=None, kwds=None): 
    386401    """penalize a function with another function: y = f(x) to y' = f(x) + p(x) 
    387402     
     
    391406 
    392407This 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 ? 
     408and 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 
     418def penalize(penalty=lambda x:0.0, args=None, kwds=None): 
    403419    """penalize a function with another function: y = f(x) to y' = f(x) + p(x) 
    404420     
  • branches/decorate/test_constraints.py

    r603 r609  
    22from mystic.math import almostEqual 
    33 
    4 def test_with_constraint(): 
     4def 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 
     18def 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 
     32def 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 
     50def 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 
     66def 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 
     79def 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 
     98def 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 
     117def test_with_mean(): 
    5118 
    6119  from mystic.math.measures import impose_mean 
     
    17130 
    18131 
    19 def test_with_constraints(): 
     132def test_with_mean_spread(): 
    20133 
    21134  from mystic.math.measures import impose_mean, impose_spread 
     
    34147 
    35148 
    36 def test_constrain(): 
     149def test_constrained_solve(): 
    37150 
    38151  @with_spread(5.0) 
     
    54167 
    55168 
     169def 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 
     197def 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 
    56211 
    57212if __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() 
    58224  test_with_constraint() 
    59   test_with_constraints() 
    60   test_constrain() 
    61225 
    62226 
  • branches/decorate/test_penalty.py

    r608 r609  
    130130 
    131131 
     132def 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 
    132150 
    133151if __name__ == '__main__': 
     
    137155  test_as_constraint() 
    138156  test_as_penalty() 
     157  test_with_penalty() 
    139158 
    140159 
  • branches/decorate/test_wrapper.py

    r604 r609  
    11from wrapper import * 
    22from mystic.math import almostEqual 
    3  
    4 def test_wrap(): 
    5  
    6   def squared(x): 
    7     return x**2 
    8  
    9   @wrap(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_nested(): 
    19  
    20   def squared(x): 
    21     return x**2 
    22  
    23   @nested(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_wrap_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   @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 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_nested_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   @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 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   @proxify(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_nested_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   @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 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   @proxify(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  
    1163 
    1174def test_monitored(): 
     
    251138 
    252139 
    253 def test_constrain(): 
    254  
    255   from mystic.math.measures import mean, spread 
    256   from mystic.math.measures import impose_mean, impose_spread 
    257   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 x 
    267  
    268   def cost(x): 
    269     return abs(sum(x) - 5.0) 
    270  
    271   from mystic.solvers import fmin_powell 
    272   from numpy import array 
    273   x = array([1,2,3,4,5]) 
    274   y = fmin_powell(cost, x, constraints=constraints, disp=False) 
    275  
    276   assert mean(y) == 5.0 
    277   assert spread(y) == 5.0 
    278   assert almostEqual(cost(y), 4*(5.0)) 
    279  
    280  
    281  
    282140if __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() 
    290141  test_monitored() 
    291142  test_counted() 
     
    297148  test_target_bounded() 
    298149  test_bounce_bounded() 
    299   test_constrain() 
    300150 
    301151 
  • branches/decorate/wrapper.py

    r608 r609  
    33 
    44#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 func 
    18     return dec 
    19  
    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 func 
    33     return dec 
    34  
    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 args 
    43 and kwds to the inner function.  The decorated function should require a 
    44 single parameter as input. 
    45     """ 
    46     def dec(f): 
    47         def func(*args, **kwds): 
    48             return f(inner(*args, **kwds)) 
    49         return func 
    50     return dec 
    515 
    526 
Note: See TracChangeset for help on using the changeset viewer.