Using a penalty function technique, I used TensorFlow to solve constrained quadratic programming problems.

First, I show a solution to an unconstrained quadratic programming problem available here or viewed online at GitHub.

For constrained problems a penalty term is introduction to the Lagrangian function. The penalty term is proportional to the sum of the constraint errors squared. Initially the penalty is relaxed; as the solution is approached the penalty term is tighted. The key elements of the solution is a technique for estimating Lagranian \(\lambda\) values.

I created a Jupyter notebook for a sample problem with one equality constraint, available here or viewed online at GitHub.

For an inequality constraint, I used a similar technique but the Lagrangian \(\lambda\) values are non-negative. The inequalities are represented as equalities along with an associated slack value. An additional optimization technique is used to determine the slack values.

I created a Jupyter notebook for a sample problem with one inequality constraint, available here or viewed online at GitHub.

This notebook includes a nice contour plot overlayed with the optimizer’s path to the solution.

Although these notebooks demostrate a single constraint, the solutions generalize to as many constraints as needed. Combining the techniques in the notebooks would produce an optimizer that handles equalities and inequalities.

Additionally, there are no particular restrictions on the quadratic term. In finance this represents the risk term. Handling a more structured risk model is not a problem, such as a model with factor risks and specific risks.

The mathematics supporting these techniques is well described in Numerical Optimization, Nocedal & Wright, 2006