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_addresses_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_addresses.h"
30 #include "gnunet-service-ats_addresses_mlp.h"
31 #include "gnunet_statistics_service.h"
36 * NOTE: Do not modify this documentation. This documentation is based on
37 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
38 * use build_txt.sh to generate plaintext output
40 * The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
41 * optimizing an mixed integer programming problem. The MLP solver uses a
42 * number of constraints to find the best adddress for a peer and an optimal
43 * bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
46 * We defined a constraint system to find an optimal bandwidth assignment.
47 * This constraint system uses as an input data addresses, bandwidth quotas,
48 * preferences and quality values. This constraint system is stored in an
49 * matrix based equotation system.
53 * A (M)LP problem consists of a target function to optimizes, constraints
54 * and rows and columns. FIXME GLP uses three arrays to index the matrix: two
55 * integer arrays storing the row and column indices in the matrix and an
56 * float array to store the coeeficient.
58 * To solve the problem we first find an initial solution for the LP problem
59 * using the LP solver and then find an MLP solution based on this solution
60 * using the MLP solver.
62 * Solving (M)LP problems has the property that finding an initial solution
63 * for the LP problem is computationally expensive and finding the MLP
64 * solution is cheaper. This is especially interesting an existing LP
65 * solution can be reused if only coefficients in the matrix have changed
66 * (addresses updated). Only when the problem size changes (addresses added
67 * or deleted) a new LP solution has to be found.
70 * The mlp solver solves the bandwidth assignment problem only on demand when
71 * an address suggestion is requested. When an address is requested mlp the
72 * solves the mlp problem and if the active address or the bandwidth assigned
73 * changes it calls the callback to addresses. The mlp solver gets notified
74 * about new addresses (adding sessions), removed addresses (address
75 * deletions) and address updates. To benefit from the mlp properties
76 * mentioned in section 5 the solver rembers if since the last solution
77 * addresses were added or deleted (problem size changed, problem has to be
78 * rebuild and solved from sratch) or if addresses were updated and the
79 * existing solution can be reused.
83 * The quotas for each network segment are passed by addresses. MLP can be
84 * adapted using configuration settings and uses the following parameters:
86 * Maximum duration for a MLP solution procees (default: 3 sec.)
88 * Maximum number of iterations for a MLP solution process (default:
90 * * MLP_MIN_CONNECTIONS:
91 * Minimum number of desired connections (default: 4)
92 * * MLP_MIN_BANDWIDTH:
93 * Minimum amount of bandwidth assigned to an address (default: 1024)
94 * * MLP_COEFFICIENT_D:
95 * Diversity coefficient (default: 1.0)
96 * * MLP_COEFFICIENT_R:
97 * Relativity coefficient (default: 1.0)
98 * * MLP_COEFFICIENT_U:
99 * Utilization coefficient (default: 1.0)
100 * * MLP_COEFFICIENT_D:
101 * Diversity coefficient (default: 1.0)
102 * * MLP_COEFFICIENT_QUALITY_DELAY:
103 * Quality delay coefficient (default: 1.0)
104 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
105 * Quality distance coefficient (default: 1.0)
106 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
107 * Quality distance coefficient (default: 1.0)
108 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
109 * Quality distance coefficient (default: 1.0)
110 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
111 * Quality distance coefficient (default: 1.0)
113 * 5.2 Data structures used
115 * mlp has for each known peer a struct ATS_Peer containing information about
116 * a specific peer. The address field solver_information contains information
117 * about the mlp properties of this address.
121 * During initialization mlp initializes the GLPK libray used to solve the
122 * MLP problem: it initializes the glpk environment and creates an initial LP
123 * problem. Next it loads the configuration values from the configuration or
124 * uses the default values configured in -addresses_mlp.h. The quotas used
125 * are given by addresses but may have to be adjusted. mlp uses a upper limit
126 * for the bandwidth assigned called BIG M and a minimum amount of bandwidth
127 * an address gets assigned as well as a minium desired number of
128 * connections. If the configured quota is bigger than BIG M, it is reduced
129 * to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
130 * *MLP_MIN_BANDWIDTH it is increased to this value.
136 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
139 * Print debug output for mlp problem creation
141 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
144 * Enable GLPK verbose output
146 #define VERBOSE_GLPK GNUNET_NO
149 * Maximize bandwidth assigned
151 * This option can be used to test if problem can be solved at all without
152 * optimizing for utility, diversity or relativity
155 #define MAXIMIZE_FOR_BANDWIDTH_ASSIGNED GNUNET_NO
158 * Intercept GLPK terminal output
159 * @param info the mlp handle
160 * @param s the string to print
161 * @return 0: glpk prints output on terminal, 0 != surpress output
164 mlp_term_hook (void *info, const char *s)
166 /* Not needed atm struct MLP_information *mlp = info; */
167 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s", s);
173 * Reset peers for next problem creation
175 * @param cls not used
177 * @param value ATS_Peer
181 reset_peers (void *cls, const struct GNUNET_HashCode * key, void *value)
183 struct ATS_Peer *peer = value;
184 peer->processed = GNUNET_NO;
189 * Delete the MLP problem and free the constrain matrix
191 * @param mlp the MLP handle
194 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
199 if (mlp->p.prob != NULL)
201 glp_delete_prob(mlp->p.prob);
205 /* delete row index */
206 if (mlp->p.ia != NULL)
208 GNUNET_free (mlp->p.ia);
212 /* delete column index */
213 if (mlp->p.ja != NULL)
215 GNUNET_free (mlp->p.ja);
219 /* delete coefficients */
220 if (mlp->p.ar != NULL)
222 GNUNET_free (mlp->p.ar);
228 mlp->p.c_d = MLP_UNDEFINED;
229 mlp->p.c_r = MLP_UNDEFINED;
230 mlp->p.r_c2 = MLP_UNDEFINED;
231 mlp->p.r_c4 = MLP_UNDEFINED;
232 mlp->p.r_c6 = MLP_UNDEFINED;
233 mlp->p.r_c9 = MLP_UNDEFINED;
234 for (c = 0; c < mlp->pv.m_q ; c ++)
235 mlp->p.r_q[c] = MLP_UNDEFINED;
236 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c ++)
237 mlp->p.r_quota[c] = MLP_UNDEFINED;
238 mlp->p.ci = MLP_UNDEFINED;
241 GNUNET_CONTAINER_multihashmap_iterate (mlp->peers, &reset_peers, NULL);
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_UP:
259 return "GNUNET_ATS_UTILIZATION_UP";
260 case GNUNET_ATS_UTILIZATION_DOWN:
261 return "GNUNET_ATS_UTILIZATION_DOWN";
262 case GNUNET_ATS_COST_LAN:
263 return "GNUNET_ATS_COST_LAN";
264 case GNUNET_ATS_COST_WAN:
265 return "GNUNET_ATS_COST_LAN";
266 case GNUNET_ATS_COST_WLAN:
267 return "GNUNET_ATS_COST_WLAN";
268 case GNUNET_ATS_NETWORK_TYPE:
269 return "GNUNET_ATS_NETWORK_TYPE";
270 case GNUNET_ATS_QUALITY_NET_DELAY:
271 return "GNUNET_ATS_QUALITY_NET_DELAY";
272 case GNUNET_ATS_QUALITY_NET_DISTANCE:
273 return "GNUNET_ATS_QUALITY_NET_DISTANCE";
281 * Translate glpk status error codes to text
282 * @param retcode return code
283 * @return string with result
286 mlp_status_to_string (int retcode)
290 return "solution is undefined";
292 return "solution is feasible";
294 return "solution is infeasible";
296 return "no feasible solution exists";
298 return "solution is optimal";
300 return "solution is unbounded";
303 return "unknown error";
308 * Translate glpk solver error codes to text
309 * @param retcode return code
310 * @return string with result
313 mlp_solve_to_string (int retcode)
319 return "invalid basis";
321 return "singular matrix";
323 return "ill-conditioned matrix";
325 return "invalid bounds";
327 return "solver failed";
329 return "objective lower limit reached";
331 return "objective upper limit reached";
333 return "iteration limit exceeded";
335 return "time limit exceeded";
337 return "no primal feasible solution";
339 return "no dual feasible solution";
341 return "root LP optimum not provided";
343 return "search terminated by application";
345 return "relative mip gap tolerance reached";
347 return "no dual feasible solution";
349 return "no convergence";
351 return "numerical instability";
353 return "invalid data";
355 return "result out of range";
358 return "unknown error";
363 * Extract an ATS performance info from an address
365 * @param address the address
366 * @param type the type to extract in HBO
367 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
370 get_performance_info (struct ATS_Address *address, uint32_t type)
373 GNUNET_assert (NULL != address);
375 if ((NULL == address->atsi) || (0 == address->atsi_count))
376 return GNUNET_ATS_VALUE_UNDEFINED;
378 for (c1 = 0; c1 < address->atsi_count; c1++)
380 if (ntohl(address->atsi[c1].type) == type)
381 return ntohl(address->atsi[c1].value);
383 return GNUNET_ATS_VALUE_UNDEFINED;
389 struct GNUNET_CONTAINER_MultiHashMap * peers;
394 mlp_create_problem_count_addresses_it (void *cls, const struct GNUNET_HashCode *key, void *value)
396 struct CountContext *cctx = cls;
397 /* Check if we have to add this peer due to a pending request */
398 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(cctx->peers, key))
403 static int mlp_create_problem_count_addresses (
404 struct GNUNET_CONTAINER_MultiHashMap * peers,
405 struct GNUNET_CONTAINER_MultiHashMap * addresses)
407 struct CountContext cctx;
410 GNUNET_CONTAINER_multihashmap_iterate (addresses, &mlp_create_problem_count_addresses_it, &cctx);
417 mlp_create_problem_set_value (struct MLP_Problem *p,
418 int row, int col, double val,
421 if ((p->ci) >= p->num_elements)
423 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
424 line, p->ci + 1, p->num_elements);
428 if ((0 == row) || (0 == col))
433 #if DEBUG_MLP_PROBLEM_CREATION
434 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u == %.2f\n",
435 line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
441 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
442 unsigned int type, unsigned int bound, double lb, double ub,
445 int col = glp_add_cols (p->prob, 1);
446 glp_set_col_name (p->prob, col, name);
447 glp_set_col_bnds (p->prob, col, bound, lb, ub);
448 glp_set_col_kind (p->prob, col, type);
449 glp_set_obj_coef (p->prob, col, coef);
450 #if DEBUG_MLP_PROBLEM_CREATION
451 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
458 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
459 unsigned int bound, double lb, double ub)
462 int row = glp_add_rows (p->prob, 1);
464 glp_set_row_name (p->prob, row, name);
465 /* set row bounds: <= 0 */
466 glp_set_row_bnds (p->prob, row, bound, lb, ub);
469 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
472 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
475 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
478 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
481 GNUNET_asprintf(&op, "ERROR");
484 #if DEBUG_MLP_PROBLEM_CREATION
485 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
494 * - address columns b and n
495 * - address dependent constraint rows c1, c3
496 * - peer dependent rows c2 and c9
497 * - Set address dependent entries in problem matrix as well
500 mlp_create_problem_add_address_information (void *cls, const struct GNUNET_HashCode *key, void *value)
502 struct GAS_MLP_Handle *mlp = cls;
503 struct MLP_Problem *p = &mlp->p;
504 struct ATS_Address *address = value;
505 struct ATS_Peer *peer;
506 struct MLP_information *mlpi;
511 /* Check if we have to add this peer due to a pending request */
512 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->peers, key))
515 mlpi = address->solver_information;
523 peer = GNUNET_CONTAINER_multihashmap_get (mlp->peers, key);
524 if (peer->processed == GNUNET_NO)
526 /* Add peer dependent constraints */
527 /* Add constraint c2 */
528 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
529 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
531 /* Add constraint c9 */
532 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
533 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
535 /* c 9) set coefficient */
536 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f, __LINE__);
538 peer->processed = GNUNET_YES;
541 /* Reset addresses' solver information */
547 for (c = 0; c < mlp->pv.m_q; c++)
550 /* Add bandwidth column */
551 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
552 #if TEST_MAX_BW_ASSIGNMENT
553 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
555 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
560 /* Add usage column */
561 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
562 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
565 /* Add address dependent constraints */
566 /* Add constraint c1) bandwidth capping
567 * b_t + (-M) * n_t <= 0
569 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
570 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
573 /* c1) set b = 1 coefficient */
574 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
575 /* c1) set n = -M coefficient */
576 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -mlp->pv.BIG_M, __LINE__);
578 /* Add constraint c 3) minimum bandwidth
579 * b_t + (-n_t * b_min) >= 0
581 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
582 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
585 /* c3) set b = 1 coefficient */
586 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
587 /* c3) set n = -b_min coefficient */
588 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
591 /* Set coefficient entries in invariant rows */
592 /* c 4) minimum connections */
593 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
594 /* c 6) maximize diversity */
595 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
596 /* c 2) 1 address peer peer */
597 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
598 /* c 9) relativity */
599 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
601 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
603 /* c 10) obey network specific quotas
604 * (1)*b_1 + ... + (1)*b_m <= quota_n
606 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
608 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
609 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
610 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
612 if (mlp->pv.quota_index[c] == addr_net)
614 mlp_create_problem_set_value (p, p->r_quota[c], mlpi->c_b, 1, __LINE__);
619 /* c 7) Optimize quality */
620 /* For all quality metrics, set quality of this address */
621 for (c = 0; c < mlp->pv.m_q; c++)
622 mlp_create_problem_set_value (p, p->r_q[c], mlpi->c_b, mlpi->q_averaged[c], __LINE__);
628 * Create the invariant columns c4, c6, c10, c8, c7
631 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
636 /* Row for c4) minimum connection */
637 /* Number of minimum connections is min(|Peers|, n_min) */
638 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);
640 /* Add row for c6) */
641 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
643 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
645 /* Add rows for c 10) */
646 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
649 GNUNET_asprintf(&text, "c10_quota_ats_%s", GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
650 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
654 /* Adding rows for c 8) */
655 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
657 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
659 /* c 7) For all quality metrics */
660 for (c = 0; c < mlp->pv.m_q; c++)
662 GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->pv.q[c]));
663 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
665 mlp_create_problem_set_value (p, p->r_q[c], p->c_q[c], -1, __LINE__);
671 * Create the invariant columns d, u, r, q0 ... qm
674 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
679 #if TEST_MAX_BW_ASSIGNMENT
684 //mlp->pv.co_R = 0.0;
686 /* Diversity d column */
687 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
689 /* Utilization u column */
690 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
692 /* Relativity r column */
693 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
695 /* Quality metric columns */
696 for (c = 0; c < mlp->pv.m_q; c++)
698 GNUNET_asprintf (&name, "q_%u", mlp->pv.q[c]);
699 #if TEST_MAX_BW_ASSIGNMENT
700 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
702 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
710 * Create the MLP problem
712 * @param mlp the MLP handle
713 * @param addresses the hashmap containing all adresses
714 * @return GNUNET_OK or GNUNET_SYSERR
717 mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
719 struct MLP_Problem *p = &mlp->p;
722 GNUNET_assert (p->prob == NULL);
723 GNUNET_assert (p->ia == NULL);
724 GNUNET_assert (p->ja == NULL);
725 GNUNET_assert (p->ar == NULL);
726 /* Reset MLP problem struct */
728 /* create the glpk problem */
729 p->prob = glp_create_prob ();
730 GNUNET_assert (NULL != p->prob);
731 p->num_peers = GNUNET_CONTAINER_multihashmap_size (mlp->peers);
732 p->num_addresses = mlp_create_problem_count_addresses (mlp->peers, addresses);
734 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
735 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses + mlp->pv.m_q + p->num_peers + 2 + 1);
736 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
737 p->num_peers, p->num_addresses, mlp->pv.m_q, p->num_elements);
739 /* Set a problem name */
740 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
741 /* Set optimization direction to maximize */
742 glp_set_obj_dir (p->prob, GLP_MAX);
744 /* Create problem matrix */
745 /* last +1 caused by glpk index starting with one: [1..elements]*/
748 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
750 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
752 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
754 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
756 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
757 return GNUNET_SYSERR;
760 /* Adding invariant columns */
761 mlp_create_problem_add_invariant_columns (mlp, p);
763 /* Adding address independent constraint rows */
764 mlp_create_problem_add_invariant_rows (mlp, p);
766 /* Adding address independent constraint rows */
767 GNUNET_CONTAINER_multihashmap_iterate (addresses, &mlp_create_problem_add_address_information, mlp);
769 /* Load the matrix */
770 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
771 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
777 * Solves the LP problem
779 * @param mlp the MLP Handle
780 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
783 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
787 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
789 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
791 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
793 /* Analyze problem status */
794 res = glp_get_status (mlp->p.prob);
796 /* solution is optimal */
798 /* solution is feasible */
800 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n",
801 res, mlp_status_to_string(res));
803 /* Problem was ill-defined, no way to handle that */
805 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed, no solution: 0x%02X %s\n",
806 res, mlp_status_to_string(res));
807 return GNUNET_SYSERR;
813 * Solves the MLP problem
815 * @param mlp the MLP Handle
816 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
819 mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
822 res = glp_intopt(mlp->p.prob, &mlp->control_param_mlp);
824 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
826 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving MLP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
827 /* Analyze problem status */
828 res = glp_mip_status(mlp->p.prob);
830 /* solution is optimal */
832 /* solution is feasible */
834 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_status_to_string(res));
836 /* Problem was ill-defined, no way to handle that */
838 LOG (GNUNET_ERROR_TYPE_WARNING,"Solving MLP problem failed, 0x%02X %s\n\n", res, mlp_status_to_string(res));
839 return GNUNET_SYSERR;
845 * Propagates the results when MLP problem was solved
847 * @param cls the MLP handle
848 * @param key the peer identity
849 * @param value the address
850 * @return GNUNET_OK to continue
853 mlp_propagate_results (void *cls, const struct GNUNET_HashCode *key, void *value)
855 struct GAS_MLP_Handle *mlp = cls;
856 struct ATS_Address *address;
857 struct MLP_information *mlpi;
858 double mlp_bw_in = MLP_NaN;
859 double mlp_bw_out = MLP_NaN;
860 double mlp_use = MLP_NaN;
862 /* Check if we have to add this peer due to a pending request */
863 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->peers, key))
866 GNUNET_assert (address->solver_information != NULL);
867 mlpi = address->solver_information;
869 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
870 if (mlp_bw_in > (double) UINT32_MAX)
872 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
873 mlp_bw_in = (double) UINT32_MAX;
875 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
876 if (mlp_bw_out > (double) UINT32_MAX)
878 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
879 mlp_bw_out = (double) UINT32_MAX;
881 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
885 if ((GLP_YES == mlp_use) && (GNUNET_NO == address->active))
887 /* Address switch: Activate address*/
888 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
889 address->active = GNUNET_YES;
890 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
891 mlpi->b_in.value__ = htonl(mlp_bw_in);
892 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
893 mlpi->b_out.value__ = htonl(mlp_bw_out);
895 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
897 else if ((GLP_NO == mlp_use) && (GNUNET_YES == address->active))
899 /* Address switch: Disable address*/
900 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
901 address->active = GNUNET_NO;
902 /* Set bandwidth to 0 */
903 address->assigned_bw_in.value__ = htonl (0);
904 mlpi->b_in.value__ = htonl(mlp_bw_in);
905 address->assigned_bw_out.value__ = htonl (0);
906 mlpi->b_out.value__ = htonl(mlp_bw_out);
908 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
910 else if ((mlp_bw_out != ntohl(address->assigned_bw_out.value__)) ||
911 (mlp_bw_in != ntohl(address->assigned_bw_in.value__)))
913 /* Bandwidth changed */
914 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
915 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
916 mlpi->b_in.value__ = htonl(mlp_bw_in);
917 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
918 mlpi->b_out.value__ = htonl(mlp_bw_out);
920 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
924 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
933 * Solves the MLP problem
935 * @param solver the MLP Handle
936 * @param addresses the address hashmap
937 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
940 GAS_mlp_solve_problem (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses)
942 struct GAS_MLP_Handle *mlp = solver;
946 struct GNUNET_TIME_Absolute start_build;
947 struct GNUNET_TIME_Relative duration_build;
948 struct GNUNET_TIME_Absolute start_lp;
949 struct GNUNET_TIME_Relative duration_lp;
950 struct GNUNET_TIME_Absolute start_mlp;
951 struct GNUNET_TIME_Relative duration_mlp;
952 GNUNET_assert (NULL != solver);
954 if ((GNUNET_NO == mlp->mlp_prob_changed) && (GNUNET_NO == mlp->mlp_prob_updated))
956 LOG (GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
960 if (GNUNET_YES == mlp->mlp_prob_changed)
962 LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
963 mlp_delete_problem (mlp);
964 start_build = GNUNET_TIME_absolute_get();
965 if (GNUNET_SYSERR == mlp_create_problem (mlp, addresses))
966 return GNUNET_SYSERR;
967 duration_build = GNUNET_TIME_absolute_get_duration (start_build);
968 mlp->control_param_lp.presolve = GLP_YES;
969 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
973 LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
974 duration_build.rel_value = 0;
978 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running LP solver %s\n", (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
979 start_lp = GNUNET_TIME_absolute_get();
980 res_lp = mlp_solve_lp_problem (mlp);
981 duration_lp = GNUNET_TIME_absolute_get_duration (start_lp);
985 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
986 start_mlp = GNUNET_TIME_absolute_get();
987 res_mip = mlp_solve_mlp_problem (mlp);
989 duration_mlp = GNUNET_TIME_absolute_get_duration (start_mlp);
992 mlp->ps.lp_res = res_lp;
993 mlp->ps.mip_res = res_mip;
994 mlp->ps.build_dur = duration_build;
995 mlp->ps.lp_dur = duration_lp;
996 mlp->ps.mip_dur = duration_mlp;
997 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
998 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
999 mlp->ps.p_cols = glp_get_num_cols (mlp->p.prob);
1000 mlp->ps.p_rows = glp_get_num_rows (mlp->p.prob);
1001 mlp->ps.p_elements = mlp->p.num_elements;
1003 LOG (GNUNET_ERROR_TYPE_DEBUG, "Execution time: Build %llu ms, LP %llu ms, MLP %llu ms\n",
1004 (unsigned long long) duration_build.rel_value,
1005 (unsigned long long) duration_lp.rel_value,
1006 (unsigned long long) duration_mlp.rel_value);
1008 /* Propagate result*/
1009 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1010 GNUNET_CONTAINER_multihashmap_iterate (addresses, &mlp_propagate_results, mlp);
1012 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1013 if (GNUNET_YES == mlp->write_mip_mps)
1015 /* Write problem and solution to disk */
1016 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value);
1017 glp_write_mps(mlp->p.prob, GLP_MPS_FILE, NULL, filename);
1018 GNUNET_free (filename);
1020 if (GNUNET_YES == mlp->write_mip_sol)
1022 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value);
1023 glp_print_mip (mlp->p.prob, filename );
1024 GNUNET_free (filename);
1027 /* Reset change and update marker */
1028 mlp->control_param_lp.presolve = GLP_NO;
1029 mlp->mlp_prob_updated = GNUNET_NO;
1030 mlp->mlp_prob_changed = GNUNET_NO;
1032 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1035 return GNUNET_SYSERR;
1039 * Add a single address to the solve
1041 * @param solver the solver Handle
1042 * @param addresses the address hashmap containing all addresses
1043 * @param address the address to add
1044 * @param network network type of this address
1047 GAS_mlp_address_add (void *solver,
1048 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1049 struct ATS_Address *address,
1052 struct GAS_MLP_Handle *mlp = solver;
1054 struct MLP_information *mlpi;
1058 GNUNET_assert (NULL != solver);
1059 GNUNET_assert (NULL != addresses);
1060 GNUNET_assert (NULL != address);
1063 if (NULL == address->solver_information)
1065 address->solver_information = GNUNET_malloc (sizeof (struct MLP_information));
1066 mlpi = address->solver_information;
1067 for (c1 = 0; c1 < mlp->pv.m_q; c1++)
1069 mlpi->q_averaged[c1] = DEFAULT_QUALITY;
1070 for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
1071 mlpi->q[c1][c2] = MLP_NaN;
1075 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding address for peer `%s' multiple times\n"), GNUNET_i2s(&address->peer));
1077 /* Is this peer included in the problem? */
1078 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &address->peer.hashPubKey)))
1080 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1084 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1085 /* Problem size changed: new address for peer with pending request */
1086 mlp->mlp_prob_changed = GNUNET_YES;
1087 if (GNUNET_YES == mlp->mlp_auto_solve)
1088 GAS_mlp_solve_problem (solver, addresses);
1093 mlp_update_quality (struct GAS_MLP_Handle *mlp,
1094 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1095 struct ATS_Address * address,
1096 const struct GNUNET_ATS_Information *ats_prev, uint32_t ats_prev_count)
1098 struct MLP_information *mlpi = address->solver_information;
1099 unsigned int c_ats_entry;
1100 unsigned int c_queue_entries;
1102 unsigned int c_queue_it;
1104 unsigned int c_qual;
1110 uint32_t prev_value;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating %u quality metrics for peer `%s'\n",
1120 ats_prev_count, GNUNET_i2s (&address->peer));
1122 GNUNET_assert (NULL != mlp);
1123 GNUNET_assert (NULL != address);
1124 GNUNET_assert (NULL != address->solver_information);
1125 GNUNET_assert (NULL != ats_prev);
1127 if (NULL == mlp->p.prob)
1130 qual_changed = GNUNET_NO;
1131 for (c_ats_entry = 0; c_ats_entry < ats_prev_count; c_ats_entry++)
1133 type = ntohl (ats_prev[c_ats_entry].type);
1134 prev_value = ntohl (ats_prev[c_ats_entry].value);
1138 /* Check for network update */
1139 if (type == GNUNET_ATS_NETWORK_TYPE)
1141 new_value = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1142 if (GNUNET_ATS_VALUE_UNDEFINED == new_value)
1143 new_value = GNUNET_ATS_NET_UNSPECIFIED;
1144 if (new_value != prev_value)
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating network for peer `%s' from `%s' to `%s'\n",
1147 GNUNET_i2s (&address->peer),
1148 GNUNET_ATS_print_network_type(prev_value),
1149 GNUNET_ATS_print_network_type(new_value));
1152 if (mlpi->c_b == MLP_UNDEFINED)
1153 continue; /* This address is not yet in the matrix*/
1155 rows = glp_get_num_rows(mlp->p.prob);
1156 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1157 val = GNUNET_malloc (rows * sizeof (double) + 1);
1158 int length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1160 for (c_net = 0; c_net <= length + 1; c_net ++)
1162 if (ind[c_net] == mlp->p.r_quota[prev_value])
1163 break; /* Found index for old network */
1166 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1167 /* Set updated column */
1168 ind[c_net] = mlp->p.r_quota[new_value];
1170 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1174 rows = glp_get_num_rows(mlp->p.prob);
1175 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1176 val = GNUNET_malloc (rows * sizeof (double) + 1);
1177 length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1179 for (c_net = 0; c_net <= length + 1; c_net ++)
1181 if (ind[c_net] == mlp->p.r_quota[prev_value])
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing old network index [%u] == [%f]\n",ind[c_net],val[c_net]);
1183 if (ind[c_net] == mlp->p.r_quota[new_value])
1185 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting new network index [%u] == [%f]\n",ind[c_net],val[c_net]);
1191 mlp->mlp_prob_changed = GNUNET_YES;
1196 /* Find index for this ATS type */
1197 for (c_cmp = 0; c_cmp < mlp->pv.m_q; c_cmp++)
1199 if (type == mlp->pv.q[c_cmp])
1205 if (-1 == type_index)
1206 continue; /* quality index not found */
1208 /* Get average queue index */
1209 avg_index = mlpi->q_avg_i[type_index];
1211 /* Update averaging queue */
1212 new_value = get_performance_info (address, type);
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s': `%s' from %u to %u\n",
1214 GNUNET_i2s (&address->peer),
1215 mlp_ats_to_string(mlp->pv.q[type_index]), prev_value, new_value);
1216 GNUNET_assert (GNUNET_ATS_VALUE_UNDEFINED != new_value);
1217 mlpi->q[type_index][avg_index] = new_value;
1219 /* Update averaging index */
1220 if (mlpi->q_avg_i[type_index] + 1 < (MLP_AVERAGING_QUEUE_LENGTH))
1221 mlpi->q_avg_i[type_index] ++;
1223 mlpi->q_avg_i[type_index] = 0;
1225 /* Update average depending on ATS type */
1228 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1229 case GNUNET_ATS_QUALITY_NET_DELAY:
1230 c_queue_entries = 0;
1232 for (c_queue_it = 0; c_queue_it < MLP_AVERAGING_QUEUE_LENGTH; c_queue_it++)
1234 if (mlpi->q[type_index][c_queue_it] != MLP_NaN)
1236 queue = mlpi->q[type_index] ;
1237 avg += queue[c_queue_it];
1241 if ((c_queue_entries > 0) && (avg > 0))
1242 /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
1243 mlpi->q_averaged[type_index] = (double) c_queue_entries / avg;
1245 mlpi->q_averaged[type_index] = 0.0;
1247 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s': `%s' average sum of %u elements == %f, average == %f, weight == %f\n",
1248 GNUNET_i2s (&address->peer),
1249 mlp_ats_to_string(mlp->pv.q[type_index]),
1252 avg / (double) c_queue_entries,
1253 mlpi->q_averaged[type_index]);
1254 qual_changed = GNUNET_YES;
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Update for ATS type `%s' not implemented!\n"),
1259 mlp_ats_to_string(type));
1263 /* Changed, but quality will be automatically set during rebuild */
1264 if ((GNUNET_YES == mlp->mlp_prob_changed) &&
1265 (GNUNET_YES == mlp->mlp_auto_solve))
1267 GAS_mlp_solve_problem (mlp, addresses);
1271 /* Update problem matrix if required */
1272 if (GNUNET_NO == qual_changed)
1275 /* Address not yet included in matrix */
1276 if (MLP_UNDEFINED == mlpi->c_b)
1279 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index]
1280 * Get column mlpi->c_b */
1281 rows = glp_get_num_rows(mlp->p.prob);
1282 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1283 val = GNUNET_malloc (rows * sizeof (double) + 1);
1284 int length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1286 for (c_qual = 0; c_qual < mlp->pv.m_q; c_qual++)
1288 for (c_row = 0; c_row <= length; c_row ++)
1290 if (ind[c_row] == mlp->p.r_q[c_qual])
1291 val[c_row] = mlpi->q_averaged[c_qual];
1294 /* Set updated column */
1295 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1298 mlp->mlp_prob_updated = GNUNET_YES;
1302 * Updates a single address in the MLP problem
1304 * If the address did not exist before in the problem:
1305 * The MLP problem has to be recreated and the problem has to be resolved
1307 * ATS performance information in address are already updated, delta + previous
1308 * values are included in atsi_prev (value GNUNET_ATS_VALUE_UNDEFINED if not existing before)
1310 * Otherwise the addresses' values can be updated and the existing base can
1313 * @param solver the solver Handle
1314 * @param addresses the address hashmap containing all addresses
1315 * @param address the update address
1316 * @param session the new session (if changed otherwise current)
1317 * @param in_use the new address in use state (if changed otherwise current)
1318 * @param atsi_prev ATS information updated + previous values, GNUNET_ATS_VALUE_UNDEFINED if not existing before
1319 * @param atsi_count_prev number of atsi values updated
1322 GAS_mlp_address_update (void *solver,
1323 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1324 struct ATS_Address *address,
1327 const struct GNUNET_ATS_Information *atsi_prev,
1328 uint32_t atsi_count_prev)
1331 struct GAS_MLP_Handle *mlp = solver;
1332 struct MLP_information *mlpi = address->solver_information;
1334 GNUNET_assert (NULL != solver);
1335 GNUNET_assert (NULL != addresses);
1336 GNUNET_assert (NULL != address);
1337 GNUNET_assert ((NULL != atsi_prev) || (0 == atsi_count_prev));
1341 LOG (GNUNET_ERROR_TYPE_ERROR, _("Updating address for peer `%s' not added before\n"), GNUNET_i2s(&address->peer));
1344 mlp_update_quality (mlp, addresses, address, atsi_prev, atsi_count_prev);
1346 /* Is this peer included in the problem? */
1347 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &address->peer.hashPubKey)))
1349 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1352 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1354 /* Problem size changed: new address for peer with pending request */
1355 mlp->mlp_prob_updated = GNUNET_YES;
1356 if (GNUNET_YES == mlp->mlp_auto_solve)
1357 GAS_mlp_solve_problem (solver, addresses);
1362 * Deletes a single address in the MLP problem
1364 * The MLP problem has to be recreated and the problem has to be resolved
1366 * @param solver the MLP Handle
1367 * @param addresses the address hashmap
1368 * the address has to be already removed from the hashmap
1369 * @param address the address to delete
1370 * @param session_only delete only session not whole address
1373 GAS_mlp_address_delete (void *solver,
1374 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1375 struct ATS_Address *address,
1379 struct GAS_MLP_Handle *mlp = solver;
1380 struct MLP_information *mlpi;
1382 GNUNET_assert (NULL != solver);
1383 GNUNET_assert (NULL != addresses);
1384 GNUNET_assert (NULL != address);
1386 mlpi = address->solver_information;
1391 address->solver_information = NULL;
1394 /* Is this peer included in the problem? */
1395 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &address->peer.hashPubKey)))
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1402 /* Problem size changed: new address for peer with pending request */
1403 mlp->mlp_prob_changed = GNUNET_YES;
1404 if (GNUNET_YES == mlp->mlp_auto_solve)
1405 GAS_mlp_solve_problem (solver, addresses);
1411 * Find the active address in the set of addresses of a peer
1412 * @param cls destination
1413 * @param key peer id
1414 * @param value address
1418 mlp_get_preferred_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1421 struct ATS_Address *aa = (struct ATS_Address *) cls;
1422 struct ATS_Address *addr = value;
1423 struct MLP_information *mlpi = addr->solver_information;
1426 if (mlpi->n == GNUNET_YES)
1429 aa->assigned_bw_in = mlpi->b_in;
1430 aa->assigned_bw_out = mlpi->b_out;
1438 * Get the preferred address for a specific peer
1440 * @param solver the MLP Handle
1441 * @param addresses address hashmap
1442 * @param peer the peer
1443 * @return suggested address
1445 const struct ATS_Address *
1446 GAS_mlp_get_preferred_address (void *solver,
1447 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1448 const struct GNUNET_PeerIdentity *peer)
1450 struct GAS_MLP_Handle *mlp = solver;
1452 struct ATS_Address *res = NULL;
1454 GNUNET_assert (NULL != solver);
1455 GNUNET_assert (NULL != addresses);
1456 GNUNET_assert (NULL != peer);
1458 LOG (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n",
1461 /* Is this peer included in the problem? */
1462 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &peer->hashPubKey)))
1464 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding peer `%s' to list of peers with requests\n",
1467 p = GNUNET_malloc (sizeof (struct ATS_Peer));
1469 p->f = DEFAULT_PEER_PREFERENCE;
1470 GNUNET_CONTAINER_multihashmap_put (mlp->peers, &peer->hashPubKey, p, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1472 /* Added new peer, we have to rebuild problem before solving */
1473 mlp->mlp_prob_changed = GNUNET_YES;
1475 if (GNUNET_YES == mlp->mlp_auto_solve)
1476 GAS_mlp_solve_problem (mlp, addresses);
1478 /* Get prefered address */
1479 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
1480 mlp_get_preferred_address_it, res);
1487 * Stop notifying about address and bandwidth changes for this peer
1489 * @param solver the MLP handle
1490 * @param addresses address hashmap
1491 * @param peer the peer
1494 GAS_mlp_stop_get_preferred_address (void *solver,
1495 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1496 const struct GNUNET_PeerIdentity *peer)
1498 struct GAS_MLP_Handle *mlp = solver;
1499 struct ATS_Peer *p = NULL;
1501 GNUNET_assert (NULL != solver);
1502 GNUNET_assert (NULL != addresses);
1503 GNUNET_assert (NULL != peer);
1505 if (NULL != (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &peer->hashPubKey)))
1507 GNUNET_CONTAINER_multihashmap_remove (mlp->peers, &peer->hashPubKey, p);
1514 * Changes the preferences for a peer in the MLP problem
1516 * @param solver the MLP Handle
1517 * @param client client
1518 * @param peer the peer
1519 * @param kind the kind to change the preference
1520 * @param score the score
1523 GAS_mlp_address_change_preference (void *solver,
1525 const struct GNUNET_PeerIdentity *peer,
1526 enum GNUNET_ATS_PreferenceKind kind,
1529 //struct GAS_MLP_Handle *mlp = solver;
1531 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing preference for address for peer `%s'\n",
1536 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1538 //struct ATS_Peer *p = mlp_find_peer (mlp, peer);
1539 //FIXME to finish implementation
1540 /* Here we have to do the matching */
1546 mlp_free_peers (void *cls, const struct GNUNET_HashCode *key, void *value)
1548 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
1549 struct ATS_Peer *p = value;
1551 GNUNET_CONTAINER_multihashmap_remove (map, key, value);
1559 * Shutdown the MLP problem solving component
1561 * @param solver the solver handle
1564 GAS_mlp_done (void *solver)
1566 struct GAS_MLP_Handle *mlp = solver;
1567 GNUNET_assert (mlp != NULL);
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down mlp solver\n");
1570 mlp_delete_problem (mlp);
1572 GNUNET_CONTAINER_multihashmap_iterate (mlp->peers, &mlp_free_peers, mlp->peers);
1573 GNUNET_CONTAINER_multihashmap_destroy (mlp->peers);
1576 /* Clean up GLPK environment */
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutdown down of mlp solver complete\n");
1585 * Init the MLP problem solving component
1587 * @param cfg the GNUNET_CONFIGURATION_Handle handle
1588 * @param stats the GNUNET_STATISTICS handle
1589 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1590 * @param out_dest array of outbound quotas
1591 * @param in_dest array of outbound quota
1592 * @param dest_length array length for quota arrays
1593 * @param bw_changed_cb callback for changed bandwidth amounts
1594 * @param bw_changed_cb_cls cls for callback
1595 * @return struct GAS_MLP_Handle on success, NULL on fail
1598 GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1599 const struct GNUNET_STATISTICS_Handle *stats,
1601 unsigned long long *out_dest,
1602 unsigned long long *in_dest,
1604 GAS_bandwidth_changed_cb bw_changed_cb,
1605 void *bw_changed_cb_cls)
1607 struct GAS_MLP_Handle * mlp = GNUNET_malloc (sizeof (struct GAS_MLP_Handle));
1612 unsigned long long tmp;
1619 struct GNUNET_TIME_Relative max_duration;
1620 long long unsigned int max_iterations;
1622 /* Init GLPK environment */
1623 int res = glp_init_env();
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1627 "initialization successful");
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1631 "environment is already initialized");
1634 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1635 "initialization failed (insufficient memory)");
1640 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1641 "initialization failed (unsupported programming model)");
1650 mlp->pv.BIG_M = (double) BIG_M_VALUE;
1652 /* Get timeout for iterations */
1653 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(cfg, "ats", "MLP_MAX_DURATION", &max_duration))
1655 max_duration = MLP_MAX_EXEC_DURATION;
1658 /* Get maximum number of iterations */
1659 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "MLP_MAX_ITERATIONS", &max_iterations))
1661 max_iterations = MLP_MAX_ITERATIONS;
1664 /* Get diversity coefficient from configuration */
1665 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1666 "MLP_COEFFICIENT_D",
1668 D = (double) tmp / 100;
1672 /* Get proportionality coefficient from configuration */
1673 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1674 "MLP_COEFFICIENT_R",
1676 R = (double) tmp / 100;
1680 /* Get utilization coefficient from configuration */
1681 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1682 "MLP_COEFFICIENT_U",
1684 U = (double) tmp / 100;
1688 /* Get quality metric coefficients from configuration */
1689 int i_delay = MLP_NaN;
1690 int i_distance = MLP_NaN;
1691 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
1692 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1694 /* initialize quality coefficients with default value 1.0 */
1695 mlp->pv.co_Q[c] = DEFAULT_QUALITY;
1697 mlp->pv.q[c] = q[c];
1698 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
1700 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
1704 if ((i_delay != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1705 "MLP_COEFFICIENT_QUALITY_DELAY",
1708 mlp->pv.co_Q[i_delay] = (double) tmp / 100;
1710 mlp->pv.co_Q[i_delay] = DEFAULT_QUALITY;
1712 if ((i_distance != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1713 "MLP_COEFFICIENT_QUALITY_DISTANCE",
1715 mlp->pv.co_Q[i_distance] = (double) tmp / 100;
1717 mlp->pv.co_Q[i_distance] = DEFAULT_QUALITY;
1719 /* Get minimum bandwidth per used address from configuration */
1720 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1721 "MLP_MIN_BANDWIDTH",
1726 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1729 /* Get minimum number of connections from configuration */
1730 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1731 "MLP_MIN_CONNECTIONS",
1735 n_min = DEFAULT_MIN_CONNECTIONS;
1737 /* Init network quotas */
1738 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1739 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1742 for (c2 = 0; c2 < dest_length; c2++)
1744 if (quotas[c] == network[c2])
1746 mlp->pv.quota_index[c] = network[c2];
1747 mlp->pv.quota_out[c] = out_dest[c2];
1748 mlp->pv.quota_in[c] = in_dest[c2];
1750 LOG (GNUNET_ERROR_TYPE_DEBUG, "Quota for network `%s' (in/out) %llu/%llu\n",
1751 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1752 mlp->pv.quota_out[c],
1753 mlp->pv.quota_in[c]);
1758 /* Check if defined quota could make problem unsolvable */
1759 if ((n_min * b_min) > mlp->pv.quota_out[c])
1761 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
1762 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1763 mlp->pv.quota_out[c],
1765 mlp->pv.quota_out[c] = (n_min * b_min);
1767 if ((n_min * b_min) > mlp->pv.quota_in[c])
1769 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
1770 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1771 mlp->pv.quota_in[c],
1773 mlp->pv.quota_in[c] = (n_min * b_min);
1776 /* Check if bandwidth is too big to make problem solvable */
1777 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
1779 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
1780 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1781 mlp->pv.quota_out[c],
1783 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
1785 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
1787 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
1788 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1789 mlp->pv.quota_in[c],
1791 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
1794 if (GNUNET_NO == found)
1796 mlp->pv.quota_in[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1797 mlp->pv.quota_out[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1798 LOG (GNUNET_ERROR_TYPE_INFO, _("Using default quota configuration for network `%s' (in/out) %llu/%llu\n"),
1799 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1800 mlp->pv.quota_in[c],
1801 mlp->pv.quota_out[c]);
1805 /* Assign options to handle */
1806 mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1807 mlp->bw_changed_cb = bw_changed_cb;
1808 mlp->bw_changed_cb_cls = bw_changed_cb_cls;
1809 /* Setting MLP Input variables */
1813 mlp->pv.b_min = b_min;
1814 mlp->pv.n_min = n_min;
1815 mlp->pv.m_q = GNUNET_ATS_QualityPropertiesCount;
1816 mlp->write_mip_mps = GNUNET_NO;
1817 mlp->write_mip_sol = GNUNET_NO;
1818 mlp->mlp_prob_changed = GNUNET_NO;
1819 mlp->mlp_prob_updated = GNUNET_NO;
1820 mlp->mlp_auto_solve = GNUNET_YES;
1821 mlp->peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1824 /* Redirect GLPK output to GNUnet logging */
1825 glp_term_hook (&mlp_term_hook, (void *) mlp);
1827 /* Init LP solving parameters */
1828 glp_init_smcp(&mlp->control_param_lp);
1829 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
1831 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
1833 mlp->control_param_lp.it_lim = max_iterations;
1834 mlp->control_param_lp.tm_lim = max_duration.rel_value;
1836 /* Init MLP solving parameters */
1837 glp_init_iocp(&mlp->control_param_mlp);
1838 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
1840 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
1842 mlp->control_param_mlp.tm_lim = max_duration.rel_value;
1844 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
1849 /* end of gnunet-service-ats_addresses_mlp.c */