Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / vector / VectAngle.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.core.*;
22
23 /**
24  * Observes the cosine angle between two vectors. The number which is output is
25  * the inner product divided by the product of the length of the vectors.
26  * All values are converted to double before processing.
27  * <p>
28  * This observer class can observe any protocol field containing a 
29  * primitive value, provided that the field is associated with a getter method 
30  * that reads it.
31  * The methods to be used are specified through parameter {@value #PAR_METHOD1}
32  * and {@value #PAR_METHOD2}.
33  * <p>
34  * Please refer to package {@link peersim.vector} for a detailed description of 
35  * this mechanism. 
36  */
37 public class VectAngle implements Control
38 {
39
40 // --------------------------------------------------------------------------
41 // Parameters
42 // --------------------------------------------------------------------------
43
44 /**
45  * The first protocol to be observed.
46  * @config
47  */
48 private static final String PAR_PROT1 = "protocol1";
49
50 /**
51  * The second protocol to be observed.
52  * @config
53  */
54 private static final String PAR_PROT2 = "protocol2";
55
56 /**
57  * The getter method used to obtain the values of the first protocol. 
58  * Defaults to <code>getValue</code> (for backward compatibility with previous 
59  * implementation of this class, that were based on the 
60  * {@link SingleValue} interface).
61  * Refer to the {@linkplain peersim.vector vector package description} for more 
62  * information about getters and setters.
63  * @config
64  */
65 private static final String PAR_METHOD1 = "getter1";
66
67 /**
68  * The getter method used to obtain the values of the second protocol. 
69  * Defaults to <code>getValue</code> (for backward compatibility with previous 
70  * implementation of this class, that were based on the 
71  * {@link SingleValue} interface).
72  * Refer to the {@linkplain peersim.vector vector package description} for more 
73  * information about getters and setters.
74  * @config
75  */
76 private static final String PAR_METHOD2 = "getter2";
77
78 // --------------------------------------------------------------------------
79 // Fields
80 // --------------------------------------------------------------------------
81
82 /** The prefix for this observer*/
83 private final String name;
84
85 private final Getter getter1;
86
87 private final Getter getter2;
88
89 // --------------------------------------------------------------------------
90 // Initialization
91 // --------------------------------------------------------------------------
92
93 /**
94  * Standard constructor that reads the configuration parameters.
95  * Invoked by the simulation engine.
96  * @param prefix the configuration prefix for this class
97  */
98 public VectAngle(String prefix)
99 {
100         name = prefix;
101         getter1 = new Getter(prefix,PAR_PROT1,PAR_METHOD1);
102         getter2 = new Getter(prefix,PAR_PROT2,PAR_METHOD2);
103 }
104
105 // --------------------------------------------------------------------------
106 // Methods
107 // --------------------------------------------------------------------------
108
109 /**
110  * Observes the cosine angle between two vectors. The printed values
111  * are: cosine, Eucledian norm of vect 1, Eucledian norm of vector 2,
112  * angle in radians.
113 * @return always false
114 */
115 public boolean execute() {
116
117         double sqrsum1 = 0, sqrsum2 = 0, prod = 0;
118         for (int i = 0; i < Network.size(); ++i)
119         {
120                 double v1= getter1.get(i).doubleValue();
121                 double v2= getter2.get(i).doubleValue();
122                 sqrsum1 += v1 * v1;
123                 sqrsum2 += v2 * v2;
124                 prod += v2 * v1;
125         }
126         
127         double cos = prod / Math.sqrt(sqrsum1) / Math.sqrt(sqrsum2);
128         
129         // deal with numeric errors
130         if( cos > 1 ) cos=1;
131         if( cos < -1 ) cos = -1;
132         
133         System.out.println(name+": " + cos + " "
134                         + Math.sqrt(sqrsum1) + " " + Math.sqrt(sqrsum2) + " "
135                         + Math.acos(cos));
136         return false;
137 }
138
139 //--------------------------------------------------------------------------
140
141 }