Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / vector / SingleValueObserver.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.vector;
20
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.util.*;
24
25 /**
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
33 */
34 public class SingleValueObserver implements Control {
35
36
37 //--------------------------------------------------------------------------
38 //Parameters
39 //--------------------------------------------------------------------------
40
41 /** 
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.
46  * @see #execute
47  *  @config
48  */
49 private static final String PAR_ACCURACY = "accuracy";
50
51 /**
52  * The protocol to operate on.
53  * @config
54  */
55 private static final String PAR_PROT = "protocol";
56
57
58 //--------------------------------------------------------------------------
59 // Fields
60 //--------------------------------------------------------------------------
61
62 /** The name of this observer in the configuration */
63 private final String name;
64
65 /** Accuracy for standard deviation used to stop the simulation */
66 private final double accuracy;
67
68 /** Protocol identifier */
69 private final int pid;
70
71
72 //--------------------------------------------------------------------------
73 // Constructor
74 //--------------------------------------------------------------------------
75
76 /**
77  * Standard constructor that reads the configuration parameters.
78  * Invoked by the simulation engine.
79  * @param name the configuration prefix for this class
80  */
81 public SingleValueObserver(String name)
82 {
83         this.name = name;
84         accuracy = Configuration.getDouble(name + "." + PAR_ACCURACY, -1);
85         pid = Configuration.getPid(name + "." + PAR_PROT);
86 }
87
88
89 //--------------------------------------------------------------------------
90 // Methods
91 //--------------------------------------------------------------------------
92
93 /**
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).
103  */
104 public boolean execute()
105 {
106         IncrementalStats stats = new IncrementalStats();
107         
108         /* Compute max, min, average */
109         for (int i = 0; i < Network.size(); i++)
110         {
111                 SingleValue v = (SingleValue)Network.get(i).getProtocol(pid);
112                 stats.add( v.getValue() );
113         }
114
115         /* Printing statistics */
116         System.out.println(name+": "+stats);
117
118         /* Terminate if accuracy target is reached */
119         return (stats.getStD()<=accuracy && CommonState.getTime()>0);
120 }
121
122 //--------------------------------------------------------------------------
123
124 }