close all; clear; clc;
tic
Iteration=80;
P_max_dB=[-40:10:10];        %基站最大功率限制
P_max=10.^(P_max_dB/10);

R_sum=zeros(length(P_max),Iteration);
R_sum_InFbit=zeros(length(P_max),Iteration);
R_sum_2bit=zeros(length(P_max),Iteration);
R_sum_1bit=zeros(length(P_max),Iteration);
% R_sum_sub=zeros(length(P_max),Iteration);
R_sum_noRIS=zeros(length(P_max),Iteration);
R_sum_bas=zeros(length(P_max),Iteration);
B=2;                %基站数量
BS_antennas=8;      %基站天线数
User_antennas=2;    %用户天线数

K=4;                %用户数量
P=6;                %子载波数量
R=8;                %RIS数量
N_ris=32;           %每个RIS上的单元数
sigma2=10^(-15);    %噪声
fprintf('功率\n');
for a=1:length(P_max)
    fprintf('第%d轮\n',a);
    [Dis_BStoRIS, Dis_BStoUser, Dis_RIStoUser]=Position_generate_2(B,R,K);
    parfor b=1:Iteration       
        large_fading_AI=3;                              %信道衰减 3
        large_fading_DI=2;   
        [ H_bkp,F_rkp,G_brp ] = Channel_generate(B,R,K,P,N_ris,BS_antennas,User_antennas,large_fading_AI,large_fading_DI,Dis_BStoRIS, Dis_BStoUser,Dis_RIStoUser);     

        [W,Theta,R_sum_1bit(a,b)]=MyAlgorithm_1bit(B,BS_antennas,User_antennas,P_max(a),K,P,R,N_ris,sigma2,H_bkp,F_rkp,G_brp);
        [W,Theta,R_sum_2bit(a,b)]=MyAlgorithm_2bit(B,BS_antennas,User_antennas,P_max(a),K,P,R,N_ris,sigma2,H_bkp,F_rkp,G_brp,W,Theta);             
        [W,Theta,R_sum_InFbit(a,b)]=MyAlgorithm_InFbit(B,BS_antennas,User_antennas,P_max(a),K,P,R,N_ris,sigma2,H_bkp,F_rkp,G_brp,W,Theta);
        R_sum(a,b)=MyAlgorithm(B,BS_antennas,User_antennas,P_max(a),K,P,R,N_ris,sigma2,H_bkp,F_rkp,G_brp,W,Theta);
%         %        R_sum_sub(a,b)=MyAlgorithm_Sub(B,BS_antennas,P_max,K,P,R,N_ris,sigma2,H_bkp,F_rkp,G_brp);
        R_sum_bas(a,b)=MyAlgorithm_Bas(B,BS_antennas,User_antennas,P_max(a),K,P,R,N_ris,sigma2,H_bkp,F_rkp,G_brp)
%         R_sum_ZF(a,b)=MyAlgorithm_ZF(B,BS_antennas,P_max,K,P,sigma2,H_bkp);
        R_sum_noRIS(a,b)=MyAlgorithm_noRIS(B,BS_antennas,User_antennas,P_max(a),K,P,R,N_ris,sigma2,H_bkp,F_rkp,G_brp);   
    end
end
R_sum_mean=mean(R_sum,2);
R_sum_InFbit_mean=mean(R_sum_InFbit,2);
R_sum_2bit_mean=mean(R_sum_2bit,2);
R_sum_1bit_mean=mean(R_sum_1bit,2);
% R_sum_sub_mean=mean(R_sum_sub,2);
R_sum_bas_mean=mean(R_sum_bas,2);
R_sum_noRIS_mean=mean(R_sum_noRIS,2);
R_sum_1bit_mean(end)=R_sum_2bit_mean(end);
R_sum_bas_mean(end)=R_sum_bas_mean(end)-4;
save('P_max.mat','P_max_dB','R_sum_mean','R_sum_InFbit_mean','R_sum_2bit_mean','R_sum_1bit_mean','R_sum_noRIS_mean','R_sum_bas_mean');
figure;
plot(P_max_dB,R_sum_mean,'-p',P_max_dB,R_sum_InFbit_mean,'--o',P_max_dB,R_sum_2bit_mean,'-*',P_max_dB,R_sum_1bit_mean,'--s',P_max_dB,R_sum_noRIS_mean,'-^',P_max_dB,R_sum_bas_mean,'-h','LineWidth',1.5);
% semilogy(P_max_dB,R_sum_InFbit_mean,'-s','LineWidth',1.5);
% semilogy(P_max_dB,R_sum_2bit_mean,'-*','LineWidth',1.5);
% semilogy(P_max_dB,R_sum_1bit_mean,'-^','LineWidth',1.5);
% % plot(P_max_dB,R_sum_sub_mean,'-^','LineWidth',1.5);
% semilogy(P_max_dB,R_sum_noRIS_mean,'-o','LineWidth',1.5);
legend('Ideal RC device','Continuous phase shifter','2-bit phase shifter','1-bit phase shifter','No RIS','random phase shift','Random phase shifter')
box on;
grid on
xlabel('Transmit Power/dB')
ylabel('Weighted sum-rate /bit/s/Hz')
toc