2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.reports;
21 import peersim.core.*;
22 import peersim.config.Configuration;
23 import peersim.graph.*;
24 import peersim.util.IncrementalStats;
26 import java.util.Iterator;
29 * It tests the network for robustness to random node removal.
30 * It does not actually remove
31 * nodes, it is only an observer, so can be applied several times during the
32 * simulation. A warning though: as a side effect it <em>may
33 * shuffle the network</em> (see {@value #PAR_N}) so if this is an issue,
34 * it should not be used,
35 * or only after the simulation has finished.
37 public class RandRemoval extends GraphObserver
40 // ===================== fields =======================================
41 // ====================================================================
43 // XXX remove side effect
45 * This parameter defines the number of runs of the iterative removal procedure
46 * to get statistics. Look out: if set to a value larger than 1 then as a side
47 * effect <em>the overlay will be shuffled</em>. Defaults to 1.
50 private static final String PAR_N = "n";
54 // ===================== initialization ================================
55 // =====================================================================
58 * Standard constructor that reads the configuration parameters.
59 * Invoked by the simulation engine.
60 * @param name the configuration prefix for this class
62 public RandRemoval(String name)
65 n = Configuration.getInt(name + "." + PAR_N, 1);
68 // ====================== methods ======================================
69 // =====================================================================
72 * Prints results of node removal tests. The following experiment is
73 * repeated {@value #PAR_N} times. From the graph 50%, 51%, ..., 99% of the nodes
74 * are removed at random. For all percentages it is calculated what is
76 * clustersize (weakly connected clusters) and the number of
77 * clusters in the remaining graph.
78 * These values are averaged over the experiments, and for all 50 different
79 * percentage values a line is printed that contains the respective averages,
80 * first the average maximal cluster size, followed by the average number
82 * @return always false
84 public boolean execute()
86 if( n < 1 ) return false;
89 System.out.println(name + ":");
91 final int size = Network.size();
93 IncrementalStats[] maxClust = new IncrementalStats[steps];
94 IncrementalStats[] clustNum = new IncrementalStats[steps];
95 for (int i = 0; i < steps; ++i) {
96 maxClust[i] = new IncrementalStats();
97 clustNum[i] = new IncrementalStats();
99 for (int j = 0; j < n; ++j) {
100 PrefixSubGraph sg = new PrefixSubGraph(g);
101 IncrementalStats stats = new IncrementalStats();
102 for (int i = 0; i < steps; i++) {
103 sg.setSize(size / 2 - i * (size / 100));
104 Map clst = ga.weaklyConnectedClusters(sg);
106 Iterator it = clst.values().iterator();
107 while (it.hasNext()) {
108 stats.add(((Integer) it.next()).intValue());
110 maxClust[i].add(stats.getMax());
111 clustNum[i].add(clst.size());
113 if( j+1 < n ) Network.shuffle();
115 for (int i = 0; i < steps; ++i) {
116 System.out.println(maxClust[i].getAverage() + " "
117 + clustNum[i].getAverage());