close all;
monte_num=200; % 蒙特卡洛仿真次数
%% 不同P_on
%clear all;
clc;
n=10:5:40; % 用户数
n_max=max(n);
m=10; % 子信道数
pfa=0.01; % 虚警概率
Ts=5*10^-3; % 感知时间1ms
Tr=195*10^-3; % 传输时间 待定
data=1;%0.5; % 信道,能量损耗影响因子
Qfa=0.05; % 系统虚警概率要求
pdoh=0;
%monte_num=10; % 蒙特卡洛仿真次数
match_method=2;
compare=3; % 比较的方法个数
Pd_req=0.95; %0.95;
conspdreq=1; %考虑各信道检测概率需求法
kc=60; % 种群数目,暂定必须是偶数,且能被4整除
Gain_sum=zeros(compare,length(n));
Overhead_sum=zeros(compare,length(n));
profit_sum=zeros(compare,length(n));
Pd_min_sum=zeros(compare,length(n));
Pd_ave_sum=zeros(compare,length(n));
Pf_sum=zeros(compare,length(n));
num_co_sum=zeros(compare,length(n));
for k=1:monte_num
k
seq=0;
R_cr=(rand(1,n_max)*0.99+0.01)*10^7; % 各节点传输速率 0.1Mbps -10M bps % zeros(1,n_max); %
R_ch=(rand(1,m)*0.99+0.01)*10^7; % 各信道传输速率 0.1Mbps -10M bps
p_d_matrix=rand(m,n_max)*0.7+0.3; % 各用户各信道检测概率矩阵 均值0.8
% P_on=rand(1,m)*0.6+0.4; %0.7; % 信道空闲概率
% 遗传算法
P_on=ones(1,m)*0.5;
[ fit_best_finalvalue, fit_best_co_num, Pd_final_ave, Pf_final ] = PM_GAMP_22( n, m, P_on, pfa, Ts,Tr,data,Qfa,R_cr,R_ch,p_d_matrix,60);
seq=seq+1;
%Gain_sum(seq,:)=Gain_sum(seq,:)+Gain_final;
%Overhead_sum(seq,:)=Overhead_sum(seq,:)+Overhead_final;
profit_sum(seq,:)=profit_sum(seq,:)+fit_best_finalvalue;
%Pd_min_sum(seq,:)=Pd_min_sum(seq,:)+Pd_final_min;
Pd_ave_sum(seq,:)=Pd_ave_sum(seq,:)+Pd_final_ave;
Pf_sum(seq,:)=Pf_sum(seq,:)+Pf_final;
num_co_sum(seq,:)=num_co_sum(seq,:)+fit_best_co_num;
P_on=ones(1,m)*0.7;
[ fit_best_finalvalue, fit_best_co_num, Pd_final_ave, Pf_final ] = PM_GAMP_22( n, m, P_on, pfa, Ts,Tr,data,Qfa,R_cr,R_ch,p_d_matrix,60);
seq=seq+1;
%Gain_sum(seq,:)=Gain_sum(seq,:)+Gain_final;
%Overhead_sum(seq,:)=Overhead_sum(seq,:)+Overhead_final;
profit_sum(seq,:)=profit_sum(seq,:)+fit_best_finalvalue;
%Pd_min_sum(seq,:)=Pd_min_sum(seq,:)+Pd_final_min;
Pd_ave_sum(seq,:)=Pd_ave_sum(seq,:)+Pd_final_ave;
Pf_sum(seq,:)=Pf_sum(seq,:)+Pf_final;
num_co_sum(seq,:)=num_co_sum(seq,:)+fit_best_co_num;
P_on=ones(1,m)*0.9;
[ fit_best_finalvalue, fit_best_co_num, Pd_final_ave, Pf_final ] = PM_GAMP_22( n, m, P_on, pfa, Ts,Tr,data,Qfa,R_cr,R_ch,p_d_matrix,60);
seq=seq+1;
%Gain_sum(seq,:)=Gain_sum(seq,:)+Gain_final;
%Overhead_sum(seq,:)=Overhead_sum(seq,:)+Overhead_final;
profit_sum(seq,:)=profit_sum(seq,:)+fit_best_finalvalue;
%Pd_min_sum(seq,:)=Pd_min_sum(seq,:)+Pd_final_min;
Pd_ave_sum(seq,:)=Pd_ave_sum(seq,:)+Pd_final_ave;
Pf_sum(seq,:)=Pf_sum(seq,:)+Pf_final;
num_co_sum(seq,:)=num_co_sum(seq,:)+fit_best_co_num;
end;
Gain_monte=Gain_sum/monte_num;
Overhead_monte=Overhead_sum/monte_num;
profit_monte=profit_sum/monte_num;
% Pd_min_monte=Pd_min_sum/monte_num;
Pd_ave_monte=Pd_ave_sum/monte_num;
Pf_monte=Pf_sum/monte_num;
num_co_monte=num_co_sum/monte_num;
figure;
grid on;hold on;
plot(n,profit_monte(1,:)','-sk'); % plot(n,profit_monte,'-s') 找到错误了,plot画不同曲线的时候,是按照每列一条曲线画的,而我是按照每行一条曲线画的,当compare~=length(n)时,由于前面有n限制,因此是按照行话,当compare=length(n),就按照默认的列画了
plot(n,profit_monte(2,:)','-dk');
plot(n,profit_monte(3,:)','-*k');
xlabel('认知用户数');ylabel('总系统效益(bps)');
legend('\it{P_{on}}=0.5','\it{P_{on}}=0.7','\it{P_{on}}=0.9','location','southeast');
figure;
grid on;hold on;
plot(n,num_co_monte(1,:)','-sk');
plot(n,num_co_monte(2,:)','-dk');
plot(n,num_co_monte(3,:)','-*k'); % 重新仿的话按照顺序3,4就行了
xlabel('认知用户数');ylabel('参与合作感知的认知用户数');
legend('\it{P_{on}}=0.5','\it{P_{on}}=0.7','\it{P_{on}}=0.9','location','southeast');