Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python-Constraint
#1
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?
Reply
#2
You could use a FunctionConstraint as described in the documentation of the python-constraint module.
Reply
#3
(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?
Reply
#4
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020