Changeset 612 for branches


Ignore:
Timestamp:
12/17/12 17:14:53 (3 years ago)
Author:
mmckerns
Message:

better substitution of mean and range for symbolic parser

Location:
branches/decorate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/symbolic.py

    r611 r612  
    249249            constraint = fixed.strip() 
    250250 
    251             # Replace 'mean' with actual expression for calculating mean 
     251            # Replace 'range', 'mean', and 'variance' 
     252            if constraint.find('range') != -1: 
     253                constraint = constraint.replace('range', 'ptp') # spread 
    252254            if constraint.find('mean') != -1: 
    253                 constraint = constraint.replace('mean', 'average(asarray(x))') 
    254             # Replace 'range' with actual expression for calculating range 
    255             if constraint.find('range') != -1: 
    256                 constraint = constraint.replace('range', 'max(x) - min(x)') 
     255                constraint = constraint.replace('mean', 'average') # mean 
     256            if constraint.find('variance') != -1: 
     257                constraint = constraint.replace('variance', 'var') # variance 
    257258            #FIXME: also enable functions from mystic.math.measures ? 
    258259 
     
    320321            constraint = fixed.strip() 
    321322 
    322             # Replace 'mean' with actual expression for calculating mean 
     323            # Replace 'range', 'mean', and 'variance' 
     324            if constraint.find('range') != -1: 
     325                constraint = constraint.replace('range', 'ptp') # spread 
    323326            if constraint.find('mean') != -1: 
    324                 constraint = constraint.replace('mean', 'average(asarray(x))') 
    325             # Replace 'range' with actual expression for calculating range 
    326             if constraint.find('range') != -1: 
    327                 constraint = constraint.replace('range', 'max(x) - min(x)') 
     327                constraint = constraint.replace('mean', 'average') # mean 
     328            if constraint.find('variance') != -1: 
     329                constraint = constraint.replace('variance', 'var') # variance 
    328330            #FIXME: also enable functions from mystic.math.measures ? 
    329331 
  • branches/decorate/test_symbolic.py

    r611 r612  
    2222 
    2323 
     24def test_numpy_penalty(): 
     25 
     26  constraints = """ 
     27  mean([x1, x2, x3]) = 5.0      
     28  x1 = x2 + x3""" 
     29 
     30  ineq,eq = generate_conditions(constraints) 
     31  assert eq[0]([7,5,3]) == 0.0 
     32  assert eq[1]([7,4,3]) == 0.0 
     33 
     34  penalty = generate_penalty((ineq,eq)) 
     35  assert penalty([9.0,5,4.0]) == 100.0 
     36  assert penalty([7.5,4,3.5]) == 0.0 
     37 
     38  constraint = as_constraint(penalty, solver='fmin') 
     39  assert almostEqual(penalty(constraint([3,4,5])), 0.0, 1e-10) 
     40 
     41 
    2442if __name__ == '__main__': 
    2543  test_generate_penalty() 
     44  test_numpy_penalty() 
    2645 
Note: See TracChangeset for help on using the changeset viewer.