Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / dynamics / OscillatingNetwork.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.dynamics;
20
21 import peersim.config.Configuration;
22 import peersim.core.*;
23
24 /**
25  * Makes the network size oscillate.
26  * The network size will be the function of time, parameterized by this
27  * parameter. The size function is
28  * <code>avg+sin(time*pi/{@value #PAR_PERIOD})*ampl</code> where
29  * <code>avg=({@value #PAR_MAX}+{@value #PAR_MIN})/2</code> and 
30  * <code>ampl=({@value #PAR_MAX}-{@value #PAR_MIN})/2</code>.
31  * This function is independent of how many times this class is executed, that
32  * is, whenever it is executed, it takes the current time and sets the network
33  * size accordingly.
34  */
35 public class OscillatingNetwork implements Control
36 {
37
38 //--------------------------------------------------------------------------
39 //Parameters
40 //--------------------------------------------------------------------------
41
42 /**
43  * Config parameter which gives the prefix of node initializers. An arbitrary
44  * number of node initializers can be specified (Along with their parameters).
45  * These will be applied
46  * on the newly created nodes. The initializers are ordered according to
47  * alphabetical order if their ID.
48  * Example:
49  * <pre>
50 control.0 DynamicNetwork
51 control.0.init.0 RandNI
52 control.0.init.0.k 5
53 control.0.init.0.protocol somelinkable
54 ...
55  * </pre>
56  * @config
57  */
58 private static final String PAR_INIT = "init";
59
60 /**
61  * Nodes are added until the size specified by this parameter is reached. The
62  * network will never exceed this size as a result of this class.
63  * If not set, there will be no limit on the size of the network.
64  * @config
65  */
66 private static final String PAR_MAX = "maxsize";
67
68 /**
69  * Nodes are removed until the size specified by this parameter is reached. The
70  * network will never go below this size as a result of this class.
71  * Defaults to 0.
72  * @config
73  */
74 private static final String PAR_MIN = "minsize";
75
76 /**
77  * Config parameter used to define the length of one period of the oscillation.
78  * The network size will be the function of time, parameterized by this
79  * parameter. The size function is
80  * <code>avg+sin(time*pi/{@value #PAR_PERIOD})*ampl</code> where
81  * <code>avg=({@value #PAR_MAX}+{@value #PAR_MIN})/2</code> and 
82  * <code>ampl=({@value #PAR_MAX}-{@value #PAR_MIN})/2</code>.
83  * @config
84  */
85 private static final String PAR_PERIOD = "period";
86
87
88 //--------------------------------------------------------------------------
89 //Fields
90 //--------------------------------------------------------------------------
91
92 /** Period */
93 private final int period;
94
95 /** Maximum size */
96 private final int minsize;
97
98 /** Minimum size */
99 private final int maxsize;
100
101 /** New nodes initializers */
102 private final NodeInitializer[] inits;
103
104
105 //--------------------------------------------------------------------------
106 // Initialization
107 //--------------------------------------------------------------------------
108
109 /**
110  * Standard constructor that reads the configuration parameters. Invoked by the
111  * simulation engine.
112  * @param prefix
113  *          the configuration prefix for this class
114  */
115 public OscillatingNetwork(String prefix)
116 {
117
118         period = Configuration.getInt(prefix + "." + PAR_PERIOD);
119         maxsize =
120                 Configuration.getInt(
121                         prefix + "." + PAR_MAX,
122                         Integer.MAX_VALUE);
123         minsize = Configuration.getInt(prefix + "." + PAR_MIN, 0);
124
125         Object[] tmp = Configuration.getInstanceArray(prefix + "." + PAR_INIT);
126         inits = new NodeInitializer[tmp.length];
127         for (int i = 0; i < tmp.length; ++i)
128         {
129                 inits[i] = (NodeInitializer) tmp[i];
130         }
131 }
132
133 //--------------------------------------------------------------------------
134 // Methods
135 //--------------------------------------------------------------------------
136
137 /**
138  * Adds n nodes to the network. Extending classes can implement any algorithm to
139  * do that. The default algorithm adds the given number of nodes after calling
140  * all the configured initializers on them.
141  * 
142  * @param n
143  *          the number of nodes to add, must be non-negative.
144  */
145 protected void add(int n)
146 {
147         for (int i = 0; i < n; ++i) {
148                 Node newnode = (Node) Network.prototype.clone();
149                 for (int j = 0; j < inits.length; ++j) {
150                         inits[j].initialize(newnode);
151                 }
152                 Network.add(newnode);
153         }
154 }
155
156 // ------------------------------------------------------------------
157
158 /**
159  * Removes n nodes from the network. Extending classes can implement any
160  * algorithm to do that. The default algorithm removes <em>random</em>
161  * nodes <em>permanently</em> simply by calling {@link Network#remove(int)}.
162  * @param n the number of nodes to remove
163  */
164 protected void remove(int n)
165 {
166         for (int i = 0; i < n; ++i) {
167                 Network.remove(CommonState.r.nextInt(Network.size()));
168         }
169 }
170
171 // ------------------------------------------------------------------
172
173 /**
174  * Takes the current time and sets the network size according to a periodic
175  * function of time.
176  * The size function is
177  * <code>avg+sin(time*pi/{@value #PAR_PERIOD})*ampl</code> where
178  * <code>avg=({@value #PAR_MAX}+{@value #PAR_MIN})/2</code> and 
179  * <code>ampl=({@value #PAR_MAX}-{@value #PAR_MIN})/2</code>.
180  * Calls {@link #add(int)} or {@link #remove} depending on whether the size
181  * needs to be increased or decreased to get the desired size.
182  * @return always false 
183  */
184 public boolean execute()
185 {
186         long time = CommonState.getTime();
187         int amplitude = (maxsize - minsize) / 2;
188         int newsize = (maxsize + minsize) / 2 + 
189           (int) (Math.sin(((double) time) / period * Math.PI) *
190           amplitude);
191         int diff = newsize - Network.size();
192         if (diff < 0)
193                 remove(-diff);
194         else
195                 add(diff);
196         
197         return false;
198 }
199
200 }