Questions tagged [cvxpy]
CVXPY is a Python-embedded modeling language for convex optimization problems inspired by the MATLAB package CVX.
623
questions
-1
votes
0
answers
22
views
ValueError: Cannot broadcast dimensions (3,) (3, 1)
import cvxpy as cvx`
import numpy as np
Q = np.matrix('1 -0.5; -0.5 2')`
f = np.matrix('-1; 0')`
A = np.matrix('1 2; 1 -4; 5 76')`
b = np.matrix('-2; -3; 1')`
x = cvx.Variable(2)`
obj = cvx.quad_form(...
0
votes
0
answers
22
views
How to implement non-zero count constraint for cvxpy in integer programming
I want to implement the constraint that the number of non-zero elements per row in x is less than 2.
constraint2 = cp.sum( x >= 0, 1) <= 2
but get the error TypeError: float() argument must be ...
0
votes
0
answers
33
views
shortest path from node A to node B in larger graphs using matrices in cvxpy library is always 0
So i've been using cvxpy recently for minimizing the route between two nodes in a graph and it has worked just fine. However, i've been facing issues with a 48 node graph, and i'm wondering if cvxpy ...
0
votes
0
answers
88
views
SVM implementation problem with CVXPY: Persistent errors with RBF kernel and linear kernel
I'm working on an SVM (Support Vector Machine) implementation using CVXPY in Python. Unfortunately, I'm having persistent problems with both the RBF kernel and the linear kernel.
I define the ...
-1
votes
0
answers
17
views
use cvxpy to optimize on an existing portfolio by limiting the number of trades to make
Sample code below to try to use at most 2 trades to optimize the portfolio, but I don't know how to express the constraint on the number of trades made to get the optimized portfolio.
What i tried ...
0
votes
1
answer
28
views
How to change problem parameter in cvxpy without recompiling?
I have a SDP problem that takes a couple MOSEK solver seconds to run but a couple minutes to compile in cvxpy. I hope to be able to change parameters and solve again without having to recompile, and ...
1
vote
1
answer
53
views
Solving non-convex optimization problem using python
I need to maximize a two-variable non-convex function, which contains a log to the base 2.
I tried CVXPY but an error appears:
cvxpy.error.DCPError: Problem does not follow DCP rules.
The function is ...
0
votes
0
answers
23
views
why does summing in cvxpy will occur broadcasting error
I having a problem of value error with my code. It keeps showing cannot broadcast dimensions (5,)(5,3). I am still learning how to use cvxpy so I cannot find the way to solve it. The problem is at &...
0
votes
0
answers
36
views
Optimizing a diagonal elements of a matrix using cvxpy
If I have a diagonal matrix, its main diagonal elements are e**(1jx) where x is the optimizable variable between 0 and 2pi, something like this: Matrix_of_X = np.diag(e**(1j*x))
How to use the CVX ...
0
votes
1
answer
77
views
Problem with installing cvxpy using pip: "Command '['cmake', '--version']' returned non-zero exit status 1" (Windows)
When I run "pip install cvxpy"
I get the following error:
Collecting osqp>=0.6.2 (from cvxpy)
Using cached osqp-0.6.3.tar.gz (228 kB)
Installing build dependencies ... error
error: ...
0
votes
0
answers
24
views
SDP Problem: No Correct Solutions Obtained using cvxpy in Python
I'm working on an optimization problem using cvxpy in Python. The optimization problem is based on a model presented in a IEEE Transactions on Automatic Control journal article:On Maximizing the ...
0
votes
0
answers
9
views
"cvxpy" library: how to perform convex optimization for real and imaginary part of variable simultaneously - python
I have a set of variables in python. I wish to minimize total variation of real and imaginary part of my variables. Here is part of my code:
import cvxpy as cp
delta = 10
# Define the variables
s_L1 =...
1
vote
0
answers
91
views
Failed cvxpy instllation -> failed building qdldl wheel -> ... -> zsh: command not found: xcode-select
I think I might scream. I'm on a Macbook Pro M1 Max 32 GB on Sonoma 14.0. Python 3.11.2 in Visual Studio.
I have been trying to pip install cvxpy for an assignment for my grad school optimization ...
0
votes
1
answer
22
views
Difficulty formulating optimisation problem according to DCP rules cvxpy
I'm trying to calculate the position of a target over time according to relative range and angle measurements from static anchors. For that I'm trying to formalise the problem with the same ...
0
votes
1
answer
24
views
Installing pyscipopt introduces bug in numpy
I've noticed the following issue after installing pyscipopt using conda across multiple python versions (3.7-3.11).
Essentially multiplying large matrices causes python to crash, empirically when the ...
0
votes
0
answers
48
views
CVXPY finds no solution (unbounded) to a problem that definitely has a solution - how to debug
I encountered the following special case where CVXPY was returning a None solution, with status unbounded, for a problem that definitely has a solution.
It doesn't seem like I can enter LaTeX, so @ ...
0
votes
1
answer
17
views
DCPError: Problem does not follow DCP rules - TV minimization using CVXPY library
I want to solve a compressed sensing problem which I wish to minimize total variation(TV) of my object vector. Here is representation of my code:
import numpy as np
import matplotlib.pyplot as plt
...
0
votes
0
answers
34
views
CVXPY: Why Norm constraint is not DCP?
cp.norm(weights, 1).is_dcp() returns true. Then why this code works:
import numpy as np
import cvxpy as cp
inputs = np.random.normal(0, 1, (100, 300))
inputs_mean = inputs.mean(axis=1) # shape (...
0
votes
0
answers
64
views
Why does construction of optimization last too long using CVXPY?
I have set up a simple linear MPC problem using python's CVXPY. The optimization problem is solved with the ECOS solver and is pretty fast. The problem is that the formulation of the optimization ...
1
vote
0
answers
51
views
How to figure out cause of SolverError in CVXPY using Gurobi
I'm running into issues on CVXPY. I used MOSEK and the exact same problem seems to solve to a solution. If I try to use GUROBI in the solve() method, it gives me a SolverError telling me to set ...
3
votes
2
answers
141
views
Cannot solve Ax=b for A using python if there is no unique solution
I am trying to solve a linear system of equations of the form Ax=b where A is a square matrix which is unknown, while the vectors x and b are known. I am interested in a physics problem where my A is ...
0
votes
0
answers
70
views
Mosek not playing nice with CVXPY and joblib
Getting some weird behavior with MOSEK, CVXPY and joblib. Trying to parallelize some jobs in Python (without even calling Mosek) and getting errors like
terminate called after throwing an instance of '...
0
votes
1
answer
50
views
How to to get the sum of the smallest N nonzero elements in a solution using cvxpy?
I'm using cvxpy to solve a mixed integer problem using MOSEK. It has a constraint on the sum of the smallest N entries that are nonzero.
I'd like to try to get something like
cvxpy.sum_smallest((A @ x)...
0
votes
0
answers
35
views
MILP variable takes only positive values
I am trying to formulate a battery dispatch strategy whereby the battery only charges when there is excess energy left after self consumption.
PV_SIZE, PV_Generation are decision variables, and ...
0
votes
0
answers
27
views
Matrix Multiplication in cvxpy
I want to include a constraint in cvxpy where 2 consecutive matrix multiplications need to be done. However it keeps saying I have a DCP error. There seems to be something wrong with the way I am ...
1
vote
1
answer
81
views
Minimizing 1/x such that x>=1 and x<=10 using CVXPY
I am trying to solve the following problem using CVXPY:
# Define the variable
x = cp.Variable()
# Define the objective function
objective = cp.Minimize(1/x)
# Define the constraints
constraints = [x ...
0
votes
0
answers
55
views
CVXOPT constraint-related error: Rank(A) < p or Rank([P; A; G]) < n
I want to run the GRAM algorithm from MKLpy on a list of dataframes mrna_df, methylation_df, and protein_df. MKLpy uses cvxopt under the hood.
GRAM code from MKLpy:
from . import Solution, Cache, ...
0
votes
1
answer
186
views
Use CVXPY to solve convex problem but got optimal-inaccurate result
When I use the cvxpy to solve the convex problem below,I get a confusing result, the source code is written like below:
import numpy as np
import cvxpy as cp
eta = 10e-27
# CVXPY to solve convex ...
1
vote
0
answers
50
views
segmenation fault when running cvxpy in modelica buildings python module
I am not sure if this is a CVXPy, modelica, python, or maybe even a C problem (I believe CVXPy and/or the SCS solver calls functions written in C). I am simulating a microgrid using openmodelica that ...
0
votes
1
answer
118
views
cvxpy/mosek improving the precision of the solution
Currently my objective function gives me a solution in the magnitude of about 1e-6. I think that gives me some precision problem.
What's the best practice to handle this kind of problems? It seems ...
0
votes
0
answers
78
views
Cvxpylayers throws an exception: "Problem must be DPP"
I have the following optimization layer that I use as output layer in my Pytorch model:
class OptimizerLayer(torch.nn.Module):
"""
Optimizer layer.
"""
def __init__(self, ...
2
votes
0
answers
230
views
CVXPY shows warning "Objective contains too many subexpressions" and takes a long time to compile the problem
I have tried to solve a simple unconstrained quadratic problem with CVXPY, which solves the problem as expected, as long as the matrix size is small enough. The following code shows a simplified ...
0
votes
0
answers
44
views
failed to build scs when installing cvxpy
trying to install cvxpy
pip3 install cvxpy
I get the following error message:
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for ...
0
votes
1
answer
63
views
How can I access the cost at each iteration when using the solve function in the cvxpy library?
If you want to solve a convex optimization problem using the cvxpy library, only the last iteration cost can be calculated and returned, while other intermediate costs and related values are printed ...
0
votes
1
answer
59
views
Why is "pcost" or "dcost" increasing in the minimization problem when using cvxpy?
I have an optimization problem which I modeled using the cvxpy library. However, when I run the solve method, I notice that the cost value increases after each iteration, even though the problem is ...
0
votes
0
answers
52
views
CVXPY complaining the problem is not DQCP (LTI decay rate)
I am following following this tutorial on the YALMIP to find the maximum decay rate for an LTI CT system, but trying to implement it in Python using the CVXPY package. The dynamics of the matrix A are ...
0
votes
0
answers
38
views
Contraining a number of assets to be less than or equal to using cvxpy
I am trying to use cvxpy to minimize the tracking error, defined as the standard deviation of the portfolio return minus the index return, with a few constraints including
the sum of weights of all ...
0
votes
1
answer
78
views
Error when forming a block matrix in CVXPY
I am trying to solve an optimization problem using CVXPY where I need to form a block matrix as a constraint. The block matrix is given by:
$$M = \begin{bmatrix} Q & \mathbf{b} \ \mathbf{b}^T &...
0
votes
0
answers
30
views
How to use cvxpy for minimizing 3D model coordinates
enter image description here
I'm trying to solve a convex objective function problem by using the cvxpy function in Python.
X = cp.Variable((4,1))
S = cp.Variable(1)
constraints = [X[-1] ==...
0
votes
0
answers
40
views
Issue with python cvxpy when trying to get the number of positive variables
I have a python dataframe with one column called "STOCK", another column called "WEIGHT", another column called "EXPECTED_RETURN", and another column called "SOURCE&...
0
votes
1
answer
82
views
How to vectorize a matrix variable in CVXPY?
I can create a symmetric matrix variable in CVXPY with X = cp.Variable((size_n, size_n), symmetric=True). Is there a way to vectorize the matrix so that I can obtain a size_n*size_n by 1 vector?
I ...
0
votes
0
answers
25
views
geometric program, function with sum of logs has unknown curvature
I am trying to solve an optimiztation problem from the paper "A Coded Compressed Sensing Scheme for Unsourced Multiple Access" with cvxpy. According to the paper the program is a geometric ...
0
votes
0
answers
72
views
Error when using CVXPY: TypeError: float() argument must be a string or a number, not 'multiply'
I am trying to use CVXPY to solve an optimization problem. Here is the code:
N = 10
threshold = 0.3
p = 1 - threshold
para_dim = 1000
para_mat = np.random.rand(para_dim, N)
para_avg = np.expand_dims(...
0
votes
1
answer
70
views
How to set a constraint where two optimization variables are not non-zero at the same time?
I have optimization variables x1 and x2. Each have different costs in the objective. I need a constraint to make x1 = 0 when x2 > 0 and x2 = 0 when x2 > 0. It's also okay to have x1 = x2 = 0. ...
0
votes
1
answer
91
views
Optimization problem with Integer variables and a squared objective
The following code works:
import cvxpy as cp
a = cp.Variable()
b = cp.Variable()
c = cp.Variable()
d = cp.Variable()
objective = cp.Minimize(cp.square(-2*a + 1.5*b + 3*c + 6.5*d - 2000))
...
0
votes
1
answer
105
views
"cvxpy" is not accessed Pylance
python==3.11.3
Macbook M1
I have successfully downloaded the module 'cvxpy', and I can import it in the terminal, but every time I try to import it in VsCode, it always tells 'ModuleNotFoundError: No ...
0
votes
1
answer
217
views
CVXPY claims matrix is not symmetric and PSD despite my matrix being symmetric and PSD
I have a quadratic program (see below) that causes an error due to my quad_form matrix being not symmetric or PSD. Why is this happening? (Note that the issue is with my matrix P below so feel free to ...
0
votes
0
answers
32
views
How to rewrite function to satisfy CVXPY ruleset (DCP error)?
I have a quadratic program that CVXPY rejects because of DCP error. However I am certain that my formulation (see code below) satisfies the quadratic program (I'm trying to match this). I suspect it ...
1
vote
0
answers
118
views
How do I avoid NaNs from CVXPY when optimizing L1 norm of entries of a 100x100 matrix?
I am trying to run an optimization using CVXPY for a large problem. I'm trying to optimize the L1 norm of some of the entries of a $100\times 100$ matrix, under constraints that restrict the set of ...
0
votes
0
answers
50
views
Trouble with the constraints, optimizing a shortest path exercise with CVXPY
I'm a newbie in coding and I'm taking an operations research course. We're using cvxpy library to solve some exercises. I was told to create one by my own and I'm trying to optimize a shortest path ...