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.*;
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.
32 public class Normalizer extends VectControl
35 // --------------------------------------------------------------------------
37 // --------------------------------------------------------------------------
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.
44 private static final String PAR_L1 = "l1";
47 // --------------------------------------------------------------------------
49 // --------------------------------------------------------------------------
52 private final double l1;
55 // --------------------------------------------------------------------------
57 // --------------------------------------------------------------------------
60 * Standard constructor that reads the configuration parameters.
61 * Invoked by the simulation engine.
62 * @param prefix the configuration prefix for this class
64 public Normalizer(String prefix)
67 l1 = Configuration.getDouble(prefix + "." + PAR_L1, 1);
69 if( setter.isInteger() )
70 throw new IllegalParameterException(prefix + "." + PAR_METHOD,
71 "setter value must be floating point, instead of "+
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());
80 //--------------------------------------------------------------------------
82 //--------------------------------------------------------------------------
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
89 * @return always false
91 public boolean execute() {
94 for (int i = 0; i < Network.size(); ++i)
96 sum += getter.getDouble(i);
101 RuntimeException("Attempted to normalize all zero vector.");
103 double factor = l1 / sum;
104 for (int i = 0; i < Network.size(); ++i)
106 double val = getter.getDouble(i)*factor;
112 //--------------------------------------------------------------------------