{ "cells": [ { "cell_type": "markdown", "id": "f2d7b3c9", "metadata": {}, "source": [ "# PICOS: A Python interface to conic optimization solvers\n", "\n", "\n", "
\n", "\"PICOS\n", "
\n", "\n", "\n", "[**PICOS**](https://picos-api.gitlab.io/picos/index.html) is a Python API that offers a convenient interface to various conic and integer programming solvers. Through PICOS, users can define optimization problems using high-level models, which support intricate vector, matrix variables, and multidimensional algebra. Once the problem is defined, PICOS automatically converts it into a format that's comprehensible by any suitable solver present during runtime. This flexibility ensures that applications remain adaptable, giving users the liberty to select from multiple commercial or open-source solvers.\n", "\n", "As with CVXPY, CORNETO allows to use PICOS as a backend for solving optimization problems. While CVXPY and PICOS have similar philosophy, their specific features, functions, and supported solvers might differ. Users might opt for one over the other based on individual needs or preferences.\n", "\n", "CORNETO can be seen also as a multi-backend implementation of PICOS and CVXPY. You can choose the backend you prefer, and CORNETO will take care of the rest." ] }, { "cell_type": "markdown", "id": "280b3377", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 1, "id": "cadfc0aa", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "
Installed version:v0.9.2-beta.1
Available backends:CVXPY v1.3.2, PICOS v2.4.17
Default backend (corneto.K):CVXPY
Installed solvers:CBC, CLARABEL, COPT, CPLEX, CVXOPT, DIFFCP, ECOS, ECOS_BB, GLPK, GLPK_MI, GUROBI, MOSEK, OSQP, PROXQP, SCIP, SCIPY, SCS
Graphviz version:v0.20.1
Repository:https://github.com/saezlab/corneto
\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import corneto as cn\n", "\n", "cn.info()" ] }, { "cell_type": "code", "execution_count": 22, "id": "37543167", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "===================================\n", " PICOS 2.4.17 \n", "===================================\n", "Problem type: Linear Program.\n", "Searching a solution strategy for CVXOPT.\n", "Solution strategy:\n", " 1. ExtraOptions\n", " 2. CVXOPTSolver\n", "Applying ExtraOptions.\n", "Building a CVXOPT problem instance.\n", "Starting solution search.\n", "-----------------------------------\n", " Python Convex Optimization Solver \n", " via internal CONELP solver \n", "-----------------------------------\n", " pcost dcost gap pres dres k/t\n", " 0: 1.3020e+00 1.3020e+00 2e+00 2e-16 1e+00 1e+00\n", " 1: 1.1568e+00 1.1401e+00 5e-01 3e-16 3e-01 3e-01\n", " 2: 7.4020e-01 7.0348e-01 1e-01 3e-16 8e-02 3e-02\n", " 3: 7.0098e-01 7.0046e-01 2e-03 2e-16 1e-03 4e-04\n", " 4: 7.0001e-01 7.0000e-01 2e-05 1e-16 1e-05 4e-06\n", " 5: 7.0000e-01 7.0000e-01 2e-07 3e-16 1e-07 4e-08\n", " 6: 7.0000e-01 7.0000e-01 2e-09 2e-16 1e-09 4e-10\n", "Optimal solution found.\n", "------------[ CVXOPT ]-------------\n", "Solver claims optimal solution for feasible problem.\n", "Applying the solution.\n", "Applied solution is primal feasible.\n", "Search 1.5e-02s, solve 2.5e-02s, overhead 66%.\n", "=============[ PICOS ]=============\n" ] } ], "source": [ "import numpy as np\n", "\n", "from corneto.backend import PicosBackend\n", "\n", "backend = PicosBackend()\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", "# Convex optimization problem\n", "P.add_objectives(sum(A @ x - b))\n", "picos_model = P.solve(solver=\"cvxopt\", verbosity=1)" ] }, { "cell_type": "code", "execution_count": 23, "id": "7047a7d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'x': <5×1 Real Variable: x>}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "P.symbols" ] }, { "cell_type": "code", "execution_count": 24, "id": "c78f5c95", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9999999999999998" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(P.symbols[\"x\"].value)" ] }, { "cell_type": "code", "execution_count": 25, "id": "235406d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "corneto.backend._base.ProblemDef" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(P)" ] }, { "cell_type": "code", "execution_count": 26, "id": "5e2900b6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[<1×1 Real Affine Expression: ([2×5]·x - [2×1])[0] + ([2×5]·x - [2×1])[1]>]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "P.objectives" ] }, { "cell_type": "code", "execution_count": 27, "id": "f5c49273", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.2999999990174851" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "P.objectives[0].value" ] }, { "cell_type": "code", "execution_count": 28, "id": "b95e2eaf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "picos.modeling.problem.Problem" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(picos_model)" ] }, { "cell_type": "code", "execution_count": 29, "id": "85947647", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.2999999990174851" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "picos_model.value" ] }, { "cell_type": "code", "execution_count": 31, "id": "c6564f2d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "corneto.backend._picos_backend.PicosExpression" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(A @ x)" ] }, { "cell_type": "code", "execution_count": 32, "id": "9a01b887", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "picos.expressions.exp_affine.AffineExpression" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type((A @ x).e)" ] } ], "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 }