0

i am new to CVXPY and would like to do some optimization,

this is my code:

from numpy.linalg import pinv
AA=pinv(A)  #m*n
AAT=AA.T    #n*m
i1=np.transpose(np.matrix([1,0])) #m*1
i2=np.transpose(np.matrix([0,1])) #m*1

from cvxpy import *
import numpy as np
from numpy import *

#construct the problem
x=Variable(1,5) #1*n
wx=np.matrix([x[0],x[1],x[2],x[3],x[4]])

#consraints
constraints = [x[0]+x[1]+x[2]+x[3]+x[4]==1]
for i in range(5):
constraints += [
    x[i] <= 1,
    x[i] >= 0,
    ]
Q=wx*A*i1*wx*a*AAT*i1
P=wx*A*i2*wx*a*AAT*i2
objective =  Minimize(Q-P)
result=prob.solve()
print(x.value)

where

wx is  1 by n matrix (the variable matrix, unknown, to be solved)
A is n by m (known),i1 is m by 1 (known),i2 is m by 1 (known)
a is n by n (known), AAT is n by m (known)

I have A(n by m),and a(n by n):

A=[[-4.10272297 -1.94100278]
[-0.07551063  0.00533883]
[-0.27742026 -0.17370814]
[ 0.07785536 -1.02386256]
[ 0.77757854  0.04405759]]

a=[[ 80.81155556   0.82893333   2.87077778   2.97862222 -18.59855556]
[  0.82893333   0.01547111   0.04784444  -0.05957111  -0.09624444]
[  2.87077778   0.04784444   0.20449444  -0.00278333  -0.3157    ]
[  2.97862222  -0.05957111  -0.00278333   4.68989889  -3.28908889]
[-18.59855556  -0.09624444  -0.3157      -3.28908889   9.14991111]]

BUT i get the following error:

TypeError: <class 'cvxpy.atoms.affine.add_expr.AddExpression'> is not a valid type for a Constant value.

it seems I am not solving a convex problem, so the CVXPY doesn't work How can I handle this?

I am new to CVXPY so please be specific!Thanks!

Stanley
  • 11
  • 2
  • (1) This code is incomplete and can't be reproduced. Furthermore your usage of cvxpy is strange. You should not need all those dots. (2) cvxpy automatically behaves like scipy.sparse matrices, meaning ```wx*a``` is enough. (3) If the final error is because of shapes or your operations itself is hard to debug giving this code. (4) If your model is non-convex, you can't formulate it within cvxpy! – sascha Dec 08 '17 at 14:26
  • I have clarified the problem and made sure that the shapes of matrix aren't the reason for my error, but it still doesn't work... – Stanley Dec 14 '17 at 02:24
  • What's so hard about editing that code into something which can be copy-pasted? Now you expect us to copy-paste the A-array and manually / or tool-supported insert commas to make it syntactically valid code and combine this with the other code-part (as np-array creation in code looks different than a print)? You also do not state which cvxpy version you are using (which actually can make a difference). – sascha Dec 14 '17 at 02:34

1 Answers1

0

other people having your problem seem to be getting it because of the array casting. change all your data types to numpy, see if it solves anything

Danii-Sh
  • 447
  • 1
  • 4
  • 18
  • It seems that i have already set my data types as numpy, but it still doesn't work... Is there any other solution? thx – Stanley Dec 14 '17 at 02:21