function TDSR %This function simulate the concepts of Dynamic Source Routing %The function finds path from source node(node1) to destination node(node10) %The output of this function is the figure displaying network topology and %the selected path from source to destination,the average trust value of %the selected path and the number of hops clear; noOfNodes =10; figure(1); clf; hold on; R =5; % node transmission range sor =1;%source node des =10;%destination node X = [1 2 3 4 8 6 7 9 10 10];%nodes' x coordinates Y = [6 2 5 8 5 1 10 2 8 5];%nodes' y coordinates Z =[1 1 0.7 0.4 0.1 0.1 0.1 1 1 1];%nodes' trust values %plotting network topology for i = 1:noOfNodes plot(X(i), Y(i), '.'); text(X(i), Y(i), num2str(i)); for j = 1:noOfNodes distance = sqrt((X(i) - X(j))^2 + (Y(i) - Y(j))^2); if distance <= R % there is a link; matrix(i, j) =1; trust(i,j)=1-((Z(i)+Z(j))/2); line([X(i) X(j)], [Y(i) Y(j)], 'LineStyle', ':'); matriz(i,j)=distance; else matrix(i, j) =inf; trust(i,j)= inf; matriz(i,j)=inf; end end end [path, cost] = dijkstra(sor,des,trust);%finding the path from source to destination trusted_path=path; trusted_path_trust=1-cost; trusted_path_hops=length(path)-1; trusted_path_distance=0; for d=2:length(path) trusted_path_distance= trusted_path_distance + matriz(path(d-1),path(d)); end trusted_path_distance; %plotting the selected path for p =1:(length(path)-1) line([X(sor) X(path(1))],[Y(sor) Y(path(1))],'Color','r','LineWidth', 1) 'LineStyle'; '-'; line([X(path(p)) X(path(p+1))], [Y(path(p)) Y(path(p+1))]) 'Color','r','LineWidth'; 1; 'LineStyle';'-' end grid hold on return;
时间: 2023-06-27 19:06:12 浏览: 171
PyPI 官网下载 | alibabacloud_tdsr20200101_py2-3.0.3-py2-none-any.whl
这是一个用 MATLAB 编写的函数,用于模拟动态源路由(DSR)的概念。该函数会寻找从源节点(node1)到目标节点(node10)的路径,并输出显示网络拓扑和选定路径、选定路径的平均信任值以及跳数的图像。
该函数首先清除所有变量,然后定义了节点数量、节点传输范围、源节点和目标节点的位置坐标以及节点的信任值。接下来,该函数会绘制网络拓扑,并根据节点之间的距离和信任值构建连接矩阵和信任矩阵。然后使用 Dijkstra 算法寻找源节点到目标节点的路径,并计算出选定路径的平均信任值、跳数和距离。最后,该函数将绘制选定路径的图像,并返回结果。
阅读全文