This repository implements neural network-based solvers for Hamilton-Jacobi-Bellman (HJB) equations in optimal control problems. We demonstrate the approach on two canonical problems:
- Motion Control: Multi-dimensional optimal control for driving systems to the origin
- Resource Allocation: Optimal production and consumption scheduling
- Motion Control: Achieved MSE < 0.001 for 2D systems, scaling to 8D with maintained accuracy
- Resource Allocation: Neural network captures bang-bang control structure with MSE = 0.00487
The HJB equation for optimal control problems takes the form:
where:
-
$J(t,x)$ is the cost-to-go function -
$f(x,u)$ represents system dynamics -
$g(x,u)$ is the running cost -
$u$ is the control input
Our neural network approach approximates both
-
UNet: Approximates optimal control
$u^*(t,x)$ with constraints (e.g.,$||u|| \leq 1$ ) -
JNet: Approximates cost-to-go function
$J(t,x)$ - Both networks use 2-layer MLPs with 64 hidden units and tanh activation
- Staged Training: Problems solved backward in time using 5 stages
- Loss Function: Combines HJB residuals, boundary conditions, and trajectory matching
- Automatic Differentiation: PyTorch autograd computes all required derivatives
git clone https://github.com/rfarell/neural_hjb.git
cd neural_hjb
# Option 1: Quick setup with conda (recommended)
./setup.sh # Creates conda environment and installs dependencies
# Option 2: Manual installation
pip install -e .Requirements: Python 3.8+, PyTorch, torchdiffeq, NumPy, Pandas, Matplotlib
./run.sh # Runs all experiments automaticallycd motion_control/scripts
python train.py --dimensions 2 4 6 8 --num_epochs 1000
python evaluate.pycd resource_allocation/scripts
python train.py --num_epochs 1000
python evaluate.pyThe motion control problem seeks to drive a system to the origin while minimizing the quadratic cost:
subject to
| Dimension | MSE (Control) | MSE (Cost-to-go) |
|---|---|---|
| 2D | 0.00126 | 0.00182 |
| 4D | 0.00002 | 0.01214 |
| 6D | 0.00003 | 0.02531 |
| 8D | 0.00049 | 4.23102 |
Figure 1: Mean squared error scaling with problem dimension. The method maintains accuracy even in higher dimensions.
Figure 2: Training loss convergence for 2D motion control across 5 time stages.
Figure 3: Learned control policy and cost-to-go function for 2D motion control at t=0.5.
The resource allocation problem optimizes production vs. consumption over time:
subject to
The analytical solution exhibits bang-bang control: $u^(t) = 1$ for $t < T - 1/\gamma$, then switches to $u^(t) = 0$.
Figure 4: Comparison of neural network predictions (solid lines) vs analytical solution (dashed) for different initial conditions. The network accurately captures the switching behavior at t=2.
Key Metrics:
- MSE for control: 0.00487
- Switching time accuracy: ±0.05 time units
- Captures sharp transitions in bang-bang control
Figure 5: Distribution of control prediction errors showing concentration near zero.
-
Dynamics:
$\dot{x} = u$ -
Cost:
$\int_0^T ||x||^2 dt + ||x(T)||^2$ -
Constraint:
$||u|| \leq 1$ -
Analytical Solution:
$u^* = -x/||x||$ when$||x|| > 0$
-
Dynamics:
$\dot{x} = \gamma u x$ -
Objective:
$\max \int_0^T (1-u)x dt$ -
Constraint:
$u \in [0,1]$ -
Analytical Solution: Bang-bang control with switching at
$t = T - 1/\gamma$
If you use this code in your research, please cite:
@software{neural_hjb,
author = {Farell, Ryan},
title = {Neural Network Solutions for Hamilton-Jacobi-Bellman Equations},
year = {2024},
url = {https://github.com/rfarell/neural_hjb}
}MIT License - see LICENSE file for details.