Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / vector / UniformDistribution.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.dynamics.*;
24
25 /**
26  * Initializes the values drawing uniform random samples from the range
27  * [{@value #PAR_MIN}, {@value #PAR_MAX}[.
28  * @see VectControl
29  * @see peersim.vector
30  */
31 public class UniformDistribution extends VectControl implements NodeInitializer
32 {
33
34 //--------------------------------------------------------------------------
35 //Parameter names
36 //--------------------------------------------------------------------------
37
38 /**
39  * The upper bound of the uniform random variable, exclusive.
40  * @config
41  */
42 private static final String PAR_MAX = "max";
43
44 /**
45  * The lower bound of the uniform
46  * random variable, inclusive. Defaults to -{@value #PAR_MAX}.
47  * @config
48  */
49 private static final String PAR_MIN = "min";
50
51 // --------------------------------------------------------------------------
52 // Fields
53 // --------------------------------------------------------------------------
54
55 /** Minimum value */
56 private final Number min;
57
58 /** Maximum value */
59 private final Number max;
60
61 // --------------------------------------------------------------------------
62 // Initialization
63 // --------------------------------------------------------------------------
64
65 /**
66  * Standard constructor that reads the configuration parameters.
67  * Invoked by the simulation engine.
68  * @param prefix the configuration prefix for this class
69  */
70 public UniformDistribution(String prefix)
71 {
72         super(prefix);
73         
74         // Read parameters based on type
75         if (setter.isInteger()) {
76                 max=Long.valueOf(Configuration.getLong(prefix + "." + PAR_MAX));
77                 min=Long.valueOf(Configuration.getLong(prefix + "." + PAR_MIN, 
78                                 -max.longValue()));
79         } else { // we know it's double or float
80                 max = new Double(Configuration.getDouble(prefix+"."+PAR_MAX));
81                 min = new Double(Configuration.getDouble(prefix+"."+PAR_MIN, 
82                                 -max.doubleValue()));
83         }
84 }
85
86 // --------------------------------------------------------------------------
87 // Methods
88 // --------------------------------------------------------------------------
89
90 /**
91  * Initializes the values drawing uniform random samples from the range
92  * [{@value #PAR_MIN}, {@value #PAR_MAX}[.
93  * @return always false
94  */
95 public boolean execute() {
96
97         if(setter.isInteger())
98         {
99                 long d = max.longValue() - min.longValue();
100                 for (int i = 0; i < Network.size(); ++i)
101                 {
102                         setter.set(i,CommonState.r.nextLong(d)+min.longValue());
103                 }
104         }
105         else
106         {
107                 double d = max.doubleValue() - min.doubleValue();
108                 for (int i = 0; i < Network.size(); ++i)
109                 {
110                         setter.set(i,CommonState.r.nextDouble()*d+
111                         min.doubleValue());
112                 }
113         }
114
115         return false;
116 }
117
118 // --------------------------------------------------------------------------
119
120 /**
121  * Initializes the value drawing a uniform random sample from the range
122  * [{@value #PAR_MIN}, {@value #PAR_MAX}[.
123  * @param n the node to initialize
124  */
125 public void initialize(Node n) {
126
127         if( setter.isInteger() )
128         {
129                 long d = max.longValue() - min.longValue();
130                 setter.set(n,CommonState.r.nextLong(d) + min.longValue());
131         }
132         else
133         {
134                 double d = max.doubleValue() - min.doubleValue();
135                 setter.set(n,CommonState.r.nextDouble()*d);
136         }
137 }
138
139 // --------------------------------------------------------------------------
140
141 }