資源簡介
隨機拓撲生成源碼,用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
- 上一篇:模糊k均值聚類算法matlab實現
- 下一篇:LBP算法的Matlab代碼
評論
共有 條評論