This is the implementation of fully first-order bilevel gradient to replace the non-fully first-order methods in differentiable optimization.
The main comparison is qpth and cvxpylayer. Our method provides a fully first-order method to differentiate through optimization layers approximately.
It has the advantage in computation and memory efficiency, but in the tradeoff of the accuracy of the gradient.
Please install the required packages:
- torch
- cvxpy
- qpth
- cvxpylayer
- gurobi (python -m pip install gurobipy)
- cvxtorch (git clone https://github.com/cvxpy/cvxtorch.git, cd cvxtorch, pip install -e .)
1) Decision-focused learning (QP case)
Key files
main.py— entrypoint for all methodsffoqp.py- previous ffo version with l2 penaltyffoqp_eq_cst.py- new ffo version without l2 penalty (faster convergence in theory)ffoqp_eq_cst_schur/pdipm/parallelize.py- try to accelerate ffoqp_eq_cst
To run the code, please use:
python main.py --method=ffoqp_eq_cst --eps=0.1 --lr=0.00001 --ydim=200
python main.py --method=ffoqp --eps=0.1 --lr=0.00001 --ydim=200
python main.py --method=cvxpylayer --eps=0.1 --lr=0.00001 --ydim=200
python main.py --method=qpth --eps=0.1 --lr=0.00001 --ydim=200
(optional)
python main.py --method=ffoqp_eq_cst_pdipm --eps=0.1 --lr=0.00001 --ydim=200
python main.py --method=ffoqp_eq_cst_parallelize --eps=0.1 --lr=0.00001 --ydim=200
python main.py --method=ffoqp_eq_cst_schur --eps=0.1 --lr=0.00001 --ydim=200
I ran a few simple experiments to compare the performance of the three methods. Each epoch includes 2048 samples (split into training and testing) to run decision-focused leanring (end-to-end learning).
For optimization problems of size 200, the computation results are as follows (please run plot_toy_dfl.ipynb to reproduce it)
The computation improvement can only be seen in larger instances of the optimization problem. This is likely due to the dominance of the overhead in the forward process to solve the optimization problem. Only when the size goes larger, the backward process of the optimization problem becomes more expensive than the forward process. In smaller instances, the overhead in the forward process dominates the computation time, and thus the improvement in the backward process is not significant enough to be observed. qpth also uses a faster algorithm to parallelize and implement the forward pass, which is why qpth is faster than cvxpylayer (in theory they are the same).
2) Second-order Cone program
To run the code, please run:
ffoqsocp_test.ipynb
We found that ffoqp's gradient approximation is more accurate than LPGD in this case.
3) Sudoku (differentiable optimization as a layer)
Key files
sudoku/main_sudoku.py— entrypoint for all methodsffocp_eq.py- new ffo version for general convex problem without l2 penaltycvxpylayers_local/cvxpylayer.py- incorporate the latest diffcp into cvxpylayer to support LPGD
To run the code, please use:
python sudoku/main_sudoku.py --method ffocp --n 3 --epoch 1 --batch_size 8
python sudoku/main_sudoku.py --method lpgd --n 3 --epoch 1 --batch_size 8
python sudoku/main_sudoku.py --method ffoqp --n 3 --epoch 1 --batch_size 8
python sudoku/main_sudoku.py --method cvxpylayer --n 3 --epoch 1 --batch_size 8
python sudoku/main_sudoku.py --method qpth --n 3 --epoch 1 --batch_size 8
To plot the results, please use
python sudoku/plot_results.py
Now we observed that LPGD has a very high test error (~0.9), while ffocp and other methods can achieve low test errors (~0.01-0.1).
Metadata
Release files for ffolayer 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ffolayer-0.1.2.tar.gz | 33.4 kB | Without provenance | Details for ffolayer-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ffolayer-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for ffolayer-0.1.2-py3-none-any.whl |
Total release size: 60.1 kB
Release files / ffolayer-0.1.2.tar.gz
| Download URL | ffolayer-0.1.2.tar.gz |
|---|---|
| Size | 33.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
70a6ee46ba2187fa62aec6bfe38714c5c10ca4ac0b6b640bae6bbac5b7a651f7
|
|
BLAKE2b-256 checksum How to use checksums |
913699b91396267f11796fecc616a1158775d22496ceee26612a385ff6360f97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.14
|
Release files / ffolayer-0.1.2-py3-none-any.whl
| Download URL | ffolayer-0.1.2-py3-none-any.whl |
|---|---|
| Size | 26.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f4177124140779be43ac5a058c5b79fd3d3eab49a21d87ad2ab576e8c656bdd7
|
|
BLAKE2b-256 checksum How to use checksums |
e852287373912001ad0cc6a877ab8e108c9fc50d10eecdf5c8934be7e1b2bd7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.14
|
Security for ffolayer 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.