Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / vector / Normalizer.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
24 /**
25  * Normalizes the values of a protocol vector.
26  * The normalization is based on the L1 norm, that is, the sum of the
27  * absolute values of the vector elements. Parameter {@value #PAR_L1} defines
28  * the L1 norm that the vector will have after normalization.
29  * @see VectControl
30  * @see peersim.vector
31  */
32 public class Normalizer extends VectControl
33 {
34
35 // --------------------------------------------------------------------------
36 // Parameters
37 // --------------------------------------------------------------------------
38
39 /**
40  * The L1 norm (sum of absolute values) to normalize to. After the operation the
41  * L1 norm will be the value given here. Defaults to 1.
42  * @config
43  */
44 private static final String PAR_L1 = "l1";
45
46
47 // --------------------------------------------------------------------------
48 // Fields
49 // --------------------------------------------------------------------------
50
51 /** L1 norm */
52 private final double l1;
53
54
55 // --------------------------------------------------------------------------
56 // Initialization
57 // --------------------------------------------------------------------------
58
59 /**
60  * Standard constructor that reads the configuration parameters.
61  * Invoked by the simulation engine.
62  * @param prefix the configuration prefix for this class
63  */
64 public Normalizer(String prefix)
65 {
66         super(prefix);
67         l1 = Configuration.getDouble(prefix + "." + PAR_L1, 1);
68         
69         if( setter.isInteger() ) 
70                 throw new IllegalParameterException(prefix + "." + PAR_METHOD,
71                         "setter value must be floating point, instead of "+
72                         setter.getType());
73                         
74         if( setter.getType() !=  getter.getType() )
75                 throw new IllegalParameterException(prefix + "." + PAR_GETTER,
76                 "getter and setter must have the same numeric type, "+
77                 "but we have "+setter.getType()+" and "+getter.getType());
78 }
79
80 //--------------------------------------------------------------------------
81 //Methods
82 //--------------------------------------------------------------------------
83
84 /**
85  * Makes the sum of the absolute values (L1 norm) equal to the value
86  * given in the configuration parameter {@value #PAR_L1}. If the value is
87  * negative, the L1 norm will be the absolute value and the vector elements
88  * change sign.
89  * @return always false
90  */
91 public boolean execute() {
92         
93         double sum = 0.0;
94         for (int i = 0; i < Network.size(); ++i)
95         {
96                 sum += getter.getDouble(i);
97         }
98         if (sum == 0.0)
99         {
100                 throw new
101                 RuntimeException("Attempted to normalize all zero vector.");
102         }
103         double factor = l1 / sum;
104         for (int i = 0; i < Network.size(); ++i)
105         {
106                 double val = getter.getDouble(i)*factor;
107                 setter.set(i,val);
108         }
109         return false;
110 }
111
112 //--------------------------------------------------------------------------
113
114 }