How to Cut your Board using CP?
Alireza Soroudi, PhD
Lead Data Scientist @ bluecrux || SMIEEE || Optimization expert in Supply chain management|| Healthcare management || Lab Digitalization || Power and Energy systems || Developer || Author / Speaker || (views are mine)
Cutting the shapes has always fascinated mathematicians. In this episode of Optimization in Open Source , we focus on cutting a chessboard.
Introduction
Honestly, finding a good math problem is always a challenge for me, so I spend some of my spare time reading math books (such a NERD!). In one of these books, I stumbled upon this fascinating problem:
In simple words "How can we split the chess board into two equal surface parts? "
The point is that each part should be connected (horizontally or vertically).
Let me clarify the concept.
Two trivial ways of spliting is as follows:
but is there any other way to do it ? yes there is
How can we find all possible different ways of this cutting?
Alright, enough with the fluff—let's dive into the fun part: the math modeling!
Math model
The first essential constraint is to have enough numbe of cells in each slice (two slices in total). where U is a boolean variable indicating if the cell i should be black or not.
Then a more challenging constraint is to ensure the connectivity of black cells (slice 1). For this purpose I assume there is a single source in slice one (found by boolean variable B_i) and all other cells in slice 1 have a sink demand which should be supplied. Keeping in mind that the connectivity of two cells is checked with Z_ij (boolean) and the flow between them is indicated with flow?b_ij (integer).
The same concept is applied to the white cells (slice 2).
if we miss constraints 6-10 in the model then the following solution might be obtained: You can see that the black cells are connected but the white cells have islands in them.
领英推荐
Finally, a cell can be a source for either black slice or white slice:
Ok, now let's look at the overall model:
As it can be observed, the model has no objective function and it's a pure constraint satisfaction. you can also see the obtaned solution. But what is the underneath of this visualization ? what is the rule of Z, flow , B, W ?
Let's have a deep dive into it. The red lines show the Zb and the blue ones are Zw. When creating the Zb,Zw elements, it should be noted that these variables should be only defined over the adjacent cells (up/dn/right/left cells).
Finished ? No the journey has just begun!
Is the obtained solution unique ?
The answer is no, but how to obtain them all ? It's simple, after obtaining the first answer th
following constraint is added to the model and it is resolved untiltehre is no solution found.
This guarantees that at least one cell will be filled differently in the next solution compared to the current one.
Here is the python code (also available in github)
Symmetry breaking
The introduced problem has some kind of symmetry which can be improved by adding extra constraints.
Conculsion
You might be wondering (or even asking me), "Why on earth would a wise person want to cut up a chessboard?" Well, get ready, because the answer is more fun than you might think!
Git repo : https://github.com/OptimizationExpert/Pyomo
Strategy and Risk Analyst | JPMorgan Chase & Co.
2 个月Great post! Super informative in understanding flow balance constraints.
Electric Vehicle Propulsion and Control (E-PiCo) Erasmus Mondus Scholarship Holder
2 个月Interesting. From a distribution electric grid perspective, we can explore all the possibilities of energizing 64 nodes using two substations. To guarantee radiality, you can add the equality constraint: (1/2)*sum(Zij_b, for i,j) + 1 = sum(Ui, for i) (i.e., the number of branches plus 1 equals the number of nodes).
Optimization Data Scientist @ MaxAB | Master of Science in Optimization
2 个月As usual, another interesting article about CP! I can think of many applications in urban planning. Do you think this type of model could scale easily to more irregular shapes?
THIS IS A PERSONAL ACCOUNT
2 个月SQL has a natural fit with Constraint Programming.
Lead Data Scientist @ bluecrux || SMIEEE || Optimization expert in Supply chain management|| Healthcare management || Lab Digitalization || Power and Energy systems || Developer || Author / Speaker || (views are mine)
2 个月?? ????? ??? ???? ????? ?? ??? ????? ?????? ????? ???? ?????? ??????? ???? PyomoChannel