Changeset 600 for branches


Ignore:
Timestamp:
11/25/12 05:10:28 (3 years ago)
Author:
mmckerns
Message:

add optimization test for penalty and (nested) constraints decorators

Location:
branches/decorate
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/test_wrapper.py

    r598 r600  
    11from wrapper import * 
     2from mystic.math import almostEqual 
    23 
    34def test_nested(): 
     
    155156  from numpy import array, arange 
    156157  x = range(11) 
    157   assert round(mean(set_mean(x, 5.0)), 15) == 5.0 
     158  assert almostEqual(mean(set_mean(x, 5.0)), 5.0) 
    158159 
    159160 
     
    181182  discrete_squared.samples([1.0, 7.0]) 
    182183  assert discrete_squared(5.6) == 7.0**2 
     184 
     185 
     186def test_constrain(): 
     187 
     188  from mystic.math.measures import mean, spread 
     189  from mystic.math.measures import impose_mean, impose_spread 
     190  def mean_constraint(x): 
     191    return impose_mean(5.0, x) 
     192 
     193  def range_constraint(x): 
     194    return impose_spread(5.0, x) 
     195 
     196  @nested(inner=range_constraint)  #XXX: @with_range(5.0) 
     197  @nested(inner=mean_constraint)   #XXX: @with_mean(5.0) 
     198  def constraints(x): 
     199    return x 
     200 
     201  def cost(x): 
     202    return abs(sum(x) - 5.0) 
     203 
     204  from mystic.solvers import fmin_powell 
     205  from numpy import array 
     206  x = array([1,2,3,4,5]) 
     207  y = fmin_powell(cost, x, constraints=constraints, disp=False) 
     208 
     209  assert mean(y) == 5.0 
     210  assert spread(y) == 5.0 
     211  assert almostEqual(cost(y), 4*(5.0)) 
    183212 
    184213 
     
    197226  test_target_bounded() 
    198227  test_bounce_bounded() 
     228  test_constrain() 
    199229 
    200230 
Note: See TracChangeset for help on using the changeset viewer.