A step-by-step tutorial demonstrating how to solve constrained optimization problems by reformulating their Karush-Kuhn-Tucker (KKT) conditions as an Initial Value Problem (IVP) and solving it with Physics-Informed Neural Networks (PINNs) using JAX, Flax, and Optax.
Let
where
The Lagrangian associated with this problem is:
where
A point
- Stationarity
- Primal Feasibility
- Dual Feasibility
- Complementary Slackness
Finding
Instead of solving the KKT system as a static root-finding problem, we embed it in a continuous-time dynamical system whose equilibrium coincides with the KKT point. This is achieved via primal-dual gradient flow dynamics:
where
We pair this ODE system with initial conditions
Rather than using a classical numerical integrator (e.g., Runge-Kutta), we parameterize the solution trajectories
The network is trained to minimize a physics-informed loss composed of three terms:
| Term | Expression | Purpose |
|---|---|---|
| ODE residual | Enforce gradient-flow dynamics | |
| Initial condition | Anchor the trajectory at |
|
| Complementarity slackness | Enforce |
The time derivative jax.grad, making the loss fully differentiable and enabling gradient-based optimization with Optax.
The tutorial uses the following small but illustrative problem:
The analytical solution is
.
├── README.md
├── requirements.txt
├── src/
│ ├── toy_problem.py # Objective, constraints, and Lagrangian (JAX)
│ ├── pinn_solver.py # Flax neural network, PINN loss, Optax training
│ └── main.py # Orchestration: train, evaluate, plot
└── notebooks/
└── tutorial.ipynb # Standalone Colab-ready interactive tutorial
This project uses uv for fast, reproducible environment management. You can run the project in two different ways depending on your workflow and preferences. For a standard local execution, you can directly launch the main.py script, which orchestrates the full pipeline including training, evaluation, and visualization. Alternatively, if you prefer an interactive and cloud-based environment, you can use the Jupyter notebook provided in notebooks/tutorial.ipynb. The notebook is fully compatible with Google Colab, allowing you to run the tutorial without installing anything locally while still being able to modify parameters, inspect intermediate results, and experiment interactively.
pip install uv
# or: curl -LsSf https://astral.sh/uv/install.sh | shuv venv
source .venv/bin/activate # Linux / macOS
# .venv\Scripts\activate # Windowsuv pip install -r requirements.txtpython src/main.pyThis trains the PINN, prints the loss at each epoch (every 500 steps), and opens two interactive Plotly charts showing the convergence of
Open notebooks/tutorial.ipynb directly in Google Colab — the first cell installs all required packages automatically via pip (the standard Colab way). For local execution, use uv pip install -r requirements.txt as above.
After training, the PINN trajectories converge to the analytical solution:
| Variable | PINN result | Analytical |
|---|---|---|
| ≈ 0.50 | 0.50 | |
| ≈ 0.50 | 0.50 | |
| ≈ −1.00 | −1.00 |
The interactive Plotly charts produced by src/main.py visualise this convergence over the pseudo-time domain
- Raissi, M., Perdikaris, P., & Karniadakis, G. E. (2019). Physics-informed neural networks: A deep learning framework for solving forward and inverse problems involving nonlinear partial differential equations. Journal of Computational physics, 378, 686-707.
- Arrow, K. J., Hurwicz, L., Uzawa, H., Chenery, H. B., Johnson, S., & Karlin, S. (1958). Studies in linear and non-linear programming (Vol. 2). Stanford: Stanford University Press.
- Karush, W. (1939). Minima of functions of several variables with inequalities as side constraints. M. Sc. Dissertation. Dept. of Mathematics, Univ. of Chicago.
- Kuhn, H. W., & Tucker, A. W. (1951). Proceedings of 2nd berkeley symposium. In Proc. 2nd Berkeley Symp. (pp. 481-492).