如何在 OMNeT++ 中创建一个随机连接的图?

How to create a randomly connected graph in OMNeT++?

我正在尝试创建一个具有随机连接节点的图。 节点应随机连接,如果一个节点已经连接到另一个节点,则不应使用不同的 inout 端口再次连接到同一节点。

In the docs有这个创建随机图的例子:

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;
        }
}

但是这种方法可能会使用不同的端口多次连接相同的两个节点,这不是我想要的。

正如您从上面的屏幕截图中看到的,node1 通过两个不同的端口连接到 node6

我不希望出现这种行为,因为在我的代码中,我使用 for 循环将消息发送到所有输出端口,然后将相同的消息发送到同一节点两次。

我可以尝试在 initialize() 函数中消除与同一节点的多个连接 我想,我只是在创建此 post 时想到的。我还没有尝试过,但我会并且会分享结果。 我也想听听你的解决方案。

您使用的示例应该在手册中得到更正。内部for循环必须从外部循环中索引的当前值开始。此外,运算符 ++ 应该用于门,因为根据 OMNeT++ 手册:

The gatename++ notation causes the first unconnected gate index to be used.

感谢 ++ 无需维护门的索引即可连接。
最后一个变化:满足条件时输入和输出门都应该连接。
您的 NED 的更正代码可能如下所示:

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 建议的简化代码:

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++;
        }
}