New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
N-dimensional variables #198
Comments
Hey Steven! I'm trying to translate an optimization problem I've written in matlab cvx into python cvxpy. import numpy as np create optimization variables
I'm aware of the fact that 1D array transposed returns itself, but I haven't been able to find a way around it. |
Hi Marzia, You can't ever use numpy functions on cvxpy objects. You have to use the cvxpy functions:
|
@SteveDiamond it seems that adding support for this is pretty straightforward in 1.0.
When I removed these restrictions (on my fork) N-d Expressions worked with every atom I tested (all elementwise atoms, some affine atoms, some non-affine atoms). N-d Variables also behaved correctly, in the ways that I tested them. |
That's great! I guess we did a better job than I thought with the refactor. We will need to add checks to certain operations like matrix multiplication, but it could be enough to simply ban N>2 in all relevant locations. We may not need any special case handling for N>2. |
Update: we should check to make sure that |
Hi Steve, I am new to cvxpy and python, I have created a three dimensional matrix using your suggestion. Create a 3D k-by-m-by-n variable.x = {} I have problem slicing the 3D matrix. Like how to extract x(1, :, 1) (matlab notation) using python commands |
You can't slice it. That's one advantage of truly supporting ND matrices. |
Sorry, in your specific example you would do |
I am running into errors if I do what you suggested import cvxpy N_sch=2 x = {} for i in range(N_perhour): print(x[0][:,0]) File "", line 1, in File "C:\Users\vchinde\AppData\Local\Continuum\anaconda2\lib\site-packages\cvxpy\expressions\expression.py", line 315, in getitem File "C:\Users\vchinde\AppData\Local\Continuum\anaconda2\lib\site-packages\cvxpy\atoms\affine\index.py", line 48, in init File "C:\Users\vchinde\AppData\Local\Continuum\anaconda2\lib\site-packages\cvxpy\utilities\key_utils.py", line 49, in validate_key IndexError: Too many indices for expression. |
It should be |
Thanks Steve. I got it |
Hi Steve, I have the following question m_z=cvxpy.Variable(shape=(24, 19)) The dimension of Simconst['m_min'] is (19L, 1L). When I run the code I get the following error "ValueError: Cannot broadcast dimensions (19, 1) (19,)". How to overcome this error? I did tried using m_i.T but has the same error |
Write |
cvxpy.Variable(dim1,dim2) some of the (dim1_index,dim2_index) combinations are meaning less how can we "do not define variables on those meaningless combinations" instead of "define variables on the whole combination, then use constraints to fix the meaningless variables " |
Another question: |
The simplest thing is to make a variable vector |
Will multidimensional variables ever be added to cvxpy? |
Not any time soon. |
had to turn to or-tools
Steven Diamond <notifications@github.com> 于2020年2月13日周四 上午10:26写道:
… Not any time soon.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://github.com/cvxgrp/cvxpy/issues/198?email_source=notifications&email_token=ABCNQ6OXXSWIGRNT5MLOMY3RCSVVNA5CNFSM4BJCB5JKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELTEWGY#issuecomment-585517851>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABCNQ6PLLAASOVH3NEW35ILRCSVVNANCNFSM4BJCB5JA>
.
--
===============================
Xuan
cx_2016@foxmail.com
|
@chenxuanhanhao thanks for letting us know. I'm curious: what's your application where the high-level logic offered by python makes it unrealistic to work only with 1d or 2d arrays? |
Thanks for cvxpy! With the uses that cvxpylayers is likely to see, I'm sure higher order tensors are going to be increasingly common (e.g. anything you wanted to do with a matrix before, you might want to do batched). In my current use case, I want to find a tensor that is close to another tensor (in the sense of kl_div) while respecting a number of constraints that deal with marginalizing out various subsets of the dimensions. (number of dimensions can be between 2 and 20.) Allowing cp.sum to work on higher-order ndarrays would satisfy my current use case (even if I am only allowed to sum over one dimension at a time). Alternatively, if just cp.transpose and cp.reshape allowed higher order ndarray, I'm sure that would facilitate easier work-arounds in many cases. |
You can transpose or reshape outside of cvxpy, in pytorch/tensorflow. |
Thanks; that's right, and I can do fancy indexing in order to sum out "dimensions" even if my representation in cvxpy is flat. In my case, the constraints I needed to add were easy to express as summations on subsets of dimensions, but difficult to write in terms of the flat representation. So, what I ended up doing was to make my own sum function that took in a flattened ndarray (i.e. transposing and flattening having already been applied outside of cvxpy), a shape tuple, and a set of dimensions to sum over and then (within my sum function) I did the indexing (via np.ravel_multi_index, np.ix_, ndarray.transpose, and ndarray.reshape) to achieve the equivalent of multi-dimensional summations on a flat cvxpy array. |
Would you mind explaining how exactly you removed the restrictions? |
@teichert would you mind sharing your code? I think might have a similar use case where I need to reshape and transpose a CVXPY variable (i.e. reshape into a 4-rank tensor, transpose 2 dimensions, then reshape back to a 2-rank tensor). |
Cristian— CVXPY’s partial_transpose atom might be useful for you here.
…On Sat, Jul 8, 2023 at 7:59 PM Cristian Emiliano Godinez Ramirez < ***@***.***> wrote:
@teichert <https://github.com/teichert> would you mind sharing your code?
I think might have a similar use case where I need to reshape and transpose
a CVXPY variable (i.e. reshape into a 4-rank tensor, transpose 2
dimensions, then reshape back to a 2-rank tensor).
—
Reply to this email directly, view it on GitHub
<#198 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACRLIFAK5AXJTYTNCMS4AH3XPHX4VANCNFSM4BJCB5JA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
thank you for your fast response, @rileyjmurray! I've seen the function, but I should have mention that my two legs correspond to "two different subsystems" as:
and as I understand |
@EmilianoG-byte thanks for the clarification. Can you open a separate GitHub issue requesting this functionality? |
@rileyjmurray Just opened the issue :). Here: #2181 |
Thanks so much! I can’t promise quick progress on it, but that will help a
lot in keeping track of your request.
…On Wed, Jul 12, 2023 at 1:55 PM Cristian Emiliano Godinez Ramirez < ***@***.***> wrote:
@rileyjmurray <https://github.com/rileyjmurray> Just opened the issue :)
—
Reply to this email directly, view it on GitHub
<#198 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACRLIFALIYZFLJLGTPWDMY3XP3QJNANCNFSM4BJCB5JA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@EmilianoG-byte sorry for the slow reply---I wasn't able to easily find the code, so I'm guessing that I was just tinkering when I did this. But it looks like you all are working on a better solution anyway. |
Hi @teichert! There is an open issue regarding this general transpose of two dimensions, but if you have already some sample code that can handle the partial transpose of multi dimensional arrays, I would be interested in taking a look at it :) |
@EmilianoG-byte okay; I did end up finding my code fwiw. I'm guessing it isn't relevant to your needs, but I'll add it here just in case. There might be more transposing capabilities in cvxpy now, but, as I recall, at the time of writing, I wanted to add constraints on ndarray variables regarding summing out various subsets of the dimensions, and the way I ended up doing it was by just having a flat variable representation and forming the ndsums based on that (using np.transpose before and after if necessary). No guarantees that this code makes any sense! :)
|
This issue seems to loss its focus from the title of N-dimensional variables Can we back to that or make new issue to track that? Re-upping @Xeunon question here.
We might be able to help to check and track list of functions that works in higher dimension and not, also make tests for that. CC: @SteveDiamond |
Currently cvxpy only supports scalar, vector, and matrix variables. Additional dimensions can be mimicked using a dict:
The text was updated successfully, but these errors were encountered: