Skip to content

Instantly share code, notes, and snippets.

@mgronhol
Created October 17, 2012 09:46
Show Gist options
  • Select an option

  • Save mgronhol/3904720 to your computer and use it in GitHub Desktop.

Select an option

Save mgronhol/3904720 to your computer and use it in GitHub Desktop.
Constraint solving using backtracking
#!/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