Burak Kaymakci
Burak Kaymakci

Reputation: 690

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

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.

Upvotes: 2

Views: 434

Answers (1)

Jerzy D.
Jerzy D.

Reputation: 7002

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

Upvotes: 1

Related Questions