• 大小: 1KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: Matlab
  • 标签: 拓扑  MATLAB  

资源简介

随机拓扑生成源码,用MATLAB软件仿真运行,只有源码,其他运行

资源截图

代码片段和文件信息

function [SxyAMCostDelayDelayJitterPacketLoss]=NetCreate(BorderLengthNodeAmountAlphaBetaPlotIfFlagIf)




%%
%参数初始化
BorderLength=10;
NodeAmount=20;
Alpha=40;
Beta=3;
PlotIf=1;
FlagIf=1;
NN=10*NodeAmount;
SSxy=zeros(NN2);
%在正方形区域内随机均匀选取NN个节点
for i=1:NN
SSxy(i1)=BorderLength*rand;
SSxy(i2)=BorderLength*rand;
end
[IDXC]=kmeans(SSxyNodeAmount);
Sxy=[[1:NodeAmount]‘C]‘;
CountNeighborAmount=0;

%按横坐标由小到大的顺序重新为每一个节点编号
temp=Sxy;
Sxy2=Sxy(2:);
Sxy2_sort=(Sxy2);
for i=1:NodeAmount
pos=find(Sxy2==Sxy2_sort(i));
if length(pos)>1
error(‘仿真故障,请重试!‘);
end
temp(1i)=i;
temp(2i)=Sxy(2pos);
temp(3i)=Sxy(3pos);
end
Sxy=temp;
%在节点间随机产生边,并构造延时矩阵和费用矩阵
AM=zeros(NodeAmountNodeAmount);
NeighborAmount=zeros(2NodeAmount);
Cost=zeros(NodeAmountNodeAmount);
Delay=zeros(NodeAmountNodeAmount);
DelayJitter=zeros(NodeAmountNodeAmount);
PacketLoss=zeros(NodeAmountNodeAmount);

for i=1:(NodeAmount-1)
for j=(i+1):NodeAmount
Distance=((Sxy(2i)-Sxy(2j))^2+(Sxy(3i)-Sxy(3j))^2)^0.5;
P=Beta*exp(-Distance^5/(Alpha*BorderLength));
if P>rand
AM(ij)=1;
AM(ji)=1;
Delay(ij)=0.5*Distance/100000;
Delay(ji)=Delay(ij);
Cost(ij)=2+8*rand;
Cost(ji)=Cost(ij);
DelayJitter(ij)=0.000001*(1+2*rand);
DelayJitter(ji)=DelayJitter(ij);
PacketLoss(ij)=0.01*rand;
PacketLoss(ji)=PacketLoss(ij);
else
Delay(ij)=inf;
Delay(ji)=inf;
Cost(ij)=inf;
Cost(ji)=inf;
DelayJitter(ij)=inf;
DelayJitter(ji)=inf;
PacketLoss(ij)=inf;
PacketLoss(ji)=inf;
end
end
end
for i=1:NodeAmount
    NeighborAmount(1i)=i;
    for j=1:NodeAmount
        NeighborAmount(2i)=NeighborAmount(2i)+AM(ij);
    end
end

for i=1:NodeAmount
    CountNeighborAmount=CountNeighborAmount+NeighborAmount(2i)
end
PJNA=CountNeighborAmount/NodeAmount;
PJNA
AM
Net_plot(BorderLengthNodeAmountSxyCostDelayDelayJitterPacketLossPlotIfFlagIf)

%用于绘制网络拓扑的函数
function Net_plot(BorderLengthNodeAmountSxyCostDelayDelayJitterPacketLossPlotIfFlagIf)
%画节点
if PlotIf==1
plot(Sxy(2:)Sxy(3:)‘r.‘)
%设置图形显示范围
xlim([0BorderLength]);
ylim([0BorderLength]);
hold on
%为节点标序号
for i=1:NodeAmount
Str=int2str(i);
text(Sxy(2i)+BorderLength/100Sxy(3i)+BorderLength/100Str‘FontSize‘12);
hold on
end
end
%画边,并给边标注费用和延时
if PlotIf==1
for i=1:(NodeAmount-1)
for j=(i+1):NodeAmount
if isinf(Cost(ij))==0
plot([Sxy(2i)Sxy(2j)][Sxy(3i)Sxy(3j)]);
if FlagIf==1
xx=0.5*(Sxy(2i)+Sxy(2j));
yy=0.5*(Sxy(3i)+Sxy(3j));
Str1=num2str(Cost(ij));
Str2=num2str(1000000*Delay(ij));
Str3=num2str(1000000*DelayJitter(ij));
Str4=num2str(100*PacketLoss(ij));
Str1=Str1(1:3);
Str2=Str2(1:3);
Str3=Str3(1:3);
Str4=Str4(1:3);
text(xxyy[‘(‘Str1‘‘Str2‘‘Str3‘‘Str4‘)‘]‘FontSize‘8);
end
hold on
end
end
end
end 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2926  2010-05-27 14:34  NetCreate.m

----------- ---------  ---------- -----  ----

                 2926                    1


评论

共有 条评论