- Timestamp:
- 08/12/15 08:26:54 (9 months ago)
- Location:
- mystic
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples2/integer_programming.py
r818 r819 39 39 pf = generate_penalty(generate_conditions(equations)) 40 40 41 from numpy import round as npround 41 from mystic.constraints import integers 42 43 @integers() 44 def round(x): 45 return x 46 42 47 43 48 if __name__ == '__main__': … … 46 51 from mystic.math import almostEqual 47 52 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) 49 54 50 55 print result[0] -
mystic/examples2/integer_programming_alt.py
r818 r819 38 38 return round(solver(x)) 39 39 40 from numpy import round as npround 40 # better is to constrain to integers, penalize otherwise 41 from mystic.constraints import integers 42 43 @integers() 44 def round(x): 45 return x 46 41 47 42 48 if __name__ == '__main__': … … 45 51 from mystic.math import almostEqual 46 52 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) 48 54 49 55 print result[0] -
mystic/mystic/constraints.py
r814 r819 243 243 Input: 244 244 constraints -- a constraints solver function or a penalty function 245 guess -- list of parameter values pr posed to solve the constraints245 guess -- list of parameter values proposed to solve the constraints 246 246 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 247 267 """ 248 268 if hasattr(constraints, 'error'): … … 555 575 range(min(seq),max(seq)). If full is a sequence (list or set), then 556 576 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 ... 557 600 """ 558 601 unique = set() … … 615 658 616 659 def 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 """ 618 678 def dec(f): 619 679 def func(x,*args,**kwds): … … 624 684 625 685 #XXX: enable near_integers and has_unique on selected members of x? 686 #FIXME: the following don't seem to belong in 'mystic.constraints' 626 687 627 688 from numpy import round, abs … … 638 699 639 700 # EOF 640 641 # EOF
Note: See TracChangeset
for help on using the changeset viewer.