{ "cells": [ { "cell_type": "markdown", "id": "f2d7b3c9", "metadata": {}, "source": [ "# CVXPY: Convex optimization, for everyone\n", "\n", "\n", "
\n", "\"cvxpy\n", "
\n", "\n", "\n", "[**CVXPY**](https://www.cvxpy.org/) is an open-source Python tool tailored for convex optimization problems. Unlike many platforms which require you to express your problem in strict formats determined by optimization solvers, CVXPY allows users to lay out their problem in a way that follows the maths.\n", "\n", "A significant benefit of this is that CVXPY decouples the problem's formulation from the solver used to tackle it. This means that once a problem is defined within CVXPY, one can seamlessly switch between or experiment with different solvers without the need to adjust the problem's representation. Additionally, CVXPY supports an extensive range of both commercial and open-source solvers, providing users with the flexibility to choose the most appropriate tool for their specific challenges, be it for experimentation or scalability. This versatility simplifies the optimization process, making it more accessible and efficient." ] }, { "cell_type": "code", "execution_count": null, "id": "7fda3624", "metadata": {}, "outputs": [], "source": [ "import corneto as cn\n", "\n", "cn.info()" ] }, { "cell_type": "code", "execution_count": 10, "id": "9b1ff17a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "===============================================================================\n", " CVXPY \n", " v1.3.2 \n", "===============================================================================\n", "(CVXPY) Aug 24 11:00:36 AM: Your problem has 5 variables, 2 constraints, and 0 parameters.\n", "(CVXPY) Aug 24 11:00:36 AM: It is compliant with the following grammars: DCP, DQCP\n", "(CVXPY) Aug 24 11:00:36 AM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)\n", "(CVXPY) Aug 24 11:00:36 AM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.\n", "-------------------------------------------------------------------------------\n", " Compilation \n", "-------------------------------------------------------------------------------\n", "(CVXPY) Aug 24 11:00:36 AM: Compiling problem (target solver=OSQP).\n", "(CVXPY) Aug 24 11:00:36 AM: Reduction chain: CvxAttr2Constr -> Qp2SymbolicQp -> QpMatrixStuffing -> OSQP\n", "(CVXPY) Aug 24 11:00:36 AM: Applying reduction CvxAttr2Constr\n", "(CVXPY) Aug 24 11:00:36 AM: Applying reduction Qp2SymbolicQp\n", "(CVXPY) Aug 24 11:00:36 AM: Applying reduction QpMatrixStuffing\n", "(CVXPY) Aug 24 11:00:36 AM: Applying reduction OSQP\n", "(CVXPY) Aug 24 11:00:36 AM: Finished problem compilation (took 1.566e-02 seconds).\n", "-------------------------------------------------------------------------------\n", " Numerical solver \n", "-------------------------------------------------------------------------------\n", "(CVXPY) Aug 24 11:00:36 AM: Invoking solver OSQP to obtain a solution.\n", "-----------------------------------------------------------------\n", " OSQP v0.6.2 - Operator Splitting QP Solver\n", " (c) Bartolomeo Stellato, Goran Banjac\n", " University of Oxford - Stanford University 2021\n", "-----------------------------------------------------------------\n", "problem: variables n = 5, constraints m = 6\n", " nnz(P) + nnz(A) = 10\n", "settings: linear system solver = qdldl,\n", " eps_abs = 1.0e-05, eps_rel = 1.0e-05,\n", " eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,\n", " rho = 1.00e-01 (adaptive),\n", " sigma = 1.00e-06, alpha = 1.60, max_iter = 10000\n", " check_termination: on (interval 25),\n", " scaling: on, scaled_termination: off\n", " warm start: on, polish: on, time_limit: off\n", "\n", "iter objective pri res dua res rho time\n", " 1 -5.2220e+00 3.68e+00 1.70e+02 1.00e-01 9.57e-05s\n", " 150 6.9994e-01 1.96e-05 7.82e-06 4.77e+00 5.18e-04s\n", "plsh 7.0000e-01 1.95e-30 0.00e+00 -------- 7.51e-04s\n", "\n", "status: solved\n", "solution polish: successful\n", "number of iterations: 150\n", "optimal objective: 0.7000\n", "run time: 7.51e-04s\n", "optimal rho estimate: 9.81e+00\n", "\n", "-------------------------------------------------------------------------------\n", " Summary \n", "-------------------------------------------------------------------------------\n", "(CVXPY) Aug 24 11:00:36 AM: Problem status: optimal\n", "(CVXPY) Aug 24 11:00:36 AM: Optimal value: -3.000e-01\n", "(CVXPY) Aug 24 11:00:36 AM: Compilation took 1.566e-02 seconds\n", "(CVXPY) Aug 24 11:00:36 AM: Solver (including time spent in interface) took 8.014e-03 seconds\n" ] } ], "source": [ "import numpy as np\n", "\n", "from corneto.backend import CvxpyBackend\n", "\n", "backend = CvxpyBackend()\n", "P = backend.Problem()\n", "A = np.array([[0.12, 0.92, 0.76, 0.98, 0.79], [0.58, 0.57, 0.53, 0.71, 0.55]])\n", "b = np.array([1, 0])\n", "x = backend.Variable(\"x\", A.shape[1])\n", "P += sum(x) == 1, x >= 0\n", "\n", "P.add_objectives(sum(A @ x - b))\n", "cvxpy_model = P.solve(solver=\"osqp\", verbosity=1)" ] }, { "cell_type": "code", "execution_count": 11, "id": "fba5b568", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.30000000000000004" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "P.objectives[0].value" ] }, { "cell_type": "code", "execution_count": 12, "id": "cb695903", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "corneto.backend._base.ProblemDef" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(P)" ] }, { "cell_type": "code", "execution_count": 13, "id": "d99c8a1e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "cvxpy.problems.problem.Problem" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(cvxpy_model)" ] }, { "cell_type": "code", "execution_count": 16, "id": "fa5f8e8a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'optimal'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cvxpy_model.status" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }