Changeset 613 for branches


Ignore:
Timestamp:
12/18/12 10:25:00 (3 years ago)
Author:
mmckerns
Message:

added impose_product and impose_sum to mystic.math.measures;
allow generate_constraint to use mystic.math.measures

Location:
branches/decorate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/symbolic.py

    r612 r613  
    9292Inputs: 
    9393    constraints -- a string of symbolic constraints, with one constraint 
    94         equation per line. Constraints can be equality, inequality, mean, 
    95         and/or range constraints. Standard python syntax rules should be 
    96         followed (with the math module already imported). 
     94        equation per line. Constraints can be equality and/or inequality 
     95        constraints. Standard python syntax should be followed (with the 
     96        math and numpy modules already imported). 
    9797    variables -- list of variable name strings. The variable names will 
    9898        be replaced in the order that they are provided, where if the 
     
    164164Inputs: 
    165165    constraints -- a string of symbolic constraints, with one constraint 
    166         equation per line. Constraints can be equality, inequality, mean, 
    167         and/or range constraints. Standard python syntax rules should be 
    168         followed (with the math module already imported). 
     166        equation per line. Constraints can be equality and/or inequality 
     167        constraints. Standard python syntax should be followed (with the 
     168        math and numpy modules already imported). 
    169169 
    170170    For example: 
     
    249249            constraint = fixed.strip() 
    250250 
    251             # Replace 'range', 'mean', and 'variance' 
    252             if constraint.find('range') != -1: 
    253                 constraint = constraint.replace('range', 'ptp') # spread 
    254             if constraint.find('mean') != -1: 
    255                 constraint = constraint.replace('mean', 'average') # mean 
    256             if constraint.find('variance') != -1: 
    257                 constraint = constraint.replace('variance', 'var') # variance 
    258             #FIXME: also enable functions from mystic.math.measures ? 
     251            # Replace 'range', 'mean', and 'variance' (uses numpy, not mystic) 
     252            if constraint.find('range(') != -1: 
     253                constraint = constraint.replace('range(', 'ptp(') # spread 
     254            if constraint.find('mean(') != -1: 
     255                constraint = constraint.replace('mean(', 'average(') # mean 
     256            if constraint.find('variance(') != -1: 
     257                constraint = constraint.replace('variance(', 'var(') # variance 
    259258 
    260259            # Sorting into equality and inequality constraints, and making all 
     
    321320            constraint = fixed.strip() 
    322321 
    323             # Replace 'range', 'mean', and 'variance' 
    324             if constraint.find('range') != -1: 
    325                 constraint = constraint.replace('range', 'ptp') # spread 
    326             if constraint.find('mean') != -1: 
    327                 constraint = constraint.replace('mean', 'average') # mean 
    328             if constraint.find('variance') != -1: 
    329                 constraint = constraint.replace('variance', 'var') # variance 
    330             #FIXME: also enable functions from mystic.math.measures ? 
     322            # Replace 'range', 'mean', and 'variance' (uses mystic, not numpy) 
     323            if constraint.find('range(') != -1: 
     324                constraint = constraint.replace('range(', 'spread(') 
     325            if constraint.find('ptp(') != -1: 
     326                constraint = constraint.replace('ptp(', 'spread(') 
     327            if constraint.find('average(') != -1: 
     328                constraint = constraint.replace('average(', 'mean(') 
     329            if constraint.find('var(') != -1: 
     330                constraint = constraint.replace('var(', 'variance(') 
     331            if constraint.find('prod(') != -1: 
     332                constraint = constraint.replace('prod(', 'product(') 
    331333 
    332334            #XXX: below this line the code is different than penalty_parser 
     
    345347                   'rhs':split[-1].lstrip('=').strip()} 
    346348            expression = expression % eqn 
     349 
     350            # allow mystic.math.measures impose_* on LHS 
     351            lhs,rhs = expression.split('=') 
     352            if lhs.find('spread(') != -1: 
     353              lhs = lhs.split('spread')[-1] 
     354              rhs = ' impose_spread( (' + rhs.lstrip() + '),' + lhs + ')' 
     355            if lhs.find('mean(') != -1: 
     356              lhs = lhs.split('mean')[-1] 
     357              rhs = ' impose_mean( (' + rhs.lstrip() + '),' + lhs + ')' 
     358            if lhs.find('variance(') != -1: 
     359              lhs = lhs.split('variance')[-1] 
     360              rhs = ' impose_variance( (' + rhs.lstrip() + '),' + lhs + ')' 
     361            if lhs.find('sum(') != -1: 
     362              lhs = lhs.split('sum')[-1] 
     363              rhs = ' impose_sum( (' + rhs.lstrip() + '),' + lhs + ')' 
     364            if lhs.find('product(') != -1: 
     365              lhs = lhs.split('product')[-1] 
     366              rhs = ' impose_product( (' + rhs.lstrip() + '),' + lhs + ')' 
     367            expression = "=".join([lhs,rhs]) 
     368 
    347369            parsed.append(expression) 
    348370 
     
    355377Inputs: 
    356378    constraints -- a string of symbolic constraints, with one constraint 
    357         equation per line. Constraints can be equality, inequality, mean, 
    358         and/or range constraints. Standard python syntax rules should be 
    359         followed (with the math module already imported). 
     379        equation per line. Constraints can be equality and/or inequality 
     380        constraints. Standard python syntax should be followed (with the 
     381        math and numpy modules already imported). 
    360382 
    361383    For example: 
     
    388410    # default is globals with numpy and math imported 
    389411    globals = {} 
    390     code = """from math import *; from numpy import *""" 
    391    #code += """from mystic.math.measures import *""" #FIXME: enable mystic code? 
     412    code = """from math import *; from numpy import *;""" 
     413   #code += """from mystic.math.measures import spread, variance, mean;""" 
    392414    code = compile(code, '<string>', 'exec') 
    393415    exec code in globals 
     
    424446Inputs: 
    425447    constraints -- a string of symbolic constraints, with one constraint 
    426         equation per line. Constraints can be equality, inequality, mean, 
    427         and/or range constraints. Standard python syntax rules should be 
    428         followed (with the math module already imported).  The left-hand 
    429         side of each equation must be simplified to support assignment. 
     448        equation per line. Constraints can be equality and/or inequality 
     449        constraints. Standard python syntax should be followed (with the 
     450        math and numpy modules already imported). The left-hand side of 
     451        each equation must be simplified to support assignment. 
    430452 
    431453    For example: 
     
    458480    # default is globals with numpy and math imported 
    459481    globals = {} 
    460     code = """from math import *; from numpy import *""" 
    461    #code += """from mystic.math.measures import *""" #FIXME: enable mystic code? 
     482    code = """from math import *; from numpy import *;""" 
     483    code += """from mystic.math.measures import spread, variance, mean;""" 
     484    code += """from mystic.math.measures import impose_spread, impose_mean;""" 
     485    code += """from mystic.math.measures import impose_sum, impose_product;""" 
     486    code += """from mystic.math.measures import impose_variance;""" 
    462487    code = compile(code, '<string>', 'exec') 
    463488    exec code in globals 
  • branches/decorate/test_symbolic.py

    r612 r613  
    66 
    77  constraints = """ 
    8   x1**2 = 2.5*x4 - a       
     8  x1**2 = 2.5*x4 - a 
    99  exp(x3/x1) >= b""" 
    1010 
     
    2121  assert almostEqual(penalty(constraint([1,0,0,2.4])), 0.0, 1e-10) 
    2222 
    23  
    2423def test_numpy_penalty(): 
    2524 
    2625  constraints = """ 
    27   mean([x1, x2, x3]) = 5.0      
     26  mean([x1, x2, x3]) = 5.0 
    2827  x1 = x2 + x3""" 
    2928 
     
    3938  assert almostEqual(penalty(constraint([3,4,5])), 0.0, 1e-10) 
    4039 
     40def test_impose_penalty(): 
     41 
     42  constraints = """ 
     43  mean([x1, x2, x3]) = 5.0 
     44  range([x1, x2, x3]) = 10.0""" 
     45 
     46  from mystic.math.measures import mean, spread 
     47  solv = generate_solvers(constraints) 
     48  assert almostEqual(mean(solv[0]([1,2,3])), 5.0) 
     49  assert almostEqual(spread(solv[1]([1,2,3])), 10.0) 
     50 
     51  constraint = generate_constraint(solv) 
     52  assert almostEqual(constraint([1,2,3]), [0.0,5.0,10.0], 1e-10) 
     53 
    4154 
    4255if __name__ == '__main__': 
    4356  test_generate_penalty() 
    4457  test_numpy_penalty() 
     58  test_impose_penalty() 
    4559 
Note: See TracChangeset for help on using the changeset viewer.