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.vector;
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.util.*;
26 * Print statistics over a vector. The vector is defined by a protocol,
27 * specified by {@value #PAR_PROT}, that has to implement
28 * {@link SingleValue}.
29 * Statistics printed are: min, max, number of samples, average, variance,
30 * number of minimal instances, number of maximal instances (using
31 * {@link IncrementalStats#toString}).
32 * @see IncrementalStats
34 public class SingleValueObserver implements Control {
37 //--------------------------------------------------------------------------
39 //--------------------------------------------------------------------------
42 * The parameter used to determine the accuracy
43 * (standard deviation) before stopping the simulation. If not
44 * defined, a negative value is used which makes sure the observer
45 * does not stop the simulation.
49 private static final String PAR_ACCURACY = "accuracy";
52 * The protocol to operate on.
55 private static final String PAR_PROT = "protocol";
58 //--------------------------------------------------------------------------
60 //--------------------------------------------------------------------------
62 /** The name of this observer in the configuration */
63 private final String name;
65 /** Accuracy for standard deviation used to stop the simulation */
66 private final double accuracy;
68 /** Protocol identifier */
69 private final int pid;
72 //--------------------------------------------------------------------------
74 //--------------------------------------------------------------------------
77 * Standard constructor that reads the configuration parameters.
78 * Invoked by the simulation engine.
79 * @param name the configuration prefix for this class
81 public SingleValueObserver(String name)
84 accuracy = Configuration.getDouble(name + "." + PAR_ACCURACY, -1);
85 pid = Configuration.getPid(name + "." + PAR_PROT);
89 //--------------------------------------------------------------------------
91 //--------------------------------------------------------------------------
94 * Print statistics over a vector. The vector is defined by a protocol,
95 * specified by {@value #PAR_PROT}, that has to implement
96 * {@link SingleValue}.
97 * Statistics printed are: min, max, number of samples, average, variance,
98 * number of minimal instances, number of maximal instances (using
99 * {@link IncrementalStats#toString}).
100 * @return true if the standard deviation is below the value of
101 * {@value #PAR_ACCURACY}, and the time of the simulation is larger then zero
102 * (i.e. it has started).
104 public boolean execute()
106 IncrementalStats stats = new IncrementalStats();
108 /* Compute max, min, average */
109 for (int i = 0; i < Network.size(); i++)
111 SingleValue v = (SingleValue)Network.get(i).getProtocol(pid);
112 stats.add( v.getValue() );
115 /* Printing statistics */
116 System.out.println(name+": "+stats);
118 /* Terminate if accuracy target is reached */
119 return (stats.getStD()<=accuracy && CommonState.getTime()>0);
122 //--------------------------------------------------------------------------