clc clear close all N_Users=50; %% Secondary Users P_Users=4; %% Primary Users N_sig_dim=3; %% Signal dimensions of secondary NoChannels=4; %% Number of channels D=50; p=10*ones(N_Users,1); %% Initial power levels of secondary users p_cons=0.1; %% Consumed power by secondary users Noise_var=0.00001; %% Normalized sequence for Secondary Users seqnce=randsrc(N_sig_dim,N_Users); for UserCounter=1:N_Users while seqnce(:,UserCounter)==-1*ones(N_sig_dim,1) seqnce(:,UserCounter)=2*randi([0 1],N_sig_dim,1)-1; end seqnce(:,UserCounter)=seqnce(:,UserCounter)/(norm(seqnce(:,UserCounter))); end %% No of Iterations Iterations=200; umax=1; %% Location of Users in xy-plane xcoordinate = D*rand(1,N_Users); ycoordinate = D*rand(1,N_Users); %% Label of Users labels = cellstr(num2str(1:N_Users)'); % Labels correspond to their order % figure(1); % plot(xcoordinate,ycoordinate,'o'); % % text(xcoordinate,ycoordinate,labels); % axis([0 D 0 D]) %% Distance d = zeros(N_Users,N_Users); ChannGain=zeros(N_Users,N_Users); %% Init_SlctdChannel=randi(NoChannels,N_Users,1); %% Initially selected channels by secondary users chann_updated=Init_SlctdChannel; f_c=zeros(1,N_Users); p_updated=zeros(N_Users,1); %% V=zeros(N_sig_dim,N_sig_dim); s_updated=zeros(N_sig_dim,N_Users); u=zeros(NoChannels,N_Users,Iterations); c=zeros(NoChannels,N_Users,Iterations); Utility=zeros(NoChannels,N_Users,Iterations); max_Utility=zeros(N_Users,Iterations); m_u=zeros(N_Users,Iterations); chann_new=zeros(N_Users,Iterations); SIR=zeros(N_Users,Iterations); max_SIR=zeros(N_Users,Iterations); ISIR=zeros(NoChannels,N_Users,Iterations); %% for IterationCounter=1:Iterations for UserCounter = 1:N_Users for j = 1:N_Users if UserCounter~=j X = [xcoordinate(UserCounter),ycoordinate(UserCounter);xcoordinate(j),ycoordinate(j)]; d(UserCounter,j) = pdist(X,'euclidean'); if d(UserCounter,j)<=p figure(2); plot(xcoordinate,ycoordinate,'o'); % text(xcoordinate,ycoordinate,labels); axis([0 D 0 D]); hold on; line([xcoordinate(UserCounter),xcoordinate(j)],[ycoordinate(UserCounter),ycoordinate(j)]); end ChannGain(UserCounter,j)=1/(d(UserCounter,j)^2); else ChannGain(UserCounter,j)=1; end end end %% Random Way point mobility s_input = struct('V_POSITION_X_INTERVAL',[0 50],...%(m) 'V_POSITION_Y_INTERVAL',[0 50],...%(m) 'V_SPEED_INTERVAL',[0.2 2.2],...%(m/s) 'V_PAUSE_INTERVAL',[0 1],...%pause time (s) 'V_WALK_INTERVAL',[2.00 6.00],...%walk time (s) 'V_DIRECTION_INTERVAL',[-180 180],...%(degrees) 'SIMULATION_TIME',10,...%(s) 'NB_NODES',50); s_mobility = Generate_Mobility(s_input); timeStep = 0.1;%(s) % test_Animate(s_mobility,s_input,timeStep); %% for UserCounter=1:N_Users for ChanCounter=1:NoChannels R=0; X1=zeros(N_sig_dim,N_sig_dim); [x]=find(ChanCounter==chann_updated); temp=[x;UserCounter]; for jj=1:length(temp) j=temp(jj); if (j~=UserCounter) % Interference Calculation R=R+(seqnce(:,UserCounter)'*seqnce(:,j)*seqnce(:,j)'*seqnce(:,UserCounter)*p(j)*ChannGain(j,UserCounter)^2/(p(UserCounter)*ChannGain(UserCounter,UserCounter)^2)); end SIR(ChanCounter,UserCounter,IterationCounter)=1/R; ISIR(ChanCounter,UserCounter,IterationCounter)=R; %Inverse SIR if (j~=UserCounter) X1=xcorr2(seqnce(:,j),seqnce(:,UserCounter)')+X1; %Correlation of signature sequences of users sharing same channel end end X=X1; [ex_vec,exv]=eig(X); V(:,ChanCounter)=ex_vec(:,1); %Eigen vector corresponding to min eigen value of correlation metrix u(ChanCounter,UserCounter,IterationCounter)=-(ex_vec(:,1)'*X*ex_vec(:,1))/p(UserCounter); %Gain of the user c(ChanCounter,UserCounter,IterationCounter)=(p_cons*ISIR(ChanCounter,UserCounter,IterationCounter)+(f_c(UserCounter)))/p(UserCounter); %Cost of the game Utility(ChanCounter,UserCounter,IterationCounter)=u(ChanCounter,UserCounter,IterationCounter)-c(ChanCounter,UserCounter,IterationCounter); end %%Maximum utility max_SIR(UserCounter,IterationCounter)=max(SIR(:,UserCounter,IterationCounter)); m_u(UserCounter,IterationCounter)=max(u(:,UserCounter,IterationCounter)); max_Utility(UserCounter,IterationCounter)=max(Utility(:,UserCounter,IterationCounter)); m=max(Utility(:,UserCounter,IterationCounter)); index=find(m==Utility(:,UserCounter,IterationCounter)); %% Finding new channel for each user if length(index)>1 index=chann_updated(UserCounter); end chann_updated(UserCounter)=index; chann_new(UserCounter,IterationCounter)=index; if (IterationCounter>1) if(chann_new(UserCounter,IterationCounter)~=chann_new(UserCounter,IterationCounter-1)) f_c(UserCounter)=f_c(UserCounter)+1; %Switching cost of users IterationChange=f_c; end end %% Calculating updated power of each user based on channel opted by each user R1=Noise_var; [z]=find(chann_updated(UserCounter)==chann_updated); temp=[z;UserCounter]; for jj=1:length(temp) j=temp(jj); if(j~=UserCounter) R1=R1+(seqnce(:,UserCounter)'*seqnce(:,j)*seqnce(:,j)'*seqnce(:,UserCounter)*p(j)*ChannGain(j,UserCounter)^2); end end Inv_SIR=R1; p_updated(UserCounter,IterationCounter)=(2*p_cons*Inv_SIR)/(ChannGain(UserCounter,UserCounter)^2*(1+f_c(UserCounter))); s_updated(:,UserCounter)=V(:,index); %Updated signature sequences p(UserCounter)=p_updated(UserCounter,IterationCounter); end Exact_pot=-sum(max_Utility); %Exact potential function is negative sum of maximum utility tot_ISIR=-Exact_pot; seqnce=s_updated; p=p_updated(:,IterationCounter); end %% figure plot(1:IterationCounter,Exact_pot) title('Potential function vs Iterations') xlabel('Number of iterations') ylabel('Potential Function') figure plot(1:IterationCounter,max_Utility) % xlim([0 20.2]) title('Cost function vs Iterations') xlabel('Number of iterations') ylabel('Utility') figure plot(1:IterationCounter,chann_new) ylim([0.8 NoChannels+.2]) set(gca,'YTick',[0 1 2 3 4 5]) title('Channel allocation vs Iterations') xlabel('Number of iterations') ylabel('Channels') figure semilogy(1:IterationCounter,p_updated) title('Power vs Iterations') xlabel('Number of iterations') ylabel('Power of each node') figure plot(1:IterationCounter,tot_ISIR) title('ISIR vs Iterations') xlabel('Number of iterations') ylabel('ISIR') % xlim([0 2.2])