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