如何在 OMNeT++ 中创建随机连接图? [英] How to create a randomly connected graph in OMNeT++?
问题描述
我正在尝试创建一个具有随机连接节点的图形.节点应该随机连接,如果一个节点已经连接到另一个节点,它不应该使用不同的inout
端口再次连接到同一个节点.
I am trying to create a graph with randomly connected nodes.
Nodes should be connected randomly and if a node is already connected to another node, it shouldn't be connected to the same node again using different inout
port.
在文档中这是创建随机图的示例:
In the docs there is this example to create a random graph:
module RandomGraph {
parameters:
int count;
double connectedness; // 0.0<x<1.0
submodules:
node[count]: Node {
gates:
in[count];
out[count];
}
connections allowunconnected:
for i=0..count-1, for j=0..count-1 {
node[i].out[j] --> node[j].in[i]
if i!=j && uniform(0,1)<connectedness;
}
}
但是这种方法可能会使用不同的端口多次连接相同的两个节点,这不是我想要的.
But this method might connect the same two nodes multiple times using different ports which is not what I want.
如上图所示,node1
通过两个不同的端口连接到 node6
.
As you can see from the above screenshot node1
is connected to node6
through two different ports.
我不想要这种行为,因为在我的代码中,我使用 for 循环向所有输出端口发送消息,然后将相同的消息发送到同一个节点两次.
I do not want this behavior because in my code I am sending a message to all the out ports using a for loop which then sends the same message twice to the same node.
我可以尝试在 initialize()
函数中消除到同一节点的多个连接,我想,我只是在创建这篇文章时想到了它.我还没有尝试过,但我会并且会分享结果.我也想听听您的解决方案.
I could try eliminating multiple connections to the same node in the initialize()
function I guess, I just thought of it while I am creating this post. I've not tried it yet but I will and will share the result.
I would like to hear your solutions as well.
推荐答案
您使用的示例应该在手册中更正.内部 for 循环必须从外部循环中索引的当前值开始.此外,运算符 ++
应该用于门,因为根据 OMNeT++ 手册:
The example you used should be corrected in the manual. The inner for-loop has to start from the current value of the index in outer loop. Moreover, the operator ++
should be used for gates, because according to OMNeT++ Manual:
gatename++ 符号导致使用第一个未连接的门索引.
The gatename++ notation causes the first unconnected gate index to be used.
感谢++
,无需维护连接门的索引.
最后一个变化:满足条件时,输入和输出门都应该连接.
更正后的 NED 代码可能如下所示:
Thanks to ++
there is no need to maintain the index of the gate to connect.
The last change: both input and output gates should be connected when the condition is met.
The corrected code of your NED may looks like:
module RandomGraph
{
parameters:
int count;
double connectedness; // 0.0<x<1.0
submodules:
node[count]: Node {
gates:
in[count];
out[count];
}
connections allowunconnected:
for i=0..count-1, for j=i..count-1, if i!=j && uniform(0,1)<connectedness {
node[i].out++ --> node[j].in++;
node[i].in++ <-- node[j].out++;
}
}
编辑
关于@Rudi 建议的简化代码:
EDIT
Simplified code concerning @Rudi suggestions:
module RandomGraph
{
parameters:
int count;
double connectedness; // 0.0<x<1.0
submodules:
node[count]: Node {
gates:
in[]; // removed the size of gate
out[];
}
connections allowunconnected:
for i=0..count-2, for j=i+1..count-1, if uniform(0,1)<connectedness {
node[i].out++ --> node[j].in++;
node[i].in++ <-- node[j].out++;
}
}
这篇关于如何在 OMNeT++ 中创建随机连接图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!