API Documentation
- classconstraint.Problem(solver=None)
Class used to define a problem and retrieve solutions.
Initialization method.
- Parameters:
solver (instance of a
Solver) – Problem solver (defaultOptimizedBacktrackingSolver)
- addConstraint(constraint,variables=None)
Add a constraint to the problem.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2,3])>>>problem.addConstraint(lambdaa,b:b==a+1,["a","b"])>>>problem.addConstraint("b == a+1 and a+b >= 2")# experimental string format, automatically parsed, preferable over callables>>>solutions=problem.getSolutions()>>>
- Parameters:
constraint (instance of
Constraint, function to be wrapped byFunctionConstraint, or string expression) – Constraint to be included in the problem. Can be either a Constraint, a callable (function or lambda), or Python-evaluable string expression that will be parsed automatically.variables (set orsequence ofvariables) –
Variablesaffectedby the constraint (default to all variables). Dependingon the constraint type the order may be important.
- addVariable(variable,domain)
Add a variable to the problem.
Example
>>>problem=Problem()>>>problem.addVariable("a",[1,2])>>>problem.getSolution()in({'a':1},{'a':2})True
- Parameters:
variable (hashable object) – Object representing a problemvariable
domain (list, tuple, set, or instance of
Domain) – Set of itemsdefining the possible values that the given variable mayassume
- addVariables(variables,domain)
Add one or more variables to the problem.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2,3])>>>solutions=problem.getSolutions()>>>len(solutions)9>>>{'a':3,'b':1}insolutionsTrue
- Parameters:
variables (sequence ofhashable objects) – Any objectcontaining a sequence of objects represeting problemvariables
domain (list, tuple, or instance of
Domain) – Set of itemsdefining the possible values that the given variablesmay assume
- getSolution()
Find and return a solution to the problem.
Example
>>>problem=Problem()>>>problem.getSolution()isNoneTrue>>>problem.addVariables(["a"],[42])>>>problem.getSolution(){'a': 42}
- Returns:
Solution for the problem
- Return type:
dictionary mapping variables to values
- getSolutionIter()
Return an iterator to the solutions of the problem.
Example
>>>problem=Problem()>>>list(problem.getSolutionIter())==[]True>>>problem.addVariables(["a"],[42])>>>iter=problem.getSolutionIter()>>>next(iter){'a': 42}>>>next(iter)Traceback (most recent call last):...StopIteration
- getSolutions()
Find and return all solutions to the problem.
Example
>>>problem=Problem()>>>problem.getSolutions()==[]True>>>problem.addVariables(["a"],[42])>>>problem.getSolutions()[{'a': 42}]
- Returns:
All solutions for the problem
- Return type:
list of dictionaries mapping variables to values
- getSolutionsAsListDict(order=None,validate=True)
Returns the searchspace as a list of tuples, a dict of the searchspace for fast lookups and the size.
- getSolutionsOrderedList(order=None)
Returns the solutions as a list of tuples, with each solution tuple ordered according toorder.
- getSolver()
Obtain the problem solver currently in use.
Example
>>>solver=OptimizedBacktrackingSolver()>>>problem=Problem(solver)>>>problem.getSolver()issolverTrue
- Returns:
Solver currently in use
- Return type:
instance of a
Solversubclass
- reset()
Reset the current problem definition.
Example
>>>problem=Problem()>>>problem.addVariable("a",[1,2])>>>problem.reset()>>>problem.getSolution()>>>
- classconstraint.Variable(name)
Helper class for variable definition.
Using this class is optional, since any hashable object,including plain strings and integers, may be used as variables.
Initialization method.
- Parameters:
name (string) – Generic variable name for problem-specificpurposes
- classconstraint.Domain(set)
Class used to control possible values for variables.
When list or tuples are used as domains, they are automaticallyconverted to an instance of that class.
Initialization method.
- Parameters:
set – Set of values, comparable by equality, that the given variables may assume.
- hideValue(value)
Hide the given value from the domain.
After that call the given value won’t be seen as a possible value on that domain anymore.The hidden value will be restored when the previous saved state is popped.
- Parameters:
value – Object currently available in the domain
- popState()
Restore domain state from the top of the stack.
Variables hidden since the last popped state are then available again.
- pushState()
Save current domain state.
Variables hidden after that call are restored when that state is popped from the stack.
- resetState()
Reset to the original domain state, including all possible values.
Solvers
- classconstraint.Solver
Abstract base class for solvers.
- getSolution(domains,constraints,vconstraints)
Return one solution for the given problem.
- getSolutionIter(domains,constraints,vconstraints)
Return an iterator for the solutions of the given problem.
- getSolutions(domains,constraints,vconstraints)
Return all solutions for the given problem.
- classconstraint.BacktrackingSolver(forwardcheck=True)
Problem solver with backtracking capabilities.
Examples
>>>result=[[('a',1),('b',2)],...[('a',1),('b',3)],...[('a',2),('b',3)]]
>>>problem=Problem(BacktrackingSolver())>>>problem.addVariables(["a","b"],[1,2,3])>>>problem.addConstraint(lambdaa,b:b>a,["a","b"])
>>>solution=problem.getSolution()>>>sorted(solution.items())inresultTrue
>>>forsolutioninproblem.getSolutionIter():...sorted(solution.items())inresultTrueTrueTrue
>>>forsolutioninproblem.getSolutions():...sorted(solution.items())inresultTrueTrueTrue
Initialization method.
- Parameters:
forwardcheck (bool) – If false forward checking will not berequested to constraints while looking for solutions(default is true)
- getSolution(domains,constraints,vconstraints)
Return one solution for the given problem.
- getSolutionIter(domains,constraints,vconstraints)
Return an iterator for the solutions of the given problem.
- getSolutions(domains,constraints,vconstraints)
Return all solutions for the given problem.
- classconstraint.OptimizedBacktrackingSolver(forwardcheck=True)
Problem solver with backtracking capabilities, implementing several optimizations for increased performance.
Optimizations are especially in obtaining all solutions.Viewhttps://github.com/python-constraint/python-constraint/pull/76 for more details.
Examples
>>>result=[[('a',1),('b',2)],...[('a',1),('b',3)],...[('a',2),('b',3)]]
>>>problem=Problem(OptimizedBacktrackingSolver())>>>problem.addVariables(["a","b"],[1,2,3])>>>problem.addConstraint(lambdaa,b:b>a,["a","b"])
>>>solution=problem.getSolution()>>>sorted(solution.items())inresultTrue
>>>forsolutioninproblem.getSolutionIter():...sorted(solution.items())inresultTrueTrueTrue
>>>forsolutioninproblem.getSolutions():...sorted(solution.items())inresultTrueTrueTrue
Initialization method.
- Parameters:
forwardcheck (bool) – If false forward checking will not berequested to constraints while looking for solutions(default is true)
- getSolution(domains,constraints,vconstraints)
Return one solution for the given problem.
- getSolutionIter(domains,constraints,vconstraints)
Return an iterator for the solutions of the given problem.
- getSolutions(domains,constraints,vconstraints)
Return all solutions for the given problem.
- getSolutionsList(domains,vconstraints)
Optimized all-solutions finder that skips forwardchecking and returns the solutions in a list.
- getSortedVariables(domains,vconstraints)
Sorts the list of variables on number of vconstraints to find unassigned variables quicker.
- Return type:
- Parameters:
domains – Dictionary mapping variables to their domains
vconstraints – Dictionary mapping variables to a listof constraints affecting the given variables.
- Returns:
the list of variables, sorted from highest number of vconstraints to lowest.
- classconstraint.RecursiveBacktrackingSolver(forwardcheck=True)
Recursive problem solver with backtracking capabilities.
Examples
>>>result=[[('a',1),('b',2)],...[('a',1),('b',3)],...[('a',2),('b',3)]]
>>>problem=Problem(RecursiveBacktrackingSolver())>>>problem.addVariables(["a","b"],[1,2,3])>>>problem.addConstraint(lambdaa,b:b>a,["a","b"])
>>>solution=problem.getSolution()>>>sorted(solution.items())inresultTrue
>>>forsolutioninproblem.getSolutions():...sorted(solution.items())inresultTrueTrueTrue
>>>problem.getSolutionIter()Traceback (most recent call last):...NotImplementedError:RecursiveBacktrackingSolver doesn't provide iteration
Initialization method.
- Parameters:
forwardcheck (bool) – If false forward checking will not berequested to constraints while looking for solutions(default is true)
- getSolution(domains,constraints,vconstraints)
Return one solution for the given problem.
- getSolutions(domains,constraints,vconstraints)
Return all solutions for the given problem.
- recursiveBacktracking(solutions,domains,vconstraints,assignments,single)
Mix the Degree and Minimum Remaing Values (MRV) heuristics.
- Parameters:
solutions – _description_
domains – _description_
vconstraints – _description_
assignments – _description_
single – _description_
- Returns:
_description_
- classconstraint.MinConflictsSolver(steps=1000,rand=None)
Problem solver based on the minimum conflicts theory.
Examples
>>>result=[[('a',1),('b',2)],...[('a',1),('b',3)],...[('a',2),('b',3)]]
>>>problem=Problem(MinConflictsSolver())>>>problem.addVariables(["a","b"],[1,2,3])>>>problem.addConstraint(lambdaa,b:b>a,["a","b"])
>>>solution=problem.getSolution()>>>sorted(solution.items())inresultTrue
>>>problem.getSolutions()Traceback (most recent call last):...NotImplementedError:MinConflictsSolver provides only a single solution
>>>problem.getSolutionIter()Traceback (most recent call last):...NotImplementedError:MinConflictsSolver doesn't provide iteration
Initialization method.
- Parameters:
steps (int) – Maximum number of steps to perform beforegiving up when looking for a solution (default is 1000)
rand (Random) – Optional random.Random instance to use forrepeatability.
- getSolution(domains,constraints,vconstraints)
Return one solution for the given problem.
- classconstraint.ParallelSolver(process_mode=False)
Problem solver that executes all-solution solve in parallel (ProcessPool or ThreadPool mode).
Sorts the domains on size, creating jobs for each value in the domain with the most variables.Each leaf job is solved locally with either optimized backtracking or recursion.Whether this is actually faster than non-parallel solving depends on your problem, and hardware and software environment.
Uses ThreadPool by default. Instantiate with process_mode=True to use ProcessPool.In ProcessPool mode, the jobs do not share memory.In ProcessPool mode, precompiled FunctionConstraints are not allowed due to pickling, use string constraints instead.
Examples
>>>result=[[('a',1),('b',2)],...[('a',1),('b',3)],...[('a',2),('b',3)]]
>>>problem=Problem(ParallelSolver())>>>problem.addVariables(["a","b"],[1,2,3])>>>problem.addConstraint("b > a",["a","b"])
>>>forsolutioninproblem.getSolutions():...sorted(solution.items())inresultTrueTrueTrue
>>>problem.getSolution()Traceback (most recent call last):...NotImplementedError:ParallelSolver only provides all solutions
>>>problem.getSolutionIter()Traceback (most recent call last):...NotImplementedError:ParallelSolver doesn't provide iteration
Initialization method. Setprocess_mode to True for using ProcessPool, otherwise uses ThreadPool.
- getSolution(domains,constraints,vconstraints)
Return one solution for the given problem.
- getSolutions(domains,constraints,vconstraints)
Return all solutions for the given problem.
Constraints
- classconstraint.Constraint
Abstract base class for constraints.
- forwardCheck(variables,domains,assignments,_unassigned=Unassigned)
Helper method for generic forward checking.
Currently, this method acts only when there’s a singleunassigned variable.
- Parameters:
- Returns:
Boolean value stating if this constraint is currentlybroken or not
- Return type:
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.FunctionConstraint(func,assigned=True)
Constraint which wraps a function defining the constraint logic.
Examples
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>deffunc(a,b):...returnb>a>>>problem.addConstraint(func,["a","b"])>>>problem.getSolution(){'a': 1, 'b': 2}
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>deffunc(a,b):...returnb>a>>>problem.addConstraint(FunctionConstraint(func),["a","b"])>>>problem.getSolution(){'a': 1, 'b': 2}
Initialization method.
- Parameters:
func (callable object) – Function wrapped and queried forconstraint logic
assigned (bool) – Whether the function may receive unassignedvariables or not
- classconstraint.AllDifferentConstraint
Constraint enforcing that values of all given variables are different.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(AllDifferentConstraint())>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 2)], [('a', 2), ('b', 1)]]
- classconstraint.AllEqualConstraint
Constraint enforcing that values of all given variables are equal.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(AllEqualConstraint())>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1)], [('a', 2), ('b', 2)]]
- classconstraint.ExactSumConstraint(exactsum,multipliers=None)
Constraint enforcing that values of given variables sum exactly to a given amount.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(ExactSumConstraint(3))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 2)], [('a', 2), ('b', 1)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-1,0,1])>>>problem.addConstraint(ExactSumConstraint(0))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -1), ('b', 1)], [('a', 0), ('b', 0)], [('a', 1), ('b', -1)]]
Initialization method.
- Parameters:
exactsum (number) – Value to be considered as the exact sum
multipliers (sequence ofnumbers) – If given, variable valueswill be multiplied by the given factors before beingsummed to be checked
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.MinSumConstraint(minsum,multipliers=None)
Constraint enforcing that values of given variables sum at least to a given amount.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(MinSumConstraint(3))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 2)], [('a', 2), ('b', 1)], [('a', 2), ('b', 2)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-3,1])>>>problem.addConstraint(MinSumConstraint(-2))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -3), ('b', 1)], [('a', 1), ('b', -3)], [('a', 1), ('b', 1)]]
Initialization method.
- Parameters:
minsum (number) – Value to be considered as the minimum sum
multipliers (sequence ofnumbers) – If given, variable valueswill be multiplied by the given factors before beingsummed to be checked
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.MaxSumConstraint(maxsum,multipliers=None)
Constraint enforcing that values of given variables sum up to a given amount.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(MaxSumConstraint(3))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1)], [('a', 1), ('b', 2)], [('a', 2), ('b', 1)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-3,1])>>>problem.addConstraint(MaxSumConstraint(-2))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -3), ('b', -3)], [('a', -3), ('b', 1)], [('a', 1), ('b', -3)]]
Initialization method.
- Parameters:
maxsum (number) – Value to be considered as the maximum sum
multipliers (sequence ofnumbers) – If given, variable valueswill be multiplied by the given factors before beingsummed to be checked
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.ExactProdConstraint(exactprod)
Constraint enforcing that values of given variables create a product of exactly a given amount.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(ExactProdConstraint(2))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 2)], [('a', 2), ('b', 1)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-2,-1,1,2])>>>problem.addConstraint(ExactProdConstraint(-2))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -2), ('b', 1)], [('a', -1), ('b', 2)], [('a', 1), ('b', -2)], [('a', 2), ('b', -1)]]
Instantiate an ExactProdConstraint.
- Parameters:
exactprod – Value to be considered as the product
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.MinProdConstraint(minprod)
Constraint enforcing that values of given variables create a product up to at least a given amount.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(MinProdConstraint(2))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 2)], [('a', 2), ('b', 1)], [('a', 2), ('b', 2)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-2,-1,1])>>>problem.addConstraint(MinProdConstraint(1))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -2), ('b', -2)], [('a', -2), ('b', -1)], [('a', -1), ('b', -2)], [('a', -1), ('b', -1)], [('a', 1), ('b', 1)]]
Instantiate a MinProdConstraint.
- Parameters:
minprod – Value to be considered as the maximum product
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.MaxProdConstraint(maxprod)
Constraint enforcing that values of given variables create a product up to at most a given amount.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(MaxProdConstraint(2))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1)], [('a', 1), ('b', 2)], [('a', 2), ('b', 1)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-2,-1,1])>>>problem.addConstraint(MaxProdConstraint(-1))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -2), ('b', 1)], [('a', -1), ('b', 1)], [('a', 1), ('b', -2)], [('a', 1), ('b', -1)]]
Instantiate a MaxProdConstraint.
- Parameters:
maxprod – Value to be considered as the maximum product
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.VariableExactSumConstraint(target_var,sum_vars,multipliers=None)
Constraint enforcing that the sum of variables equals the value of another variable.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b","c"],[1,2,3])>>>problem.addConstraint(VariableExactSumConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1), ('c', 2)], [('a', 1), ('b', 2), ('c', 3)], [('a', 2), ('b', 1), ('c', 3)]]>>>problem=Problem()>>>problem.addVariable('a',[-1,0,1])>>>problem.addVariable('b',[-1,0,1])>>>problem.addVariable('c',[0,2])>>>problem.addConstraint(VariableExactSumConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -1), ('b', 1), ('c', 0)], [('a', 0), ('b', 0), ('c', 0)], [('a', 1), ('b', -1), ('c', 0)], [('a', 1), ('b', 1), ('c', 2)]]
Initialization method.
- Parameters:
target_var (Variable) – The target variable to sum to.
sum_vars (sequence ofVariables) – The variables to sum up.
multipliers (sequence ofnumbers) – If given, variable values(except the last) will be multiplied by the given factors before beingsummed to match the last variable.
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.VariableMinSumConstraint(target_var,sum_vars,multipliers=None)
Constraint enforcing that the sum of variables sum at least to the value of another variable.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b","c"],[1,4])>>>problem.addConstraint(VariableMinSumConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1), ('c', 1)], [('a', 1), ('b', 4), ('c', 1)], [('a', 1), ('b', 4), ('c', 4)], [('a', 4), ('b', 1), ('c', 1)], [('a', 4), ('b', 1), ('c', 4)], [('a', 4), ('b', 4), ('c', 1)], [('a', 4), ('b', 4), ('c', 4)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-3,1])>>>problem.addVariable('c',[-2,2])>>>problem.addConstraint(VariableMinSumConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -3), ('b', 1), ('c', -2)], [('a', 1), ('b', -3), ('c', -2)], [('a', 1), ('b', 1), ('c', -2)], [('a', 1), ('b', 1), ('c', 2)]]
Initialization method.
- Parameters:
target_var (Variable) – The target variable to sum to.
sum_vars (sequence ofVariables) – The variables to sum up.
multipliers (sequence ofnumbers) – If given, variable values(except the last) will be multiplied by the given factors before beingsummed to match the last variable.
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.VariableMaxSumConstraint(target_var,sum_vars,multipliers=None)
Constraint enforcing that the sum of variables sum at most to the value of another variable.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b","c"],[1,3,4])>>>problem.addConstraint(VariableMaxSumConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1), ('c', 3)], [('a', 1), ('b', 1), ('c', 4)], [('a', 1), ('b', 3), ('c', 4)], [('a', 3), ('b', 1), ('c', 4)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-2,1])>>>problem.addVariable('c',[-3,-1])>>>problem.addConstraint(VariableMaxSumConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -2), ('b', -2), ('c', -3)], [('a', -2), ('b', -2), ('c', -1)], [('a', -2), ('b', 1), ('c', -1)], [('a', 1), ('b', -2), ('c', -1)]]
Initialization method.
- Parameters:
target_var (Variable) – The target variable to sum to.
sum_vars (sequence ofVariables) – The variables to sum up.
multipliers (sequence ofnumbers) – If given, variable values(except the last) will be multiplied by the given factors before beingsummed to match the last variable.
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.VariableExactProdConstraint(target_var,product_vars)
Constraint enforcing that the product of variables equals the value of another variable.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b","c"],[1,2])>>>problem.addConstraint(VariableExactProdConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1), ('c', 1)], [('a', 1), ('b', 2), ('c', 2)], [('a', 2), ('b', 1), ('c', 2)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-2,-1,2])>>>problem.addVariable('c',[-2,1])>>>problem.addConstraint(VariableExactProdConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -1), ('b', -1), ('c', 1)], [('a', -1), ('b', 2), ('c', -2)], [('a', 2), ('b', -1), ('c', -2)]]
Instantiate a VariableExactProdConstraint.
- Parameters:
target_var (Variable) – The target variable to match.
product_vars (sequence ofVariables) – The variables to calculate the product of.
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.VariableMinProdConstraint(target_var,product_vars)
Constraint enforcing that the product of variables is at least the value of another variable.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b","c"],[-1,2])>>>problem.addConstraint(VariableMinProdConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -1), ('b', -1), ('c', -1)], [('a', 2), ('b', 2), ('c', -1)], [('a', 2), ('b', 2), ('c', 2)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-2,-1,1])>>>problem.addVariable('c',[2,5])>>>problem.addConstraint(VariableMinProdConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -2), ('b', -2), ('c', 2)], [('a', -2), ('b', -1), ('c', 2)], [('a', -1), ('b', -2), ('c', 2)]]
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.VariableMaxProdConstraint(target_var,product_vars)
Constraint enforcing that the product of variables is at most the value of another variable.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b","c"],[-1,2])>>>problem.addConstraint(VariableMaxProdConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -1), ('b', -1), ('c', 2)], [('a', -1), ('b', 2), ('c', -1)], [('a', -1), ('b', 2), ('c', 2)], [('a', 2), ('b', -1), ('c', -1)], [('a', 2), ('b', -1), ('c', 2)]]>>>problem=Problem()>>>problem.addVariables(["a","b"],[-2,-1,1])>>>problem.addVariable('c',[-2,-3])>>>problem.addConstraint(VariableMaxProdConstraint('c',['a','b']))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', -2), ('b', 1), ('c', -2)], [('a', 1), ('b', -2), ('c', -2)]]
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.InSetConstraint(set)
Constraint enforcing that values of given variables are present in the given set.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(InSetConstraint([1]))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1)]]
Initialization method.
- Parameters:
set (set) – Set of allowed values
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.NotInSetConstraint(set)
Constraint enforcing that values of given variables are not present in the given set.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(NotInSetConstraint([1]))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 2), ('b', 2)]]
Initialization method.
- Parameters:
set (set) – Set of disallowed values
- preProcess(variables,domains,constraints,vconstraints)
Preprocess variable domains.
This method is called before starting to look for solutions,and is used to prune domains with specific constraint logicwhen possible. For instance, any constraints with a singlevariable may be applied on all possible values and removed,since they may act on individual values even without furtherknowledge about other assignments.
- Parameters:
variables (sequence) – Variables affected by that constraint,in the same order provided by the user
domains (dict) – Dictionary mapping variables to theirdomains
constraints (list) – List of pairs of (constraint, variables)
vconstraints (dict) – Dictionary mapping variables to a listof constraints affecting the given variables.
- classconstraint.SomeInSetConstraint(set,n=1,exact=False)
Constraint enforcing that at least some of the values of given variables must be present in a given set.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(SomeInSetConstraint([1]))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 1)], [('a', 1), ('b', 2)], [('a', 2), ('b', 1)]]
Initialization method.
- classconstraint.SomeNotInSetConstraint(set,n=1,exact=False)
Constraint enforcing that at least some of the values of given variables must not be present in a given set.
Example
>>>problem=Problem()>>>problem.addVariables(["a","b"],[1,2])>>>problem.addConstraint(SomeNotInSetConstraint([1]))>>>sorted(sorted(x.items())forxinproblem.getSolutions())[[('a', 1), ('b', 2)], [('a', 2), ('b', 1)], [('a', 2), ('b', 2)]]
Initialization method.