Ball
December 10, 2023, 1:20pm
1
Dear all
I use cvx with mosek to address a single variable problem, the problem has log function and exp function. These are my codes:
clc;
clear;
rng(1);
epsilon = 1e-5;
Max_iter =5;
width_user_distribution = 30;
height_user_distribution = 30;
numUsers = 100;
[H1,H2,H3]=environment_generate(width_user_distribution,height_user_distribution,numUsers);
H1=db2pow(H1);
H2=db2pow(H2);
H3=db2pow(H3);
num_elements_x = 8;
num_elements_y = 8;
num_elements = num_elements_xnum_elements_y;
fc = 2.4e9; % Carrier frequency in Hz
c = 3e8; % Speed of light in m/s
lambda = 1;
phase_shifts = exp(1i rand(num_elements_x, num_elements_y) * 2 * pi);
IRS_matrix = diag(phase_shifts(:));
elements_binary = randi([0,1],1,64);
elements_selected_matrix = diag(elements_binary(1,:));
G = H2.'elements_selected_matrix IRS_matrixH3;
IRS_matrix_selected = elements_selected_matrix IRS_matrix;
iter = 0;
sum_rate = ;
sum_rate_old = 100;
r1=0.5;
r2=0.5;
P=50;
P2=db2pow(Pones(100,1))/2;
noise_power=-50;
noise_pow=db2pow(noise_power);
x=r2 log(1+P2.H1/noise_pow);
y=r1 log(1+P2.(H2’IRS_matrix_selected H3)‘);
(H2’IRS_matrix_selected H3))’)/(4 r1r2 2);
y_star=sqrt(x+y)/(4r1 r25);
cvx_begin quiet
cvx_solver Mosek
variable p(100,1)
a=r2 log(1+p.H1/noise_pow);
b=r1 log(real(1+p.(H2’IRS_matrix_selected H3)'));
d=real(sqrt(a+b));
maximize(sum(d-real(y_star.^2 4r1 r2*2)))
subject to
p>=0;
p<=db2pow(P);
cvx_end
In this code, H1, H2 and H3 are generated by the other code, specifically, H2 and H3 are complex vector. The code is expressed by;
width_user_distribution = 30; % 宽度(米)
height_user_distribution = 30; % 高度(米)
numUsers = 100; % 用户数量
x_user = rand(numUsers, 1) * width_user_distribution;
y_user = rand(numUsers, 1) * height_user_distribution;
z_user = zeros(100,1);
pos_user = [x_user,y_user,z_user];
H_UAV = 10; % Height of the UAV in meters
X_UAV = 10;
Y_UAV = 5;
pos_UAV = [X_UAV,Y_UAV,H_UAV];
X_IRS = 15;
Y_IRS = 15;
Z_IRS = 0;
pos_IRS = [X_IRS,Y_IRS,Z_IRS];
distance_Uu = sqrt(sum((pos_user - pos_UAV).^2, 2));
distance_UI = norm(pos_IRS-pos_UAV);
distance_Iu = sqrt(sum((pos_user - pos_IRS).^2, 2));
num_elements_x = 8;
num_elements_y = 8;
num_elements = num_elements_xnum_elements_y;
fc = 2.4e9; % Carrier frequency in Hz
c = 3e8; % Speed of light in m/s
lambda = 1;
beta0 = 0.01; %the reference path loss at distance is 1m -20dB
beta = 2; %the path loss factor
noise_power = -50; %-50dBm 10^-8hz
band = 1000000; %bandwidth 80KHZ
H_Uu=sqrt(beta0 (distance_Uu.^(-beta)));
vector_UI = zeros(num_elements,1);
for e = 0:num_elements-1
vectors1 = exp(-1ipi e2 lambda/lambda);
vector_UI(e+1) = vectors1;
end
H_UI = sqrt(beta0*(distance_UI).^(-2))vector_UI;
vector_Iu = zeros(64, 1);
H_Iu = zeros(64,numUsers);
for n = 1:numUsers
phi_Iu = (x_user(n, 1) - X_IRS) / distance_Iu(n, 1);
large_passloss = sqrt(beta0 * (distance_Iu(n, 1)).^(-2));
result_matrix = zeros(num_elements, 1);
for e = 0:num_elements-1
vectors2 = exp(-1i * pi * e * 2 * lambda * phi_Iu / lambda);
result_matrix(e + 1) = vectors2;
end
vector_Iu = large_passloss result_matrix;
H_Iu(:,n) = vector_Iu;
end
The first thing to do is to change units to improve numerical scaling so that all non-zero input data is within a small number of orders of magnitude of 1.
Then follow the advice in Debugging infeasible models - YALMIP , all of which except for section 1 also applies to CVX.
Ball
December 14, 2023, 1:53pm
3
Thanks for your advices and my problem is addressed. But now I face a new problem: because my optimized objective function has a exp term, an error is generated: Illegal operation: exp( {affine} ). Can you give me some advices?
I download Mosek toolbox, Mosek cannot address exp term, right? In addition, i find the top topic about the replacement of exp term in forum, but i cannot understand the replacement accurately, can you give me a simple example to introduce the replacement for me please?I will be thankful whether you help me or not.
That is the exponential of a complex number. nonlinear complex expressions are neither convex nor concave; hence not allowed by CVX.
Ball
December 14, 2023, 2:22pm
7
Thanks for your advices, but my finial objective function is like: sqrt(exp(li*x)), can you tell me is it a convex or concave problem please?
That makes no sense as an objective function, no matter what optimization tool or solver you use. An objective function must evaluate to a real scalar.
Ball
December 14, 2023, 2:59pm
9
Thank you and I understand. Can I ask another question? In cvx, if there is complex term, is there any ways to replace or transfer complex term?
I have no idea what you are asking. Can you try describing it more clearly, perhaps with an example?
Ball
December 14, 2023, 3:17pm
11
I mean, i assume there is a complex term in my cvx objective problem and thus cvx cannot address the problem directly, right? So is there any ways to replace the complex term in objective problem?
Such as transfer complex term as real term? I am not sure.
Sincerely
It’s your optimization problem, so you have to decide what objective function is appropriate (makes sense for your intended use).
Ball
December 14, 2023, 3:35pm
13
OK thanks for your help, I will check my problem.
Ball
December 15, 2023, 2:08am
14
I have a question, cvx cannot to address complex term, so what is the meaning of code “variable x(n) complex”?
Based on CVX guidence, i find the complex variable can be defined in cvx by “variable x(n) complex”. But, cvx is unenable to address complex term in objective function. Can you tell me how to use “variable x(n) complex”?
Ball
December 15, 2023, 2:09am
15
The error is still generated when i use “variable x(n) complex”.
Here is a made up example. The objective function evaluates to a real scalar, even though it is a function of a complex vector variable.
cvx_begin
variable x(4) complex
minimize(x'*x)
imag(x) >= 3
cvx_end
Calling Mosek 10.1.10: 14 variables, 5 equality constraints
MOSEK Version 10.1.20 (Build date: 2023-11-28 08:15:13)
Copyright (c) MOSEK ApS, Denmark WWW: mosek.com
Platform: Windows/64-X86
Problem
Name :
Objective sense : minimize
Type : CONIC (conic optimization problem)
Constraints : 5
Affine conic cons. : 0
Disjunctive cons. : 0
Cones : 1
Scalar variables : 14
Matrix variables : 0
Integer variables : 0
Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries : 2 time : 0.00
Lin. dep. - tries : 1 time : 0.00
Lin. dep. - primal attempts : 1 successes : 1
Lin. dep. - dual attempts : 0 successes : 0
Lin. dep. - primal deps. : 0 dual deps. : 0
Presolve terminated. Time: 0.00
Optimizer - threads : 8
Optimizer - solved problem : the primal
Optimizer - Constraints : 1
Optimizer - Cones : 1
Optimizer - Scalar variables : 3 conic : 3
Optimizer - Semi-definite variables: 0 scalarized : 0
Factor - setup time : 0.00
Factor - dense det. time : 0.00 GP order time : 0.00
Factor - nonzeros before factor : 1 after factor : 1
Factor - dense dim. : 0 flops : 9.00e+00
ITE PFEAS DFEAS GFEAS PRSTATUS POBJ DOBJ MU TIME
0 6.0e+00 1.0e+00 2.0e+00 0.00e+00 1.000000000e+00 0.000000000e+00 1.0e+00 0.00
1 6.8e-01 1.1e-01 5.3e-01 -8.73e-01 5.534285522e+00 9.531909383e+00 1.1e-01 0.01
2 1.2e-01 2.1e-02 1.3e-01 -4.53e-01 1.665036004e+01 2.538810460e+01 2.1e-02 0.01
3 2.8e-02 4.7e-03 2.0e-02 1.78e-01 2.906322927e+01 3.345917857e+01 4.7e-03 0.01
4 3.4e-03 5.6e-04 9.0e-04 8.54e-01 3.499292715e+01 3.560567807e+01 5.6e-04 0.01
5 3.2e-04 5.4e-05 2.8e-05 9.30e-01 3.589565789e+01 3.596079962e+01 5.4e-05 0.01
6 3.7e-06 6.2e-07 3.5e-08 9.86e-01 3.599877019e+01 3.599955500e+01 6.2e-07 0.01
7 5.9e-10 2.6e-09 1.0e-13 1.00e+00 3.599999980e+01 3.600000005e+01 1.9e-10 0.01
Optimizer terminated. Time: 0.01
Interior-point solution summary
Problem status : PRIMAL_AND_DUAL_FEASIBLE
Solution status : OPTIMAL
Primal. obj: 3.5999999802e+01 nrm: 2e+01 Viol. con: 3e-12 var: 0e+00 cones: 4e-09
Dual. obj: 3.6000000048e+01 nrm: 4e+01 Viol. con: 0e+00 var: 1e-09 cones: 0e+00
Optimizer summary
Optimizer - time: 0.01
Interior-point - iterations : 7 time: 0.01
Basis identification - time: 0.00
Primal - iterations : 0 time: 0.00
Dual - iterations : 0 time: 0.00
Clean primal - iterations : 0 time: 0.00
Clean dual - iterations : 0 time: 0.00
Simplex - time: 0.00
Primal simplex - iterations : 0 time: 0.00
Dual simplex - iterations : 0 time: 0.00
Mixed integer - relaxations: 0 time: 0.00
Status: Solved
Optimal value (cvx_optval): +36
disp(x)
0.000000000000000 + 3.000000000000000i
0.000000000000000 + 3.000000000000000i
0.000000000000000 + 3.000000000000000i
0.000000000000000 + 3.000000000000000i
Ball
December 15, 2023, 2:40am
17
Ok i undertand and thank for your help.