Wednesday, March 29, 2023

Visualization of network structure on the peersim simulator

Guest Author: Nisarg Negi, M.S. Data Science, UB  

Peersim is an open-source simulator for large-scale peer-to-peer systems. It can be used to design and test large-scale machine-learning algorithms. One example is a large-scale, Support Vector Machine algorithm -- Gossip-bAseD sub-GradiEnT SVM (Source Code). The ready-to-use implementation of gossip-based protocols on the simulator is a powerful alternative to serverless Federated Learning environments. 

Visualizing the graph structure in Peersim is often a cumbersome task. The following description provides one example of how this can be accomplished. We assume that the graph will be generated using the wireKOut configuration:

network.size 5 network.node.size 5 network.node.dim 4932 network.node peersim.gossip.PegasosNode network.node.resourcepath data/sido network.node.lambda 0.0001 network.node.maxiter 1 network.node.examperiter 1 degree 2 protocol.0 peersim.core.IdleProtocol protocol.0.cache degree protocol.1 peersim.gossip.GadgetProtocol protocol.1.linkable 0 # learning rate and iter for GADGET, keep lambda smaller and iter larger protocol.1.lambda 0.0001 protocol.1.iter 1 protocol.1.prot pushsum1 protocol.1.prot pushSV protocol.1.method randomr protocol.1.param 1 init.0 peersim.dynamics.WireKOut init.0.protocol 0 init.0.k degree

For this, we had to modify some components of the peersim GraphFactory library. Since the code runs from the configuration file, more than simple editing of our code would be required.

We call GraphIO and create this graph, but we modified the library.
In the GraphFactory, after the graph is generated we call the GraphIO function and pass our randomly generated graph as a parameter. GraphIO returns the Graph saved in DOT format as below:

We saved the above configuration in a text file. On Unix systems, we can install graphviz:
sudo apt install graphviz
and utilize it to convert the text output graph to a better-visualized graph with the command:

dot -Tpng network_digraph_output.txt -o graph.png

The following code changes are required in the wireKOut function:

public static Graph wireKOut( Graph g, int k, Random r ) { final int n = g.size(); if( n < 2 ) return g; if( n <= k ) k=n-1; int[] nodes = new int[n]; for(int i=0; i<nodes.length; ++i) nodes[i]=i; for(int i=0; i<n; ++i) { int j=0; while(j<k) { int newedge = j+r.nextInt(n-j); int tmp = nodes[j]; nodes[j] = nodes[newedge]; nodes[newedge] = tmp; if( nodes[j] != i ) { g.setEdge(i,nodes[j]); j++; } } } ///Code to print the graph network try { System.out.print("\n GraphFactory:"+ g.toString()+ "\n"); peersim.graph.GraphIO.writeDOT( g,System.err); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try {//saving network digraph to file //Can use graphviz to print it, linux commands below: //sudo apt install graphviz //dot -Tpng network_digraph_output.txt -o graph.png FileOutputStream fos = new FileOutputStream("network_digraph_output.txt"); PrintStream ps = new PrintStream(fos); peersim.graph.GraphIO.writeDOT(g,ps); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return g; }