2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file ats/gnunet-service-ats-solver_mlp.c
23 * @brief ats mlp problem solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-ats-solver_mlp.h"
30 #include "gnunet_statistics_service.h"
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
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
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.
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.
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.
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.
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.
82 * The quotas for each network segment are passed by addresses. MLP can be
83 * adapted using configuration settings and uses the following parameters:
85 * Maximum duration for a MLP solution procees (default: 3 sec.)
87 * Maximum number of iterations for a MLP solution process (default:
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)
112 * 5.2 Data structures used
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.
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.
135 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
138 * Print debug output for mlp problem creation
140 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
143 * Enable GLPK verbose output
145 #define VERBOSE_GLPK GNUNET_NO
148 * Maximize bandwidth assigned
150 * This option can be used to test if problem can be solved at all without
151 * optimizing for utility, diversity or relativity
154 #define MAXIMIZE_FOR_BANDWIDTH_ASSIGNED GNUNET_NO
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
163 mlp_term_hook (void *info, const char *s)
165 /* Not needed atm struct MLP_information *mlp = info; */
166 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s", s);
172 * Reset peers for next problem creation
174 * @param cls not used
176 * @param value ATS_Peer
180 reset_peers (void *cls, const struct GNUNET_HashCode * key, void *value)
182 struct ATS_Peer *peer = value;
183 peer->processed = GNUNET_NO;
188 * Delete the MLP problem and free the constrain matrix
190 * @param mlp the MLP handle
193 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
198 if (mlp->p.prob != NULL)
200 glp_delete_prob(mlp->p.prob);
204 /* delete row index */
205 if (mlp->p.ia != NULL)
207 GNUNET_free (mlp->p.ia);
211 /* delete column index */
212 if (mlp->p.ja != NULL)
214 GNUNET_free (mlp->p.ja);
218 /* delete coefficients */
219 if (mlp->p.ar != NULL)
221 GNUNET_free (mlp->p.ar);
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;
240 GNUNET_CONTAINER_multihashmap_iterate (mlp->requested_peers, &reset_peers, NULL);
245 * Translate ATS properties to text
246 * Just intended for debugging
248 * @param ats_index the ATS index
249 * @return string with result
252 mlp_ats_to_string (int 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";
280 * Translate glpk status error codes to text
281 * @param retcode return code
282 * @return string with result
285 mlp_status_to_string (int retcode)
289 return "solution is undefined";
291 return "solution is feasible";
293 return "solution is infeasible";
295 return "no feasible solution exists";
297 return "solution is optimal";
299 return "solution is unbounded";
302 return "unknown error";
307 * Translate glpk solver error codes to text
308 * @param retcode return code
309 * @return string with result
312 mlp_solve_to_string (int retcode)
318 return "invalid basis";
320 return "singular matrix";
322 return "ill-conditioned matrix";
324 return "invalid bounds";
326 return "solver failed";
328 return "objective lower limit reached";
330 return "objective upper limit reached";
332 return "iteration limit exceeded";
334 return "time limit exceeded";
336 return "no primal feasible solution";
338 return "no dual feasible solution";
340 return "root LP optimum not provided";
342 return "search terminated by application";
344 return "relative mip gap tolerance reached";
346 return "no dual feasible solution";
348 return "no convergence";
350 return "numerical instability";
352 return "invalid data";
354 return "result out of range";
357 return "unknown error";
362 * Extract an ATS performance info from an address
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
369 get_performance_info (struct ATS_Address *address, uint32_t type)
372 GNUNET_assert (NULL != address);
374 if ((NULL == address->atsi) || (0 == address->atsi_count))
375 return GNUNET_ATS_VALUE_UNDEFINED;
377 for (c1 = 0; c1 < address->atsi_count; c1++)
379 if (ntohl(address->atsi[c1].type) == type)
380 return ntohl(address->atsi[c1].value);
382 return GNUNET_ATS_VALUE_UNDEFINED;
388 struct GNUNET_CONTAINER_MultiHashMap * peers;
393 mlp_create_problem_count_addresses_it (void *cls, const struct GNUNET_HashCode *key, void *value)
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))
402 static int mlp_create_problem_count_addresses (
403 struct GNUNET_CONTAINER_MultiHashMap * peers,
404 const struct GNUNET_CONTAINER_MultiHashMap * addresses)
406 struct CountContext cctx;
409 GNUNET_CONTAINER_multihashmap_iterate (addresses, &mlp_create_problem_count_addresses_it, &cctx);
415 * Updates an existing value in the matrix
417 * Extract the row, updates the value and updates the row in the problem
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 * @param GNUNET_YES value changed, GNUNET_NO value did not change, GNUNET_SYSERR
428 mlp_create_problem_update_value (struct MLP_Problem *p,
429 int row, int col, double val,
439 GNUNET_assert (NULL != p);
440 GNUNET_assert (NULL != p->prob);
442 /* Get number of columns and prepare data structure */
443 c_cols = glp_get_num_cols(p->prob);
445 return GNUNET_SYSERR;
447 val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
448 GNUNET_assert (NULL != val_array);
449 ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
450 GNUNET_assert (NULL != ind_array);
451 /* Extract the row */
453 if (0 == (c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array)))
455 GNUNET_free (ind_array);
456 GNUNET_free (val_array);
457 return GNUNET_SYSERR;
460 /* Update the value */
461 for (c1 = 1; c1 < (c_elems+1); c1++)
463 if (ind_array[c1] == col)
466 if ((c_elems + 1)== c1)
468 GNUNET_free (ind_array);
469 GNUNET_free (val_array);
470 return GNUNET_SYSERR; /* not found */
473 LOG (GNUNET_ERROR_TYPE_ERROR, "[P] Updating value in [%s : %s] from `%.2f' to `%.2f'\n",
474 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
476 if (val != val_array[c1])
482 /* Update the row in the matrix */
483 glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
484 GNUNET_free (ind_array);
485 GNUNET_free (val_array);
490 * Creates a new value in the matrix
492 * Sets the row and column index in the problem array and increments the
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
502 mlp_create_problem_set_value (struct MLP_Problem *p,
503 int row, int col, double val,
506 if ((p->ci) >= p->num_elements)
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);
513 if ((0 == row) || (0 == col))
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]);
526 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
527 unsigned int type, unsigned int bound, double lb, double ub,
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",
543 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
544 unsigned int bound, double lb, double ub)
547 int row = glp_add_rows (p->prob, 1);
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);
554 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
557 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
560 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
563 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
566 GNUNET_asprintf(&op, "ERROR");
569 #if DEBUG_MLP_PROBLEM_CREATION
570 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
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
585 mlp_create_problem_add_address_information (void *cls, const struct GNUNET_HashCode *key, void *value)
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;
597 /* Check if we have to add this peer due to a pending request */
598 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->requested_peers, key))
601 mlpi = address->solver_information;
604 fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
610 peer = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, key);
611 if (peer->processed == GNUNET_NO)
613 /* Add peer dependent constraints */
614 /* Add constraint c2 */
615 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
616 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
618 /* Add constraint c9 */
619 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
620 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
622 /* c 9) set coefficient */
623 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f, __LINE__);
624 peer->processed = GNUNET_YES;
627 /* Reset addresses' solver information */
634 /* Add bandwidth column */
635 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
636 #if TEST_MAX_BW_ASSIGNMENT
637 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
639 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
644 /* Add usage column */
645 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
646 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
649 /* Add address dependent constraints */
650 /* Add constraint c1) bandwidth capping
651 * b_t + (-M) * n_t <= 0
653 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
654 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
657 /* c1) set b = 1 coefficient */
658 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
659 /* c1) set n = -M coefficient */
660 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -mlp->pv.BIG_M, __LINE__);
662 /* Add constraint c 3) minimum bandwidth
663 * b_t + (-n_t * b_min) >= 0
665 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
666 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
669 /* c3) set b = 1 coefficient */
670 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
671 /* c3) set n = -b_min coefficient */
672 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
675 /* Set coefficient entries in invariant rows */
676 /* c 4) minimum connections */
677 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
678 /* c 6) maximize diversity */
679 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
680 /* c 2) 1 address peer peer */
681 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
682 /* c 9) relativity */
683 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
685 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
687 /* c 10) obey network specific quotas
688 * (1)*b_1 + ... + (1)*b_m <= quota_n
690 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
692 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
693 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
694 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
696 if (mlp->pv.quota_index[c] == addr_net)
698 mlp_create_problem_set_value (p, p->r_quota[c], mlpi->c_b, 1, __LINE__);
703 /* c 7) Optimize quality */
704 /* For all quality metrics, set quality of this address */
705 props = mlp->get_properties (mlp->get_properties_cls, address);
706 for (c = 0; c < mlp->pv.m_q; c++)
707 mlp_create_problem_set_value (p, p->r_q[c], mlpi->c_b, props[c], __LINE__);
713 * Create the invariant columns c4, c6, c10, c8, c7
716 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
721 /* Row for c4) minimum connection */
722 /* Number of minimum connections is min(|Peers|, n_min) */
723 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);
725 /* Add row for c6) */
726 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
728 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
730 /* Add rows for c 10) */
731 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
734 GNUNET_asprintf(&text, "c10_quota_ats_%s", GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
735 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
739 /* Adding rows for c 8) */
740 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
742 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
744 /* c 7) For all quality metrics */
745 for (c = 0; c < mlp->pv.m_q; c++)
747 GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->pv.q[c]));
748 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
750 mlp_create_problem_set_value (p, p->r_q[c], p->c_q[c], -1, __LINE__);
756 * Create the invariant columns d, u, r, q0 ... qm
759 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
764 #if TEST_MAX_BW_ASSIGNMENT
769 //mlp->pv.co_R = 0.0;
771 /* Diversity d column */
772 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
774 /* Utilization u column */
775 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
777 /* Relativity r column */
778 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
780 /* Quality metric columns */
781 for (c = 0; c < mlp->pv.m_q; c++)
783 GNUNET_asprintf (&name, "q_%u", mlp->pv.q[c]);
784 #if TEST_MAX_BW_ASSIGNMENT
785 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
787 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
795 * Create the MLP problem
797 * @param mlp the MLP handle
798 * @param addresses the hashmap containing all adresses
799 * @return GNUNET_OK or GNUNET_SYSERR
802 mlp_create_problem (struct GAS_MLP_Handle *mlp)
804 struct MLP_Problem *p = &mlp->p;
807 GNUNET_assert (p->prob == NULL);
808 GNUNET_assert (p->ia == NULL);
809 GNUNET_assert (p->ja == NULL);
810 GNUNET_assert (p->ar == NULL);
811 /* Reset MLP problem struct */
813 /* create the glpk problem */
814 p->prob = glp_create_prob ();
815 GNUNET_assert (NULL != p->prob);
816 p->num_peers = GNUNET_CONTAINER_multihashmap_size (mlp->requested_peers);
817 p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers, mlp->addresses);
819 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
820 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses + mlp->pv.m_q + p->num_peers + 2 + 1);
821 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
822 p->num_peers, p->num_addresses, mlp->pv.m_q, p->num_elements);
824 /* Set a problem name */
825 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
826 /* Set optimization direction to maximize */
827 glp_set_obj_dir (p->prob, GLP_MAX);
829 /* Create problem matrix */
830 /* last +1 caused by glpk index starting with one: [1..elements]*/
833 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
835 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
837 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
839 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
841 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
842 return GNUNET_SYSERR;
845 /* Adding invariant columns */
846 mlp_create_problem_add_invariant_columns (mlp, p);
848 /* Adding address independent constraint rows */
849 mlp_create_problem_add_invariant_rows (mlp, p);
851 /* Adding address dependent columns constraint rows */
852 GNUNET_CONTAINER_multihashmap_iterate (mlp->addresses, &mlp_create_problem_add_address_information, mlp);
854 /* Load the matrix */
855 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
856 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
862 * Solves the LP problem
864 * @param mlp the MLP Handle
865 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
868 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
872 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
874 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
876 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
878 /* Analyze problem status */
879 res = glp_get_status (mlp->p.prob);
881 /* solution is optimal */
883 /* solution is feasible */
885 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n",
886 res, mlp_status_to_string(res));
888 /* Problem was ill-defined, no way to handle that */
890 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed, no solution: 0x%02X %s\n",
891 res, mlp_status_to_string(res));
892 return GNUNET_SYSERR;
898 * Solves the MLP problem
900 * @param mlp the MLP Handle
901 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
904 mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
907 res = glp_intopt(mlp->p.prob, &mlp->control_param_mlp);
909 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
911 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving MLP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
912 /* Analyze problem status */
913 res = glp_mip_status(mlp->p.prob);
915 /* solution is optimal */
917 /* solution is feasible */
919 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_status_to_string(res));
921 /* Problem was ill-defined, no way to handle that */
923 LOG (GNUNET_ERROR_TYPE_WARNING,"Solving MLP problem failed, 0x%02X %s\n\n", res, mlp_status_to_string(res));
924 return GNUNET_SYSERR;
930 * Propagates the results when MLP problem was solved
932 * @param cls the MLP handle
933 * @param key the peer identity
934 * @param value the address
935 * @return GNUNET_OK to continue
938 mlp_propagate_results (void *cls, const struct GNUNET_HashCode *key, void *value)
940 struct GAS_MLP_Handle *mlp = cls;
941 struct ATS_Address *address;
942 struct MLP_information *mlpi;
943 double mlp_bw_in = MLP_NaN;
944 double mlp_bw_out = MLP_NaN;
945 double mlp_use = MLP_NaN;
947 /* Check if we have to add this peer due to a pending request */
948 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->requested_peers, key))
951 GNUNET_assert (address->solver_information != NULL);
952 mlpi = address->solver_information;
954 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
955 if (mlp_bw_in > (double) UINT32_MAX)
957 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
958 mlp_bw_in = (double) UINT32_MAX;
960 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
961 if (mlp_bw_out > (double) UINT32_MAX)
963 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
964 mlp_bw_out = (double) UINT32_MAX;
966 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
970 if ((GLP_YES == mlp_use) && (GNUNET_NO == address->active))
972 /* Address switch: Activate address*/
973 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
974 address->active = GNUNET_YES;
975 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
976 mlpi->b_in.value__ = htonl(mlp_bw_in);
977 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
978 mlpi->b_out.value__ = htonl(mlp_bw_out);
980 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
982 else if ((GLP_NO == mlp_use) && (GNUNET_YES == address->active))
984 /* Address switch: Disable address*/
985 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
986 address->active = GNUNET_NO;
987 /* Set bandwidth to 0 */
988 address->assigned_bw_in.value__ = htonl (0);
989 mlpi->b_in.value__ = htonl(mlp_bw_in);
990 address->assigned_bw_out.value__ = htonl (0);
991 mlpi->b_out.value__ = htonl(mlp_bw_out);
993 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
995 else if ((mlp_bw_out != ntohl(address->assigned_bw_out.value__)) ||
996 (mlp_bw_in != ntohl(address->assigned_bw_in.value__)))
998 /* Bandwidth changed */
999 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1000 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
1001 mlpi->b_in.value__ = htonl(mlp_bw_in);
1002 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
1003 mlpi->b_out.value__ = htonl(mlp_bw_out);
1005 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1009 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1018 * Solves the MLP problem
1020 * @param solver the MLP Handle
1021 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
1024 GAS_mlp_solve_problem (void *solver)
1026 struct GAS_MLP_Handle *mlp = solver;
1030 struct GNUNET_TIME_Absolute start_build;
1031 struct GNUNET_TIME_Relative duration_build;
1032 struct GNUNET_TIME_Absolute start_lp;
1033 struct GNUNET_TIME_Relative duration_lp;
1034 struct GNUNET_TIME_Absolute start_mlp;
1035 struct GNUNET_TIME_Relative duration_mlp;
1036 GNUNET_assert (NULL != solver);
1038 if (GNUNET_YES == mlp->bulk_lock)
1040 mlp->bulk_request ++;
1044 if (0 == GNUNET_CONTAINER_multihashmap_size(mlp->requested_peers))
1049 if (0 == GNUNET_CONTAINER_multihashmap_size(mlp->addresses))
1055 if ((GNUNET_NO == mlp->mlp_prob_changed) && (GNUNET_NO == mlp->mlp_prob_updated))
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
1060 if (GNUNET_YES == mlp->mlp_prob_changed)
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
1063 mlp_delete_problem (mlp);
1064 start_build = GNUNET_TIME_absolute_get();
1065 if (GNUNET_SYSERR == mlp_create_problem (mlp))
1066 return GNUNET_SYSERR;
1067 duration_build = GNUNET_TIME_absolute_get_duration (start_build);
1068 mlp->control_param_lp.presolve = GLP_YES;
1069 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
1074 duration_build.rel_value = 0;
1078 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running LP solver %s\n", (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
1079 start_lp = GNUNET_TIME_absolute_get();
1080 res_lp = mlp_solve_lp_problem (mlp);
1081 duration_lp = GNUNET_TIME_absolute_get_duration (start_lp);
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
1086 start_mlp = GNUNET_TIME_absolute_get();
1087 res_mip = mlp_solve_mlp_problem (mlp);
1089 duration_mlp = GNUNET_TIME_absolute_get_duration (start_mlp);
1092 mlp->ps.lp_res = res_lp;
1093 mlp->ps.mip_res = res_mip;
1094 mlp->ps.build_dur = duration_build;
1095 mlp->ps.lp_dur = duration_lp;
1096 mlp->ps.mip_dur = duration_mlp;
1097 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1098 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1099 mlp->ps.p_cols = glp_get_num_cols (mlp->p.prob);
1100 mlp->ps.p_rows = glp_get_num_rows (mlp->p.prob);
1101 mlp->ps.p_elements = mlp->p.num_elements;
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, "Execution time: Build %llu ms, LP %llu ms, MLP %llu ms\n",
1104 (unsigned long long) duration_build.rel_value,
1105 (unsigned long long) duration_lp.rel_value,
1106 (unsigned long long) duration_mlp.rel_value);
1108 /* Propagate result*/
1109 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1110 GNUNET_CONTAINER_multihashmap_iterate (mlp->addresses, &mlp_propagate_results, mlp);
1112 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1113 if (GNUNET_YES == mlp->write_mip_mps)
1115 /* Write problem to disk */
1116 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value);
1117 LOG (GNUNET_ERROR_TYPE_ERROR, "DUMP: %s \n", filename);
1118 glp_write_lp(mlp->p.prob, NULL, filename);
1119 GNUNET_free (filename);
1121 if (GNUNET_YES == mlp->write_mip_sol)
1123 /* Write solution to disk */
1124 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value);
1125 glp_print_mip (mlp->p.prob, filename );
1126 LOG (GNUNET_ERROR_TYPE_ERROR, "DUMP: %s \n", filename);
1127 GNUNET_free (filename);
1130 /* Reset change and update marker */
1131 mlp->control_param_lp.presolve = GLP_NO;
1132 mlp->mlp_prob_updated = GNUNET_NO;
1133 mlp->mlp_prob_changed = GNUNET_NO;
1135 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1138 return GNUNET_SYSERR;
1142 * Add a single address to the solve
1144 * @param solver the solver Handle
1145 * @param addresses the address hashmap containing all addresses
1146 * @param address the address to add
1147 * @param network network type of this address
1150 GAS_mlp_address_add (void *solver,
1151 struct ATS_Address *address,
1154 struct GAS_MLP_Handle *mlp = solver;
1157 GNUNET_assert (NULL != solver);
1158 GNUNET_assert (NULL != address);
1160 if (NULL == address->solver_information)
1162 address->solver_information = GNUNET_malloc (sizeof (struct MLP_information));
1165 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding address for peer `%s' multiple times\n"), GNUNET_i2s(&address->peer));
1167 /* Is this peer included in the problem? */
1168 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &address->peer.hashPubKey)))
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1175 /* Problem size changed: new address for peer with pending request */
1176 mlp->mlp_prob_changed = GNUNET_YES;
1177 if (GNUNET_YES == mlp->mlp_auto_solve)
1178 GAS_mlp_solve_problem (solver);
1183 /* Check for network update */
1184 if (type == GNUNET_ATS_NETWORK_TYPE)
1186 new_value = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1187 if (GNUNET_ATS_VALUE_UNDEFINED == new_value)
1188 new_value = GNUNET_ATS_NET_UNSPECIFIED;
1189 if (new_value != prev_value)
1191 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating network for peer `%s' from `%s' to `%s'\n",
1192 GNUNET_i2s (&address->peer),
1193 GNUNET_ATS_print_network_type(prev_value),
1194 GNUNET_ATS_print_network_type(new_value));
1197 if (mlpi->c_b == MLP_UNDEFINED)
1198 continue; /* This address is not yet in the matrix*/
1200 rows = glp_get_num_rows(mlp->p.prob);
1201 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1202 val = GNUNET_malloc (rows * sizeof (double) + 1);
1203 int length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1205 for (c_net = 0; c_net <= length + 1; c_net ++)
1207 if (ind[c_net] == mlp->p.r_quota[prev_value])
1208 break; /* Found index for old network */
1211 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1212 /* Set updated column */
1213 ind[c_net] = mlp->p.r_quota[new_value];
1215 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1219 rows = glp_get_num_rows(mlp->p.prob);
1220 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1221 val = GNUNET_malloc (rows * sizeof (double) + 1);
1222 length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1224 for (c_net = 0; c_net <= length + 1; c_net ++)
1226 if (ind[c_net] == mlp->p.r_quota[prev_value])
1227 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing old network index [%u] == [%f]\n",ind[c_net],val[c_net]);
1228 if (ind[c_net] == mlp->p.r_quota[new_value])
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting new network index [%u] == [%f]\n",ind[c_net],val[c_net]);
1236 mlp->mlp_prob_changed = GNUNET_YES;
1242 GAS_mlp_address_property_changed (void *solver,
1243 struct ATS_Address *address,
1248 struct MLP_information *mlpi = address->solver_information;
1249 struct GAS_MLP_Handle *mlp = solver;
1254 GNUNET_assert (NULL != solver);
1255 GNUNET_assert (NULL != address);
1259 LOG (GNUNET_ERROR_TYPE_ERROR,
1260 _("Updating address property `%s' for peer `%s' %p not added before\n"),
1261 GNUNET_ATS_print_property_type (type),
1262 GNUNET_i2s(&address->peer),
1268 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers,
1269 &address->peer.hashPubKey)))
1271 /* Peer is not requested, so no need to update problem */
1274 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating property `%s' address for peer `%s'\n",
1275 GNUNET_ATS_print_property_type (type),
1276 GNUNET_i2s(&address->peer));
1278 /* Find row index */
1280 for (c1 = 0; c1 < mlp->pv.m_q; c1++)
1282 if (type == mlp->pv.q[c1])
1288 if (-1 == type_index)
1291 return; /* quality index not found */
1294 LOG (GNUNET_ERROR_TYPE_ERROR,
1295 _("Updating address property `%s' for peer `%s' %p\n"),
1296 GNUNET_ATS_print_property_type (type),
1297 GNUNET_i2s(&address->peer),
1299 type_index, mlp->p.r_q[type_index]);
1301 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index] */
1302 if (GNUNET_YES == mlp_create_problem_update_value (&mlp->p,
1303 mlp->p.r_q[type_index], mlpi->c_b, rel_value, __LINE__))
1305 mlp->mlp_prob_updated = GNUNET_YES;
1306 if (GNUNET_YES == mlp->mlp_auto_solve)
1307 GAS_mlp_solve_problem (solver);
1313 GAS_mlp_address_session_changed (void *solver,
1314 struct ATS_Address *address,
1315 uint32_t cur_session,
1316 uint32_t new_session)
1318 /* Nothing to do here */
1323 GAS_mlp_address_inuse_changed (void *solver,
1324 struct ATS_Address *address,
1328 /* Nothing to do here */
1334 GAS_mlp_address_change_network (void *solver,
1335 struct ATS_Address *address,
1336 uint32_t current_network,
1337 uint32_t new_network)
1339 struct MLP_information *mlpi = address->solver_information;
1340 struct GAS_MLP_Handle *mlp = solver;
1342 int nets_avail[] = GNUNET_ATS_NetworkType;
1350 GNUNET_assert (NULL != solver);
1351 GNUNET_assert (NULL != address);
1359 if (mlpi->c_b == MLP_UNDEFINED)
1360 return; /* This address is not yet in the matrix*/
1362 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers,
1363 &address->peer.hashPubKey)))
1365 /* Peer is not requested, so no need to update problem */
1370 if (current_network == new_network)
1376 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount ; c1 ++)
1378 if (nets_avail[c1] == new_network)
1382 if (GNUNET_ATS_NetworkTypeCount == c1)
1384 /* Invalid network */
1389 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating network for peer `%s' from `%s' to `%s'\n",
1390 GNUNET_i2s (&address->peer),
1391 GNUNET_ATS_print_network_type(current_network),
1392 GNUNET_ATS_print_network_type(new_network));
1394 /* Get row for this address */
1395 rows = glp_get_num_rows(mlp->p.prob);
1396 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1397 val = GNUNET_malloc (rows * sizeof (double) + 1);
1398 length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1400 /* Remove index from old network */
1401 for (c1 = 1; c1 < length +1; c1 ++)
1403 if (ind[c1] == mlp->p.r_quota[c1])
1404 break; /* Found index for old network */
1406 val[c1] = 0.0; /* Remove from previous network */
1407 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1408 ind[c1] = mlp->p.r_quota[new_network]; /* Add to new network */
1410 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1414 length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1415 for (c1 = 1; c1 < length + 1; c1 ++)
1417 fprintf (stderr, "%u: %s\n", ind[c1], glp_get_row_name(mlp->p.prob, ind[c1]));
1420 mlp->mlp_prob_changed = GNUNET_YES;
1425 * Deletes a single address in the MLP problem
1427 * The MLP problem has to be recreated and the problem has to be resolved
1429 * @param solver the MLP Handle
1430 * @param addresses the address hashmap
1431 * the address has to be already removed from the hashmap
1432 * @param address the address to delete
1433 * @param session_only delete only session not whole address
1436 GAS_mlp_address_delete (void *solver,
1437 struct ATS_Address *address,
1441 struct GAS_MLP_Handle *mlp = solver;
1442 struct MLP_information *mlpi;
1444 GNUNET_assert (NULL != solver);
1445 GNUNET_assert (NULL != address);
1447 mlpi = address->solver_information;
1448 if ((GNUNET_NO == session_only) && (NULL != mlpi))
1451 address->solver_information = NULL;
1454 /* Is this peer included in the problem? */
1455 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &address->peer.hashPubKey)))
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s for peer `%s' without address request \n",
1458 (session_only == GNUNET_YES) ? "session" : "address",
1459 GNUNET_i2s(&address->peer));
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s for peer `%s' with address request \n",
1463 (session_only == GNUNET_YES) ? "session" : "address",
1464 GNUNET_i2s(&address->peer));
1466 /* Problem size changed: new address for peer with pending request */
1467 mlp->mlp_prob_changed = GNUNET_YES;
1468 if (GNUNET_YES == mlp->mlp_auto_solve)
1469 GAS_mlp_solve_problem (solver);
1475 * Find the active address in the set of addresses of a peer
1476 * @param cls destination
1477 * @param key peer id
1478 * @param value address
1482 mlp_get_preferred_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1485 struct ATS_Address *aa = (struct ATS_Address *) cls;
1486 struct ATS_Address *addr = value;
1487 struct MLP_information *mlpi = addr->solver_information;
1490 if (mlpi->n == GNUNET_YES)
1493 aa->assigned_bw_in = mlpi->b_in;
1494 aa->assigned_bw_out = mlpi->b_out;
1501 static double get_peer_pref_value (struct GAS_MLP_Handle *mlp, const struct GNUNET_PeerIdentity *peer)
1504 const double *preferences = NULL;
1506 preferences = mlp->get_preferences (mlp->get_preferences_cls, peer);
1509 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1511 if (c != GNUNET_ATS_PREFERENCE_END)
1513 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
1514 res += preferences[c];
1517 res /= (GNUNET_ATS_PreferenceCount -1);
1523 * Get the preferred address for a specific peer
1525 * @param solver the MLP Handle
1526 * @param addresses address hashmap
1527 * @param peer the peer
1528 * @return suggested address
1530 const struct ATS_Address *
1531 GAS_mlp_get_preferred_address (void *solver,
1532 const struct GNUNET_PeerIdentity *peer)
1534 struct GAS_MLP_Handle *mlp = solver;
1536 struct ATS_Address *res = NULL;
1538 GNUNET_assert (NULL != solver);
1539 GNUNET_assert (NULL != peer);
1541 LOG (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n",
1544 /* Is this peer included in the problem? */
1545 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1547 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding peer `%s' to list of requested_peers with requests\n",
1550 p = GNUNET_malloc (sizeof (struct ATS_Peer));
1552 p->f = get_peer_pref_value (mlp, peer);
1553 GNUNET_CONTAINER_multihashmap_put (mlp->requested_peers, &peer->hashPubKey, p, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1555 /* Added new peer, we have to rebuild problem before solving */
1556 mlp->mlp_prob_changed = GNUNET_YES;
1558 if (GNUNET_YES == mlp->mlp_auto_solve)
1559 GAS_mlp_solve_problem (mlp);
1561 /* Get prefered address */
1562 GNUNET_CONTAINER_multihashmap_get_multiple (mlp->addresses, &peer->hashPubKey,
1563 mlp_get_preferred_address_it, res);
1570 * Start a bulk operation
1572 * @param solver the solver
1575 GAS_mlp_bulk_start (void *solver)
1577 LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1578 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1580 GNUNET_assert (NULL != solver);
1586 GAS_mlp_bulk_stop (void *solver)
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1590 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1591 GNUNET_assert (NULL != solver);
1593 if (s->bulk_lock < 1)
1600 if (0 < s->bulk_request)
1602 GAS_mlp_solve_problem (solver);
1610 * Stop notifying about address and bandwidth changes for this peer
1612 * @param solver the MLP handle
1613 * @param addresses address hashmap
1614 * @param peer the peer
1617 GAS_mlp_stop_get_preferred_address (void *solver,
1618 const struct GNUNET_PeerIdentity *peer)
1620 struct GAS_MLP_Handle *mlp = solver;
1621 struct ATS_Peer *p = NULL;
1623 GNUNET_assert (NULL != solver);
1624 GNUNET_assert (NULL != peer);
1626 if (NULL != (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1628 GNUNET_CONTAINER_multihashmap_remove (mlp->requested_peers, &peer->hashPubKey, p);
1635 * Changes the preferences for a peer in the MLP problem
1637 * @param solver the MLP Handle
1638 * @param addresses the address hashmap
1639 * @param peer the peer
1640 * @param kind the kind to change the preference
1641 * @param pref_rel the relative score
1644 GAS_mlp_address_change_preference (void *solver,
1645 const struct GNUNET_PeerIdentity *peer,
1646 enum GNUNET_ATS_PreferenceKind kind,
1649 struct GAS_MLP_Handle *mlp = solver;
1650 struct ATS_Peer *p = NULL;
1652 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing preference for address for peer `%s' to %.2f\n",
1653 GNUNET_i2s(peer), pref_rel);
1655 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1656 /* Update the constraints with changed preferences */
1658 /* Update quality constraint c7 */
1660 /* Update relativity constraint c9 */
1661 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1663 LOG (GNUNET_ERROR_TYPE_ERROR, "Updating preference for unknown peer `%s'\n", GNUNET_i2s(peer));
1666 p->f = get_peer_pref_value (mlp, peer);
1667 LOG (GNUNET_ERROR_TYPE_ERROR, "PEER PREF: %s %.2f\n",
1668 GNUNET_i2s(peer), p->f);
1669 mlp_create_problem_update_value (&mlp->p, p->r_c9, mlp->p.c_r, -p->f, __LINE__);
1671 /* Problem size changed: new address for peer with pending request */
1672 mlp->mlp_prob_updated = GNUNET_YES;
1673 if (GNUNET_YES == mlp->mlp_auto_solve)
1674 GAS_mlp_solve_problem (solver);
1680 mlp_free_peers (void *cls, const struct GNUNET_HashCode *key, void *value)
1682 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
1683 struct ATS_Peer *p = value;
1685 GNUNET_CONTAINER_multihashmap_remove (map, key, value);
1693 * Shutdown the MLP problem solving component
1695 * @param solver the solver handle
1698 GAS_mlp_done (void *solver)
1700 struct GAS_MLP_Handle *mlp = solver;
1701 GNUNET_assert (mlp != NULL);
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down mlp solver\n");
1704 mlp_delete_problem (mlp);
1706 GNUNET_CONTAINER_multihashmap_iterate (mlp->requested_peers, &mlp_free_peers, mlp->requested_peers);
1707 GNUNET_CONTAINER_multihashmap_destroy (mlp->requested_peers);
1708 mlp->requested_peers = NULL;
1710 /* Clean up GLPK environment */
1714 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutdown down of mlp solver complete\n");
1719 * Init the MLP problem solving component
1721 * @param cfg the GNUNET_CONFIGURATION_Handle handle
1722 * @param stats the GNUNET_STATISTICS handle
1723 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1724 * @param out_dest array of outbound quotas
1725 * @param in_dest array of outbound quota
1726 * @param dest_length array length for quota arrays
1727 * @param bw_changed_cb callback for changed bandwidth amounts
1728 * @param bw_changed_cb_cls cls for callback
1729 * @param get_preference callback to get relative preferences for a peer
1730 * @param get_preference_cls cls for callback to get relative preferences
1731 * @return struct GAS_MLP_Handle on success, NULL on fail
1734 GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1735 const struct GNUNET_STATISTICS_Handle *stats,
1736 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1738 unsigned long long *out_dest,
1739 unsigned long long *in_dest,
1741 GAS_bandwidth_changed_cb bw_changed_cb,
1742 void *bw_changed_cb_cls,
1743 GAS_get_preferences get_preference,
1744 void *get_preference_cls,
1745 GAS_get_properties get_properties,
1746 void *get_properties_cls)
1748 struct GAS_MLP_Handle * mlp = GNUNET_malloc (sizeof (struct GAS_MLP_Handle));
1753 unsigned long long tmp;
1760 struct GNUNET_TIME_Relative max_duration;
1761 long long unsigned int max_iterations;
1763 GNUNET_assert (NULL != cfg);
1764 GNUNET_assert (NULL != stats);
1765 GNUNET_assert (NULL != addresses);
1766 GNUNET_assert (NULL != bw_changed_cb);
1767 GNUNET_assert (NULL != get_preference);
1768 GNUNET_assert (NULL != get_properties);
1770 /* Init GLPK environment */
1771 int res = glp_init_env();
1774 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1775 "initialization successful");
1778 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1779 "environment is already initialized");
1782 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1783 "initialization failed (insufficient memory)");
1788 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1789 "initialization failed (unsupported programming model)");
1797 mlp->write_mip_mps = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats",
1799 if (GNUNET_SYSERR == mlp->write_mip_mps)
1800 mlp->write_mip_mps = GNUNET_NO;
1801 mlp->write_mip_sol = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats",
1803 if (GNUNET_SYSERR == mlp->write_mip_sol)
1804 mlp->write_mip_sol = GNUNET_NO;
1806 mlp->pv.BIG_M = (double) BIG_M_VALUE;
1808 /* Get timeout for iterations */
1809 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(cfg, "ats", "MLP_MAX_DURATION", &max_duration))
1811 max_duration = MLP_MAX_EXEC_DURATION;
1814 /* Get maximum number of iterations */
1815 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "MLP_MAX_ITERATIONS", &max_iterations))
1817 max_iterations = MLP_MAX_ITERATIONS;
1820 /* Get diversity coefficient from configuration */
1821 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1822 "MLP_COEFFICIENT_D",
1824 D = (double) tmp / 100;
1828 /* Get proportionality coefficient from configuration */
1829 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1830 "MLP_COEFFICIENT_R",
1832 R = (double) tmp / 100;
1836 /* Get utilization coefficient from configuration */
1837 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1838 "MLP_COEFFICIENT_U",
1840 U = (double) tmp / 100;
1844 /* Get quality metric coefficients from configuration */
1845 int i_delay = MLP_NaN;
1846 int i_distance = MLP_NaN;
1847 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
1848 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1850 /* initialize quality coefficients with default value 1.0 */
1851 mlp->pv.co_Q[c] = DEFAULT_QUALITY;
1853 mlp->pv.q[c] = q[c];
1854 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
1856 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
1860 if ((i_delay != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1861 "MLP_COEFFICIENT_QUALITY_DELAY",
1864 mlp->pv.co_Q[i_delay] = (double) tmp / 100;
1866 mlp->pv.co_Q[i_delay] = DEFAULT_QUALITY;
1868 if ((i_distance != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1869 "MLP_COEFFICIENT_QUALITY_DISTANCE",
1871 mlp->pv.co_Q[i_distance] = (double) tmp / 100;
1873 mlp->pv.co_Q[i_distance] = DEFAULT_QUALITY;
1875 /* Get minimum bandwidth per used address from configuration */
1876 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1877 "MLP_MIN_BANDWIDTH",
1882 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1885 /* Get minimum number of connections from configuration */
1886 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1887 "MLP_MIN_CONNECTIONS",
1891 n_min = DEFAULT_MIN_CONNECTIONS;
1893 /* Init network quotas */
1894 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1895 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1898 for (c2 = 0; c2 < dest_length; c2++)
1900 if (quotas[c] == network[c2])
1902 mlp->pv.quota_index[c] = network[c2];
1903 mlp->pv.quota_out[c] = out_dest[c2];
1904 mlp->pv.quota_in[c] = in_dest[c2];
1906 LOG (GNUNET_ERROR_TYPE_DEBUG, "Quota for network `%s' (in/out) %llu/%llu\n",
1907 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1908 mlp->pv.quota_out[c],
1909 mlp->pv.quota_in[c]);
1914 /* Check if defined quota could make problem unsolvable */
1915 if ((n_min * b_min) > mlp->pv.quota_out[c])
1917 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
1918 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1919 mlp->pv.quota_out[c],
1921 mlp->pv.quota_out[c] = (n_min * b_min);
1923 if ((n_min * b_min) > mlp->pv.quota_in[c])
1925 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
1926 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1927 mlp->pv.quota_in[c],
1929 mlp->pv.quota_in[c] = (n_min * b_min);
1932 /* Check if bandwidth is too big to make problem solvable */
1933 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
1935 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
1936 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1937 mlp->pv.quota_out[c],
1939 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
1941 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
1943 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
1944 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1945 mlp->pv.quota_in[c],
1947 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
1950 if (GNUNET_NO == found)
1952 mlp->pv.quota_in[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1953 mlp->pv.quota_out[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1954 LOG (GNUNET_ERROR_TYPE_INFO, _("Using default quota configuration for network `%s' (in/out) %llu/%llu\n"),
1955 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1956 mlp->pv.quota_in[c],
1957 mlp->pv.quota_out[c]);
1961 /* Assign options to handle */
1962 mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1963 mlp->addresses = addresses;
1964 mlp->bw_changed_cb = bw_changed_cb;
1965 mlp->bw_changed_cb_cls = bw_changed_cb_cls;
1966 mlp->get_preferences = get_preference;
1967 mlp->get_preferences_cls = get_preference_cls;
1968 mlp->get_properties = get_properties;
1969 mlp->get_properties_cls = get_properties_cls;
1970 /* Setting MLP Input variables */
1974 mlp->pv.b_min = b_min;
1975 mlp->pv.n_min = n_min;
1976 mlp->pv.m_q = GNUNET_ATS_QualityPropertiesCount;
1977 mlp->mlp_prob_changed = GNUNET_NO;
1978 mlp->mlp_prob_updated = GNUNET_NO;
1979 mlp->mlp_auto_solve = GNUNET_YES;
1980 mlp->requested_peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1981 mlp->bulk_request = 0;
1985 /* Redirect GLPK output to GNUnet logging */
1986 glp_term_hook (&mlp_term_hook, (void *) mlp);
1988 /* Init LP solving parameters */
1989 glp_init_smcp(&mlp->control_param_lp);
1990 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
1992 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
1994 mlp->control_param_lp.it_lim = max_iterations;
1995 mlp->control_param_lp.tm_lim = max_duration.rel_value;
1997 /* Init MLP solving parameters */
1998 glp_init_iocp(&mlp->control_param_mlp);
1999 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
2001 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
2003 mlp->control_param_mlp.tm_lim = max_duration.rel_value;
2005 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
2010 /* end of gnunet-service-ats_addresses_mlp.c */