2 This file is part of GNUnet.
3 Copyright (C) 2011-2014 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 "gnunet_util_lib.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_ats_plugin.h"
31 #include "gnunet-service-ats_addresses.h"
32 #include "gnunet_statistics_service.h"
37 #define BIG_M_VALUE (UINT32_MAX) /10
38 #define BIG_M_STRING "unlimited"
40 #define MLP_AVERAGING_QUEUE_LENGTH 3
42 #define MLP_MAX_EXEC_DURATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
43 #define MLP_MAX_ITERATIONS 4096
45 #define MLP_DEFAULT_D 1.0
46 #define MLP_DEFAULT_R 1.0
47 #define MLP_DEFAULT_U 1.0
48 #define MLP_DEFAULT_QUALITY 1.0
49 #define MLP_DEFAULT_MIN_CONNECTIONS 4
50 #define MLP_DEFAULT_PEER_PREFERENCE 1.0
53 #define MLP_UNDEFINED 0
57 enum MLP_Output_Format
72 double lp_objective_value;
73 double mlp_objective_value;
88 struct GNUNET_PeerIdentity id;
90 /* Was this peer already added to the current problem? */
93 /* constraint 2: 1 address per peer*/
96 /* constraint 9: relativity */
99 /* Legacy preference value */
106 * GLPK (MLP) problem object
110 /* Number of addresses in problem */
111 unsigned int num_addresses;
112 /* Number of peers in problem */
113 unsigned int num_peers;
114 /* Number of elements in problem matrix */
115 unsigned int num_elements;
117 /* Row index constraint 2: */
119 /* Row index constraint 4: minimum connections */
121 /* Row index constraint 6: maximize diversity */
123 /* Row index constraint 8: utilization*/
125 /* Row index constraint 9: relativity*/
127 /* Row indices quality metrics */
128 int r_q[GNUNET_ATS_QualityPropertiesCount];
129 /* Row indices ATS network quotas */
130 int r_quota[GNUNET_ATS_NetworkTypeCount];
132 /* Column index Diversity (D) column */
134 /* Column index Utilization (U) column */
136 /* Column index Proportionality (R) column */
138 /* Column index quality metrics */
139 int c_q[GNUNET_ATS_QualityPropertiesCount];
144 /* Row index array */
146 /* Column index array */
148 /* Column index value */
155 /* Big M value for bandwidth capping */
164 /* ATS Quality metrics
166 * Array with GNUNET_ATS_QualityPropertiesCount elements
167 * contains mapping to GNUNET_ATS_Property*/
168 int q[GNUNET_ATS_QualityPropertiesCount];
170 /* Number of quality metrics */
173 /* Number of quality metrics */
176 /* Quality metric coefficients*/
177 double co_Q[GNUNET_ATS_QualityPropertiesCount];
179 /* Ressource costs coefficients*/
180 double co_RC[GNUNET_ATS_QualityPropertiesCount];
182 /* Diversity coefficient */
185 /* Utility coefficient */
188 /* Relativity coefficient */
191 /* Minimum bandwidth assigned to an address */
194 /* Minimum number of addresses with bandwidth assigned */
198 /* Array mapping array index to ATS network */
199 int quota_index[GNUNET_ATS_NetworkTypeCount];
200 /* Outbound quotas */
201 unsigned long long quota_out[GNUNET_ATS_NetworkTypeCount];
204 unsigned long long quota_in[GNUNET_ATS_NetworkTypeCount];
206 /* ATS ressource costs
207 * array with GNUNET_ATS_QualityPropertiesCount elements
208 * contains mapping to GNUNET_ATS_Property
210 int rc[GNUNET_ATS_QualityPropertiesCount];
217 struct GAS_MLP_Handle
219 struct GNUNET_ATS_PluginEnvironment *env;
222 * Exclude peer from next result propagation
224 const struct GNUNET_PeerIdentity *exclude_peer;
227 * Encapsulation for the MLP problem
229 struct MLP_Problem p;
232 * Encapsulation for the MLP problem variables
234 struct MLP_Variables pv;
237 * Encapsulation for the MLP solution
239 struct MLP_Solution ps;
247 * Number of changes while solver was locked
249 int stat_bulk_requests;
252 * GLPK LP control parameter
254 glp_smcp control_param_lp;
257 * GLPK LP control parameter
259 glp_iocp control_param_mlp;
262 * Peers with pending address requests
264 struct GNUNET_CONTAINER_MultiPeerMap *requested_peers;
267 * Was the problem updated since last solution
269 int stat_mlp_prob_updated;
272 * Has the problem size changed since last solution
274 int stat_mlp_prob_changed;
277 * Solve the problem automatically when updates occur?
278 * Default: GNUNET_YES
279 * Can be disabled for test and measurements
281 int opt_mlp_auto_solve;
284 * Write all MILP problems to a MPS file
286 int opt_dump_problem_all;
289 * Write all MILP problem solutions to a file
291 int opt_dump_solution_all;
294 * Write MILP problems to a MPS file when solver fails
296 int opt_dump_problem_on_fail;
299 * Write MILP problem solutions to a file when solver fails
301 int opt_dump_solution_on_fail;
304 * solve feasibility only
306 int opt_dbg_feasibility_only;
309 * solve autoscale the problem
311 int opt_dbg_autoscale_problem;
314 * use the intopt presolver instead of simplex
316 int opt_dbg_intopt_presolver;
321 int opt_dbg_glpk_verbose;
324 * solve autoscale the problem
326 int opt_dbg_optimize_relativity;
329 * solve autoscale the problem
331 int opt_dbg_optimize_diversity;
334 * solve autoscale the problem
336 int opt_dbg_optimize_quality;
339 * solve autoscale the problem
341 int opt_dbg_optimize_utility;
347 enum MLP_Output_Format opt_log_format;
351 * Address specific MLP information
353 struct MLP_information
357 * Bandwidth assigned outbound
362 * Bandwidth assigned inbound
372 * bandwidth column index
377 * address usage column
384 * constraint 1: bandwidth capping
389 * constraint 3: minimum bandwidth
398 * NOTE: Do not modify this documentation. This documentation is based on
399 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
400 * use build_txt.sh to generate plaintext output
402 * The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
403 * optimizing an mixed integer programming problem. The MLP solver uses a
404 * number of constraints to find the best adddress for a peer and an optimal
405 * bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
408 * We defined a constraint system to find an optimal bandwidth assignment.
409 * This constraint system uses as an input data addresses, bandwidth quotas,
410 * preferences and quality values. This constraint system is stored in an
411 * matrix based equotation system.
415 * A (M)LP problem consists of a target function to optimizes, constraints
416 * and rows and columns. FIXME GLP uses three arrays to index the matrix: two
417 * integer arrays storing the row and column indices in the matrix and an
418 * float array to store the coeeficient.
420 * To solve the problem we first find an initial solution for the LP problem
421 * using the LP solver and then find an MLP solution based on this solution
422 * using the MLP solver.
424 * Solving (M)LP problems has the property that finding an initial solution
425 * for the LP problem is computationally expensive and finding the MLP
426 * solution is cheaper. This is especially interesting an existing LP
427 * solution can be reused if only coefficients in the matrix have changed
428 * (addresses updated). Only when the problem size changes (addresses added
429 * or deleted) a new LP solution has to be found.
432 * The mlp solver solves the bandwidth assignment problem only on demand when
433 * an address suggestion is requested. When an address is requested mlp the
434 * solves the mlp problem and if the active address or the bandwidth assigned
435 * changes it calls the callback to addresses. The mlp solver gets notified
436 * about new addresses (adding sessions), removed addresses (address
437 * deletions) and address updates. To benefit from the mlp properties
438 * mentioned in section 5 the solver rembers if since the last solution
439 * addresses were added or deleted (problem size changed, problem has to be
440 * rebuild and solved from sratch) or if addresses were updated and the
441 * existing solution can be reused.
445 * The quotas for each network segment are passed by addresses. MLP can be
446 * adapted using configuration settings and uses the following parameters:
447 * * MLP_MAX_DURATION:
448 * Maximum duration for a MLP solution procees (default: 3 sec.)
449 * * MLP_MAX_ITERATIONS:
450 * Maximum number of iterations for a MLP solution process (default:
452 * * MLP_MIN_CONNECTIONS:
453 * Minimum number of desired connections (default: 4)
454 * * MLP_MIN_BANDWIDTH:
455 * Minimum amount of bandwidth assigned to an address (default: 1024)
456 * * MLP_COEFFICIENT_D:
457 * Diversity coefficient (default: 1.0)
458 * * MLP_COEFFICIENT_R:
459 * Relativity coefficient (default: 1.0)
460 * * MLP_COEFFICIENT_U:
461 * Utilization coefficient (default: 1.0)
462 * * MLP_COEFFICIENT_D:
463 * Diversity coefficient (default: 1.0)
464 * * MLP_COEFFICIENT_QUALITY_DELAY:
465 * Quality delay coefficient (default: 1.0)
466 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
467 * Quality distance coefficient (default: 1.0)
468 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
469 * Quality distance coefficient (default: 1.0)
470 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
471 * Quality distance coefficient (default: 1.0)
472 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
473 * Quality distance coefficient (default: 1.0)
475 * 5.2 Data structures used
477 * mlp has for each known peer a struct ATS_Peer containing information about
478 * a specific peer. The address field solver_information contains information
479 * about the mlp properties of this address.
483 * During initialization mlp initializes the GLPK libray used to solve the
484 * MLP problem: it initializes the glpk environment and creates an initial LP
485 * problem. Next it loads the configuration values from the configuration or
486 * uses the default values configured in -addresses_mlp.h. The quotas used
487 * are given by addresses but may have to be adjusted. mlp uses a upper limit
488 * for the bandwidth assigned called BIG M and a minimum amount of bandwidth
489 * an address gets assigned as well as a minium desired number of
490 * connections. If the configured quota is bigger than BIG M, it is reduced
491 * to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
492 * *MLP_MIN_BANDWIDTH it is increased to this value.
498 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
501 * Print debug output for mlp problem creation
503 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
507 * Intercept GLPK terminal output
508 * @param info the mlp handle
509 * @param s the string to print
510 * @return 0: glpk prints output on terminal, 0 != surpress output
513 mlp_term_hook (void *info, const char *s)
515 struct GAS_MLP_Handle *mlp = info;
517 if (mlp->opt_dbg_glpk_verbose)
518 LOG (GNUNET_ERROR_TYPE_ERROR, "%s", s);
524 * Reset peers for next problem creation
526 * @param cls not used
528 * @param value ATS_Peer
532 reset_peers (void *cls,
533 const struct GNUNET_PeerIdentity *key,
536 struct ATS_Peer *peer = value;
537 peer->processed = GNUNET_NO;
542 * Delete the MLP problem and free the constrain matrix
544 * @param mlp the MLP handle
547 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
552 if (mlp->p.prob != NULL)
554 glp_delete_prob(mlp->p.prob);
558 /* delete row index */
559 if (mlp->p.ia != NULL)
561 GNUNET_free (mlp->p.ia);
565 /* delete column index */
566 if (mlp->p.ja != NULL)
568 GNUNET_free (mlp->p.ja);
572 /* delete coefficients */
573 if (mlp->p.ar != NULL)
575 GNUNET_free (mlp->p.ar);
581 mlp->p.c_d = MLP_UNDEFINED;
582 mlp->p.c_r = MLP_UNDEFINED;
583 mlp->p.r_c2 = MLP_UNDEFINED;
584 mlp->p.r_c4 = MLP_UNDEFINED;
585 mlp->p.r_c6 = MLP_UNDEFINED;
586 mlp->p.r_c9 = MLP_UNDEFINED;
587 for (c = 0; c < mlp->pv.m_q ; c ++)
588 mlp->p.r_q[c] = MLP_UNDEFINED;
589 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c ++)
590 mlp->p.r_quota[c] = MLP_UNDEFINED;
591 mlp->p.ci = MLP_UNDEFINED;
594 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
600 * Translate glpk status error codes to text
601 * @param retcode return code
602 * @return string with result
605 mlp_status_to_string (int retcode)
609 return "solution is undefined";
611 return "solution is feasible";
613 return "solution is infeasible";
615 return "no feasible solution exists";
617 return "solution is optimal";
619 return "solution is unbounded";
622 return "unknown error";
628 * Translate glpk solver error codes to text
629 * @param retcode return code
630 * @return string with result
633 mlp_solve_to_string (int retcode)
639 return "invalid basis";
641 return "singular matrix";
643 return "ill-conditioned matrix";
645 return "invalid bounds";
647 return "solver failed";
649 return "objective lower limit reached";
651 return "objective upper limit reached";
653 return "iteration limit exceeded";
655 return "time limit exceeded";
657 return "no primal feasible solution";
659 return "no dual feasible solution";
661 return "root LP optimum not provided";
663 return "search terminated by application";
665 return "relative mip gap tolerance reached";
667 return "no dual feasible solution";
669 return "no convergence";
671 return "numerical instability";
673 return "invalid data";
675 return "result out of range";
678 return "unknown error";
683 * Extract an ATS performance info from an address
685 * @param address the address
686 * @param type the type to extract in HBO
687 * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
690 get_performance_info (struct ATS_Address *address, uint32_t type)
693 GNUNET_assert (NULL != address);
695 if ((NULL == address->atsi) || (0 == address->atsi_count))
696 return GNUNET_ATS_VALUE_UNDEFINED;
698 for (c1 = 0; c1 < address->atsi_count; c1++)
700 if (ntohl (address->atsi[c1].type) == type)
701 return ntohl (address->atsi[c1].value);
703 return GNUNET_ATS_VALUE_UNDEFINED;
709 const struct GNUNET_CONTAINER_MultiPeerMap *map;
714 mlp_create_problem_count_addresses_it (void *cls,
715 const struct GNUNET_PeerIdentity *key,
718 struct CountContext *cctx = cls;
720 /* Check if we have to add this peer due to a pending request */
721 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
728 mlp_create_problem_count_addresses (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
729 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
731 struct CountContext cctx;
733 cctx.map = requested_peers;
735 GNUNET_CONTAINER_multipeermap_iterate (addresses,
736 &mlp_create_problem_count_addresses_it, &cctx);
742 mlp_create_problem_count_peers_it (void *cls,
743 const struct GNUNET_PeerIdentity *key,
746 struct CountContext *cctx = cls;
748 /* Check if we have to addresses for the requested peer */
749 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
756 mlp_create_problem_count_peers (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
757 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
759 struct CountContext cctx;
761 cctx.map = addresses;
763 GNUNET_CONTAINER_multipeermap_iterate (requested_peers,
764 &mlp_create_problem_count_peers_it, &cctx);
770 * Updates an existing value in the matrix
772 * Extract the row, updates the value and updates the row in the problem
774 * @param p the mlp problem
775 * @param row the row to create the value in
776 * @param col the column to create the value in
777 * @param val the value to set
778 * @param line calling line for debbuging
779 * @return GNUNET_YES value changed, GNUNET_NO value did not change, GNUNET_SYSERR
783 mlp_create_problem_update_value (struct MLP_Problem *p,
784 int row, int col, double val,
795 GNUNET_assert (NULL != p);
796 GNUNET_assert (NULL != p->prob);
798 /* Get number of columns and prepare data structure */
799 c_cols = glp_get_num_cols(p->prob);
801 return GNUNET_SYSERR;
803 val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
804 GNUNET_assert (NULL != val_array);
805 ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
806 GNUNET_assert (NULL != ind_array);
807 /* Extract the row */
809 /* Update the value */
810 c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array);
812 for (c1 = 1; c1 < (c_elems+1); c1++)
814 if (ind_array[c1] == col)
820 if (GNUNET_NO == found)
822 ind_array[c_elems+1] = col;
823 val_array[c_elems+1] = val;
824 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Setting value in [%s : %s] to `%.2f'\n",
825 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
827 glp_set_mat_row (p->prob, row, c_elems+1, ind_array, val_array);
828 GNUNET_free (ind_array);
829 GNUNET_free (val_array);
835 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Updating value in [%s : %s] from `%.2f' to `%.2f'\n",
836 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
838 if (val != val_array[c1])
843 /* Update the row in the matrix */
844 glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
847 GNUNET_free (ind_array);
848 GNUNET_free (val_array);
853 * Creates a new value in the matrix
855 * Sets the row and column index in the problem array and increments the
858 * @param p the mlp problem
859 * @param row the row to create the value in
860 * @param col the column to create the value in
861 * @param val the value to set
862 * @param line calling line for debbuging
865 mlp_create_problem_set_value (struct MLP_Problem *p,
866 int row, int col, double val,
869 if ((p->ci) >= p->num_elements)
871 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
872 line, p->ci + 1, p->num_elements);
876 if ((0 == row) || (0 == col))
879 LOG (GNUNET_ERROR_TYPE_ERROR, "[P]: Invalid call from line %u: row = %u, col = %u\n",
885 #if DEBUG_MLP_PROBLEM_CREATION
886 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u == %.2f\n",
887 line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
893 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
894 unsigned int type, unsigned int bound, double lb, double ub,
897 int col = glp_add_cols (p->prob, 1);
898 glp_set_col_name (p->prob, col, name);
899 glp_set_col_bnds (p->prob, col, bound, lb, ub);
900 glp_set_col_kind (p->prob, col, type);
901 glp_set_obj_coef (p->prob, col, coef);
902 #if DEBUG_MLP_PROBLEM_CREATION
903 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
910 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
911 unsigned int bound, double lb, double ub)
914 int row = glp_add_rows (p->prob, 1);
916 glp_set_row_name (p->prob, row, name);
917 /* set row bounds: <= 0 */
918 glp_set_row_bnds (p->prob, row, bound, lb, ub);
922 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
925 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
928 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
931 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
934 GNUNET_asprintf(&op, "ERROR");
937 #if DEBUG_MLP_PROBLEM_CREATION
938 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
947 * - address columns b and n
948 * - address dependent constraint rows c1, c3
949 * - peer dependent rows c2 and c9
950 * - Set address dependent entries in problem matrix as well
953 mlp_create_problem_add_address_information (void *cls,
954 const struct GNUNET_PeerIdentity *key,
957 struct GAS_MLP_Handle *mlp = cls;
958 struct MLP_Problem *p = &mlp->p;
959 struct ATS_Address *address = value;
960 struct ATS_Peer *peer;
961 struct MLP_information *mlpi;
967 uint32_t addr_net_index;
968 unsigned long long max_quota;
971 /* Check if we have to add this peer due to a pending request */
972 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(mlp->requested_peers, key))
975 mlpi = address->solver_information;
978 fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
983 addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
984 for (addr_net_index = 0; addr_net_index < GNUNET_ATS_NetworkTypeCount; addr_net_index++)
986 if (mlp->pv.quota_index[addr_net_index] == addr_net)
990 if (addr_net_index >= GNUNET_ATS_NetworkTypeCount)
997 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
999 if (mlp->pv.quota_out[c] > max_quota)
1000 max_quota = mlp->pv.quota_out[c];
1001 if (mlp->pv.quota_in[c] > max_quota)
1002 max_quota = mlp->pv.quota_in[c];
1004 if (max_quota > mlp->pv.BIG_M)
1005 cur_bigm = (double) mlp->pv.BIG_M;
1007 cur_bigm = max_quota;
1011 peer = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, key);
1012 GNUNET_assert (NULL != peer);
1013 if (peer->processed == GNUNET_NO)
1015 /* Add peer dependent constraints */
1016 /* Add c2) One address active per peer */
1017 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
1018 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
1020 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1022 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1024 /* Add c9) Relativity */
1025 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
1026 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
1028 /* c9) set coefficient */
1029 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f , __LINE__);
1032 peer->processed = GNUNET_YES;
1035 /* Reset addresses' solver information */
1042 /* Add bandwidth column */
1043 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
1044 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1046 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
1050 /* Maximize for bandwidth assignment in feasibility testing */
1051 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
1055 /* Add address active column */
1056 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
1057 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
1060 /* Add address dependent constraints */
1061 /* Add c1) bandwidth capping: b_t + (-M) * n_t <= 0 */
1062 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
1063 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
1065 /* c1) set b = 1 coefficient */
1066 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
1067 /* c1) set n = - min (M, quota) coefficient */
1068 cur_bigm = (double) mlp->pv.quota_out[addr_net_index];
1069 if (cur_bigm > mlp->pv.BIG_M)
1070 cur_bigm = (double) mlp->pv.BIG_M;
1071 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -cur_bigm, __LINE__);
1073 /* Add constraint c 3) minimum bandwidth
1074 * b_t + (-n_t * b_min) >= 0
1076 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
1077 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
1080 /* c3) set b = 1 coefficient */
1081 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
1082 /* c3) set n = -b_min coefficient */
1083 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
1086 /* Set coefficient entries in invariant rows */
1090 /* c 4) minimum connections */
1091 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
1092 /* c 2) 1 address peer peer */
1093 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
1094 /* c 10) obey network specific quotas
1095 * (1)*b_1 + ... + (1)*b_m <= quota_n
1097 mlp_create_problem_set_value (p, p->r_quota[addr_net_index], mlpi->c_b, 1, __LINE__);
1100 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1102 /* c 6) maximize diversity */
1103 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
1104 /* c 9) relativity */
1105 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1106 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
1108 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1109 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
1110 /* c 7) Optimize quality */
1111 /* For all quality metrics, set quality of this address */
1112 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1114 props = mlp->env->get_property (mlp->env->cls, address);
1115 for (c = 0; c < mlp->pv.m_q; c++)
1117 if ((props[c] < 1.0) && (props[c] > 2.0))
1119 fprintf (stderr, "PROP == %.3f \t ", props[c]);
1122 mlp_create_problem_set_value (p, p->r_q[c], mlpi->c_b, props[c], __LINE__);
1132 * Create the invariant columns c4, c6, c10, c8, c7
1135 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
1141 /* Row for c4) minimum connection */
1142 /* Number of minimum connections is min(|Peers|, n_min) */
1143 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);
1145 /* Rows for c 10) Enforce network quotas */
1146 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1149 GNUNET_asprintf(&text, "c10_quota_ats_%s",
1150 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
1151 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
1156 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1159 /* Add row for c6) Maximize for diversity */
1160 if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
1162 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
1163 /* Set c6 ) Setting -D */
1164 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
1167 /* Adding rows for c 8) Maximize utility */
1168 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1170 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
1172 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
1175 /* For all quality metrics:
1176 * c 7) Maximize quality, austerity */
1177 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1179 for (c = 0; c < mlp->pv.m_q; c++)
1181 GNUNET_asprintf (&name, "c7_q%i_%s", c,
1182 GNUNET_ATS_print_property_type (mlp->pv.q[c]));
1183 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
1185 mlp_create_problem_set_value (p, p->r_q[c], p->c_q[c], -1, __LINE__);
1193 * Create the invariant columns d, u, r, q0 ... qm
1196 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
1198 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1203 /* Diversity d column */
1204 if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
1205 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
1207 /* Utilization u column */
1208 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1209 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
1211 /* Relativity r column */
1212 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1213 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
1215 /* Quality metric columns */
1216 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1218 for (c = 0; c < mlp->pv.m_q; c++)
1220 GNUNET_asprintf (&name, "q_%u", mlp->pv.q[c]);
1221 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
1230 * Create the MLP problem
1232 * @param mlp the MLP handle
1233 * @return #GNUNET_OK or #GNUNET_SYSERR
1236 mlp_create_problem (struct GAS_MLP_Handle *mlp)
1238 struct MLP_Problem *p = &mlp->p;
1239 int res = GNUNET_OK;
1241 GNUNET_assert (p->prob == NULL);
1242 GNUNET_assert (p->ia == NULL);
1243 GNUNET_assert (p->ja == NULL);
1244 GNUNET_assert (p->ar == NULL);
1245 /* Reset MLP problem struct */
1247 /* create the glpk problem */
1248 p->prob = glp_create_prob ();
1249 GNUNET_assert (NULL != p->prob);
1250 p->num_peers = mlp_create_problem_count_peers (mlp->requested_peers, mlp->env->addresses);
1251 p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers,
1252 mlp->env->addresses);
1254 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
1255 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses +
1256 mlp->pv.m_q + p->num_peers + 2 + 1);
1257 LOG (GNUNET_ERROR_TYPE_DEBUG,
1258 "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
1264 /* Set a problem name */
1265 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
1266 /* Set optimization direction to maximize */
1267 glp_set_obj_dir (p->prob, GLP_MAX);
1269 /* Create problem matrix */
1270 /* last +1 caused by glpk index starting with one: [1..elements]*/
1273 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
1275 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
1277 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
1279 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
1281 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
1282 return GNUNET_SYSERR;
1285 /* Adding invariant columns */
1286 mlp_create_problem_add_invariant_columns (mlp, p);
1288 /* Adding address independent constraint rows */
1289 mlp_create_problem_add_invariant_rows (mlp, p);
1291 /* Adding address dependent columns constraint rows */
1292 GNUNET_CONTAINER_multipeermap_iterate (mlp->env->addresses,
1293 &mlp_create_problem_add_address_information,
1296 /* Load the matrix */
1297 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
1298 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
1299 if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
1301 glp_scale_prob (p->prob, GLP_SF_AUTO);
1309 * Solves the LP problem
1311 * @param mlp the MLP Handle
1312 * @return #GNUNET_OK if could be solved, #GNUNET_SYSERR on failure
1315 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
1319 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
1321 LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: %s\n",
1322 mlp_solve_to_string (res));
1324 LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem failed: %s\n",
1325 mlp_solve_to_string (res));
1327 /* Analyze problem status */
1328 res_status = glp_get_status (mlp->p.prob);
1329 switch (res_status) {
1330 case GLP_OPT: /* solution is optimal */
1331 LOG (GNUNET_ERROR_TYPE_INFO,
1332 "Solving LP problem: %s, %s\n",
1333 mlp_solve_to_string(res),
1334 mlp_status_to_string(res_status));
1337 LOG (GNUNET_ERROR_TYPE_ERROR,
1338 "Solving LP problem failed: %s %s\n",
1339 mlp_solve_to_string(res),
1340 mlp_status_to_string(res_status));
1341 return GNUNET_SYSERR;
1347 * Propagates the results when MLP problem was solved
1349 * @param cls the MLP handle
1350 * @param key the peer identity
1351 * @param value the address
1352 * @return #GNUNET_OK to continue
1355 mlp_propagate_results (void *cls,
1356 const struct GNUNET_PeerIdentity *key,
1359 struct GAS_MLP_Handle *mlp = cls;
1360 struct ATS_Address *address;
1361 struct MLP_information *mlpi;
1362 double mlp_bw_in = MLP_NaN;
1363 double mlp_bw_out = MLP_NaN;
1364 double mlp_use = MLP_NaN;
1366 /* Check if we have to add this peer due to a pending request */
1367 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mlp->requested_peers,
1373 GNUNET_assert (address->solver_information != NULL);
1374 mlpi = address->solver_information;
1376 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
1377 if (mlp_bw_in > (double) UINT32_MAX)
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
1380 mlp_bw_in = (double) UINT32_MAX;
1382 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
1383 if (mlp_bw_out > (double) UINT32_MAX)
1385 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
1386 mlp_bw_out = (double) UINT32_MAX;
1388 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
1392 * LOG (GNUNET_ERROR_TYPE_INFO, "MLP result address: `%s' `%s' length %u session %u, mlp use %.3f\n",
1393 * GNUNET_i2s(&address->peer), address->plugin,
1394 * address->addr_len, address->session_id);
1397 if (GLP_YES == mlp_use)
1399 /* This address was selected by the solver to be used */
1400 mlpi->n = GNUNET_YES;
1401 if (GNUNET_NO == address->active)
1403 /* Address was not used before, enabling address */
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n",
1405 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1406 address->active = GNUNET_YES;
1407 address->assigned_bw_in = mlp_bw_in;
1408 mlpi->b_in = mlp_bw_in;
1409 address->assigned_bw_out = mlp_bw_out;
1410 mlpi->b_out = mlp_bw_out;
1411 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1412 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
1415 else if (GNUNET_YES == address->active)
1417 /* Address was used before, check for bandwidth change */
1418 if ((mlp_bw_out != address->assigned_bw_out) ||
1419 (mlp_bw_in != address->assigned_bw_in))
1421 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n",
1422 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1423 address->assigned_bw_in = mlp_bw_in;
1424 mlpi->b_in = mlp_bw_in;
1425 address->assigned_bw_out = mlp_bw_out;
1426 mlpi->b_out = mlp_bw_out;
1427 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1428 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
1435 else if (GLP_NO == mlp_use)
1437 /* This address was selected by the solver to be not used */
1438 mlpi->n = GNUNET_NO;
1439 if (GNUNET_NO == address->active)
1441 /* Address was not used before, nothing to do */
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n",
1443 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1446 else if (GNUNET_YES == address->active)
1448 /* Address was used before, disabling address */
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n",
1450 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1451 address->active = GNUNET_NO;
1452 /* Set bandwidth to 0 */
1453 address->assigned_bw_in = 0;
1455 address->assigned_bw_out = 0;
1470 notify (struct GAS_MLP_Handle *mlp,
1471 enum GAS_Solver_Operation op,
1472 enum GAS_Solver_Status stat,
1473 enum GAS_Solver_Additional_Information add)
1475 if (NULL != mlp->env->info_cb)
1476 mlp->env->info_cb (mlp->env->cls, op, stat, add);
1481 mlp_branch_and_cut_cb (glp_tree *tree, void *info)
1483 struct GAS_MLP_Handle *mlp = info;
1486 switch (glp_ios_reason (tree))
1489 /* Do nothing here */
1492 /* Do nothing here */
1495 /* Do nothing here */
1498 /* Do nothing here */
1501 /* Do nothing here */
1504 /* Do nothing here */
1507 /* A better solution was found */
1508 mlp->ps.mlp_gap = glp_ios_mip_gap (tree);
1509 mlp_obj = glp_mip_obj_val (mlp->p.prob);
1510 mlp->ps.lp_mlp_gap = (abs(mlp_obj - mlp->ps.lp_objective_value)) / (abs(mlp_obj) + DBL_EPSILON);
1512 LOG (GNUNET_ERROR_TYPE_INFO,
1513 "Found better integer solution, current gaps: %.3f <= %.3f, %.3f <= %.3f\n",
1514 mlp->ps.mlp_gap, mlp->pv.mip_gap,
1515 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1517 if (mlp->ps.mlp_gap <= mlp->pv.mip_gap)
1519 LOG (GNUNET_ERROR_TYPE_INFO,
1520 "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
1521 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1522 glp_ios_terminate (tree);
1525 if (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap)
1527 LOG (GNUNET_ERROR_TYPE_INFO,
1528 "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
1529 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1530 glp_ios_terminate (tree);
1542 * Solves the MLP problem
1544 * @param solver the MLP Handle
1545 * @return #GNUNET_OK if could be solved, #GNUNET_SYSERR on failure
1548 GAS_mlp_solve_problem (void *solver)
1550 struct GAS_MLP_Handle *mlp = solver;
1556 struct GNUNET_TIME_Absolute start_total;
1557 struct GNUNET_TIME_Absolute start_cur_op;
1558 struct GNUNET_TIME_Relative dur_total;
1559 struct GNUNET_TIME_Relative dur_setup;
1560 struct GNUNET_TIME_Relative dur_lp;
1561 struct GNUNET_TIME_Relative dur_mlp;
1563 GNUNET_assert(NULL != solver);
1565 if (GNUNET_YES == mlp->stat_bulk_lock)
1567 mlp->stat_bulk_requests++;
1570 notify(mlp, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS,
1571 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1572 start_total = GNUNET_TIME_absolute_get();
1574 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->requested_peers))
1576 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1577 return GNUNET_OK; /* No pending requests */
1579 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->env->addresses))
1581 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1582 return GNUNET_OK; /* No addresses available */
1585 if ((GNUNET_NO == mlp->stat_mlp_prob_changed)
1586 && (GNUNET_NO == mlp->stat_mlp_prob_updated))
1588 LOG(GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
1589 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1592 if (GNUNET_YES == mlp->stat_mlp_prob_changed)
1594 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
1595 notify(mlp, GAS_OP_SOLVE_SETUP_START, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1596 mlp_delete_problem(mlp);
1597 if (GNUNET_SYSERR == mlp_create_problem(mlp))
1599 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_FAIL, GAS_INFO_FULL);
1600 return GNUNET_SYSERR;
1602 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1603 if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
1605 mlp->control_param_lp.presolve = GLP_YES; /* LP presolver, we need lp solution */
1606 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
1610 mlp->control_param_lp.presolve = GNUNET_NO; /* LP presolver, we need lp solution */
1611 mlp->control_param_mlp.presolve = GLP_YES; /* No presolver, we have LP solution */
1612 dur_lp = GNUNET_TIME_UNIT_ZERO;
1617 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
1620 /* Reset solution info */
1621 mlp->ps.lp_objective_value = 0.0;
1622 mlp->ps.mlp_gap = 1.0;
1623 mlp->ps.mlp_objective_value = 0.0;
1624 mlp->ps.lp_mlp_gap = 0.0;
1626 dur_setup = GNUNET_TIME_absolute_get_duration (start_total);
1629 if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
1631 notify(mlp, GAS_OP_SOLVE_MLP_LP_START, GAS_STAT_SUCCESS,
1632 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1633 LOG(GNUNET_ERROR_TYPE_DEBUG,
1634 "Running LP solver %s\n",
1635 (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
1636 start_cur_op = GNUNET_TIME_absolute_get();
1639 /* Only for debugging:
1640 * Always use LP presolver:
1641 * mlp->control_param_lp.presolve = GLP_YES; */
1642 res_lp = mlp_solve_lp_problem(mlp);
1643 if (GNUNET_OK == res_lp)
1645 mlp->ps.lp_objective_value = glp_get_obj_val (mlp->p.prob);
1646 LOG (GNUNET_ERROR_TYPE_DEBUG,
1647 "LP solution was: %.3f\n",
1648 mlp->ps.lp_objective_value);
1651 dur_lp = GNUNET_TIME_absolute_get_duration (start_cur_op);
1652 notify(mlp, GAS_OP_SOLVE_MLP_LP_STOP,
1653 (GNUNET_OK == res_lp) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1654 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1657 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
1660 /* Run MLP solver */
1661 if ((GNUNET_OK == res_lp) || (GNUNET_YES == mlp->opt_dbg_intopt_presolver))
1663 LOG(GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
1664 notify(mlp, GAS_OP_SOLVE_MLP_MLP_START, GAS_STAT_SUCCESS,
1665 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1666 start_cur_op = GNUNET_TIME_absolute_get();
1670 /* Only for debugging, always use LP presolver */
1671 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
1672 mlp->control_param_mlp.presolve = GNUNET_YES;
1674 mip_res = glp_intopt (mlp->p.prob, &mlp->control_param_mlp);
1679 LOG (GNUNET_ERROR_TYPE_INFO,
1680 "Solving MLP problem: %s\n",
1681 mlp_solve_to_string (mip_res));
1683 case GLP_ETMLIM: /* Time limit reached */
1684 case GLP_EMIPGAP: /* MIP gap tolerance limit reached */
1685 case GLP_ESTOP: /* Solver was instructed to stop*/
1686 /* Semi-successful */
1687 LOG (GNUNET_ERROR_TYPE_INFO,
1688 "Solving MLP problem solution was interupted: %s\n",
1689 mlp_solve_to_string (mip_res));
1698 LOG (GNUNET_ERROR_TYPE_INFO,
1699 "Solving MLP problem failed: %s\n",
1700 mlp_solve_to_string (mip_res));
1704 /* Analyze problem status */
1705 mip_status = glp_mip_status(mlp->p.prob);
1708 case GLP_OPT: /* solution is optimal */
1709 LOG (GNUNET_ERROR_TYPE_WARNING,
1710 "Solution of MLP problem is optimal: %s, %s\n",
1711 mlp_solve_to_string (mip_res),
1712 mlp_status_to_string (mip_status));
1713 mip_res = GNUNET_OK;
1715 case GLP_FEAS: /* solution is feasible but not proven optimal */
1717 if ( (mlp->ps.mlp_gap <= mlp->pv.mip_gap) ||
1718 (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap) )
1720 LOG (GNUNET_ERROR_TYPE_INFO,
1721 "Solution of MLP problem is feasible and solution within gap constraints: %s, %s\n",
1722 mlp_solve_to_string (mip_res),
1723 mlp_status_to_string (mip_status));
1724 mip_res = GNUNET_OK;
1728 LOG (GNUNET_ERROR_TYPE_WARNING,
1729 "Solution of MLP problem is feasible but solution not within gap constraints: %s, %s\n",
1730 mlp_solve_to_string (mip_res),
1731 mlp_status_to_string (mip_status));
1732 mip_res = GNUNET_SYSERR;
1735 case GLP_UNDEF: /* Solution undefined */
1736 case GLP_NOFEAS: /* No feasible solution */
1738 LOG (GNUNET_ERROR_TYPE_ERROR,
1739 "Solving MLP problem failed: %s %s\n",
1740 mlp_solve_to_string (mip_res),
1741 mlp_status_to_string (mip_status));
1742 mip_res = GNUNET_SYSERR;
1746 dur_mlp = GNUNET_TIME_absolute_get_duration (start_cur_op);
1747 dur_total = GNUNET_TIME_absolute_get_duration (start_total);
1749 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP,
1750 (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1751 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1755 /* Do not execute mip solver since lp solution is invalid */
1756 dur_mlp = GNUNET_TIME_UNIT_ZERO;
1757 dur_total = GNUNET_TIME_absolute_get_duration (start_total);
1759 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP, GAS_STAT_FAIL,
1760 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1761 mip_res = GNUNET_SYSERR;
1764 /* Notify about end */
1765 notify(mlp, GAS_OP_SOLVE_STOP,
1766 ((GNUNET_OK == mip_res) && (GNUNET_OK == mip_res)) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1767 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1769 LOG (GNUNET_ERROR_TYPE_DEBUG,
1770 "Execution time for %s solve: (total/setup/lp/mlp) : %llu %llu %llu %llu\n",
1771 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? "full" : "updated",
1772 (unsigned long long) dur_total.rel_value_us,
1773 (unsigned long long) dur_setup.rel_value_us,
1774 (unsigned long long) dur_lp.rel_value_us,
1775 (unsigned long long) dur_mlp.rel_value_us);
1778 mlp->ps.lp_res = res_lp;
1779 mlp->ps.mip_res = mip_res;
1780 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1781 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1782 mlp->ps.p_cols = glp_get_num_cols(mlp->p.prob);
1783 mlp->ps.p_rows = glp_get_num_rows(mlp->p.prob);
1784 mlp->ps.p_elements = mlp->p.num_elements;
1786 /* Propagate result*/
1787 notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1788 (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1790 if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
1792 GNUNET_CONTAINER_multipeermap_iterate(mlp->env->addresses,
1793 &mlp_propagate_results, mlp);
1795 notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1796 (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1799 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1800 if ( (GNUNET_YES == mlp->opt_dump_problem_all) ||
1801 (mlp->opt_dump_problem_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
1803 /* Write problem to disk */
1804 switch (mlp->opt_log_format) {
1806 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.cplex", mlp->p.num_peers,
1807 mlp->p.num_addresses, time.abs_value_us);
1808 glp_write_lp (mlp->p.prob, NULL, filename);
1811 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.glpk", mlp->p.num_peers,
1812 mlp->p.num_addresses, time.abs_value_us);
1813 glp_write_prob (mlp->p.prob, 0, filename);
1816 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers,
1817 mlp->p.num_addresses, time.abs_value_us);
1818 glp_write_mps (mlp->p.prob, GLP_MPS_FILE, NULL, filename);
1823 LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped problem to file: `%s' \n", filename);
1824 GNUNET_free(filename);
1826 if ( (mlp->opt_dump_solution_all) ||
1827 (mlp->opt_dump_solution_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
1829 /* Write solution to disk */
1830 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers,
1831 mlp->p.num_addresses, time.abs_value_us);
1832 glp_print_mip(mlp->p.prob, filename);
1833 LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped solution to file: `%s' \n", filename);
1834 GNUNET_free(filename);
1837 /* Reset change and update marker */
1838 mlp->control_param_lp.presolve = GLP_NO;
1839 mlp->stat_mlp_prob_updated = GNUNET_NO;
1840 mlp->stat_mlp_prob_changed = GNUNET_NO;
1842 if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
1845 return GNUNET_SYSERR;
1849 * Add a single address to the solve
1851 * @param solver the solver Handle
1852 * @param address the address to add
1853 * @param network network type of this address
1856 GAS_mlp_address_add (void *solver,
1857 struct ATS_Address *address,
1860 struct GAS_MLP_Handle *mlp = solver;
1862 GNUNET_assert (NULL != solver);
1863 GNUNET_assert (NULL != address);
1865 if (GNUNET_ATS_NetworkTypeCount <= network)
1871 if (NULL == address->solver_information)
1873 address->solver_information = GNUNET_new (struct MLP_information);
1876 LOG (GNUNET_ERROR_TYPE_ERROR,
1877 _("Adding address for peer `%s' multiple times\n"),
1878 GNUNET_i2s(&address->peer));
1880 /* Is this peer included in the problem? */
1882 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1885 /* FIXME: should this be an error? */
1886 LOG (GNUNET_ERROR_TYPE_DEBUG,
1887 "Adding address for peer `%s' without address request\n",
1888 GNUNET_i2s(&address->peer));
1892 LOG (GNUNET_ERROR_TYPE_DEBUG,
1893 "Adding address for peer `%s' with address request \n",
1894 GNUNET_i2s(&address->peer));
1895 /* Problem size changed: new address for peer with pending request */
1896 mlp->stat_mlp_prob_changed = GNUNET_YES;
1897 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1898 GAS_mlp_solve_problem (solver);
1903 * Transport properties for this address have changed
1905 * @param solver solver handle
1906 * @param address the address
1907 * @param type the ATSI type in HBO
1908 * @param abs_value the absolute value of the property
1909 * @param rel_value the normalized value
1912 GAS_mlp_address_property_changed (void *solver,
1913 struct ATS_Address *address,
1918 struct MLP_information *mlpi = address->solver_information;
1919 struct GAS_MLP_Handle *mlp = solver;
1923 GNUNET_assert (NULL != solver);
1924 GNUNET_assert (NULL != address);
1928 LOG (GNUNET_ERROR_TYPE_INFO,
1929 _("Updating address property `%s' for peer `%s' %p not added before\n"),
1930 GNUNET_ATS_print_property_type (type),
1931 GNUNET_i2s(&address->peer),
1938 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1941 /* Peer is not requested, so no need to update problem */
1944 LOG (GNUNET_ERROR_TYPE_INFO, "Updating property `%s' address for peer `%s' to abs %llu rel %.3f\n",
1945 GNUNET_ATS_print_property_type (type),
1946 GNUNET_i2s(&address->peer),
1950 if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
1953 /* Find row index */
1955 for (c1 = 0; c1 < mlp->pv.m_q; c1++)
1957 if (type == mlp->pv.q[c1])
1963 if (-1 == type_index)
1966 return; /* quality index not found */
1969 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index] */
1970 if (GNUNET_YES == mlp_create_problem_update_value (&mlp->p,
1971 mlp->p.r_q[type_index], mlpi->c_b, rel_value, __LINE__))
1973 mlp->stat_mlp_prob_updated = GNUNET_YES;
1974 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1975 GAS_mlp_solve_problem (solver);
1982 * Find the active address in the set of addresses of a peer
1983 * @param cls destination
1984 * @param key peer id
1985 * @param value address
1986 * @return #GNUNET_OK
1989 mlp_get_preferred_address_it (void *cls,
1990 const struct GNUNET_PeerIdentity *key,
1993 static int counter = 0;
1994 struct ATS_Address **aa = cls;
1995 struct ATS_Address *addr = value;
1996 struct MLP_information *mlpi = addr->solver_information;
2003 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004 * "MLP [%u] Peer `%s' %s length %u session %u active %s mlp active %s\n",
2005 * counter, GNUNET_i2s (&addr->peer), addr->plugin, addr->addr_len, addr->session_id,
2006 * (GNUNET_YES == addr->active) ? "active" : "inactive",
2007 * (GNUNET_YES == mlpi->n) ? "active" : "inactive");
2010 if (GNUNET_YES == mlpi->n)
2014 (*aa)->assigned_bw_in = mlpi->b_in;
2015 (*aa)->assigned_bw_out = mlpi->b_out;
2024 get_peer_pref_value (struct GAS_MLP_Handle *mlp,
2025 const struct GNUNET_PeerIdentity *peer)
2028 const double *preferences = NULL;
2031 preferences = mlp->env->get_preferences (mlp->env->cls, peer);
2033 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
2035 if (c != GNUNET_ATS_PREFERENCE_END)
2037 /* fprintf (stderr, "VALUE[%u] %s %.3f \n",
2038 * c, GNUNET_i2s (&cur->addr->peer), t[c]); */
2039 res += preferences[c];
2043 res /= (GNUNET_ATS_PreferenceCount -1);
2046 LOG (GNUNET_ERROR_TYPE_DEBUG,
2047 "Peer preference for peer `%s' == %.2f\n",
2048 GNUNET_i2s(peer), res);
2055 * Get the preferred address for a specific peer
2057 * @param solver the MLP Handle
2058 * @param peer the peer
2059 * @return suggested address
2061 static const struct ATS_Address *
2062 GAS_mlp_get_preferred_address (void *solver,
2063 const struct GNUNET_PeerIdentity *peer)
2065 struct GAS_MLP_Handle *mlp = solver;
2067 struct ATS_Address *res;
2069 GNUNET_assert (NULL != solver);
2070 GNUNET_assert (NULL != peer);
2072 LOG (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n",
2075 /* Is this peer included in the problem? */
2077 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
2080 LOG (GNUNET_ERROR_TYPE_INFO, "Adding peer `%s' to list of requested_peers with requests\n",
2083 p = GNUNET_new (struct ATS_Peer);
2085 p->f = get_peer_pref_value (mlp, peer);
2086 GNUNET_CONTAINER_multipeermap_put (mlp->requested_peers,
2088 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2090 /* Added new peer, we have to rebuild problem before solving */
2091 mlp->stat_mlp_prob_changed = GNUNET_YES;
2093 if ((GNUNET_YES == mlp->opt_mlp_auto_solve)&&
2094 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(mlp->env->addresses,
2097 mlp->exclude_peer = peer;
2098 GAS_mlp_solve_problem (mlp);
2099 mlp->exclude_peer = NULL;
2102 /* Get prefered address */
2104 GNUNET_CONTAINER_multipeermap_get_multiple (mlp->env->addresses, peer,
2105 &mlp_get_preferred_address_it, &res);
2111 * Deletes a single address in the MLP problem
2113 * The MLP problem has to be recreated and the problem has to be resolved
2115 * @param solver the MLP Handle
2116 * @param address the address to delete
2117 * @param session_only delete only session not whole address
2120 GAS_mlp_address_delete (void *solver,
2121 struct ATS_Address *address,
2124 struct GAS_MLP_Handle *mlp = solver;
2125 struct MLP_information *mlpi;
2128 GNUNET_assert (NULL != solver);
2129 GNUNET_assert (NULL != address);
2131 mlpi = address->solver_information;
2132 if ((GNUNET_NO == session_only) && (NULL != mlpi))
2134 /* Remove full address */
2136 address->solver_information = NULL;
2138 was_active = address->active;
2139 address->active = GNUNET_NO;
2140 address->assigned_bw_in = 0;
2141 address->assigned_bw_out = 0;
2143 /* Is this peer included in the problem? */
2145 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
2148 LOG (GNUNET_ERROR_TYPE_INFO,
2149 "Deleting %s for peer `%s' without address request \n",
2150 (session_only == GNUNET_YES) ? "session" : "address",
2151 GNUNET_i2s(&address->peer));
2154 LOG (GNUNET_ERROR_TYPE_INFO, "Deleting %s for peer `%s' with address request \n",
2155 (session_only == GNUNET_YES) ? "session" : "address",
2156 GNUNET_i2s(&address->peer));
2158 /* Problem size changed: new address for peer with pending request */
2159 mlp->stat_mlp_prob_changed = GNUNET_YES;
2160 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2162 GAS_mlp_solve_problem (solver);
2164 if (GNUNET_YES == was_active)
2166 if (NULL == GAS_mlp_get_preferred_address (solver, &address->peer))
2168 /* No alternative address, disconnecting peer */
2169 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
2178 * Start a bulk operation
2180 * @param solver the solver
2183 GAS_mlp_bulk_start (void *solver)
2185 struct GAS_MLP_Handle *s = solver;
2187 LOG (GNUNET_ERROR_TYPE_DEBUG,
2188 "Locking solver for bulk operation ...\n");
2189 GNUNET_assert (NULL != solver);
2190 s->stat_bulk_lock ++;
2195 GAS_mlp_bulk_stop (void *solver)
2197 struct GAS_MLP_Handle *s = solver;
2199 LOG (GNUNET_ERROR_TYPE_DEBUG,
2200 "Unlocking solver from bulk operation ...\n");
2201 GNUNET_assert (NULL != solver);
2203 if (s->stat_bulk_lock < 1)
2208 s->stat_bulk_lock --;
2210 if (0 < s->stat_bulk_requests)
2212 GAS_mlp_solve_problem (solver);
2213 s->stat_bulk_requests= 0;
2220 * Stop notifying about address and bandwidth changes for this peer
2222 * @param solver the MLP handle
2223 * @param peer the peer
2226 GAS_mlp_stop_get_preferred_address (void *solver,
2227 const struct GNUNET_PeerIdentity *peer)
2229 struct GAS_MLP_Handle *mlp = solver;
2230 struct ATS_Peer *p = NULL;
2232 GNUNET_assert (NULL != solver);
2233 GNUNET_assert (NULL != peer);
2234 if (NULL != (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
2236 GNUNET_assert (GNUNET_YES ==
2237 GNUNET_CONTAINER_multipeermap_remove (mlp->requested_peers, peer, p));
2240 mlp->stat_mlp_prob_changed = GNUNET_YES;
2241 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2243 GAS_mlp_solve_problem (solver);
2250 * Changes the preferences for a peer in the MLP problem
2252 * @param solver the MLP Handle
2253 * @param peer the peer
2254 * @param kind the kind to change the preference
2255 * @param pref_rel the relative score
2258 GAS_mlp_address_change_preference (void *solver,
2259 const struct GNUNET_PeerIdentity *peer,
2260 enum GNUNET_ATS_PreferenceKind kind,
2263 struct GAS_MLP_Handle *mlp = solver;
2266 LOG (GNUNET_ERROR_TYPE_DEBUG,
2267 "Changing preference for address for peer `%s' to %.2f\n",
2271 GNUNET_STATISTICS_update (mlp->env->stats,
2272 "# LP address preference changes", 1, GNUNET_NO);
2273 /* Update the constraints with changed preferences */
2277 /* Update relativity constraint c9 */
2278 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
2280 LOG (GNUNET_ERROR_TYPE_INFO,
2281 "Updating preference for unknown peer `%s'\n",
2286 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
2288 p->f = get_peer_pref_value (mlp, peer);
2289 mlp_create_problem_update_value (&mlp->p,
2295 /* Problem size changed: new address for peer with pending request */
2296 mlp->stat_mlp_prob_updated = GNUNET_YES;
2297 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2298 GAS_mlp_solve_problem (solver);
2304 * Get application feedback for a peer
2306 * @param solver the solver handle
2307 * @param application the application
2308 * @param peer the peer to change the preference for
2309 * @param scope the time interval for this feedback: [now - scope .. now]
2310 * @param kind the kind to change the preference
2311 * @param score the score
2314 GAS_mlp_address_preference_feedback (void *solver,
2315 struct GNUNET_SERVER_Client *application,
2316 const struct GNUNET_PeerIdentity *peer,
2317 const struct GNUNET_TIME_Relative scope,
2318 enum GNUNET_ATS_PreferenceKind kind,
2321 struct GAS_PROPORTIONAL_Handle *s = solver;
2323 GNUNET_assert (NULL != solver);
2324 GNUNET_assert (NULL != peer);
2325 GNUNET_assert (NULL != s);
2330 mlp_free_peers (void *cls,
2331 const struct GNUNET_PeerIdentity *key, void *value)
2333 struct GNUNET_CONTAINER_MultiPeerMap *map = cls;
2334 struct ATS_Peer *p = value;
2336 GNUNET_assert (GNUNET_YES ==
2337 GNUNET_CONTAINER_multipeermap_remove (map, key, value));
2345 * Shutdown the MLP problem solving component
2347 * @param cls the solver handle
2351 libgnunet_plugin_ats_mlp_done (void *cls)
2353 struct GNUNET_ATS_SolverFunctions *sf = cls;
2354 struct GAS_MLP_Handle *mlp = sf->cls;
2356 LOG (GNUNET_ERROR_TYPE_DEBUG,
2357 "Shutting down mlp solver\n");
2358 mlp_delete_problem (mlp);
2359 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
2361 mlp->requested_peers);
2362 GNUNET_CONTAINER_multipeermap_destroy (mlp->requested_peers);
2363 mlp->requested_peers = NULL;
2365 /* Clean up GLPK environment */
2369 LOG (GNUNET_ERROR_TYPE_DEBUG,
2370 "Shutdown down of mlp solver complete\n");
2376 libgnunet_plugin_ats_mlp_init (void *cls)
2378 static struct GNUNET_ATS_SolverFunctions sf;
2379 struct GNUNET_ATS_PluginEnvironment *env = cls;
2380 struct GAS_MLP_Handle * mlp = GNUNET_new (struct GAS_MLP_Handle);
2382 unsigned long long tmp;
2388 struct GNUNET_TIME_Relative max_duration;
2389 long long unsigned int max_iterations;
2391 /* Init GLPK environment */
2392 int res = glp_init_env();
2395 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
2396 "initialization successful");
2399 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
2400 "environment is already initialized");
2403 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
2404 "initialization failed (insufficient memory)");
2409 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
2410 "initialization failed (unsupported programming model)");
2418 mlp->opt_dump_problem_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2419 "ats", "MLP_DUMP_PROBLEM_ALL");
2420 if (GNUNET_SYSERR == mlp->opt_dump_problem_all)
2421 mlp->opt_dump_problem_all = GNUNET_NO;
2423 mlp->opt_dump_solution_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2424 "ats", "MLP_DUMP_SOLUTION_ALL");
2425 if (GNUNET_SYSERR == mlp->opt_dump_solution_all)
2426 mlp->opt_dump_solution_all = GNUNET_NO;
2428 mlp->opt_dump_problem_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2429 "ats", "MLP_DUMP_PROBLEM_ON_FAIL");
2430 if (GNUNET_SYSERR == mlp->opt_dump_problem_on_fail)
2431 mlp->opt_dump_problem_on_fail = GNUNET_NO;
2433 mlp->opt_dump_solution_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2434 "ats", "MLP_DUMP_SOLUTION_ON_FAIL");
2435 if (GNUNET_SYSERR == mlp->opt_dump_solution_on_fail)
2436 mlp->opt_dump_solution_on_fail = GNUNET_NO;
2438 mlp->opt_dbg_glpk_verbose = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2439 "ats", "MLP_DBG_GLPK_VERBOSE");
2440 if (GNUNET_SYSERR == mlp->opt_dbg_glpk_verbose)
2441 mlp->opt_dbg_glpk_verbose = GNUNET_NO;
2443 mlp->opt_dbg_feasibility_only = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2444 "ats", "MLP_DBG_FEASIBILITY_ONLY");
2445 if (GNUNET_SYSERR == mlp->opt_dbg_feasibility_only)
2446 mlp->opt_dbg_feasibility_only = GNUNET_NO;
2447 if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
2448 LOG (GNUNET_ERROR_TYPE_WARNING,
2449 "MLP solver is configured to check feasibility only!\n");
2451 mlp->opt_dbg_autoscale_problem = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2452 "ats", "MLP_DBG_AUTOSCALE_PROBLEM");
2453 if (GNUNET_SYSERR == mlp->opt_dbg_autoscale_problem)
2454 mlp->opt_dbg_autoscale_problem = GNUNET_NO;
2455 if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
2456 LOG (GNUNET_ERROR_TYPE_WARNING,
2457 "MLP solver is configured automatically scale the problem!\n");
2459 mlp->opt_dbg_intopt_presolver = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2460 "ats", "MLP_DBG_INTOPT_PRESOLVE");
2461 if (GNUNET_SYSERR == mlp->opt_dbg_intopt_presolver)
2462 mlp->opt_dbg_intopt_presolver = GNUNET_NO;
2463 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
2464 LOG (GNUNET_ERROR_TYPE_WARNING,
2465 "MLP solver is configured use the mlp presolver\n");
2467 mlp->opt_dbg_optimize_diversity = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2468 "ats", "MLP_DBG_OPTIMIZE_DIVERSITY");
2469 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_diversity)
2470 mlp->opt_dbg_optimize_diversity = GNUNET_YES;
2471 if (GNUNET_NO == mlp->opt_dbg_optimize_diversity)
2472 LOG (GNUNET_ERROR_TYPE_WARNING,
2473 "MLP solver is not optimizing for diversity\n");
2475 mlp->opt_dbg_optimize_relativity= GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2476 "ats", "MLP_DBG_OPTIMIZE_RELATIVITY");
2477 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_relativity)
2478 mlp->opt_dbg_optimize_relativity = GNUNET_YES;
2479 if (GNUNET_NO == mlp->opt_dbg_optimize_relativity)
2480 LOG (GNUNET_ERROR_TYPE_WARNING,
2481 "MLP solver is not optimizing for relativity\n");
2483 mlp->opt_dbg_optimize_quality = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2484 "ats", "MLP_DBG_OPTIMIZE_QUALITY");
2485 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_quality)
2486 mlp->opt_dbg_optimize_quality = GNUNET_YES;
2487 if (GNUNET_NO == mlp->opt_dbg_optimize_quality)
2488 LOG (GNUNET_ERROR_TYPE_WARNING,
2489 "MLP solver is not optimizing for quality\n");
2491 mlp->opt_dbg_optimize_utility = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2492 "ats", "MLP_DBG_OPTIMIZE_UTILITY");
2493 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_utility)
2494 mlp->opt_dbg_optimize_utility = GNUNET_YES;
2495 if (GNUNET_NO == mlp->opt_dbg_optimize_utility)
2496 LOG (GNUNET_ERROR_TYPE_WARNING,
2497 "MLP solver is not optimizing for utility\n");
2499 if ( (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
2500 (GNUNET_NO == mlp->opt_dbg_optimize_quality) &&
2501 (GNUNET_NO == mlp->opt_dbg_optimize_relativity) &&
2502 (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
2503 (GNUNET_NO == mlp->opt_dbg_feasibility_only))
2505 LOG (GNUNET_ERROR_TYPE_ERROR,
2506 _("MLP solver is not optimizing for anything, changing to feasibility check\n"));
2507 mlp->opt_dbg_feasibility_only = GNUNET_YES;
2510 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2511 "ats", "MLP_LOG_FORMAT", &outputformat))
2512 mlp->opt_log_format = MLP_CPLEX;
2515 GNUNET_STRINGS_utf8_toupper(outputformat, outputformat);
2516 if (0 == strcmp (outputformat, "MPS"))
2518 mlp->opt_log_format = MLP_MPS;
2520 else if (0 == strcmp (outputformat, "CPLEX"))
2522 mlp->opt_log_format = MLP_CPLEX;
2524 else if (0 == strcmp (outputformat, "GLPK"))
2526 mlp->opt_log_format = MLP_GLPK;
2530 LOG (GNUNET_ERROR_TYPE_WARNING,
2531 "Invalid log format `%s' in configuration, using CPLEX!\n",
2533 mlp->opt_log_format = MLP_CPLEX;
2535 GNUNET_free (outputformat);
2538 mlp->pv.BIG_M = (double) BIG_M_VALUE;
2540 mlp->pv.mip_gap = (double) 0.0;
2541 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2542 "MLP_MAX_MIP_GAP", &f_tmp))
2544 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2546 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2551 mlp->pv.mip_gap = f_tmp;
2552 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2557 mlp->pv.lp_mip_gap = (double) 0.0;
2558 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2559 "MLP_MAX_LP_MIP_GAP", &f_tmp))
2561 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2563 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2568 mlp->pv.lp_mip_gap = f_tmp;
2569 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2574 /* Get timeout for iterations */
2575 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats",
2576 "MLP_MAX_DURATION", &max_duration))
2578 max_duration = MLP_MAX_EXEC_DURATION;
2581 /* Get maximum number of iterations */
2582 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(env->cfg, "ats",
2583 "MLP_MAX_ITERATIONS", &max_iterations))
2585 max_iterations = MLP_MAX_ITERATIONS;
2588 /* Get diversity coefficient from configuration */
2589 mlp->pv.co_D = MLP_DEFAULT_D;
2590 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2591 "MLP_COEFFICIENT_D", &f_tmp))
2595 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2596 "MLP_COEFFICIENT_D", f_tmp);
2600 mlp->pv.co_D = f_tmp;
2601 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2602 "MLP_COEFFICIENT_D", f_tmp);
2606 /* Get relativity coefficient from configuration */
2607 mlp->pv.co_R = MLP_DEFAULT_R;
2608 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2609 "MLP_COEFFICIENT_R", &f_tmp))
2613 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2614 "MLP_COEFFICIENT_R", f_tmp);
2618 mlp->pv.co_R = f_tmp;
2619 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2620 "MLP_COEFFICIENT_R", f_tmp);
2625 /* Get utilization coefficient from configuration */
2626 mlp->pv.co_U = MLP_DEFAULT_U;
2627 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2628 "MLP_COEFFICIENT_U", &f_tmp))
2632 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2633 "MLP_COEFFICIENT_U", f_tmp);
2637 mlp->pv.co_U = f_tmp;
2638 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2639 "MLP_COEFFICIENT_U", f_tmp);
2643 /* Get quality metric coefficients from configuration */
2644 int i_delay = MLP_NaN;
2645 int i_distance = MLP_NaN;
2646 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
2647 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
2649 /* initialize quality coefficients with default value 1.0 */
2650 mlp->pv.co_Q[c] = MLP_DEFAULT_QUALITY;
2652 mlp->pv.q[c] = q[c];
2653 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
2655 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
2659 if ( (i_delay != MLP_NaN) &&
2660 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2661 "MLP_COEFFICIENT_QUALITY_DELAY", &tmp)) )
2662 mlp->pv.co_Q[i_delay] = (double) tmp / 100;
2664 mlp->pv.co_Q[i_delay] = MLP_DEFAULT_QUALITY;
2666 if ( (i_distance != MLP_NaN) &&
2667 (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2668 "MLP_COEFFICIENT_QUALITY_DISTANCE", &tmp)) )
2669 mlp->pv.co_Q[i_distance] = (double) tmp / 100;
2671 mlp->pv.co_Q[i_distance] = MLP_DEFAULT_QUALITY;
2673 /* Get minimum bandwidth per used address from configuration */
2674 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2675 "MLP_MIN_BANDWIDTH",
2680 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2683 /* Get minimum number of connections from configuration */
2684 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2685 "MLP_MIN_CONNECTIONS",
2689 n_min = MLP_DEFAULT_MIN_CONNECTIONS;
2691 /* Init network quotas */
2692 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2694 mlp->pv.quota_index[c] = c;
2695 mlp->pv.quota_out[c] = env->out_quota[c];
2696 mlp->pv.quota_in[c] = env->in_quota[c];
2698 LOG (GNUNET_ERROR_TYPE_INFO,
2699 "Quota for network `%s' (in/out) %llu/%llu\n",
2700 GNUNET_ATS_print_network_type (c),
2701 mlp->pv.quota_out[c],
2702 mlp->pv.quota_in[c]);
2703 /* Check if defined quota could make problem unsolvable */
2704 if ((n_min * b_min) > mlp->pv.quota_out[c])
2706 LOG (GNUNET_ERROR_TYPE_INFO,
2707 _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2708 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2709 mlp->pv.quota_out[c],
2711 mlp->pv.quota_out[c] = (n_min * b_min);
2713 if ((n_min * b_min) > mlp->pv.quota_in[c])
2715 LOG (GNUNET_ERROR_TYPE_INFO,
2716 _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2717 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2718 mlp->pv.quota_in[c],
2720 mlp->pv.quota_in[c] = (n_min * b_min);
2722 /* Check if bandwidth is too big to make problem solvable */
2723 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
2725 LOG (GNUNET_ERROR_TYPE_INFO,
2726 _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
2727 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2728 mlp->pv.quota_out[c],
2730 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
2732 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
2734 LOG (GNUNET_ERROR_TYPE_INFO,
2735 _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
2736 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2737 mlp->pv.quota_in[c],
2739 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
2744 sf.s_add = &GAS_mlp_address_add;
2745 sf.s_address_update_property = &GAS_mlp_address_property_changed;
2746 sf.s_get = &GAS_mlp_get_preferred_address;
2747 sf.s_get_stop = &GAS_mlp_stop_get_preferred_address;
2748 sf.s_pref = &GAS_mlp_address_change_preference;
2749 sf.s_feedback = &GAS_mlp_address_preference_feedback;
2750 sf.s_del = &GAS_mlp_address_delete;
2751 sf.s_bulk_start = &GAS_mlp_bulk_start;
2752 sf.s_bulk_stop = &GAS_mlp_bulk_stop;
2754 /* Setting MLP Input variables */
2755 mlp->pv.b_min = b_min;
2756 mlp->pv.n_min = n_min;
2757 mlp->pv.m_q = GNUNET_ATS_QualityPropertiesCount;
2758 mlp->stat_mlp_prob_changed = GNUNET_NO;
2759 mlp->stat_mlp_prob_updated = GNUNET_NO;
2760 mlp->opt_mlp_auto_solve = GNUNET_YES;
2761 mlp->requested_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2762 mlp->stat_bulk_requests = 0;
2763 mlp->stat_bulk_lock = 0;
2766 /* Redirect GLPK output to GNUnet logging */
2767 glp_term_hook (&mlp_term_hook, (void *) mlp);
2769 /* Init LP solving parameters */
2770 glp_init_smcp(&mlp->control_param_lp);
2771 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
2772 if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
2773 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
2775 mlp->control_param_lp.it_lim = max_iterations;
2776 mlp->control_param_lp.tm_lim = max_duration.rel_value_us / 1000LL;
2778 /* Init MLP solving parameters */
2779 glp_init_iocp(&mlp->control_param_mlp);
2780 /* Setting callback function */
2781 mlp->control_param_mlp.cb_func = &mlp_branch_and_cut_cb;
2782 mlp->control_param_mlp.cb_info = mlp;
2783 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
2784 mlp->control_param_mlp.mip_gap = mlp->pv.mip_gap;
2785 if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
2786 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
2787 mlp->control_param_mlp.tm_lim = max_duration.rel_value_us / 1000LL;
2789 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
2794 /* end of plugin_ats_mlp.c */