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 * @return 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,
440 GNUNET_assert (NULL != p);
441 GNUNET_assert (NULL != p->prob);
443 /* Get number of columns and prepare data structure */
444 c_cols = glp_get_num_cols(p->prob);
446 return GNUNET_SYSERR;
448 val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
449 GNUNET_assert (NULL != val_array);
450 ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
451 GNUNET_assert (NULL != ind_array);
452 /* Extract the row */
454 /* Update the value */
455 c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array);
457 for (c1 = 1; c1 < (c_elems+1); c1++)
459 if (ind_array[c1] == col)
465 if (GNUNET_NO == found)
467 ind_array[c_elems+1] = col;
468 val_array[c_elems+1] = val;
469 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Setting value in [%s : %s] to `%.2f'\n",
470 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
472 glp_set_mat_row (p->prob, row, c_elems+1, ind_array, val_array);
473 GNUNET_free (ind_array);
474 GNUNET_free (val_array);
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Updating value in [%s : %s] from `%.2f' to `%.2f'\n",
481 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
483 if (val != val_array[c1])
488 /* Update the row in the matrix */
489 glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
492 GNUNET_free (ind_array);
493 GNUNET_free (val_array);
498 * Creates a new value in the matrix
500 * Sets the row and column index in the problem array and increments the
503 * @param p the mlp problem
504 * @param row the row to create the value in
505 * @param col the column to create the value in
506 * @param val the value to set
507 * @param line calling line for debbuging
510 mlp_create_problem_set_value (struct MLP_Problem *p,
511 int row, int col, double val,
514 if ((p->ci) >= p->num_elements)
516 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
517 line, p->ci + 1, p->num_elements);
521 if ((0 == row) || (0 == col))
526 #if DEBUG_MLP_PROBLEM_CREATION
527 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u == %.2f\n",
528 line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
534 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
535 unsigned int type, unsigned int bound, double lb, double ub,
538 int col = glp_add_cols (p->prob, 1);
539 glp_set_col_name (p->prob, col, name);
540 glp_set_col_bnds (p->prob, col, bound, lb, ub);
541 glp_set_col_kind (p->prob, col, type);
542 glp_set_obj_coef (p->prob, col, coef);
543 #if DEBUG_MLP_PROBLEM_CREATION
544 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
551 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
552 unsigned int bound, double lb, double ub)
555 int row = glp_add_rows (p->prob, 1);
557 glp_set_row_name (p->prob, row, name);
558 /* set row bounds: <= 0 */
559 glp_set_row_bnds (p->prob, row, bound, lb, ub);
563 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
566 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
569 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
572 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
575 GNUNET_asprintf(&op, "ERROR");
578 #if DEBUG_MLP_PROBLEM_CREATION
579 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
588 * - address columns b and n
589 * - address dependent constraint rows c1, c3
590 * - peer dependent rows c2 and c9
591 * - Set address dependent entries in problem matrix as well
594 mlp_create_problem_add_address_information (void *cls, const struct GNUNET_HashCode *key, void *value)
596 struct GAS_MLP_Handle *mlp = cls;
597 struct MLP_Problem *p = &mlp->p;
598 struct ATS_Address *address = value;
599 struct ATS_Peer *peer;
600 struct MLP_information *mlpi;
606 /* Check if we have to add this peer due to a pending request */
607 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->requested_peers, key))
610 mlpi = address->solver_information;
613 fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
619 peer = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, key);
620 if (peer->processed == GNUNET_NO)
622 /* Add peer dependent constraints */
623 /* Add constraint c2 */
624 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
625 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
627 /* Add constraint c9 */
628 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
629 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
631 /* c 9) set coefficient */
632 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f, __LINE__);
633 peer->processed = GNUNET_YES;
636 /* Reset addresses' solver information */
643 /* Add bandwidth column */
644 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
645 #if TEST_MAX_BW_ASSIGNMENT
646 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
648 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
653 /* Add usage column */
654 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
655 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
658 /* Add address dependent constraints */
659 /* Add constraint c1) bandwidth capping
660 * b_t + (-M) * n_t <= 0
662 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
663 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
666 /* c1) set b = 1 coefficient */
667 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
668 /* c1) set n = -M coefficient */
669 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -mlp->pv.BIG_M, __LINE__);
671 /* Add constraint c 3) minimum bandwidth
672 * b_t + (-n_t * b_min) >= 0
674 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
675 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
678 /* c3) set b = 1 coefficient */
679 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
680 /* c3) set n = -b_min coefficient */
681 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
684 /* Set coefficient entries in invariant rows */
685 /* c 4) minimum connections */
686 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
687 /* c 6) maximize diversity */
688 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
689 /* c 2) 1 address peer peer */
690 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
691 /* c 9) relativity */
692 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
694 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
696 /* c 10) obey network specific quotas
697 * (1)*b_1 + ... + (1)*b_m <= quota_n
699 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
701 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
702 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
703 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
705 if (mlp->pv.quota_index[c] == addr_net)
707 mlp_create_problem_set_value (p, p->r_quota[c], mlpi->c_b, 1, __LINE__);
712 /* c 7) Optimize quality */
713 /* For all quality metrics, set quality of this address */
714 props = mlp->get_properties (mlp->get_properties_cls, address);
715 for (c = 0; c < mlp->pv.m_q; c++)
716 mlp_create_problem_set_value (p, p->r_q[c], mlpi->c_b, props[c], __LINE__);
722 * Create the invariant columns c4, c6, c10, c8, c7
725 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
730 /* Row for c4) minimum connection */
731 /* Number of minimum connections is min(|Peers|, n_min) */
732 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);
734 /* Add row for c6) */
735 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
737 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
739 /* Add rows for c 10) */
740 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
743 GNUNET_asprintf(&text, "c10_quota_ats_%s",
744 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
745 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
749 /* Adding rows for c 8) */
750 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
752 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
754 /* c 7) For all quality metrics */
755 for (c = 0; c < mlp->pv.m_q; c++)
757 GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->pv.q[c]));
758 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
760 mlp_create_problem_set_value (p, p->r_q[c], p->c_q[c], -1, __LINE__);
766 * Create the invariant columns d, u, r, q0 ... qm
769 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
774 #if TEST_MAX_BW_ASSIGNMENT
779 //mlp->pv.co_R = 0.0;
781 /* Diversity d column */
782 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
784 /* Utilization u column */
785 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
787 /* Relativity r column */
788 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
790 /* Quality metric columns */
791 for (c = 0; c < mlp->pv.m_q; c++)
793 GNUNET_asprintf (&name, "q_%u", mlp->pv.q[c]);
794 #if TEST_MAX_BW_ASSIGNMENT
795 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
797 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
805 * Create the MLP problem
807 * @param mlp the MLP handle
808 * @return GNUNET_OK or GNUNET_SYSERR
811 mlp_create_problem (struct GAS_MLP_Handle *mlp)
813 struct MLP_Problem *p = &mlp->p;
816 GNUNET_assert (p->prob == NULL);
817 GNUNET_assert (p->ia == NULL);
818 GNUNET_assert (p->ja == NULL);
819 GNUNET_assert (p->ar == NULL);
820 /* Reset MLP problem struct */
822 /* create the glpk problem */
823 p->prob = glp_create_prob ();
824 GNUNET_assert (NULL != p->prob);
825 p->num_peers = GNUNET_CONTAINER_multihashmap_size (mlp->requested_peers);
826 p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers, mlp->addresses);
828 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
829 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses +
830 mlp->pv.m_q + p->num_peers + 2 + 1);
831 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
832 p->num_peers, p->num_addresses, mlp->pv.m_q, p->num_elements);
834 /* Set a problem name */
835 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
836 /* Set optimization direction to maximize */
837 glp_set_obj_dir (p->prob, GLP_MAX);
839 /* Create problem matrix */
840 /* last +1 caused by glpk index starting with one: [1..elements]*/
843 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
845 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
847 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
849 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
851 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
852 return GNUNET_SYSERR;
855 /* Adding invariant columns */
856 mlp_create_problem_add_invariant_columns (mlp, p);
858 /* Adding address independent constraint rows */
859 mlp_create_problem_add_invariant_rows (mlp, p);
861 /* Adding address dependent columns constraint rows */
862 GNUNET_CONTAINER_multihashmap_iterate (mlp->addresses, &mlp_create_problem_add_address_information, mlp);
864 /* Load the matrix */
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
866 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
872 * Solves the LP problem
874 * @param mlp the MLP Handle
875 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
878 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
882 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
884 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
886 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
888 /* Analyze problem status */
889 res = glp_get_status (mlp->p.prob);
891 /* solution is optimal */
893 /* solution is feasible */
895 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n",
896 res, mlp_status_to_string(res));
898 /* Problem was ill-defined, no way to handle that */
900 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed, no solution: 0x%02X %s\n",
901 res, mlp_status_to_string(res));
902 return GNUNET_SYSERR;
908 * Solves the MLP problem
910 * @param mlp the MLP Handle
911 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
914 mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
917 res = glp_intopt(mlp->p.prob, &mlp->control_param_mlp);
919 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
921 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving MLP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
922 /* Analyze problem status */
923 res = glp_mip_status(mlp->p.prob);
925 /* solution is optimal */
927 /* solution is feasible */
929 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_status_to_string(res));
931 /* Problem was ill-defined, no way to handle that */
933 LOG (GNUNET_ERROR_TYPE_WARNING,"Solving MLP problem failed, 0x%02X %s\n\n", res, mlp_status_to_string(res));
934 return GNUNET_SYSERR;
940 * Propagates the results when MLP problem was solved
942 * @param cls the MLP handle
943 * @param key the peer identity
944 * @param value the address
945 * @return GNUNET_OK to continue
948 mlp_propagate_results (void *cls, const struct GNUNET_HashCode *key, void *value)
950 struct GAS_MLP_Handle *mlp = cls;
951 struct ATS_Address *address;
952 struct MLP_information *mlpi;
953 double mlp_bw_in = MLP_NaN;
954 double mlp_bw_out = MLP_NaN;
955 double mlp_use = MLP_NaN;
957 /* Check if we have to add this peer due to a pending request */
958 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->requested_peers, key))
963 GNUNET_assert (address->solver_information != NULL);
964 mlpi = address->solver_information;
966 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
967 if (mlp_bw_in > (double) UINT32_MAX)
969 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
970 mlp_bw_in = (double) UINT32_MAX;
972 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
973 if (mlp_bw_out > (double) UINT32_MAX)
975 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
976 mlp_bw_out = (double) UINT32_MAX;
978 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
982 * LOG (GNUNET_ERROR_TYPE_INFO, "MLP result address: `%s' `%s' length %u session %u, mlp use %f\n",
983 * GNUNET_i2s(&address->peer), address->plugin,
984 * address->addr_len, address->session_id);
987 if (GLP_YES == mlp_use)
989 /* This address was selected by the solver to be used */
990 mlpi->n = GNUNET_YES;
991 if (GNUNET_NO == address->active)
993 /* Address was not used before, enabling address */
994 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
995 address->active = GNUNET_YES;
996 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
997 mlpi->b_in.value__ = htonl(mlp_bw_in);
998 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
999 mlpi->b_out.value__ = htonl(mlp_bw_out);
1000 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1002 else if (GNUNET_YES == address->active)
1004 /* Address was used before, check for bandwidth change */
1005 if ((mlp_bw_out != ntohl(address->assigned_bw_out.value__)) ||
1006 (mlp_bw_in != ntohl(address->assigned_bw_in.value__)))
1008 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n",
1009 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1010 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
1011 mlpi->b_in.value__ = htonl(mlp_bw_in);
1012 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
1013 mlpi->b_out.value__ = htonl(mlp_bw_out);
1014 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1020 else if (GLP_NO == mlp_use)
1022 /* This address was selected by the solver to be not used */
1023 mlpi->n = GNUNET_NO;
1024 if (GNUNET_NO == address->active)
1026 /* Address was not used before, nothing to do */
1027 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n",
1028 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1030 else if (GNUNET_YES == address->active)
1032 /* Address was used before, disabling address */
1033 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n",
1034 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1035 address->active = GNUNET_NO;
1036 /* Set bandwidth to 0 */
1037 address->assigned_bw_in = BANDWIDTH_ZERO;
1038 mlpi->b_in.value__ = htonl(mlp_bw_in);
1039 address->assigned_bw_out = BANDWIDTH_ZERO;
1040 mlpi->b_out.value__ = htonl(mlp_bw_out);
1041 //mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1055 * Solves the MLP problem
1057 * @param solver the MLP Handle
1058 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
1061 GAS_mlp_solve_problem (void *solver)
1063 struct GAS_MLP_Handle *mlp = solver;
1067 struct GNUNET_TIME_Absolute start_build;
1068 struct GNUNET_TIME_Relative duration_build;
1069 struct GNUNET_TIME_Absolute start_lp;
1070 struct GNUNET_TIME_Relative duration_lp;
1071 struct GNUNET_TIME_Absolute start_mlp;
1072 struct GNUNET_TIME_Relative duration_mlp;
1073 GNUNET_assert (NULL != solver);
1075 if (GNUNET_YES == mlp->bulk_lock)
1077 mlp->bulk_request ++;
1081 if (0 == GNUNET_CONTAINER_multihashmap_size(mlp->requested_peers))
1082 return GNUNET_OK; /* No pending requests */
1083 if (0 == GNUNET_CONTAINER_multihashmap_size(mlp->addresses))
1084 return GNUNET_OK; /* No addresses available */
1086 if ((GNUNET_NO == mlp->mlp_prob_changed) && (GNUNET_NO == mlp->mlp_prob_updated))
1088 LOG (GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
1091 if (GNUNET_YES == mlp->mlp_prob_changed)
1093 LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
1094 mlp_delete_problem (mlp);
1095 start_build = GNUNET_TIME_absolute_get();
1096 if (GNUNET_SYSERR == mlp_create_problem (mlp))
1097 return GNUNET_SYSERR;
1098 duration_build = GNUNET_TIME_absolute_get_duration (start_build);
1099 mlp->control_param_lp.presolve = GLP_YES;
1100 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
1105 duration_build.rel_value_us = 0;
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running LP solver %s\n",
1110 (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
1111 start_lp = GNUNET_TIME_absolute_get();
1112 res_lp = mlp_solve_lp_problem (mlp);
1113 duration_lp = GNUNET_TIME_absolute_get_duration (start_lp);
1116 /* Run MLP solver */
1117 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
1118 start_mlp = GNUNET_TIME_absolute_get();
1119 res_mip = mlp_solve_mlp_problem (mlp);
1121 duration_mlp = GNUNET_TIME_absolute_get_duration (start_mlp);
1124 mlp->ps.lp_res = res_lp;
1125 mlp->ps.mip_res = res_mip;
1126 mlp->ps.build_dur = duration_build;
1127 mlp->ps.lp_dur = duration_lp;
1128 mlp->ps.mip_dur = duration_mlp;
1129 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1130 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1131 mlp->ps.p_cols = glp_get_num_cols (mlp->p.prob);
1132 mlp->ps.p_rows = glp_get_num_rows (mlp->p.prob);
1133 mlp->ps.p_elements = mlp->p.num_elements;
1135 LOG (GNUNET_ERROR_TYPE_DEBUG,
1136 "Execution time: Build %s\n",
1137 GNUNET_STRINGS_relative_time_to_string (duration_build, GNUNET_NO));
1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1139 "Execution time: LP %s\n",
1140 GNUNET_STRINGS_relative_time_to_string (duration_lp, GNUNET_NO));
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "Execution time: MLP %s\n",
1143 GNUNET_STRINGS_relative_time_to_string (duration_mlp, GNUNET_NO));
1145 /* Propagate result*/
1146 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1147 GNUNET_CONTAINER_multihashmap_iterate (mlp->addresses, &mlp_propagate_results, mlp);
1149 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1150 if (GNUNET_YES == mlp->write_mip_mps)
1152 /* Write problem to disk */
1153 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value_us);
1154 LOG (GNUNET_ERROR_TYPE_ERROR, "DUMP: %s \n", filename);
1155 glp_write_lp(mlp->p.prob, NULL, filename);
1156 GNUNET_free (filename);
1158 if (GNUNET_YES == mlp->write_mip_sol)
1160 /* Write solution to disk */
1161 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value_us);
1162 glp_print_mip (mlp->p.prob, filename );
1163 LOG (GNUNET_ERROR_TYPE_ERROR, "DUMP: %s \n", filename);
1164 GNUNET_free (filename);
1167 /* Reset change and update marker */
1168 mlp->control_param_lp.presolve = GLP_NO;
1169 mlp->mlp_prob_updated = GNUNET_NO;
1170 mlp->mlp_prob_changed = GNUNET_NO;
1172 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1175 return GNUNET_SYSERR;
1179 * Add a single address to the solve
1181 * @param solver the solver Handle
1182 * @param address the address to add
1183 * @param network network type of this address
1186 GAS_mlp_address_add (void *solver,
1187 struct ATS_Address *address,
1190 struct GAS_MLP_Handle *mlp = solver;
1193 GNUNET_assert (NULL != solver);
1194 GNUNET_assert (NULL != address);
1196 if (NULL == address->solver_information)
1198 address->solver_information = GNUNET_malloc (sizeof (struct MLP_information));
1201 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding address for peer `%s' multiple times\n"), GNUNET_i2s(&address->peer));
1203 /* Is this peer included in the problem? */
1204 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &address->peer.hashPubKey)))
1206 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1210 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1211 /* Problem size changed: new address for peer with pending request */
1212 mlp->mlp_prob_changed = GNUNET_YES;
1213 if (GNUNET_YES == mlp->mlp_auto_solve)
1214 GAS_mlp_solve_problem (solver);
1219 * Transport properties for this address have changed
1221 * @param solver solver handle
1222 * @param address the address
1223 * @param type the ATSI type in HBO
1224 * @param abs_value the absolute value of the property
1225 * @param rel_value the normalized value
1228 GAS_mlp_address_property_changed (void *solver,
1229 struct ATS_Address *address,
1234 struct MLP_information *mlpi = address->solver_information;
1235 struct GAS_MLP_Handle *mlp = solver;
1240 GNUNET_assert (NULL != solver);
1241 GNUNET_assert (NULL != address);
1245 LOG (GNUNET_ERROR_TYPE_ERROR,
1246 _("Updating address property `%s' for peer `%s' %p not added before\n"),
1247 GNUNET_ATS_print_property_type (type),
1248 GNUNET_i2s(&address->peer),
1254 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers,
1255 &address->peer.hashPubKey)))
1257 /* Peer is not requested, so no need to update problem */
1260 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating property `%s' address for peer `%s'\n",
1261 GNUNET_ATS_print_property_type (type),
1262 GNUNET_i2s(&address->peer));
1264 /* Find row index */
1266 for (c1 = 0; c1 < mlp->pv.m_q; c1++)
1268 if (type == mlp->pv.q[c1])
1274 if (-1 == type_index)
1277 return; /* quality index not found */
1280 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index] */
1281 if (GNUNET_YES == mlp_create_problem_update_value (&mlp->p,
1282 mlp->p.r_q[type_index], mlpi->c_b, rel_value, __LINE__))
1284 mlp->mlp_prob_updated = GNUNET_YES;
1285 if (GNUNET_YES == mlp->mlp_auto_solve)
1286 GAS_mlp_solve_problem (solver);
1292 * Transport session for this address has changed
1294 * NOTE: values in addresses are already updated
1296 * @param solver solver handle
1297 * @param address the address
1298 * @param cur_session the current session
1299 * @param new_session the new session
1302 GAS_mlp_address_session_changed (void *solver,
1303 struct ATS_Address *address,
1304 uint32_t cur_session,
1305 uint32_t new_session)
1307 /* Nothing to do here */
1313 * Transport session for this address has changed
1315 * NOTE: values in addresses are already updated
1317 * @param solver solver handle
1318 * @param address the address
1319 * @param in_use usage state
1322 GAS_mlp_address_inuse_changed (void *solver,
1323 struct ATS_Address *address,
1326 /* Nothing to do here */
1332 * Network scope for this address has changed
1334 * NOTE: values in addresses are already updated
1336 * @param solver solver handle
1337 * @param address the address
1338 * @param current_network the current network
1339 * @param new_network the new network
1342 GAS_mlp_address_change_network (void *solver,
1343 struct ATS_Address *address,
1344 uint32_t current_network,
1345 uint32_t new_network)
1347 struct MLP_information *mlpi = address->solver_information;
1348 struct GAS_MLP_Handle *mlp = solver;
1350 int nets_avail[] = GNUNET_ATS_NetworkType;
1353 GNUNET_assert (NULL != solver);
1354 GNUNET_assert (NULL != address);
1362 if (mlpi->c_b == MLP_UNDEFINED)
1363 return; /* This address is not yet in the matrix*/
1365 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers,
1366 &address->peer.hashPubKey)))
1368 /* Peer is not requested, so no need to update problem */
1373 if (current_network == new_network)
1379 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount ; c1 ++)
1381 if (nets_avail[c1] == new_network)
1385 if (GNUNET_ATS_NetworkTypeCount == c1)
1387 /* Invalid network */
1392 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating network for peer `%s' from `%s' to `%s'\n",
1393 GNUNET_i2s (&address->peer),
1394 GNUNET_ATS_print_network_type(current_network),
1395 GNUNET_ATS_print_network_type(new_network));
1397 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount; c1++)
1399 if (mlp->pv.quota_index[c1] == current_network)
1401 /* Remove from old network */
1402 mlp_create_problem_update_value (&mlp->p,
1404 mlpi->c_b, 0.0, __LINE__);
1409 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount; c1++)
1411 if (mlp->pv.quota_index[c1] == new_network)
1413 /* Remove from old network */
1414 if (GNUNET_SYSERR == mlp_create_problem_update_value (&mlp->p,
1416 mlpi->c_b, 1.0, __LINE__))
1418 /* This quota did not exist in the problem, recreate */
1425 mlp->mlp_prob_changed = GNUNET_YES;
1430 * Deletes a single address in the MLP problem
1432 * The MLP problem has to be recreated and the problem has to be resolved
1434 * @param solver the MLP Handle
1435 * @param address the address to delete
1436 * @param session_only delete only session not whole address
1439 GAS_mlp_address_delete (void *solver,
1440 struct ATS_Address *address,
1444 struct GAS_MLP_Handle *mlp = solver;
1445 struct MLP_information *mlpi;
1447 GNUNET_assert (NULL != solver);
1448 GNUNET_assert (NULL != address);
1450 mlpi = address->solver_information;
1451 if ((GNUNET_NO == session_only) && (NULL != mlpi))
1453 /* Remove full address */
1455 address->solver_information = NULL;
1457 address->active = GNUNET_NO;
1459 /* Is this peer included in the problem? */
1460 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &address->peer.hashPubKey)))
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s for peer `%s' without address request \n",
1463 (session_only == GNUNET_YES) ? "session" : "address",
1464 GNUNET_i2s(&address->peer));
1467 LOG (GNUNET_ERROR_TYPE_INFO, "Deleting %s for peer `%s' with address request \n",
1468 (session_only == GNUNET_YES) ? "session" : "address",
1469 GNUNET_i2s(&address->peer));
1471 /* Problem size changed: new address for peer with pending request */
1472 mlp->mlp_prob_changed = GNUNET_YES;
1473 if (GNUNET_YES == mlp->mlp_auto_solve)
1475 GAS_mlp_solve_problem (solver);
1482 * Find the active address in the set of addresses of a peer
1483 * @param cls destination
1484 * @param key peer id
1485 * @param value address
1489 mlp_get_preferred_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1491 static int counter = 0;
1492 struct ATS_Address **aa = (struct ATS_Address **) cls;
1493 struct ATS_Address *addr = value;
1494 struct MLP_information *mlpi = addr->solver_information;
1500 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1501 * "MLP [%u] Peer `%s' %s length %u session %u active %s mlp active %s\n",
1502 * counter, GNUNET_i2s (&addr->peer), addr->plugin, addr->addr_len, addr->session_id,
1503 * (GNUNET_YES == addr->active) ? "active" : "inactive",
1504 * (GNUNET_YES == mlpi->n) ? "active" : "inactive");
1507 if (GNUNET_YES == mlpi->n)
1511 (*aa)->assigned_bw_in = mlpi->b_in;
1512 (*aa)->assigned_bw_out = mlpi->b_out;
1520 static double get_peer_pref_value (struct GAS_MLP_Handle *mlp, const struct GNUNET_PeerIdentity *peer)
1523 const double *preferences = NULL;
1525 preferences = mlp->get_preferences (mlp->get_preferences_cls, peer);
1528 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1530 if (c != GNUNET_ATS_PREFERENCE_END)
1532 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
1533 res += preferences[c];
1536 res /= (GNUNET_ATS_PreferenceCount -1);
1542 * Get the preferred address for a specific peer
1544 * @param solver the MLP Handle
1545 * @param peer the peer
1546 * @return suggested address
1548 const struct ATS_Address *
1549 GAS_mlp_get_preferred_address (void *solver,
1550 const struct GNUNET_PeerIdentity *peer)
1552 struct GAS_MLP_Handle *mlp = solver;
1554 struct ATS_Address *res;
1556 GNUNET_assert (NULL != solver);
1557 GNUNET_assert (NULL != peer);
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n",
1562 /* Is this peer included in the problem? */
1563 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1565 LOG (GNUNET_ERROR_TYPE_INFO, "Adding peer `%s' to list of requested_peers with requests\n",
1568 p = GNUNET_malloc (sizeof (struct ATS_Peer));
1570 p->f = get_peer_pref_value (mlp, peer);
1571 GNUNET_CONTAINER_multihashmap_put (mlp->requested_peers, &peer->hashPubKey, p, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1573 /* Added new peer, we have to rebuild problem before solving */
1574 mlp->mlp_prob_changed = GNUNET_YES;
1576 if ((GNUNET_YES == mlp->mlp_auto_solve)&&
1577 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(mlp->addresses,
1578 &peer->hashPubKey)))
1579 GAS_mlp_solve_problem (mlp);
1581 /* Get prefered address */
1583 GNUNET_CONTAINER_multihashmap_get_multiple (mlp->addresses, &peer->hashPubKey,
1584 mlp_get_preferred_address_it, &res);
1590 * Start a bulk operation
1592 * @param solver the solver
1595 GAS_mlp_bulk_start (void *solver)
1597 LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1598 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1600 GNUNET_assert (NULL != solver);
1606 GAS_mlp_bulk_stop (void *solver)
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1610 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1611 GNUNET_assert (NULL != solver);
1613 if (s->bulk_lock < 1)
1620 if (0 < s->bulk_request)
1622 GAS_mlp_solve_problem (solver);
1630 * Stop notifying about address and bandwidth changes for this peer
1632 * @param solver the MLP handle
1633 * @param peer the peer
1636 GAS_mlp_stop_get_preferred_address (void *solver,
1637 const struct GNUNET_PeerIdentity *peer)
1639 struct GAS_MLP_Handle *mlp = solver;
1640 struct ATS_Peer *p = NULL;
1642 GNUNET_assert (NULL != solver);
1643 GNUNET_assert (NULL != peer);
1644 if (NULL != (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1646 GNUNET_CONTAINER_multihashmap_remove (mlp->requested_peers, &peer->hashPubKey, p);
1649 mlp->mlp_prob_changed = GNUNET_YES;
1650 if (GNUNET_YES == mlp->mlp_auto_solve)
1652 GAS_mlp_solve_problem (solver);
1659 * Changes the preferences for a peer in the MLP problem
1661 * @param solver the MLP Handle
1662 * @param peer the peer
1663 * @param kind the kind to change the preference
1664 * @param pref_rel the relative score
1667 GAS_mlp_address_change_preference (void *solver,
1668 const struct GNUNET_PeerIdentity *peer,
1669 enum GNUNET_ATS_PreferenceKind kind,
1672 struct GAS_MLP_Handle *mlp = solver;
1673 struct ATS_Peer *p = NULL;
1675 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing preference for address for peer `%s' to %.2f\n",
1676 GNUNET_i2s(peer), pref_rel);
1678 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1679 /* Update the constraints with changed preferences */
1681 /* Update quality constraint c7 */
1683 /* Update relativity constraint c9 */
1684 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->requested_peers, &peer->hashPubKey)))
1686 LOG (GNUNET_ERROR_TYPE_ERROR, "Updating preference for unknown peer `%s'\n", GNUNET_i2s(peer));
1689 p->f = get_peer_pref_value (mlp, peer);
1690 LOG (GNUNET_ERROR_TYPE_ERROR, "PEER PREF: %s %.2f\n",
1691 GNUNET_i2s(peer), p->f);
1692 mlp_create_problem_update_value (&mlp->p, p->r_c9, mlp->p.c_r, -p->f, __LINE__);
1694 /* Problem size changed: new address for peer with pending request */
1695 mlp->mlp_prob_updated = GNUNET_YES;
1696 if (GNUNET_YES == mlp->mlp_auto_solve)
1697 GAS_mlp_solve_problem (solver);
1703 * Get application feedback for a peer
1705 * @param solver the solver handle
1706 * @param application the application
1707 * @param peer the peer to change the preference for
1708 * @param scope the time interval for this feedback: [now - scope .. now]
1709 * @param kind the kind to change the preference
1710 * @param score the score
1713 GAS_mlp_address_preference_feedback (void *solver,
1715 const struct GNUNET_PeerIdentity *peer,
1716 const struct GNUNET_TIME_Relative scope,
1717 enum GNUNET_ATS_PreferenceKind kind,
1720 struct GAS_PROPORTIONAL_Handle *s = solver;
1721 GNUNET_assert (NULL != solver);
1722 GNUNET_assert (NULL != peer);
1724 GNUNET_assert (NULL != s);
1729 mlp_free_peers (void *cls, const struct GNUNET_HashCode *key, void *value)
1731 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
1732 struct ATS_Peer *p = value;
1734 GNUNET_CONTAINER_multihashmap_remove (map, key, value);
1742 * Shutdown the MLP problem solving component
1744 * @param solver the solver handle
1747 GAS_mlp_done (void *solver)
1749 struct GAS_MLP_Handle *mlp = solver;
1750 GNUNET_assert (mlp != NULL);
1752 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down mlp solver\n");
1753 mlp_delete_problem (mlp);
1755 GNUNET_CONTAINER_multihashmap_iterate (mlp->requested_peers, &mlp_free_peers, mlp->requested_peers);
1756 GNUNET_CONTAINER_multihashmap_destroy (mlp->requested_peers);
1757 mlp->requested_peers = NULL;
1759 /* Clean up GLPK environment */
1763 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutdown down of mlp solver complete\n");
1768 * Init the MLP problem solving component
1770 * @param cfg the GNUNET_CONFIGURATION_Handle handle
1771 * @param stats the GNUNET_STATISTICS handle
1772 * @param addresses the address hashmap
1773 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1774 * @param out_dest array of outbound quotas
1775 * @param in_dest array of outbound quota
1776 * @param dest_length array length for quota arrays
1777 * @param bw_changed_cb callback for changed bandwidth amounts
1778 * @param bw_changed_cb_cls cls for callback
1779 * @param get_preference callback to get relative preferences for a peer
1780 * @param get_preference_cls cls for callback to get relative preferences
1781 * @param get_properties callback to get relative properties
1782 * @param get_properties_cls cls for callback to get relative properties
1783 * @return struct GAS_MLP_Handle on success, NULL on fail
1786 GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1787 const struct GNUNET_STATISTICS_Handle *stats,
1788 const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1790 unsigned long long *out_dest,
1791 unsigned long long *in_dest,
1793 GAS_bandwidth_changed_cb bw_changed_cb,
1794 void *bw_changed_cb_cls,
1795 GAS_get_preferences get_preference,
1796 void *get_preference_cls,
1797 GAS_get_properties get_properties,
1798 void *get_properties_cls)
1800 struct GAS_MLP_Handle * mlp = GNUNET_malloc (sizeof (struct GAS_MLP_Handle));
1805 unsigned long long tmp;
1812 struct GNUNET_TIME_Relative max_duration;
1813 long long unsigned int max_iterations;
1815 GNUNET_assert (NULL != cfg);
1816 GNUNET_assert (NULL != stats);
1817 GNUNET_assert (NULL != addresses);
1818 GNUNET_assert (NULL != bw_changed_cb);
1819 GNUNET_assert (NULL != get_preference);
1820 GNUNET_assert (NULL != get_properties);
1822 /* Init GLPK environment */
1823 int res = glp_init_env();
1826 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1827 "initialization successful");
1830 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1831 "environment is already initialized");
1834 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1835 "initialization failed (insufficient memory)");
1840 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1841 "initialization failed (unsupported programming model)");
1849 mlp->write_mip_mps = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats",
1851 if (GNUNET_SYSERR == mlp->write_mip_mps)
1852 mlp->write_mip_mps = GNUNET_NO;
1853 mlp->write_mip_sol = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats",
1855 if (GNUNET_SYSERR == mlp->write_mip_sol)
1856 mlp->write_mip_sol = GNUNET_NO;
1858 mlp->pv.BIG_M = (double) BIG_M_VALUE;
1860 /* Get timeout for iterations */
1861 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(cfg, "ats", "MLP_MAX_DURATION", &max_duration))
1863 max_duration = MLP_MAX_EXEC_DURATION;
1866 /* Get maximum number of iterations */
1867 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "MLP_MAX_ITERATIONS", &max_iterations))
1869 max_iterations = MLP_MAX_ITERATIONS;
1872 /* Get diversity coefficient from configuration */
1873 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1874 "MLP_COEFFICIENT_D",
1876 D = (double) tmp / 100;
1880 /* Get proportionality coefficient from configuration */
1881 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1882 "MLP_COEFFICIENT_R",
1884 R = (double) tmp / 100;
1888 /* Get utilization coefficient from configuration */
1889 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1890 "MLP_COEFFICIENT_U",
1892 U = (double) tmp / 100;
1896 /* Get quality metric coefficients from configuration */
1897 int i_delay = MLP_NaN;
1898 int i_distance = MLP_NaN;
1899 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
1900 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1902 /* initialize quality coefficients with default value 1.0 */
1903 mlp->pv.co_Q[c] = DEFAULT_QUALITY;
1905 mlp->pv.q[c] = q[c];
1906 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
1908 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
1912 if ((i_delay != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1913 "MLP_COEFFICIENT_QUALITY_DELAY",
1916 mlp->pv.co_Q[i_delay] = (double) tmp / 100;
1918 mlp->pv.co_Q[i_delay] = DEFAULT_QUALITY;
1920 if ((i_distance != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1921 "MLP_COEFFICIENT_QUALITY_DISTANCE",
1923 mlp->pv.co_Q[i_distance] = (double) tmp / 100;
1925 mlp->pv.co_Q[i_distance] = DEFAULT_QUALITY;
1927 /* Get minimum bandwidth per used address from configuration */
1928 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1929 "MLP_MIN_BANDWIDTH",
1934 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1937 /* Get minimum number of connections from configuration */
1938 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1939 "MLP_MIN_CONNECTIONS",
1943 n_min = DEFAULT_MIN_CONNECTIONS;
1945 /* Init network quotas */
1946 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1947 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1950 for (c2 = 0; c2 < dest_length; c2++)
1952 if (quotas[c] == network[c2])
1954 mlp->pv.quota_index[c] = network[c2];
1955 mlp->pv.quota_out[c] = out_dest[c2];
1956 mlp->pv.quota_in[c] = in_dest[c2];
1958 LOG (GNUNET_ERROR_TYPE_DEBUG, "Quota for network `%s' (in/out) %llu/%llu\n",
1959 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1960 mlp->pv.quota_out[c],
1961 mlp->pv.quota_in[c]);
1966 /* Check if defined quota could make problem unsolvable */
1967 if ((n_min * b_min) > mlp->pv.quota_out[c])
1969 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
1970 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1971 mlp->pv.quota_out[c],
1973 mlp->pv.quota_out[c] = (n_min * b_min);
1975 if ((n_min * b_min) > mlp->pv.quota_in[c])
1977 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
1978 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1979 mlp->pv.quota_in[c],
1981 mlp->pv.quota_in[c] = (n_min * b_min);
1984 /* Check if bandwidth is too big to make problem solvable */
1985 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
1987 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
1988 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1989 mlp->pv.quota_out[c],
1991 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
1993 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
1995 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
1996 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1997 mlp->pv.quota_in[c],
1999 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
2002 if (GNUNET_NO == found)
2004 mlp->pv.quota_in[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2005 mlp->pv.quota_out[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2006 LOG (GNUNET_ERROR_TYPE_INFO, _("Using default quota configuration for network `%s' (in/out) %llu/%llu\n"),
2007 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2008 mlp->pv.quota_in[c],
2009 mlp->pv.quota_out[c]);
2013 /* Assign options to handle */
2014 mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
2015 mlp->addresses = addresses;
2016 mlp->bw_changed_cb = bw_changed_cb;
2017 mlp->bw_changed_cb_cls = bw_changed_cb_cls;
2018 mlp->get_preferences = get_preference;
2019 mlp->get_preferences_cls = get_preference_cls;
2020 mlp->get_properties = get_properties;
2021 mlp->get_properties_cls = get_properties_cls;
2022 /* Setting MLP Input variables */
2026 mlp->pv.b_min = b_min;
2027 mlp->pv.n_min = n_min;
2028 mlp->pv.m_q = GNUNET_ATS_QualityPropertiesCount;
2029 mlp->mlp_prob_changed = GNUNET_NO;
2030 mlp->mlp_prob_updated = GNUNET_NO;
2031 mlp->mlp_auto_solve = GNUNET_YES;
2032 mlp->requested_peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2033 mlp->bulk_request = 0;
2037 /* Redirect GLPK output to GNUnet logging */
2038 glp_term_hook (&mlp_term_hook, (void *) mlp);
2040 /* Init LP solving parameters */
2041 glp_init_smcp(&mlp->control_param_lp);
2042 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
2044 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
2046 mlp->control_param_lp.it_lim = max_iterations;
2047 mlp->control_param_lp.tm_lim = max_duration.rel_value_us / 1000LL;
2049 /* Init MLP solving parameters */
2050 glp_init_iocp(&mlp->control_param_mlp);
2051 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
2053 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
2055 mlp->control_param_mlp.tm_lim = max_duration.rel_value_us / 1000LL;
2057 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
2062 /* end of gnunet-service-ats_addresses_mlp.c */