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/plugin_ats_mlp.c
23 * @brief ats mlp problem solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "plugin_ats_mlp.h"
33 * NOTE: Do not modify this documentation. This documentation is based on
34 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
35 * use build_txt.sh to generate plaintext output
37 * The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
38 * optimizing an mixed integer programming problem. The MLP solver uses a
39 * number of constraints to find the best adddress for a peer and an optimal
40 * bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
43 * We defined a constraint system to find an optimal bandwidth assignment.
44 * This constraint system uses as an input data addresses, bandwidth quotas,
45 * preferences and quality values. This constraint system is stored in an
46 * matrix based equotation system.
50 * A (M)LP problem consists of a target function to optimizes, constraints
51 * and rows and columns. FIXME GLP uses three arrays to index the matrix: two
52 * integer arrays storing the row and column indices in the matrix and an
53 * float array to store the coeeficient.
55 * To solve the problem we first find an initial solution for the LP problem
56 * using the LP solver and then find an MLP solution based on this solution
57 * using the MLP solver.
59 * Solving (M)LP problems has the property that finding an initial solution
60 * for the LP problem is computationally expensive and finding the MLP
61 * solution is cheaper. This is especially interesting an existing LP
62 * solution can be reused if only coefficients in the matrix have changed
63 * (addresses updated). Only when the problem size changes (addresses added
64 * or deleted) a new LP solution has to be found.
67 * The mlp solver solves the bandwidth assignment problem only on demand when
68 * an address suggestion is requested. When an address is requested mlp the
69 * solves the mlp problem and if the active address or the bandwidth assigned
70 * changes it calls the callback to addresses. The mlp solver gets notified
71 * about new addresses (adding sessions), removed addresses (address
72 * deletions) and address updates. To benefit from the mlp properties
73 * mentioned in section 5 the solver rembers if since the last solution
74 * addresses were added or deleted (problem size changed, problem has to be
75 * rebuild and solved from sratch) or if addresses were updated and the
76 * existing solution can be reused.
80 * The quotas for each network segment are passed by addresses. MLP can be
81 * adapted using configuration settings and uses the following parameters:
83 * Maximum duration for a MLP solution procees (default: 3 sec.)
84 * * MLP_MAX_ITERATIONS:
85 * Maximum number of iterations for a MLP solution process (default:
87 * * MLP_MIN_CONNECTIONS:
88 * Minimum number of desired connections (default: 4)
89 * * MLP_MIN_BANDWIDTH:
90 * Minimum amount of bandwidth assigned to an address (default: 1024)
91 * * MLP_COEFFICIENT_D:
92 * Diversity coefficient (default: 1.0)
93 * * MLP_COEFFICIENT_R:
94 * Relativity coefficient (default: 1.0)
95 * * MLP_COEFFICIENT_U:
96 * Utilization coefficient (default: 1.0)
97 * * MLP_COEFFICIENT_D:
98 * Diversity coefficient (default: 1.0)
99 * * MLP_COEFFICIENT_QUALITY_DELAY:
100 * Quality delay coefficient (default: 1.0)
101 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
102 * Quality distance coefficient (default: 1.0)
103 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
104 * Quality distance coefficient (default: 1.0)
105 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
106 * Quality distance coefficient (default: 1.0)
107 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
108 * Quality distance coefficient (default: 1.0)
110 * 5.2 Data structures used
112 * mlp has for each known peer a struct ATS_Peer containing information about
113 * a specific peer. The address field solver_information contains information
114 * about the mlp properties of this address.
118 * During initialization mlp initializes the GLPK libray used to solve the
119 * MLP problem: it initializes the glpk environment and creates an initial LP
120 * problem. Next it loads the configuration values from the configuration or
121 * uses the default values configured in -addresses_mlp.h. The quotas used
122 * are given by addresses but may have to be adjusted. mlp uses a upper limit
123 * for the bandwidth assigned called BIG M and a minimum amount of bandwidth
124 * an address gets assigned as well as a minium desired number of
125 * connections. If the configured quota is bigger than BIG M, it is reduced
126 * to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
127 * *MLP_MIN_BANDWIDTH it is increased to this value.
133 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
136 * Print debug output for mlp problem creation
138 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
142 * Intercept GLPK terminal output
143 * @param info the mlp handle
144 * @param s the string to print
145 * @return 0: glpk prints output on terminal, 0 != surpress output
148 mlp_term_hook (void *info, const char *s)
150 struct GAS_MLP_Handle *mlp = info;
151 if (mlp->opt_dbg_glpk_verbose)
152 LOG (GNUNET_ERROR_TYPE_ERROR, "%s", s);
158 * Reset peers for next problem creation
160 * @param cls not used
162 * @param value ATS_Peer
166 reset_peers (void *cls,
167 const struct GNUNET_PeerIdentity *key,
170 struct ATS_Peer *peer = value;
171 peer->processed = GNUNET_NO;
176 * Delete the MLP problem and free the constrain matrix
178 * @param mlp the MLP handle
181 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
186 if (mlp->p.prob != NULL)
188 glp_delete_prob(mlp->p.prob);
192 /* delete row index */
193 if (mlp->p.ia != NULL)
195 GNUNET_free (mlp->p.ia);
199 /* delete column index */
200 if (mlp->p.ja != NULL)
202 GNUNET_free (mlp->p.ja);
206 /* delete coefficients */
207 if (mlp->p.ar != NULL)
209 GNUNET_free (mlp->p.ar);
215 mlp->p.c_d = MLP_UNDEFINED;
216 mlp->p.c_r = MLP_UNDEFINED;
217 mlp->p.r_c2 = MLP_UNDEFINED;
218 mlp->p.r_c4 = MLP_UNDEFINED;
219 mlp->p.r_c6 = MLP_UNDEFINED;
220 mlp->p.r_c9 = MLP_UNDEFINED;
221 for (c = 0; c < mlp->pv.m_q ; c ++)
222 mlp->p.r_q[c] = MLP_UNDEFINED;
223 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c ++)
224 mlp->p.r_quota[c] = MLP_UNDEFINED;
225 mlp->p.ci = MLP_UNDEFINED;
228 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
234 * Translate ATS properties to text
235 * Just intended for debugging
237 * @param ats_index the ATS index
238 * @return string with result
241 mlp_ats_to_string (int ats_index)
244 case GNUNET_ATS_ARRAY_TERMINATOR:
245 return "GNUNET_ATS_ARRAY_TERMINATOR";
246 case GNUNET_ATS_UTILIZATION_OUT:
247 return "GNUNET_ATS_UTILIZATION_OUT";
248 case GNUNET_ATS_UTILIZATION_IN:
249 return "GNUNET_ATS_UTILIZATION_IN";
250 case GNUNET_ATS_UTILIZATION_PAYLOAD_OUT:
251 return "GNUNET_ATS_UTILIZATION_PAYLOAD_OUT";
252 case GNUNET_ATS_UTILIZATION_PAYLOAD_IN:
253 return "GNUNET_ATS_UTILIZATION_PAYLOAD_IN";
254 case GNUNET_ATS_COST_LAN:
255 return "GNUNET_ATS_COST_LAN";
256 case GNUNET_ATS_COST_WAN:
257 return "GNUNET_ATS_COST_LAN";
258 case GNUNET_ATS_COST_WLAN:
259 return "GNUNET_ATS_COST_WLAN";
260 case GNUNET_ATS_NETWORK_TYPE:
261 return "GNUNET_ATS_NETWORK_TYPE";
262 case GNUNET_ATS_QUALITY_NET_DELAY:
263 return "GNUNET_ATS_QUALITY_NET_DELAY";
264 case GNUNET_ATS_QUALITY_NET_DISTANCE:
265 return "GNUNET_ATS_QUALITY_NET_DISTANCE";
273 * Translate glpk status error codes to text
274 * @param retcode return code
275 * @return string with result
278 mlp_status_to_string (int retcode)
282 return "solution is undefined";
284 return "solution is feasible";
286 return "solution is infeasible";
288 return "no feasible solution exists";
290 return "solution is optimal";
292 return "solution is unbounded";
295 return "unknown error";
300 * Translate glpk solver error codes to text
301 * @param retcode return code
302 * @return string with result
305 mlp_solve_to_string (int retcode)
311 return "invalid basis";
313 return "singular matrix";
315 return "ill-conditioned matrix";
317 return "invalid bounds";
319 return "solver failed";
321 return "objective lower limit reached";
323 return "objective upper limit reached";
325 return "iteration limit exceeded";
327 return "time limit exceeded";
329 return "no primal feasible solution";
331 return "no dual feasible solution";
333 return "root LP optimum not provided";
335 return "search terminated by application";
337 return "relative mip gap tolerance reached";
339 return "no dual feasible solution";
341 return "no convergence";
343 return "numerical instability";
345 return "invalid data";
347 return "result out of range";
350 return "unknown error";
355 * Extract an ATS performance info from an address
357 * @param address the address
358 * @param type the type to extract in HBO
359 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
362 get_performance_info (struct ATS_Address *address, uint32_t type)
365 GNUNET_assert (NULL != address);
367 if ((NULL == address->atsi) || (0 == address->atsi_count))
368 return GNUNET_ATS_VALUE_UNDEFINED;
370 for (c1 = 0; c1 < address->atsi_count; c1++)
372 if (ntohl (address->atsi[c1].type) == type)
373 return ntohl (address->atsi[c1].value);
375 return GNUNET_ATS_VALUE_UNDEFINED;
381 const struct GNUNET_CONTAINER_MultiPeerMap *map;
386 mlp_create_problem_count_addresses_it (void *cls,
387 const struct GNUNET_PeerIdentity *key,
390 struct CountContext *cctx = cls;
392 /* Check if we have to add this peer due to a pending request */
393 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
400 mlp_create_problem_count_addresses (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
401 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
403 struct CountContext cctx;
405 cctx.map = requested_peers;
407 GNUNET_CONTAINER_multipeermap_iterate (addresses,
408 &mlp_create_problem_count_addresses_it, &cctx);
414 mlp_create_problem_count_peers_it (void *cls,
415 const struct GNUNET_PeerIdentity *key,
418 struct CountContext *cctx = cls;
420 /* Check if we have to addresses for the requested peer */
421 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
428 mlp_create_problem_count_peers (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
429 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
431 struct CountContext cctx;
433 cctx.map = addresses;
435 GNUNET_CONTAINER_multipeermap_iterate (requested_peers,
436 &mlp_create_problem_count_peers_it, &cctx);
442 * Updates an existing value in the matrix
444 * Extract the row, updates the value and updates the row in the problem
446 * @param p the mlp problem
447 * @param row the row to create the value in
448 * @param col the column to create the value in
449 * @param val the value to set
450 * @param line calling line for debbuging
451 * @return GNUNET_YES value changed, GNUNET_NO value did not change, GNUNET_SYSERR
455 mlp_create_problem_update_value (struct MLP_Problem *p,
456 int row, int col, double val,
467 GNUNET_assert (NULL != p);
468 GNUNET_assert (NULL != p->prob);
470 /* Get number of columns and prepare data structure */
471 c_cols = glp_get_num_cols(p->prob);
473 return GNUNET_SYSERR;
475 val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
476 GNUNET_assert (NULL != val_array);
477 ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
478 GNUNET_assert (NULL != ind_array);
479 /* Extract the row */
481 /* Update the value */
482 c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array);
484 for (c1 = 1; c1 < (c_elems+1); c1++)
486 if (ind_array[c1] == col)
492 if (GNUNET_NO == found)
494 ind_array[c_elems+1] = col;
495 val_array[c_elems+1] = val;
496 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Setting value in [%s : %s] to `%.2f'\n",
497 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
499 glp_set_mat_row (p->prob, row, c_elems+1, ind_array, val_array);
500 GNUNET_free (ind_array);
501 GNUNET_free (val_array);
507 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Updating value in [%s : %s] from `%.2f' to `%.2f'\n",
508 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
510 if (val != val_array[c1])
515 /* Update the row in the matrix */
516 glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
519 GNUNET_free (ind_array);
520 GNUNET_free (val_array);
525 * Creates a new value in the matrix
527 * Sets the row and column index in the problem array and increments the
530 * @param p the mlp problem
531 * @param row the row to create the value in
532 * @param col the column to create the value in
533 * @param val the value to set
534 * @param line calling line for debbuging
537 mlp_create_problem_set_value (struct MLP_Problem *p,
538 int row, int col, double val,
541 if ((p->ci) >= p->num_elements)
543 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
544 line, p->ci + 1, p->num_elements);
548 if ((0 == row) || (0 == col))
551 LOG (GNUNET_ERROR_TYPE_ERROR, "[P]: Invalid call from line %u: row = %u, col = %u\n",
557 #if DEBUG_MLP_PROBLEM_CREATION
558 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u == %.2f\n",
559 line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
565 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
566 unsigned int type, unsigned int bound, double lb, double ub,
569 int col = glp_add_cols (p->prob, 1);
570 glp_set_col_name (p->prob, col, name);
571 glp_set_col_bnds (p->prob, col, bound, lb, ub);
572 glp_set_col_kind (p->prob, col, type);
573 glp_set_obj_coef (p->prob, col, coef);
574 #if DEBUG_MLP_PROBLEM_CREATION
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
582 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
583 unsigned int bound, double lb, double ub)
586 int row = glp_add_rows (p->prob, 1);
588 glp_set_row_name (p->prob, row, name);
589 /* set row bounds: <= 0 */
590 glp_set_row_bnds (p->prob, row, bound, lb, ub);
594 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
597 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
600 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
603 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
606 GNUNET_asprintf(&op, "ERROR");
609 #if DEBUG_MLP_PROBLEM_CREATION
610 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
619 * - address columns b and n
620 * - address dependent constraint rows c1, c3
621 * - peer dependent rows c2 and c9
622 * - Set address dependent entries in problem matrix as well
625 mlp_create_problem_add_address_information (void *cls,
626 const struct GNUNET_PeerIdentity *key,
629 struct GAS_MLP_Handle *mlp = cls;
630 struct MLP_Problem *p = &mlp->p;
631 struct ATS_Address *address = value;
632 struct ATS_Peer *peer;
633 struct MLP_information *mlpi;
639 uint32_t addr_net_index;
640 unsigned long long max_quota;
643 /* Check if we have to add this peer due to a pending request */
644 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(mlp->requested_peers, key))
647 mlpi = address->solver_information;
650 fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
655 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
656 for (addr_net_index = 0; addr_net_index < GNUNET_ATS_NetworkTypeCount; addr_net_index++)
658 if (mlp->pv.quota_index[addr_net_index] == addr_net)
662 if (addr_net_index >= GNUNET_ATS_NetworkTypeCount)
669 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
671 if (mlp->pv.quota_out[c] > max_quota)
672 max_quota = mlp->pv.quota_out[c];
673 if (mlp->pv.quota_in[c] > max_quota)
674 max_quota = mlp->pv.quota_in[c];
676 if (max_quota > mlp->pv.BIG_M)
677 cur_bigm = (double) mlp->pv.BIG_M;
679 cur_bigm = max_quota;
683 peer = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, key);
684 if (peer->processed == GNUNET_NO)
686 /* Add peer dependent constraints */
687 /* Add c2) One address active per peer */
688 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
689 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
691 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
693 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
695 /* Add c9) Relativity */
696 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
697 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
699 /* c9) set coefficient */
700 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f , __LINE__);
703 peer->processed = GNUNET_YES;
706 /* Reset addresses' solver information */
713 /* Add bandwidth column */
714 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
715 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
717 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
721 /* Maximize for bandwidth assignment in feasibility testing */
722 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
726 /* Add address active column */
727 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
728 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
731 /* Add address dependent constraints */
732 /* Add c1) bandwidth capping: b_t + (-M) * n_t <= 0 */
733 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
734 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
736 /* c1) set b = 1 coefficient */
737 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
738 /* c1) set n = - min (M, quota) coefficient */
739 cur_bigm = (double) mlp->pv.quota_out[addr_net_index];
740 if (cur_bigm > mlp->pv.BIG_M)
741 cur_bigm = (double) mlp->pv.BIG_M;
742 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -cur_bigm, __LINE__);
744 /* Add constraint c 3) minimum bandwidth
745 * b_t + (-n_t * b_min) >= 0
747 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
748 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
751 /* c3) set b = 1 coefficient */
752 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
753 /* c3) set n = -b_min coefficient */
754 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
757 /* Set coefficient entries in invariant rows */
761 /* c 4) minimum connections */
762 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
763 /* c 2) 1 address peer peer */
764 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
765 /* c 10) obey network specific quotas
766 * (1)*b_1 + ... + (1)*b_m <= quota_n
768 mlp_create_problem_set_value (p, p->r_quota[addr_net_index], mlpi->c_b, 1, __LINE__);
771 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
773 /* c 6) maximize diversity */
774 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
775 /* c 9) relativity */
776 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
777 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
779 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
780 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
781 /* c 7) Optimize quality */
782 /* For all quality metrics, set quality of this address */
783 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
785 props = mlp->get_properties (mlp->get_properties_cls, address);
786 for (c = 0; c < mlp->pv.m_q; c++)
788 if ((props[c] < 1.0) && (props[c] > 2.0))
790 fprintf (stderr, "PROP == %.3f \t ", props[c]);
793 mlp_create_problem_set_value (p, p->r_q[c], mlpi->c_b, props[c], __LINE__);
802 * Create the invariant columns c4, c6, c10, c8, c7
805 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
811 /* Row for c4) minimum connection */
812 /* Number of minimum connections is min(|Peers|, n_min) */
813 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);
815 /* Rows for c 10) Enforce network quotas */
816 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
819 GNUNET_asprintf(&text, "c10_quota_ats_%s",
820 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
821 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
826 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
829 /* Add row for c6) Maximize for diversity */
830 if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
832 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
833 /* Set c6 ) Setting -D */
834 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
837 /* Adding rows for c 8) Maximize utility */
838 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
840 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
842 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
845 /* For all quality metrics:
846 * c 7) Maximize quality, austerity */
847 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
849 for (c = 0; c < mlp->pv.m_q; c++)
851 GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->pv.q[c]));
852 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
854 mlp_create_problem_set_value (p, p->r_q[c], p->c_q[c], -1, __LINE__);
862 * Create the invariant columns d, u, r, q0 ... qm
865 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
867 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
872 /* Diversity d column */
873 if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
874 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
876 /* Utilization u column */
877 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
878 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
880 /* Relativity r column */
881 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
882 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
884 /* Quality metric columns */
885 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
887 for (c = 0; c < mlp->pv.m_q; c++)
889 GNUNET_asprintf (&name, "q_%u", mlp->pv.q[c]);
890 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
899 * Create the MLP problem
901 * @param mlp the MLP handle
902 * @return GNUNET_OK or GNUNET_SYSERR
905 mlp_create_problem (struct GAS_MLP_Handle *mlp)
907 struct MLP_Problem *p = &mlp->p;
910 GNUNET_assert (p->prob == NULL);
911 GNUNET_assert (p->ia == NULL);
912 GNUNET_assert (p->ja == NULL);
913 GNUNET_assert (p->ar == NULL);
914 /* Reset MLP problem struct */
916 /* create the glpk problem */
917 p->prob = glp_create_prob ();
918 GNUNET_assert (NULL != p->prob);
919 p->num_peers = mlp_create_problem_count_peers (mlp->requested_peers, mlp->addresses);
920 p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers, mlp->addresses);
922 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
923 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses +
924 mlp->pv.m_q + p->num_peers + 2 + 1);
925 LOG (GNUNET_ERROR_TYPE_DEBUG,
926 "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
932 /* Set a problem name */
933 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
934 /* Set optimization direction to maximize */
935 glp_set_obj_dir (p->prob, GLP_MAX);
937 /* Create problem matrix */
938 /* last +1 caused by glpk index starting with one: [1..elements]*/
941 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
943 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
945 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
947 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
949 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
950 return GNUNET_SYSERR;
953 /* Adding invariant columns */
954 mlp_create_problem_add_invariant_columns (mlp, p);
956 /* Adding address independent constraint rows */
957 mlp_create_problem_add_invariant_rows (mlp, p);
959 /* Adding address dependent columns constraint rows */
960 GNUNET_CONTAINER_multipeermap_iterate (mlp->addresses,
961 &mlp_create_problem_add_address_information,
964 /* Load the matrix */
965 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
966 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
967 if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
969 glp_scale_prob (p->prob, GLP_SF_AUTO);
976 * Solves the LP problem
978 * @param mlp the MLP Handle
979 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
982 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
986 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
988 LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: %s\n",
989 mlp_solve_to_string (res));
991 LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem failed: %s\n",
992 mlp_solve_to_string (res));
994 /* Analyze problem status */
995 res_status = glp_get_status (mlp->p.prob);
996 switch (res_status) {
997 case GLP_OPT: /* solution is optimal */
998 LOG (GNUNET_ERROR_TYPE_INFO,
999 "Solving LP problem: %s, %s\n",
1000 mlp_solve_to_string(res),
1001 mlp_status_to_string(res_status));
1004 LOG (GNUNET_ERROR_TYPE_ERROR,
1005 "Solving LP problem failed: %s %s\n",
1006 mlp_solve_to_string(res),
1007 mlp_status_to_string(res_status));
1008 return GNUNET_SYSERR;
1014 * Propagates the results when MLP problem was solved
1016 * @param cls the MLP handle
1017 * @param key the peer identity
1018 * @param value the address
1019 * @return #GNUNET_OK to continue
1022 mlp_propagate_results (void *cls,
1023 const struct GNUNET_PeerIdentity *key,
1026 struct GAS_MLP_Handle *mlp = cls;
1027 struct ATS_Address *address;
1028 struct MLP_information *mlpi;
1029 double mlp_bw_in = MLP_NaN;
1030 double mlp_bw_out = MLP_NaN;
1031 double mlp_use = MLP_NaN;
1033 /* Check if we have to add this peer due to a pending request */
1034 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mlp->requested_peers,
1040 GNUNET_assert (address->solver_information != NULL);
1041 mlpi = address->solver_information;
1043 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
1044 if (mlp_bw_in > (double) UINT32_MAX)
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
1047 mlp_bw_in = (double) UINT32_MAX;
1049 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
1050 if (mlp_bw_out > (double) UINT32_MAX)
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
1053 mlp_bw_out = (double) UINT32_MAX;
1055 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
1059 * LOG (GNUNET_ERROR_TYPE_INFO, "MLP result address: `%s' `%s' length %u session %u, mlp use %.3f\n",
1060 * GNUNET_i2s(&address->peer), address->plugin,
1061 * address->addr_len, address->session_id);
1064 if (GLP_YES == mlp_use)
1066 /* This address was selected by the solver to be used */
1067 mlpi->n = GNUNET_YES;
1068 if (GNUNET_NO == address->active)
1070 /* Address was not used before, enabling address */
1071 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n",
1072 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1073 address->active = GNUNET_YES;
1074 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
1075 mlpi->b_in.value__ = htonl(mlp_bw_in);
1076 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
1077 mlpi->b_out.value__ = htonl(mlp_bw_out);
1078 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1079 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1082 else if (GNUNET_YES == address->active)
1084 /* Address was used before, check for bandwidth change */
1085 if ((mlp_bw_out != ntohl(address->assigned_bw_out.value__)) ||
1086 (mlp_bw_in != ntohl(address->assigned_bw_in.value__)))
1088 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n",
1089 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1090 address->assigned_bw_in.value__ = htonl (mlp_bw_in);
1091 mlpi->b_in.value__ = htonl(mlp_bw_in);
1092 address->assigned_bw_out.value__ = htonl (mlp_bw_out);
1093 mlpi->b_out.value__ = htonl(mlp_bw_out);
1094 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1095 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1102 else if (GLP_NO == mlp_use)
1104 /* This address was selected by the solver to be not used */
1105 mlpi->n = GNUNET_NO;
1106 if (GNUNET_NO == address->active)
1108 /* Address was not used before, nothing to do */
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n",
1110 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1113 else if (GNUNET_YES == address->active)
1115 /* Address was used before, disabling address */
1116 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n",
1117 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1118 address->active = GNUNET_NO;
1119 /* Set bandwidth to 0 */
1120 address->assigned_bw_in = BANDWIDTH_ZERO;
1121 mlpi->b_in.value__ = htonl(mlp_bw_in);
1122 address->assigned_bw_out = BANDWIDTH_ZERO;
1123 mlpi->b_out.value__ = htonl(mlp_bw_out);
1124 //mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1136 static void notify (struct GAS_MLP_Handle *mlp,
1137 enum GAS_Solver_Operation op,
1138 enum GAS_Solver_Status stat,
1139 enum GAS_Solver_Additional_Information add)
1141 if (NULL != mlp->env->info_cb)
1142 mlp->env->info_cb (mlp->env->info_cb_cls, op, stat, add);
1145 static void mlp_branch_and_cut_cb (glp_tree *tree, void *info)
1147 struct GAS_MLP_Handle *mlp = info;
1150 switch (glp_ios_reason (tree))
1153 /* Do nothing here */
1156 /* Do nothing here */
1159 /* Do nothing here */
1162 /* Do nothing here */
1165 /* Do nothing here */
1168 /* Do nothing here */
1171 /* A better solution was found */
1172 mlp->ps.mlp_gap = glp_ios_mip_gap (tree);
1173 mlp_obj = glp_mip_obj_val (mlp->p.prob);
1174 mlp->ps.lp_mlp_gap = (abs(mlp_obj - mlp->ps.lp_objective_value)) / (abs(mlp_obj) + DBL_EPSILON);
1176 LOG (GNUNET_ERROR_TYPE_INFO,
1177 "Found better integer solution, current gaps: %.3f <= %.3f, %.3f <= %.3f\n",
1178 mlp->ps.mlp_gap, mlp->pv.mip_gap,
1179 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1181 if (mlp->ps.mlp_gap <= mlp->pv.mip_gap)
1183 LOG (GNUNET_ERROR_TYPE_INFO,
1184 "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
1185 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1186 glp_ios_terminate (tree);
1189 if (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap)
1191 LOG (GNUNET_ERROR_TYPE_INFO,
1192 "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
1193 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1194 glp_ios_terminate (tree);
1206 * Solves the MLP problem
1208 * @param solver the MLP Handle
1209 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
1212 GAS_mlp_solve_problem (void *solver)
1214 struct GAS_MLP_Handle *mlp = solver;
1220 struct GNUNET_TIME_Absolute start_total;
1221 struct GNUNET_TIME_Absolute start_cur_op;
1222 struct GNUNET_TIME_Relative dur_total;
1223 struct GNUNET_TIME_Relative dur_setup;
1224 struct GNUNET_TIME_Relative dur_lp;
1225 struct GNUNET_TIME_Relative dur_mlp;
1227 GNUNET_assert(NULL != solver);
1229 if (GNUNET_YES == mlp->stat_bulk_lock)
1231 mlp->stat_bulk_requests++;
1234 notify(mlp, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS,
1235 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1236 start_total = GNUNET_TIME_absolute_get();
1238 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->requested_peers))
1240 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1241 return GNUNET_OK; /* No pending requests */
1243 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->addresses))
1245 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1246 return GNUNET_OK; /* No addresses available */
1249 if ((GNUNET_NO == mlp->stat_mlp_prob_changed)
1250 && (GNUNET_NO == mlp->stat_mlp_prob_updated))
1252 LOG(GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
1253 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1256 if (GNUNET_YES == mlp->stat_mlp_prob_changed)
1258 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
1259 notify(mlp, GAS_OP_SOLVE_SETUP_START, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1260 mlp_delete_problem(mlp);
1261 if (GNUNET_SYSERR == mlp_create_problem(mlp))
1263 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_FAIL, GAS_INFO_FULL);
1264 return GNUNET_SYSERR;
1266 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1267 if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
1269 mlp->control_param_lp.presolve = GLP_YES; /* LP presolver, we need lp solution */
1270 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
1274 mlp->control_param_lp.presolve = GNUNET_NO; /* LP presolver, we need lp solution */
1275 mlp->control_param_mlp.presolve = GLP_YES; /* No presolver, we have LP solution */
1276 dur_lp = GNUNET_TIME_UNIT_ZERO;
1281 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
1284 /* Reset solution info */
1285 mlp->ps.lp_objective_value = 0.0;
1286 mlp->ps.mlp_gap = 1.0;
1287 mlp->ps.mlp_objective_value = 0.0;
1288 mlp->ps.lp_mlp_gap = 0.0;
1290 dur_setup = GNUNET_TIME_absolute_get_duration (start_total);
1293 if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
1295 notify(mlp, GAS_OP_SOLVE_MLP_LP_START, GAS_STAT_SUCCESS,
1296 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1297 LOG(GNUNET_ERROR_TYPE_DEBUG,
1298 "Running LP solver %s\n",
1299 (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
1300 start_cur_op = GNUNET_TIME_absolute_get();
1303 /* Only for debugging, always use LP presolver:
1304 * mlp->control_param_lp.presolve = GLP_YES; */
1305 res_lp = mlp_solve_lp_problem(mlp);
1306 if (GNUNET_OK == res_lp)
1308 mlp->ps.lp_objective_value = glp_get_obj_val (mlp->p.prob);
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 "LP solution was: %.3f\n",
1311 mlp->ps.lp_objective_value);
1314 dur_lp = GNUNET_TIME_absolute_get_duration (start_cur_op);
1315 notify(mlp, GAS_OP_SOLVE_MLP_LP_STOP,
1316 (GNUNET_OK == res_lp) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1317 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1320 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
1323 /* Run MLP solver */
1324 if ((GNUNET_OK == res_lp) || (GNUNET_YES == mlp->opt_dbg_intopt_presolver))
1326 LOG(GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
1327 notify(mlp, GAS_OP_SOLVE_MLP_MLP_START, GAS_STAT_SUCCESS,
1328 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1329 start_cur_op = GNUNET_TIME_absolute_get();
1333 /* Only for debugging, always use MLP presolver */
1334 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
1335 mlp->control_param_mlp.presolve = GNUNET_YES;
1338 mip_res = glp_intopt (mlp->p.prob, &mlp->control_param_mlp);
1343 LOG (GNUNET_ERROR_TYPE_INFO,
1344 "Solving MLP problem: %s\n",
1345 mlp_solve_to_string (mip_res));
1347 case GLP_ETMLIM: /* Time limit reached */
1348 case GLP_EMIPGAP: /* MIP gap tolerance limit reached */
1349 case GLP_ESTOP: /* Solver was instructed to stop*/
1350 /* Semi-successful */
1351 LOG (GNUNET_ERROR_TYPE_INFO,
1352 "Solving MLP problem solution was interupted: %s\n",
1353 mlp_solve_to_string (mip_res));
1362 LOG (GNUNET_ERROR_TYPE_INFO,
1363 "Solving MLP problem failed: %s\n",
1364 mlp_solve_to_string (mip_res));
1368 /* Analyze problem status */
1369 mip_status = glp_mip_status(mlp->p.prob);
1372 case GLP_OPT: /* solution is optimal */
1373 LOG (GNUNET_ERROR_TYPE_WARNING,
1374 "Solution of MLP problem is optimal: %s, %s\n",
1375 mlp_solve_to_string (mip_res),
1376 mlp_status_to_string (mip_status));
1377 mip_res = GNUNET_OK;
1379 case GLP_FEAS: /* solution is feasible but not proven optimal */
1381 if ( (mlp->ps.mlp_gap <= mlp->pv.mip_gap) ||
1382 (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap) )
1384 LOG (GNUNET_ERROR_TYPE_INFO,
1385 "Solution of MLP problem is feasible and solution within gap constraints: %s, %s\n",
1386 mlp_solve_to_string (mip_res),
1387 mlp_status_to_string (mip_status));
1388 mip_res = GNUNET_OK;
1392 LOG (GNUNET_ERROR_TYPE_WARNING,
1393 "Solution of MLP problem is feasible but solution not within gap constraints: %s, %s\n",
1394 mlp_solve_to_string (mip_res),
1395 mlp_status_to_string (mip_status));
1396 mip_res = GNUNET_SYSERR;
1399 case GLP_UNDEF: /* Solution undefined */
1400 case GLP_NOFEAS: /* No feasible solution */
1402 LOG (GNUNET_ERROR_TYPE_ERROR,
1403 "Solving MLP problem failed: %s %s\n",
1404 mlp_solve_to_string (mip_res),
1405 mlp_status_to_string (mip_status));
1406 mip_res = GNUNET_SYSERR;
1410 dur_mlp = GNUNET_TIME_absolute_get_duration (start_cur_op);
1411 dur_total = GNUNET_TIME_absolute_get_duration (start_total);
1413 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP,
1414 (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1415 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1419 /* Do not execute mip solver since lp solution is invalid */
1420 dur_mlp = GNUNET_TIME_UNIT_ZERO;
1421 dur_total = GNUNET_TIME_absolute_get_duration (start_total);
1423 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP, GAS_STAT_FAIL,
1424 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1425 mip_res = GNUNET_SYSERR;
1428 /* Notify about end */
1429 notify(mlp, GAS_OP_SOLVE_STOP,
1430 ((GNUNET_OK == mip_res) && (GNUNET_OK == mip_res)) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1431 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1433 LOG (GNUNET_ERROR_TYPE_DEBUG,
1434 "Execution time for %s solve: (total/setup/lp/mlp) : %llu %llu %llu %llu\n",
1435 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? "full" : "updated",
1436 (unsigned long long) dur_total.rel_value_us,
1437 (unsigned long long) dur_setup.rel_value_us,
1438 (unsigned long long) dur_lp.rel_value_us,
1439 (unsigned long long) dur_mlp.rel_value_us);
1442 mlp->ps.lp_res = res_lp;
1443 mlp->ps.mip_res = mip_res;
1444 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1445 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1446 mlp->ps.p_cols = glp_get_num_cols(mlp->p.prob);
1447 mlp->ps.p_rows = glp_get_num_rows(mlp->p.prob);
1448 mlp->ps.p_elements = mlp->p.num_elements;
1450 /* Propagate result*/
1451 notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1452 (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1454 if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
1456 GNUNET_CONTAINER_multipeermap_iterate(mlp->addresses,
1457 &mlp_propagate_results, mlp);
1459 notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1460 (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1463 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1464 if ( (GNUNET_YES == mlp->opt_dump_problem_all) ||
1465 (mlp->opt_dump_problem_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
1467 /* Write problem to disk */
1468 switch (mlp->opt_log_format) {
1470 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.cplex", mlp->p.num_peers,
1471 mlp->p.num_addresses, time.abs_value_us);
1472 glp_write_lp (mlp->p.prob, NULL, filename);
1475 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.glpk", mlp->p.num_peers,
1476 mlp->p.num_addresses, time.abs_value_us);
1477 glp_write_prob (mlp->p.prob, 0, filename);
1480 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers,
1481 mlp->p.num_addresses, time.abs_value_us);
1482 glp_write_mps (mlp->p.prob, GLP_MPS_FILE, NULL, filename);
1487 LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped problem to file: `%s' \n", filename);
1488 GNUNET_free(filename);
1490 if ( (mlp->opt_dump_solution_all) ||
1491 (mlp->opt_dump_solution_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
1493 /* Write solution to disk */
1494 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers,
1495 mlp->p.num_addresses, time.abs_value_us);
1496 glp_print_mip(mlp->p.prob, filename);
1497 LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped solution to file: `%s' \n", filename);
1498 GNUNET_free(filename);
1501 /* Reset change and update marker */
1502 mlp->control_param_lp.presolve = GLP_NO;
1503 mlp->stat_mlp_prob_updated = GNUNET_NO;
1504 mlp->stat_mlp_prob_changed = GNUNET_NO;
1506 if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
1509 return GNUNET_SYSERR;
1513 * Add a single address to the solve
1515 * @param solver the solver Handle
1516 * @param address the address to add
1517 * @param network network type of this address
1520 GAS_mlp_address_add (void *solver,
1521 struct ATS_Address *address,
1524 struct GAS_MLP_Handle *mlp = solver;
1527 GNUNET_assert (NULL != solver);
1528 GNUNET_assert (NULL != address);
1530 if (GNUNET_ATS_NetworkTypeCount <= network)
1536 if (NULL == address->solver_information)
1538 address->solver_information = GNUNET_new (struct MLP_information);
1541 LOG (GNUNET_ERROR_TYPE_ERROR,
1542 _("Adding address for peer `%s' multiple times\n"),
1543 GNUNET_i2s(&address->peer));
1545 /* Is this peer included in the problem? */
1546 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1549 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' without address request \n", GNUNET_i2s(&address->peer));
1553 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer `%s' with address request \n", GNUNET_i2s(&address->peer));
1554 /* Problem size changed: new address for peer with pending request */
1555 mlp->stat_mlp_prob_changed = GNUNET_YES;
1556 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1557 GAS_mlp_solve_problem (solver);
1562 * Transport properties for this address have changed
1564 * @param solver solver handle
1565 * @param address the address
1566 * @param type the ATSI type in HBO
1567 * @param abs_value the absolute value of the property
1568 * @param rel_value the normalized value
1571 GAS_mlp_address_property_changed (void *solver,
1572 struct ATS_Address *address,
1577 struct MLP_information *mlpi = address->solver_information;
1578 struct GAS_MLP_Handle *mlp = solver;
1583 GNUNET_assert (NULL != solver);
1584 GNUNET_assert (NULL != address);
1588 LOG (GNUNET_ERROR_TYPE_INFO,
1589 _("Updating address property `%s' for peer `%s' %p not added before\n"),
1590 GNUNET_ATS_print_property_type (type),
1591 GNUNET_i2s(&address->peer),
1597 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1600 /* Peer is not requested, so no need to update problem */
1603 LOG (GNUNET_ERROR_TYPE_INFO, "Updating property `%s' address for peer `%s' to abs %llu rel %.3f\n",
1604 GNUNET_ATS_print_property_type (type),
1605 GNUNET_i2s(&address->peer),
1609 if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
1612 /* Find row index */
1614 for (c1 = 0; c1 < mlp->pv.m_q; c1++)
1616 if (type == mlp->pv.q[c1])
1622 if (-1 == type_index)
1625 return; /* quality index not found */
1628 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index] */
1629 if (GNUNET_YES == mlp_create_problem_update_value (&mlp->p,
1630 mlp->p.r_q[type_index], mlpi->c_b, rel_value, __LINE__))
1632 mlp->stat_mlp_prob_updated = GNUNET_YES;
1633 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1634 GAS_mlp_solve_problem (solver);
1641 * Transport session for this address has changed
1643 * NOTE: values in addresses are already updated
1645 * @param solver solver handle
1646 * @param address the address
1647 * @param cur_session the current session
1648 * @param new_session the new session
1651 GAS_mlp_address_session_changed (void *solver,
1652 struct ATS_Address *address,
1653 uint32_t cur_session,
1654 uint32_t new_session)
1656 /* Nothing to do here */
1662 * Transport session for this address has changed
1664 * NOTE: values in addresses are already updated
1666 * @param solver solver handle
1667 * @param address the address
1668 * @param in_use usage state
1671 GAS_mlp_address_inuse_changed (void *solver,
1672 struct ATS_Address *address,
1675 /* Nothing to do here */
1681 * Network scope for this address has changed
1683 * NOTE: values in addresses are already updated
1685 * @param solver solver handle
1686 * @param address the address
1687 * @param current_network the current network
1688 * @param new_network the new network
1691 GAS_mlp_address_change_network (void *solver,
1692 struct ATS_Address *address,
1693 uint32_t current_network,
1694 uint32_t new_network)
1696 struct MLP_information *mlpi = address->solver_information;
1697 struct GAS_MLP_Handle *mlp = solver;
1699 int nets_avail[] = GNUNET_ATS_NetworkType;
1702 GNUNET_assert (NULL != solver);
1703 GNUNET_assert (NULL != address);
1705 if (GNUNET_ATS_NetworkTypeCount <= new_network)
1717 if (mlpi->c_b == MLP_UNDEFINED)
1718 return; /* This address is not yet in the matrix*/
1720 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1723 /* Peer is not requested, so no need to update problem */
1728 if (current_network == new_network)
1734 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount ; c1 ++)
1736 if (nets_avail[c1] == new_network)
1740 if (GNUNET_ATS_NetworkTypeCount == c1)
1742 /* Invalid network */
1747 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating network for peer `%s' from `%s' to `%s'\n",
1748 GNUNET_i2s (&address->peer),
1749 GNUNET_ATS_print_network_type(current_network),
1750 GNUNET_ATS_print_network_type(new_network));
1752 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount; c1++)
1754 if (mlp->pv.quota_index[c1] == current_network)
1756 /* Remove from old network */
1757 mlp_create_problem_update_value (&mlp->p,
1759 mlpi->c_b, 0.0, __LINE__);
1764 for (c1 = 0; c1 < GNUNET_ATS_NetworkTypeCount; c1++)
1766 if (mlp->pv.quota_index[c1] == new_network)
1768 /* Remove from old network */
1769 if (GNUNET_SYSERR == mlp_create_problem_update_value (&mlp->p,
1771 mlpi->c_b, 1.0, __LINE__))
1773 /* This quota did not exist in the problem, recreate */
1780 mlp->stat_mlp_prob_changed = GNUNET_YES;
1785 * Deletes a single address in the MLP problem
1787 * The MLP problem has to be recreated and the problem has to be resolved
1789 * @param solver the MLP Handle
1790 * @param address the address to delete
1791 * @param session_only delete only session not whole address
1794 GAS_mlp_address_delete (void *solver,
1795 struct ATS_Address *address,
1799 struct GAS_MLP_Handle *mlp = solver;
1800 struct MLP_information *mlpi;
1803 GNUNET_assert (NULL != solver);
1804 GNUNET_assert (NULL != address);
1806 mlpi = address->solver_information;
1807 if ((GNUNET_NO == session_only) && (NULL != mlpi))
1809 /* Remove full address */
1811 address->solver_information = NULL;
1813 was_active = address->active;
1814 address->active = GNUNET_NO;
1815 address->assigned_bw_in = BANDWIDTH_ZERO;
1816 address->assigned_bw_out = BANDWIDTH_ZERO;
1818 /* Is this peer included in the problem? */
1819 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1822 LOG (GNUNET_ERROR_TYPE_INFO, "Deleting %s for peer `%s' without address request \n",
1823 (session_only == GNUNET_YES) ? "session" : "address",
1824 GNUNET_i2s(&address->peer));
1827 LOG (GNUNET_ERROR_TYPE_INFO, "Deleting %s for peer `%s' with address request \n",
1828 (session_only == GNUNET_YES) ? "session" : "address",
1829 GNUNET_i2s(&address->peer));
1831 /* Problem size changed: new address for peer with pending request */
1832 mlp->stat_mlp_prob_changed = GNUNET_YES;
1833 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1835 GAS_mlp_solve_problem (solver);
1837 if (GNUNET_YES == was_active)
1839 if (NULL == GAS_mlp_get_preferred_address (solver, &address->peer))
1841 /* No alternative address, disconnecting peer */
1842 mlp->bw_changed_cb (mlp->bw_changed_cb_cls, address);
1851 * Find the active address in the set of addresses of a peer
1852 * @param cls destination
1853 * @param key peer id
1854 * @param value address
1858 mlp_get_preferred_address_it (void *cls,
1859 const struct GNUNET_PeerIdentity *key,
1862 static int counter = 0;
1863 struct ATS_Address **aa = cls;
1864 struct ATS_Address *addr = value;
1865 struct MLP_information *mlpi = addr->solver_information;
1872 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1873 * "MLP [%u] Peer `%s' %s length %u session %u active %s mlp active %s\n",
1874 * counter, GNUNET_i2s (&addr->peer), addr->plugin, addr->addr_len, addr->session_id,
1875 * (GNUNET_YES == addr->active) ? "active" : "inactive",
1876 * (GNUNET_YES == mlpi->n) ? "active" : "inactive");
1879 if (GNUNET_YES == mlpi->n)
1883 (*aa)->assigned_bw_in = mlpi->b_in;
1884 (*aa)->assigned_bw_out = mlpi->b_out;
1893 get_peer_pref_value (struct GAS_MLP_Handle *mlp, const struct GNUNET_PeerIdentity *peer)
1896 const double *preferences = NULL;
1898 preferences = mlp->get_preferences (mlp->get_preferences_cls, peer);
1901 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
1903 if (c != GNUNET_ATS_PREFERENCE_END)
1905 /* fprintf (stderr, "VALUE[%u] %s %.3f \n",
1906 * c, GNUNET_i2s (&cur->addr->peer), t[c]); */
1907 res += preferences[c];
1911 res /= (GNUNET_ATS_PreferenceCount -1);
1914 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer preference for peer `%s' == %.2f\n",
1915 GNUNET_i2s(peer), res);
1922 * Get the preferred address for a specific peer
1924 * @param solver the MLP Handle
1925 * @param peer the peer
1926 * @return suggested address
1928 const struct ATS_Address *
1929 GAS_mlp_get_preferred_address (void *solver,
1930 const struct GNUNET_PeerIdentity *peer)
1932 struct GAS_MLP_Handle *mlp = solver;
1934 struct ATS_Address *res;
1936 GNUNET_assert (NULL != solver);
1937 GNUNET_assert (NULL != peer);
1939 LOG (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n",
1942 /* Is this peer included in the problem? */
1943 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1946 LOG (GNUNET_ERROR_TYPE_INFO, "Adding peer `%s' to list of requested_peers with requests\n",
1949 p = GNUNET_new (struct ATS_Peer);
1951 p->f = get_peer_pref_value (mlp, peer);
1952 GNUNET_CONTAINER_multipeermap_put (mlp->requested_peers,
1954 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1956 /* Added new peer, we have to rebuild problem before solving */
1957 mlp->stat_mlp_prob_changed = GNUNET_YES;
1959 if ((GNUNET_YES == mlp->opt_mlp_auto_solve)&&
1960 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(mlp->addresses,
1963 mlp->exclude_peer = peer;
1964 GAS_mlp_solve_problem (mlp);
1965 mlp->exclude_peer = NULL;
1968 /* Get prefered address */
1970 GNUNET_CONTAINER_multipeermap_get_multiple (mlp->addresses, peer,
1971 mlp_get_preferred_address_it, &res);
1977 * Start a bulk operation
1979 * @param solver the solver
1982 GAS_mlp_bulk_start (void *solver)
1984 LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1985 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1987 GNUNET_assert (NULL != solver);
1989 s->stat_bulk_lock ++;
1993 GAS_mlp_bulk_stop (void *solver)
1995 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1997 struct GAS_MLP_Handle *s = (struct GAS_MLP_Handle *) solver;
1998 GNUNET_assert (NULL != solver);
2000 if (s->stat_bulk_lock < 1)
2005 s->stat_bulk_lock --;
2007 if (0 < s->stat_bulk_requests)
2009 GAS_mlp_solve_problem (solver);
2010 s->stat_bulk_requests= 0;
2017 * Stop notifying about address and bandwidth changes for this peer
2019 * @param solver the MLP handle
2020 * @param peer the peer
2023 GAS_mlp_stop_get_preferred_address (void *solver,
2024 const struct GNUNET_PeerIdentity *peer)
2026 struct GAS_MLP_Handle *mlp = solver;
2027 struct ATS_Peer *p = NULL;
2029 GNUNET_assert (NULL != solver);
2030 GNUNET_assert (NULL != peer);
2031 if (NULL != (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
2033 GNUNET_CONTAINER_multipeermap_remove (mlp->requested_peers, peer, p);
2036 mlp->stat_mlp_prob_changed = GNUNET_YES;
2037 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2039 GAS_mlp_solve_problem (solver);
2046 * Changes the preferences for a peer in the MLP problem
2048 * @param solver the MLP Handle
2049 * @param peer the peer
2050 * @param kind the kind to change the preference
2051 * @param pref_rel the relative score
2054 GAS_mlp_address_change_preference (void *solver,
2055 const struct GNUNET_PeerIdentity *peer,
2056 enum GNUNET_ATS_PreferenceKind kind,
2059 struct GAS_MLP_Handle *mlp = solver;
2060 struct ATS_Peer *p = NULL;
2062 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing preference for address for peer `%s' to %.2f\n",
2063 GNUNET_i2s(peer), pref_rel);
2065 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
2066 /* Update the constraints with changed preferences */
2070 /* Update relativity constraint c9 */
2071 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
2073 LOG (GNUNET_ERROR_TYPE_INFO, "Updating preference for unknown peer `%s'\n", GNUNET_i2s(peer));
2077 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
2079 p->f = get_peer_pref_value (mlp, peer);
2080 mlp_create_problem_update_value (&mlp->p, p->r_c9, mlp->p.c_r, -p->f, __LINE__);
2082 /* Problem size changed: new address for peer with pending request */
2083 mlp->stat_mlp_prob_updated = GNUNET_YES;
2084 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2085 GAS_mlp_solve_problem (solver);
2091 * Get application feedback for a peer
2093 * @param solver the solver handle
2094 * @param application the application
2095 * @param peer the peer to change the preference for
2096 * @param scope the time interval for this feedback: [now - scope .. now]
2097 * @param kind the kind to change the preference
2098 * @param score the score
2101 GAS_mlp_address_preference_feedback (void *solver,
2103 const struct GNUNET_PeerIdentity *peer,
2104 const struct GNUNET_TIME_Relative scope,
2105 enum GNUNET_ATS_PreferenceKind kind,
2108 struct GAS_PROPORTIONAL_Handle *s = solver;
2109 GNUNET_assert (NULL != solver);
2110 GNUNET_assert (NULL != peer);
2112 GNUNET_assert (NULL != s);
2117 mlp_free_peers (void *cls,
2118 const struct GNUNET_PeerIdentity *key, void *value)
2120 struct GNUNET_CONTAINER_MultiPeerMap *map = cls;
2121 struct ATS_Peer *p = value;
2123 GNUNET_CONTAINER_multipeermap_remove (map, key, value);
2131 * Shutdown the MLP problem solving component
2133 * @param cls the solver handle
2137 libgnunet_plugin_ats_mlp_done (void *cls)
2139 struct GAS_MLP_Handle *mlp = cls;
2140 GNUNET_assert (mlp != NULL);
2142 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down mlp solver\n");
2143 mlp_delete_problem (mlp);
2145 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
2147 mlp->requested_peers);
2148 GNUNET_CONTAINER_multipeermap_destroy (mlp->requested_peers);
2149 mlp->requested_peers = NULL;
2151 /* Clean up GLPK environment */
2155 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutdown down of mlp solver complete\n");
2161 libgnunet_plugin_ats_mlp_init (void *cls)
2163 struct GNUNET_ATS_PluginEnvironment *env = cls;
2164 struct GAS_MLP_Handle * mlp = GNUNET_new (struct GAS_MLP_Handle);
2167 unsigned long long tmp;
2175 struct GNUNET_TIME_Relative max_duration;
2176 long long unsigned int max_iterations;
2178 GNUNET_assert (NULL != env->cfg);
2179 GNUNET_assert (NULL != env->addresses);
2180 GNUNET_assert (NULL != env->bandwidth_changed_cb);
2181 GNUNET_assert (NULL != env->get_preferences);
2182 GNUNET_assert (NULL != env->get_property);
2184 /* Init GLPK environment */
2185 int res = glp_init_env();
2188 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
2189 "initialization successful");
2192 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
2193 "environment is already initialized");
2196 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
2197 "initialization failed (insufficient memory)");
2202 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
2203 "initialization failed (unsupported programming model)");
2211 mlp->opt_dump_problem_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2212 "ats", "MLP_DUMP_PROBLEM_ALL");
2213 if (GNUNET_SYSERR == mlp->opt_dump_problem_all)
2214 mlp->opt_dump_problem_all = GNUNET_NO;
2216 mlp->opt_dump_solution_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2217 "ats", "MLP_DUMP_SOLUTION_ALL");
2218 if (GNUNET_SYSERR == mlp->opt_dump_solution_all)
2219 mlp->opt_dump_solution_all = GNUNET_NO;
2221 mlp->opt_dump_problem_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2222 "ats", "MLP_DUMP_PROBLEM_ON_FAIL");
2223 if (GNUNET_SYSERR == mlp->opt_dump_problem_on_fail)
2224 mlp->opt_dump_problem_on_fail = GNUNET_NO;
2226 mlp->opt_dump_solution_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2227 "ats", "MLP_DUMP_SOLUTION_ON_FAIL");
2228 if (GNUNET_SYSERR == mlp->opt_dump_solution_on_fail)
2229 mlp->opt_dump_solution_on_fail = GNUNET_NO;
2231 mlp->opt_dbg_glpk_verbose = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2232 "ats", "MLP_DBG_GLPK_VERBOSE");
2233 if (GNUNET_SYSERR == mlp->opt_dbg_glpk_verbose)
2234 mlp->opt_dbg_glpk_verbose = GNUNET_NO;
2236 mlp->opt_dbg_feasibility_only = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2237 "ats", "MLP_DBG_FEASIBILITY_ONLY");
2238 if (GNUNET_SYSERR == mlp->opt_dbg_feasibility_only)
2239 mlp->opt_dbg_feasibility_only = GNUNET_NO;
2240 if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
2241 LOG (GNUNET_ERROR_TYPE_WARNING,
2242 "MLP solver is configured to check feasibility only!\n");
2244 mlp->opt_dbg_autoscale_problem = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2245 "ats", "MLP_DBG_AUTOSCALE_PROBLEM");
2246 if (GNUNET_SYSERR == mlp->opt_dbg_autoscale_problem)
2247 mlp->opt_dbg_autoscale_problem = GNUNET_NO;
2248 if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
2249 LOG (GNUNET_ERROR_TYPE_WARNING,
2250 "MLP solver is configured automatically scale the problem!\n");
2252 mlp->opt_dbg_intopt_presolver = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2253 "ats", "MLP_DBG_INTOPT_PRESOLVE");
2254 if (GNUNET_SYSERR == mlp->opt_dbg_intopt_presolver)
2255 mlp->opt_dbg_intopt_presolver = GNUNET_NO;
2256 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
2257 LOG (GNUNET_ERROR_TYPE_WARNING,
2258 "MLP solver is configured use the mlp presolver\n");
2260 mlp->opt_dbg_optimize_diversity = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2261 "ats", "MLP_DBG_OPTIMIZE_DIVERSITY");
2262 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_diversity)
2263 mlp->opt_dbg_optimize_diversity = GNUNET_YES;
2264 if (GNUNET_NO == mlp->opt_dbg_optimize_diversity)
2265 LOG (GNUNET_ERROR_TYPE_WARNING,
2266 "MLP solver is not optimizing for diversity\n");
2268 mlp->opt_dbg_optimize_relativity= GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2269 "ats", "MLP_DBG_OPTIMIZE_RELATIVITY");
2270 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_relativity)
2271 mlp->opt_dbg_optimize_relativity = GNUNET_YES;
2272 if (GNUNET_NO == mlp->opt_dbg_optimize_relativity)
2273 LOG (GNUNET_ERROR_TYPE_WARNING,
2274 "MLP solver is not optimizing for relativity\n");
2276 mlp->opt_dbg_optimize_quality = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2277 "ats", "MLP_DBG_OPTIMIZE_QUALITY");
2278 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_quality)
2279 mlp->opt_dbg_optimize_quality = GNUNET_YES;
2280 if (GNUNET_NO == mlp->opt_dbg_optimize_quality)
2281 LOG (GNUNET_ERROR_TYPE_WARNING,
2282 "MLP solver is not optimizing for quality\n");
2284 mlp->opt_dbg_optimize_utility = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2285 "ats", "MLP_DBG_OPTIMIZE_UTILITY");
2286 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_utility)
2287 mlp->opt_dbg_optimize_utility = GNUNET_YES;
2288 if (GNUNET_NO == mlp->opt_dbg_optimize_utility)
2289 LOG (GNUNET_ERROR_TYPE_WARNING,
2290 "MLP solver is not optimizing for utility\n");
2292 if ( (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
2293 (GNUNET_NO == mlp->opt_dbg_optimize_quality) &&
2294 (GNUNET_NO == mlp->opt_dbg_optimize_relativity) &&
2295 (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
2296 (GNUNET_NO == mlp->opt_dbg_feasibility_only))
2298 LOG (GNUNET_ERROR_TYPE_ERROR,
2299 _("MLP solver is not optimizing for anything, changing to feasibility check\n"));
2300 mlp->opt_dbg_feasibility_only = GNUNET_YES;
2303 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2304 "ats", "MLP_LOG_FORMAT", &outputformat))
2305 mlp->opt_log_format = MLP_CPLEX;
2308 GNUNET_STRINGS_utf8_toupper(outputformat, outputformat);
2309 if (0 == strcmp (outputformat, "MPS"))
2311 mlp->opt_log_format = MLP_MPS;
2313 else if (0 == strcmp (outputformat, "CPLEX"))
2315 mlp->opt_log_format = MLP_CPLEX;
2317 else if (0 == strcmp (outputformat, "GLPK"))
2319 mlp->opt_log_format = MLP_GLPK;
2323 LOG (GNUNET_ERROR_TYPE_WARNING,
2324 "Invalid log format `%s' in configuration, using CPLEX!\n",
2326 mlp->opt_log_format = MLP_CPLEX;
2328 GNUNET_free (outputformat);
2331 mlp->pv.BIG_M = (double) BIG_M_VALUE;
2333 mlp->pv.mip_gap = (double) 0.0;
2334 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2335 "MLP_MAX_MIP_GAP", &f_tmp))
2337 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2339 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2344 mlp->pv.mip_gap = f_tmp;
2345 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2350 mlp->pv.lp_mip_gap = (double) 0.0;
2351 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2352 "MLP_MAX_LP_MIP_GAP", &f_tmp))
2354 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2356 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2361 mlp->pv.lp_mip_gap = f_tmp;
2362 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2367 /* Get timeout for iterations */
2368 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats",
2369 "MLP_MAX_DURATION", &max_duration))
2371 max_duration = MLP_MAX_EXEC_DURATION;
2374 /* Get maximum number of iterations */
2375 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(env->cfg, "ats",
2376 "MLP_MAX_ITERATIONS", &max_iterations))
2378 max_iterations = MLP_MAX_ITERATIONS;
2381 /* Get diversity coefficient from configuration */
2382 mlp->pv.co_D = DEFAULT_D;
2383 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2384 "MLP_COEFFICIENT_D", &f_tmp))
2388 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2389 "MLP_COEFFICIENT_D", f_tmp);
2393 mlp->pv.co_D = f_tmp;
2394 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2395 "MLP_COEFFICIENT_D", f_tmp);
2399 /* Get relativity coefficient from configuration */
2400 mlp->pv.co_R = DEFAULT_R;
2401 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2402 "MLP_COEFFICIENT_R", &f_tmp))
2406 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2407 "MLP_COEFFICIENT_R", f_tmp);
2411 mlp->pv.co_R = f_tmp;
2412 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2413 "MLP_COEFFICIENT_R", f_tmp);
2418 /* Get utilization coefficient from configuration */
2419 mlp->pv.co_U = DEFAULT_U;
2420 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2421 "MLP_COEFFICIENT_U", &f_tmp))
2425 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2426 "MLP_COEFFICIENT_U", f_tmp);
2430 mlp->pv.co_U = f_tmp;
2431 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2432 "MLP_COEFFICIENT_U", f_tmp);
2436 /* Get quality metric coefficients from configuration */
2437 int i_delay = MLP_NaN;
2438 int i_distance = MLP_NaN;
2439 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
2440 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
2442 /* initialize quality coefficients with default value 1.0 */
2443 mlp->pv.co_Q[c] = DEFAULT_QUALITY;
2445 mlp->pv.q[c] = q[c];
2446 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
2448 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
2452 if ( (i_delay != MLP_NaN) &&
2453 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2454 "MLP_COEFFICIENT_QUALITY_DELAY", &tmp)) )
2455 mlp->pv.co_Q[i_delay] = (double) tmp / 100;
2457 mlp->pv.co_Q[i_delay] = DEFAULT_QUALITY;
2459 if ( (i_distance != MLP_NaN) &&
2460 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2461 "MLP_COEFFICIENT_QUALITY_DISTANCE", &tmp)) )
2462 mlp->pv.co_Q[i_distance] = (double) tmp / 100;
2464 mlp->pv.co_Q[i_distance] = DEFAULT_QUALITY;
2466 /* Get minimum bandwidth per used address from configuration */
2467 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2468 "MLP_MIN_BANDWIDTH",
2473 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2476 /* Get minimum number of connections from configuration */
2477 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2478 "MLP_MIN_CONNECTIONS",
2482 n_min = DEFAULT_MIN_CONNECTIONS;
2484 /* Init network quotas */
2485 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
2486 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2489 for (c2 = 0; c2 < env->network_count; c2++)
2491 if (quotas[c] == env->networks[c2])
2493 mlp->pv.quota_index[c] = env->networks[c2];
2494 mlp->pv.quota_out[c] = env->out_quota[c2];
2495 mlp->pv.quota_in[c] = env->in_quota[c2];
2498 LOG (GNUNET_ERROR_TYPE_INFO,
2499 "Quota for network `%s' (in/out) %llu/%llu\n",
2500 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2501 mlp->pv.quota_out[c],
2502 mlp->pv.quota_in[c]);
2508 /* Check if defined quota could make problem unsolvable */
2509 if ((n_min * b_min) > mlp->pv.quota_out[c])
2511 LOG (GNUNET_ERROR_TYPE_INFO,
2512 _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2513 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2514 mlp->pv.quota_out[c],
2516 mlp->pv.quota_out[c] = (n_min * b_min);
2518 if ((n_min * b_min) > mlp->pv.quota_in[c])
2520 LOG (GNUNET_ERROR_TYPE_INFO,
2521 _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2522 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2523 mlp->pv.quota_in[c],
2525 mlp->pv.quota_in[c] = (n_min * b_min);
2528 /* Check if bandwidth is too big to make problem solvable */
2529 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
2531 LOG (GNUNET_ERROR_TYPE_INFO,
2532 _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
2533 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2534 mlp->pv.quota_out[c],
2536 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
2538 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
2540 LOG (GNUNET_ERROR_TYPE_INFO, _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
2541 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2542 mlp->pv.quota_in[c],
2544 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
2547 if (GNUNET_NO == found)
2549 mlp->pv.quota_in[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2550 mlp->pv.quota_out[c] = ntohl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2551 LOG (GNUNET_ERROR_TYPE_INFO, _("Using default quota configuration for network `%s' (in/out) %llu/%llu\n"),
2552 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2553 mlp->pv.quota_in[c],
2554 mlp->pv.quota_out[c]);
2558 env->sf.s_add = &GAS_mlp_address_add;
2559 env->sf.s_address_update_property = &GAS_mlp_address_property_changed;
2560 env->sf.s_address_update_session = &GAS_mlp_address_session_changed;
2561 env->sf.s_address_update_inuse = &GAS_mlp_address_inuse_changed;
2562 env->sf.s_address_update_network = &GAS_mlp_address_change_network;
2563 env->sf.s_get = &GAS_mlp_get_preferred_address;
2564 env->sf.s_get_stop = &GAS_mlp_stop_get_preferred_address;
2565 env->sf.s_pref = &GAS_mlp_address_change_preference;
2566 env->sf.s_feedback = &GAS_mlp_address_preference_feedback;
2567 env->sf.s_del = &GAS_mlp_address_delete;
2568 env->sf.s_bulk_start = &GAS_mlp_bulk_start;
2569 env->sf.s_bulk_stop = &GAS_mlp_bulk_stop;
2572 /* Assign options to handle */
2573 mlp->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
2574 mlp->addresses = env->addresses;
2575 mlp->bw_changed_cb = env->bandwidth_changed_cb;
2576 mlp->bw_changed_cb_cls = env->bw_changed_cb_cls;
2577 mlp->get_preferences = env->get_preferences;
2578 mlp->get_preferences_cls = env->get_preference_cls;
2579 mlp->get_properties = env->get_property;
2580 mlp->get_properties_cls = env->get_property_cls;
2581 /* Setting MLP Input variables */
2583 mlp->pv.b_min = b_min;
2584 mlp->pv.n_min = n_min;
2585 mlp->pv.m_q = GNUNET_ATS_QualityPropertiesCount;
2586 mlp->stat_mlp_prob_changed = GNUNET_NO;
2587 mlp->stat_mlp_prob_updated = GNUNET_NO;
2588 mlp->opt_mlp_auto_solve = GNUNET_YES;
2589 mlp->requested_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2590 mlp->stat_bulk_requests = 0;
2591 mlp->stat_bulk_lock = 0;
2594 /* Redirect GLPK output to GNUnet logging */
2595 glp_term_hook (&mlp_term_hook, (void *) mlp);
2597 /* Init LP solving parameters */
2598 glp_init_smcp(&mlp->control_param_lp);
2599 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
2600 if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
2601 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
2603 mlp->control_param_lp.it_lim = max_iterations;
2604 mlp->control_param_lp.tm_lim = max_duration.rel_value_us / 1000LL;
2606 /* Init MLP solving parameters */
2607 glp_init_iocp(&mlp->control_param_mlp);
2608 /* Setting callback function */
2609 mlp->control_param_mlp.cb_func = &mlp_branch_and_cut_cb;
2610 mlp->control_param_mlp.cb_info = mlp;
2611 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
2612 mlp->control_param_mlp.mip_gap = mlp->pv.mip_gap;
2613 if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
2614 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
2615 mlp->control_param_mlp.tm_lim = max_duration.rel_value_us / 1000LL;
2617 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
2622 /* end of plugin_ats_mlp.c */