Search code examples
omnet++

OMNeT++ : for loop and value assignation in .NED file


I'm a beginner in using the OMNeT++ simulator, sorry in advance if I do something wrong. I would like to create a network that is randomly disposed, and for that I'm trying to implement a random graph program as a .ned file. Here is an example of the code I would like to implement (written in .c, only to show you an example) :

int nbHost = 10;
double randConnect = 0.6;

for (int i = 0; i < 10; i++)
{
    for (int src = 0; src < nbHost-1; src++)
    {
        if (uniform(0,1) < randConnect)
        {
            // set dest different of src
            int dest = uniform(0,nbHost-2);
            if (dest >= src)
                dest++;
            
            //connect src to host
        }
    }
    randConnect -= 0.05;        
}

I tried to translate it into a .ned file, and got this :

simple Node
{
    parameters:
        @display("i=block/routing");
        
    gates:
        inout gate[];
}

network Network
{
    parameters:
        int nbHost = default(10);
        int count = default(10);
        double randConnect = default(0.6);
        int dest;
        
    types:
        channel Channel extends ned.DelayChannel { delay = 100ms; }
        
    submodules:
        host[nbHost]: Node;
        
    connections allowunconnected:
        for i=0..count, for src=0..nbHost-1, if uniform(0,1)<randConnect {
            
            dest = uniform(0,nbHost-2);
            if dest >= src {
                dest++;
            }
            
            host[src].gate++ <--> Channel <--> host[dest].gate++;
        }
        
}

But several problems appear and I can't find a solution. First, it seems like I can't assign a value like I want to do with dest = uniform(0,nbHost-2);. I don't know if this is the proper way or even possible. Second, the line if dest >= src { is not possible like this, because nested connection groups is not allowed and I don't know how to manage it. Finally, I can't even find a way to add randConnect -= 0.05; on the .ned file, due to the way the for loop is made.

Any help or tips would be welcome!


Solution

  • Well, .ned is a declarative language, so it is somewhat limited what you can do with it. Specifically, you cannot have control structures or variable declarations, so translating your C code directly is not the way to go. This is how the problem would be solved in NED:

    network RandomGraph
    {
        parameters:
            int n = default(10);
            double connectedness = 0.6 - (${0..10})*0.05; // 11 runs
        submodules:
            node[n]: RandomGraphNode;
        connections:
            for i = 0..n-2, for j = i+1..n-1 {
                node[i].g++ <--> node[j].g++ if uniform(0,1)<connectedness;
            }
    }
    

    Note that the ${} construct in the connectedness expression is called iteration variable (look in the manual for more info)