- Timestamp:
- 12/18/12 10:25:00 (3 years ago)
- Location:
- branches/decorate
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/symbolic.py
r612 r613 92 92 Inputs: 93 93 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 be96 followed (with the math modulealready 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). 97 97 variables -- list of variable name strings. The variable names will 98 98 be replaced in the order that they are provided, where if the … … 164 164 Inputs: 165 165 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 be168 followed (with the math modulealready 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). 169 169 170 170 For example: … … 249 249 constraint = fixed.strip() 250 250 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 259 258 260 259 # Sorting into equality and inequality constraints, and making all … … 321 320 constraint = fixed.strip() 322 321 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(') 331 333 332 334 #XXX: below this line the code is different than penalty_parser … … 345 347 'rhs':split[-1].lstrip('=').strip()} 346 348 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 347 369 parsed.append(expression) 348 370 … … 355 377 Inputs: 356 378 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 be359 followed (with the math modulealready 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). 360 382 361 383 For example: … … 388 410 # default is globals with numpy and math imported 389 411 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;""" 392 414 code = compile(code, '<string>', 'exec') 393 415 exec code in globals … … 424 446 Inputs: 425 447 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 be428 followed (with the math module already imported). The left-hand429 side ofeach 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. 430 452 431 453 For example: … … 458 480 # default is globals with numpy and math imported 459 481 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;""" 462 487 code = compile(code, '<string>', 'exec') 463 488 exec code in globals -
branches/decorate/test_symbolic.py
r612 r613 6 6 7 7 constraints = """ 8 x1**2 = 2.5*x4 - a 8 x1**2 = 2.5*x4 - a 9 9 exp(x3/x1) >= b""" 10 10 … … 21 21 assert almostEqual(penalty(constraint([1,0,0,2.4])), 0.0, 1e-10) 22 22 23 24 23 def test_numpy_penalty(): 25 24 26 25 constraints = """ 27 mean([x1, x2, x3]) = 5.0 26 mean([x1, x2, x3]) = 5.0 28 27 x1 = x2 + x3""" 29 28 … … 39 38 assert almostEqual(penalty(constraint([3,4,5])), 0.0, 1e-10) 40 39 40 def 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 41 54 42 55 if __name__ == '__main__': 43 56 test_generate_penalty() 44 57 test_numpy_penalty() 58 test_impose_penalty() 45 59
Note: See TracChangeset
for help on using the changeset viewer.