Changeset 819


Ignore:
Timestamp:
08/12/15 08:26:54 (9 months ago)
Author:
mmckerns
Message:

use mystic.constraints.integers in integer_programming examples;
add docs to mystic.constraints.unique (and impose_unique)

Location:
mystic
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples2/integer_programming.py

    r818 r819  
    3939pf = generate_penalty(generate_conditions(equations)) 
    4040 
    41 from numpy import round as npround 
     41from mystic.constraints import integers 
     42 
     43@integers() 
     44def round(x): 
     45  return x 
     46 
    4247 
    4348if __name__ == '__main__': 
     
    4651    from mystic.math import almostEqual 
    4752 
    48     result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraints=npround, npop=20, gtol=50, disp=True, full_output=True) 
     53    result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraints=round, npop=20, gtol=50, disp=True, full_output=True) 
    4954 
    5055    print result[0] 
  • mystic/examples2/integer_programming_alt.py

    r818 r819  
    3838    return round(solver(x)) 
    3939 
    40 from numpy import round as npround 
     40# better is to constrain to integers, penalize otherwise 
     41from mystic.constraints import integers 
     42 
     43@integers() 
     44def round(x): 
     45  return x 
     46 
    4147 
    4248if __name__ == '__main__': 
     
    4551    from mystic.math import almostEqual 
    4652 
    47     result = diffev2(objective, x0=bounds, bounds=bounds, penalty=penalty, constraints=npround, npop=20, gtol=50, disp=True, full_output=True) 
     53    result = diffev2(objective, x0=bounds, bounds=bounds, penalty=penalty, constraints=round, npop=30, gtol=50, disp=True, full_output=True) 
    4854 
    4955    print result[0] 
  • mystic/mystic/constraints.py

    r814 r819  
    243243Input: 
    244244    constraints -- a constraints solver function or a penalty function 
    245     guess -- list of parameter values prposed to solve the constraints 
     245    guess -- list of parameter values proposed to solve the constraints 
    246246    tol -- residual error magnitude for which constraints are considered solved 
     247 
     248    For example: 
     249    >>> @normalized() 
     250    ... def constraint(x): 
     251    ...   return x 
     252    ...  
     253    >>> constraint([.5,.5]) 
     254    [0.5, 0.5] 
     255    >>> issolution(constraint, [.5,.5]) 
     256    True 
     257    >>>  
     258    >>> from mystic.penalty import quadratic_inequality 
     259    >>> @quadratic_inequality(lambda x: x[0] - x[1] + 10) 
     260    ... def penalty(x): 
     261    ...   return 0.0 
     262    ...  
     263    >>> penalty([-10,.5]) 
     264    0.0 
     265    >>> issolution(penalty, [-10,.5]) 
     266    True 
    247267    """ 
    248268    if hasattr(constraints, 'error'): 
     
    555575    range(min(seq),max(seq)). If full is a sequence (list or set), then 
    556576    unique values are selected from the given sequence.  
     577 
     578    For example: 
     579    >>> unique([1,2,3,1,2,4], range(11)) 
     580    [1, 2, 3, 9, 8, 4] 
     581    >>> unique([1,2,3,1,2,9], range(11)) 
     582    [1, 2, 3, 8, 5, 9] 
     583    >>> try: 
     584    ...     unique([1,2,3,1,2,13], range(11)) 
     585    ... except ValueError: 
     586    ...     pass 
     587    ... 
     588    >>> 
     589    >>> unique([1,2,3,1,2,4], {'min':0, 'max':11}) 
     590    [1, 2, 3, 4.175187820357143, 2.5407265707465716, 4] 
     591    >>> mcon.unique([1,2,3,1,2,4], float) 
     592    [1, 2, 3, 1.012375036824941, 3.9821250727509905, 4] 
     593    >>> unique([1,2,3,1,2,10], int) 
     594    [1, 2, 3, 9, 6, 10] 
     595    >>> try: 
     596    ...     unique([1,2,3,1,2,4], int) 
     597    ... except ValueError: 
     598    ...     pass 
     599    ... 
    557600    """ 
    558601    unique = set() 
     
    615658 
    616659def impose_unique(seq=None): 
    617     """ensure all values are unique and found in the given set""" 
     660    """ensure all values are unique and found in the given set 
     661 
     662    For example: 
     663    >>> @impose_unique(range(11)) 
     664    ... def doit(x): 
     665    ...     return x 
     666    ...  
     667    >>> doit([1,2,3,1,2,4]) 
     668    [1, 2, 3, 9, 8, 4] 
     669    >>> doit([1,2,3,1,2,10]) 
     670    [1, 2, 3, 8, 5, 10] 
     671    >>> try: 
     672    ...     doit([1,2,3,1,2,13]) 
     673    ... except ValueError: 
     674    ...     print "Bad Input" 
     675    ... 
     676    Bad Input 
     677""" 
    618678    def dec(f): 
    619679        def func(x,*args,**kwds): 
     
    624684 
    625685#XXX: enable near_integers and has_unique on selected members of x? 
     686#FIXME: the following don't seem to belong in 'mystic.constraints' 
    626687 
    627688from numpy import round, abs 
     
    638699 
    639700# EOF 
    640  
    641 # EOF 
Note: See TracChangeset for help on using the changeset viewer.