Title: Possibility to set constraints of list of dict data? · python-constraint/python-constraint · Discussion #90 · GitHub
Open Graph Title: Possibility to set constraints of list of dict data? · python-constraint/python-constraint · Discussion #90
X Title: Possibility to set constraints of list of dict data? · python-constraint/python-constraint · Discussion #90
Description: Possibility to set constraints of list of dict data?
Open Graph Description: Hello, I am trying to solve a product csp-problem with python constraint. For the sake of it. Let's say that my data looks as following. front_frame = [ {"size": [40, 50], "color": ["green"]}, {"si...
X Description: Hello, I am trying to solve a product csp-problem with python constraint. For the sake of it. Let's say that my data looks as following. front_frame = [ {"size": [40, 50], "color...
Opengraph URL: https://github.com/python-constraint/python-constraint/discussions/90
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"Possibility to set constraints of list of dict data?","text":"Hello,
\nI am trying to solve a product csp-problem with python constraint.
\nFor the sake of it. Let's say that my data looks as following.
\nfront_frame = [\n{\"size\": [40, 50], \"color\": [\"green\"]},\n{\"size\": [60], \"color\": [\"blue\", \"red\"]]\n]\n\nback_frame = [\n{\"size\": [50, 60], \"color\": [\"green\"]},\n{\"size\": [30, 40], \"color\": [\"blue\", \"red\"]]\n\nproblem = Problem()\nproblem.addVariable(\"frame\", frame)\nproblem.addVariable(\"wheel\", wheel)\n
\nI am looking for a way to express that the size of the front_frame must be equal to the back_frame.
\nAs in:
\nif size = 30, no solution
\nif size = 40, then front_frame = {\"size\": [40, 50], \"color\": [\"green\"]}, back_frame = {\"size\": [30, 40], \"color\": [\"blue\", \"red\"]]
\nif size = 50, then front_frame = {\"size\": [40, 50], \"color\": [\"green\"]}, back_frame = {\"size\": [50, 60], \"color\": [\"green\"]}
\nif size = 60, then front_frame = {\"size\": [60], \"color\": [\"blue\", \"red\"]], back_frame = {\"size\": [50, 60], \"color\": [\"green\"]}
\nWould this be possible when using python-constraint?
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"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.
\nWe start by defining the two variables, the front and back frames.
\nfrom constraint import Problem\nproblem = Problem()\nproblem.addVariable(\"front\", [\n {\"size\": [40, 50], \"color\": [\"green\"]},\n {\"size\": [60], \"color\": [\"blue\", \"red\"]}\n])\nproblem.addVariable(\"back\", [\n {\"size\": [50, 60], \"color\": [\"green\"]},\n {\"size\": [30, 40], \"color\": [\"blue\", \"red\"]}\n])\n
\nNext, we add the constraint and get the solutions:
\ndef equal_size(front, back):\n return any(size in front[\"size\"] for size in back[\"size\"])\n\nproblem.addConstraint(equal_size, (\"front\", \"back\"))\nprint(problem.getSolutions())\n
\nRunning this results in the following list, corresponding to the answer you provided:
\n[{'back': {'size': [30, 40], 'color': ['blue', 'red']}, 'front': {'size': [40, 50], 'color': ['green']}}, \n{'back': {'size': [50, 60], 'color': ['green']}, 'front': {'size': [60], 'color': ['blue', 'red']}}, \n{'back': {'size': [50, 60], 'color': ['green']}, 'front': {'size': [40, 50], 'color': ['green']}}]\n
\nWhile 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:
\nproblem = Problem()\nproblem.addVariable(\"frame\", [\"front\", \"back\"])\nproblem.addVariable(\"size\", [30, 40, 50, 60])\nproblem.addVariable(\"color\", [\"green\", \"blue\", \"red\"])\n
\nOn which you define the following constraints:
\ndef actual_values(frame, size, color):\n if frame == \"front\":\n if color == \"green\":\n return size == 40 or size == 50\n else:\n return size == 60\n else:\n if color == \"green\":\n return size == 50 or size == 60\n else:\n return size == 30 or size == 40\n\nproblem.addConstraint(actual_values, (\"frame\", \"size\", \"color\"))\n
\nOf course you can then repackage the solutions into your dictionary format.
\nI hope this provides an answer to your question, feel free to ask further questions or reopen if necessary.
","upvoteCount":1,"url":"https://github.com/python-constraint/python-constraint/discussions/90#discussioncomment-12338386"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:9c293b02-1290-e185-78f5-72378ce3f84e |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | D28A:31BFB5:41AE47:5C6D77:696E543F |
| html-safe-nonce | 86b58c0085945a6c4f7cc7ed4b8ce4a7773256ca3b60b25ce2799e974de1079c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjhBOjMxQkZCNTo0MUFFNDc6NUM2RDc3OjY5NkU1NDNGIiwidmlzaXRvcl9pZCI6IjYyMTg0ODA5NTU0NzE4NDQ0MTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d17eb688e6887b64c8b974dddb96ed2df4f16744d4370b98f5162b0ecc0bbcdc |
| hovercard-subject-tag | discussion:7927855 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/python-constraint/python-constraint/90/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/a5bf847c3b3776e7dee64ea32fe4370790d5f91f7a17d7142c4b2ca1b1279274/python-constraint/python-constraint/discussions/90 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a5bf847c3b3776e7dee64ea32fe4370790d5f91f7a17d7142c4b2ca1b1279274/python-constraint/python-constraint/discussions/90 |
| og:image:alt | Hello, I am trying to solve a product csp-problem with python constraint. For the sake of it. Let's say that my data looks as following. front_frame = [ {"size": [40, 50], "color": ["green"]}, {"si... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | f68b42d371252b0f236260d6234f4304a806fe5ac43d59faa21fb59d80df103b |
| turbo-cache-control | no-preview |
| go-import | github.com/python-constraint/python-constraint git https://github.com/python-constraint/python-constraint.git |
| octolytics-dimension-user_id | 14961635 |
| octolytics-dimension-user_login | python-constraint |
| octolytics-dimension-repository_id | 43628779 |
| octolytics-dimension-repository_nwo | python-constraint/python-constraint |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 43628779 |
| octolytics-dimension-repository_network_root_nwo | python-constraint/python-constraint |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 6b74bc8dbcd10b5d69fd9ee9d2cfdc8b35e18a4c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width