## Wednesday, September 27, 2023

### Math and ChatGPT

Performing symbolic math steps is often related to pattern recognition. In theory, ChatGPT could be doing a good job here. I wanted to find the inverse of $f(x) = {\mathrm{sign}}(x) \log(1+|x|)$ This function is a form of a signed logarithmic scaling. So, let's see what ChatGPT is telling us:

## Wednesday, September 20, 2023

### Julia vs Python

I gave a talk to economists (i.e., not professional programmers) about a Julia project we were working on. Julia is famous for its speed. It uses LLVM  as back-end for its JIT (Just In Time) compilation. As seeing is believing, here is an example algorithm which is used to compare performance between different languages and tools. This example was chosen as it is small, easy to explain, and easy to program while still showing meaningful time differences.

We have a square $$[-1,+1]\times[-1,+1]$$ and an inscribing circle with radius $$1$$. See the dynamic figure below. Their areas are $$4$$ and $$\pi$$ respectively. The idea is to draw $$n$$ points \begin{align}& x_i \sim U(-1,+1) \\ & y_i \sim U(-1,+1)\end{align}Let $$m$$ be the number of points inside the circle, i.e. with $x_i^2+y_i^2\lt 1$ Obviously, from the ratio of the areas, we have $\frac{m}{n} \approx \frac{\pi}{4}$ It follows that an estimate of $$\pi$$ is $\hat{\pi}=4\frac{m}{n}$ Simulation with n=1000

## Monday, September 4, 2023

### Critiquing a GAMS Model

It is always interesting to read GAMS models written by someone else. There are probably three things one can observe:

• A nice formulation or concept that is useful to learn about.
• A bad implementation: something that really should not be done that way.
• A piece of code that is correct and defensible, but I would write it differently. This includes things like style, layout, formatting, etc.
My way of reading GAMS code is often to start editing and make it "my code". It is a bit slower process, but that comes with its advantages: better understanding of what is going on, and often cleaner code.

Here, I am looking at the model sambal.gms in the GAMS model library . It is a very small model, but I have many thoughts about it. The complete model is reproduced in Appendix 1. Let's walk through it.

The matrix balancing problem is to find a nearby matrix such that row- and column sums are obeyed. A relative quadratic objective is used to minimize the sum of the squared deviations between the original data (the priors) and the final matrix. Zeros in the matrix need to be maintained: they can't become nonzero. This is sometimes called sparsity preservation. Often, sign-preservation is another condition. That is not part of this model. Note that, in this model, not only the matrix is updated but also the row and column totals.

## Tuesday, August 29, 2023

### Matrix balancing: introduction

Matrix Balancing Models are often used in economic modeling exercises: they create consistent data sets from data originating from different, conflicting data sources. A standard example is updating a matrix subject to given row and column sums. An example can look like: Update orange cells subject to row/column sums

The empty cells are zero, and they should remain zero. In other words, we need to preserve sparsity. Often, we have non-negativity restrictions on the values. The mathematical model can look like this:

## Friday, August 4, 2023

### Some TSP MTZ experiments

In , a question was posed about a TSP model using the MTZ (Miller-Tucker-Zemlin) subtour elimination constraints. The results with Julia/glpk were disappointing. With $$n=58$$ cities, things were taken so long that the solver seemed to hang. Here I want to see how a precise formulation with a good MIP solver can do better. As seeing is believing, let's do some experiments.

The standard MTZ formulation can be derived easily. We use the binary variables $\color{darkred}x_{i,j}=\begin{cases}1 & \text{if city j is visited directly after going to city i}\\ 0 & \text{otherwise}\end{cases}$

## Wednesday, July 12, 2023

### Coloring edges of a grid

This is a little coloring problem from :

Given an m * n grid, each edge must be colored. However, there are 2 constraints:

1. Entire grid must use only 3 unique colors
2. Each square in the grid must be colored using exactly 2 colors (2 edges per color)

Let's try to build a GAMS model for this. The question itself is not so interesting, but the modeling for setting up these grids is.

## 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

CVXPY is a popular tool to model and solve convex optimization problems. Sometimes, it throws a "DCP" error, even for convex problems. DCP stands for Disciplined Convex Programming, the underlying framework for working with guaranteed convex models. The error says: I can not verify this is convex.

