ncopt

Constrained optimization for Pytorch using the SQP-GS algorithm

https://github.com/fabian-sp/ncopt

Science Score: 57.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 2 DOI reference(s) in README
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.2%) to scientific vocabulary

Keywords

constrained-optimization optimization pytorch sqp
Last synced: 6 months ago · JSON representation ·

Repository

Constrained optimization for Pytorch using the SQP-GS algorithm

Basic Info
  • Host: GitHub
  • Owner: fabian-sp
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 1 MB
Statistics
  • Stars: 34
  • Watchers: 2
  • Forks: 4
  • Open Issues: 1
  • Releases: 2
Topics
constrained-optimization optimization pytorch sqp
Created about 5 years ago · Last pushed almost 2 years ago
Metadata Files
Readme License Citation

README.md

ncOPT

This repository is for solving constrained optimization problems where objective and/or constraint functions are (arbitrary) PyTorch modules. It is mainly intended for optimization with pre-trained networks, but might be useful also in other contexts.

Short Description

The algorithms in this package can solve problems of the form

min f(x) s.t. g(x) <= 0 h(x) = 0

where f, g and h are locally Lipschitz functions.

Key functionalities of the package:

  • forward and backward pass of functions can be done on GPU
  • batched evaluation and Jacobian computation using PyTorch's autograd; no gradient implementation needed
  • support for inequality and equality constraints; constraints can use only a subset of the optimization variable as input

Table of contents

  1. Installation
  2. Getting started
  3. Examples
  4. References

Disclaimer

1) We have not (yet) extensively tested the solver on large-scale problems. 2) The implemented solver is designed for nonsmooth, nonconvex problems, and as such, can solve a very general problem class. If your problem has a specific structure (e.g. convexity), then you will almost certainly get better performance by using software/solvers that are specifically written for the respective problem type. As starting point, check out cvxpy. 3) The solver is not guaranteed to converge to a (global) solution (see the theoretical results in [1] for convergence to local solutions under certain assumptions).

Installation

The package is available from PyPi with

python -m pip install ncopt

Alternatively, for an editable version of this package in your Python environment, clone this repository and run the command

python -m pip install --editable .

Getting started

Solver interface

The main solver implemented in this package is called SQP-GS, and has been developed by Curtis and Overton in [1]. See the detailed documentation.

The SQP-GS solver can be called via

python from ncopt.sqpgs import SQPGS problem = SQPGS(f, gI, gE) problem.solve() Here f is the objective function, and gI and gE are a list of inequality and equality constraints. An empty list can be passed if no (in)equality constraints are needed.

The ObjectiveOrConstraint class

The objective f and each element of gI and gE should be passed as an instance of ncopt.functions.ObjectiveOrConstraint (a simple wrapper around a torch.nn.Module).

  • Each constraint function is allowed to have multi-dimensional output (see example below).
  • IMPORTANT: For the objective, we further need to specify the dimension of the optimization variable with the argument dim. For each constraint, we need to specify the output dimension with dim_out.

For example, a linear constraint function Ax - b <= 0 can be implemented as follows:

python from ncopt.functions import ObjectiveOrConstraint A = .. # your data b = .. # your data g = ObjectiveOrConstraint(torch.nn.Linear(2, 2), dim_out=2) g.model.weight.data = A # pass A g.model.bias.data = -b # pass b

Functionalities

Let's assume we have a torch.nn.Module, call it model, which we want to use as objective/constraint. For the solver, we can pass the function as

f = ObjectiveOrConstraint(model)

  • Dimension handling: Each function must be designed for batched evaluation (that is, the first dimension of the input is the batch size). Also, the output shape of model must be two-dimensional, that is (batch_size, dim_out), where dim_out is the output dimension for a constraint, and dim_out=1 for the objective.

  • Device handling: The forward pass, and Jacobian calculation is done on the device on which the parameters of your model. For example, you can use model.to(device) before creating f. See this Colab example how to use a GPU.

  • Input preparation: Different constraints might only need a part of the optimization variable as input, or might require additional preparation such as reshaping from vector to image. (Note that the optimization variable is handled always as vector). For this, you can specify a callable prepare_input when initializing a ObjectiveOrConstraint object. Any reshaping or cropping etc. can be handled with this function. Please note that prepare_input should be compatible with batched forward passes.

Examples

2D Nonsmooth Rosenbrock

This example is taken from Example 5.1 in [1] and involves minimizing a nonsmooth Rosenbrock function, constrained with a maximum function. The picture below shows the trajectory of the SQP-GS solver for different starting points. The final iterates are marked with the black plus while the analytical solution is marked with the golden star. We can see that the algorithm finds the minimizer consistently.

Link to example script

SQP-GS trajectories for a 2-dim example

Sparse signal recovery

This example is taken from Example 5.3 in [1]. We minimize the q-norm $||x||_q$ under the constraint of approximate signal recovery $||Rx-y|| \leq \delta$. Here $R$ comes from the Discrete Cosine Transform.

Link to example script

When $q\geq 1$ the problem is convex and rather easy to solve. For $q<1$, we observed that the number of sample points need to be increased a lot in order to make the subproblems solvable in reasonable time.

This problem has dimension 256, with one scalar constraint. To give a feeling for runtime, below is the runtime per iteration (for $q=1$), split into the main parts: sample points and compute gradients (sample_and_grad), solve the quadratic subproblem (subproblem), do the update step of iterate and approximate Hessian (step), and all other routines.

Timings for SQP-GS with dim 256

Pretrained neural network constraint

This toy example illustrates how to use a pretrained neural network as constraint function in ncOPT. We train a simple model to learn the mapping $(x1,x2) \mapsto \max(\sqrt{2}x1, 2x2) -1$. Then, we load the model checkpoint to use it as constraint.

Below we show the feasible set (in blue), and the final iterate, if we use as objective the squared distance to the vector of ones.

Link to example script

Link to training script

SQP-GS final iterate with learned constraint

References

[1] Frank E. Curtis and Michael L. Overton, A sequential quadratic programming algorithm for nonconvex, nonsmooth constrained optimization, SIAM Journal on Optimization 2012 22:2, 474-500, https://doi.org/10.1137/090780201.

Owner

  • Login: fabian-sp
  • Kind: user
  • Location: Munich
  • Company: TUM

:bulb: Optimization :coffee: Machine Learning

Citation (CITATION.cff)

cff-version: 1.2.0
authors:
  - family-names: Schaipp
    given-names: Fabian
  - family-names: Schiele
    given-names: Philipp
title: "Constrained optimization for Pytorch using the SQP-GS algorithm."
version: 0.1.0
date-released: 2024-xx-xx

GitHub Events

Total
  • Watch event: 5
  • Issue comment event: 1
  • Fork event: 2
Last Year
  • Watch event: 5
  • Issue comment event: 1
  • Fork event: 2

Committers

Last synced: about 2 years ago

All Time
  • Total Commits: 28
  • Total Committers: 2
  • Avg Commits per committer: 14.0
  • Development Distribution Score (DDS): 0.143
Past Year
  • Commits: 3
  • Committers: 1
  • Avg Commits per committer: 3.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
fabian-sp f****p@h****e 24
fabian-sp 5****p 4
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 8 months ago

All Time
  • Total issues: 1
  • Total pull requests: 15
  • Average time to close issues: N/A
  • Average time to close pull requests: 6 days
  • Total issue authors: 1
  • Total pull request authors: 2
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.6
  • Merged pull requests: 12
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 1
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 1
  • Pull request authors: 0
  • Average comments per issue: 0.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • gnorman7 (1)
Pull Request Authors
  • phschiele (14)
  • fabian-sp (12)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 22 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 2
  • Total maintainers: 1
pypi.org: ncopt

Constrained optimization for Pytorch using the SQP-GS algorithm.

  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 22 Last month
Rankings
Dependent packages count: 10.9%
Stargazers count: 15.9%
Forks count: 17.6%
Average: 26.6%
Dependent repos count: 61.8%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/workflows/unit_tests.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
requirements.txt pypi
  • cvxopt >=1.2.0
  • numpy *