Python Forum

Full Version: Python-Constraint
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Let's say I have:

problem = Problem()
problem.addVariables(['a', 'b'], [1, 2, 3])
problem.addConstraint(AllDifferentConstraint())
problem.addConstraint(***Ignore Order***)

I want the solutions to be:
[{'a': 3, 'b': 2}, {'a': 3, 'b': 1}, {'a': 2, 'b': 1}]
Instead of:
[{'a': 3, 'b': 2}, {'a': 3, 'b': 1}, {'a': 2, 'b': 3}, {'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3}]

So that, for example {'a': 3, 'b': 2} and {'a': 2, 'b': 3} would be considered the same solution and only one of those will appear in the solution set. How can I create a constraint like this?
You could use a FunctionConstraint as described in the documentation of the python-constraint module.
(Nov-08-2020, 08:29 PM)Gribouillis Wrote: [ -> ]You could use a FunctionConstraint as described in the documentation of the python-constraint module.

I'm familiar with the function constraint and I think that's probably what I need to use, but I haven't been able to figure out what the function should look like. It seems like the constraints can only constrain the current solution that it's working on. If it gets to {'a': 3, 'b': 2}, how would it know if {'a': 2, 'b': 3} has already been added to the solution list? I've thought about doing a hash sum to detect duplicate solutions, but that runs into the same problem.

Can you explain what the function might look like to achieve this?
In the problem that you described above, there is a symetry so that if {'a': x, 'b': y} is a solution, so is {'a': y, 'b': x}. In this case, you can use the exact function from the documentation by imposing the additional constraint that a < b.