Here are some small examples of convex objectives (under minimization) one would expect to work.

ObjectiveCVXPY codeResultNotes
$\color{darkred}x^T \color{darkred}x$x.T@xDCP errorprint shows minimize x@x, i.e. transpose is dropped
$\color{darkred}x^T \color{darkred}x$x@xDCP 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@xtransformed 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@pfirst term transformed into QuadForm(x,Q)

Not everything makes sense to me. I am not sure why x.T@x is not properly recognized, but x.T@Q@x is.

## 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 , 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  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]


The pattern is shared by rows 4, 5, and 6.

Can we formulate a MIP model for this? My first attempt is as follows.

## Tuesday, May 2, 2023

### Solving as network with lowerbounds

In , 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

The model I want to tackle here is from :

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

Here we use some Python inside an otherwise empty GAMS model:

 $onEmbeddedCode Python: dir = r"%system.fp%"print(dir)$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  used from R on data frames
• DuckDB  used from Python, again on data frames
So these databases don't only run inside R or Python but also can operate directly on data frames.

## Tuesday, April 4, 2023

### Compression and large tables (Excel and CSV)

Here is an interesting little experiment: load a large CSV file into Excel. My original Powerpoint slide from a recent presentation was not as complete as it should be. Here is more size info:

## Saturday, March 25, 2023

### Simultaneous equation models and data errors

My experience is that using (optimization) models is a great way to provide quality assurance on the data. Data collection can be very difficult and expensive. It can also be quite easy to have errors cropping up somewhere along the way. When using an optimization model, we will get hammered by such errors.

## Thursday, March 16, 2023

### Algorithm vs. model

From :

We are given a plane defined by Ax+By+Cz-D = 0 where D is significantly larger than A,B,C and GCD(A,B,C) = 1. How would I find all points (x, y, z), where x,y,z are integers and >= 0, that lie on the plane in an efficient manner?

So the poster asks for an algorithm to find $$\color{darkred}x,\color{darkred}y,\color{darkred}z \in \{0,1,2,\dots\}$$ such that $\color{darkblue}A \cdot \color{darkred}x + \color{darkblue}B \cdot \color{darkred}y + \color{darkblue}C \cdot \color{darkred}z = \color{darkblue}D$ Besides the assumptions stated in the question, I'll further assume $$\color{darkblue}A,\color{darkblue}B,\color{darkblue}C,\color{darkblue}D\gt 0$$.

## Tuesday, March 14, 2023

### Choosing between NLP solvers: interior point or active set.

One way to categorize (local) nonlinear programming (NLP) solvers is active set methods and interior point solvers. Some representative large-scale sparse solvers are:

• Active set: CONOPT, SNOPT. These are using SQP algorithms.
• Interior point: IPOPT, Knitro. Note: Knitro also contains an active set algorithm.
These are two very different types of algorithms. They work very well on different types of problems. If you do a lot of nonlinear optimization, it actually makes sense to have both types of solvers available. With modeling tools like AMPL and GAMS, it is very easy to switch between solvers. Testing performance between two solvers is then also a snap.

Here I want to discuss some factors that can help in deciding whether to use an active set solver or an interior point solver.

## Monday, March 6, 2023

### Some approaches for moving data between MS Access and GAMS

Moving data between different environments is always more difficult than we hope. Here I list some approaches and actually try them out on a small dataset. We hit some bugs along the way and also a few conceptual stumbling blocks (even for this stylized example). We had some issues with Access as well as GAMS Connect.

This question came up in an actual project. My idea was: "Let me show you how this can be done". I am afraid, I got carried away a bit. But it demonstrates that we should not underestimate these, at first sight, menial tasks. When the data set becomes larger, the problems compound. We can't eyeball the data, and statistically, it is more likely we encounter some problems.

## Friday, February 24, 2023

### Another fast MIP model: covering

In , the following problem is stated:

• There is a collection of $$n=1,000$$ test questions.
• Each question covers a number of skills.
• Given is a requirement for a number of questions for each required skill (e.g., 4 questions about skill 1, 3 questions about skill 2, etc.).
• Create a test with the minimum number of questions that fulfills the requirements.
Of course, we see some remarks about NP-Hardness. Complexity theory is about bounds, about worst-case and asymptotic behavior. Even if we can't prove that a particular problem with a particular dataset given to a particular solver must be fast, it does not mean the opposite: that it must be slow. It is not at all unusual that we can solve things very quickly, even though the complexity bounds are terrible. Of course, this depends on the model, on the data set, and on the solver. Here, we see an open-source MIP solver can solve this particular problem in less than 0.01 seconds. Many well-educated people misunderstand complexity theory – to their detriment. They miss out on great ways to solve actual problems! Obviously, this is a problem with how complexity theory is taught.

## Thursday, February 16, 2023

### Assigning jobs to machines without overlap

Here we consider the following problem from :

• We have jobs with a given start time and completion time
• Jobs can be repeated on given days (e.g. job 1 needs to run on Monday, Wednesday, and Friday)
• We want to assign jobs to machines in such a way that there is no overlap
• The objective is to minimize the number of machines needed to execute all jobs
The planning horizon is a week, and the problems has 50 jobs.

## Wednesday, February 15, 2023

### Supplier selection: an easy MIP

This is a fairly standard supplier selection model. It was posted in . It is interesting as it is a good fit for a MIP model: it is simple to model, it solves fast, and it is not obvious how to solve without an optimization model.

The problem is:
• We want to order items in different quantities from suppliers.
• Suppliers have an available inventory for these items. This can be zero.
• We can split the ordering over different suppliers.
• The cost structure is as follows:
• Shipping cost is a fixed cost per supplier.
• Item cost is a variable per-unit cost.

## Monday, February 13, 2023

### Populating SQLite databases

GAMS has three easy ways to populate a SQLite database:

1. Using the tool gdx2sqlite. This tool populates a SQLite database with data from a GDX file. This means we first have to export GAMS data to a GDX file. As there is quite some file I/O going on here (writing GDX file, reading GDX file, writing database), I would expect this to be slower than the next method.
2. The new GAMS-connect facility. This does not use intermediate files, and directly copies records from in-memory data. This should be the fastest.
3. Old fashioned CSV files. We first export data as a GDX file, and then use gdxdump to convert the data to a CSV file. Then sqlite can import the CSV file, and populate the database. There is much file I/O here, so this should be slow.

## Saturday, January 28, 2023

### Tiny non-convex quadratic model brings solvers to their knees

Here is a very small geometric problem:

Given $$n$$ points in 2d space, find the smallest triangle that contains all these points. Find the smallest triangle containing all points.

This looks like a simple problem. Somewhat to my surprise, my attempt here does not bear that out.

## Tuesday, January 24, 2023

### Export GAMS GDX file to different Python formats (CSV,Feather,Pickle)

A GAMS GDX file is a binary file with a number of GAMS symbols with their data. When collaborating with colleagues that work with Python, it may be useful to convert a GAMS GDX with relevant data to data files that Python can consume.

The idea is here that a GAMS user can run the GAMS script, without knowing Python or having Python installed. (GAMS comes with its own somewhat barebones Python, which is used inside the GAMS script). On the other hand, the Python user will not need to have GAMS installed to read the generated data files. This is opposed to Python packages and tools that can interact with GDX files directly. You can see a few in the PyPI directory . Those will require both a GAMS system and a Python environment.

## Wednesday, January 11, 2023

### MIP Bounds

MIP (and related) solvers have a unique property: they provide a bound on the best possible integer solution at time $$t$$. Together with the best-found integer solution so far, we have two bounds. Somewhere in between is the final optimal solution. The typical picture for these two bounds is as follows:

## Monday, January 2, 2023

### High Level MIP Modeling

Most LP/MIP modeling tools stay close to what is the underpinning of a Mixed-Integer Programming problem: a system of linear equations (equalities and inequalities) plus a linear objective. Examples are Pulp and GAMS. In Constraint Programming (CP), modeling tools need to provide access to higher-level global constraints. Without these global constraints (such as the famous all-different constraint), CP solvers would not perform very well.