Given an m * n grid, each edge must be colored. However, there are 2 constraints:
- Entire grid must use only 3 unique colors
- Each square in the grid must be colored using exactly 2 colors (2 edges per color)
I am a full-time consultant and provide services related to the design, implementation and deployment of mathematical programming, optimization and data-science applications. I also teach courses and workshops. Usually I cannot blog about projects I am doing, but there are many technical notes I'd like to share. Not in the least so I have an easy way to search and find them again myself. You can reach me at erwin@amsterdamoptimization.com.
Wednesday, July 12, 2023
Coloring edges of a grid
Thursday, July 6, 2023
A Julia thingy
In Julia, we can write 2x instead of 2*x. Not the most earth-shattering. But a bit special nonetheless.
The expression in the last cell is interpreted as a function call.
Sunday, July 2, 2023
Some confusion here
Sometimes you end up visiting strange sites. This is a question & answer site. The question is:
Obviously, this is not a good question. It is something like "what is the difference between a ham sandwich and butter?".
The answers are not so good either.
Tuesday, June 27, 2023
CVXPY DCP errors
| Objective | CVXPY code | Result | Notes |
|---|---|---|---|
| \[\color{darkred}x^T \color{darkred}x\] | x.T@x | DCP error | print shows minimize x@x, i.e. transpose is dropped |
| \[\color{darkred}x^T \color{darkred}x\] | x@x | DCP error | |
| \[\color{darkred}x^T \color{darkred}x\] | cp.sum_squares(x) | transformed into quad_over_lin(x, 1.0) | |
| \[\color{darkred}x^T \color{darkblue}Q \color{darkred}x\] | x.T@Q@x | transformed into QuadForm(x,Q) | |
| \[\color{darkred}y:=\color{darkred}x-\color{darkblue}p\]\[\color{darkred}x^T \color{darkblue}Q \color{darkred}y\] | y=x-p x.T@Q@y | DCP error | |
| \[\color{darkred}x^T \color{darkblue}Q \color{darkred}x - \color{darkred}x^T \color{darkblue}Q \color{darkblue}p\] | x.T@Q@x - x.T@Q@p | first term transformed into QuadForm(x,Q) |
Wednesday, May 10, 2023
Generate all solutions that sum up to one
In a post, the following question was posed:
We can select unique values \(\displaystyle\frac{1}{i}\) for \(i=1,\dots,n\). Find all combinations that add up to 1.
A complete enumeration scheme was slow even for \(n=10\). Can we use a MIP model for this or something related?
A single solution is easily found using the model:
| Mathematical Model |
|---|
| \[ \begin{align} & \sum_{i=1}^n \frac{1}{i} \cdot \color{darkred}x_i = 1 \\ & \color{darkred}x_i \in \{0,1\} \end{align}\] |
Sunday, May 7, 2023
Finding common patterns
In [1], the following problem is stated:
Given a boolean matrix, with \(m\) rows and \(n\) columns, find the largest pattern of ones that is found in at least \(\color{darkblue}K\) rows. We can ignore cells where the pattern has a zero value: they don't count.
A small example [1] is given:
row 1:[0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1] row 2:[0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1] row 3:[0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1] row 4:[1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1] row 5:[1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1] row 6:[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1]
With \(K=3\), we can form a pattern with 10 nonzero elements:
row 1: [0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1] row 2: [0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1] row 3: [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1] row 4: [1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1] row 5: [1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1] row 6: [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1] pattern:[1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1]
Tuesday, May 2, 2023
Solving as network with lowerbounds
In [1], we looked at the following problem:
| Mathematical Model |
|---|
| \[ \begin{align} \min& \sum_{i,j} \color{darkblue}a_{i,j} \cdot \color{darkred}x_{i,j} \\ & \sum_j \color{darkblue}a_{i,j}\cdot \color{darkred}x_{i,j} \ge \color{darkblue}r_i && \forall i \\ & \sum_i \color{darkblue}a_{i,j}\cdot \color{darkred}x_{i,j} \ge \color{darkblue}c_j && \forall j \\ & \color{darkred}x_{i,j} \in \{0,1\} \end{align}\] |
Wednesday, April 26, 2023
A large MIP model that should be solved as LP: the root node
In this post, I want to discuss an observation about the root node when solving a MIP model.
Problem Description
We have a large matrix \(\color{darkblue}A=\color{darkblue}a_{i,j}\) with values 0 and 1. In addition, we have a minimum on the row and column totals. These are called \(\color{darkblue}r_i\) and \(\color{darkblue}c_j\). The goal is to remove as many 1's in the matrix \(\color{darkblue}A\) subject to these minimum row and column totals.
Tuesday, April 18, 2023
Some GAMS embedded Python notes
Here are two issues you may want to be aware of. The discussion below is relevant for Windows and not so much for Unix variants.
Using Python Raw strings for directories
|
$onEmbeddedCode Python: dir = r"%system.fp%" $offEmbeddedCode |
Wednesday, April 5, 2023
In-process, in-memory databases
There are a few database systems that are a bit different. They are libraries that can be linked directly to your application. Linking can be done statically (during the compilation/linking step) or dynamically (using a shared library or DLL). Here I want to show two cases:
- SQLite [1] used from R on data frames
- DuckDB [2] used from Python, again on data frames