major change to solver api: split _update function since it combined 3 different...
[oweals/gnunet.git] / src / ats / gnunet-service-ats-solver_mlp.c
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-solver_mlp.c
23  * @brief ats mlp problem solver
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-ats-solver_mlp.h"
30 #include "gnunet_statistics_service.h"
31 #include "glpk.h"
32
33 /**
34  *
35  * NOTE: Do not modify this documentation. This documentation is based on
36  * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
37  * use build_txt.sh to generate plaintext output
38  *
39  *    The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
40  *    optimizing an mixed integer programming problem. The MLP solver uses a
41  *    number of constraints to find the best adddress for a peer and an optimal
42  *    bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
43  *    MLP problem.
44  *
45  *    We defined a constraint system to find an optimal bandwidth assignment.
46  *    This constraint system uses as an input data addresses, bandwidth quotas,
47  *    preferences and quality values. This constraint system is stored in an
48  *    matrix based equotation system.
49  *
50  *   5 Using GLPK
51  *
52  *    A (M)LP problem consists of a target function to optimizes, constraints
53  *    and rows and columns. FIXME GLP uses three arrays to index the matrix: two
54  *    integer arrays storing the row and column indices in the matrix and an
55  *    float array to store the coeeficient.
56  *
57  *    To solve the problem we first find an initial solution for the LP problem
58  *    using the LP solver and then find an MLP solution based on this solution
59  *    using the MLP solver.
60  *
61  *    Solving (M)LP problems has the property that finding an initial solution
62  *    for the LP problem is computationally expensive and finding the MLP
63  *    solution is cheaper. This is especially interesting an existing LP
64  *    solution can be reused if only coefficients in the matrix have changed
65  *    (addresses updated). Only when the problem size changes (addresses added
66  *    or deleted) a new LP solution has to be found.
67  *
68  *    Intended usage
69  *    The mlp solver solves the bandwidth assignment problem only on demand when
70  *    an address suggestion is requested. When an address is requested mlp the
71  *    solves the mlp problem and if the active address or the bandwidth assigned
72  *    changes it calls the callback to addresses. The mlp solver gets notified
73  *    about new addresses (adding sessions), removed addresses (address
74  *    deletions) and address updates. To benefit from the mlp properties
75  *    mentioned in section 5 the solver rembers if since the last solution
76  *    addresses were added or deleted (problem size changed, problem has to be
77  *    rebuild and solved from sratch) or if addresses were updated and the
78  *    existing solution can be reused.
79  *
80  *     5.1 Input data
81  *
82  *    The quotas for each network segment are passed by addresses. MLP can be
83  *    adapted using configuration settings and uses the following parameters:
84  *      * MLP_MAX_DURATION:
85  *        Maximum duration for a MLP solution procees (default: 3 sec.)
86  *      * MLP_MAX_DURATION:
87  *        Maximum number of iterations for a MLP solution process (default:
88  *        1024)
89  *      * MLP_MIN_CONNECTIONS:
90  *        Minimum number of desired connections (default: 4)
91  *      * MLP_MIN_BANDWIDTH:
92  *        Minimum amount of bandwidth assigned to an address (default: 1024)
93  *      * MLP_COEFFICIENT_D:
94  *        Diversity coefficient (default: 1.0)
95  *      * MLP_COEFFICIENT_R:
96  *        Relativity coefficient (default: 1.0)
97  *      * MLP_COEFFICIENT_U:
98  *        Utilization coefficient (default: 1.0)
99  *      * MLP_COEFFICIENT_D:
100  *        Diversity coefficient (default: 1.0)
101  *      * MLP_COEFFICIENT_QUALITY_DELAY:
102  *        Quality delay coefficient (default: 1.0)
103  *      * MLP_COEFFICIENT_QUALITY_DISTANCE:
104  *        Quality distance coefficient (default: 1.0)
105  *      * MLP_COEFFICIENT_QUALITY_DISTANCE:
106  *        Quality distance coefficient (default: 1.0)
107  *      * MLP_COEFFICIENT_QUALITY_DISTANCE:
108  *        Quality distance coefficient (default: 1.0)
109  *      * MLP_COEFFICIENT_QUALITY_DISTANCE:
110  *        Quality distance coefficient (default: 1.0)
111  *
112  *     5.2 Data structures used
113  *
114  *    mlp has for each known peer a struct ATS_Peer containing information about
115  *    a specific peer. The address field solver_information contains information
116  *    about the mlp properties of this address.
117  *
118  *     5.3 Initializing
119  *
120  *    During initialization mlp initializes the GLPK libray used to solve the
121  *    MLP problem: it initializes the glpk environment and creates an initial LP
122  *    problem. Next it loads the configuration values from the configuration or
123  *    uses the default values configured in -addresses_mlp.h. The quotas used
124  *    are given by addresses but may have to be adjusted. mlp uses a upper limit
125  *    for the bandwidth assigned called BIG M and a minimum amount of bandwidth
126  *    an address gets assigned as well as a minium desired number of
127  *    connections. If the configured quota is bigger than BIG M, it is reduced
128  *    to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
129  *    *MLP_MIN_BANDWIDTH it is increased to this value.
130  *
131  *     5.4 Shutdown
132
133  */
134
135 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
136
137 /**
138  * Print debug output for mlp problem creation
139  */
140 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
141
142 /**
143  * Enable GLPK verbose output
144  */
145 #define VERBOSE_GLPK GNUNET_NO
146
147 /**
148  * Maximize bandwidth assigned
149  *
150  * This option can be used to test if problem can be solved at all without
151  * optimizing for utility, diversity or relativity
152  *
153  */
154 #define MAXIMIZE_FOR_BANDWIDTH_ASSIGNED GNUNET_NO
155
156 /**
157  * Intercept GLPK terminal output
158  * @param info the mlp handle
159  * @param s the string to print
160  * @return 0: glpk prints output on terminal, 0 != surpress output
161  */
162 static int
163 mlp_term_hook (void *info, const char *s)
164 {
165   /* Not needed atm struct MLP_information *mlp = info; */
166   LOG (GNUNET_ERROR_TYPE_DEBUG, "%s", s);
167   return 1;
168 }
169
170
171 /**
172  * Reset peers for next problem creation
173  *
174  * @param cls not used
175  * @param key the key
176  * @param value ATS_Peer
177  * @return GNUNET_OK
178  */
179 static int
180 reset_peers (void *cls, const struct GNUNET_HashCode * key, void *value)
181  {
182          struct ATS_Peer *peer = value;
183          peer->processed = GNUNET_NO;
184          return GNUNET_OK;
185  }
186
187 /**
188  * Delete the MLP problem and free the constrain matrix
189  *
190  * @param mlp the MLP handle
191  */
192 static void
193 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
194 {
195         int c;
196   if (mlp == NULL)
197         return;
198         if (mlp->p.prob != NULL)
199         {
200                 glp_delete_prob(mlp->p.prob);
201                 mlp->p.prob = NULL;
202         }
203
204         /* delete row index */
205         if (mlp->p.ia != NULL)
206         {
207                 GNUNET_free (mlp->p.ia);
208                 mlp->p.ia = NULL;
209         }
210
211         /* delete column index */
212         if (mlp->p.ja != NULL)
213         {
214                 GNUNET_free (mlp->p.ja);
215                 mlp->p.ja = NULL;
216         }
217
218         /* delete coefficients */
219         if (mlp->p.ar != NULL)
220         {
221                 GNUNET_free (mlp->p.ar);
222                 mlp->p.ar = NULL;
223         }
224         mlp->p.ci = 0;
225         mlp->p.prob = NULL;
226
227   mlp->p.c_d = MLP_UNDEFINED;
228   mlp->p.c_r = MLP_UNDEFINED;
229   mlp->p.r_c2 = MLP_UNDEFINED;
230   mlp->p.r_c4 = MLP_UNDEFINED;
231   mlp->p.r_c6 = MLP_UNDEFINED;
232   mlp->p.r_c9 = MLP_UNDEFINED;
233   for (c = 0; c < mlp->pv.m_q ; c ++)
234         mlp->p.r_q[c] = MLP_UNDEFINED;
235   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c ++)
236         mlp->p.r_quota[c] = MLP_UNDEFINED;
237   mlp->p.ci = MLP_UNDEFINED;
238
239
240   GNUNET_CONTAINER_multihashmap_iterate (mlp->requested_peers, &reset_peers, NULL);
241 }
242
243
244 /**
245  * Translate ATS properties to text
246  * Just intended for debugging
247  *
248  * @param ats_index the ATS index
249  * @return string with result
250  */
251 const char *
252 mlp_ats_to_string (int ats_index)
253 {
254   switch (ats_index) {
255     case GNUNET_ATS_ARRAY_TERMINATOR:
256       return "GNUNET_ATS_ARRAY_TERMINATOR";
257     case GNUNET_ATS_UTILIZATION_UP:
258       return "GNUNET_ATS_UTILIZATION_UP";
259     case GNUNET_ATS_UTILIZATION_DOWN:
260       return "GNUNET_ATS_UTILIZATION_DOWN";
261     case GNUNET_ATS_COST_LAN:
262       return "GNUNET_ATS_COST_LAN";
263     case GNUNET_ATS_COST_WAN:
264       return "GNUNET_ATS_COST_LAN";
265     case GNUNET_ATS_COST_WLAN:
266       return "GNUNET_ATS_COST_WLAN";
267     case GNUNET_ATS_NETWORK_TYPE:
268       return "GNUNET_ATS_NETWORK_TYPE";
269     case GNUNET_ATS_QUALITY_NET_DELAY:
270       return "GNUNET_ATS_QUALITY_NET_DELAY";
271     case GNUNET_ATS_QUALITY_NET_DISTANCE:
272       return "GNUNET_ATS_QUALITY_NET_DISTANCE";
273     default:
274       GNUNET_break (0);
275       return "unknown";
276   }
277 }
278
279 /**
280  * Translate glpk status error codes to text
281  * @param retcode return code
282  * @return string with result
283  */
284 const char *
285 mlp_status_to_string (int retcode)
286 {
287   switch (retcode) {
288     case GLP_UNDEF:
289       return "solution is undefined";
290     case GLP_FEAS:
291       return "solution is feasible";
292     case GLP_INFEAS:
293       return "solution is infeasible";
294     case GLP_NOFEAS:
295       return "no feasible solution exists";
296     case GLP_OPT:
297       return "solution is optimal";
298     case GLP_UNBND:
299       return "solution is unbounded";
300     default:
301       GNUNET_break (0);
302       return "unknown error";
303   }
304 }
305
306 /**
307  * Translate glpk solver error codes to text
308  * @param retcode return code
309  * @return string with result
310  */
311 const char *
312 mlp_solve_to_string (int retcode)
313 {
314   switch (retcode) {
315     case 0:
316       return "ok";
317     case GLP_EBADB:
318       return "invalid basis";
319     case GLP_ESING:
320       return "singular matrix";
321     case GLP_ECOND:
322       return "ill-conditioned matrix";
323     case GLP_EBOUND:
324       return "invalid bounds";
325     case GLP_EFAIL:
326       return "solver failed";
327     case GLP_EOBJLL:
328       return "objective lower limit reached";
329     case GLP_EOBJUL:
330       return "objective upper limit reached";
331     case GLP_EITLIM:
332       return "iteration limit exceeded";
333     case GLP_ETMLIM:
334       return "time limit exceeded";
335     case GLP_ENOPFS:
336       return "no primal feasible solution";
337     case GLP_ENODFS:
338       return "no dual feasible solution";
339     case GLP_EROOT:
340       return "root LP optimum not provided";
341     case GLP_ESTOP:
342       return "search terminated by application";
343     case GLP_EMIPGAP:
344       return "relative mip gap tolerance reached";
345     case GLP_ENOFEAS:
346       return "no dual feasible solution";
347     case GLP_ENOCVG:
348       return "no convergence";
349     case GLP_EINSTAB:
350       return "numerical instability";
351     case GLP_EDATA:
352       return "invalid data";
353     case GLP_ERANGE:
354       return "result out of range";
355     default:
356       GNUNET_break (0);
357       return "unknown error";
358   }
359 }
360
361 /**
362  * Extract an ATS performance info from an address
363  *
364  * @param address the address
365  * @param type the type to extract in HBO
366  * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
367  */
368 static int
369 get_performance_info (struct ATS_Address *address, uint32_t type)
370 {
371         int c1;
372         GNUNET_assert (NULL != address);
373
374         if ((NULL == address->atsi) || (0 == address->atsi_count))
375                         return GNUNET_ATS_VALUE_UNDEFINED;
376
377         for (c1 = 0; c1 < address->atsi_count; c1++)
378         {
379                         if (ntohl(address->atsi[c1].type) == type)
380                                 return ntohl(address->atsi[c1].value);
381         }
382         return GNUNET_ATS_VALUE_UNDEFINED;
383 }
384
385
386 struct CountContext
387 {
388         struct GNUNET_CONTAINER_MultiHashMap * peers;
389         int result;
390 };
391
392 static int
393 mlp_create_problem_count_addresses_it (void *cls, const struct GNUNET_HashCode *key, void *value)
394 {
395         struct CountContext *cctx = cls;
396   /* Check if we have to add this peer due to a pending request */
397   if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(cctx->peers, key))
398         cctx->result++;
399   return GNUNET_OK;
400 }
401
402 static int mlp_create_problem_count_addresses (
403                 struct GNUNET_CONTAINER_MultiHashMap * peers,
404                 const struct GNUNET_CONTAINER_MultiHashMap * addresses)
405 {
406         struct CountContext cctx;
407         cctx.peers = peers;
408         cctx.result = 0;
409   GNUNET_CONTAINER_multihashmap_iterate (addresses, &mlp_create_problem_count_addresses_it, &cctx);
410   return cctx.result;
411 }
412
413
414 /**
415  * Updates an existing value in the matrix
416  *
417  * Extract the row, updates the value and updates the row in the problem
418  *
419  * @param p the mlp problem
420  * @param row the row to create the value in
421  * @param col the column to create the value in
422  * @param val the value to set
423  * @param line calling line for debbuging
424  */
425 static void
426 mlp_create_problem_update_value (struct MLP_Problem *p,
427                                                                                                                         int row, int col, double val,
428                                                                                                                         int line)
429 {
430         int c_cols;
431         int c_elems;
432         int c1;
433         double *val_array;
434   int *ind_array;
435
436         GNUNET_assert (NULL != p);
437         GNUNET_assert (NULL != p->prob);
438
439         /* Get number of columns and prepare data structure */
440         c_cols = glp_get_num_cols(p->prob);
441         if (0 >= c_cols)
442                 return;
443
444         val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
445         GNUNET_assert (NULL != val_array);
446         ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
447         GNUNET_assert (NULL != ind_array);
448         /* Extract the row */
449
450         if (0 == (c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array)))
451         {
452           GNUNET_free (ind_array);
453           GNUNET_free (val_array);
454                 return;
455         }
456
457         /* Update the value */
458         for (c1 = 1; c1 < (c_elems+1); c1++)
459         {
460                 /* Debug
461                 fprintf (stderr, "cur %u of %u: [%u] %s <-> [%u] %s\n",
462                                 c1, c_elems,
463                                 ind_array[c1],
464                                 glp_get_col_name(p->prob, ind_array[c1]),
465                                 col,
466                                 glp_get_col_name(p->prob, col)
467                                 );
468                         */
469                 if (ind_array[c1] == col)
470                         break;
471         }
472         if ((c_elems + 1)== c1)
473         {
474           GNUNET_free (ind_array);
475           GNUNET_free (val_array);
476                 return; /* not found */
477         }
478         /* Update value */
479         LOG (GNUNET_ERROR_TYPE_ERROR, "[P] Updating value for peer from `%.2f' to `%.2f'\n",
480                         val_array[c1], val);
481         val_array[c1] = val;
482
483         /* Update the row in the matrix */
484         glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
485   GNUNET_free (ind_array);
486   GNUNET_free (val_array);
487 }
488
489 /**
490  * Creates a new value in the matrix
491  *
492  * Sets the row and column index in the problem array and increments the
493  * position field
494  *
495  * @param p the mlp problem
496  * @param row the row to create the value in
497  * @param col the column to create the value in
498  * @param val the value to set
499  * @param line calling line for debbuging
500  */
501 static void
502 mlp_create_problem_set_value (struct MLP_Problem *p,
503                                                                                                                         int row, int col, double val,
504                                                                                                                         int line)
505 {
506         if ((p->ci) >= p->num_elements)
507         {
508                 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
509                                 line, p->ci + 1, p->num_elements);
510                 GNUNET_break (0);
511                 return;
512         }
513         if ((0 == row) || (0 == col))
514                 GNUNET_break (0);
515   p->ia[p->ci] = row ;
516   p->ja[p->ci] = col;
517   p->ar[p->ci] = val;
518 #if  DEBUG_MLP_PROBLEM_CREATION
519         LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u ==  %.2f\n",
520                         line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
521 #endif
522   p->ci++;
523 }
524
525 static int
526 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
527                 unsigned int type, unsigned int bound, double lb, double ub,
528                 double coef)
529 {
530         int col = glp_add_cols (p->prob, 1);
531   glp_set_col_name (p->prob, col, name);
532   glp_set_col_bnds (p->prob, col, bound, lb, ub);
533   glp_set_col_kind (p->prob, col, type);
534   glp_set_obj_coef (p->prob, col, coef);
535 #if  DEBUG_MLP_PROBLEM_CREATION
536         LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
537                         col, name, coef);
538 #endif
539   return col;
540 }
541
542 static int
543 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
544                 unsigned int bound, double lb, double ub)
545 {
546         char * op;
547   int row = glp_add_rows (p->prob, 1);
548   /* set row name */
549   glp_set_row_name (p->prob, row, name);
550   /* set row bounds: <= 0 */
551   glp_set_row_bnds (p->prob, row, bound, lb, ub);
552   switch (bound) {
553                 case GLP_UP:
554                         GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
555                         break;
556                 case GLP_DB:
557                         GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
558                         break;
559                 case GLP_FX:
560                         GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
561                         break;
562                 case GLP_LO:
563                         GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
564                         break;
565                 default:
566                         GNUNET_asprintf(&op, "ERROR");
567                         break;
568         }
569 #if  DEBUG_MLP_PROBLEM_CREATION
570                 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
571                                 row, name, op);
572 #endif
573         GNUNET_free (op);
574         return row;
575 }
576
577 /**
578  * Create the
579  * - address columns b and n
580  * - address dependent constraint rows c1, c3
581  * - peer dependent rows c2 and c9
582  * - Set address dependent entries in problem matrix as well
583  */
584 static int
585 mlp_create_problem_add_address_information (void *cls, const struct GNUNET_HashCode *key, void *value)
586 {
587   struct GAS_MLP_Handle *mlp = cls;
588   struct MLP_Problem *p = &mlp->p;
589   struct ATS_Address *address = value;
590   struct ATS_Peer *peer;
591   struct MLP_information *mlpi;
592   char *name;
593   uint32_t addr_net;
594   int c;
595
596   /* Check if we have to add this peer due to a pending request */
597   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->requested_peers, key))
598         return GNUNET_OK;
599
600   mlpi = address->solver_information;
601   if (NULL == mlpi)
602   {
603                 fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
604                 GNUNET_break (0);
605                 return GNUNET_OK;
606   }
607
608   /* Get peer */
609   peer = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, key);
610   if (peer->processed == GNUNET_NO)
611   {
612                 /* Add peer dependent constraints */
613                 /* Add constraint c2 */
614           GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
615           peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
616                 GNUNET_free (name);
617                 /* Add constraint c9 */
618           GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
619           peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
620                 GNUNET_free (name);
621           /* c 9) set coefficient */
622                 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f, __LINE__);
623                 peer->processed = GNUNET_YES;
624   }
625
626   /* Reset addresses' solver information */
627   mlpi->c_b = 0;
628   mlpi->c_n = 0;
629   mlpi->n = 0;
630   mlpi->r_c1 = 0;
631   mlpi->r_c3 = 0;
632   for (c = 0; c < mlp->pv.m_q; c++)
633         mlpi->r_q[0] = 0;
634
635   /* Add bandwidth column */
636   GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
637 #if TEST_MAX_BW_ASSIGNMENT
638   mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
639 #else
640   mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
641 #endif
642
643   GNUNET_free (name);
644
645   /* Add usage column */
646   GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
647   mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
648   GNUNET_free (name);
649
650         /* Add address dependent constraints */
651         /* Add constraint c1) bandwidth capping
652    * b_t  + (-M) * n_t <= 0
653    * */
654   GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
655   mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
656         GNUNET_free (name);
657
658         /*  c1) set b = 1 coefficient */
659         mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
660         /*  c1) set n = -M coefficient */
661         mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -mlp->pv.BIG_M, __LINE__);
662
663   /* Add constraint c 3) minimum bandwidth
664    * b_t + (-n_t * b_min) >= 0
665    * */
666   GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
667         mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
668         GNUNET_free (name);
669
670         /*  c3) set b = 1 coefficient */
671         mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
672         /*  c3) set n = -b_min coefficient */
673         mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
674
675
676         /* Set coefficient entries in invariant rows */
677   /* c 4) minimum connections */
678         mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
679   /* c 6) maximize diversity */
680         mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
681   /* c 2) 1 address peer peer */
682         mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
683   /* c 9) relativity */
684         mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
685   /* c 8) utility */
686         mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
687
688   /* c 10) obey network specific quotas
689    * (1)*b_1 + ... + (1)*b_m <= quota_n
690    */
691   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
692   {
693         addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
694         if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
695                 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
696
697     if (mlp->pv.quota_index[c] == addr_net)
698     {
699                 mlp_create_problem_set_value (p, p->r_quota[c], mlpi->c_b, 1, __LINE__);
700       break;
701     }
702   }
703
704   /* c 7) Optimize quality */
705   /* For all quality metrics, set quality of this address */
706   for (c = 0; c < mlp->pv.m_q; c++)
707   {
708
709     mlp_create_problem_set_value (p, p->r_q[c], mlpi->c_b, mlpi->q_averaged[c], __LINE__);
710   }
711
712   return GNUNET_OK;
713 }
714
715 /**
716  * Create the invariant columns c4, c6, c10, c8, c7
717  */
718 static void
719 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
720 {
721   char *name;
722   int c;
723
724   /* Row for c4) minimum connection */
725   /* Number of minimum connections is min(|Peers|, n_min) */
726   p->r_c4 = mlp_create_problem_create_constraint (p, "c4", GLP_LO, (mlp->pv.n_min > p->num_peers) ? p->num_peers : mlp->pv.n_min, 0.0);
727
728   /* Add row for c6) */
729         p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
730   /* c6 )Setting -D */
731         mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
732
733   /* Add rows for c 10) */
734   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
735   {
736       char * text;
737       GNUNET_asprintf(&text, "c10_quota_ats_%s", GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
738                 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
739                 GNUNET_free (text);
740   }
741
742   /* Adding rows for c 8) */
743   p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
744   /* -u */
745         mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
746
747         /* c 7) For all quality metrics */
748         for (c = 0; c < mlp->pv.m_q; c++)
749         {
750                 GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->pv.q[c]));
751                 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
752                 GNUNET_free (name);
753                 mlp_create_problem_set_value (p, p->r_q[c], p->c_q[c], -1, __LINE__);
754         }
755 }
756
757
758 /**
759  * Create the invariant columns d, u, r, q0 ... qm
760  */
761 static void
762 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
763 {
764   char *name;
765   int c;
766
767 #if TEST_MAX_BW_ASSIGNMENT
768   mlp->pv.co_D = 0.0;
769   mlp->pv.co_U = 0.0;
770
771 #endif
772   //mlp->pv.co_R = 0.0;
773
774   /* Diversity d column  */
775   p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
776
777   /* Utilization u column  */
778   p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
779
780   /* Relativity r column  */
781   p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
782
783   /* Quality metric columns */
784   for (c = 0; c < mlp->pv.m_q; c++)
785   {
786     GNUNET_asprintf (&name, "q_%u", mlp->pv.q[c]);
787 #if TEST_MAX_BW_ASSIGNMENT
788         p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
789 #else
790         p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
791 #endif
792         GNUNET_free (name);
793   }
794 }
795
796
797 /**
798  * Create the MLP problem
799  *
800  * @param mlp the MLP handle
801  * @param addresses the hashmap containing all adresses
802  * @return GNUNET_OK or GNUNET_SYSERR
803  */
804 static int
805 mlp_create_problem (struct GAS_MLP_Handle *mlp)
806 {
807   struct MLP_Problem *p = &mlp->p;
808         int res = GNUNET_OK;
809
810   GNUNET_assert (p->prob == NULL);
811   GNUNET_assert (p->ia == NULL);
812   GNUNET_assert (p->ja == NULL);
813   GNUNET_assert (p->ar == NULL);
814   /* Reset MLP problem struct */
815
816   /* create the glpk problem */
817   p->prob = glp_create_prob ();
818   GNUNET_assert (NULL != p->prob);
819   p->num_peers = GNUNET_CONTAINER_multihashmap_size (mlp->requested_peers);
820   p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers, mlp->addresses);
821
822   /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
823   p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses +  mlp->pv.m_q + p->num_peers + 2 + 1);
824         LOG (GNUNET_ERROR_TYPE_DEBUG, "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
825                         p->num_peers, p->num_addresses, mlp->pv.m_q, p->num_elements);
826
827   /* Set a problem name */
828   glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
829   /* Set optimization direction to maximize */
830   glp_set_obj_dir (p->prob, GLP_MAX);
831
832   /* Create problem matrix */
833   /* last +1 caused by glpk index starting with one: [1..elements]*/
834   p->ci = 1;
835   /* row index */
836   p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
837   /* column index */
838   p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
839   /* coefficient */
840   p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
841
842   if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
843   {
844                 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
845                 return GNUNET_SYSERR;
846   }
847
848   /* Adding invariant columns */
849   mlp_create_problem_add_invariant_columns (mlp, p);
850
851   /* Adding address independent constraint rows */
852   mlp_create_problem_add_invariant_rows (mlp, p);
853
854   /* Adding address dependent columns constraint rows */
855   GNUNET_CONTAINER_multihashmap_iterate (mlp->addresses, &mlp_create_problem_add_address_information, mlp);
856
857   /* Load the matrix */
858         LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
859   glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
860
861   return res;
862 }
863
864 /**
865  * Solves the LP problem
866  *
867  * @param mlp the MLP Handle
868  * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
869  */
870 static int
871 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
872 {
873         int res = 0;
874
875         res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
876         if (0 == res)
877                 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
878         else
879                 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
880
881   /* Analyze problem status  */
882   res = glp_get_status (mlp->p.prob);
883   switch (res) {
884     /* solution is optimal */
885     case GLP_OPT:
886     /* solution is feasible */
887     case GLP_FEAS:
888       LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n",
889                 res, mlp_status_to_string(res));
890       return GNUNET_OK;
891     /* Problem was ill-defined, no way to handle that */
892     default:
893       LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed, no solution: 0x%02X %s\n",
894                 res, mlp_status_to_string(res));
895       return GNUNET_SYSERR;
896   }
897 }
898
899
900 /**
901  * Solves the MLP problem
902  *
903  * @param mlp the MLP Handle
904  * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
905  */
906 int
907 mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
908 {
909         int res = 0;
910         res = glp_intopt(mlp->p.prob, &mlp->control_param_mlp);
911         if (0 == res)
912                 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
913         else
914                 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving MLP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
915   /* Analyze problem status  */
916   res = glp_mip_status(mlp->p.prob);
917   switch (res) {
918     /* solution is optimal */
919     case GLP_OPT:
920     /* solution is feasible */
921     case GLP_FEAS:
922       LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_status_to_string(res));
923       return GNUNET_OK;
924     /* Problem was ill-defined, no way to handle that */
925     default:
926       LOG (GNUNET_ERROR_TYPE_WARNING,"Solving MLP problem failed, 0x%02X %s\n\n", res, mlp_status_to_string(res));
927       return GNUNET_SYSERR;
928   }
929 }
930
931
932 /**
933  * Propagates the results when MLP problem was solved
934  *
935  * @param cls the MLP handle
936  * @param key the peer identity
937  * @param value the address
938  * @return GNUNET_OK to continue
939  */
940 int
941 mlp_propagate_results (void *cls, const struct GNUNET_HashCode *key, void *value)
942 {
943         struct GAS_MLP_Handle *mlp = cls;
944         struct ATS_Address *address;
945         struct MLP_information *mlpi;
946         double mlp_bw_in = MLP_NaN;
947         double mlp_bw_out = MLP_NaN;
948         double mlp_use = MLP_NaN;
949
950   /* Check if we have to add this peer due to a pending request */
951   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->requested_peers, key))
952         return GNUNET_OK;
953   address = value;
954   GNUNET_assert (address->solver_information != NULL);
955   mlpi = address->solver_information;
956
957   mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
958   if (mlp_bw_in > (double) UINT32_MAX)
959   {
960                 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
961                 mlp_bw_in = (double) UINT32_MAX;
962   }
963   mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
964   if (mlp_bw_out > (double) UINT32_MAX)
965   {
966                 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
967                 mlp_bw_out = (double) UINT32_MAX;
968   }
969   mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
970
971
972
973   if ((GLP_YES == mlp_use) && (GNUNET_NO == address->active))
974   {
975         /* Address switch: Activate address*/
976         LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
977                 address->active = GNUNET_YES;
978                 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
979                 mlpi->b_in.value__ = htonl(mlp_bw_in);
980                 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
981                 mlpi->b_out.value__ = htonl(mlp_bw_out);
982                 mlpi->n = mlp_use;
983                 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
984   }
985   else if ((GLP_NO == mlp_use) && (GNUNET_YES == address->active))
986   {
987                 /* Address switch: Disable address*/
988         LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
989                 address->active = GNUNET_NO;
990                 /* Set bandwidth to 0 */
991                 address->assigned_bw_in.value__ = htonl (0);
992                 mlpi->b_in.value__ = htonl(mlp_bw_in);
993                 address->assigned_bw_out.value__ = htonl (0);
994                 mlpi->b_out.value__ = htonl(mlp_bw_out);
995                 mlpi->n = mlp_use;
996                 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
997   }
998   else if ((mlp_bw_out != ntohl(address->assigned_bw_out.value__)) ||
999                                  (mlp_bw_in != ntohl(address->assigned_bw_in.value__)))
1000   {
1001         /* Bandwidth changed */
1002                 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1003                 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
1004                 mlpi->b_in.value__ = htonl(mlp_bw_in);
1005                 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
1006                 mlpi->b_out.value__ = htonl(mlp_bw_out);
1007                 mlpi->n = mlp_use;
1008                 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1009   }
1010   else
1011   {
1012     LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1013   }
1014
1015   return GNUNET_OK;
1016 }
1017
1018
1019
1020 /**
1021  * Solves the MLP problem
1022  *
1023  * @param solver the MLP Handle
1024  * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
1025  */
1026 int
1027 GAS_mlp_solve_problem (void *solver)
1028 {
1029         struct GAS_MLP_Handle *mlp = solver;
1030         char *filename;
1031         int res_lp = 0;
1032         int res_mip = 0;
1033         struct GNUNET_TIME_Absolute start_build;
1034         struct GNUNET_TIME_Relative duration_build;
1035         struct GNUNET_TIME_Absolute start_lp;
1036         struct GNUNET_TIME_Relative duration_lp;
1037         struct GNUNET_TIME_Absolute start_mlp;
1038         struct GNUNET_TIME_Relative duration_mlp;
1039         GNUNET_assert (NULL != solver);
1040
1041         if (GNUNET_YES == mlp->bulk_lock)
1042         {
1043                 mlp->bulk_request ++;
1044                 return GNUNET_NO;
1045         }
1046
1047         if (0 == GNUNET_CONTAINER_multihashmap_size(mlp->requested_peers))
1048         {
1049                 GNUNET_break (0);
1050                 return GNUNET_OK;
1051         }
1052         if (0 == GNUNET_CONTAINER_multihashmap_size(mlp->addresses))
1053         {
1054                 GNUNET_break (0);
1055                 return GNUNET_OK;
1056         }
1057
1058         if ((GNUNET_NO == mlp->mlp_prob_changed) && (GNUNET_NO == mlp->mlp_prob_updated))
1059         {
1060                 LOG (GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
1061                 return GNUNET_OK;
1062         }
1063         if (GNUNET_YES == mlp->mlp_prob_changed)
1064         {
1065                         LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
1066                         mlp_delete_problem (mlp);
1067                         start_build = GNUNET_TIME_absolute_get();
1068                         if (GNUNET_SYSERR == mlp_create_problem (mlp))
1069                                 return GNUNET_SYSERR;
1070                         duration_build = GNUNET_TIME_absolute_get_duration (start_build);
1071                         mlp->control_param_lp.presolve = GLP_YES;
1072                         mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
1073         }
1074         else
1075         {
1076                         LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
1077                         duration_build.rel_value = 0;
1078         }
1079
1080         /* Run LP solver */
1081         LOG (GNUNET_ERROR_TYPE_DEBUG, "Running LP solver %s\n", (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
1082         start_lp = GNUNET_TIME_absolute_get();
1083         res_lp = mlp_solve_lp_problem (mlp);
1084         duration_lp = GNUNET_TIME_absolute_get_duration (start_lp);
1085
1086
1087   /* Run LP solver */
1088         LOG (GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
1089         start_mlp = GNUNET_TIME_absolute_get();
1090         res_mip = mlp_solve_mlp_problem (mlp);
1091
1092         duration_mlp = GNUNET_TIME_absolute_get_duration (start_mlp);
1093
1094         /* Save stats */
1095         mlp->ps.lp_res = res_lp;
1096         mlp->ps.mip_res = res_mip;
1097         mlp->ps.build_dur = duration_build;
1098         mlp->ps.lp_dur = duration_lp;
1099         mlp->ps.mip_dur = duration_mlp;
1100         mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1101         mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1102         mlp->ps.p_cols = glp_get_num_cols (mlp->p.prob);
1103         mlp->ps.p_rows = glp_get_num_rows (mlp->p.prob);
1104         mlp->ps.p_elements = mlp->p.num_elements;
1105
1106         LOG (GNUNET_ERROR_TYPE_DEBUG, "Execution time: Build %llu ms, LP %llu ms,  MLP %llu ms\n",
1107                         (unsigned long long) duration_build.rel_value,
1108                         (unsigned long long) duration_lp.rel_value,
1109                         (unsigned long long) duration_mlp.rel_value);
1110
1111         /* Propagate result*/
1112         if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1113                 GNUNET_CONTAINER_multihashmap_iterate (mlp->addresses, &mlp_propagate_results, mlp);
1114
1115         struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1116         if (GNUNET_YES == mlp->write_mip_mps)
1117         {
1118                 /* Write problem to disk */
1119                 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value);
1120                 LOG (GNUNET_ERROR_TYPE_ERROR, "DUMP: %s \n", filename);
1121                 glp_write_lp(mlp->p.prob, NULL, filename);
1122                 GNUNET_free (filename);
1123         }
1124         if (GNUNET_YES == mlp->write_mip_sol)
1125         {
1126                 /* Write solution to disk */
1127                 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value);
1128                 glp_print_mip (mlp->p.prob, filename );
1129                 LOG (GNUNET_ERROR_TYPE_ERROR, "DUMP: %s \n", filename);
1130                 GNUNET_free (filename);
1131         }
1132
1133         /* Reset change and update marker */
1134         mlp->control_param_lp.presolve = GLP_NO;
1135         mlp->mlp_prob_updated = GNUNET_NO;
1136         mlp->mlp_prob_changed = GNUNET_NO;
1137
1138         if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1139                 return GNUNET_OK;
1140         else
1141                 return GNUNET_SYSERR;
1142 }
1143
1144 /**
1145  * Add a single address to the solve
1146  *
1147  * @param solver the solver Handle
1148  * @param addresses the address hashmap containing all addresses
1149  * @param address the address to add
1150  * @param network network type of this address
1151  */
1152 void
1153 GAS_mlp_address_add (void *solver,
1154                                                                                 struct ATS_Address *address,
1155                                                                                 uint32_t network)
1156 {
1157   struct GAS_MLP_Handle *mlp = solver;
1158   struct ATS_Peer *p;
1159   struct MLP_information *mlpi;
1160   int c1;
1161   int c2;
1162
1163   GNUNET_assert (NULL != solver);
1164   GNUNET_assert (NULL != address);
1165
1166   if (NULL == address->solver_information)
1167   {
1168                 address->solver_information = GNUNET_malloc (sizeof (struct MLP_information));
1169                 mlpi = address->solver_information;
1170           for (c1 = 0; c1 < mlp->pv.m_q; c1++)
1171           {
1172                 mlpi->q_averaged[c1] = DEFAULT_QUALITY;
1173                 for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
1174                         mlpi->q[c1][c2] = MLP_NaN;
1175           }
1176   }
1177   else
1178       LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding address for peer `%s' multiple times\n"), GNUNET_i2s(&address->peer));
1179
1180   /* Is this peer included in the problem? */
1181   if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &address->peer.hashPubKey)))
1182   {
1183     LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1184         return;
1185   }
1186
1187         LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1188         /* Problem size changed: new address for peer with pending request */
1189         mlp->mlp_prob_changed = GNUNET_YES;
1190         if (GNUNET_YES == mlp->mlp_auto_solve)
1191                 GAS_mlp_solve_problem (solver);
1192 }
1193
1194
1195 static void
1196 mlp_update_quality (struct GAS_MLP_Handle *mlp,
1197                 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1198                 struct ATS_Address * address,
1199                 const struct GNUNET_ATS_Information *ats_prev, uint32_t ats_prev_count)
1200 {
1201   struct MLP_information *mlpi = address->solver_information;
1202   unsigned int c_ats_entry;
1203   unsigned int c_queue_entries;
1204   unsigned int c_cmp;
1205   unsigned int c_queue_it;
1206   unsigned int c_row;
1207   unsigned int c_qual;
1208   unsigned int c_net;
1209   int qual_changed;
1210   int type_index;
1211   int avg_index;
1212   uint32_t type;
1213   uint32_t prev_value;
1214   uint32_t new_value;
1215   double avg;
1216   double *queue;
1217   int rows;
1218   double *val;
1219   int *ind;
1220
1221
1222         LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating %u quality metrics for peer `%s'\n",
1223       ats_prev_count, GNUNET_i2s (&address->peer));
1224
1225         GNUNET_assert (NULL != mlp);
1226   GNUNET_assert (NULL != address);
1227   GNUNET_assert (NULL != address->solver_information);
1228   GNUNET_assert (NULL != ats_prev);
1229
1230   if (NULL == mlp->p.prob)
1231         return;
1232
1233   qual_changed = GNUNET_NO;
1234   for (c_ats_entry = 0; c_ats_entry < ats_prev_count; c_ats_entry++)
1235   {
1236                 type = ntohl (ats_prev[c_ats_entry].type);
1237                 prev_value = ntohl (ats_prev[c_ats_entry].value);
1238                 type_index = -1;
1239                 avg_index = -1;
1240
1241                 /* Check for network update */
1242                 if (type == GNUNET_ATS_NETWORK_TYPE)
1243                 {
1244                                 new_value = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1245                         if (GNUNET_ATS_VALUE_UNDEFINED == new_value)
1246                                 new_value = GNUNET_ATS_NET_UNSPECIFIED;
1247                                 if (new_value != prev_value)
1248                                 {
1249                                 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating network for peer `%s' from `%s' to `%s'\n",
1250                               GNUNET_i2s (&address->peer),
1251                               GNUNET_ATS_print_network_type(prev_value),
1252                               GNUNET_ATS_print_network_type(new_value));
1253                                 }
1254
1255                                 if (mlpi->c_b == MLP_UNDEFINED)
1256                                         continue; /* This address is not yet in the matrix*/
1257
1258                           rows = glp_get_num_rows(mlp->p.prob);
1259                           ind = GNUNET_malloc (rows * sizeof (int) + 1);
1260                           val = GNUNET_malloc (rows * sizeof (double) + 1);
1261                           int length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1262
1263                           for (c_net = 0; c_net <= length + 1; c_net ++)
1264                           {
1265                                 if (ind[c_net] == mlp->p.r_quota[prev_value])
1266                                         break; /* Found index for old network */
1267                           }
1268                           val[c_net] = 0.0;
1269                                 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1270                                 /* Set updated column */
1271                                 ind[c_net] = mlp->p.r_quota[new_value];
1272                                 val[c_net] = 1.0;
1273                                 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1274                           GNUNET_free (ind);
1275                           GNUNET_free (val);
1276
1277                           rows = glp_get_num_rows(mlp->p.prob);
1278                           ind = GNUNET_malloc (rows * sizeof (int) + 1);
1279                           val = GNUNET_malloc (rows * sizeof (double) + 1);
1280                           length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1281
1282                           for (c_net = 0; c_net <= length + 1; c_net ++)
1283                           {
1284                                 if (ind[c_net] == mlp->p.r_quota[prev_value])
1285                                         LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing old network index [%u] == [%f]\n",ind[c_net],val[c_net]);
1286                                 if (ind[c_net] == mlp->p.r_quota[new_value])
1287                                 {
1288                                         LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting new network index [%u] == [%f]\n",ind[c_net],val[c_net]);
1289                                         break;
1290                                 }
1291                           }
1292                           GNUNET_free (ind);
1293                           GNUNET_free (val);
1294                           mlp->mlp_prob_changed = GNUNET_YES;
1295                                 continue;
1296                 }
1297
1298
1299                 /* Find index for this ATS type */
1300           for (c_cmp = 0; c_cmp < mlp->pv.m_q; c_cmp++)
1301           {
1302             if (type == mlp->pv.q[c_cmp])
1303             {
1304                 type_index = c_cmp;
1305               break;
1306             }
1307           }
1308           if (-1 == type_index)
1309                 continue; /* quality index not found */
1310
1311           /* Get average queue index */
1312           avg_index = mlpi->q_avg_i[type_index];
1313
1314           /* Update averaging queue */
1315           new_value = get_performance_info (address, type);
1316       LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s': `%s' from %u to %u\n",
1317         GNUNET_i2s (&address->peer),
1318         mlp_ats_to_string(mlp->pv.q[type_index]), prev_value, new_value);
1319           GNUNET_assert (GNUNET_ATS_VALUE_UNDEFINED != new_value);
1320           mlpi->q[type_index][avg_index] = new_value;
1321
1322           /* Update averaging index */
1323       if (mlpi->q_avg_i[type_index] + 1 < (MLP_AVERAGING_QUEUE_LENGTH))
1324         mlpi->q_avg_i[type_index] ++;
1325       else
1326         mlpi->q_avg_i[type_index] = 0;
1327
1328           /* Update average depending on ATS type */
1329       switch (type)
1330       {
1331         case GNUNET_ATS_QUALITY_NET_DISTANCE:
1332         case GNUNET_ATS_QUALITY_NET_DELAY:
1333                 c_queue_entries = 0;
1334                 avg = 0;
1335           for (c_queue_it = 0; c_queue_it < MLP_AVERAGING_QUEUE_LENGTH; c_queue_it++)
1336           {
1337             if (mlpi->q[type_index][c_queue_it] != MLP_NaN)
1338             {
1339               queue = mlpi->q[type_index] ;
1340               avg += queue[c_queue_it];
1341               c_queue_entries ++;
1342             }
1343           }
1344           if ((c_queue_entries > 0) && (avg > 0))
1345             /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
1346             mlpi->q_averaged[type_index] = (double) c_queue_entries / avg;
1347           else
1348             mlpi->q_averaged[type_index] = 0.0;
1349
1350           LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s': `%s' average sum of %u elements == %f, average == %f, weight == %f\n",
1351             GNUNET_i2s (&address->peer),
1352             mlp_ats_to_string(mlp->pv.q[type_index]),
1353             c_queue_entries,
1354             avg,
1355             avg / (double) c_queue_entries,
1356             mlpi->q_averaged[type_index]);
1357           qual_changed = GNUNET_YES;
1358                 break;
1359         default:
1360                 GNUNET_break (0);
1361                 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Update for ATS type `%s' not implemented!\n"),
1362                                 mlp_ats_to_string(type));
1363       }
1364   }
1365
1366   /* Changed, but quality will be automatically set during rebuild */
1367   if ((GNUNET_YES == mlp->mlp_prob_changed) &&
1368           (GNUNET_YES == mlp->mlp_auto_solve))
1369   {
1370                 GAS_mlp_solve_problem (mlp);
1371                 return;
1372   }
1373
1374   /* Update problem matrix if required */
1375   if (GNUNET_NO == qual_changed)
1376         return;
1377
1378   /* Address not yet included in matrix */
1379   if (MLP_UNDEFINED == mlpi->c_b)
1380         return;
1381
1382   /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index]
1383    * Get column mlpi->c_b */
1384   rows = glp_get_num_rows(mlp->p.prob);
1385   ind = GNUNET_malloc (rows * sizeof (int) + 1);
1386   val = GNUNET_malloc (rows * sizeof (double) + 1);
1387   int length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1388
1389         for (c_qual = 0; c_qual < mlp->pv.m_q; c_qual++)
1390         {
1391                 for (c_row = 0; c_row <= length; c_row ++)
1392                 {
1393                                 if (ind[c_row] == mlp->p.r_q[c_qual])
1394                                         val[c_row] = mlpi->q_averaged[c_qual];
1395                 }
1396         }
1397         /* Set updated column */
1398         glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1399   GNUNET_free (ind);
1400   GNUNET_free (val);
1401   mlp->mlp_prob_updated = GNUNET_YES;
1402 }
1403
1404 void
1405 GAS_mlp_address_property_changed (void *solver,
1406                                                                                                                         struct ATS_Address *address,
1407                                                                                                                         uint32_t type,
1408                                                                                                                         uint32_t abs_value,
1409                                                                                                                         double rel_value)
1410 {
1411         GNUNET_break (0);
1412 }
1413
1414
1415 void
1416 GAS_mlp_address_session_changed (void *solver,
1417                                                                                                                         struct ATS_Address *address,
1418                                                                                                                         uint32_t cur_session,
1419                                                                                                                         uint32_t new_session)
1420 {
1421         GNUNET_break (0);
1422 }
1423
1424 void
1425 GAS_mlp_address_inuse_changed (void *solver,
1426                                                                                                                          struct ATS_Address *address,
1427                                                                                                                          uint32_t session,
1428                                                                                                                          int in_use)
1429 {
1430         GNUNET_break (0);
1431 }
1432
1433
1434 void
1435 GAS_mlp_address_change_network (void *solver,
1436                                                                                                                                            struct ATS_Address *address,
1437                                                                                                                                            uint32_t current_network,
1438                                                                                                                                            uint32_t new_network)
1439 {
1440         GNUNET_break (0);
1441 }
1442
1443 /**
1444  * Updates a single address in the MLP problem
1445  *
1446  * If the address did not exist before in the problem:
1447  * The MLP problem has to be recreated and the problem has to be resolved
1448  *
1449  * ATS performance information in address are already updated, delta + previous
1450  * values are included in atsi_prev (value GNUNET_ATS_VALUE_UNDEFINED if not existing before)
1451  *
1452  * Otherwise the addresses' values can be updated and the existing base can
1453  * be reused
1454  *
1455  * @param solver the solver Handle
1456  * @param addresses the address hashmap containing all addresses
1457  * @param address the update address
1458  * @param prev_session the new session (if changed otherwise current)
1459  * @param prev_in_use the new address in use state (if changed otherwise current)
1460  * @param prev_atsi ATS information updated + previous values, GNUNET_ATS_VALUE_UNDEFINED if not existing before
1461  * @param prev_atsi_count number of atsi values updated
1462  */
1463 void
1464 GAS_mlp_address_update (void *solver,
1465                         struct ATS_Address *address,
1466                         uint32_t prev_session,
1467                         int prev_in_use,
1468                         const struct GNUNET_ATS_Information *prev_atsi,
1469                         uint32_t prev_atsi_count)
1470 {
1471         struct ATS_Peer *p;
1472         struct GAS_MLP_Handle *mlp = solver;
1473         struct MLP_information *mlpi = address->solver_information;
1474         int c1;
1475
1476         GNUNET_assert (NULL != solver);
1477         GNUNET_assert (NULL != address);
1478         GNUNET_assert ((NULL != prev_atsi) || (0 == prev_atsi_count));
1479
1480   if (NULL == mlpi)
1481   {
1482       LOG (GNUNET_ERROR_TYPE_ERROR, _("Updating address for peer `%s' not added before\n"), GNUNET_i2s(&address->peer));
1483       return;
1484   }
1485
1486   if (address->session_id != prev_session)
1487   {
1488         /* Session changed */
1489
1490   }
1491   if ((NULL != prev_atsi) && (0 != prev_atsi_count))
1492   {
1493         /* Properties changed */
1494         for (c1 = 0; c1 < prev_atsi_count; c1++ )
1495         {
1496         LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating `%s'\n",
1497                         GNUNET_ATS_print_property_type (ntohl(prev_atsi[c1].type)));
1498         //mlp->get_properties (mlp->get_properties_cls );
1499         }
1500         //mlp_update_quality (mlp, mlp->addresses, address, prev_atsi, prev_atsi_count);
1501   }
1502
1503   /* Is this peer included in the problem? */
1504   if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &address->peer.hashPubKey)))
1505   {
1506         LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' without address request \n",
1507                 GNUNET_i2s(&address->peer));
1508         return;
1509   }
1510         LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' with address request \n",
1511                         GNUNET_i2s(&address->peer));
1512         mlp->mlp_prob_updated = GNUNET_YES;
1513
1514         if (GNUNET_YES == mlp->mlp_auto_solve)
1515                 GAS_mlp_solve_problem (solver);
1516 }
1517
1518 /**
1519  * Deletes a single address in the MLP problem
1520  *
1521  * The MLP problem has to be recreated and the problem has to be resolved
1522  *
1523  * @param solver the MLP Handle
1524  * @param addresses the address hashmap
1525  *        the address has to be already removed from the hashmap
1526  * @param address the address to delete
1527  * @param session_only delete only session not whole address
1528  */
1529 void
1530 GAS_mlp_address_delete (void *solver,
1531     struct ATS_Address *address,
1532     int session_only)
1533 {
1534         struct ATS_Peer *p;
1535         struct GAS_MLP_Handle *mlp = solver;
1536         struct MLP_information *mlpi;
1537
1538         GNUNET_assert (NULL != solver);
1539         GNUNET_assert (NULL != address);
1540
1541         mlpi = address->solver_information;
1542         if ((GNUNET_NO == session_only) && (NULL != mlpi))
1543         {
1544                         GNUNET_free (mlpi);
1545                         address->solver_information = NULL;
1546         }
1547
1548   /* Is this peer included in the problem? */
1549   if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &address->peer.hashPubKey)))
1550   {
1551     LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s for peer `%s' without address request \n",
1552                 (session_only == GNUNET_YES) ? "session" : "address",
1553                 GNUNET_i2s(&address->peer));
1554         return;
1555   }
1556   LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s for peer `%s' with address request \n",
1557                 (session_only == GNUNET_YES) ? "session" : "address",
1558                 GNUNET_i2s(&address->peer));
1559
1560         /* Problem size changed: new address for peer with pending request */
1561         mlp->mlp_prob_changed = GNUNET_YES;
1562         if (GNUNET_YES == mlp->mlp_auto_solve)
1563                 GAS_mlp_solve_problem (solver);
1564   return;
1565 }
1566
1567
1568 /**
1569  * Find the active address in the set of addresses of a peer
1570  * @param cls destination
1571  * @param key peer id
1572  * @param value address
1573  * @return GNUNET_OK
1574  */
1575 static int
1576 mlp_get_preferred_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1577 {
1578
1579   struct ATS_Address *aa = (struct ATS_Address *) cls;
1580   struct ATS_Address *addr = value;
1581   struct MLP_information *mlpi = addr->solver_information;
1582   if (mlpi == NULL)
1583     return GNUNET_YES;
1584   if (mlpi->n == GNUNET_YES)
1585   {
1586     aa = addr;
1587       aa->assigned_bw_in = mlpi->b_in;
1588       aa->assigned_bw_out = mlpi->b_out;
1589     return GNUNET_NO;
1590   }
1591   return GNUNET_YES;
1592 }
1593
1594
1595 static double get_peer_pref_value (struct GAS_MLP_Handle *mlp, const struct GNUNET_PeerIdentity *peer)
1596 {
1597         double res;
1598   const double *preferences = NULL;
1599   int c;
1600   preferences = mlp->get_preferences (mlp->get_preferences_cls, peer);
1601
1602   res = 0.0;
1603         for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1604         {
1605                 if (c != GNUNET_ATS_PREFERENCE_END)
1606                 {
1607                         //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
1608                         res += preferences[c];
1609                 }
1610         }
1611         res /= (GNUNET_ATS_PreferenceCount -1);
1612         return res;
1613 }
1614
1615
1616 /**
1617  * Get the preferred address for a specific peer
1618  *
1619  * @param solver the MLP Handle
1620  * @param addresses address hashmap
1621  * @param peer the peer
1622  * @return suggested address
1623  */
1624 const struct ATS_Address *
1625 GAS_mlp_get_preferred_address (void *solver,
1626                                const struct GNUNET_PeerIdentity *peer)
1627 {
1628   struct GAS_MLP_Handle *mlp = solver;
1629   struct ATS_Peer *p;
1630   struct ATS_Address *res = NULL;
1631
1632   GNUNET_assert (NULL != solver);
1633   GNUNET_assert (NULL != peer);
1634
1635   LOG (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n",
1636                 GNUNET_i2s (peer));
1637
1638   /* Is this peer included in the problem? */
1639   if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1640   {
1641           LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding peer `%s' to list of requested_peers with requests\n",
1642                         GNUNET_i2s (peer));
1643
1644           p = GNUNET_malloc (sizeof (struct ATS_Peer));
1645           p->id = (*peer);
1646           p->f = get_peer_pref_value (mlp, peer);
1647           GNUNET_CONTAINER_multihashmap_put (mlp->requested_peers, &peer->hashPubKey, p, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1648
1649           /* Added new peer, we have to rebuild problem before solving */
1650           mlp->mlp_prob_changed = GNUNET_YES;
1651   }
1652   if (GNUNET_YES == mlp->mlp_auto_solve)
1653         GAS_mlp_solve_problem (mlp);
1654
1655   /* Get prefered address */
1656   GNUNET_CONTAINER_multihashmap_get_multiple (mlp->addresses, &peer->hashPubKey,
1657                                                                                                                                                                                 mlp_get_preferred_address_it, res);
1658
1659   return res;
1660 }
1661
1662
1663 /**
1664  * Start a bulk operation
1665  *
1666  * @param solver the solver
1667  */
1668 void
1669 GAS_mlp_bulk_start (void *solver)
1670 {
1671   LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1672   struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1673
1674   GNUNET_assert (NULL != solver);
1675
1676   s->bulk_lock ++;
1677 }
1678
1679 void
1680 GAS_mlp_bulk_stop (void *solver)
1681 {
1682         LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1683
1684   struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1685   GNUNET_assert (NULL != solver);
1686
1687   if (s->bulk_lock < 1)
1688   {
1689         GNUNET_break (0);
1690         return;
1691   }
1692   s->bulk_lock --;
1693
1694   if (0 < s->bulk_request)
1695   {
1696         GAS_mlp_solve_problem (solver);
1697         s->bulk_request= 0;
1698   }
1699 }
1700
1701
1702
1703 /**
1704  * Stop notifying about address and bandwidth changes for this peer
1705  *
1706  * @param solver the MLP handle
1707  * @param addresses address hashmap
1708  * @param peer the peer
1709  */
1710 void
1711 GAS_mlp_stop_get_preferred_address (void *solver,
1712                                      const struct GNUNET_PeerIdentity *peer)
1713 {
1714   struct GAS_MLP_Handle *mlp = solver;
1715   struct ATS_Peer *p = NULL;
1716
1717   GNUNET_assert (NULL != solver);
1718   GNUNET_assert (NULL != peer);
1719
1720   if (NULL != (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1721   {
1722         GNUNET_CONTAINER_multihashmap_remove (mlp->requested_peers, &peer->hashPubKey, p);
1723         GNUNET_free (p);
1724   }
1725 }
1726
1727
1728 /**
1729  * Changes the preferences for a peer in the MLP problem
1730  *
1731  * @param solver the MLP Handle
1732  * @param addresses the address hashmap
1733  * @param peer the peer
1734  * @param kind the kind to change the preference
1735  * @param pref_rel the relative score
1736  */
1737 void
1738 GAS_mlp_address_change_preference (void *solver,
1739                                                                    const struct GNUNET_PeerIdentity *peer,
1740                                                                    enum GNUNET_ATS_PreferenceKind kind,
1741                                                                    double pref_rel)
1742 {
1743   struct GAS_MLP_Handle *mlp = solver;
1744   struct ATS_Peer *p = NULL;
1745
1746   LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing preference for address for peer `%s' to %.2f\n",
1747                 GNUNET_i2s(peer), pref_rel);
1748
1749   GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1750   /* Update the constraints with changed preferences */
1751
1752   /* Update quality constraint c7 */
1753
1754   /* Update relativity constraint c9 */
1755   if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1756   {
1757     LOG (GNUNET_ERROR_TYPE_ERROR, "Updating preference for unknown peer `%s'\n", GNUNET_i2s(peer));
1758         return;
1759   }
1760   p->f = get_peer_pref_value (mlp, peer);
1761   LOG (GNUNET_ERROR_TYPE_ERROR, "PEER PREF: %s %.2f\n",
1762                 GNUNET_i2s(peer), p->f);
1763   mlp_create_problem_update_value (&mlp->p, p->r_c9, mlp->p.c_r, -p->f, __LINE__);
1764
1765         /* Problem size changed: new address for peer with pending request */
1766         mlp->mlp_prob_updated = GNUNET_YES;
1767         if (GNUNET_YES == mlp->mlp_auto_solve)
1768                 GAS_mlp_solve_problem (solver);
1769   return;
1770 }
1771
1772
1773 static int
1774 mlp_free_peers (void *cls, const struct GNUNET_HashCode *key, void *value)
1775 {
1776         struct GNUNET_CONTAINER_MultiHashMap *map = cls;
1777         struct ATS_Peer *p = value;
1778
1779         GNUNET_CONTAINER_multihashmap_remove (map, key, value);
1780         GNUNET_free (p);
1781
1782         return GNUNET_OK;
1783 }
1784
1785
1786 /**
1787  * Shutdown the MLP problem solving component
1788  *
1789  * @param solver the solver handle
1790  */
1791 void
1792 GAS_mlp_done (void *solver)
1793 {
1794   struct GAS_MLP_Handle *mlp = solver;
1795   GNUNET_assert (mlp != NULL);
1796
1797   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down mlp solver\n");
1798   mlp_delete_problem (mlp);
1799
1800   GNUNET_CONTAINER_multihashmap_iterate (mlp->requested_peers, &mlp_free_peers, mlp->requested_peers);
1801   GNUNET_CONTAINER_multihashmap_destroy (mlp->requested_peers);
1802   mlp->requested_peers = NULL;
1803
1804   /* Clean up GLPK environment */
1805   glp_free_env();
1806   GNUNET_free (mlp);
1807
1808   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutdown down of mlp solver complete\n");
1809 }
1810
1811
1812 /**
1813  * Init the MLP problem solving component
1814  *
1815  * @param cfg the GNUNET_CONFIGURATION_Handle handle
1816  * @param stats the GNUNET_STATISTICS handle
1817  * @param network array of GNUNET_ATS_NetworkType with length dest_length
1818  * @param out_dest array of outbound quotas
1819  * @param in_dest array of outbound quota
1820  * @param dest_length array length for quota arrays
1821  * @param bw_changed_cb callback for changed bandwidth amounts
1822  * @param bw_changed_cb_cls cls for callback
1823  * @param get_preference callback to get relative preferences for a peer
1824  * @param get_preference_cls cls for callback to get relative preferences
1825  * @return struct GAS_MLP_Handle on success, NULL on fail
1826  */
1827 void *
1828 GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1829               const struct GNUNET_STATISTICS_Handle *stats,
1830               const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1831               int *network,
1832               unsigned long long *out_dest,
1833               unsigned long long *in_dest,
1834               int dest_length,
1835               GAS_bandwidth_changed_cb bw_changed_cb,
1836               void *bw_changed_cb_cls,
1837               GAS_get_preferences get_preference,
1838               void *get_preference_cls,
1839               GAS_get_properties get_properties,
1840               void *get_properties_cls)
1841 {
1842   struct GAS_MLP_Handle * mlp = GNUNET_malloc (sizeof (struct GAS_MLP_Handle));
1843
1844   double D;
1845   double R;
1846   double U;
1847   unsigned long long tmp;
1848   unsigned int b_min;
1849   unsigned int n_min;
1850   int c;
1851   int c2;
1852   int found;
1853
1854   struct GNUNET_TIME_Relative max_duration;
1855   long long unsigned int max_iterations;
1856
1857   GNUNET_assert (NULL != cfg);
1858   GNUNET_assert (NULL != stats);
1859   GNUNET_assert (NULL != addresses);
1860   GNUNET_assert (NULL != bw_changed_cb);
1861   GNUNET_assert (NULL != get_preference);
1862   GNUNET_assert (NULL != get_properties);
1863
1864   /* Init GLPK environment */
1865   int res = glp_init_env();
1866   switch (res) {
1867     case 0:
1868         LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1869           "initialization successful");
1870       break;
1871     case 1:
1872         LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1873           "environment is already initialized");
1874       break;
1875     case 2:
1876         LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1877           "initialization failed (insufficient memory)");
1878       GNUNET_free(mlp);
1879       return NULL;
1880       break;
1881     case 3:
1882         LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1883           "initialization failed (unsupported programming model)");
1884       GNUNET_free(mlp);
1885       return NULL;
1886       break;
1887     default:
1888       break;
1889   }
1890
1891    mlp->write_mip_mps = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats",
1892                          "DUMP_MLP");
1893    if (GNUNET_SYSERR == mlp->write_mip_mps)
1894          mlp->write_mip_mps = GNUNET_NO;
1895    mlp->write_mip_sol = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats",
1896                          "DUMP_MLP");
1897    if (GNUNET_SYSERR == mlp->write_mip_sol)
1898          mlp->write_mip_sol = GNUNET_NO;
1899
1900   mlp->pv.BIG_M = (double) BIG_M_VALUE;
1901
1902   /* Get timeout for iterations */
1903   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(cfg, "ats", "MLP_MAX_DURATION", &max_duration))
1904   {
1905     max_duration = MLP_MAX_EXEC_DURATION;
1906   }
1907
1908   /* Get maximum number of iterations */
1909   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "MLP_MAX_ITERATIONS", &max_iterations))
1910   {
1911     max_iterations = MLP_MAX_ITERATIONS;
1912   }
1913
1914   /* Get diversity coefficient from configuration */
1915   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1916                                                       "MLP_COEFFICIENT_D",
1917                                                       &tmp))
1918     D = (double) tmp / 100;
1919   else
1920     D = DEFAULT_D;
1921
1922   /* Get proportionality coefficient from configuration */
1923   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1924                                                       "MLP_COEFFICIENT_R",
1925                                                       &tmp))
1926     R = (double) tmp / 100;
1927   else
1928     R = DEFAULT_R;
1929
1930   /* Get utilization coefficient from configuration */
1931   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1932                                                       "MLP_COEFFICIENT_U",
1933                                                       &tmp))
1934     U = (double) tmp / 100;
1935   else
1936     U = DEFAULT_U;
1937
1938   /* Get quality metric coefficients from configuration */
1939   int i_delay = MLP_NaN;
1940   int i_distance = MLP_NaN;
1941   int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
1942   for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1943   {
1944     /* initialize quality coefficients with default value 1.0 */
1945                 mlp->pv.co_Q[c] = DEFAULT_QUALITY;
1946
1947     mlp->pv.q[c] = q[c];
1948     if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
1949       i_delay = c;
1950     if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
1951       i_distance = c;
1952   }
1953
1954   if ((i_delay != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1955                                                       "MLP_COEFFICIENT_QUALITY_DELAY",
1956                                                       &tmp)))
1957
1958         mlp->pv.co_Q[i_delay] = (double) tmp / 100;
1959   else
1960         mlp->pv.co_Q[i_delay] = DEFAULT_QUALITY;
1961
1962   if ((i_distance != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1963                                                       "MLP_COEFFICIENT_QUALITY_DISTANCE",
1964                                                       &tmp)))
1965         mlp->pv.co_Q[i_distance] = (double) tmp / 100;
1966   else
1967         mlp->pv.co_Q[i_distance] = DEFAULT_QUALITY;
1968
1969   /* Get minimum bandwidth per used address from configuration */
1970   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1971                                                       "MLP_MIN_BANDWIDTH",
1972                                                       &tmp))
1973     b_min = tmp;
1974   else
1975   {
1976     b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1977   }
1978
1979   /* Get minimum number of connections from configuration */
1980   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1981                                                       "MLP_MIN_CONNECTIONS",
1982                                                       &tmp))
1983     n_min = tmp;
1984   else
1985     n_min = DEFAULT_MIN_CONNECTIONS;
1986
1987   /* Init network quotas */
1988   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1989   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1990   {
1991                 found = GNUNET_NO;
1992           for (c2 = 0; c2 < dest_length; c2++)
1993           {
1994                         if (quotas[c] == network[c2])
1995                   {
1996                                         mlp->pv.quota_index[c] = network[c2];
1997                                         mlp->pv.quota_out[c] = out_dest[c2];
1998                       mlp->pv.quota_in[c] = in_dest[c2];
1999                       found = GNUNET_YES;
2000                       LOG (GNUNET_ERROR_TYPE_DEBUG, "Quota for network `%s' (in/out) %llu/%llu\n",
2001                                                                 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2002                                                                 mlp->pv.quota_out[c],
2003                                                                 mlp->pv.quota_in[c]);
2004                       break;
2005                   }
2006           }
2007
2008       /* Check if defined quota could make problem unsolvable */
2009       if ((n_min * b_min) > mlp->pv.quota_out[c])
2010       {
2011         LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2012                         GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2013                         mlp->pv.quota_out[c],
2014                         (n_min * b_min));
2015         mlp->pv.quota_out[c] = (n_min * b_min);
2016       }
2017       if ((n_min * b_min) > mlp->pv.quota_in[c])
2018       {
2019         LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2020                         GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2021                         mlp->pv.quota_in[c],
2022                         (n_min * b_min));
2023         mlp->pv.quota_in[c] = (n_min * b_min);
2024       }
2025
2026       /* Check if bandwidth is too big to make problem solvable */
2027       if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
2028       {
2029         LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
2030                         GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2031                         mlp->pv.quota_out[c],
2032                         mlp->pv.BIG_M);
2033         mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
2034       }
2035       if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
2036       {
2037         LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
2038                         GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2039                         mlp->pv.quota_in[c],
2040                         mlp->pv.BIG_M);
2041         mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
2042       }
2043
2044           if (GNUNET_NO == found)
2045                         {
2046                 mlp->pv.quota_in[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2047                 mlp->pv.quota_out[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2048                                 LOG (GNUNET_ERROR_TYPE_INFO, _("Using default quota configuration for network `%s' (in/out) %llu/%llu\n"),
2049                                                 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2050                                                 mlp->pv.quota_in[c],
2051                                                 mlp->pv.quota_out[c]);
2052                         }
2053   }
2054
2055   /* Assign options to handle */
2056   mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
2057   mlp->addresses = addresses;
2058   mlp->bw_changed_cb = bw_changed_cb;
2059   mlp->bw_changed_cb_cls = bw_changed_cb_cls;
2060   mlp->get_preferences = get_preference;
2061   mlp->get_preferences_cls = get_preference_cls;
2062   mlp->get_properties = get_properties;
2063   mlp->get_properties_cls = get_properties_cls;
2064   /* Setting MLP Input variables */
2065   mlp->pv.co_D = D;
2066   mlp->pv.co_R = R;
2067   mlp->pv.co_U = U;
2068   mlp->pv.b_min = b_min;
2069   mlp->pv.n_min = n_min;
2070   mlp->pv.m_q = GNUNET_ATS_QualityPropertiesCount;
2071   mlp->mlp_prob_changed = GNUNET_NO;
2072   mlp->mlp_prob_updated = GNUNET_NO;
2073   mlp->mlp_auto_solve = GNUNET_YES;
2074   mlp->requested_peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2075   mlp->bulk_request = 0;
2076   mlp->bulk_lock = 0;
2077
2078   /* Setup GLPK */
2079   /* Redirect GLPK output to GNUnet logging */
2080   glp_term_hook (&mlp_term_hook, (void *) mlp);
2081
2082   /* Init LP solving parameters */
2083   glp_init_smcp(&mlp->control_param_lp);
2084   mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
2085 #if VERBOSE_GLPK
2086   mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
2087 #endif
2088   mlp->control_param_lp.it_lim = max_iterations;
2089   mlp->control_param_lp.tm_lim = max_duration.rel_value;
2090
2091   /* Init MLP solving parameters */
2092   glp_init_iocp(&mlp->control_param_mlp);
2093   mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
2094 #if VERBOSE_GLPK
2095   mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
2096 #endif
2097   mlp->control_param_mlp.tm_lim = max_duration.rel_value;
2098
2099   LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
2100
2101   return mlp;
2102 }
2103
2104 /* end of gnunet-service-ats_addresses_mlp.c */