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

不问归期 提交于 2021-01-27 20:02:20

问题


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.

connected nodes

As you can see from the above screenshot node1 is connected to node6 through two different ports.

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.

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.


回答1:


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:

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

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

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


来源:https://stackoverflow.com/questions/58779882/how-to-create-a-randomly-connected-graph-in-omnet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!