### Getting started with solver-benchmarks Source: https://www.cvxpy.org/resources/solver_benchmarks/index.html Clone the repository, install dependencies, run benchmarks, and summarize results. ```bash git clone https://github.com/cvxpy/solver-benchmarks.git cd solver-benchmarks uv sync uv run python run_benchmarks.py uv run python summarize.py ``` -------------------------------- ### Example setup Source: https://www.cvxpy.org/examples/applications/maximise_minimum_SINR_BV4.20.html Sets up the parameters for the example case. ```python np.set_printoptions(precision=3) # in this case we will use a gain matrix with a signal weight of 0.6 and interference weight of 0.1 G = np.array([[0.6,0.1,0.1,0.1,0.1], [0.1,0.6,0.1,0.1,0.1], [0.1,0.1,0.6,0.1,0.1], [0.1,0.1,0.1,0.6,0.1], [0.1,0.1,0.1,0.1,0.6]]) # in this case m=n, but this generalises if we want n receivers and m transmitters n, m = np.shape(G) # set maximum power of each transmitter and receiver saturation level P_max = np.array([1.]*n) ``` -------------------------------- ### Warm start example Source: https://www.cvxpy.org/tutorial/solvers/index.html This example demonstrates how warm start can accelerate solving a sequence of related least-squares problems by reusing previous solve information. ```python import cvxpy as cp import numpy # Problem data. m = 2000 n = 1000 numpy.random.seed(1) A = numpy.random.randn(m, n) b = cp.Parameter(m) # Construct the problem. x = cp.Variable(n) prob = cp.Problem(cp.Minimize(cp.sum_squares(A @ x - b)), [x >= 0]) b.value = numpy.random.randn(m) prob.solve() print("First solve time:", prob.solver_stats.solve_time) b.value = numpy.random.randn(m) prob.solve(warm_start=True) print("Second solve time:", prob.solver_stats.solve_time) ``` -------------------------------- ### Example Setup Source: https://www.cvxpy.org/examples/applications/water_filling_BVex5.2.html?q= Sets up an example for the water-filling problem with 3 buckets and specific alpha values. It also configures numpy for printing precision. ```python # As an example, we will solve the water filling problem with 3 buckets, each with different α np.set_printoptions(precision=3) buckets = 3 alpha = np.array([0.8, 1.0, 1.2]) ``` -------------------------------- ### Getting Problem Data Example Source: https://www.cvxpy.org/api_reference/cvxpy.problems.html Shows how to retrieve the low-level problem data, the solving chain, and inverse data used by a solver. It demonstrates how to use these components to solve the problem via the data and then unpack the results. ```python objective = ... constraints = ... problem = cp.Problem(objective, constraints) data, chain, inverse_data = problem.get_problem_data(cp.SCS) # calls SCS using `data` soln = chain.solve_via_data(problem, data) ``` -------------------------------- ### Install cyipopt Source: https://www.cvxpy.org/install/index.html Install the Python interface for IPOPT. ```bash pip install cyipopt ``` -------------------------------- ### Install CVXPY from source Source: https://www.cvxpy.org/install/index.html Install CVXPY from source. ```bash pip install . ``` -------------------------------- ### Install IPOPT Source: https://www.cvxpy.org/install/index.html Instructions for installing the IPOPT system library and the Python interface cyipopt. ```bash # First install the IPOPT system library, followed by the Python interface cyipopt: ``` -------------------------------- ### Install IPOPT Source: https://www.cvxpy.org/install/index.html Installation commands for IPOPT on Ubuntu/Debian, macOS, and Windows. ```bash sudo apt-get install coinor-libipopt-dev ``` ```bash brew install ipopt ``` ```bash conda install -c conda-forge ipopt ``` -------------------------------- ### Install CVXOPT with GLPK bindings (Manual) Source: https://www.cvxpy.org/install/index.html?q= On platforms where GLPK is not bundled with CVXOPT, these environment variables and installation command are used to install CVXOPT with GLPK support. ```bash CVXOPT_BUILD_GLPK=1 CVXOPT_GLPK_LIB_DIR=/path/to/glpk-X.X/lib CVXOPT_GLPK_INC_DIR=/path/to/glpk-X.X/include pip install cvxopt ``` -------------------------------- ### Install CVXOPT and GLPK Source: https://www.cvxpy.org/install/index.html?q= On platforms where GLPK is bundled with CVXOPT, this command suffices to install support for both solvers. ```bash pip install cvxopt ``` -------------------------------- ### Install CVXPY using pip Source: https://www.cvxpy.org/install/index.html Basic installation of CVXPY using pip. ```bash pip install cvxpy ``` -------------------------------- ### Example QCP Source: https://www.cvxpy.org/tutorial/dqcp/index.html?q= Example code that solves a simple QCP. ```python import cvxpy as cp x = cp.Variable() y = cp.Variable(pos=True) objective_fn = -cp.sqrt(x) / y problem = cp.Problem(cp.Minimize(objective_fn), [cp.exp(x) <= y]) problem.solve(qcp=True) assert problem.is_dqcp() print("Optimal value: ", problem.value) print("x: ", x.value) print("y: ", y.value) ``` -------------------------------- ### Example Usage and Output Source: https://www.cvxpy.org/version/1.9/examples/applications/optimal_power_gaussian_channel_BV4.62.html?q= Demonstrates how to use the `optimal_power` function with specific parameters and shows the expected output, including the optimization status, optimal utility, power levels, and bandwidth allocations. ```python np.set_printoptions(precision=3) n = 5 # number of receivers in the system a_val = np.arange(10,n+10)/(1.0*n) # α b_val = np.arange(10,n+10)/(1.0*n) # β P_tot = 0.5 W_tot = 1.0 status, utility, power, bandwidth = optimal_power(n, a_val, b_val, P_tot, W_tot) print('Status: {}'.format(status)) print('Optimal utility value = {:.4g}'.format(utility)) print('Optimal power level:\n{}'.format(power)) print('Optimal bandwidth:\n{}'.format(bandwidth)) ``` ```text Status: optimal Optimal utility value = 2.451 Optimal power level: [1.151e-09 1.708e-09 2.756e-09 5.788e-09 5.000e-01] Optimal bandwidth: [3.091e-09 3.955e-09 5.908e-09 1.193e-08 1.000e+00] ``` -------------------------------- ### List installed solvers Source: https://www.cvxpy.org/tutorial/solvers/index.html Shows how to use the `installed_solvers` utility function to get a list of solvers supported by the current CVXPY installation. ```python print(installed_solvers()) ``` -------------------------------- ### Example Output Source: https://www.cvxpy.org/examples/applications/optimal_power_gaussian_channel_BV4.62.html?q= The output of the example usage, showing the status of the optimization, the optimal utility value, and the optimal power and bandwidth allocations for each channel. ```text Status: optimal Optimal utility value = 2.451 Optimal power level: [1.151e-09 1.708e-09 2.756e-09 5.788e-09 5.000e-01] Optimal bandwidth: [3.091e-09 3.955e-09 5.908e-09 1.193e-08 1.000e+00] ``` -------------------------------- ### Getting Problem Data for SCS Solver Source: https://www.cvxpy.org/tutorial/advanced/index.html?q= Example of retrieving low-level problem data and using the SolvingChain to solve and unpack results. ```python problem = cp.Problem(objective, constraints) data, chain, inverse_data = problem.get_problem_data(cp.SCS) # calls SCS using `data` soln = chain.solve_via_data(problem, data) # unpacks the solution returned by SCS into `problem` problem.unpack_results(soln, chain, inverse_data) ``` -------------------------------- ### Example Usage Source: https://www.cvxpy.org/examples/applications/optimal_power_gaussian_channel_BV4.62.html?q= An example demonstrating how to use the `optimal_power` function with specific parameters for 5 channels, alpha and beta values, and total power and bandwidth. It prints the status, optimal utility, power levels, and bandwidth. ```python np.set_printoptions(precision=3) n = 5 # number of receivers in the system a_val = np.arange(10,n+10)/(1.0*n) # α b_val = np.arange(10,n+10)/(1.0*n) # β P_tot = 0.5 W_tot = 1.0 status, utility, power, bandwidth = optimal_power(n, a_val, b_val, P_tot, W_tot) print('Status: {}'.format(status)) print('Optimal utility value = {:.4g}'.format(utility)) print('Optimal power level:\n{}'.format(power)) print('Optimal bandwidth:\n{}'.format(bandwidth)) ``` -------------------------------- ### Example Source: https://www.cvxpy.org/examples/applications/min_condition_number_by_scaling.html?q= This code snippet demonstrates how to solve the generalized eigenvalue problem (GEVP) for minimizing the condition number of a matrix using CVXPY. It includes helper functions for condition number evaluation, matrix setup, variable definition, constraint formulation, and problem solving. ```python # import packages import cvxpy as cp import numpy as np # create helper functions def cond(A): return np.linalg.cond(A) def evalCond(M,Q,P): L = np.diag(np.diag(P.value)**(1/2)) R = np.diag(np.diag(Q.value)**(-1/2)) Mnew = L @ M @ R return np.linalg.cond(Mnew) # create a random matrix m = 3 n = 2 np.random.seed(2) M = np.random.rand(m,n) # specify the variables p = cp.Variable(m,pos=True) P = cp.diag(p) q = cp.Variable(n,pos=True) Q = cp.diag(q) # define the variables for GEVP A = M.T @ P @ M B = Q C = A - Q # create the constraints and objective ep = 1e-3 constr = [C >= ep*np.eye(C.shape[0]), P >= ep*np.eye(P.shape[0]), Q >= ep*np.eye(Q.shape[0])] # note: the variable lambda = gamma^2 from the problem statement objFun = cp.Minimize(cp.gen_lambda_max(A,B)) # create the problem problem = cp.Problem(objFun,constr) # check if DQCP print("Is the problem DQCP? ",problem.is_dqcp()) # solve problem.solve(qcp=True,solver="SCS") # print results if problem.status not in ["infeasible", "unbounded"]: print("Initial Condition Number: ",cond(M)) print("Optimized Condition Number: ",evalCond(M,Q,P)) else: print(problem.status) ``` -------------------------------- ### Custom QP Solver Example Source: https://www.cvxpy.org/tutorial/solvers/index.html An example demonstrating how to implement and use a custom QP solver by subclassing OSQP and overriding the solve_via_data method. ```python import cvxpy as cp from cvxpy.reductions.solvers.qp_solvers.osqp_qpif import OSQP class CUSTOM_OSQP(OSQP): MIP_CAPABLE=False def name(self): return "CUSTOM_OSQP" def solve_via_data(self, *args, **kwargs): print("Solving with a custom QP solver!") super().solve_via_data(*args, **kwargs) x = cp.Variable() quadratic = cp.square(x) problem = cp.Problem(cp.Minimize(quadratic)) problem.solve(solver=CUSTOM_OSQP()) ``` -------------------------------- ### Install KNITRO Source: https://www.cvxpy.org/install/index.html Install the KNITRO solver for CVXPY. ```bash pip install knitro ``` -------------------------------- ### CVXPY Problem Setup Source: https://www.cvxpy.org/examples/applications/optimal_power_gaussian_channel_BV4.62.html?q= Imports necessary libraries and defines the CVXPY problem for optimal power and bandwidth allocation. ```python #!/usr/bin/env python3 # @author: R. Gowers, S. Al-Izzi, T. Pollington, R. Hill & K. Briggs import numpy as np import cvxpy as cp ``` -------------------------------- ### Install CVXPY from conda-forge Source: https://www.cvxpy.org/install/index.html Install CVXPY from the conda-forge channel. ```bash conda install -c conda-forge cvxpy ``` -------------------------------- ### Import and setup packages Source: https://www.cvxpy.org/examples/applications/clock_mesh.html Imports necessary libraries (cvxpy, numpy, scipy, matplotlib) and sets up plotting properties for LaTeX rendering. ```python import cvxpy as cp import numpy as np import scipy as scipy import matplotlib.pyplot as plt # Show plots inline in ipython. %matplotlib inline # Plot properties. plt.rc('text', usetex=True) plt.rc('font', family='serif') font = {'weight' : 'normal', 'size' : 16} plt.rc('font', **font) ``` -------------------------------- ### Install CVXPY from source (editable) Source: https://www.cvxpy.org/install/index.html Install CVXPY from source with editable mode. ```bash pip install -e . ``` -------------------------------- ### Install and set up pre-commit Source: https://www.cvxpy.org/contributing/index.html?q= Commands to install pre-commit to automatically check coding conventions before each commit. ```bash pip install pre-commit pre-commit install ``` -------------------------------- ### einsum examples Source: https://www.cvxpy.org/api_reference/cvxpy.atoms.affine.html?q= Examples demonstrating the use of the einsum atom for matrix multiplication and trace calculation. ```python import cvxpy as cp A = cp.Variable((3, 4)) B = cp.Variable((4, 5)) # Matrix multiplication result = cp.einsum('ij,jk->ik', A, B) # Trace of a matrix C = cp.Variable((3, 3)) trace = cp.einsum('ii->', C) ``` -------------------------------- ### Selecting a canonicalization backend Source: https://www.cvxpy.org/tutorial/performance/index.html?q= Example of how to select a canonicalization backend when solving a problem. ```python prob.solve(canon_backend=cp.SCIPY_CANON_BACKEND) ``` -------------------------------- ### Install HiGHS Python Interface Source: https://www.cvxpy.org/install/index.html?q= Command to install the HiGHS Python interface for CVXPY. ```bash pip install highspy ``` -------------------------------- ### Install cvxpy-base using conda Source: https://www.cvxpy.org/install/index.html Install CVXPY without default solver dependencies using conda. ```bash conda install cvxpy-base ``` -------------------------------- ### Using cp.quad_form with sparse matrices Source: https://www.cvxpy.org/tutorial/performance/index.html?q= Demonstrates how to use cp.quad_form with sparse matrices and provides alternatives for diagonal matrices. ```python cp.quad_form(x, P) ``` ```python cp.quad_form(x, scipy.sparse.diags_array(P_diag)) ``` ```python cp.sum(cp.multiply(P_diag, cp.square(x))) ``` -------------------------------- ### Install cvxpy-base using pip Source: https://www.cvxpy.org/install/index.html Install CVXPY without default solver dependencies using pip. ```bash pip install cvxpy-base ``` -------------------------------- ### Stacking example Source: https://www.cvxpy.org/_modules/cvxpy/atoms/affine/stack.html Demonstrates how to stack CVXPY Parameters along a new axis. ```python import cvxpy as cp a = cp.Parameter((3,)) b = cp.Parameter((3,)) y = cp.stack([a, b], axis=0) y.shape z = cp.stack([a, b], axis=-1) z.shape ```