Created
October 17, 2012 09:46
-
-
Save mgronhol/3904720 to your computer and use it in GitHub Desktop.
Constraint solving using backtracking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import libConstraint as LC | |
| solution = LC.Solution() | |
| @solution.must_satisfy | |
| def sum_constraint( params, candidate ): | |
| S = sum( candidate ) | |
| if S > 10: | |
| return False | |
| return True | |
| @solution.must_satisfy | |
| def begin_constraint( params, candidate ): | |
| if len( candidate ) > 2: | |
| if candidate[:2] != [1,2]: | |
| return False | |
| return True | |
| @solution.is_valid | |
| def valid_solution( params, candidate ): | |
| S = sum( candidate ) | |
| return S == 10 | |
| import pprint | |
| pprint.pprint( solution.get( domain = [1,2,3,4,5,10,11] ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment