Skip to main content

No project description provided

Project description

Build Status

diffcp

diffcp is a Python package for computing the derivative of a convex cone program, with respect to its problem data. The derivative is implemented as an abstract linear map, with methods for its forward application and its adjoint.

The implementation is based on the calculations in our paper Differentiating through a cone program.

Installation

diffcp is available on PyPI, as a source distribution. Install it with

pip install diffcp

You will need a C++11-capable compiler to build diffcp.

diffcp requires:

diffcp uses Eigen; Eigen operations can be automatically vectorized by compilers. To enable vectorization, install with

MARCH_NATIVE=1 pip install diffcp

OpenMP can be enabled by passing extra arguments to your compiler. For example, on linux, you can tell gcc to activate the OpenMP extension by specifying the flag "-fopenmp":

OPENMP_FLAG="-fopenmp" pip install diffcp

To enable both vectorization and OpenMP (on linux), use

MARCH_NATIVE=1 OPENMP_FLAG="-fopenmp" pip install diffcp

Cone programs

diffcp differentiates through a primal-dual cone program pair. The primal problem must be expressed as

minimize        c'x
subject to      Ax + s = b
                s in K

where x and s are variables, A, b and c are the user-supplied problem data, and K is a user-defined convex cone. The corresponding dual problem is

minimize        b'y
subject to      A'y + c == 0
                y in K^*

with dual variable y.

Usage

diffcp exposes the function

solve_and_derivative(A, b, c, cone_dict, warm_start=None, solver=None, **kwargs).

This function returns a primal-dual solution x, y, and s, along with functions for evaluating the derivative and its adjoint (transpose). These functions respectively compute right and left multiplication of the derivative of the solution map at A, b, and c by a vector. The solver argument determines which solver to use; the available solvers are solver="SCS" and solver="ECOS". If no solver is specified, diffcp will choose the solver itself. In the case that the problem is not solved, i.e. the solver fails for some reason, we will raise a SolverError Exception.

Arguments

The arguments A, b, and c correspond to the problem data of a cone program.

  • A must be a SciPy sparse CSC matrix.
  • b and c must be NumPy arrays.
  • cone_dict is a dictionary that defines the convex cone K.
  • warm_start is an optional tuple (x, y, s) at which to warm-start. (Note: this is only available for the SCS solver).
  • **kwargs are keyword arguments to forward to the solver (e.g., verbose=False).

These inputs must conform to the SCS convention for problem data. The keys in cone_dict correspond to the cones, with

  • diffcp.ZERO for the zero cone,
  • diffcp.POS for the positive orthant,
  • diffcp.SOC for a product of SOC cones,
  • diffcp.PSD for a product of PSD cones, and
  • diffcp.EXP for a product of exponential cones.

The values in cone_dict denote the sizes of each cone; the values of diffcp.SOC, diffcp.PSD, and diffcp.EXP should be lists. The order of the rows of A must match the ordering of the cones given above. For more details, consult the SCS documentation.

Return value

The function solve_and_derivative returns a tuple

(x, y, s, derivative, adjoint_derivative)
  • x, y, and s are a primal-dual solution.

  • derivative is a function that applies the derivative at (A, b, c) to perturbations dA, db, dc. It has the signature derivative(dA, db, dc) -> dx, dy, ds, where dA is a SciPy sparse CSC matrix with the same sparsity pattern as A, and db and dc are NumPy arrays. dx, dy, and ds are NumPy arrays, approximating the change in the primal-dual solution due to the perturbation.

  • adjoint_derivative is a function that applies the adjoint of the derivative to perturbations dx, dy, ds. It has the signature adjoint_derivative(dx, dy, ds) -> dA, db, dc, where dx, dy, and ds are NumPy arrays.

Example

import numpy as np
from scipy import sparse

import diffcp

cone_dict = {
    diffcp.ZERO: 3,
    diffcp.POS: 3,
    diffcp.SOC: [5]
}

m = 3 + 3 + 5
n = 5

A, b, c = diffcp.utils.random_cone_prog(m, n, cone_dict)
x, y, s, D, DT = diffcp.solve_and_derivative(A, b, c, cone_dict)

# evaluate the derivative
nonzeros = A.nonzero()
data = 1e-4 * np.random.randn(A.size)
dA = sparse.csc_matrix((data, nonzeros), shape=A.shape)
db = 1e-4 * np.random.randn(m)
dc = 1e-4 * np.random.randn(n)
dx, dy, ds = D(dA, db, dc)

# evaluate the adjoint of the derivative
dx = c
dy = np.zeros(m)
ds = np.zeros(m)
dA, db, dc = DT(dx, dy, ds)

For more examples, including the SDP example described in the paper, see the examples directory.

Citing

If you wish to cite diffcp, please use the following BibTex:

@article{diffcp2019,
    author       = {Agrawal, A. and Barratt, S. and Boyd, S. and Busseti, E. and Moursi, W.},
    title        = {Differentiating through a Cone Program},
    journal      = {Journal of Applied and Numerical Optimization},
    year         = {2019},
    volume       = {1},
    number       = {2},
    pages        = {107--115},
}

@misc{diffcp,
    author       = {Agrawal, A. and Barratt, S. and Boyd, S. and Busseti, E. and Moursi, W.},
    title        = {{diffcp}: differentiating through a cone program, version 1.0},
    howpublished = {\url{https://github.com/cvxgrp/diffcp}},
    year         = 2019
}

The following thesis concurrently derived the mathematics behind differentiating cone programs.

@phdthesis{amos2019differentiable,
  author       = {Brandon Amos},
  title        = {{Differentiable Optimization-Based Modeling for Machine Learning}},
  school       = {Carnegie Mellon University},
  year         = 2019,
  month        = May,
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffcp-1.0.21.tar.gz (2.2 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

diffcp-1.0.21-cp310-cp310-win_amd64.whl (187.8 kB view details)

Uploaded CPython 3.10Windows x86-64

diffcp-1.0.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

diffcp-1.0.21-cp310-cp310-macosx_10_9_x86_64.whl (270.2 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

diffcp-1.0.21-cp39-cp39-win_amd64.whl (186.0 kB view details)

Uploaded CPython 3.9Windows x86-64

diffcp-1.0.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

diffcp-1.0.21-cp39-cp39-macosx_10_9_x86_64.whl (270.4 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

diffcp-1.0.21-cp38-cp38-win_amd64.whl (187.7 kB view details)

Uploaded CPython 3.8Windows x86-64

diffcp-1.0.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

diffcp-1.0.21-cp38-cp38-macosx_10_9_x86_64.whl (270.2 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

diffcp-1.0.21-cp37-cp37m-win_amd64.whl (187.8 kB view details)

Uploaded CPython 3.7mWindows x86-64

diffcp-1.0.21-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

diffcp-1.0.21-cp37-cp37m-macosx_10_9_x86_64.whl (267.8 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

File details

Details for the file diffcp-1.0.21.tar.gz.

File metadata

  • Download URL: diffcp-1.0.21.tar.gz
  • Upload date:
  • Size: 2.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.14

File hashes

Hashes for diffcp-1.0.21.tar.gz
Algorithm Hash digest
SHA256 4314a04f5eb724853fa1c6f318fa632e3c2f2278e352cec0b8e457dbeacfcf0c
MD5 49c3c4233327ccab8ad29a0a14e3a107
BLAKE2b-256 d3e9980d47c8478de5dd3113ccbdcbe3deafd62b6d6efced341d6bad1690bbbc

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: diffcp-1.0.21-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 187.8 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for diffcp-1.0.21-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1e1da2144fb802ba59e1067ac8c666773ce8cf6e841219a92480230de8ce8f00
MD5 12d52206717bcc9ee742d43b9db5366a
BLAKE2b-256 1e1a662180a18f2b0ce875c20bbd1aa9ad9446bebc7bcf8a786db7087dbc1d72

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.0.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 edf1d1d2e27fa6f748848eb31b9b371fc2213c9df6a5b3e6ed8f19627dd4c075
MD5 0ba68a5e12717c48d383050ecf2d8f52
BLAKE2b-256 1adeac8de1df75dd2505ca0099a96348dd8fdef80f74b0bcba32ee8673021ff6

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.0.21-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5a41dc87a4bb5bdbaa35dbbba095de7e8350d872c4e91efa5ec3f402481685b9
MD5 b92b565d39c08be17ffaa47c4c3080b3
BLAKE2b-256 8ad0138c9801efe9d1f4567f499e6ec3115578e60dc63405542bcc240932a9b2

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: diffcp-1.0.21-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 186.0 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for diffcp-1.0.21-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 175e4a360d21029a503d97cf0d8866e6e3357b90a899404e3d3cd883475f0a3d
MD5 cc84871ed9eceec39e6a95176caddcb3
BLAKE2b-256 4790c80ff3582a162eb296d5ddeab399fbdd490fc10324236748b394695c9910

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.0.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 065d7ddb985f2d2ea64b847137bd6e758ed4e6dbab3f68b84f80d485b55a5daa
MD5 596a9b9f1ed84c8023fd496cc2eddec1
BLAKE2b-256 75de34499b6f6cb39cb483e7de7de2101183959f2d3a416f7827f80905f028bb

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.0.21-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 577ae0bce16e2f83f6d0847399984fa69d0ceec383f3178e3d7bf31e1f074f3a
MD5 af5b6130a88d68890c38b0e7a168f1a7
BLAKE2b-256 c75b8ba2e3a5c42e0d8215097cbfa9ef30a6acb2d8e186bc4132ed3d613ac9c6

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: diffcp-1.0.21-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 187.7 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for diffcp-1.0.21-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 366709dbf0be2c5c345bc9f448e8171c61a015ba62f9e2df10461b5fc068b7c1
MD5 ee3a5e056def71ca7def36347eb1b743
BLAKE2b-256 f91b28dfac912d7e41037a21e68c290e334c70ed537b4bf4b00b8b1fe42f9b37

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.0.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 037d086f320d9cfb5441608b83297b51c78c9bb29d207a92b35504555d1d0bd9
MD5 3abdab3600823c57d5fc1376f170faa7
BLAKE2b-256 1cbcc8cc419571e1f424982143c5ac68edc1a0bbbb50302d9af05bcbe65bfa09

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.0.21-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8bc5d18c1a5588f606739e400c26233229f6fe8a9a9199258dbe18e6d6d6cc82
MD5 a29a95657db64564f3408a53a44b9bc0
BLAKE2b-256 ac0175fae181a625273f8e6211d19d8a32355924c2ab80740142c5076a7dd80b

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: diffcp-1.0.21-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 187.8 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.9

File hashes

Hashes for diffcp-1.0.21-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 0783ab18f73801f7b8b766df18c6042ae51b5ee540dbe3e52ff842a860d17b51
MD5 2a894fdfbb2c95e4821daf3e1a6f7aa3
BLAKE2b-256 9bc4a36d3acba2e9323d28ee6226c3a3a5b02ddb22f0e85c91ac2ae81be00d89

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.0.21-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a333746390697fc640d72718ccf3abe3ba8bfffb559a40dbb0c46aaad922bc42
MD5 f2ab312aa7047de50475218985c8f54c
BLAKE2b-256 26472a79fdba46b97078892beec6402cf92cbfef788575573f292f5c8a3c6ea4

See more details on using hashes here.

File details

Details for the file diffcp-1.0.21-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.0.21-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 91e04f21c8d6c245a668daf0199f89af8b68c99266c7aa5f38324780474caf07
MD5 9d3e37d937d3af9d9156f505a34e03ff
BLAKE2b-256 55100524ddb68538359ae35d31068849653045277471baa90a7c8f94c6ad06d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page