Possibility to set constraints of list of dict data? #90
-
|
Hello, I am looking for a way to express that the size of the front_frame must be equal to the back_frame. Would this be possible when using python-constraint? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi Sebastian, that's definitely possible. If possible I would recommend not nesting variables you want to constraint in dictionaries, because it complicates things - I've given an alternative below. However, let's first solve it the way you provided it. Next, we add the constraint and get the solutions: Running this results in the following list, corresponding to the answer you provided: While this solves your issue, it is a bit more cumbersome because you're already providing half of the solution yourself and doesn't get you the full benefit of the solver. The more natural approach is to think of constraint solvers as defining all possible combinations of values (a Cartesian product) and using the constraints to get the combinations that are actually possible. In your case that means a three-dimensional grid of frame, size, and color, something like this: On which you define the following constraints: Of course you can then repackage the solutions into your dictionary format. |
Beta Was this translation helpful? Give feedback.
Hi Sebastian, that's definitely possible. If possible I would recommend not nesting variables you want to constraint in dictionaries, because it complicates things - I've given an alternative below. However, let's first solve it the way you provided it.
We start by defining the two variables, the front and back frames.
Next, we add the constraint and get the solutions: