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/libgnunet_plugin_ats_mlp.c
23 * @brief ats mlp problem solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "libgnunet_plugin_ats_mlp.h"
33 * NOTE: Do not modify this documentation. This documentation is based on
34 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
35 * use build_txt.sh to generate plaintext output
37 * The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
38 * optimizing an mixed integer programming problem. The MLP solver uses a
39 * number of constraints to find the best adddress for a peer and an optimal
40 * bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
43 * We defined a constraint system to find an optimal bandwidth assignment.
44 * This constraint system uses as an input data addresses, bandwidth quotas,
45 * preferences and quality values. This constraint system is stored in an
46 * matrix based equotation system.
50 * A (M)LP problem consists of a target function to optimizes, constraints
51 * and rows and columns. FIXME GLP uses three arrays to index the matrix: two
52 * integer arrays storing the row and column indices in the matrix and an
53 * float array to store the coeeficient.
55 * To solve the problem we first find an initial solution for the LP problem
56 * using the LP solver and then find an MLP solution based on this solution
57 * using the MLP solver.
59 * Solving (M)LP problems has the property that finding an initial solution
60 * for the LP problem is computationally expensive and finding the MLP
61 * solution is cheaper. This is especially interesting an existing LP
62 * solution can be reused if only coefficients in the matrix have changed
63 * (addresses updated). Only when the problem size changes (addresses added
64 * or deleted) a new LP solution has to be found.
67 * The mlp solver solves the bandwidth assignment problem only on demand when
68 * an address suggestion is requested. When an address is requested mlp the
69 * solves the mlp problem and if the active address or the bandwidth assigned
70 * changes it calls the callback to addresses. The mlp solver gets notified
71 * about new addresses (adding sessions), removed addresses (address
72 * deletions) and address updates. To benefit from the mlp properties
73 * mentioned in section 5 the solver rembers if since the last solution
74 * addresses were added or deleted (problem size changed, problem has to be
75 * rebuild and solved from sratch) or if addresses were updated and the
76 * existing solution can be reused.
80 * The quotas for each network segment are passed by addresses. MLP can be
81 * adapted using configuration settings and uses the following parameters:
83 * Maximum duration for a MLP solution procees (default: 3 sec.)
85 * Maximum number of iterations for a MLP solution process (default:
87 * * MLP_MIN_CONNECTIONS:
88 * Minimum number of desired connections (default: 4)
89 * * MLP_MIN_BANDWIDTH:
90 * Minimum amount of bandwidth assigned to an address (default: 1024)
91 * * MLP_COEFFICIENT_D:
92 * Diversity coefficient (default: 1.0)
93 * * MLP_COEFFICIENT_R:
94 * Relativity coefficient (default: 1.0)
95 * * MLP_COEFFICIENT_U:
96 * Utilization coefficient (default: 1.0)
97 * * MLP_COEFFICIENT_D:
98 * Diversity coefficient (default: 1.0)
99 * * MLP_COEFFICIENT_QUALITY_DELAY:
100 * Quality delay coefficient (default: 1.0)
101 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
102 * Quality distance coefficient (default: 1.0)
103 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
104 * Quality distance coefficient (default: 1.0)
105 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
106 * Quality distance coefficient (default: 1.0)
107 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
108 * Quality distance coefficient (default: 1.0)
110 * 5.2 Data structures used
112 * mlp has for each known peer a struct ATS_Peer containing information about
113 * a specific peer. The address field solver_information contains information
114 * about the mlp properties of this address.
118 * During initialization mlp initializes the GLPK libray used to solve the
119 * MLP problem: it initializes the glpk environment and creates an initial LP
120 * problem. Next it loads the configuration values from the configuration or
121 * uses the default values configured in -addresses_mlp.h. The quotas used
122 * are given by addresses but may have to be adjusted. mlp uses a upper limit
123 * for the bandwidth assigned called BIG M and a minimum amount of bandwidth
124 * an address gets assigned as well as a minium desired number of
125 * connections. If the configured quota is bigger than BIG M, it is reduced
126 * to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
127 * *MLP_MIN_BANDWIDTH it is increased to this value.
133 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
136 * Print debug output for mlp problem creation
138 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
141 * Enable GLPK verbose output
143 #define VERBOSE_GLPK GNUNET_NO
146 * Maximize bandwidth assigned
148 * This option can be used to test if problem can be solved at all without
149 * optimizing for utility, diversity or relativity
152 #define MAXIMIZE_FOR_BANDWIDTH_ASSIGNED GNUNET_NO
155 * Intercept GLPK terminal output
156 * @param info the mlp handle
157 * @param s the string to print
158 * @return 0: glpk prints output on terminal, 0 != surpress output
161 mlp_term_hook (void *info, const char *s)
163 /* Not needed atm struct MLP_information *mlp = info; */
164 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s", s);
170 * Reset peers for next problem creation
172 * @param cls not used
174 * @param value ATS_Peer
178 reset_peers (void *cls,
179 const struct GNUNET_PeerIdentity *key,
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_multipeermap_iterate (mlp->requested_peers,
246 * Translate ATS properties to text
247 * Just intended for debugging
249 * @param ats_index the ATS index
250 * @return string with result
253 mlp_ats_to_string (int ats_index)
256 case GNUNET_ATS_ARRAY_TERMINATOR:
257 return "GNUNET_ATS_ARRAY_TERMINATOR";
258 case GNUNET_ATS_UTILIZATION_OUT:
259 return "GNUNET_ATS_UTILIZATION_OUT";
260 case GNUNET_ATS_UTILIZATION_IN:
261 return "GNUNET_ATS_UTILIZATION_IN";
262 case GNUNET_ATS_UTILIZATION_PAYLOAD_OUT:
263 return "GNUNET_ATS_UTILIZATION_PAYLOAD_OUT";
264 case GNUNET_ATS_UTILIZATION_PAYLOAD_IN:
265 return "GNUNET_ATS_UTILIZATION_PAYLOAD_IN";
266 case GNUNET_ATS_COST_LAN:
267 return "GNUNET_ATS_COST_LAN";
268 case GNUNET_ATS_COST_WAN:
269 return "GNUNET_ATS_COST_LAN";
270 case GNUNET_ATS_COST_WLAN:
271 return "GNUNET_ATS_COST_WLAN";
272 case GNUNET_ATS_NETWORK_TYPE:
273 return "GNUNET_ATS_NETWORK_TYPE";
274 case GNUNET_ATS_QUALITY_NET_DELAY:
275 return "GNUNET_ATS_QUALITY_NET_DELAY";
276 case GNUNET_ATS_QUALITY_NET_DISTANCE:
277 return "GNUNET_ATS_QUALITY_NET_DISTANCE";
285 * Translate glpk status error codes to text
286 * @param retcode return code
287 * @return string with result
290 mlp_status_to_string (int retcode)
294 return "solution is undefined";
296 return "solution is feasible";
298 return "solution is infeasible";
300 return "no feasible solution exists";
302 return "solution is optimal";
304 return "solution is unbounded";
307 return "unknown error";
312 * Translate glpk solver error codes to text
313 * @param retcode return code
314 * @return string with result
317 mlp_solve_to_string (int retcode)
323 return "invalid basis";
325 return "singular matrix";
327 return "ill-conditioned matrix";
329 return "invalid bounds";
331 return "solver failed";
333 return "objective lower limit reached";
335 return "objective upper limit reached";
337 return "iteration limit exceeded";
339 return "time limit exceeded";
341 return "no primal feasible solution";
343 return "no dual feasible solution";
345 return "root LP optimum not provided";
347 return "search terminated by application";
349 return "relative mip gap tolerance reached";
351 return "no dual feasible solution";
353 return "no convergence";
355 return "numerical instability";
357 return "invalid data";
359 return "result out of range";
362 return "unknown error";
367 * Extract an ATS performance info from an address
369 * @param address the address
370 * @param type the type to extract in HBO
371 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
374 get_performance_info (struct ATS_Address *address, uint32_t type)
377 GNUNET_assert (NULL != address);
379 if ((NULL == address->atsi) || (0 == address->atsi_count))
380 return GNUNET_ATS_VALUE_UNDEFINED;
382 for (c1 = 0; c1 < address->atsi_count; c1++)
384 if (ntohl(address->atsi[c1].type) == type)
385 return ntohl(address->atsi[c1].value);
387 return GNUNET_ATS_VALUE_UNDEFINED;
393 const struct GNUNET_CONTAINER_MultiPeerMap *peers;
398 mlp_create_problem_count_addresses_it (void *cls,
399 const struct GNUNET_PeerIdentity *key,
402 struct CountContext *cctx = cls;
404 /* Check if we have to add this peer due to a pending request */
405 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->peers, key))
412 mlp_create_problem_count_addresses (const struct GNUNET_CONTAINER_MultiPeerMap *peers,
413 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
415 struct CountContext cctx;
419 GNUNET_CONTAINER_multipeermap_iterate (addresses,
420 &mlp_create_problem_count_addresses_it, &cctx);
426 * Updates an existing value in the matrix
428 * Extract the row, updates the value and updates the row in the problem
430 * @param p the mlp problem
431 * @param row the row to create the value in
432 * @param col the column to create the value in
433 * @param val the value to set
434 * @param line calling line for debbuging
435 * @return GNUNET_YES value changed, GNUNET_NO value did not change, GNUNET_SYSERR
439 mlp_create_problem_update_value (struct MLP_Problem *p,
440 int row, int col, double val,
451 GNUNET_assert (NULL != p);
452 GNUNET_assert (NULL != p->prob);
454 /* Get number of columns and prepare data structure */
455 c_cols = glp_get_num_cols(p->prob);
457 return GNUNET_SYSERR;
459 val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
460 GNUNET_assert (NULL != val_array);
461 ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
462 GNUNET_assert (NULL != ind_array);
463 /* Extract the row */
465 /* Update the value */
466 c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array);
468 for (c1 = 1; c1 < (c_elems+1); c1++)
470 if (ind_array[c1] == col)
476 if (GNUNET_NO == found)
478 ind_array[c_elems+1] = col;
479 val_array[c_elems+1] = val;
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Setting value in [%s : %s] to `%.2f'\n",
481 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
483 glp_set_mat_row (p->prob, row, c_elems+1, ind_array, val_array);
484 GNUNET_free (ind_array);
485 GNUNET_free (val_array);
491 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Updating value in [%s : %s] from `%.2f' to `%.2f'\n",
492 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
494 if (val != val_array[c1])
499 /* Update the row in the matrix */
500 glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
503 GNUNET_free (ind_array);
504 GNUNET_free (val_array);
509 * Creates a new value in the matrix
511 * Sets the row and column index in the problem array and increments the
514 * @param p the mlp problem
515 * @param row the row to create the value in
516 * @param col the column to create the value in
517 * @param val the value to set
518 * @param line calling line for debbuging
521 mlp_create_problem_set_value (struct MLP_Problem *p,
522 int row, int col, double val,
525 if ((p->ci) >= p->num_elements)
527 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
528 line, p->ci + 1, p->num_elements);
532 if ((0 == row) || (0 == col))
537 #if DEBUG_MLP_PROBLEM_CREATION
538 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u == %.2f\n",
539 line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
545 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
546 unsigned int type, unsigned int bound, double lb, double ub,
549 int col = glp_add_cols (p->prob, 1);
550 glp_set_col_name (p->prob, col, name);
551 glp_set_col_bnds (p->prob, col, bound, lb, ub);
552 glp_set_col_kind (p->prob, col, type);
553 glp_set_obj_coef (p->prob, col, coef);
554 #if DEBUG_MLP_PROBLEM_CREATION
555 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
562 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
563 unsigned int bound, double lb, double ub)
566 int row = glp_add_rows (p->prob, 1);
568 glp_set_row_name (p->prob, row, name);
569 /* set row bounds: <= 0 */
570 glp_set_row_bnds (p->prob, row, bound, lb, ub);
574 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
577 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
580 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
583 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
586 GNUNET_asprintf(&op, "ERROR");
589 #if DEBUG_MLP_PROBLEM_CREATION
590 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
599 * - address columns b and n
600 * - address dependent constraint rows c1, c3
601 * - peer dependent rows c2 and c9
602 * - Set address dependent entries in problem matrix as well
605 mlp_create_problem_add_address_information (void *cls,
606 const struct GNUNET_PeerIdentity *key,
609 struct GAS_MLP_Handle *mlp = cls;
610 struct MLP_Problem *p = &mlp->p;
611 struct ATS_Address *address = value;
612 struct ATS_Peer *peer;
613 struct MLP_information *mlpi;
619 /* Check if we have to add this peer due to a pending request */
620 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(mlp->requested_peers, key))
623 mlpi = address->solver_information;
626 fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
632 peer = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, key);
633 if (peer->processed == GNUNET_NO)
635 /* Add peer dependent constraints */
636 /* Add constraint c2 */
637 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
638 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
640 /* Add constraint c9 */
641 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
642 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
644 /* c 9) set coefficient */
645 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f, __LINE__);
646 peer->processed = GNUNET_YES;
649 /* Reset addresses' solver information */
656 /* Add bandwidth column */
657 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
658 #if TEST_MAX_BW_ASSIGNMENT
659 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
661 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
666 /* Add usage column */
667 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
668 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
671 /* Add address dependent constraints */
672 /* Add constraint c1) bandwidth capping
673 * b_t + (-M) * n_t <= 0
675 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
676 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
679 /* c1) set b = 1 coefficient */
680 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
681 /* c1) set n = -M coefficient */
682 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -mlp->pv.BIG_M, __LINE__);
684 /* Add constraint c 3) minimum bandwidth
685 * b_t + (-n_t * b_min) >= 0
687 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
688 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
691 /* c3) set b = 1 coefficient */
692 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
693 /* c3) set n = -b_min coefficient */
694 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
697 /* Set coefficient entries in invariant rows */
698 /* c 4) minimum connections */
699 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
700 /* c 6) maximize diversity */
701 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
702 /* c 2) 1 address peer peer */
703 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
704 /* c 9) relativity */
705 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
707 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
709 /* c 10) obey network specific quotas
710 * (1)*b_1 + ... + (1)*b_m <= quota_n
712 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
714 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
715 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
716 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
718 if (mlp->pv.quota_index[c] == addr_net)
720 mlp_create_problem_set_value (p, p->r_quota[c], mlpi->c_b, 1, __LINE__);
725 /* c 7) Optimize quality */
726 /* For all quality metrics, set quality of this address */
727 props = mlp->get_properties (mlp->get_properties_cls, address);
728 for (c = 0; c < mlp->pv.m_q; c++)
729 mlp_create_problem_set_value (p, p->r_q[c], mlpi->c_b, props[c], __LINE__);
735 * Create the invariant columns c4, c6, c10, c8, c7
738 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
743 /* Row for c4) minimum connection */
744 /* Number of minimum connections is min(|Peers|, n_min) */
745 p->r_c4 = mlp_create_problem_create_constraint (p, "c4", GLP_LO, (mlp->pv.n_min > p->num_peers) ? p->num_peers : mlp->pv.n_min, 0.0);
747 /* Add row for c6) */
748 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
750 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
752 /* Add rows for c 10) */
753 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
756 GNUNET_asprintf(&text, "c10_quota_ats_%s",
757 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
758 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
762 /* Adding rows for c 8) */
763 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
765 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
767 /* c 7) For all quality metrics */
768 for (c = 0; c < mlp->pv.m_q; c++)
770 GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->pv.q[c]));
771 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
773 mlp_create_problem_set_value (p, p->r_q[c], p->c_q[c], -1, __LINE__);
779 * Create the invariant columns d, u, r, q0 ... qm
782 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
787 #if TEST_MAX_BW_ASSIGNMENT
792 //mlp->pv.co_R = 0.0;
794 /* Diversity d column */
795 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
797 /* Utilization u column */
798 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
800 /* Relativity r column */
801 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
803 /* Quality metric columns */
804 for (c = 0; c < mlp->pv.m_q; c++)
806 GNUNET_asprintf (&name, "q_%u", mlp->pv.q[c]);
807 #if TEST_MAX_BW_ASSIGNMENT
808 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
810 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
818 * Create the MLP problem
820 * @param mlp the MLP handle
821 * @return GNUNET_OK or GNUNET_SYSERR
824 mlp_create_problem (struct GAS_MLP_Handle *mlp)
826 struct MLP_Problem *p = &mlp->p;
829 GNUNET_assert (p->prob == NULL);
830 GNUNET_assert (p->ia == NULL);
831 GNUNET_assert (p->ja == NULL);
832 GNUNET_assert (p->ar == NULL);
833 /* Reset MLP problem struct */
835 /* create the glpk problem */
836 p->prob = glp_create_prob ();
837 GNUNET_assert (NULL != p->prob);
838 p->num_peers = GNUNET_CONTAINER_multipeermap_size (mlp->requested_peers);
839 p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers, mlp->addresses);
841 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
842 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses +
843 mlp->pv.m_q + p->num_peers + 2 + 1);
844 LOG (GNUNET_ERROR_TYPE_DEBUG,
845 "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
851 /* Set a problem name */
852 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
853 /* Set optimization direction to maximize */
854 glp_set_obj_dir (p->prob, GLP_MAX);
856 /* Create problem matrix */
857 /* last +1 caused by glpk index starting with one: [1..elements]*/
860 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
862 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
864 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
866 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
868 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
869 return GNUNET_SYSERR;
872 /* Adding invariant columns */
873 mlp_create_problem_add_invariant_columns (mlp, p);
875 /* Adding address independent constraint rows */
876 mlp_create_problem_add_invariant_rows (mlp, p);
878 /* Adding address dependent columns constraint rows */
879 GNUNET_CONTAINER_multipeermap_iterate (mlp->addresses,
880 &mlp_create_problem_add_address_information,
883 /* Load the matrix */
884 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
885 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
891 * Solves the LP problem
893 * @param mlp the MLP Handle
894 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
897 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
901 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
903 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
905 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
907 /* Analyze problem status */
908 res = glp_get_status (mlp->p.prob);
910 /* solution is optimal */
912 /* solution is feasible */
914 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n",
915 res, mlp_status_to_string(res));
917 /* Problem was ill-defined, no way to handle that */
919 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed, no solution: 0x%02X %s\n",
920 res, mlp_status_to_string(res));
921 return GNUNET_SYSERR;
927 * Solves the MLP problem
929 * @param mlp the MLP Handle
930 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
933 mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
936 res = glp_intopt(mlp->p.prob, &mlp->control_param_mlp);
938 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
940 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving MLP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
941 /* Analyze problem status */
942 res = glp_mip_status(mlp->p.prob);
944 /* solution is optimal */
946 /* solution is feasible */
948 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_status_to_string(res));
950 /* Problem was ill-defined, no way to handle that */
952 LOG (GNUNET_ERROR_TYPE_WARNING,"Solving MLP problem failed, 0x%02X %s\n\n", res, mlp_status_to_string(res));
953 return GNUNET_SYSERR;
958 * Propagates the results when MLP problem was solved
960 * @param cls the MLP handle
961 * @param key the peer identity
962 * @param value the address
963 * @return #GNUNET_OK to continue
966 mlp_propagate_results (void *cls,
967 const struct GNUNET_PeerIdentity *key,
970 struct GAS_MLP_Handle *mlp = cls;
971 struct ATS_Address *address;
972 struct MLP_information *mlpi;
973 double mlp_bw_in = MLP_NaN;
974 double mlp_bw_out = MLP_NaN;
975 double mlp_use = MLP_NaN;
977 /* Check if we have to add this peer due to a pending request */
978 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mlp->requested_peers,
984 GNUNET_assert (address->solver_information != NULL);
985 mlpi = address->solver_information;
987 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
988 if (mlp_bw_in > (double) UINT32_MAX)
990 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
991 mlp_bw_in = (double) UINT32_MAX;
993 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
994 if (mlp_bw_out > (double) UINT32_MAX)
996 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
997 mlp_bw_out = (double) UINT32_MAX;
999 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
1003 * LOG (GNUNET_ERROR_TYPE_INFO, "MLP result address: `%s' `%s' length %u session %u, mlp use %f\n",
1004 * GNUNET_i2s(&address->peer), address->plugin,
1005 * address->addr_len, address->session_id);
1008 if (GLP_YES == mlp_use)
1010 /* This address was selected by the solver to be used */
1011 mlpi->n = GNUNET_YES;
1012 if (GNUNET_NO == address->active)
1014 /* Address was not used before, enabling address */
1015 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n",
1016 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1017 address->active = GNUNET_YES;
1018 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
1019 mlpi->b_in.value__ = htonl(mlp_bw_in);
1020 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
1021 mlpi->b_out.value__ = htonl(mlp_bw_out);
1022 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1023 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1026 else if (GNUNET_YES == address->active)
1028 /* Address was used before, check for bandwidth change */
1029 if ((mlp_bw_out != ntohl(address->assigned_bw_out.value__)) ||
1030 (mlp_bw_in != ntohl(address->assigned_bw_in.value__)))
1032 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n",
1033 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1034 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
1035 mlpi->b_in.value__ = htonl(mlp_bw_in);
1036 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
1037 mlpi->b_out.value__ = htonl(mlp_bw_out);
1038 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1039 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1046 else if (GLP_NO == mlp_use)
1048 /* This address was selected by the solver to be not used */
1049 mlpi->n = GNUNET_NO;
1050 if (GNUNET_NO == address->active)
1052 /* Address was not used before, nothing to do */
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n",
1054 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1057 else if (GNUNET_YES == address->active)
1059 /* Address was used before, disabling address */
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n",
1061 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1062 address->active = GNUNET_NO;
1063 /* Set bandwidth to 0 */
1064 address->assigned_bw_in = BANDWIDTH_ZERO;
1065 mlpi->b_in.value__ = htonl(mlp_bw_in);
1066 address->assigned_bw_out = BANDWIDTH_ZERO;
1067 mlpi->b_out.value__ = htonl(mlp_bw_out);
1068 //mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1080 static void notify (struct GAS_MLP_Handle *mlp,
1081 enum GAS_Solver_Operation op,
1082 enum GAS_Solver_Status stat,
1083 enum GAS_Solver_Additional_Information add)
1085 if (NULL != mlp->env->info_cb)
1086 mlp->env->info_cb (mlp->env->info_cb_cls, op, stat, add);
1089 * Solves the MLP problem
1091 * @param solver the MLP Handle
1092 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
1095 GAS_mlp_solve_problem (void *solver)
1097 struct GAS_MLP_Handle *mlp = solver;
1102 struct GNUNET_TIME_Absolute start;
1103 struct GNUNET_TIME_Relative dur_total;
1104 struct GNUNET_TIME_Relative dur_setup;
1105 struct GNUNET_TIME_Relative dur_lp;
1106 struct GNUNET_TIME_Relative dur_mlp;
1108 GNUNET_assert(NULL != solver);
1110 if (GNUNET_YES == mlp->bulk_lock)
1112 mlp->bulk_request++;
1115 notify(mlp, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS,
1116 (GNUNET_YES == mlp->mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1117 start = GNUNET_TIME_absolute_get();
1119 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->requested_peers))
1121 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1122 return GNUNET_OK; /* No pending requests */
1124 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->addresses))
1126 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1127 return GNUNET_OK; /* No addresses available */
1130 if ((GNUNET_NO == mlp->mlp_prob_changed)
1131 && (GNUNET_NO == mlp->mlp_prob_updated))
1133 LOG(GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
1134 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1137 if (GNUNET_YES == mlp->mlp_prob_changed)
1139 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
1140 notify(mlp, GAS_OP_SOLVE_SETUP_START, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1141 mlp_delete_problem(mlp);
1142 if (GNUNET_SYSERR == mlp_create_problem(mlp))
1144 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_FAIL, GAS_INFO_FULL);
1145 return GNUNET_SYSERR;
1147 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1148 mlp->control_param_lp.presolve = GLP_YES;
1149 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
1153 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
1156 dur_setup = GNUNET_TIME_absolute_get_duration (start);
1157 mlp->control_param_lp.presolve = GLP_YES;
1160 notify(mlp, GAS_OP_SOLVE_MLP_LP_START, GAS_STAT_SUCCESS,
1161 (GNUNET_YES == mlp->mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1162 LOG(GNUNET_ERROR_TYPE_DEBUG,
1163 "Running LP solver %s\n",
1164 (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
1165 res_lp = mlp_solve_lp_problem(mlp);
1166 notify(mlp, GAS_OP_SOLVE_MLP_LP_STOP,
1167 (GNUNET_OK == res_lp) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1168 (GNUNET_YES == mlp->mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1170 dur_lp = GNUNET_TIME_absolute_get_duration (start);
1171 dur_lp = GNUNET_TIME_relative_subtract(dur_lp, dur_setup);
1173 /* Run MLP solver */
1174 LOG(GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
1175 notify(mlp, GAS_OP_SOLVE_MLP_MLP_START, GAS_STAT_SUCCESS,
1176 (GNUNET_YES == mlp->mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1177 res_mip = mlp_solve_mlp_problem(mlp);
1178 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP,
1179 (GNUNET_OK == res_lp) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1180 (GNUNET_YES == mlp->mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1181 notify(mlp, GAS_OP_SOLVE_STOP,
1182 (GNUNET_OK == res_mip) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1183 (GNUNET_YES == mlp->mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1185 dur_mlp = GNUNET_TIME_absolute_get_duration (start);
1186 dur_mlp = GNUNET_TIME_relative_subtract(dur_mlp, dur_setup);
1187 dur_mlp = GNUNET_TIME_relative_subtract(dur_mlp, dur_lp);
1188 dur_total = GNUNET_TIME_absolute_get_duration (start);
1190 LOG (GNUNET_ERROR_TYPE_DEBUG,
1191 "Execution time for %s solve: (total/setup/lp/mlp) : %llu %llu %llu %llu\n",
1192 (GNUNET_YES == mlp->mlp_prob_changed) ? "full" : "updated",
1193 (unsigned long long) dur_total.rel_value_us,
1194 (unsigned long long) dur_setup.rel_value_us,
1195 (unsigned long long) dur_lp.rel_value_us,
1196 (unsigned long long) dur_mlp.rel_value_us);
1199 mlp->ps.lp_res = res_lp;
1200 mlp->ps.mip_res = res_mip;
1201 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1202 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1203 mlp->ps.p_cols = glp_get_num_cols(mlp->p.prob);
1204 mlp->ps.p_rows = glp_get_num_rows(mlp->p.prob);
1205 mlp->ps.p_elements = mlp->p.num_elements;
1207 /* Propagate result*/
1208 notify(mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1209 (GNUNET_OK == res_lp) && (GNUNET_OK == res_mip) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1211 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1213 GNUNET_CONTAINER_multipeermap_iterate(mlp->addresses,
1214 &mlp_propagate_results, mlp);
1216 notify(mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1217 (GNUNET_OK == res_lp) && (GNUNET_OK == res_mip) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1220 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1221 if (GNUNET_YES == mlp->write_mip_mps)
1223 /* Write problem to disk */
1224 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers,
1225 mlp->p.num_addresses, time.abs_value_us);
1226 LOG(GNUNET_ERROR_TYPE_ERROR, "DUMP: %s \n", filename);
1227 glp_write_lp(mlp->p.prob, NULL, filename);
1228 GNUNET_free(filename);
1230 if (GNUNET_YES == mlp->write_mip_sol)
1232 /* Write solution to disk */
1233 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers,
1234 mlp->p.num_addresses, time.abs_value_us);
1235 glp_print_mip(mlp->p.prob, filename);
1236 LOG(GNUNET_ERROR_TYPE_ERROR, "DUMP: %s \n", filename);
1237 GNUNET_free(filename);
1240 /* Reset change and update marker */
1241 mlp->control_param_lp.presolve = GLP_NO;
1242 mlp->mlp_prob_updated = GNUNET_NO;
1243 mlp->mlp_prob_changed = GNUNET_NO;
1245 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1248 return GNUNET_SYSERR;
1252 * Add a single address to the solve
1254 * @param solver the solver Handle
1255 * @param address the address to add
1256 * @param network network type of this address
1259 GAS_mlp_address_add (void *solver,
1260 struct ATS_Address *address,
1263 struct GAS_MLP_Handle *mlp = solver;
1266 GNUNET_assert (NULL != solver);
1267 GNUNET_assert (NULL != address);
1269 if (NULL == address->solver_information)
1271 address->solver_information = GNUNET_new (struct MLP_information);
1274 LOG (GNUNET_ERROR_TYPE_ERROR,
1275 _("Adding address for peer `%s' multiple times\n"),
1276 GNUNET_i2s(&address->peer));
1278 /* Is this peer included in the problem? */
1279 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1287 /* Problem size changed: new address for peer with pending request */
1288 mlp->mlp_prob_changed = GNUNET_YES;
1289 if (GNUNET_YES == mlp->mlp_auto_solve)
1290 GAS_mlp_solve_problem (solver);
1295 * Transport properties for this address have changed
1297 * @param solver solver handle
1298 * @param address the address
1299 * @param type the ATSI type in HBO
1300 * @param abs_value the absolute value of the property
1301 * @param rel_value the normalized value
1304 GAS_mlp_address_property_changed (void *solver,
1305 struct ATS_Address *address,
1310 struct MLP_information *mlpi = address->solver_information;
1311 struct GAS_MLP_Handle *mlp = solver;
1316 GNUNET_assert (NULL != solver);
1317 GNUNET_assert (NULL != address);
1321 LOG (GNUNET_ERROR_TYPE_ERROR,
1322 _("Updating address property `%s' for peer `%s' %p not added before\n"),
1323 GNUNET_ATS_print_property_type (type),
1324 GNUNET_i2s(&address->peer),
1330 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1333 /* Peer is not requested, so no need to update problem */
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating property `%s' address for peer `%s'\n",
1337 GNUNET_ATS_print_property_type (type),
1338 GNUNET_i2s(&address->peer));
1340 /* Find row index */
1342 for (c1 = 0; c1 < mlp->pv.m_q; c1++)
1344 if (type == mlp->pv.q[c1])
1350 if (-1 == type_index)
1353 return; /* quality index not found */
1356 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index] */
1357 if (GNUNET_YES == mlp_create_problem_update_value (&mlp->p,
1358 mlp->p.r_q[type_index], mlpi->c_b, rel_value, __LINE__))
1360 mlp->mlp_prob_updated = GNUNET_YES;
1361 if (GNUNET_YES == mlp->mlp_auto_solve)
1362 GAS_mlp_solve_problem (solver);
1368 * Transport session for this address has changed
1370 * NOTE: values in addresses are already updated
1372 * @param solver solver handle
1373 * @param address the address
1374 * @param cur_session the current session
1375 * @param new_session the new session
1378 GAS_mlp_address_session_changed (void *solver,
1379 struct ATS_Address *address,
1380 uint32_t cur_session,
1381 uint32_t new_session)
1383 /* Nothing to do here */
1389 * Transport session for this address has changed
1391 * NOTE: values in addresses are already updated
1393 * @param solver solver handle
1394 * @param address the address
1395 * @param in_use usage state
1398 GAS_mlp_address_inuse_changed (void *solver,
1399 struct ATS_Address *address,
1402 /* Nothing to do here */
1408 * Network scope for this address has changed
1410 * NOTE: values in addresses are already updated
1412 * @param solver solver handle
1413 * @param address the address
1414 * @param current_network the current network
1415 * @param new_network the new network
1418 GAS_mlp_address_change_network (void *solver,
1419 struct ATS_Address *address,
1420 uint32_t current_network,
1421 uint32_t new_network)
1423 struct MLP_information *mlpi = address->solver_information;
1424 struct GAS_MLP_Handle *mlp = solver;
1426 int nets_avail[] = GNUNET_ATS_NetworkType;
1429 GNUNET_assert (NULL != solver);
1430 GNUNET_assert (NULL != address);
1438 if (mlpi->c_b == MLP_UNDEFINED)
1439 return; /* This address is not yet in the matrix*/
1441 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1444 /* Peer is not requested, so no need to update problem */
1449 if (current_network == new_network)
1455 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount ; c1 ++)
1457 if (nets_avail[c1] == new_network)
1461 if (GNUNET_ATS_NetworkTypeCount == c1)
1463 /* Invalid network */
1468 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating network for peer `%s' from `%s' to `%s'\n",
1469 GNUNET_i2s (&address->peer),
1470 GNUNET_ATS_print_network_type(current_network),
1471 GNUNET_ATS_print_network_type(new_network));
1473 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount; c1++)
1475 if (mlp->pv.quota_index[c1] == current_network)
1477 /* Remove from old network */
1478 mlp_create_problem_update_value (&mlp->p,
1480 mlpi->c_b, 0.0, __LINE__);
1485 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount; c1++)
1487 if (mlp->pv.quota_index[c1] == new_network)
1489 /* Remove from old network */
1490 if (GNUNET_SYSERR == mlp_create_problem_update_value (&mlp->p,
1492 mlpi->c_b, 1.0, __LINE__))
1494 /* This quota did not exist in the problem, recreate */
1501 mlp->mlp_prob_changed = GNUNET_YES;
1506 * Deletes a single address in the MLP problem
1508 * The MLP problem has to be recreated and the problem has to be resolved
1510 * @param solver the MLP Handle
1511 * @param address the address to delete
1512 * @param session_only delete only session not whole address
1515 GAS_mlp_address_delete (void *solver,
1516 struct ATS_Address *address,
1520 struct GAS_MLP_Handle *mlp = solver;
1521 struct MLP_information *mlpi;
1524 GNUNET_assert (NULL != solver);
1525 GNUNET_assert (NULL != address);
1527 mlpi = address->solver_information;
1528 if ((GNUNET_NO == session_only) && (NULL != mlpi))
1530 /* Remove full address */
1532 address->solver_information = NULL;
1534 was_active = address->active;
1535 address->active = GNUNET_NO;
1536 address->assigned_bw_in = BANDWIDTH_ZERO;
1537 address->assigned_bw_out = BANDWIDTH_ZERO;
1539 /* Is this peer included in the problem? */
1540 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1543 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s for peer `%s' without address request \n",
1544 (session_only == GNUNET_YES) ? "session" : "address",
1545 GNUNET_i2s(&address->peer));
1548 LOG (GNUNET_ERROR_TYPE_INFO, "Deleting %s for peer `%s' with address request \n",
1549 (session_only == GNUNET_YES) ? "session" : "address",
1550 GNUNET_i2s(&address->peer));
1552 /* Problem size changed: new address for peer with pending request */
1553 mlp->mlp_prob_changed = GNUNET_YES;
1554 if (GNUNET_YES == mlp->mlp_auto_solve)
1556 GAS_mlp_solve_problem (solver);
1558 if (GNUNET_YES == was_active)
1560 if (NULL == GAS_mlp_get_preferred_address (solver, &address->peer))
1562 /* No alternative address, disconnecting peer */
1563 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1572 * Find the active address in the set of addresses of a peer
1573 * @param cls destination
1574 * @param key peer id
1575 * @param value address
1579 mlp_get_preferred_address_it (void *cls,
1580 const struct GNUNET_PeerIdentity *key,
1583 static int counter = 0;
1584 struct ATS_Address **aa = cls;
1585 struct ATS_Address *addr = value;
1586 struct MLP_information *mlpi = addr->solver_information;
1593 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 * "MLP [%u] Peer `%s' %s length %u session %u active %s mlp active %s\n",
1595 * counter, GNUNET_i2s (&addr->peer), addr->plugin, addr->addr_len, addr->session_id,
1596 * (GNUNET_YES == addr->active) ? "active" : "inactive",
1597 * (GNUNET_YES == mlpi->n) ? "active" : "inactive");
1600 if (GNUNET_YES == mlpi->n)
1604 (*aa)->assigned_bw_in = mlpi->b_in;
1605 (*aa)->assigned_bw_out = mlpi->b_out;
1614 get_peer_pref_value (struct GAS_MLP_Handle *mlp, const struct GNUNET_PeerIdentity *peer)
1617 const double *preferences = NULL;
1619 preferences = mlp->get_preferences (mlp->get_preferences_cls, peer);
1622 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1624 if (c != GNUNET_ATS_PREFERENCE_END)
1626 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
1627 res += preferences[c];
1630 res /= (GNUNET_ATS_PreferenceCount -1);
1636 * Get the preferred address for a specific peer
1638 * @param solver the MLP Handle
1639 * @param peer the peer
1640 * @return suggested address
1642 const struct ATS_Address *
1643 GAS_mlp_get_preferred_address (void *solver,
1644 const struct GNUNET_PeerIdentity *peer)
1646 struct GAS_MLP_Handle *mlp = solver;
1648 struct ATS_Address *res;
1650 GNUNET_assert (NULL != solver);
1651 GNUNET_assert (NULL != peer);
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n",
1656 /* Is this peer included in the problem? */
1657 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1660 LOG (GNUNET_ERROR_TYPE_INFO, "Adding peer `%s' to list of requested_peers with requests\n",
1663 p = GNUNET_malloc (sizeof (struct ATS_Peer));
1665 p->f = get_peer_pref_value (mlp, peer);
1666 GNUNET_CONTAINER_multipeermap_put (mlp->requested_peers,
1668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1670 /* Added new peer, we have to rebuild problem before solving */
1671 mlp->mlp_prob_changed = GNUNET_YES;
1673 if ((GNUNET_YES == mlp->mlp_auto_solve)&&
1674 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(mlp->addresses,
1677 mlp->exclude_peer = peer;
1678 GAS_mlp_solve_problem (mlp);
1679 mlp->exclude_peer = NULL;
1682 /* Get prefered address */
1684 GNUNET_CONTAINER_multipeermap_get_multiple (mlp->addresses, peer,
1685 mlp_get_preferred_address_it, &res);
1691 * Start a bulk operation
1693 * @param solver the solver
1696 GAS_mlp_bulk_start (void *solver)
1698 LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1699 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1701 GNUNET_assert (NULL != solver);
1707 GAS_mlp_bulk_stop (void *solver)
1709 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1711 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1712 GNUNET_assert (NULL != solver);
1714 if (s->bulk_lock < 1)
1721 if (0 < s->bulk_request)
1723 GAS_mlp_solve_problem (solver);
1731 * Stop notifying about address and bandwidth changes for this peer
1733 * @param solver the MLP handle
1734 * @param peer the peer
1737 GAS_mlp_stop_get_preferred_address (void *solver,
1738 const struct GNUNET_PeerIdentity *peer)
1740 struct GAS_MLP_Handle *mlp = solver;
1741 struct ATS_Peer *p = NULL;
1743 GNUNET_assert (NULL != solver);
1744 GNUNET_assert (NULL != peer);
1745 if (NULL != (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
1747 GNUNET_CONTAINER_multipeermap_remove (mlp->requested_peers, peer, p);
1750 mlp->mlp_prob_changed = GNUNET_YES;
1751 if (GNUNET_YES == mlp->mlp_auto_solve)
1753 GAS_mlp_solve_problem (solver);
1760 * Changes the preferences for a peer in the MLP problem
1762 * @param solver the MLP Handle
1763 * @param peer the peer
1764 * @param kind the kind to change the preference
1765 * @param pref_rel the relative score
1768 GAS_mlp_address_change_preference (void *solver,
1769 const struct GNUNET_PeerIdentity *peer,
1770 enum GNUNET_ATS_PreferenceKind kind,
1773 struct GAS_MLP_Handle *mlp = solver;
1774 struct ATS_Peer *p = NULL;
1776 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing preference for address for peer `%s' to %.2f\n",
1777 GNUNET_i2s(peer), pref_rel);
1779 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1780 /* Update the constraints with changed preferences */
1782 /* Update quality constraint c7 */
1784 /* Update relativity constraint c9 */
1785 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
1787 LOG (GNUNET_ERROR_TYPE_ERROR, "Updating preference for unknown peer `%s'\n", GNUNET_i2s(peer));
1790 p->f = get_peer_pref_value (mlp, peer);
1791 LOG (GNUNET_ERROR_TYPE_ERROR, "PEER PREF: %s %.2f\n",
1792 GNUNET_i2s(peer), p->f);
1793 mlp_create_problem_update_value (&mlp->p, p->r_c9, mlp->p.c_r, -p->f, __LINE__);
1795 /* Problem size changed: new address for peer with pending request */
1796 mlp->mlp_prob_updated = GNUNET_YES;
1797 if (GNUNET_YES == mlp->mlp_auto_solve)
1798 GAS_mlp_solve_problem (solver);
1804 * Get application feedback for a peer
1806 * @param solver the solver handle
1807 * @param application the application
1808 * @param peer the peer to change the preference for
1809 * @param scope the time interval for this feedback: [now - scope .. now]
1810 * @param kind the kind to change the preference
1811 * @param score the score
1814 GAS_mlp_address_preference_feedback (void *solver,
1816 const struct GNUNET_PeerIdentity *peer,
1817 const struct GNUNET_TIME_Relative scope,
1818 enum GNUNET_ATS_PreferenceKind kind,
1821 struct GAS_PROPORTIONAL_Handle *s = solver;
1822 GNUNET_assert (NULL != solver);
1823 GNUNET_assert (NULL != peer);
1825 GNUNET_assert (NULL != s);
1830 mlp_free_peers (void *cls,
1831 const struct GNUNET_PeerIdentity *key, void *value)
1833 struct GNUNET_CONTAINER_MultiPeerMap *map = cls;
1834 struct ATS_Peer *p = value;
1836 GNUNET_CONTAINER_multipeermap_remove (map, key, value);
1844 * Shutdown the MLP problem solving component
1846 * @param cls the solver handle
1850 libgnunet_plugin_ats_mlp_done (void *cls)
1852 struct GAS_MLP_Handle *mlp = cls;
1853 GNUNET_assert (mlp != NULL);
1855 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down mlp solver\n");
1856 mlp_delete_problem (mlp);
1858 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
1860 mlp->requested_peers);
1861 GNUNET_CONTAINER_multipeermap_destroy (mlp->requested_peers);
1862 mlp->requested_peers = NULL;
1864 /* Clean up GLPK environment */
1868 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutdown down of mlp solver complete\n");
1874 libgnunet_plugin_ats_mlp_init (void *cls)
1876 struct GNUNET_ATS_PluginEnvironment *env = cls;
1877 struct GAS_MLP_Handle * mlp = GNUNET_malloc (sizeof (struct GAS_MLP_Handle));
1882 unsigned long long tmp;
1889 struct GNUNET_TIME_Relative max_duration;
1890 long long unsigned int max_iterations;
1892 GNUNET_assert (NULL != env->cfg);
1893 GNUNET_assert (NULL != env->stats);
1894 GNUNET_assert (NULL != env->addresses);
1895 GNUNET_assert (NULL != env->bandwidth_changed_cb);
1896 GNUNET_assert (NULL != env->get_preferences);
1897 GNUNET_assert (NULL != env->get_property);
1899 /* Init GLPK environment */
1900 int res = glp_init_env();
1903 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1904 "initialization successful");
1907 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1908 "environment is already initialized");
1911 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1912 "initialization failed (insufficient memory)");
1917 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1918 "initialization failed (unsupported programming model)");
1926 mlp->write_mip_mps = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "ats",
1928 if (GNUNET_SYSERR == mlp->write_mip_mps)
1929 mlp->write_mip_mps = GNUNET_NO;
1930 mlp->write_mip_sol = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "ats",
1932 if (GNUNET_SYSERR == mlp->write_mip_sol)
1933 mlp->write_mip_sol = GNUNET_NO;
1935 mlp->pv.BIG_M = (double) BIG_M_VALUE;
1937 /* Get timeout for iterations */
1938 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats", "MLP_MAX_DURATION", &max_duration))
1940 max_duration = MLP_MAX_EXEC_DURATION;
1943 /* Get maximum number of iterations */
1944 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(env->cfg, "ats", "MLP_MAX_ITERATIONS", &max_iterations))
1946 max_iterations = MLP_MAX_ITERATIONS;
1949 /* Get diversity coefficient from configuration */
1950 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
1951 "MLP_COEFFICIENT_D",
1953 D = (double) tmp / 100;
1957 /* Get proportionality coefficient from configuration */
1958 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
1959 "MLP_COEFFICIENT_R",
1961 R = (double) tmp / 100;
1965 /* Get utilization coefficient from configuration */
1966 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
1967 "MLP_COEFFICIENT_U",
1969 U = (double) tmp / 100;
1973 /* Get quality metric coefficients from configuration */
1974 int i_delay = MLP_NaN;
1975 int i_distance = MLP_NaN;
1976 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
1977 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1979 /* initialize quality coefficients with default value 1.0 */
1980 mlp->pv.co_Q[c] = DEFAULT_QUALITY;
1982 mlp->pv.q[c] = q[c];
1983 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
1985 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
1989 if ((i_delay != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
1990 "MLP_COEFFICIENT_QUALITY_DELAY",
1993 mlp->pv.co_Q[i_delay] = (double) tmp / 100;
1995 mlp->pv.co_Q[i_delay] = DEFAULT_QUALITY;
1997 if ((i_distance != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
1998 "MLP_COEFFICIENT_QUALITY_DISTANCE",
2000 mlp->pv.co_Q[i_distance] = (double) tmp / 100;
2002 mlp->pv.co_Q[i_distance] = DEFAULT_QUALITY;
2004 /* Get minimum bandwidth per used address from configuration */
2005 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2006 "MLP_MIN_BANDWIDTH",
2011 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2014 /* Get minimum number of connections from configuration */
2015 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2016 "MLP_MIN_CONNECTIONS",
2020 n_min = DEFAULT_MIN_CONNECTIONS;
2022 /* Init network quotas */
2023 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2024 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2027 for (c2 = 0; c2 < env->network_count; c2++)
2029 if (quotas[c] == env->networks[c2])
2031 mlp->pv.quota_index[c] = env->networks[c2];
2032 mlp->pv.quota_out[c] = env->out_quota[c2];
2033 mlp->pv.quota_in[c] = env->in_quota[c2];
2035 LOG (GNUNET_ERROR_TYPE_DEBUG, "Quota for network `%s' (in/out) %llu/%llu\n",
2036 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2037 mlp->pv.quota_out[c],
2038 mlp->pv.quota_in[c]);
2043 /* Check if defined quota could make problem unsolvable */
2044 if ((n_min * b_min) > mlp->pv.quota_out[c])
2046 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2047 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2048 mlp->pv.quota_out[c],
2050 mlp->pv.quota_out[c] = (n_min * b_min);
2052 if ((n_min * b_min) > mlp->pv.quota_in[c])
2054 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2055 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2056 mlp->pv.quota_in[c],
2058 mlp->pv.quota_in[c] = (n_min * b_min);
2061 /* Check if bandwidth is too big to make problem solvable */
2062 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
2064 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
2065 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2066 mlp->pv.quota_out[c],
2068 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
2070 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
2072 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
2073 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2074 mlp->pv.quota_in[c],
2076 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
2079 if (GNUNET_NO == found)
2081 mlp->pv.quota_in[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2082 mlp->pv.quota_out[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2083 LOG (GNUNET_ERROR_TYPE_INFO, _("Using default quota configuration for network `%s' (in/out) %llu/%llu\n"),
2084 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2085 mlp->pv.quota_in[c],
2086 mlp->pv.quota_out[c]);
2090 env->sf.s_add = &GAS_mlp_address_add;
2091 env->sf.s_address_update_property = &GAS_mlp_address_property_changed;
2092 env->sf.s_address_update_session = &GAS_mlp_address_session_changed;
2093 env->sf.s_address_update_inuse = &GAS_mlp_address_inuse_changed;
2094 env->sf.s_address_update_network = &GAS_mlp_address_change_network;
2095 env->sf.s_get = &GAS_mlp_get_preferred_address;
2096 env->sf.s_get_stop = &GAS_mlp_stop_get_preferred_address;
2097 env->sf.s_pref = &GAS_mlp_address_change_preference;
2098 env->sf.s_feedback = &GAS_mlp_address_preference_feedback;
2099 env->sf.s_del = &GAS_mlp_address_delete;
2100 env->sf.s_bulk_start = &GAS_mlp_bulk_start;
2101 env->sf.s_bulk_stop = &GAS_mlp_bulk_stop;
2104 /* Assign options to handle */
2105 mlp->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
2106 mlp->addresses = env->addresses;
2107 mlp->bw_changed_cb = env->bandwidth_changed_cb;
2108 mlp->bw_changed_cb_cls = env->bw_changed_cb_cls;
2109 mlp->get_preferences = env->get_preferences;
2110 mlp->get_preferences_cls = env->get_preference_cls;
2111 mlp->get_properties = env->get_property;
2112 mlp->get_properties_cls = env->get_property_cls;
2113 /* Setting MLP Input variables */
2118 mlp->pv.b_min = b_min;
2119 mlp->pv.n_min = n_min;
2120 mlp->pv.m_q = GNUNET_ATS_QualityPropertiesCount;
2121 mlp->mlp_prob_changed = GNUNET_NO;
2122 mlp->mlp_prob_updated = GNUNET_NO;
2123 mlp->mlp_auto_solve = GNUNET_YES;
2124 mlp->requested_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2125 mlp->bulk_request = 0;
2129 /* Redirect GLPK output to GNUnet logging */
2130 glp_term_hook (&mlp_term_hook, (void *) mlp);
2132 /* Init LP solving parameters */
2133 glp_init_smcp(&mlp->control_param_lp);
2134 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
2136 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
2138 mlp->control_param_lp.it_lim = max_iterations;
2139 mlp->control_param_lp.tm_lim = max_duration.rel_value_us / 1000LL;
2141 /* Init MLP solving parameters */
2142 glp_init_iocp(&mlp->control_param_mlp);
2143 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
2145 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
2147 mlp->control_param_mlp.tm_lim = max_duration.rel_value_us / 1000LL;
2149 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
2154 /* end of gnunet-service-ats_addresses_mlp.c */