CVXPY library to optimise utility function

103 views Asked by At

I am trying to maximise the utility function with two constraints. But I am not sure where I need to fix the Code. I have tried making the dimensions of the matrices same and specified in the code

I have defined the global parameters I have specified the constraints

theta1 = 1
theta2 = 0.1
big_gamma1 = 1
big_gamma2= 0.5
gamma1 = 0.5
gamma2 = 0.5
z2 = 0.7*np.ones(5)
z1 = np.linspace(0.01, 4, 5)
xi_1 = 1/z1
xi1 = xi_1.transpose
print(z1)

z2 = 0.07*np.ones((1,5))
print(z2)
z1 = np.linspace(0.01, 4, 5)
xi_1 = 1/z1
xi1 = xi_1.transpose

r1 = cp.Variable((5,1))
r2 = cp.Variable((5,1))
f1 = cp.Variable((1,5))
f2 = cp.Variable((1,5))

constraints = [f1 + f2 ==1,
              f1@r1 + f2@r2 == 1]

obj = cp.Problem(cp.Minimize((f1@r1 + f2@xi1)@z1 + f2@(r2 - xi1)@(z2)), [f1 + f2 ==1,
              f1*r1 + f2*r2 == 1])
print("obj is DCP:", obj.is_dcp())

Error in the code.

[[0.07 0.07 0.07 0.07 0.07]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-b52595a9b4b9> in <module>
     13               f1@r1 + f2@r2 == 1]
     14 
---> 15 obj = cp.Problem(cp.Minimize((f1@r1 + f2@xi1)@z1 + f2@(r2 - xi1)@(z2)), [f1 + f2 ==1,
     16               f1*r1 + f2*r2 == 1])
     17 print("obj is DCP:", obj.is_dcp())

~/opt/anaconda3/lib/python3.7/site-packages/cvxpy/expressions/expression.py in cast_op(self, other)
     44         """A wrapped binary operator that can handle non-Expression arguments.
     45         """
---> 46         other = self.cast_to_const(other)
     47         return binary_op(self, other)
     48     return cast_op

~/opt/anaconda3/lib/python3.7/site-packages/cvxpy/expressions/expression.py in cast_to_const(expr)
    465                         "Combine Expressions using atoms such as bmat, hstack, and vstack."
    466                     )
--> 467         return expr if isinstance(expr, Expression) else cvxtypes.constant()(expr)
    468 
    469     @staticmethod

~/opt/anaconda3/lib/python3.7/site-packages/cvxpy/expressions/constants/constant.py in __init__(self, value)
     43             self._sparse = True
     44         else:
---> 45             self._value = intf.DEFAULT_INTF.const_to_matrix(value)
     46             self._sparse = False
     47         self._imag = None

~/opt/anaconda3/lib/python3.7/site-packages/cvxpy/interface/numpy_interface/ndarray_interface.py in const_to_matrix(self, value, convert_scalars)
     48             return result
     49         else:
---> 50             return result.astype(numpy.float64)
     51 
     52     # Return an identity matrix.

TypeError: float() argument must be a string or a number, not 'builtin_function_or_method'
0

There are 0 answers