Skip to content

Running a simulation

Radu Ioan Ciobanu edited this page Sep 13, 2016 · 4 revisions

In order to run a simulation, we need to parse a trace, instantiate opportunistic nodes for each device in the trace, and then run the algorithm (and maybe compute some statistics at the end). Thus, the first step is to instantiate the parser object implemented here:

// create a new Myparser object
Parser parser = new MyParser();

We can also print out some trace statistics, to see how the data can be accessed:

// print some trace statistics
double duration = (double) (parser.getTraceData().getEndTime() - parser.getTraceData().getStartTime()) / (Parser.MILLIS_PER_MINUTE * 60);
System.out.println("Trace duration in hours: " + duration);
System.out.println("Trace contacts: " + parser.getTraceData().getContactsCount());
System.out.println("Trace contacts per hour: " + (parser.getTraceData().getContactsCount() / duration));
System.out.println("Nodes: " + parser.getNodesNumber());

Then, we initialize nodes for all the devices in the trace, depending on the desired opportunistic solution. In the example below, we instantiate a MyAlgorithm node (as shown in this example), the data memory size is 10000, the exchange history size is 100, the random number generator seed is 0, the algorithm is used for routing, and no altruism information is employed:

// initialize MyAlgorithm  nodes
long seed = 0;
boolean dissemination = false;
Node[] nodes = new Node[parser.getNodesNumber()];
for (int i = 0; i < nodes.length; i++) {
    nodes[i] = new MyAlgorithm(i, nodes.length, parser.getContextData().get(i),
                               parser.getSocialNetwork()[i], 10000, 100, seed,
                               parser.getTraceData().getStartTime(),
                               parser.getTraceData().getEndTime(), dissemination, false);
}

After the nodes have been initialized, we can run the actual trace, using the static runTrace method from the Node class, which takes each contact in the trace chronologically, and applies the desired opportunistic algorithm when two nodes meet:

// run the trace
List<Message> messages = Node.runTrace(nodes, parser.getTraceData(), false, dissemination, seed);
System.out.println("Messages: " + messages.size());

Finally, if we wish to see the statistics of the algorithm, we can use the static methods from the Stats class, as follows:

// print opportunistic algorithm statistics
System.out.println(nodes[0].getName());
System.out.println("" + Stats.computeHitRate(messages, nodes, dissemination));
System.out.println("" + Stats.computeDeliveryCost(messages, nodes, dissemination));
System.out.println("" + Stats.computeDeliveryLatency(messages, nodes, dissemination));
System.out.println("" + Stats.computeHopCount(messages, nodes, dissemination));
Clone this wiki locally