Package: diffcp
diffcpdiffcpis 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 paperDifferentiating through a cone program.Installationdiffcpis available on PyPI, as a source distribution. Install it withpipinstalldiffcpYou will need a C++11-capable compiler to builddiffcp.diffcprequires:NumPy>= 1.15SciPy>= 1.10SCS>= 2.0.2pybind11>= 2.4threadpoolctl>= 1.1ECOS>= 2.0.10Python >= 3.7diffcpuses Eigen; Eigen operations can be automatically vectorized by compilers. To enable vectorization, install withMARCH_NATIVE=1pipinstalldiffcpOpenMP 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"pipinstalldiffcpTo enable both vectorization and OpenMP (on linux), useMARCH_NATIVE=1OPENMP_FLAG="-fopenmp"pipinstalldiffcpCone programsdiffcpdifferentiates through a primal-dual cone program pair. The primal problem must be expressed asminimize        c'x
subject to      Ax + s = b
                s in Kwherexandsare variables,A,bandcare the user-supplied problem data, andKis a user-defined convex cone. The corresponding dual problem isminimize        b'y
subject to      A'y + c == 0
                y in K^*with dual variabley.Usagediffcpexposes the functionsolve_and_derivative(A,b,c,cone_dict,warm_start=None,solver=None,**kwargs).This function returns a primal-dual solutionx,y, ands, 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 atA,b, andcby a vector.
Thesolverargument determines which solver to use; the available solvers
aresolver="SCS"andsolver="ECOS".
If no solver is specified,diffcpwill choose the solver itself.
In the case that the problem is not solved, i.e. the solver fails for some reason, we will raise
aSolverErrorException.ArgumentsThe argumentsA,b, andccorrespond to the problem data of a cone program.Amust be aSciPy sparse CSC matrix.bandcmust be NumPy arrays.cone_dictis a dictionary that defines the convex coneK.warm_startis an optional tuple(x, y, s)at which to warm-start. (Note: this is only available for the SCS solver).**kwargsare keyword arguments to forward to the solver (e.g.,verbose=False).These inputs must conform to theSCS conventionfor problem data. The keys incone_dictcorrespond to the cones, withdiffcp.ZEROfor the zero cone,diffcp.POSfor the positive orthant,diffcp.SOCfor a product of SOC cones,diffcp.PSDfor a product of PSD cones, anddiffcp.EXPfor a product of exponential cones.The values incone_dictdenote the sizes of each cone; the values ofdiffcp.SOC,diffcp.PSD, anddiffcp.EXPshould be lists. The order of the rows ofAmust match the ordering of the cones given above. For more details, consult theSCS documentation.Return valueThe functionsolve_and_derivativereturns a tuple(x,y,s,derivative,adjoint_derivative)x,y, andsare a primal-dual solution.derivativeis a function that applies the derivative at(A, b, c)to perturbationsdA,db,dc. It has the signaturederivative(dA, db, dc) -> dx, dy, ds, wheredAis a SciPy sparse CSC matrix with the same sparsity pattern asA, anddbanddcare NumPy arrays.dx,dy, anddsare NumPy arrays, approximating the change in the primal-dual solution due to the perturbation.adjoint_derivativeis a function that applies the adjoint of the derivative to perturbationsdx,dy,ds. It has the signatureadjoint_derivative(dx, dy, ds) -> dA, db, dc, wheredx,dy, anddsare NumPy arrays.Exampleimportnumpyasnpfromscipyimportsparseimportdiffcpcone_dict={diffcp.ZERO:3,diffcp.POS:3,diffcp.SOC:[5]}m=3+3+5n=5A,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 derivativenonzeros=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 derivativedx=cdy=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 theexamplesdirectory.CitingIf you wish to citediffcp, 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,
}