config fix
[oweals/gnunet.git] / src / sensor / plugin_sensor_model_gaussian.c
1 /*
2  * This file is part of GNUnet
3  * (C) 2013 Christian Grothoff (and other contributing authors)
4  *
5  * GNUnet is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 3, or (at your
8  * option) any later version.
9  *
10  * GNUnet is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with GNUnet; see the file COPYING.  If not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * @file sensor/plugin_sensor_model_gaussian.c
23  * @brief Gaussian model for sensor analysis
24  * @author Omar Tarabai
25  */
26
27 #include "platform.h"
28 #include "gnunet_sensor_model_plugin.h"
29 #include "gnunet_sensor_service.h"
30 #include "sensor.h"
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-model-gaussian", __VA_ARGS__)
33
34 /*
35  * Plugin state information
36  */
37 struct Plugin
38 {
39
40   /*
41    * Configuration handle
42    */
43   const struct GNUNET_CONFIGURATION_Handle *cfg;
44
45   /*
46    * Number of initial readings to be used for training only
47    */
48   int training_window;
49
50   /*
51    * Number of standard deviations considered within "normal"
52    */
53   int confidence_interval;
54
55 };
56
57 /*
58  * State of single model instance
59  */
60 struct Model
61 {
62
63   /*
64    * Pointer to the plugin state
65    */
66   struct Plugin *plugin;
67
68   /*
69    * Number of readings so far
70    */
71   int n;
72
73   /*
74    * Sum of readings
75    */
76   long double sum;
77
78   /*
79    * Sum square of readings
80    */
81   long double sumsq;
82
83 };
84
85 static void
86 update_sums (struct Model *model, double val)
87 {
88   model->sum += val;
89   model->sumsq += val * val;
90   model->n ++;
91 }
92
93 /*
94  * Feed a new value to a model
95  *
96  * @param cls closure (model state)
97  * @param val value to be fed to the model
98  * @return #GNUNET_YES in case of a detected outlier, #GNUNET_NO otherwise
99  */
100 static int
101 sensor_gaussian_model_feed (void *cls, double val)
102 {
103   struct Model *model = cls;
104   struct Plugin *plugin = model->plugin;
105   long double mean;
106   long double stddev;
107   long double allowed_variance;
108
109   if (model->n < plugin->training_window)
110   {
111     update_sums(model, val);
112     return GNUNET_NO;
113   }
114   mean = model->sum / model->n;
115   stddev = sqrt(
116       (model->sumsq - 2 * mean * model->sum + model->n * mean * mean)
117       /
118       (model->n - 1)
119     );
120   allowed_variance = (plugin->confidence_interval * stddev);
121   if ((val < (mean - allowed_variance)) ||
122       (val > (mean + allowed_variance)))
123     return GNUNET_YES;
124   return GNUNET_NO;
125 }
126
127 /*
128  * Destroy a model instance
129  *
130  * @param cls closure (model state)
131  */
132 static void
133 sensor_gaussian_model_destroy_model (void *cls)
134 {
135   struct Model *model = cls;
136
137   GNUNET_free(model);
138 }
139
140 /*
141  * Create a model instance
142  *
143  * @param cls closure (plugin state)
144  * @return model state to be used for later calls
145  */
146 static void *
147 sensor_gaussian_model_create_model (void *cls)
148 {
149   struct Plugin *plugin = cls;
150   struct Model *model;
151
152   model = GNUNET_new(struct Model);
153   model->plugin = plugin;
154   return model;
155 }
156
157 /*
158  * Entry point for the plugin.
159  *
160  * @param cls The struct GNUNET_CONFIGURATION_Handle.
161  * @return NULL on error, otherwise the plugin context
162  */
163 void *
164 libgnunet_plugin_sensor_model_gaussian_init (void *cls)
165 {
166   static struct Plugin plugin;
167   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
168   struct GNUNET_SENSOR_ModelFunctions *api;
169   unsigned long long num;
170
171   if (NULL != plugin.cfg)
172     return NULL;                /* can only initialize once! */
173   memset (&plugin, 0, sizeof (struct Plugin));
174   plugin.cfg = cfg;
175   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg,
176       "sensor-model-gaussian", "TRAINING_WINDOW", &num))
177   {
178     LOG (GNUNET_ERROR_TYPE_ERROR,
179         _("Missing `TRAINING_WINDOW' value in configuration.\n"));
180     return NULL;
181   }
182   plugin.training_window = (int) num;
183   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg,
184         "sensor-model-gaussian", "CONFIDENCE_INTERVAL", &num))
185   {
186     LOG (GNUNET_ERROR_TYPE_ERROR,
187         _("Missing `CONFIDENCE_INTERVAL' value in configuration.\n"));
188     return NULL;
189   }
190   plugin.confidence_interval = (int) num;
191   api = GNUNET_new (struct GNUNET_SENSOR_ModelFunctions);
192   api->cls = &plugin;
193   api->create_model = &sensor_gaussian_model_create_model;
194   api->destroy_model = &sensor_gaussian_model_destroy_model;
195   api->feed_model = &sensor_gaussian_model_feed;
196   LOG(GNUNET_ERROR_TYPE_DEBUG, "Gaussian model plugin is running.\n");
197   return api;
198 }
199
200 /*
201  * Exit point from the plugin.
202  *
203  * @param cls The plugin context (as returned by "init")
204  * @return Always NULL
205  */
206 void *
207 libgnunet_plugin_sensor_model_gaussian_done (void *cls)
208 {
209   struct GNUNET_SENSOR_ModelFunctions *api = cls;
210   struct Plugin *plugin = api->cls;
211
212   plugin->cfg = NULL;
213   GNUNET_free (api);
214   LOG (GNUNET_ERROR_TYPE_DEBUG, "Guassian model plugin is finished\n");
215   return NULL;
216
217 }
218
219 /* end of plugin_sensor_model_gaussian.c */