pythontypeerrorcvxpyconvex-optimizationconvex

TypeError: tri() missing 1 required positional argument: 'N'


I can't get past the error:

> ---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-4-3e1d3e33fb00> in <module>()
      7 d_unpair = np.random.beta(n,n)
      8 d_pair = np.random.beta(2,2,size=n)*0.20
----> 9 d = np.tril(d_unpair)+np.tril(d_unpair, -1).T-np.diag(d_unpair)*delta+d_pair*delta
     10 G=np.zeros((n,n))
     11 g = A/d**3.5

<__array_function__ internals> in tril(*args, **kwargs)

/usr/local/lib/python3.7/dist-packages/numpy/lib/twodim_base.py in tril(m, k)
    432     """
    433     m = asanyarray(m)
--> 434     mask = tri(*m.shape[-2:], k=k, dtype=bool)
    435 
    436     return where(mask, m, zeros(1, m.dtype))

TypeError: tri() missing 1 required positional argument: 'N'

This was for a research purpose and I typed it exactly as it was in this research paper. (Hyperlinked). [The second one to be precise.]. The first code worked fine but I just can't get pass this error in the second code.

The code

import cvxpy as cvx
import numpy as np
np.random.seed(100)
n = 3 #number of transmitter = receiver
A = 0.025 #uniform receiver coefficient
delta = np.identity(n) #identity matrix
d_unpair = np.random.beta(n,n)
d_pair = np.random.beta(2,2,size=n)*0.20
d = np.tril(d_unpair)+np.tril(d_unpair, -1).T-np.diag(d_unpair)*delta+d_pair*delta
G=np.zeros((n,n))
g = A/d**3.5
S_hat=G*delta
I_hat = G-S_hat
σ = 5.0*np.ones(n)
γ = 1.0
Pmax = 1.0
p=cvx.Variable(n)
obj = cvx.Minimize(cvx.sum(p))
constraints = [p>=0, S_hat*p-γ*(I_hat*p+σ)>= 0, p <=Pmax]
prob=cvx.Problem(obj, constraints)
prob.solve()
powers = np.asarray(p.value)
print('Solution status ={0}'.format(prob.status))
print('Optimal solution ={0:.3f}'.format(prob.value))
if prob.status == 'optimal':
  for j in range(n):
    print('Power{0}={1:.3f}'.format(j,powers[j]))

I never worked with the CVXPY module before so I looked up few sample questions here in stack overflow but none of them matched my problem, almost all of them were referring to "TypeError: Missing 1 required positional argument: 'self'"

How can I fix this so it gives a proper output?


Solution

  • Add size=1 to the call to numpy.random.beta where d_unpair is assigned:

    d_unpair = np.random.beta(n, n, size=1)
    

    The error happens because numpy.random.beta has default size=None which makes the method return a scalar value (a float in this case), which in turn is a bad argument to numpy.tril. Setting size=1 makes numpy.random.beta return a ndarray.