2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/gnunet-service-ats-solver_mlp.c
23 * @brief ats mlp problem solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-ats-solver_mlp.h"
30 #include "gnunet_statistics_service.h"
35 * NOTE: Do not modify this documentation. This documentation is based on
36 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
37 * use build_txt.sh to generate plaintext output
39 * The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
40 * optimizing an mixed integer programming problem. The MLP solver uses a
41 * number of constraints to find the best adddress for a peer and an optimal
42 * bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
45 * We defined a constraint system to find an optimal bandwidth assignment.
46 * This constraint system uses as an input data addresses, bandwidth quotas,
47 * preferences and quality values. This constraint system is stored in an
48 * matrix based equotation system.
52 * A (M)LP problem consists of a target function to optimizes, constraints
53 * and rows and columns. FIXME GLP uses three arrays to index the matrix: two
54 * integer arrays storing the row and column indices in the matrix and an
55 * float array to store the coeeficient.
57 * To solve the problem we first find an initial solution for the LP problem
58 * using the LP solver and then find an MLP solution based on this solution
59 * using the MLP solver.
61 * Solving (M)LP problems has the property that finding an initial solution
62 * for the LP problem is computationally expensive and finding the MLP
63 * solution is cheaper. This is especially interesting an existing LP
64 * solution can be reused if only coefficients in the matrix have changed
65 * (addresses updated). Only when the problem size changes (addresses added
66 * or deleted) a new LP solution has to be found.
69 * The mlp solver solves the bandwidth assignment problem only on demand when
70 * an address suggestion is requested. When an address is requested mlp the
71 * solves the mlp problem and if the active address or the bandwidth assigned
72 * changes it calls the callback to addresses. The mlp solver gets notified
73 * about new addresses (adding sessions), removed addresses (address
74 * deletions) and address updates. To benefit from the mlp properties
75 * mentioned in section 5 the solver rembers if since the last solution
76 * addresses were added or deleted (problem size changed, problem has to be
77 * rebuild and solved from sratch) or if addresses were updated and the
78 * existing solution can be reused.
82 * The quotas for each network segment are passed by addresses. MLP can be
83 * adapted using configuration settings and uses the following parameters:
85 * Maximum duration for a MLP solution procees (default: 3 sec.)
87 * Maximum number of iterations for a MLP solution process (default:
89 * * MLP_MIN_CONNECTIONS:
90 * Minimum number of desired connections (default: 4)
91 * * MLP_MIN_BANDWIDTH:
92 * Minimum amount of bandwidth assigned to an address (default: 1024)
93 * * MLP_COEFFICIENT_D:
94 * Diversity coefficient (default: 1.0)
95 * * MLP_COEFFICIENT_R:
96 * Relativity coefficient (default: 1.0)
97 * * MLP_COEFFICIENT_U:
98 * Utilization coefficient (default: 1.0)
99 * * MLP_COEFFICIENT_D:
100 * Diversity coefficient (default: 1.0)
101 * * MLP_COEFFICIENT_QUALITY_DELAY:
102 * Quality delay coefficient (default: 1.0)
103 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
104 * Quality distance coefficient (default: 1.0)
105 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
106 * Quality distance coefficient (default: 1.0)
107 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
108 * Quality distance coefficient (default: 1.0)
109 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
110 * Quality distance coefficient (default: 1.0)
112 * 5.2 Data structures used
114 * mlp has for each known peer a struct ATS_Peer containing information about
115 * a specific peer. The address field solver_information contains information
116 * about the mlp properties of this address.
120 * During initialization mlp initializes the GLPK libray used to solve the
121 * MLP problem: it initializes the glpk environment and creates an initial LP
122 * problem. Next it loads the configuration values from the configuration or
123 * uses the default values configured in -addresses_mlp.h. The quotas used
124 * are given by addresses but may have to be adjusted. mlp uses a upper limit
125 * for the bandwidth assigned called BIG M and a minimum amount of bandwidth
126 * an address gets assigned as well as a minium desired number of
127 * connections. If the configured quota is bigger than BIG M, it is reduced
128 * to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
129 * *MLP_MIN_BANDWIDTH it is increased to this value.
135 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
138 * Print debug output for mlp problem creation
140 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
143 * Enable GLPK verbose output
145 #define VERBOSE_GLPK GNUNET_NO
148 * Maximize bandwidth assigned
150 * This option can be used to test if problem can be solved at all without
151 * optimizing for utility, diversity or relativity
154 #define MAXIMIZE_FOR_BANDWIDTH_ASSIGNED GNUNET_NO
157 * Intercept GLPK terminal output
158 * @param info the mlp handle
159 * @param s the string to print
160 * @return 0: glpk prints output on terminal, 0 != surpress output
163 mlp_term_hook (void *info, const char *s)
165 /* Not needed atm struct MLP_information *mlp = info; */
166 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s", s);
172 * Reset peers for next problem creation
174 * @param cls not used
176 * @param value ATS_Peer
180 reset_peers (void *cls, const struct GNUNET_HashCode * key, void *value)
182 struct ATS_Peer *peer = value;
183 peer->processed = GNUNET_NO;
188 * Delete the MLP problem and free the constrain matrix
190 * @param mlp the MLP handle
193 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
198 if (mlp->p.prob != NULL)
200 glp_delete_prob(mlp->p.prob);
204 /* delete row index */
205 if (mlp->p.ia != NULL)
207 GNUNET_free (mlp->p.ia);
211 /* delete column index */
212 if (mlp->p.ja != NULL)
214 GNUNET_free (mlp->p.ja);
218 /* delete coefficients */
219 if (mlp->p.ar != NULL)
221 GNUNET_free (mlp->p.ar);
227 mlp->p.c_d = MLP_UNDEFINED;
228 mlp->p.c_r = MLP_UNDEFINED;
229 mlp->p.r_c2 = MLP_UNDEFINED;
230 mlp->p.r_c4 = MLP_UNDEFINED;
231 mlp->p.r_c6 = MLP_UNDEFINED;
232 mlp->p.r_c9 = MLP_UNDEFINED;
233 for (c = 0; c < mlp->pv.m_q ; c ++)
234 mlp->p.r_q[c] = MLP_UNDEFINED;
235 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c ++)
236 mlp->p.r_quota[c] = MLP_UNDEFINED;
237 mlp->p.ci = MLP_UNDEFINED;
240 GNUNET_CONTAINER_multihashmap_iterate (mlp->peers, &reset_peers, NULL);
245 * Translate ATS properties to text
246 * Just intended for debugging
248 * @param ats_index the ATS index
249 * @return string with result
252 mlp_ats_to_string (int ats_index)
255 case GNUNET_ATS_ARRAY_TERMINATOR:
256 return "GNUNET_ATS_ARRAY_TERMINATOR";
257 case GNUNET_ATS_UTILIZATION_UP:
258 return "GNUNET_ATS_UTILIZATION_UP";
259 case GNUNET_ATS_UTILIZATION_DOWN:
260 return "GNUNET_ATS_UTILIZATION_DOWN";
261 case GNUNET_ATS_COST_LAN:
262 return "GNUNET_ATS_COST_LAN";
263 case GNUNET_ATS_COST_WAN:
264 return "GNUNET_ATS_COST_LAN";
265 case GNUNET_ATS_COST_WLAN:
266 return "GNUNET_ATS_COST_WLAN";
267 case GNUNET_ATS_NETWORK_TYPE:
268 return "GNUNET_ATS_NETWORK_TYPE";
269 case GNUNET_ATS_QUALITY_NET_DELAY:
270 return "GNUNET_ATS_QUALITY_NET_DELAY";
271 case GNUNET_ATS_QUALITY_NET_DISTANCE:
272 return "GNUNET_ATS_QUALITY_NET_DISTANCE";
280 * Translate glpk status error codes to text
281 * @param retcode return code
282 * @return string with result
285 mlp_status_to_string (int retcode)
289 return "solution is undefined";
291 return "solution is feasible";
293 return "solution is infeasible";
295 return "no feasible solution exists";
297 return "solution is optimal";
299 return "solution is unbounded";
302 return "unknown error";
307 * Translate glpk solver error codes to text
308 * @param retcode return code
309 * @return string with result
312 mlp_solve_to_string (int retcode)
318 return "invalid basis";
320 return "singular matrix";
322 return "ill-conditioned matrix";
324 return "invalid bounds";
326 return "solver failed";
328 return "objective lower limit reached";
330 return "objective upper limit reached";
332 return "iteration limit exceeded";
334 return "time limit exceeded";
336 return "no primal feasible solution";
338 return "no dual feasible solution";
340 return "root LP optimum not provided";
342 return "search terminated by application";
344 return "relative mip gap tolerance reached";
346 return "no dual feasible solution";
348 return "no convergence";
350 return "numerical instability";
352 return "invalid data";
354 return "result out of range";
357 return "unknown error";
362 * Extract an ATS performance info from an address
364 * @param address the address
365 * @param type the type to extract in HBO
366 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
369 get_performance_info (struct ATS_Address *address, uint32_t type)
372 GNUNET_assert (NULL != address);
374 if ((NULL == address->atsi) || (0 == address->atsi_count))
375 return GNUNET_ATS_VALUE_UNDEFINED;
377 for (c1 = 0; c1 < address->atsi_count; c1++)
379 if (ntohl(address->atsi[c1].type) == type)
380 return ntohl(address->atsi[c1].value);
382 return GNUNET_ATS_VALUE_UNDEFINED;
388 struct GNUNET_CONTAINER_MultiHashMap * peers;
393 mlp_create_problem_count_addresses_it (void *cls, const struct GNUNET_HashCode *key, void *value)
395 struct CountContext *cctx = cls;
396 /* Check if we have to add this peer due to a pending request */
397 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(cctx->peers, key))
402 static int mlp_create_problem_count_addresses (
403 struct GNUNET_CONTAINER_MultiHashMap * peers,
404 struct GNUNET_CONTAINER_MultiHashMap * addresses)
406 struct CountContext cctx;
409 GNUNET_CONTAINER_multihashmap_iterate (addresses, &mlp_create_problem_count_addresses_it, &cctx);
416 mlp_create_problem_set_value (struct MLP_Problem *p,
417 int row, int col, double val,
420 if ((p->ci) >= p->num_elements)
422 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
423 line, p->ci + 1, p->num_elements);
427 if ((0 == row) || (0 == col))
432 #if DEBUG_MLP_PROBLEM_CREATION
433 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u == %.2f\n",
434 line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
440 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
441 unsigned int type, unsigned int bound, double lb, double ub,
444 int col = glp_add_cols (p->prob, 1);
445 glp_set_col_name (p->prob, col, name);
446 glp_set_col_bnds (p->prob, col, bound, lb, ub);
447 glp_set_col_kind (p->prob, col, type);
448 glp_set_obj_coef (p->prob, col, coef);
449 #if DEBUG_MLP_PROBLEM_CREATION
450 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
457 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
458 unsigned int bound, double lb, double ub)
461 int row = glp_add_rows (p->prob, 1);
463 glp_set_row_name (p->prob, row, name);
464 /* set row bounds: <= 0 */
465 glp_set_row_bnds (p->prob, row, bound, lb, ub);
468 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
471 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
474 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
477 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
480 GNUNET_asprintf(&op, "ERROR");
483 #if DEBUG_MLP_PROBLEM_CREATION
484 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
493 * - address columns b and n
494 * - address dependent constraint rows c1, c3
495 * - peer dependent rows c2 and c9
496 * - Set address dependent entries in problem matrix as well
499 mlp_create_problem_add_address_information (void *cls, const struct GNUNET_HashCode *key, void *value)
501 struct GAS_MLP_Handle *mlp = cls;
502 struct MLP_Problem *p = &mlp->p;
503 struct ATS_Address *address = value;
504 struct ATS_Peer *peer;
505 struct MLP_information *mlpi;
510 /* Check if we have to add this peer due to a pending request */
511 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->peers, key))
514 mlpi = address->solver_information;
522 peer = GNUNET_CONTAINER_multihashmap_get (mlp->peers, key);
523 if (peer->processed == GNUNET_NO)
525 /* Add peer dependent constraints */
526 /* Add constraint c2 */
527 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
528 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
530 /* Add constraint c9 */
531 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
532 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
534 /* c 9) set coefficient */
535 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f, __LINE__);
537 peer->processed = GNUNET_YES;
540 /* Reset addresses' solver information */
546 for (c = 0; c < mlp->pv.m_q; c++)
549 /* Add bandwidth column */
550 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
551 #if TEST_MAX_BW_ASSIGNMENT
552 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
554 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
559 /* Add usage column */
560 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
561 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
564 /* Add address dependent constraints */
565 /* Add constraint c1) bandwidth capping
566 * b_t + (-M) * n_t <= 0
568 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
569 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
572 /* c1) set b = 1 coefficient */
573 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
574 /* c1) set n = -M coefficient */
575 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -mlp->pv.BIG_M, __LINE__);
577 /* Add constraint c 3) minimum bandwidth
578 * b_t + (-n_t * b_min) >= 0
580 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
581 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
584 /* c3) set b = 1 coefficient */
585 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
586 /* c3) set n = -b_min coefficient */
587 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
590 /* Set coefficient entries in invariant rows */
591 /* c 4) minimum connections */
592 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
593 /* c 6) maximize diversity */
594 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
595 /* c 2) 1 address peer peer */
596 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
597 /* c 9) relativity */
598 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
600 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
602 /* c 10) obey network specific quotas
603 * (1)*b_1 + ... + (1)*b_m <= quota_n
605 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
607 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
608 if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
609 addr_net = GNUNET_ATS_NET_UNSPECIFIED;
611 if (mlp->pv.quota_index[c] == addr_net)
613 mlp_create_problem_set_value (p, p->r_quota[c], mlpi->c_b, 1, __LINE__);
618 /* c 7) Optimize quality */
619 /* For all quality metrics, set quality of this address */
620 for (c = 0; c < mlp->pv.m_q; c++)
623 mlp_create_problem_set_value (p, p->r_q[c], mlpi->c_b, mlpi->q_averaged[c], __LINE__);
630 * Create the invariant columns c4, c6, c10, c8, c7
633 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
638 /* Row for c4) minimum connection */
639 /* Number of minimum connections is min(|Peers|, n_min) */
640 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);
642 /* Add row for c6) */
643 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
645 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
647 /* Add rows for c 10) */
648 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
651 GNUNET_asprintf(&text, "c10_quota_ats_%s", GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
652 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
656 /* Adding rows for c 8) */
657 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
659 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
661 /* c 7) For all quality metrics */
662 for (c = 0; c < mlp->pv.m_q; c++)
664 GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->pv.q[c]));
665 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
667 mlp_create_problem_set_value (p, p->r_q[c], p->c_q[c], -1, __LINE__);
673 * Create the invariant columns d, u, r, q0 ... qm
676 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
681 #if TEST_MAX_BW_ASSIGNMENT
686 //mlp->pv.co_R = 0.0;
688 /* Diversity d column */
689 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
691 /* Utilization u column */
692 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
694 /* Relativity r column */
695 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
697 /* Quality metric columns */
698 for (c = 0; c < mlp->pv.m_q; c++)
700 GNUNET_asprintf (&name, "q_%u", mlp->pv.q[c]);
701 #if TEST_MAX_BW_ASSIGNMENT
702 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
704 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
712 * Create the MLP problem
714 * @param mlp the MLP handle
715 * @param addresses the hashmap containing all adresses
716 * @return GNUNET_OK or GNUNET_SYSERR
719 mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
721 struct MLP_Problem *p = &mlp->p;
724 GNUNET_assert (p->prob == NULL);
725 GNUNET_assert (p->ia == NULL);
726 GNUNET_assert (p->ja == NULL);
727 GNUNET_assert (p->ar == NULL);
728 /* Reset MLP problem struct */
730 /* create the glpk problem */
731 p->prob = glp_create_prob ();
732 GNUNET_assert (NULL != p->prob);
733 p->num_peers = GNUNET_CONTAINER_multihashmap_size (mlp->peers);
734 p->num_addresses = mlp_create_problem_count_addresses (mlp->peers, addresses);
736 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
737 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses + mlp->pv.m_q + p->num_peers + 2 + 1);
738 LOG (GNUNET_ERROR_TYPE_DEBUG, "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
739 p->num_peers, p->num_addresses, mlp->pv.m_q, p->num_elements);
741 /* Set a problem name */
742 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
743 /* Set optimization direction to maximize */
744 glp_set_obj_dir (p->prob, GLP_MAX);
746 /* Create problem matrix */
747 /* last +1 caused by glpk index starting with one: [1..elements]*/
750 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
752 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
754 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
756 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
758 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
759 return GNUNET_SYSERR;
762 /* Adding invariant columns */
763 mlp_create_problem_add_invariant_columns (mlp, p);
765 /* Adding address independent constraint rows */
766 mlp_create_problem_add_invariant_rows (mlp, p);
768 /* Adding address dependent columns constraint rows */
769 GNUNET_CONTAINER_multihashmap_iterate (addresses, &mlp_create_problem_add_address_information, mlp);
771 /* Load the matrix */
772 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
773 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
779 * Solves the LP problem
781 * @param mlp the MLP Handle
782 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
785 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
789 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
791 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
793 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
795 /* Analyze problem status */
796 res = glp_get_status (mlp->p.prob);
798 /* solution is optimal */
800 /* solution is feasible */
802 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: 0x%02X %s\n",
803 res, mlp_status_to_string(res));
805 /* Problem was ill-defined, no way to handle that */
807 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving LP problem failed, no solution: 0x%02X %s\n",
808 res, mlp_status_to_string(res));
809 return GNUNET_SYSERR;
815 * Solves the MLP problem
817 * @param mlp the MLP Handle
818 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
821 mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
824 res = glp_intopt(mlp->p.prob, &mlp->control_param_mlp);
826 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_solve_to_string(res));
828 LOG (GNUNET_ERROR_TYPE_WARNING, "Solving MLP problem failed: 0x%02X %s\n", res, mlp_solve_to_string(res));
829 /* Analyze problem status */
830 res = glp_mip_status(mlp->p.prob);
832 /* solution is optimal */
834 /* solution is feasible */
836 LOG (GNUNET_ERROR_TYPE_DEBUG, "Solving MLP problem: 0x%02X %s\n", res, mlp_status_to_string(res));
838 /* Problem was ill-defined, no way to handle that */
840 LOG (GNUNET_ERROR_TYPE_WARNING,"Solving MLP problem failed, 0x%02X %s\n\n", res, mlp_status_to_string(res));
841 return GNUNET_SYSERR;
847 * Propagates the results when MLP problem was solved
849 * @param cls the MLP handle
850 * @param key the peer identity
851 * @param value the address
852 * @return GNUNET_OK to continue
855 mlp_propagate_results (void *cls, const struct GNUNET_HashCode *key, void *value)
857 struct GAS_MLP_Handle *mlp = cls;
858 struct ATS_Address *address;
859 struct MLP_information *mlpi;
860 double mlp_bw_in = MLP_NaN;
861 double mlp_bw_out = MLP_NaN;
862 double mlp_use = MLP_NaN;
864 /* Check if we have to add this peer due to a pending request */
865 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(mlp->peers, key))
868 GNUNET_assert (address->solver_information != NULL);
869 mlpi = address->solver_information;
871 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
872 if (mlp_bw_in > (double) UINT32_MAX)
874 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
875 mlp_bw_in = (double) UINT32_MAX;
877 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
878 if (mlp_bw_out > (double) UINT32_MAX)
880 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
881 mlp_bw_out = (double) UINT32_MAX;
883 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
887 if ((GLP_YES == mlp_use) && (GNUNET_NO == address->active))
889 /* Address switch: Activate address*/
890 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
891 address->active = GNUNET_YES;
892 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
893 mlpi->b_in.value__ = htonl(mlp_bw_in);
894 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
895 mlpi->b_out.value__ = htonl(mlp_bw_out);
897 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
899 else if ((GLP_NO == mlp_use) && (GNUNET_YES == address->active))
901 /* Address switch: Disable address*/
902 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
903 address->active = GNUNET_NO;
904 /* Set bandwidth to 0 */
905 address->assigned_bw_in.value__ = htonl (0);
906 mlpi->b_in.value__ = htonl(mlp_bw_in);
907 address->assigned_bw_out.value__ = htonl (0);
908 mlpi->b_out.value__ = htonl(mlp_bw_out);
910 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
912 else if ((mlp_bw_out != ntohl(address->assigned_bw_out.value__)) ||
913 (mlp_bw_in != ntohl(address->assigned_bw_in.value__)))
915 /* Bandwidth changed */
916 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
917 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
918 mlpi->b_in.value__ = htonl(mlp_bw_in);
919 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
920 mlpi->b_out.value__ = htonl(mlp_bw_out);
922 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
926 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n", (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
935 * Solves the MLP problem
937 * @param solver the MLP Handle
938 * @param addresses the address hashmap
939 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
942 GAS_mlp_solve_problem (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses)
944 struct GAS_MLP_Handle *mlp = solver;
948 struct GNUNET_TIME_Absolute start_build;
949 struct GNUNET_TIME_Relative duration_build;
950 struct GNUNET_TIME_Absolute start_lp;
951 struct GNUNET_TIME_Relative duration_lp;
952 struct GNUNET_TIME_Absolute start_mlp;
953 struct GNUNET_TIME_Relative duration_mlp;
954 GNUNET_assert (NULL != solver);
956 if (GNUNET_YES == mlp->bulk_lock)
958 mlp->bulk_request ++;
962 if ((GNUNET_NO == mlp->mlp_prob_changed) && (GNUNET_NO == mlp->mlp_prob_updated))
964 LOG (GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
967 mlp->addresses = addresses;
968 if (GNUNET_YES == mlp->mlp_prob_changed)
970 LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
971 mlp_delete_problem (mlp);
972 start_build = GNUNET_TIME_absolute_get();
973 if (GNUNET_SYSERR == mlp_create_problem (mlp, addresses))
974 return GNUNET_SYSERR;
975 duration_build = GNUNET_TIME_absolute_get_duration (start_build);
976 mlp->control_param_lp.presolve = GLP_YES;
977 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
981 LOG (GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
982 duration_build.rel_value = 0;
986 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running LP solver %s\n", (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
987 start_lp = GNUNET_TIME_absolute_get();
988 res_lp = mlp_solve_lp_problem (mlp);
989 duration_lp = GNUNET_TIME_absolute_get_duration (start_lp);
993 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
994 start_mlp = GNUNET_TIME_absolute_get();
995 res_mip = mlp_solve_mlp_problem (mlp);
997 duration_mlp = GNUNET_TIME_absolute_get_duration (start_mlp);
1000 mlp->ps.lp_res = res_lp;
1001 mlp->ps.mip_res = res_mip;
1002 mlp->ps.build_dur = duration_build;
1003 mlp->ps.lp_dur = duration_lp;
1004 mlp->ps.mip_dur = duration_mlp;
1005 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1006 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1007 mlp->ps.p_cols = glp_get_num_cols (mlp->p.prob);
1008 mlp->ps.p_rows = glp_get_num_rows (mlp->p.prob);
1009 mlp->ps.p_elements = mlp->p.num_elements;
1011 LOG (GNUNET_ERROR_TYPE_DEBUG, "Execution time: Build %llu ms, LP %llu ms, MLP %llu ms\n",
1012 (unsigned long long) duration_build.rel_value,
1013 (unsigned long long) duration_lp.rel_value,
1014 (unsigned long long) duration_mlp.rel_value);
1016 /* Propagate result*/
1017 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1018 GNUNET_CONTAINER_multihashmap_iterate (addresses, &mlp_propagate_results, mlp);
1020 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1021 if (GNUNET_YES == mlp->write_mip_mps)
1023 /* Write problem and solution to disk */
1024 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value);
1025 glp_write_mps(mlp->p.prob, GLP_MPS_FILE, NULL, filename);
1026 GNUNET_free (filename);
1028 if (GNUNET_YES == mlp->write_mip_sol)
1030 GNUNET_asprintf (&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers, mlp->p.num_addresses, time.abs_value);
1031 glp_print_mip (mlp->p.prob, filename );
1032 GNUNET_free (filename);
1035 /* Reset change and update marker */
1036 mlp->control_param_lp.presolve = GLP_NO;
1037 mlp->mlp_prob_updated = GNUNET_NO;
1038 mlp->mlp_prob_changed = GNUNET_NO;
1040 if ((GNUNET_OK == res_lp) && (GNUNET_OK == res_mip))
1043 return GNUNET_SYSERR;
1047 * Add a single address to the solve
1049 * @param solver the solver Handle
1050 * @param addresses the address hashmap containing all addresses
1051 * @param address the address to add
1052 * @param network network type of this address
1055 GAS_mlp_address_add (void *solver,
1056 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1057 struct ATS_Address *address,
1060 struct GAS_MLP_Handle *mlp = solver;
1062 struct MLP_information *mlpi;
1066 GNUNET_assert (NULL != solver);
1067 GNUNET_assert (NULL != addresses);
1068 GNUNET_assert (NULL != address);
1070 mlp->addresses = addresses;
1071 if (NULL == address->solver_information)
1073 address->solver_information = GNUNET_malloc (sizeof (struct MLP_information));
1074 mlpi = address->solver_information;
1075 for (c1 = 0; c1 < mlp->pv.m_q; c1++)
1077 mlpi->q_averaged[c1] = DEFAULT_QUALITY;
1078 for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
1079 mlpi->q[c1][c2] = MLP_NaN;
1083 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding address for peer `%s' multiple times\n"), GNUNET_i2s(&address->peer));
1085 /* Is this peer included in the problem? */
1086 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &address->peer.hashPubKey)))
1088 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1092 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1093 /* Problem size changed: new address for peer with pending request */
1094 mlp->mlp_prob_changed = GNUNET_YES;
1095 if (GNUNET_YES == mlp->mlp_auto_solve)
1096 GAS_mlp_solve_problem (solver, addresses);
1101 mlp_update_quality (struct GAS_MLP_Handle *mlp,
1102 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1103 struct ATS_Address * address,
1104 const struct GNUNET_ATS_Information *ats_prev, uint32_t ats_prev_count)
1106 struct MLP_information *mlpi = address->solver_information;
1107 unsigned int c_ats_entry;
1108 unsigned int c_queue_entries;
1110 unsigned int c_queue_it;
1112 unsigned int c_qual;
1118 uint32_t prev_value;
1127 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating %u quality metrics for peer `%s'\n",
1128 ats_prev_count, GNUNET_i2s (&address->peer));
1130 GNUNET_assert (NULL != mlp);
1131 GNUNET_assert (NULL != address);
1132 GNUNET_assert (NULL != address->solver_information);
1133 GNUNET_assert (NULL != ats_prev);
1135 if (NULL == mlp->p.prob)
1138 qual_changed = GNUNET_NO;
1139 for (c_ats_entry = 0; c_ats_entry < ats_prev_count; c_ats_entry++)
1141 type = ntohl (ats_prev[c_ats_entry].type);
1142 prev_value = ntohl (ats_prev[c_ats_entry].value);
1146 /* Check for network update */
1147 if (type == GNUNET_ATS_NETWORK_TYPE)
1149 new_value = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1150 if (GNUNET_ATS_VALUE_UNDEFINED == new_value)
1151 new_value = GNUNET_ATS_NET_UNSPECIFIED;
1152 if (new_value != prev_value)
1154 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating network for peer `%s' from `%s' to `%s'\n",
1155 GNUNET_i2s (&address->peer),
1156 GNUNET_ATS_print_network_type(prev_value),
1157 GNUNET_ATS_print_network_type(new_value));
1160 if (mlpi->c_b == MLP_UNDEFINED)
1161 continue; /* This address is not yet in the matrix*/
1163 rows = glp_get_num_rows(mlp->p.prob);
1164 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1165 val = GNUNET_malloc (rows * sizeof (double) + 1);
1166 int length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1168 for (c_net = 0; c_net <= length + 1; c_net ++)
1170 if (ind[c_net] == mlp->p.r_quota[prev_value])
1171 break; /* Found index for old network */
1174 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1175 /* Set updated column */
1176 ind[c_net] = mlp->p.r_quota[new_value];
1178 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1182 rows = glp_get_num_rows(mlp->p.prob);
1183 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1184 val = GNUNET_malloc (rows * sizeof (double) + 1);
1185 length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1187 for (c_net = 0; c_net <= length + 1; c_net ++)
1189 if (ind[c_net] == mlp->p.r_quota[prev_value])
1190 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing old network index [%u] == [%f]\n",ind[c_net],val[c_net]);
1191 if (ind[c_net] == mlp->p.r_quota[new_value])
1193 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting new network index [%u] == [%f]\n",ind[c_net],val[c_net]);
1199 mlp->mlp_prob_changed = GNUNET_YES;
1204 /* Find index for this ATS type */
1205 for (c_cmp = 0; c_cmp < mlp->pv.m_q; c_cmp++)
1207 if (type == mlp->pv.q[c_cmp])
1213 if (-1 == type_index)
1214 continue; /* quality index not found */
1216 /* Get average queue index */
1217 avg_index = mlpi->q_avg_i[type_index];
1219 /* Update averaging queue */
1220 new_value = get_performance_info (address, type);
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s': `%s' from %u to %u\n",
1222 GNUNET_i2s (&address->peer),
1223 mlp_ats_to_string(mlp->pv.q[type_index]), prev_value, new_value);
1224 GNUNET_assert (GNUNET_ATS_VALUE_UNDEFINED != new_value);
1225 mlpi->q[type_index][avg_index] = new_value;
1227 /* Update averaging index */
1228 if (mlpi->q_avg_i[type_index] + 1 < (MLP_AVERAGING_QUEUE_LENGTH))
1229 mlpi->q_avg_i[type_index] ++;
1231 mlpi->q_avg_i[type_index] = 0;
1233 /* Update average depending on ATS type */
1236 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1237 case GNUNET_ATS_QUALITY_NET_DELAY:
1238 c_queue_entries = 0;
1240 for (c_queue_it = 0; c_queue_it < MLP_AVERAGING_QUEUE_LENGTH; c_queue_it++)
1242 if (mlpi->q[type_index][c_queue_it] != MLP_NaN)
1244 queue = mlpi->q[type_index] ;
1245 avg += queue[c_queue_it];
1249 if ((c_queue_entries > 0) && (avg > 0))
1250 /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
1251 mlpi->q_averaged[type_index] = (double) c_queue_entries / avg;
1253 mlpi->q_averaged[type_index] = 0.0;
1255 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s': `%s' average sum of %u elements == %f, average == %f, weight == %f\n",
1256 GNUNET_i2s (&address->peer),
1257 mlp_ats_to_string(mlp->pv.q[type_index]),
1260 avg / (double) c_queue_entries,
1261 mlpi->q_averaged[type_index]);
1262 qual_changed = GNUNET_YES;
1266 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Update for ATS type `%s' not implemented!\n"),
1267 mlp_ats_to_string(type));
1271 /* Changed, but quality will be automatically set during rebuild */
1272 if ((GNUNET_YES == mlp->mlp_prob_changed) &&
1273 (GNUNET_YES == mlp->mlp_auto_solve))
1275 GAS_mlp_solve_problem (mlp, addresses);
1279 /* Update problem matrix if required */
1280 if (GNUNET_NO == qual_changed)
1283 /* Address not yet included in matrix */
1284 if (MLP_UNDEFINED == mlpi->c_b)
1287 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index]
1288 * Get column mlpi->c_b */
1289 rows = glp_get_num_rows(mlp->p.prob);
1290 ind = GNUNET_malloc (rows * sizeof (int) + 1);
1291 val = GNUNET_malloc (rows * sizeof (double) + 1);
1292 int length = glp_get_mat_col (mlp->p.prob, mlpi->c_b, ind, val);
1294 for (c_qual = 0; c_qual < mlp->pv.m_q; c_qual++)
1296 for (c_row = 0; c_row <= length; c_row ++)
1298 if (ind[c_row] == mlp->p.r_q[c_qual])
1299 val[c_row] = mlpi->q_averaged[c_qual];
1302 /* Set updated column */
1303 glp_set_mat_col (mlp->p.prob, mlpi->c_b, length, ind, val);
1306 mlp->mlp_prob_updated = GNUNET_YES;
1310 * Updates a single address in the MLP problem
1312 * If the address did not exist before in the problem:
1313 * The MLP problem has to be recreated and the problem has to be resolved
1315 * ATS performance information in address are already updated, delta + previous
1316 * values are included in atsi_prev (value GNUNET_ATS_VALUE_UNDEFINED if not existing before)
1318 * Otherwise the addresses' values can be updated and the existing base can
1321 * @param solver the solver Handle
1322 * @param addresses the address hashmap containing all addresses
1323 * @param address the update address
1324 * @param prev_session the new session (if changed otherwise current)
1325 * @param prev_in_use the new address in use state (if changed otherwise current)
1326 * @param prev_atsi ATS information updated + previous values, GNUNET_ATS_VALUE_UNDEFINED if not existing before
1327 * @param prev_atsi_count number of atsi values updated
1330 GAS_mlp_address_update (void *solver,
1331 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1332 struct ATS_Address *address,
1333 uint32_t prev_session,
1335 const struct GNUNET_ATS_Information *prev_atsi,
1336 uint32_t prev_atsi_count)
1339 struct GAS_MLP_Handle *mlp = solver;
1340 struct MLP_information *mlpi = address->solver_information;
1342 GNUNET_assert (NULL != solver);
1343 GNUNET_assert (NULL != addresses);
1344 GNUNET_assert (NULL != address);
1345 GNUNET_assert ((NULL != prev_atsi) || (0 == prev_atsi_count));
1347 mlp->addresses = addresses;
1350 LOG (GNUNET_ERROR_TYPE_ERROR, _("Updating address for peer `%s' not added before\n"), GNUNET_i2s(&address->peer));
1353 mlp_update_quality (mlp, addresses, address, prev_atsi, prev_atsi_count);
1355 /* Is this peer included in the problem? */
1356 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &address->peer.hashPubKey)))
1358 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1363 /* Problem size changed: new address for peer with pending request */
1364 mlp->mlp_prob_updated = GNUNET_YES;
1366 if (GNUNET_YES == mlp->mlp_auto_solve)
1367 GAS_mlp_solve_problem (solver, addresses);
1372 * Deletes a single address in the MLP problem
1374 * The MLP problem has to be recreated and the problem has to be resolved
1376 * @param solver the MLP Handle
1377 * @param addresses the address hashmap
1378 * the address has to be already removed from the hashmap
1379 * @param address the address to delete
1380 * @param session_only delete only session not whole address
1383 GAS_mlp_address_delete (void *solver,
1384 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1385 struct ATS_Address *address,
1389 struct GAS_MLP_Handle *mlp = solver;
1390 struct MLP_information *mlpi;
1392 GNUNET_assert (NULL != solver);
1393 GNUNET_assert (NULL != addresses);
1394 GNUNET_assert (NULL != address);
1396 mlpi = address->solver_information;
1397 mlp->addresses = addresses;
1401 address->solver_information = NULL;
1404 /* Is this peer included in the problem? */
1405 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &address->peer.hashPubKey)))
1407 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1412 /* Problem size changed: new address for peer with pending request */
1413 mlp->mlp_prob_changed = GNUNET_YES;
1414 if (GNUNET_YES == mlp->mlp_auto_solve)
1415 GAS_mlp_solve_problem (solver, addresses);
1421 * Find the active address in the set of addresses of a peer
1422 * @param cls destination
1423 * @param key peer id
1424 * @param value address
1428 mlp_get_preferred_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1431 struct ATS_Address *aa = (struct ATS_Address *) cls;
1432 struct ATS_Address *addr = value;
1433 struct MLP_information *mlpi = addr->solver_information;
1436 if (mlpi->n == GNUNET_YES)
1439 aa->assigned_bw_in = mlpi->b_in;
1440 aa->assigned_bw_out = mlpi->b_out;
1447 static double get_peer_pref_value (struct GAS_MLP_Handle *mlp, const struct GNUNET_PeerIdentity *peer)
1450 const double *preferences = NULL;
1452 preferences = mlp->get_preferences (mlp->get_preferences_cls, peer);
1455 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1457 if (c != GNUNET_ATS_PREFERENCE_END)
1459 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
1460 res += preferences[c];
1463 res /= (GNUNET_ATS_PreferenceCount -1);
1469 * Get the preferred address for a specific peer
1471 * @param solver the MLP Handle
1472 * @param addresses address hashmap
1473 * @param peer the peer
1474 * @return suggested address
1476 const struct ATS_Address *
1477 GAS_mlp_get_preferred_address (void *solver,
1478 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1479 const struct GNUNET_PeerIdentity *peer)
1481 struct GAS_MLP_Handle *mlp = solver;
1483 struct ATS_Address *res = NULL;
1485 GNUNET_assert (NULL != solver);
1486 GNUNET_assert (NULL != addresses);
1487 GNUNET_assert (NULL != peer);
1489 LOG (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n",
1491 mlp->addresses = addresses;
1492 /* Is this peer included in the problem? */
1493 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &peer->hashPubKey)))
1495 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding peer `%s' to list of peers with requests\n",
1498 p = GNUNET_malloc (sizeof (struct ATS_Peer));
1500 p->f = get_peer_pref_value (mlp, peer);
1501 GNUNET_CONTAINER_multihashmap_put (mlp->peers, &peer->hashPubKey, p, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1503 /* Added new peer, we have to rebuild problem before solving */
1504 mlp->mlp_prob_changed = GNUNET_YES;
1506 if (GNUNET_YES == mlp->mlp_auto_solve)
1507 GAS_mlp_solve_problem (mlp, addresses);
1509 /* Get prefered address */
1510 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
1511 mlp_get_preferred_address_it, res);
1518 * Start a bulk operation
1520 * @param solver the solver
1523 GAS_mlp_bulk_start (void *solver)
1525 LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1526 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1528 GNUNET_assert (NULL != solver);
1534 GAS_mlp_bulk_stop (void *solver)
1536 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1538 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1539 GNUNET_assert (NULL != solver);
1541 if (s->bulk_lock < 1)
1548 if (0 < s->bulk_request)
1550 GAS_mlp_solve_problem (solver, s->addresses);
1558 * Stop notifying about address and bandwidth changes for this peer
1560 * @param solver the MLP handle
1561 * @param addresses address hashmap
1562 * @param peer the peer
1565 GAS_mlp_stop_get_preferred_address (void *solver,
1566 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1567 const struct GNUNET_PeerIdentity *peer)
1569 struct GAS_MLP_Handle *mlp = solver;
1570 struct ATS_Peer *p = NULL;
1572 GNUNET_assert (NULL != solver);
1573 GNUNET_assert (NULL != addresses);
1574 GNUNET_assert (NULL != peer);
1576 if (NULL != (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &peer->hashPubKey)))
1578 GNUNET_CONTAINER_multihashmap_remove (mlp->peers, &peer->hashPubKey, p);
1585 * Changes the preferences for a peer in the MLP problem
1587 * @param solver the MLP Handle
1588 * @param addresses the address hashmap
1589 * @param peer the peer
1590 * @param kind the kind to change the preference
1591 * @param pref_rel the relative score
1594 GAS_mlp_address_change_preference (void *solver,
1595 struct GNUNET_CONTAINER_MultiHashMap *addresses,
1596 const struct GNUNET_PeerIdentity *peer,
1597 enum GNUNET_ATS_PreferenceKind kind,
1600 struct GAS_MLP_Handle *mlp = solver;
1601 struct ATS_Peer *p = NULL;
1603 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing preference for address for peer `%s'\n",
1606 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1607 mlp->addresses = addresses;
1608 /* Update the constraints with changed preferences */
1610 /* Update quality constraint c7 */
1612 /* Update relativity constraint c9 */
1613 if (NULL == (p = GNUNET_CONTAINER_multihashmap_get (mlp->peers, &peer->hashPubKey)))
1615 LOG (GNUNET_ERROR_TYPE_ERROR, "Updating preference for unknown peer `%s' \n", GNUNET_i2s(peer));
1618 p->f = get_peer_pref_value (mlp, peer);
1619 /* FXIME: cannot use set_value mlp_create_problem_set_value (&mlp->p, p->r_c9, mlp->p.c_r, -p->f, __LINE__);*/
1621 /* Problem size changed: new address for peer with pending request */
1622 mlp->mlp_prob_updated = GNUNET_YES;
1623 if (GNUNET_YES == mlp->mlp_auto_solve)
1624 GAS_mlp_solve_problem (solver, addresses);
1630 mlp_free_peers (void *cls, const struct GNUNET_HashCode *key, void *value)
1632 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
1633 struct ATS_Peer *p = value;
1635 GNUNET_CONTAINER_multihashmap_remove (map, key, value);
1643 * Shutdown the MLP problem solving component
1645 * @param solver the solver handle
1648 GAS_mlp_done (void *solver)
1650 struct GAS_MLP_Handle *mlp = solver;
1651 GNUNET_assert (mlp != NULL);
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down mlp solver\n");
1654 mlp_delete_problem (mlp);
1656 GNUNET_CONTAINER_multihashmap_iterate (mlp->peers, &mlp_free_peers, mlp->peers);
1657 GNUNET_CONTAINER_multihashmap_destroy (mlp->peers);
1660 /* Clean up GLPK environment */
1664 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutdown down of mlp solver complete\n");
1669 * Init the MLP problem solving component
1671 * @param cfg the GNUNET_CONFIGURATION_Handle handle
1672 * @param stats the GNUNET_STATISTICS handle
1673 * @param network array of GNUNET_ATS_NetworkType with length dest_length
1674 * @param out_dest array of outbound quotas
1675 * @param in_dest array of outbound quota
1676 * @param dest_length array length for quota arrays
1677 * @param bw_changed_cb callback for changed bandwidth amounts
1678 * @param bw_changed_cb_cls cls for callback
1679 * @param get_preference callback to get relative preferences for a peer
1680 * @param get_preference_cls cls for callback to get relative preferences
1681 * @return struct GAS_MLP_Handle on success, NULL on fail
1684 GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1685 const struct GNUNET_STATISTICS_Handle *stats,
1687 unsigned long long *out_dest,
1688 unsigned long long *in_dest,
1690 GAS_bandwidth_changed_cb bw_changed_cb,
1691 void *bw_changed_cb_cls,
1692 GAS_get_preferences get_preference,
1693 void *get_preference_cls)
1695 struct GAS_MLP_Handle * mlp = GNUNET_malloc (sizeof (struct GAS_MLP_Handle));
1700 unsigned long long tmp;
1707 struct GNUNET_TIME_Relative max_duration;
1708 long long unsigned int max_iterations;
1710 /* Init GLPK environment */
1711 int res = glp_init_env();
1714 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1715 "initialization successful");
1718 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1719 "environment is already initialized");
1722 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1723 "initialization failed (insufficient memory)");
1728 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1729 "initialization failed (unsupported programming model)");
1738 mlp->pv.BIG_M = (double) BIG_M_VALUE;
1740 /* Get timeout for iterations */
1741 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(cfg, "ats", "MLP_MAX_DURATION", &max_duration))
1743 max_duration = MLP_MAX_EXEC_DURATION;
1746 /* Get maximum number of iterations */
1747 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "MLP_MAX_ITERATIONS", &max_iterations))
1749 max_iterations = MLP_MAX_ITERATIONS;
1752 /* Get diversity coefficient from configuration */
1753 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1754 "MLP_COEFFICIENT_D",
1756 D = (double) tmp / 100;
1760 /* Get proportionality coefficient from configuration */
1761 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1762 "MLP_COEFFICIENT_R",
1764 R = (double) tmp / 100;
1768 /* Get utilization coefficient from configuration */
1769 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1770 "MLP_COEFFICIENT_U",
1772 U = (double) tmp / 100;
1776 /* Get quality metric coefficients from configuration */
1777 int i_delay = MLP_NaN;
1778 int i_distance = MLP_NaN;
1779 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
1780 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1782 /* initialize quality coefficients with default value 1.0 */
1783 mlp->pv.co_Q[c] = DEFAULT_QUALITY;
1785 mlp->pv.q[c] = q[c];
1786 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
1788 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
1792 if ((i_delay != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1793 "MLP_COEFFICIENT_QUALITY_DELAY",
1796 mlp->pv.co_Q[i_delay] = (double) tmp / 100;
1798 mlp->pv.co_Q[i_delay] = DEFAULT_QUALITY;
1800 if ((i_distance != MLP_NaN) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1801 "MLP_COEFFICIENT_QUALITY_DISTANCE",
1803 mlp->pv.co_Q[i_distance] = (double) tmp / 100;
1805 mlp->pv.co_Q[i_distance] = DEFAULT_QUALITY;
1807 /* Get minimum bandwidth per used address from configuration */
1808 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1809 "MLP_MIN_BANDWIDTH",
1814 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1817 /* Get minimum number of connections from configuration */
1818 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1819 "MLP_MIN_CONNECTIONS",
1823 n_min = DEFAULT_MIN_CONNECTIONS;
1825 /* Init network quotas */
1826 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1827 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1830 for (c2 = 0; c2 < dest_length; c2++)
1832 if (quotas[c] == network[c2])
1834 mlp->pv.quota_index[c] = network[c2];
1835 mlp->pv.quota_out[c] = out_dest[c2];
1836 mlp->pv.quota_in[c] = in_dest[c2];
1838 LOG (GNUNET_ERROR_TYPE_DEBUG, "Quota for network `%s' (in/out) %llu/%llu\n",
1839 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1840 mlp->pv.quota_out[c],
1841 mlp->pv.quota_in[c]);
1846 /* Check if defined quota could make problem unsolvable */
1847 if ((n_min * b_min) > mlp->pv.quota_out[c])
1849 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
1850 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1851 mlp->pv.quota_out[c],
1853 mlp->pv.quota_out[c] = (n_min * b_min);
1855 if ((n_min * b_min) > mlp->pv.quota_in[c])
1857 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
1858 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1859 mlp->pv.quota_in[c],
1861 mlp->pv.quota_in[c] = (n_min * b_min);
1864 /* Check if bandwidth is too big to make problem solvable */
1865 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
1867 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
1868 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1869 mlp->pv.quota_out[c],
1871 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
1873 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
1875 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
1876 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1877 mlp->pv.quota_in[c],
1879 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
1882 if (GNUNET_NO == found)
1884 mlp->pv.quota_in[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1885 mlp->pv.quota_out[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1886 LOG (GNUNET_ERROR_TYPE_INFO, _("Using default quota configuration for network `%s' (in/out) %llu/%llu\n"),
1887 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
1888 mlp->pv.quota_in[c],
1889 mlp->pv.quota_out[c]);
1893 /* Assign options to handle */
1894 mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1895 mlp->bw_changed_cb = bw_changed_cb;
1896 mlp->bw_changed_cb_cls = bw_changed_cb_cls;
1897 mlp->get_preferences = get_preference;
1898 mlp->get_preferences_cls = get_preference_cls;
1899 /* Setting MLP Input variables */
1903 mlp->pv.b_min = b_min;
1904 mlp->pv.n_min = n_min;
1905 mlp->pv.m_q = GNUNET_ATS_QualityPropertiesCount;
1906 mlp->write_mip_mps = GNUNET_NO;
1907 mlp->write_mip_sol = GNUNET_NO;
1908 mlp->mlp_prob_changed = GNUNET_NO;
1909 mlp->mlp_prob_updated = GNUNET_NO;
1910 mlp->mlp_auto_solve = GNUNET_YES;
1911 mlp->peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1912 mlp->bulk_request = 0;
1916 /* Redirect GLPK output to GNUnet logging */
1917 glp_term_hook (&mlp_term_hook, (void *) mlp);
1919 /* Init LP solving parameters */
1920 glp_init_smcp(&mlp->control_param_lp);
1921 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
1923 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
1925 mlp->control_param_lp.it_lim = max_iterations;
1926 mlp->control_param_lp.tm_lim = max_duration.rel_value;
1928 /* Init MLP solving parameters */
1929 glp_init_iocp(&mlp->control_param_mlp);
1930 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
1932 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
1934 mlp->control_param_mlp.tm_lim = max_duration.rel_value;
1936 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
1941 /* end of gnunet-service-ats_addresses_mlp.c */