def comp(theta, xx): resid_old = [] resid_new = [] A_old = np.zeros([T, M, NN], dtype=complex) A_new = np.zeros([T, M, NN], dtype=complex) theta_matrix = np.diag(theta) for t in range(T): # A[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] A_old[t] = Hd[t] A_new[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] # print(A_new[t].shape) y_old = np.zeros([T, M, 1], dtype=complex) y_new = np.zeros([T, M, 1], dtype=complex) for t in range(T): y_old[t] = np.dot(A_old[t], xx[t]) y_new[t] = np.dot(A_new[t], xx[t]) x_array_old, resid_array_old = pa.PIA_ASP(y_old, A_old, sp=0) x_array_new, resid_array_new = pa.PIA_ASP(y_new, A_new, sp=0) print('resid_old', resid_array_old[-1] / np.linalg.norm(A_old[t])) print('resid_new', resid_array_new[-1] / np.linalg.norm(A_new[t])) return resid_array_old[-1] / np.linalg.norm( A_old[t]), resid_array_new[-1] / np.linalg.norm(A_new[t])
def comp(theta, xx, num): A_old = np.zeros([T, M, NN], dtype=complex) A_new = np.zeros([T, M, NN], dtype=complex) theta_matrix = np.diag(theta) for t in range(T): # A[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] A_old[t] = Hd[t] # 这个应该是由theta_optimization 来决定的 A_new[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] A_new[t] = np.dot(np.dot(G[t, :, 0:num], theta_matrix), Hr[t, 0:num, :]) + Hd[t] y_old = np.zeros([T, M, 1], dtype=complex) y_new = np.zeros([T, M, 1], dtype=complex) for t in range(T): y_old[t] = np.dot(A_old[t], xx[t]) y_new[t] = np.dot(A_new[t], xx[t]) before = time.time() x_array_old, resid_array_old = pa.PIA_ASP(y_old, A_old, sp=50) after = time.time() print('old', after - before) before = time.time() x_array_new, resid_array_new = pa.PIA_ASP(y_new, A_new, sp=50) after = time.time() print('new', after - before) resid_old = resid_array_old[-1] resid_new = resid_array_new[-1] # print('resid_old', resid_array_old[-1] / np.linalg.norm(A_old[t])) # print('resid_new', resid_array_new[-1] / np.linalg.norm(A_new[t])) # print('resid_old', resid_array_old[-1] / np.linalg.norm(y_old[t])) # print('resid_new', resid_array_new[-1] / np.linalg.norm(y_new[t])) return resid_array_old[-1] / np.linalg.norm( y_old[t]), resid_array_new[-1] / np.linalg.norm(y_new[t])
def comp(theta, xx): A_old = np.zeros([T, M, NN], dtype=complex) A_new = np.zeros([T, M, NN], dtype=complex) theta_matrix = np.diag(theta) for t in range(T): # A[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] A_old[t] = Hd[t] # 这个应该是由theta_optimization 来决定的 A_new[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] A_new[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] # print(A_new[t].shape) y_old = np.zeros([T, M, 1], dtype=complex) y_new = np.zeros([T, M, 1], dtype=complex) for t in range(T): y_old[t] = np.dot(A_old[t], xx[t]) y_new[t] = np.dot(A_new[t], xx[t]) x_array_old, resid_array_old = pa.PIA_ASP(y_old, A_old, sp=0) x_array_new, resid_array_new = pa.PIA_ASP(y_new, A_new, sp=0) resid_old = resid_array_old[-1] resid_new = resid_array_new[-1] print('resid_old', resid_array_old[-1] / np.linalg.norm(A_old[t])) print('resid_new', resid_array_new[-1] / np.linalg.norm(A_new[t]), end='\t')
def optimize(n, x, t, the, theta_w, theta_record): min_sum = 999 min_theta = 0 theta_matrix = np.zeros([N, N], dtype=complex) for i in range(N): theta_matrix[i][i] = math.cos(the[i]) + 1j * math.sin(the[i]) for th in theta_array: theta_matrix[n][n] = th A = np.zeros([T, M, NN], dtype=complex) for t in range(T): A[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] # print(A.shape) y = np.zeros([T, M, 1], dtype=complex) for t in range(T): y[t] = np.dot(A[t], (x[t] + sigma[t])) np.savetxt('/Users/xfn/Downloads/cvx/projects/data/sigma.csv', sigma[0], delimiter=',') # print(y.shape) x_array, resid_array = pa.PIA_ASP(y, A, sp=0) # print(resid_array) sum_else = XL[n] * abs(th - theta_w[n]) + ( rho + L) / 2 * np.linalg.norm(th - theta_w[n]) # sum_else = XL[n] * (th - theta_w[n]) + (rho + L) / 2 * np.linalg.norm(th - theta_w[n]) # sum_else = 0 # print(resid_array[-1]) # print('sum_else', sum_else) if resid_array[-1] + sum_else < min_sum: # if sum_else < min_sum: min_sum = resid_array[-1] + sum_else # min_sum = sum_else min_theta = th # A = np.dot(np.dot(G, np.diag(theta)), Hr) + Hd # sumthetan = 0 # for i in range(M): # sumthetan += derivativeTheta(i, n, x, t) * derivativeTheta(i, n, x, t) # # sumthetan += XL[n] * (theta_wan - theta) + (rho + L) / 2 * np.linalg.norm(theta_wan - theta) return min_theta
def optimize(n, x, t, the, theta_w, theta_record, num): min_sum = 999 min_theta = 0 theta_matrix = np.zeros([num, num], dtype=complex) for i in range(num): theta_matrix[i][i] = math.cos(the[i]) + 1j * math.sin(the[i]) for th in theta_array: theta_matrix[n][n] = th A = np.zeros([T, M, NN], dtype=complex) for t in range(T): A[t] = np.dot(np.dot(G[t, :, 0:num], theta_matrix), Hr[t, 0:num, :]) + Hd[t] # print(A.shape) y = np.zeros([T, M, 1], dtype=complex) for t in range(T): y[t] = np.dot(A[t], x[t]) # print(y.shape) x_array, resid_array = pa.PIA_ASP(y, A, sp=50) # print(resid_array) sum_else = XL[n] * abs(th - theta_w[n]) + ( rho + L) / 2 * np.linalg.norm(th - theta_w[n]) # sum_else = XL[n] * (th - theta_w[n]) + (rho + L) / 2 * np.linalg.norm(th - theta_w[n]) # sum_else = 0 # print(resid_array[-1]) # print('sum_else', sum_else) if resid_array[-1] + sum_else < min_sum: # if sum_else < min_sum: min_sum = resid_array[-1] + sum_else # min_sum = sum_else min_theta = th # A = np.dot(np.dot(G, np.diag(theta)), Hr) + Hd # sumthetan = 0 # for i in range(M): # sumthetan += derivativeTheta(i, n, x, t) * derivativeTheta(i, n, x, t) # # sumthetan += XL[n] * (theta_wan - theta) + (rho + L) / 2 * np.linalg.norm(theta_wan - theta) return min_theta
KK = 20 Hr = np.random.uniform(low=1e-6, high=1e-4, size=[T, N, NN]) Hd = np.random.uniform(low=1e-13, high=1e-10, size=[T, M, NN]) G = np.random.uniform(low=1e-8, high=1e-6, size=[T, M, N]) x = np.load('x.npy') theta_opt = np.load('theta.npy') theta_matrix = np.diag(theta_opt) if __name__ == "__main__": # x = dataset.setRelativity(NN, T, K, KK) A_old = np.zeros([T, M, NN], dtype=complex) A_new = np.zeros([T, M, NN], dtype=complex) for t in range(T): # A[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] A_old[t] = Hd[t] #这个应该是由theta_optimization 来决定的 A_new[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] A_new[t] = np.dot(np.dot(G[t], theta_matrix), Hr[t]) + Hd[t] y_old = np.zeros([T, M, 1], dtype=complex) y_new = np.zeros([T, M, 1], dtype=complex) for t in range(T): y_old[t] = np.dot(A_old[t], x[t]) y_new[t] = np.dot(A_new[t], x[t]) x_array_old, resid_array_old = pa.PIA_ASP(y_old, A_old, sp=50) x_array_new, resid_array_new = pa.PIA_ASP(y_new, A_new, sp=50) print('resid_old', resid_array_old[-1]) print('resid_new', resid_array_new[-1])