Reputation: 91
I want to create a network with random positions (x,y) of the nodes in the ned file. At first, I don't want to create any link between the nodes. I know setting the parameters x,y and writing each node hard-coded would work. But I want to do it automatically
network Network {
submodules:
node1:Node {
@display("p=250,300");
}
node2:Node {
@display("p=591,450");
}
node3:Node {
@display("p=213,150");
}
}
want to turn this, into something like:
network Network {
submodules:
for i=0..50 {
node[i]:Node{
@display("p=randomX,randomY");
}
}
Upvotes: 2
Views: 1768
Reputation: 881
Typically, such a random placement is configured through the NED and the INI configuration file. But you can also do it just within the NED file, take a look at this example that will run in plain OMNeT++ without any additional framework:
network Random
{
parameters:
int n @prompt("Number of nodes") = default(10);
volatile int posX = intuniform (0,100);
volatile int posY = intuniform (0,100);
submodules:
node[n]: Node{
parameters:
@display("p=$posX,$posY");
}
}
This will draw a integer from a uniform distribution between 0 and 100 each time that a node is positioned. The ``volatile``` is necessary to allows multiple evaluations of the parameter expression (check the OMNeT Simulation Manual for additional infos).
You can also parametrize the min and max values of the intuniform distribution if you want.
IMPORTANT: Randomness in OMNeT++ (and simulation frameworks in general) is only pseudo-randomness. A random number generator uses a seed value to start calculating a stream of random numbers. If the seed is the same, the drawn random numbers are the same. You will thus see the same random node placement each time you start the simulation. This is not a bug but an important feature to enable repeated simulation runs and repeatability of experiments in general. If you want different random values in NED or INI variables then you could modify the seed start value so that it is different at each run.
You could (for example) use the process ID as the seed start value. Each time you start the simulation (run the OMNeT GUI), a (hopefully different) process ID will be generated and used as the seed start value.
Insert the following line in your corresponding omnetpp.ini
file:
seed-set = ${processid}
If you want to add random connections afterwards too, then take a look at the neddemo
folder in the OMNeT++ examples. It has a nice example of random connections that is also explained in the OMNeT++ simulation manual.
Upvotes: 3