[Python] python-constraint packages
python-constraint 패키지에서 제공하는 모듈을 소개
이 모듈 여러개를 적절하게 결합하면 제한 조건을 설정할 수 있을 것 같다.
Type AllDifferentConstraint
Constraint enforcing that values of all given variables are different
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.getSolutions()
# [{'a': 2, 'b': 2}, {'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 1}]
problem.addConstraint(AllDifferentConstraint())
problem.getSolutions()
# [{'a': 2, 'b': 1}, {'a': 1, 'b': 2}]
Type AllEqualConstraint
Constraint enforcing that values of all given variables are equal
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.getSolutions()
# [{'a': 2, 'b': 2}, {'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 1}]
problem.addConstraint(AllEqualConstraint())
problem.getSolutions()
# [{'a': 2, 'b': 2}, {'a': 1, 'b': 1}]
Type BacktrackingSolver
Problem solver with backtracking capabilities
problem = Problem(BacktrackingSolver())
problem.addVariables(["a", "b"], [1, 2, 3])
problem.addConstraint(lambda a, b: b > a, ["a", "b"])
solution = problem.getSolution()
solution
# {'a': 2, 'b': 3}
solutions = problem.getSolutions()
solutions
# [{'a': 2, 'b': 3}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3}]
Type ExactSumConstraint
Constraint enforcing that values of given variables sum exactly to a given amount
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.addConstraint(ExactSumConstraint(3))
problem.getSolutions()
# [{'a': 2, 'b': 1}, {'a': 1, 'b': 2}]
Type FunctionConstraint
Constraint which wraps a function defining the constraint logic
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
def func(a, b):
return b > a
problem.addConstraint(func, ["a", "b"])
problem.getSolution()
# {'a': 1, 'b': 2}
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
def func(a, b):
return b > a
problem.addConstraint(FunctionConstraint(func), ["a", "b"])
problem.getSolution()
# {'a': 1, 'b': 2}
Type InSetConstraint
Constraint enforcing that values of given variables are present in the given set
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.addConstraint(InSetConstraint([1]))
problem.getSolutions()
# [{'a': 1, 'b': 1}]
Type MaxSumConstraint
Constraint enforcing that values of given variables sum up to a given amount
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.addConstraint(MaxSumConstraint(3))
problem.getSolutions()
# [{'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 1}]
Type MinConflictsSolver
Problem solver based on the minimum conflicts theory
problem = Problem(MinConflictsSolver())
problem.addVariables(["a", "b"], [1, 2, 3])
problem.addConstraint(lambda a, b: b > a, ["a", "b"])
solution = problem.getSolution()
solution
# {'a': 1, 'b': 2}
Type MinSumConstraint
Constraint enforcing that values of given variables sum at least to a given amount
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.addConstraint(MinSumConstraint(3))
solutionS = problem.getSolutions()
solutionS
# [{'a': 2, 'b': 2}, {'a': 2, 'b': 1}, {'a': 1, 'b': 2}]
Type NotInSetConstraint
Constraint enforcing that values of given variables are not present in the given set
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.addConstraint(NotInSetConstraint([1]))
problem.getSolutions()
# [{'a': 2, 'b': 2}]
Type RecursiveBacktrackingSolver
Recursive problem solver with backtracking capabilities
problem = Problem(RecursiveBacktrackingSolver())
problem.addVariables(["a", "b"], [1, 2, 3])
problem.addConstraint(lambda a, b: b > a, ["a", "b"])
problem.getSolutions()
# [{'a': 1, 'b': 2}, {'a': 1, 'b': 3}, {'a': 2, 'b': 3}]
Type SomeInSetConstraint
Constraint enforcing that at least some of the values of given variables must be present in a given set
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.addConstraint(SomeInSetConstraint([1]))
problem.getSolutions()
Type SomeNotInSetConstraint
Constraint enforcing that at least some of the values of given variables must not be present in a given set
problem = Problem()
problem.addVariables(["a", "b"], [1, 2])
problem.addConstraint(SomeNotInSetConstraint([1]))
problem.getSolutions()
# [{'a': 2, 'b': 2}, {'a': 2, 'b': 1}, {'a': 1, 'b': 2}]
API Documentation
labix.org