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.config.Configuration;
22 import peersim.graph.GraphAlgorithms;
23 import peersim.util.IncrementalStats;
26 * Prints reports on the graph like average clustering and average path length,
27 * based on random sampling of the nodes.
28 * In fact its functionality is a subset of the union of {@link Clustering}
29 * and {@link BallExpansion}, and therefore is redundant,
30 * but it is there for historical reasons.
34 public class GraphStats extends GraphObserver {
37 // ===================== fields =======================================
38 // ====================================================================
41 * The number of nodes to use for
42 * sampling average path length.
43 * Statistics are printed over a set of node pairs.
44 * To create the set of pairs, we select the given number of different nodes
45 * first, and then pair all these nodes with every other node in the network.
46 * If zero is given, then no statistics
47 * will be printed about path length. If a negative value is given then
48 * the value is the full size of the graph.
52 private static final String PAR_NL = "nl";
55 * The number of nodes to use to sample
57 * If zero is given, then no statistics
58 * will be printed about clustering. If a negative value is given then
59 * the value is the full size of the graph.
63 private static final String PAR_NC = "nc";
70 // ===================== initialization ================================
71 // =====================================================================
75 * Standard constructor that reads the configuration parameters.
76 * Invoked by the simulation engine.
77 * @param name the configuration prefix for this class
79 public GraphStats(String name) {
82 nl = Configuration.getInt(name+"."+PAR_NL,0);
83 nc = Configuration.getInt(name+"."+PAR_NC,0);
87 // ====================== methods ======================================
88 // =====================================================================
91 * Returns statistics over minimal path length and clustering.
92 * The output is the average over the set of
93 * clustering coefficients of randomly selected nodes, and the
94 * set of distances from randomly selected nodes to all the other nodes.
95 * The output is always concatenated in one line, containing zero, one or two
96 * numbers (averages) as defined by the config parameters.
97 * Note that the path length between a pair of nodes can be infinite, in which
98 * case the statistics will reflect this (the average will be infinite, etc).
99 * See also the configuration parameters.
100 * @return always false
104 public boolean execute() {
106 System.out.print(name+": ");
108 IncrementalStats stats = new IncrementalStats();
114 final int n = ( nc<0 ? g.size() : nc );
115 for(int i=0; i<n && i<g.size(); ++i)
117 stats.add(GraphAlgorithms.clustering(g,i));
119 System.out.print(stats.getAverage()+" ");
125 final int n = ( nl<0 ? g.size() : nl );
127 for(int i=0; i<n && i<g.size(); ++i)
130 for(int j=0; j<g.size(); ++j)
135 stats.add(Double.POSITIVE_INFINITY);
142 System.out.print(stats.getAverage());
145 System.out.println();