- latest changes
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses_mlp.h
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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 ats/gnunet-service-ats_addresses_mlp.h
23  * @brief ats mlp problem solver
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet-service-ats_addresses.h"
30 #if HAVE_LIBGLPK
31 #include "glpk.h"
32 #endif
33
34 #ifndef GNUNET_SERVICE_ATS_ADDRESSES_MLP_H
35 #define GNUNET_SERVICE_ATS_ADDRESSES_MLP_H
36
37 #define DEBUG_MLP GNUNET_EXTRA_LOGGING
38
39 #define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
40 #define MLP_MAX_ITERATIONS      INT_MAX
41
42 struct ATS_Peer
43 {
44   struct ATS_Peer *next;
45   struct ATS_Peer *prev;
46
47   struct GNUNET_PeerIdentity id;
48
49   /* Array of quality preferences */
50   double f_q[GNUNET_ATS_QualityPropertiesCount];
51   /* Legacy preference value */
52   double f;
53
54   /* constraint 2: 1 address per peer*/
55   unsigned int r_c2;
56
57   /* constraint 9: relativity */
58   unsigned int r_c9;
59
60   struct ATS_Address *head;
61   struct ATS_Address *tail;
62 };
63
64 /**
65  * MLP Handle
66  */
67 struct GAS_MLP_Handle
68 {
69   /**
70    * Statistics handle
71    */
72   struct GNUNET_STATISTICS_Handle *stats;
73
74   /**
75    * GLPK (MLP) problem object
76    */
77 #if HAVE_LIBGLPK
78   glp_prob *prob;
79 #else
80   void *prob;
81 #endif
82
83   double BIG_M;
84
85   /**
86    * GLPK LP control parameter
87    */
88   glp_smcp control_param_lp;
89
90   /**
91    * GLPK LP control parameter
92    */
93   glp_iocp control_param_mlp;
94
95   /**
96    * Solves the task in an regular interval
97    */
98   GNUNET_SCHEDULER_TaskIdentifier mlp_task;
99
100   /**
101    * Interval between scheduled problem solving
102    */
103   struct GNUNET_TIME_Relative exec_interval;
104
105   /**
106    * Maximum execution time per problem solving
107    */
108   struct GNUNET_TIME_Relative max_exec_duration;
109
110   /**
111    * Maximum number of LP iterations per problem solving
112    */
113   unsigned int max_iterations;
114
115   /* state information */
116
117   /**
118    * Do we need to use the LP presolver?
119    *
120    * If the problem addresses were added or removed and the last basis was we
121    * need to use the presolver.
122    * presolver_required == GNUNET_YES
123    *
124    * If values were modified, we can reuse a valid basis
125    * presolver_required == GNUNET_NO
126    */
127   int presolver_required;
128
129   /* statistics */
130
131   /**
132    * Time of last execution
133    */
134   struct GNUNET_TIME_Absolute last_execution;
135
136
137   /**
138    * How often was the LP problem solved
139    */
140   unsigned int lp_solved;
141
142   /**
143    * total duration of all lp solver executions
144    */
145   uint64_t lp_total_duration;
146
147   /**
148    * How often was the MLP problem solved
149    */
150   unsigned int mlp_solved;
151
152   /**
153    * total duration of all mlp solver executions
154    */
155   uint64_t mlp_total_duration;
156
157   unsigned int addr_in_problem;
158
159   /* Information about the problem */
160
161   struct ATS_Peer *peer_head;
162   struct ATS_Peer *peer_tail;
163
164   /* Number of peers */
165   unsigned int c_p;
166
167   /* current problem matrix */
168   /* row index array */
169   int *ia;
170   /* column index array */
171   int *ja;
172   /* column index array */
173   double *ar;
174   /* current size of the constraint matrix |indices| */
175   unsigned int cm_size;
176   unsigned int ci;
177
178   /* Row index constraint 2: */
179   unsigned int r_c2;
180   /* Row index constraint 4: minimum connections */
181   unsigned int r_c4;
182   /* Row index constraint 6: maximize diversity */
183   unsigned int r_c6;
184   /* Row index constraint 8: utilization*/
185   unsigned int r_c8;
186   /* Row index constraint 9: relativity*/
187   unsigned int r_c9;
188
189   /* column index Diversity (D) column */
190   int c_d;
191   double co_D;
192
193   /* column index Utilization (U) column */
194   int c_u;
195   double co_U;
196
197   /* column index Proportionality (R) column */
198   int c_r;
199   double co_R;
200
201   /* ATS Quality metrics
202    *
203    * array with GNUNET_ATS_QualityPropertiesCount elements
204    * contains mapping to GNUNET_ATS_Property*/
205   int q[GNUNET_ATS_QualityPropertiesCount];
206
207   /* column index quality metrics  */
208   int c_q[GNUNET_ATS_QualityPropertiesCount];
209
210   /* column index quality metrics  */
211   int r_q[GNUNET_ATS_QualityPropertiesCount];
212
213   /* quality metric coefficients*/
214   double co_Q[GNUNET_ATS_QualityPropertiesCount];
215
216   /* number of quality metrics */
217   int m_q;
218
219   /* ATS ressource costs
220    *
221    * array with GNUNET_ATS_QualityPropertiesCount elements
222    * contains mapping to GNUNET_ATS_Property*/
223   int rc[GNUNET_ATS_QualityPropertiesCount];
224
225   /* column index ressource costs  */
226   int c_rc[GNUNET_ATS_QualityPropertiesCount];
227
228   /* ressource costs coefficients*/
229   double co_RC[GNUNET_ATS_QualityPropertiesCount];
230
231   /* number of quality metrics */
232   int m_rc;
233
234   /* minimum bandwidth assigned to an address */
235   unsigned int b_min;
236
237   /* minimum number of addresses with bandwidth assigned */
238   unsigned int n_min;
239 };
240
241
242 /**
243  * Address specific MLP information
244  */
245 struct MLP_information
246 {
247   /* bandwidth column index */
248   signed int c_b;
249
250   /* address usage column */
251   signed int c_n;
252
253   /* row indexes */
254
255   /* constraint 1: bandwidth capping */
256   unsigned int r_c1;
257
258   /* constraint 3: minimum bandwidth */
259   unsigned int r_c3;
260 };
261
262
263 /**
264  * Init the MLP problem solving component
265  *
266  * @param cfg configuration handle
267  * @param stats the GNUNET_STATISTICS handle
268  * @param max_duration maximum numbers of iterations for the LP/MLP Solver
269  * @param max_iterations maximum time limit for the LP/MLP Solver
270  * @return struct GAS_MLP_Handle * on success, NULL on fail
271  */
272 struct GAS_MLP_Handle *
273 GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
274               const struct GNUNET_STATISTICS_Handle *stats,
275               struct GNUNET_TIME_Relative max_duration,
276               unsigned int max_iterations);
277
278
279 /**
280  * Updates a single address in the MLP problem
281  *
282  * If the address did not exist before in the problem:
283  * The MLP problem has to be recreated and the problem has to be resolved
284  *
285  * Otherwise the addresses' values can be updated and the existing base can
286  * be reused
287  *
288  * @param mlp the MLP Handle
289  * @param addresses the address hashmap
290  *        the address has to be already added from the hashmap
291  * @param address the address to update
292  */
293 void
294 GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address);
295
296
297 /**
298  * Deletes a single address in the MLP problem
299  *
300  * The MLP problem has to be recreated and the problem has to be resolved
301  *
302  * @param mlp the MLP Handle
303  * @param addresses the address hashmap
304  *        the address has to be already removed from the hashmap
305  * @param address the address to delete
306  */
307 void
308 GAS_mlp_address_delete (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address);
309
310
311 /**
312  * Changes the preferences for a peer in the MLP problem
313  *
314  * @param mlp the MLP Handle
315  * @param peer the peer
316  * @param kind the kind to change the preference
317  * @param float the score
318  */
319 void
320 GAS_mlp_address_change_preference (struct GAS_MLP_Handle *mlp,
321                                    const struct GNUNET_PeerIdentity *peer,
322                                    enum GNUNET_ATS_PreferenceKind kind,
323                                    float score);
324
325
326 /**
327  * Shutdown the MLP problem solving component
328  */
329 void
330 GAS_mlp_done ();
331
332 #endif
333 /* end of gnunet-service-ats_addresses_mlp.h */