Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / reports / GraphStats.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18                 
19 package peersim.reports;
20
21 import peersim.config.Configuration;
22 import peersim.graph.GraphAlgorithms;
23 import peersim.util.IncrementalStats;
24
25 /**
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.
31 * @see BallExpansion
32 * @see Clustering
33 */
34 public class GraphStats extends GraphObserver {
35
36
37 // ===================== fields =======================================
38 // ====================================================================
39
40 /** 
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.
49 * Defaults to zero.
50 * @config
51 */
52 private static final String PAR_NL = "nl";
53
54 /** 
55 * The number of nodes to use to sample
56 * average clustering.
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.
60 * Defaults to zero.
61 * @config
62 */
63 private static final String PAR_NC = "nc";
64
65 private final int nc;
66
67 private final int nl;
68
69
70 // ===================== initialization ================================
71 // =====================================================================
72
73
74 /**
75  * Standard constructor that reads the configuration parameters.
76  * Invoked by the simulation engine.
77  * @param name the configuration prefix for this class
78  */
79 public GraphStats(String name) {
80
81         super(name);
82         nl = Configuration.getInt(name+"."+PAR_NL,0);
83         nc = Configuration.getInt(name+"."+PAR_NC,0);
84 }
85
86
87 // ====================== methods ======================================
88 // =====================================================================
89
90 /**
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
101 * @see BallExpansion
102 * @see Clustering
103 */
104 public boolean execute() {
105         
106         System.out.print(name+": ");
107         
108         IncrementalStats stats = new IncrementalStats();
109         updateGraph();
110         
111         if( nc != 0 )
112         {
113                 stats.reset();
114                 final int n = ( nc<0 ? g.size() : nc );
115                 for(int i=0; i<n && i<g.size(); ++i)
116                 {
117                         stats.add(GraphAlgorithms.clustering(g,i));
118                 }
119                 System.out.print(stats.getAverage()+" ");
120         }
121         
122         if( nl != 0 )
123         {
124                 stats.reset();
125                 final int n = ( nl<0 ? g.size() : nl );
126                 outerloop:
127                 for(int i=0; i<n && i<g.size(); ++i)
128                 {
129                         ga.dist(g,i);
130                         for(int j=0; j<g.size(); ++j)
131                         {
132                                 if( j==i ) continue;
133                                 if (ga.d[j] == -1)
134                                 {
135                                         stats.add(Double.POSITIVE_INFINITY);
136                                         break outerloop;
137                                 }
138                                 else
139                                         stats.add(ga.d[j]); 
140                         }
141                 }
142                 System.out.print(stats.getAverage());
143         }
144         
145         System.out.println();
146         return false;
147 }
148
149 }
150