- Timestamp:
- 12/19/12 21:38:17 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/_symbolic.py
r616 r617 1 1 from symbolic import * 2 from wrapper import isbounded3 from penalty import issolution4 2 from mystic.tools import permutations 3 #from wrapper import isbounded 4 #from penalty import issolution 5 5 6 6 def _classify_variables(constraints, variables='x', nvars=None): … … 38 38 found in the constraints equation string. 39 39 """ 40 if ">" in constraints or "<" in constraints: 41 raise NotImplementedError, "cannot classify inequalities" 42 40 43 #XXX: use simplify_symbolic? or first if not in form xi = ... ? 41 44 if list_or_tuple_or_ndarray(variables): … … 155 158 found in the constraints equation string. 156 159 """ 160 if ">" in constraints or "<" in constraints: 161 raise NotImplementedError, "cannot simplify inequalities" 162 157 163 #XXX: if constraints contain x1,x2,x4 for 'x', should x3 be in code,xlist? 158 164 if list_or_tuple_or_ndarray(variables): … … 255 261 else: ndim = 0 256 262 257 warn = True # if True, don't supress warning about old versions of sympy263 warn = True # if True, don't supress warnings 258 264 if kwds.has_key('warn'): warn = kwds['warn'] 259 265 locals = kwds.get('locals', None) … … 358 364 By default, increasing order (i.e. 1, 2, ...) is used. order 359 365 must enumerate all variables, hence len(order) == nvars. 360 366 """ 367 """ 361 368 Further Inputs: 362 details -- boolean for whether or not to also return 'variable_info'.363 369 guess -- list of parameter values proposed to solve the constraints. 364 370 lower_bounds -- list of lower bounds on solution values. … … 368 374 upper bounds, the constraints function must be formulated in a manner 369 375 such that it does not immediately violate the given bounds. 370 """ 371 """ 376 372 377 Returns a constraints function with the constraints simplified nicely. 373 378 For example, the constraints function is: … … 382 387 inverter/simplifier will probably also work! It just won't get rid of 383 388 redundancies, but that should be ok. 384 """ 385 strict = False # if True, force to use 'permutations' code 386 warn = True # if True, don't supress warning about old versions of sympy 387 verbose = False # if False, keep information to a minimum 388 details = False # if True, print details from _classify_variables 389 guess = None 390 upper_bounds = None 391 lower_bounds = None 389 """ 390 permute = False # if True, force to use 'permutations' code 391 warn = True # if True, don't supress warnings 392 verbose = False # if True, print tempcode and _classify_variables 393 # guess = None 394 # upper_bounds = None 395 # lower_bounds = None 392 396 #-----------------------undocumented------------------------------- 393 if kwds.has_key(' strict'): strict = kwds['strict']397 if kwds.has_key('permute'): permute = kwds['permute'] 394 398 if kwds.has_key('warn'): warn = kwds['warn'] 395 399 if kwds.has_key('verbose'): verbose = kwds['verbose'] 400 # if kwds.has_key('guess'): guess = kwds['guess'] 401 # if kwds.has_key('upper_bounds'): upper_bounds = kwds['upper_bounds'] 402 # if kwds.has_key('lower_bounds'): lower_bounds = kwds['lower_bounds'] 396 403 #------------------------------------------------------------------ 397 if kwds.has_key('details'): details = kwds['details']398 if kwds.has_key('guess'): guess = kwds['guess']399 if kwds.has_key('upper_bounds'): upper_bounds = kwds['upper_bounds']400 if kwds.has_key('lower_bounds'): lower_bounds = kwds['lower_bounds']401 404 402 405 if list_or_tuple_or_ndarray(variables): … … 411 414 else: ndim = 0 412 415 if nvars: ndim = nvars 413 414 415 416 # elif guess: ndim = len(guess) 417 # elif lower_bounds: ndim = len(lower_bounds) 418 # elif upper_bounds: ndim = len(upper_bounds) 416 419 417 420 # The following code attempts to construct something like: … … 429 432 from sympy import Eq, Symbol 430 433 from sympy import solve as symsol 431 except ImportError: 432 if warn: print "Warning: sympy not installed." # Equation will not be simplified."434 except ImportError: # Equation will not be simplified." 435 if warn: print "Warning: sympy not installed." 433 436 solv = generate_solvers(constraints, variables=varname) 434 437 return generate_constraint(solv) … … 443 446 444 447 # Figure out if trying various permutations is necessary 445 446 strict= True448 # if (guess and lower_bounds) or (guess and upper_bounds): 449 # permute = True 447 450 448 451 xinlist = xlist.split(',')[:-1] … … 451 454 xorder = list(asarray(xinlist)[list(order)]) 452 455 453 if strict:456 if permute: 454 457 # If there are strict bounds and initial x value, form a constraints 455 458 # function for each permutation. … … 474 477 475 478 if soln == None and warn: 476 print "Warning: constraints seem to be inconsistent."479 print "Warning: sympy could not simplify equation." 477 480 478 481 solvedstring = "" … … 499 502 solv = generate_solvers(string, variables=varname) 500 503 cf = generate_constraint(solv) 501 if guess: compatible = isbounded(cf, guess, min=lower_bounds, \ 502 max=upper_bounds) 503 else: compatible = True 504 # if guess: compatible = isbounded(cf, guess, min=lower_bounds, \ 505 # max=upper_bounds) 506 # else: compatible = True 507 compatible = True #(due to commented out 'guess') 504 508 if compatible: 505 509 #print string # Debugging 506 if details: 507 info = _classify_variables(string, varname, ndim) 508 return [cf, info] 509 else: 510 return cf# Return the first compatible constraints function 510 if verbose: 511 print _classify_variables(string, varname, ndim) 512 return cf # Return the first compatible constraints function 511 513 else: 512 514 _constraints = cf # Save for later and try other permutations 513 515 # Perhaps raising an Exception is better? But sometimes it's ok... 514 warning='Warning: guess does not satisfy both constraints and bounds.\n\515 Constraints function may be faulty.'516 #warning='Warning: guess incompatible with constraints and bounds.' 517 warning='Warning: an error occurred in building the constraints.' 516 518 if warn: print warning 517 if details: 518 info = _classify_variables(string, varname, ndim) 519 return [cf, info] 519 if verbose: 520 print _classify_variables(string, varname, ndim) 520 521 return cf 521 522 … … 528 529 exec code in globals(), locals() #FIXME: insecure 529 530 if soln == None and warn: 530 print "Warning: constraints seem to be inconsistent."531 print "Warning: sympy could not simplify equation." 531 532 532 533 solvedstring = "" … … 536 537 solv = generate_solvers(solvedstring, variables=varname) 537 538 cf = generate_constraint(solv) 538 if details: 539 info = _classify_variables(solvedstring, varname, ndim) 540 return [cf, info] 539 if verbose: 540 print _classify_variables(solvedstring, varname, ndim) 541 541 return cf 542 542 … … 580 580 By default, increasing order (i.e. 1, 2, ...) is used. order 581 581 must enumerate all variables, hence len(order) == nvars. 582 582 """ 583 """ 583 584 Further Inputs: 584 details -- boolean for whether or not to also return 'variable_info'.585 585 guess -- list of parameter values proposed to solve the constraints. 586 586 lower_bounds -- list of lower bounds on solution values. … … 636 636 By default, increasing order (i.e. 1, 2, ...) is used. order 637 637 must enumerate all variables, hence len(order) == nvars. 638 638 """ 639 """ 639 640 Further Inputs: 640 details -- boolean for whether or not to also return 'variable_info'.641 641 guess -- list of parameter values proposed to solve the constraints. 642 642 lower_bounds -- list of lower bounds on solution values. … … 647 647 such that it does not immediately violate the given bounds. 648 648 """ 649 strict= False # if True, force to use 'permutations' code650 warn = True # if True, don't supress warning about old versions of sympy651 verbose = False # if True, return all permutations652 details= False # if True, print details from _classify_variables653 654 655 649 permute = False # if True, force to use 'permutations' code 650 _all = False # if True, return all permutations 651 warn = True # if True, don't supress warnings 652 verbose = False # if True, print details from _classify_variables 653 # guess = None 654 # upper_bounds = None 655 # lower_bounds = None 656 656 #-----------------------undocumented------------------------------- 657 if kwds.has_key('strict'): strict = kwds['strict'] 657 if kwds.has_key('all'): _all = kwds['all'] 658 if kwds.has_key('permute'): permute = kwds['permute'] 658 659 if kwds.has_key('warn'): warn = kwds['warn'] 659 660 if kwds.has_key('verbose'): verbose = kwds['verbose'] 661 # if kwds.has_key('guess'): guess = kwds['guess'] 662 # if kwds.has_key('upper_bounds'): upper_bounds = kwds['upper_bounds'] 663 # if kwds.has_key('lower_bounds'): lower_bounds = kwds['lower_bounds'] 660 664 #------------------------------------------------------------------ 661 if kwds.has_key('details'): details = kwds['details']662 if kwds.has_key('guess'): guess = kwds['guess']663 if kwds.has_key('upper_bounds'): upper_bounds = kwds['upper_bounds']664 if kwds.has_key('lower_bounds'): lower_bounds = kwds['lower_bounds']665 665 666 666 if list_or_tuple_or_ndarray(variables): … … 675 675 else: ndim = 0 676 676 if nvars: ndim = nvars 677 678 679 677 # elif guess: ndim = len(guess) 678 # elif lower_bounds: ndim = len(lower_bounds) 679 # elif upper_bounds: ndim = len(upper_bounds) 680 680 681 681 # Figure out if trying various permutations is necessary 682 683 strict= True682 # if (guess and lower_bounds) or (guess and upper_bounds): 683 # permute = True 684 684 685 685 eqns = constraints.splitlines() … … 763 763 cf = generate_constraint(solv) 764 764 765 if verbose:765 if _all: 766 766 complete_list.append(cf) 767 767 continue 768 768 769 if strict and guess: 770 try: # catches trying to square root a negative number, for example. 771 cf(guess) 772 except ValueError: 773 continue 774 compatible = isbounded(cf, guess, min=lower_bounds, \ 775 max=upper_bounds) 776 satisfied = issolution(cf, cf(guess)) 777 if compatible and satisfied: 778 if details: 779 info = _classify_variables('\n'.join(simplified), varname, ndim) 780 return [cf, info] 781 else: 782 return cf 783 else: #not strict or not guess 784 if details: 785 info = _classify_variables('\n'.join(simplified), varname, ndim) 786 return [cf, info] 787 else: 788 return cf 789 790 warning='Warning: guess does not satisfy both constraints and bounds.\n\ 791 Constraints may not be enforced correctly. Constraints function may be faulty.' 769 # if permute and guess: 770 # try: # catches trying to square root a negative number, for example. 771 # cf(guess) 772 # except ValueError: 773 # continue 774 # compatible = isbounded(cf, guess, min=lower_bounds, \ 775 # max=upper_bounds) 776 # satisfied = issolution(cf, cf(guess)) 777 # if compatible and satisfied: 778 # if verbose: 779 # print _classify_variables('\n'.join(simplified), varname, ndim) 780 # return cf 781 # else: #not permute or not guess 782 if True: #(due to commented out 'guess') 783 if verbose: 784 print _classify_variables('\n'.join(simplified), varname, ndim) 785 return cf 786 787 # warning='Warning: guess incompatible with constraints and bounds.' 788 warning='Warning: an error occurred in building the constraints.' 792 789 if warn: print warning 793 if details:794 info = _classify_variables('\n'.join(simplified), varname, ndim)795 return [cf, info]796 797 790 if verbose: 791 print _classify_variables('\n'.join(simplified), varname, ndim) 792 if _all: 798 793 return complete_list 799 794 return cf
Note: See TracChangeset
for help on using the changeset viewer.