2 This file is part of GNUnet.
3 Copyright (C) 2011-2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file ats/plugin_ats_mlp.c
21 * @brief ats mlp problem solver
22 * @author Matthias Wachs
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_ats_plugin.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet_statistics_service.h"
35 #define BIG_M_VALUE (UINT32_MAX) /10
36 #define BIG_M_STRING "unlimited"
38 #define MLP_AVERAGING_QUEUE_LENGTH 3
40 #define MLP_MAX_EXEC_DURATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
41 #define MLP_MAX_ITERATIONS 4096
43 #define MLP_DEFAULT_D 1.0
44 #define MLP_DEFAULT_R 1.0
45 #define MLP_DEFAULT_U 1.0
46 #define MLP_DEFAULT_QUALITY 1.0
47 #define MLP_DEFAULT_MIN_CONNECTIONS 4
48 #define MLP_DEFAULT_PEER_PREFERENCE 1.0
51 #define MLP_UNDEFINED 0
55 enum MLP_Output_Format
65 RQ_QUALITY_METRIC_DELAY = 0,
66 RQ_QUALITY_METRIC_DISTANCE = 1,
67 RQ_QUALITY_METRIC_COUNT = 2
72 print_quality_type (enum QualityMetrics qm)
75 case RQ_QUALITY_METRIC_DELAY:
77 case RQ_QUALITY_METRIC_DISTANCE:
93 double lp_objective_value;
94 double mlp_objective_value;
109 struct GNUNET_PeerIdentity id;
111 /* Was this peer already added to the current problem? */
114 /* constraint 2: 1 address per peer*/
117 /* constraint 9: relativity */
120 /* Legacy preference value */
127 * GLPK (MLP) problem object
131 /* Number of addresses in problem */
132 unsigned int num_addresses;
133 /* Number of peers in problem */
134 unsigned int num_peers;
135 /* Number of elements in problem matrix */
136 unsigned int num_elements;
138 /* Row index constraint 2: */
140 /* Row index constraint 4: minimum connections */
142 /* Row index constraint 6: maximize diversity */
144 /* Row index constraint 8: utilization*/
146 /* Row index constraint 9: relativity*/
148 /* Row indices quality metrics */
149 int r_q[RQ_QUALITY_METRIC_COUNT];
150 /* Row indices ATS network quotas */
151 int r_quota[GNUNET_NT_COUNT];
153 /* Column index Diversity (D) column */
155 /* Column index Utilization (U) column */
157 /* Column index Proportionality (R) column */
159 /* Column index quality metrics */
160 int c_q[RQ_QUALITY_METRIC_COUNT];
165 /* Row index array */
167 /* Column index array */
169 /* Column index value */
176 /* Big M value for bandwidth capping */
185 /* Number of quality metrics @deprecated, use RQ_QUALITY_METRIC_COUNT */
188 /* Number of quality metrics */
191 /* Quality metric coefficients*/
192 double co_Q[RQ_QUALITY_METRIC_COUNT];
194 /* Ressource costs coefficients*/
195 double co_RC[RQ_QUALITY_METRIC_COUNT];
197 /* Diversity coefficient */
200 /* Utility coefficient */
203 /* Relativity coefficient */
206 /* Minimum bandwidth assigned to an address */
209 /* Minimum number of addresses with bandwidth assigned */
213 /* Array mapping array index to ATS network */
214 int quota_index[GNUNET_NT_COUNT];
215 /* Outbound quotas */
216 unsigned long long quota_out[GNUNET_NT_COUNT];
219 unsigned long long quota_in[GNUNET_NT_COUNT];
221 /* ATS ressource costs
222 * array with GNUNET_ATS_QualityPropertiesCount elements
223 * contains mapping to GNUNET_ATS_Property
225 int rc[RQ_QUALITY_METRIC_COUNT];
232 struct GAS_MLP_Handle
234 struct GNUNET_ATS_PluginEnvironment *env;
237 * Exclude peer from next result propagation
239 const struct GNUNET_PeerIdentity *exclude_peer;
242 * Encapsulation for the MLP problem
244 struct MLP_Problem p;
247 * Encapsulation for the MLP problem variables
249 struct MLP_Variables pv;
252 * Encapsulation for the MLP solution
254 struct MLP_Solution ps;
262 * Number of changes while solver was locked
264 int stat_bulk_requests;
267 * GLPK LP control parameter
269 glp_smcp control_param_lp;
272 * GLPK LP control parameter
274 glp_iocp control_param_mlp;
277 * Peers with pending address requests
279 struct GNUNET_CONTAINER_MultiPeerMap *requested_peers;
282 * Was the problem updated since last solution
284 int stat_mlp_prob_updated;
287 * Has the problem size changed since last solution
289 int stat_mlp_prob_changed;
292 * Solve the problem automatically when updates occur?
293 * Default: GNUNET_YES
294 * Can be disabled for test and measurements
296 int opt_mlp_auto_solve;
299 * Write all MILP problems to a MPS file
301 int opt_dump_problem_all;
304 * Write all MILP problem solutions to a file
306 int opt_dump_solution_all;
309 * Write MILP problems to a MPS file when solver fails
311 int opt_dump_problem_on_fail;
314 * Write MILP problem solutions to a file when solver fails
316 int opt_dump_solution_on_fail;
319 * solve feasibility only
321 int opt_dbg_feasibility_only;
324 * solve autoscale the problem
326 int opt_dbg_autoscale_problem;
329 * use the intopt presolver instead of simplex
331 int opt_dbg_intopt_presolver;
336 int opt_dbg_glpk_verbose;
339 * solve autoscale the problem
341 int opt_dbg_optimize_relativity;
344 * solve autoscale the problem
346 int opt_dbg_optimize_diversity;
349 * solve autoscale the problem
351 int opt_dbg_optimize_quality;
354 * solve autoscale the problem
356 int opt_dbg_optimize_utility;
362 enum MLP_Output_Format opt_log_format;
366 * Address specific MLP information
368 struct MLP_information
372 * Bandwidth assigned outbound
377 * Bandwidth assigned inbound
387 * bandwidth column index
392 * address usage column
399 * constraint 1: bandwidth capping
404 * constraint 3: minimum bandwidth
413 * NOTE: Do not modify this documentation. This documentation is based on
414 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
415 * use build_txt.sh to generate plaintext output
417 * The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
418 * optimizing an mixed integer programming problem. The MLP solver uses a
419 * number of constraints to find the best adddress for a peer and an optimal
420 * bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
423 * We defined a constraint system to find an optimal bandwidth assignment.
424 * This constraint system uses as an input data addresses, bandwidth quotas,
425 * preferences and quality values. This constraint system is stored in an
426 * matrix based equotation system.
430 * A (M)LP problem consists of a target function to optimizes, constraints
431 * and rows and columns. FIXME GLP uses three arrays to index the matrix: two
432 * integer arrays storing the row and column indices in the matrix and an
433 * float array to store the coeeficient.
435 * To solve the problem we first find an initial solution for the LP problem
436 * using the LP solver and then find an MLP solution based on this solution
437 * using the MLP solver.
439 * Solving (M)LP problems has the property that finding an initial solution
440 * for the LP problem is computationally expensive and finding the MLP
441 * solution is cheaper. This is especially interesting an existing LP
442 * solution can be reused if only coefficients in the matrix have changed
443 * (addresses updated). Only when the problem size changes (addresses added
444 * or deleted) a new LP solution has to be found.
447 * The mlp solver solves the bandwidth assignment problem only on demand when
448 * an address suggestion is requested. When an address is requested mlp the
449 * solves the mlp problem and if the active address or the bandwidth assigned
450 * changes it calls the callback to addresses. The mlp solver gets notified
451 * about new addresses (adding sessions), removed addresses (address
452 * deletions) and address updates. To benefit from the mlp properties
453 * mentioned in section 5 the solver rembers if since the last solution
454 * addresses were added or deleted (problem size changed, problem has to be
455 * rebuild and solved from sratch) or if addresses were updated and the
456 * existing solution can be reused.
460 * The quotas for each network segment are passed by addresses. MLP can be
461 * adapted using configuration settings and uses the following parameters:
462 * * MLP_MAX_DURATION:
463 * Maximum duration for a MLP solution procees (default: 3 sec.)
464 * * MLP_MAX_ITERATIONS:
465 * Maximum number of iterations for a MLP solution process (default:
467 * * MLP_MIN_CONNECTIONS:
468 * Minimum number of desired connections (default: 4)
469 * * MLP_MIN_BANDWIDTH:
470 * Minimum amount of bandwidth assigned to an address (default: 1024)
471 * * MLP_COEFFICIENT_D:
472 * Diversity coefficient (default: 1.0)
473 * * MLP_COEFFICIENT_R:
474 * Relativity coefficient (default: 1.0)
475 * * MLP_COEFFICIENT_U:
476 * Utilization coefficient (default: 1.0)
477 * * MLP_COEFFICIENT_D:
478 * Diversity coefficient (default: 1.0)
479 * * MLP_COEFFICIENT_QUALITY_DELAY:
480 * Quality delay coefficient (default: 1.0)
481 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
482 * Quality distance coefficient (default: 1.0)
483 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
484 * Quality distance coefficient (default: 1.0)
485 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
486 * Quality distance coefficient (default: 1.0)
487 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
488 * Quality distance coefficient (default: 1.0)
490 * 5.2 Data structures used
492 * mlp has for each known peer a struct ATS_Peer containing information about
493 * a specific peer. The address field solver_information contains information
494 * about the mlp properties of this address.
498 * During initialization mlp initializes the GLPK libray used to solve the
499 * MLP problem: it initializes the glpk environment and creates an initial LP
500 * problem. Next it loads the configuration values from the configuration or
501 * uses the default values configured in -addresses_mlp.h. The quotas used
502 * are given by addresses but may have to be adjusted. mlp uses a upper limit
503 * for the bandwidth assigned called BIG M and a minimum amount of bandwidth
504 * an address gets assigned as well as a minium desired number of
505 * connections. If the configured quota is bigger than BIG M, it is reduced
506 * to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
507 * *MLP_MIN_BANDWIDTH it is increased to this value.
513 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
516 * Print debug output for mlp problem creation
518 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
522 * Intercept GLPK terminal output
523 * @param info the mlp handle
524 * @param s the string to print
525 * @return 0: glpk prints output on terminal, 0 != surpress output
528 mlp_term_hook (void *info, const char *s)
530 struct GAS_MLP_Handle *mlp = info;
532 if (mlp->opt_dbg_glpk_verbose)
533 LOG (GNUNET_ERROR_TYPE_ERROR, "%s", s);
539 * Reset peers for next problem creation
541 * @param cls not used
543 * @param value ATS_Peer
547 reset_peers (void *cls,
548 const struct GNUNET_PeerIdentity *key,
551 struct ATS_Peer *peer = value;
552 peer->processed = GNUNET_NO;
557 * Delete the MLP problem and free the constrain matrix
559 * @param mlp the MLP handle
562 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
567 if (mlp->p.prob != NULL)
569 glp_delete_prob(mlp->p.prob);
573 /* delete row index */
574 if (mlp->p.ia != NULL)
576 GNUNET_free (mlp->p.ia);
580 /* delete column index */
581 if (mlp->p.ja != NULL)
583 GNUNET_free (mlp->p.ja);
587 /* delete coefficients */
588 if (mlp->p.ar != NULL)
590 GNUNET_free (mlp->p.ar);
596 mlp->p.c_d = MLP_UNDEFINED;
597 mlp->p.c_r = MLP_UNDEFINED;
598 mlp->p.r_c2 = MLP_UNDEFINED;
599 mlp->p.r_c4 = MLP_UNDEFINED;
600 mlp->p.r_c6 = MLP_UNDEFINED;
601 mlp->p.r_c9 = MLP_UNDEFINED;
602 for (c = 0; c < RQ_QUALITY_METRIC_COUNT ; c ++)
603 mlp->p.r_q[c] = MLP_UNDEFINED;
604 for (c = 0; c < GNUNET_NT_COUNT; c ++)
605 mlp->p.r_quota[c] = MLP_UNDEFINED;
606 mlp->p.ci = MLP_UNDEFINED;
609 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
615 * Translate glpk status error codes to text
616 * @param retcode return code
617 * @return string with result
620 mlp_status_to_string (int retcode)
624 return "solution is undefined";
626 return "solution is feasible";
628 return "solution is infeasible";
630 return "no feasible solution exists";
632 return "solution is optimal";
634 return "solution is unbounded";
637 return "unknown error";
643 * Translate glpk solver error codes to text
644 * @param retcode return code
645 * @return string with result
648 mlp_solve_to_string (int retcode)
654 return "invalid basis";
656 return "singular matrix";
658 return "ill-conditioned matrix";
660 return "invalid bounds";
662 return "solver failed";
664 return "objective lower limit reached";
666 return "objective upper limit reached";
668 return "iteration limit exceeded";
670 return "time limit exceeded";
672 return "no primal feasible solution";
674 return "no dual feasible solution";
676 return "root LP optimum not provided";
678 return "search terminated by application";
680 return "relative mip gap tolerance reached";
682 return "no dual feasible solution";
684 return "no convergence";
686 return "numerical instability";
688 return "invalid data";
690 return "result out of range";
693 return "unknown error";
700 const struct GNUNET_CONTAINER_MultiPeerMap *map;
705 mlp_create_problem_count_addresses_it (void *cls,
706 const struct GNUNET_PeerIdentity *key,
709 struct CountContext *cctx = cls;
711 /* Check if we have to add this peer due to a pending request */
712 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
719 mlp_create_problem_count_addresses (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
720 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
722 struct CountContext cctx;
724 cctx.map = requested_peers;
726 GNUNET_CONTAINER_multipeermap_iterate (addresses,
727 &mlp_create_problem_count_addresses_it, &cctx);
733 mlp_create_problem_count_peers_it (void *cls,
734 const struct GNUNET_PeerIdentity *key,
737 struct CountContext *cctx = cls;
739 /* Check if we have to addresses for the requested peer */
740 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
747 mlp_create_problem_count_peers (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
748 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
750 struct CountContext cctx;
752 cctx.map = addresses;
754 GNUNET_CONTAINER_multipeermap_iterate (requested_peers,
755 &mlp_create_problem_count_peers_it, &cctx);
761 * Updates an existing value in the matrix
763 * Extract the row, updates the value and updates the row in the problem
765 * @param p the mlp problem
766 * @param row the row to create the value in
767 * @param col the column to create the value in
768 * @param val the value to set
769 * @param line calling line for debbuging
770 * @return GNUNET_YES value changed, GNUNET_NO value did not change, GNUNET_SYSERR
774 mlp_create_problem_update_value (struct MLP_Problem *p,
775 int row, int col, double val,
786 GNUNET_assert (NULL != p->prob);
788 /* Get number of columns and prepare data structure */
789 c_cols = glp_get_num_cols(p->prob);
791 return GNUNET_SYSERR;
793 val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
794 GNUNET_assert (NULL != val_array);
795 ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
796 GNUNET_assert (NULL != ind_array);
797 /* Extract the row */
799 /* Update the value */
800 c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array);
802 for (c1 = 1; c1 < (c_elems+1); c1++)
804 if (ind_array[c1] == col)
810 if (GNUNET_NO == found)
812 ind_array[c_elems+1] = col;
813 val_array[c_elems+1] = val;
814 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Setting value in [%s : %s] to `%.2f'\n",
815 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
817 glp_set_mat_row (p->prob, row, c_elems+1, ind_array, val_array);
818 GNUNET_free (ind_array);
819 GNUNET_free (val_array);
825 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Updating value in [%s : %s] from `%.2f' to `%.2f'\n",
826 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
828 if (val != val_array[c1])
833 /* Update the row in the matrix */
834 glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
837 GNUNET_free (ind_array);
838 GNUNET_free (val_array);
843 * Creates a new value in the matrix
845 * Sets the row and column index in the problem array and increments the
848 * @param p the mlp problem
849 * @param row the row to create the value in
850 * @param col the column to create the value in
851 * @param val the value to set
852 * @param line calling line for debbuging
855 mlp_create_problem_set_value (struct MLP_Problem *p,
856 int row, int col, double val,
859 if ((p->ci) >= p->num_elements)
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
862 line, p->ci + 1, p->num_elements);
866 if ((0 == row) || (0 == col))
869 LOG (GNUNET_ERROR_TYPE_ERROR, "[P]: Invalid call from line %u: row = %u, col = %u\n",
875 #if DEBUG_MLP_PROBLEM_CREATION
876 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u == %.2f\n",
877 line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
883 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
884 unsigned int type, unsigned int bound, double lb, double ub,
887 int col = glp_add_cols (p->prob, 1);
888 glp_set_col_name (p->prob, col, name);
889 glp_set_col_bnds (p->prob, col, bound, lb, ub);
890 glp_set_col_kind (p->prob, col, type);
891 glp_set_obj_coef (p->prob, col, coef);
892 #if DEBUG_MLP_PROBLEM_CREATION
893 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
900 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
901 unsigned int bound, double lb, double ub)
904 int row = glp_add_rows (p->prob, 1);
906 glp_set_row_name (p->prob, row, name);
907 /* set row bounds: <= 0 */
908 glp_set_row_bnds (p->prob, row, bound, lb, ub);
912 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
915 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
918 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
921 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
924 GNUNET_asprintf(&op, "ERROR");
927 #if DEBUG_MLP_PROBLEM_CREATION
928 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
937 * - address columns b and n
938 * - address dependent constraint rows c1, c3
939 * - peer dependent rows c2 and c9
940 * - Set address dependent entries in problem matrix as well
943 mlp_create_problem_add_address_information (void *cls,
944 const struct GNUNET_PeerIdentity *key,
947 struct GAS_MLP_Handle *mlp = cls;
948 struct MLP_Problem *p = &mlp->p;
949 struct ATS_Address *address = value;
950 struct ATS_Peer *peer;
951 struct MLP_information *mlpi;
955 uint32_t addr_net_index;
956 unsigned long long max_quota;
959 /* Check if we have to add this peer due to a pending request */
960 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(mlp->requested_peers, key))
963 mlpi = address->solver_information;
966 fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
971 addr_net = address->properties.scope;
972 for (addr_net_index = 0; addr_net_index < GNUNET_NT_COUNT; addr_net_index++)
974 if (mlp->pv.quota_index[addr_net_index] == addr_net)
978 if (addr_net_index >= GNUNET_NT_COUNT)
985 for (c = 0; c < GNUNET_NT_COUNT; c++)
987 if (mlp->pv.quota_out[c] > max_quota)
988 max_quota = mlp->pv.quota_out[c];
989 if (mlp->pv.quota_in[c] > max_quota)
990 max_quota = mlp->pv.quota_in[c];
992 if (max_quota > mlp->pv.BIG_M)
993 cur_bigm = (double) mlp->pv.BIG_M;
995 cur_bigm = max_quota;
999 peer = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, key);
1000 GNUNET_assert (NULL != peer);
1001 if (peer->processed == GNUNET_NO)
1003 /* Add peer dependent constraints */
1004 /* Add c2) One address active per peer */
1005 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
1006 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
1008 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1010 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1012 /* Add c9) Relativity */
1013 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
1014 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
1016 /* c9) set coefficient */
1017 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f , __LINE__);
1020 peer->processed = GNUNET_YES;
1023 /* Reset addresses' solver information */
1030 /* Add bandwidth column */
1031 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
1032 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1034 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
1038 /* Maximize for bandwidth assignment in feasibility testing */
1039 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
1043 /* Add address active column */
1044 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
1045 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
1048 /* Add address dependent constraints */
1049 /* Add c1) bandwidth capping: b_t + (-M) * n_t <= 0 */
1050 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
1051 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
1053 /* c1) set b = 1 coefficient */
1054 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
1055 /* c1) set n = - min (M, quota) coefficient */
1056 cur_bigm = (double) mlp->pv.quota_out[addr_net_index];
1057 if (cur_bigm > mlp->pv.BIG_M)
1058 cur_bigm = (double) mlp->pv.BIG_M;
1059 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -cur_bigm, __LINE__);
1061 /* Add constraint c 3) minimum bandwidth
1062 * b_t + (-n_t * b_min) >= 0
1064 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
1065 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
1068 /* c3) set b = 1 coefficient */
1069 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
1070 /* c3) set n = -b_min coefficient */
1071 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
1074 /* Set coefficient entries in invariant rows */
1078 /* c 4) minimum connections */
1079 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
1080 /* c 2) 1 address peer peer */
1081 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
1082 /* c 10) obey network specific quotas
1083 * (1)*b_1 + ... + (1)*b_m <= quota_n
1085 mlp_create_problem_set_value (p, p->r_quota[addr_net_index], mlpi->c_b, 1, __LINE__);
1088 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1090 /* c 6) maximize diversity */
1091 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
1092 /* c 9) relativity */
1093 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1094 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
1096 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1097 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
1098 /* c 7) Optimize quality */
1099 /* For all quality metrics, set quality of this address */
1100 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1102 mlp_create_problem_set_value (p,
1103 p->r_q[RQ_QUALITY_METRIC_DELAY],
1105 address->norm_delay.norm,
1107 mlp_create_problem_set_value (p,
1108 p->r_q[RQ_QUALITY_METRIC_DISTANCE],
1110 address->norm_distance.norm,
1120 * Create the invariant columns c4, c6, c10, c8, c7
1123 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
1129 /* Row for c4) minimum connection */
1130 /* Number of minimum connections is min(|Peers|, n_min) */
1131 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);
1133 /* Rows for c 10) Enforce network quotas */
1134 for (c = 0; c < GNUNET_NT_COUNT; c++)
1137 GNUNET_asprintf(&text, "c10_quota_ats_%s",
1138 GNUNET_NT_to_string(mlp->pv.quota_index[c]));
1139 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
1144 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1147 /* Add row for c6) Maximize for diversity */
1148 if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
1150 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
1151 /* Set c6 ) Setting -D */
1152 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
1155 /* Adding rows for c 8) Maximize utility */
1156 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1158 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
1160 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
1163 /* For all quality metrics:
1164 * c 7) Maximize quality, austerity */
1165 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1167 for (c = 0; c < mlp->pv.m_q; c++)
1169 GNUNET_asprintf (&name,
1171 print_quality_type (c));
1172 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
1174 mlp_create_problem_set_value (p,
1176 p->c_q[c], -1, __LINE__);
1184 * Create the invariant columns d, u, r, q0 ... qm
1187 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
1189 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1194 /* Diversity d column */
1195 if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
1196 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
1198 /* Utilization u column */
1199 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1200 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
1202 /* Relativity r column */
1203 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1204 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
1206 /* Quality metric columns */
1207 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1209 for (c = 0; c < mlp->pv.m_q; c++)
1211 GNUNET_asprintf (&name, "q_%u", c);
1212 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
1221 * Create the MLP problem
1223 * @param mlp the MLP handle
1224 * @return #GNUNET_OK or #GNUNET_SYSERR
1227 mlp_create_problem (struct GAS_MLP_Handle *mlp)
1229 struct MLP_Problem *p = &mlp->p;
1230 int res = GNUNET_OK;
1232 GNUNET_assert (p->prob == NULL);
1233 GNUNET_assert (p->ia == NULL);
1234 GNUNET_assert (p->ja == NULL);
1235 GNUNET_assert (p->ar == NULL);
1236 /* Reset MLP problem struct */
1238 /* create the glpk problem */
1239 p->prob = glp_create_prob ();
1240 GNUNET_assert (NULL != p->prob);
1241 p->num_peers = mlp_create_problem_count_peers (mlp->requested_peers, mlp->env->addresses);
1242 p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers,
1243 mlp->env->addresses);
1245 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
1246 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses +
1247 mlp->pv.m_q + p->num_peers + 2 + 1);
1248 LOG (GNUNET_ERROR_TYPE_DEBUG,
1249 "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
1255 /* Set a problem name */
1256 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
1257 /* Set optimization direction to maximize */
1258 glp_set_obj_dir (p->prob, GLP_MAX);
1260 /* Create problem matrix */
1261 /* last +1 caused by glpk index starting with one: [1..elements]*/
1264 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
1266 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
1268 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
1270 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
1272 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
1273 return GNUNET_SYSERR;
1276 /* Adding invariant columns */
1277 mlp_create_problem_add_invariant_columns (mlp, p);
1279 /* Adding address independent constraint rows */
1280 mlp_create_problem_add_invariant_rows (mlp, p);
1282 /* Adding address dependent columns constraint rows */
1283 GNUNET_CONTAINER_multipeermap_iterate (mlp->env->addresses,
1284 &mlp_create_problem_add_address_information,
1287 /* Load the matrix */
1288 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
1289 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
1290 if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
1292 glp_scale_prob (p->prob, GLP_SF_AUTO);
1300 * Solves the LP problem
1302 * @param mlp the MLP Handle
1303 * @return #GNUNET_OK if could be solved, #GNUNET_SYSERR on failure
1306 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
1310 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
1312 LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: %s\n",
1313 mlp_solve_to_string (res));
1315 LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem failed: %s\n",
1316 mlp_solve_to_string (res));
1318 /* Analyze problem status */
1319 res_status = glp_get_status (mlp->p.prob);
1320 switch (res_status) {
1321 case GLP_OPT: /* solution is optimal */
1322 LOG (GNUNET_ERROR_TYPE_INFO,
1323 "Solving LP problem: %s, %s\n",
1324 mlp_solve_to_string(res),
1325 mlp_status_to_string(res_status));
1328 LOG (GNUNET_ERROR_TYPE_ERROR,
1329 "Solving LP problem failed: %s %s\n",
1330 mlp_solve_to_string(res),
1331 mlp_status_to_string(res_status));
1332 return GNUNET_SYSERR;
1338 * Propagates the results when MLP problem was solved
1340 * @param cls the MLP handle
1341 * @param key the peer identity
1342 * @param value the address
1343 * @return #GNUNET_OK to continue
1346 mlp_propagate_results (void *cls,
1347 const struct GNUNET_PeerIdentity *key,
1350 struct GAS_MLP_Handle *mlp = cls;
1351 struct ATS_Address *address;
1352 struct MLP_information *mlpi;
1353 double mlp_bw_in = MLP_NaN;
1354 double mlp_bw_out = MLP_NaN;
1355 double mlp_use = MLP_NaN;
1357 /* Check if we have to add this peer due to a pending request */
1358 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mlp->requested_peers,
1364 GNUNET_assert (address->solver_information != NULL);
1365 mlpi = address->solver_information;
1367 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
1368 if (mlp_bw_in > (double) UINT32_MAX)
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
1371 mlp_bw_in = (double) UINT32_MAX;
1373 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
1374 if (mlp_bw_out > (double) UINT32_MAX)
1376 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
1377 mlp_bw_out = (double) UINT32_MAX;
1379 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
1383 * LOG (GNUNET_ERROR_TYPE_INFO, "MLP result address: `%s' `%s' length %u session %u, mlp use %.3f\n",
1384 * GNUNET_i2s(&address->peer), address->plugin,
1385 * address->addr_len, address->session_id);
1388 if (GLP_YES == mlp_use)
1390 /* This address was selected by the solver to be used */
1391 mlpi->n = GNUNET_YES;
1392 if (GNUNET_NO == address->active)
1394 /* Address was not used before, enabling address */
1395 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n",
1396 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1397 address->active = GNUNET_YES;
1398 address->assigned_bw_in = mlp_bw_in;
1399 mlpi->b_in = mlp_bw_in;
1400 address->assigned_bw_out = mlp_bw_out;
1401 mlpi->b_out = mlp_bw_out;
1402 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1403 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
1406 else if (GNUNET_YES == address->active)
1408 /* Address was used before, check for bandwidth change */
1409 if ((mlp_bw_out != address->assigned_bw_out) ||
1410 (mlp_bw_in != address->assigned_bw_in))
1412 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n",
1413 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1414 address->assigned_bw_in = mlp_bw_in;
1415 mlpi->b_in = mlp_bw_in;
1416 address->assigned_bw_out = mlp_bw_out;
1417 mlpi->b_out = mlp_bw_out;
1418 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1419 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
1426 else if (GLP_NO == mlp_use)
1428 /* This address was selected by the solver to be not used */
1429 mlpi->n = GNUNET_NO;
1430 if (GNUNET_NO == address->active)
1432 /* Address was not used before, nothing to do */
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n",
1434 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1437 else if (GNUNET_YES == address->active)
1439 /* Address was used before, disabling address */
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n",
1441 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1442 address->active = GNUNET_NO;
1443 /* Set bandwidth to 0 */
1444 address->assigned_bw_in = 0;
1446 address->assigned_bw_out = 0;
1461 notify (struct GAS_MLP_Handle *mlp,
1462 enum GAS_Solver_Operation op,
1463 enum GAS_Solver_Status stat,
1464 enum GAS_Solver_Additional_Information add)
1466 mlp->env->info_cb (mlp->env->cls,
1474 mlp_branch_and_cut_cb (glp_tree *tree, void *info)
1476 struct GAS_MLP_Handle *mlp = info;
1479 switch (glp_ios_reason (tree))
1482 /* Do nothing here */
1485 /* Do nothing here */
1488 /* Do nothing here */
1491 /* Do nothing here */
1494 /* Do nothing here */
1497 /* Do nothing here */
1500 /* A better solution was found */
1501 mlp->ps.mlp_gap = glp_ios_mip_gap (tree);
1502 mlp_obj = glp_mip_obj_val (mlp->p.prob);
1503 mlp->ps.lp_mlp_gap = (abs(mlp_obj - mlp->ps.lp_objective_value)) / (abs(mlp_obj) + DBL_EPSILON);
1505 LOG (GNUNET_ERROR_TYPE_INFO,
1506 "Found better integer solution, current gaps: %.3f <= %.3f, %.3f <= %.3f\n",
1507 mlp->ps.mlp_gap, mlp->pv.mip_gap,
1508 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1510 if (mlp->ps.mlp_gap <= mlp->pv.mip_gap)
1512 LOG (GNUNET_ERROR_TYPE_INFO,
1513 "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
1514 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1515 glp_ios_terminate (tree);
1518 if (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap)
1520 LOG (GNUNET_ERROR_TYPE_INFO,
1521 "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
1522 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1523 glp_ios_terminate (tree);
1535 * Solves the MLP problem
1537 * @param solver the MLP Handle
1538 * @return #GNUNET_OK if could be solved, #GNUNET_SYSERR on failure
1541 GAS_mlp_solve_problem (void *solver)
1543 struct GAS_MLP_Handle *mlp = solver;
1549 struct GNUNET_TIME_Absolute start_total;
1550 struct GNUNET_TIME_Absolute start_cur_op;
1551 struct GNUNET_TIME_Relative dur_total;
1552 struct GNUNET_TIME_Relative dur_setup;
1553 struct GNUNET_TIME_Relative dur_lp;
1554 struct GNUNET_TIME_Relative dur_mlp;
1556 GNUNET_assert(NULL != solver);
1557 dur_lp = GNUNET_TIME_UNIT_ZERO;
1559 if (GNUNET_YES == mlp->stat_bulk_lock)
1561 mlp->stat_bulk_requests++;
1564 notify(mlp, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS,
1565 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1566 start_total = GNUNET_TIME_absolute_get();
1568 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->requested_peers))
1570 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1571 return GNUNET_OK; /* No pending requests */
1573 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->env->addresses))
1575 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1576 return GNUNET_OK; /* No addresses available */
1579 if ((GNUNET_NO == mlp->stat_mlp_prob_changed)
1580 && (GNUNET_NO == mlp->stat_mlp_prob_updated))
1582 LOG(GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
1583 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1586 if (GNUNET_YES == mlp->stat_mlp_prob_changed)
1588 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
1589 notify(mlp, GAS_OP_SOLVE_SETUP_START, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1590 mlp_delete_problem (mlp);
1591 if (GNUNET_SYSERR == mlp_create_problem (mlp))
1593 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_FAIL, GAS_INFO_FULL);
1594 return GNUNET_SYSERR;
1596 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1597 if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
1599 mlp->control_param_lp.presolve = GLP_YES; /* LP presolver, we need lp solution */
1600 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
1604 mlp->control_param_lp.presolve = GNUNET_NO; /* LP presolver, we need lp solution */
1605 mlp->control_param_mlp.presolve = GLP_YES; /* No presolver, we have LP solution */
1606 dur_lp = GNUNET_TIME_UNIT_ZERO;
1611 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
1614 /* Reset solution info */
1615 mlp->ps.lp_objective_value = 0.0;
1616 mlp->ps.mlp_gap = 1.0;
1617 mlp->ps.mlp_objective_value = 0.0;
1618 mlp->ps.lp_mlp_gap = 0.0;
1620 dur_setup = GNUNET_TIME_absolute_get_duration (start_total);
1623 if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
1625 notify(mlp, GAS_OP_SOLVE_MLP_LP_START, GAS_STAT_SUCCESS,
1626 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1627 LOG(GNUNET_ERROR_TYPE_DEBUG,
1628 "Running LP solver %s\n",
1629 (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
1630 start_cur_op = GNUNET_TIME_absolute_get();
1633 /* Only for debugging:
1634 * Always use LP presolver:
1635 * mlp->control_param_lp.presolve = GLP_YES; */
1636 res_lp = mlp_solve_lp_problem(mlp);
1637 if (GNUNET_OK == res_lp)
1639 mlp->ps.lp_objective_value = glp_get_obj_val (mlp->p.prob);
1640 LOG (GNUNET_ERROR_TYPE_DEBUG,
1641 "LP solution was: %.3f\n",
1642 mlp->ps.lp_objective_value);
1645 dur_lp = GNUNET_TIME_absolute_get_duration (start_cur_op);
1646 notify(mlp, GAS_OP_SOLVE_MLP_LP_STOP,
1647 (GNUNET_OK == res_lp) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1648 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1651 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
1654 /* Run MLP solver */
1655 if ((GNUNET_OK == res_lp) || (GNUNET_YES == mlp->opt_dbg_intopt_presolver))
1657 LOG(GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
1658 notify(mlp, GAS_OP_SOLVE_MLP_MLP_START, GAS_STAT_SUCCESS,
1659 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1660 start_cur_op = GNUNET_TIME_absolute_get();
1664 /* Only for debugging, always use LP presolver */
1665 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
1666 mlp->control_param_mlp.presolve = GNUNET_YES;
1668 mip_res = glp_intopt (mlp->p.prob, &mlp->control_param_mlp);
1673 LOG (GNUNET_ERROR_TYPE_INFO,
1674 "Solving MLP problem: %s\n",
1675 mlp_solve_to_string (mip_res));
1677 case GLP_ETMLIM: /* Time limit reached */
1678 case GLP_EMIPGAP: /* MIP gap tolerance limit reached */
1679 case GLP_ESTOP: /* Solver was instructed to stop*/
1680 /* Semi-successful */
1681 LOG (GNUNET_ERROR_TYPE_INFO,
1682 "Solving MLP problem solution was interupted: %s\n",
1683 mlp_solve_to_string (mip_res));
1692 LOG (GNUNET_ERROR_TYPE_INFO,
1693 "Solving MLP problem failed: %s\n",
1694 mlp_solve_to_string (mip_res));
1698 /* Analyze problem status */
1699 mip_status = glp_mip_status(mlp->p.prob);
1702 case GLP_OPT: /* solution is optimal */
1703 LOG (GNUNET_ERROR_TYPE_WARNING,
1704 "Solution of MLP problem is optimal: %s, %s\n",
1705 mlp_solve_to_string (mip_res),
1706 mlp_status_to_string (mip_status));
1707 mip_res = GNUNET_OK;
1709 case GLP_FEAS: /* solution is feasible but not proven optimal */
1711 if ( (mlp->ps.mlp_gap <= mlp->pv.mip_gap) ||
1712 (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap) )
1714 LOG (GNUNET_ERROR_TYPE_INFO,
1715 "Solution of MLP problem is feasible and solution within gap constraints: %s, %s\n",
1716 mlp_solve_to_string (mip_res),
1717 mlp_status_to_string (mip_status));
1718 mip_res = GNUNET_OK;
1722 LOG (GNUNET_ERROR_TYPE_WARNING,
1723 "Solution of MLP problem is feasible but solution not within gap constraints: %s, %s\n",
1724 mlp_solve_to_string (mip_res),
1725 mlp_status_to_string (mip_status));
1726 mip_res = GNUNET_SYSERR;
1729 case GLP_UNDEF: /* Solution undefined */
1730 case GLP_NOFEAS: /* No feasible solution */
1732 LOG (GNUNET_ERROR_TYPE_ERROR,
1733 "Solving MLP problem failed: %s %s\n",
1734 mlp_solve_to_string (mip_res),
1735 mlp_status_to_string (mip_status));
1736 mip_res = GNUNET_SYSERR;
1740 dur_mlp = GNUNET_TIME_absolute_get_duration (start_cur_op);
1741 dur_total = GNUNET_TIME_absolute_get_duration (start_total);
1743 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP,
1744 (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1745 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1749 /* Do not execute mip solver since lp solution is invalid */
1750 dur_mlp = GNUNET_TIME_UNIT_ZERO;
1751 dur_total = GNUNET_TIME_absolute_get_duration (start_total);
1753 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP, GAS_STAT_FAIL,
1754 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1755 mip_res = GNUNET_SYSERR;
1758 /* Notify about end */
1759 notify(mlp, GAS_OP_SOLVE_STOP,
1760 ((GNUNET_OK == mip_res) && (GNUNET_OK == mip_res)) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1761 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1763 LOG (GNUNET_ERROR_TYPE_DEBUG,
1764 "Execution time for %s solve: (total/setup/lp/mlp) : %llu %llu %llu %llu\n",
1765 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? "full" : "updated",
1766 (unsigned long long) dur_total.rel_value_us,
1767 (unsigned long long) dur_setup.rel_value_us,
1768 (unsigned long long) dur_lp.rel_value_us,
1769 (unsigned long long) dur_mlp.rel_value_us);
1772 mlp->ps.lp_res = res_lp;
1773 mlp->ps.mip_res = mip_res;
1774 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1775 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1776 mlp->ps.p_cols = glp_get_num_cols(mlp->p.prob);
1777 mlp->ps.p_rows = glp_get_num_rows(mlp->p.prob);
1778 mlp->ps.p_elements = mlp->p.num_elements;
1780 /* Propagate result*/
1781 notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1782 (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1784 if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
1786 GNUNET_CONTAINER_multipeermap_iterate(mlp->env->addresses,
1787 &mlp_propagate_results, mlp);
1789 notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1790 (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1793 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1794 if ( (GNUNET_YES == mlp->opt_dump_problem_all) ||
1795 (mlp->opt_dump_problem_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
1797 /* Write problem to disk */
1798 switch (mlp->opt_log_format) {
1800 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.cplex", mlp->p.num_peers,
1801 mlp->p.num_addresses, time.abs_value_us);
1802 glp_write_lp (mlp->p.prob, NULL, filename);
1805 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.glpk", mlp->p.num_peers,
1806 mlp->p.num_addresses, time.abs_value_us);
1807 glp_write_prob (mlp->p.prob, 0, filename);
1810 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers,
1811 mlp->p.num_addresses, time.abs_value_us);
1812 glp_write_mps (mlp->p.prob, GLP_MPS_FILE, NULL, filename);
1817 LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped problem to file: `%s' \n", filename);
1818 GNUNET_free(filename);
1820 if ( (mlp->opt_dump_solution_all) ||
1821 (mlp->opt_dump_solution_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
1823 /* Write solution to disk */
1824 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers,
1825 mlp->p.num_addresses, time.abs_value_us);
1826 glp_print_mip(mlp->p.prob, filename);
1827 LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped solution to file: `%s' \n", filename);
1828 GNUNET_free(filename);
1831 /* Reset change and update marker */
1832 mlp->control_param_lp.presolve = GLP_NO;
1833 mlp->stat_mlp_prob_updated = GNUNET_NO;
1834 mlp->stat_mlp_prob_changed = GNUNET_NO;
1836 if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
1839 return GNUNET_SYSERR;
1843 * Add a single address to the solve
1845 * @param solver the solver Handle
1846 * @param address the address to add
1847 * @param network network type of this address
1850 GAS_mlp_address_add (void *solver,
1851 struct ATS_Address *address,
1854 struct GAS_MLP_Handle *mlp = solver;
1856 if (GNUNET_NT_COUNT <= network)
1862 if (NULL == address->solver_information)
1864 address->solver_information = GNUNET_new (struct MLP_information);
1867 LOG (GNUNET_ERROR_TYPE_ERROR,
1868 _("Adding address for peer `%s' multiple times\n"),
1869 GNUNET_i2s(&address->peer));
1871 /* Is this peer included in the problem? */
1873 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1876 /* FIXME: should this be an error? */
1877 LOG (GNUNET_ERROR_TYPE_DEBUG,
1878 "Adding address for peer `%s' without address request\n",
1879 GNUNET_i2s(&address->peer));
1883 LOG (GNUNET_ERROR_TYPE_DEBUG,
1884 "Adding address for peer `%s' with address request \n",
1885 GNUNET_i2s(&address->peer));
1886 /* Problem size changed: new address for peer with pending request */
1887 mlp->stat_mlp_prob_changed = GNUNET_YES;
1888 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1889 GAS_mlp_solve_problem (solver);
1894 * Transport properties for this address have changed
1896 * @param solver solver handle
1897 * @param address the address
1900 GAS_mlp_address_property_changed (void *solver,
1901 struct ATS_Address *address)
1903 struct MLP_information *mlpi = address->solver_information;
1904 struct GAS_MLP_Handle *mlp = solver;
1906 if (NULL == mlp->p.prob)
1907 return; /* There is no MLP problem to update yet */
1911 LOG (GNUNET_ERROR_TYPE_INFO,
1912 _("Updating address property for peer `%s' %p not added before\n"),
1913 GNUNET_i2s (&address->peer),
1919 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1922 /* Peer is not requested, so no need to update problem */
1925 LOG (GNUNET_ERROR_TYPE_DEBUG,
1926 "Updating properties for peer `%s'\n",
1927 GNUNET_i2s(&address->peer));
1929 if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
1932 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index] */
1934 mlp_create_problem_update_value (&mlp->p,
1935 mlp->p.r_q[RQ_QUALITY_METRIC_DELAY],
1937 address->norm_delay.norm,
1940 mlp_create_problem_update_value (&mlp->p,
1941 mlp->p.r_q[RQ_QUALITY_METRIC_DISTANCE],
1943 address->norm_distance.norm,
1946 mlp->stat_mlp_prob_updated = GNUNET_YES;
1947 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1948 GAS_mlp_solve_problem (solver);
1955 * Find the active address in the set of addresses of a peer
1956 * @param cls destination
1957 * @param key peer id
1958 * @param value address
1959 * @return #GNUNET_OK
1962 mlp_get_preferred_address_it (void *cls,
1963 const struct GNUNET_PeerIdentity *key,
1966 static int counter = 0;
1967 struct ATS_Address **aa = cls;
1968 struct ATS_Address *addr = value;
1969 struct MLP_information *mlpi = addr->solver_information;
1976 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1977 * "MLP [%u] Peer `%s' %s length %u session %u active %s mlp active %s\n",
1978 * counter, GNUNET_i2s (&addr->peer), addr->plugin, addr->addr_len, addr->session_id,
1979 * (GNUNET_YES == addr->active) ? "active" : "inactive",
1980 * (GNUNET_YES == mlpi->n) ? "active" : "inactive");
1983 if (GNUNET_YES == mlpi->n)
1987 (*aa)->assigned_bw_in = mlpi->b_in;
1988 (*aa)->assigned_bw_out = mlpi->b_out;
1997 get_peer_pref_value (struct GAS_MLP_Handle *mlp,
1998 const struct GNUNET_PeerIdentity *peer)
2001 const double *preferences;
2004 preferences = mlp->env->get_preferences (mlp->env->cls, peer);
2006 for (c = 0; c < GNUNET_ATS_PREFERENCE_END; c++)
2008 /* fprintf (stderr, "VALUE[%u] %s %.3f \n",
2009 * c, GNUNET_i2s (&cur->addr->peer), t[c]); */
2010 res += preferences[c];
2013 res /= GNUNET_ATS_PREFERENCE_END;
2016 LOG (GNUNET_ERROR_TYPE_DEBUG,
2017 "Peer preference for peer `%s' == %.2f\n",
2018 GNUNET_i2s(peer), res);
2025 * Get the preferred address for a specific peer
2027 * @param solver the MLP Handle
2028 * @param peer the peer
2031 GAS_mlp_get_preferred_address (void *solver,
2032 const struct GNUNET_PeerIdentity *peer)
2034 struct GAS_MLP_Handle *mlp = solver;
2036 struct ATS_Address *res;
2038 LOG (GNUNET_ERROR_TYPE_DEBUG,
2039 "Getting preferred address for `%s'\n",
2042 /* Is this peer included in the problem? */
2044 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
2047 LOG (GNUNET_ERROR_TYPE_INFO, "Adding peer `%s' to list of requested_peers with requests\n",
2050 p = GNUNET_new (struct ATS_Peer);
2052 p->f = get_peer_pref_value (mlp, peer);
2053 GNUNET_CONTAINER_multipeermap_put (mlp->requested_peers,
2055 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2057 /* Added new peer, we have to rebuild problem before solving */
2058 mlp->stat_mlp_prob_changed = GNUNET_YES;
2060 if ((GNUNET_YES == mlp->opt_mlp_auto_solve)&&
2061 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(mlp->env->addresses,
2064 mlp->exclude_peer = peer;
2065 GAS_mlp_solve_problem (mlp);
2066 mlp->exclude_peer = NULL;
2069 /* Get prefered address */
2071 GNUNET_CONTAINER_multipeermap_get_multiple (mlp->env->addresses, peer,
2072 &mlp_get_preferred_address_it, &res);
2074 mlp->env->bandwidth_changed_cb (mlp->env->cls,
2080 * Deletes a single address in the MLP problem
2082 * The MLP problem has to be recreated and the problem has to be resolved
2084 * @param solver the MLP Handle
2085 * @param address the address to delete
2088 GAS_mlp_address_delete (void *solver,
2089 struct ATS_Address *address)
2091 struct GAS_MLP_Handle *mlp = solver;
2092 struct MLP_information *mlpi;
2093 struct ATS_Address *res;
2096 mlpi = address->solver_information;
2099 /* Remove full address */
2101 address->solver_information = NULL;
2103 was_active = address->active;
2104 address->active = GNUNET_NO;
2105 address->assigned_bw_in = 0;
2106 address->assigned_bw_out = 0;
2108 /* Is this peer included in the problem? */
2110 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
2113 LOG (GNUNET_ERROR_TYPE_INFO,
2114 "Deleting address for peer `%s' without address request \n",
2115 GNUNET_i2s(&address->peer));
2118 LOG (GNUNET_ERROR_TYPE_INFO,
2119 "Deleting address for peer `%s' with address request \n",
2120 GNUNET_i2s (&address->peer));
2122 /* Problem size changed: new address for peer with pending request */
2123 mlp->stat_mlp_prob_changed = GNUNET_YES;
2124 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2126 GAS_mlp_solve_problem (solver);
2128 if (GNUNET_YES == was_active)
2130 GAS_mlp_get_preferred_address (solver, &address->peer);
2132 GNUNET_CONTAINER_multipeermap_get_multiple (mlp->env->addresses,
2134 &mlp_get_preferred_address_it,
2138 /* No alternative address, disconnecting peer */
2139 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
2146 * Start a bulk operation
2148 * @param solver the solver
2151 GAS_mlp_bulk_start (void *solver)
2153 struct GAS_MLP_Handle *s = solver;
2155 LOG (GNUNET_ERROR_TYPE_DEBUG,
2156 "Locking solver for bulk operation ...\n");
2157 GNUNET_assert (NULL != solver);
2158 s->stat_bulk_lock ++;
2163 GAS_mlp_bulk_stop (void *solver)
2165 struct GAS_MLP_Handle *s = solver;
2167 LOG (GNUNET_ERROR_TYPE_DEBUG,
2168 "Unlocking solver from bulk operation ...\n");
2169 GNUNET_assert (NULL != solver);
2171 if (s->stat_bulk_lock < 1)
2176 s->stat_bulk_lock --;
2178 if (0 < s->stat_bulk_requests)
2180 GAS_mlp_solve_problem (solver);
2181 s->stat_bulk_requests= 0;
2188 * Stop notifying about address and bandwidth changes for this peer
2190 * @param solver the MLP handle
2191 * @param peer the peer
2194 GAS_mlp_stop_get_preferred_address (void *solver,
2195 const struct GNUNET_PeerIdentity *peer)
2197 struct GAS_MLP_Handle *mlp = solver;
2198 struct ATS_Peer *p = NULL;
2200 GNUNET_assert (NULL != solver);
2201 GNUNET_assert (NULL != peer);
2202 if (NULL != (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
2204 GNUNET_assert (GNUNET_YES ==
2205 GNUNET_CONTAINER_multipeermap_remove (mlp->requested_peers, peer, p));
2208 mlp->stat_mlp_prob_changed = GNUNET_YES;
2209 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2211 GAS_mlp_solve_problem (solver);
2218 * Changes the preferences for a peer in the MLP problem
2220 * @param solver the MLP Handle
2221 * @param peer the peer
2222 * @param kind the kind to change the preference
2223 * @param pref_rel the relative score
2226 GAS_mlp_address_change_preference (void *solver,
2227 const struct GNUNET_PeerIdentity *peer,
2228 enum GNUNET_ATS_PreferenceKind kind,
2231 struct GAS_MLP_Handle *mlp = solver;
2234 LOG (GNUNET_ERROR_TYPE_DEBUG,
2235 "Changing preference for address for peer `%s' to %.2f\n",
2239 GNUNET_STATISTICS_update (mlp->env->stats,
2240 "# LP address preference changes", 1, GNUNET_NO);
2241 /* Update the constraints with changed preferences */
2245 /* Update relativity constraint c9 */
2246 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
2248 LOG (GNUNET_ERROR_TYPE_INFO,
2249 "Updating preference for unknown peer `%s'\n",
2254 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
2256 p->f = get_peer_pref_value (mlp, peer);
2257 mlp_create_problem_update_value (&mlp->p,
2263 /* Problem size changed: new address for peer with pending request */
2264 mlp->stat_mlp_prob_updated = GNUNET_YES;
2265 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2266 GAS_mlp_solve_problem (solver);
2272 * Get application feedback for a peer
2274 * @param solver the solver handle
2275 * @param application the application
2276 * @param peer the peer to change the preference for
2277 * @param scope the time interval for this feedback: [now - scope .. now]
2278 * @param kind the kind to change the preference
2279 * @param score the score
2282 GAS_mlp_address_preference_feedback (void *solver,
2283 struct GNUNET_SERVICE_Client *application,
2284 const struct GNUNET_PeerIdentity *peer,
2285 const struct GNUNET_TIME_Relative scope,
2286 enum GNUNET_ATS_PreferenceKind kind,
2289 struct GAS_PROPORTIONAL_Handle *s = solver;
2291 GNUNET_assert (NULL != solver);
2292 GNUNET_assert (NULL != peer);
2293 GNUNET_assert (NULL != s);
2298 mlp_free_peers (void *cls,
2299 const struct GNUNET_PeerIdentity *key, void *value)
2301 struct GNUNET_CONTAINER_MultiPeerMap *map = cls;
2302 struct ATS_Peer *p = value;
2304 GNUNET_assert (GNUNET_YES ==
2305 GNUNET_CONTAINER_multipeermap_remove (map, key, value));
2313 * Shutdown the MLP problem solving component
2315 * @param cls the solver handle
2319 libgnunet_plugin_ats_mlp_done (void *cls)
2321 struct GNUNET_ATS_SolverFunctions *sf = cls;
2322 struct GAS_MLP_Handle *mlp = sf->cls;
2324 LOG (GNUNET_ERROR_TYPE_DEBUG,
2325 "Shutting down mlp solver\n");
2326 mlp_delete_problem (mlp);
2327 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
2329 mlp->requested_peers);
2330 GNUNET_CONTAINER_multipeermap_destroy (mlp->requested_peers);
2331 mlp->requested_peers = NULL;
2333 /* Clean up GLPK environment */
2337 LOG (GNUNET_ERROR_TYPE_DEBUG,
2338 "Shutdown down of mlp solver complete\n");
2344 libgnunet_plugin_ats_mlp_init (void *cls)
2346 static struct GNUNET_ATS_SolverFunctions sf;
2347 struct GNUNET_ATS_PluginEnvironment *env = cls;
2348 struct GAS_MLP_Handle * mlp = GNUNET_new (struct GAS_MLP_Handle);
2350 unsigned long long tmp;
2356 struct GNUNET_TIME_Relative max_duration;
2357 long long unsigned int max_iterations;
2359 /* Init GLPK environment */
2360 int res = glp_init_env();
2363 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
2364 "initialization successful");
2367 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
2368 "environment is already initialized");
2371 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
2372 "initialization failed (insufficient memory)");
2377 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
2378 "initialization failed (unsupported programming model)");
2386 mlp->opt_dump_problem_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2387 "ats", "MLP_DUMP_PROBLEM_ALL");
2388 if (GNUNET_SYSERR == mlp->opt_dump_problem_all)
2389 mlp->opt_dump_problem_all = GNUNET_NO;
2391 mlp->opt_dump_solution_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2392 "ats", "MLP_DUMP_SOLUTION_ALL");
2393 if (GNUNET_SYSERR == mlp->opt_dump_solution_all)
2394 mlp->opt_dump_solution_all = GNUNET_NO;
2396 mlp->opt_dump_problem_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2397 "ats", "MLP_DUMP_PROBLEM_ON_FAIL");
2398 if (GNUNET_SYSERR == mlp->opt_dump_problem_on_fail)
2399 mlp->opt_dump_problem_on_fail = GNUNET_NO;
2401 mlp->opt_dump_solution_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2402 "ats", "MLP_DUMP_SOLUTION_ON_FAIL");
2403 if (GNUNET_SYSERR == mlp->opt_dump_solution_on_fail)
2404 mlp->opt_dump_solution_on_fail = GNUNET_NO;
2406 mlp->opt_dbg_glpk_verbose = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2407 "ats", "MLP_DBG_GLPK_VERBOSE");
2408 if (GNUNET_SYSERR == mlp->opt_dbg_glpk_verbose)
2409 mlp->opt_dbg_glpk_verbose = GNUNET_NO;
2411 mlp->opt_dbg_feasibility_only = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2412 "ats", "MLP_DBG_FEASIBILITY_ONLY");
2413 if (GNUNET_SYSERR == mlp->opt_dbg_feasibility_only)
2414 mlp->opt_dbg_feasibility_only = GNUNET_NO;
2415 if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
2416 LOG (GNUNET_ERROR_TYPE_WARNING,
2417 "MLP solver is configured to check feasibility only!\n");
2419 mlp->opt_dbg_autoscale_problem = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2420 "ats", "MLP_DBG_AUTOSCALE_PROBLEM");
2421 if (GNUNET_SYSERR == mlp->opt_dbg_autoscale_problem)
2422 mlp->opt_dbg_autoscale_problem = GNUNET_NO;
2423 if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
2424 LOG (GNUNET_ERROR_TYPE_WARNING,
2425 "MLP solver is configured automatically scale the problem!\n");
2427 mlp->opt_dbg_intopt_presolver = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2428 "ats", "MLP_DBG_INTOPT_PRESOLVE");
2429 if (GNUNET_SYSERR == mlp->opt_dbg_intopt_presolver)
2430 mlp->opt_dbg_intopt_presolver = GNUNET_NO;
2431 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
2432 LOG (GNUNET_ERROR_TYPE_WARNING,
2433 "MLP solver is configured use the mlp presolver\n");
2435 mlp->opt_dbg_optimize_diversity = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2436 "ats", "MLP_DBG_OPTIMIZE_DIVERSITY");
2437 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_diversity)
2438 mlp->opt_dbg_optimize_diversity = GNUNET_YES;
2439 if (GNUNET_NO == mlp->opt_dbg_optimize_diversity)
2440 LOG (GNUNET_ERROR_TYPE_WARNING,
2441 "MLP solver is not optimizing for diversity\n");
2443 mlp->opt_dbg_optimize_relativity= GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2444 "ats", "MLP_DBG_OPTIMIZE_RELATIVITY");
2445 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_relativity)
2446 mlp->opt_dbg_optimize_relativity = GNUNET_YES;
2447 if (GNUNET_NO == mlp->opt_dbg_optimize_relativity)
2448 LOG (GNUNET_ERROR_TYPE_WARNING,
2449 "MLP solver is not optimizing for relativity\n");
2451 mlp->opt_dbg_optimize_quality = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2452 "ats", "MLP_DBG_OPTIMIZE_QUALITY");
2453 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_quality)
2454 mlp->opt_dbg_optimize_quality = GNUNET_YES;
2455 if (GNUNET_NO == mlp->opt_dbg_optimize_quality)
2456 LOG (GNUNET_ERROR_TYPE_WARNING,
2457 "MLP solver is not optimizing for quality\n");
2459 mlp->opt_dbg_optimize_utility = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2460 "ats", "MLP_DBG_OPTIMIZE_UTILITY");
2461 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_utility)
2462 mlp->opt_dbg_optimize_utility = GNUNET_YES;
2463 if (GNUNET_NO == mlp->opt_dbg_optimize_utility)
2464 LOG (GNUNET_ERROR_TYPE_WARNING,
2465 "MLP solver is not optimizing for utility\n");
2467 if ( (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
2468 (GNUNET_NO == mlp->opt_dbg_optimize_quality) &&
2469 (GNUNET_NO == mlp->opt_dbg_optimize_relativity) &&
2470 (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
2471 (GNUNET_NO == mlp->opt_dbg_feasibility_only))
2473 LOG (GNUNET_ERROR_TYPE_ERROR,
2474 _("MLP solver is not optimizing for anything, changing to feasibility check\n"));
2475 mlp->opt_dbg_feasibility_only = GNUNET_YES;
2478 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2479 "ats", "MLP_LOG_FORMAT", &outputformat))
2480 mlp->opt_log_format = MLP_CPLEX;
2483 GNUNET_STRINGS_utf8_toupper(outputformat, outputformat);
2484 if (0 == strcmp (outputformat, "MPS"))
2486 mlp->opt_log_format = MLP_MPS;
2488 else if (0 == strcmp (outputformat, "CPLEX"))
2490 mlp->opt_log_format = MLP_CPLEX;
2492 else if (0 == strcmp (outputformat, "GLPK"))
2494 mlp->opt_log_format = MLP_GLPK;
2498 LOG (GNUNET_ERROR_TYPE_WARNING,
2499 "Invalid log format `%s' in configuration, using CPLEX!\n",
2501 mlp->opt_log_format = MLP_CPLEX;
2503 GNUNET_free (outputformat);
2506 mlp->pv.BIG_M = (double) BIG_M_VALUE;
2508 mlp->pv.mip_gap = (double) 0.0;
2509 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2510 "MLP_MAX_MIP_GAP", &f_tmp))
2512 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2514 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2519 mlp->pv.mip_gap = f_tmp;
2520 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2525 mlp->pv.lp_mip_gap = (double) 0.0;
2526 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2527 "MLP_MAX_LP_MIP_GAP", &f_tmp))
2529 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2531 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2536 mlp->pv.lp_mip_gap = f_tmp;
2537 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2542 /* Get timeout for iterations */
2543 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats",
2544 "MLP_MAX_DURATION", &max_duration))
2546 max_duration = MLP_MAX_EXEC_DURATION;
2549 /* Get maximum number of iterations */
2550 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(env->cfg, "ats",
2551 "MLP_MAX_ITERATIONS", &max_iterations))
2553 max_iterations = MLP_MAX_ITERATIONS;
2556 /* Get diversity coefficient from configuration */
2557 mlp->pv.co_D = MLP_DEFAULT_D;
2558 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2559 "MLP_COEFFICIENT_D", &f_tmp))
2563 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2564 "MLP_COEFFICIENT_D", f_tmp);
2568 mlp->pv.co_D = f_tmp;
2569 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2570 "MLP_COEFFICIENT_D", f_tmp);
2574 /* Get relativity coefficient from configuration */
2575 mlp->pv.co_R = MLP_DEFAULT_R;
2576 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2577 "MLP_COEFFICIENT_R", &f_tmp))
2581 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2582 "MLP_COEFFICIENT_R", f_tmp);
2586 mlp->pv.co_R = f_tmp;
2587 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2588 "MLP_COEFFICIENT_R", f_tmp);
2593 /* Get utilization coefficient from configuration */
2594 mlp->pv.co_U = MLP_DEFAULT_U;
2595 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2596 "MLP_COEFFICIENT_U", &f_tmp))
2600 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2601 "MLP_COEFFICIENT_U", f_tmp);
2605 mlp->pv.co_U = f_tmp;
2606 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2607 "MLP_COEFFICIENT_U", f_tmp);
2611 /* Get quality metric coefficients from configuration */
2612 for (c = 0; c < RQ_QUALITY_METRIC_COUNT; c++)
2614 /* initialize quality coefficients with default value 1.0 */
2615 mlp->pv.co_Q[c] = MLP_DEFAULT_QUALITY;
2620 GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2621 "MLP_COEFFICIENT_QUALITY_DELAY",
2623 mlp->pv.co_Q[RQ_QUALITY_METRIC_DELAY] = (double) tmp / 100;
2625 mlp->pv.co_Q[RQ_QUALITY_METRIC_DELAY] = MLP_DEFAULT_QUALITY;
2628 GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2629 "MLP_COEFFICIENT_QUALITY_DISTANCE",
2631 mlp->pv.co_Q[RQ_QUALITY_METRIC_DISTANCE] = (double) tmp / 100;
2633 mlp->pv.co_Q[RQ_QUALITY_METRIC_DISTANCE] = MLP_DEFAULT_QUALITY;
2635 /* Get minimum bandwidth per used address from configuration */
2636 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2637 "MLP_MIN_BANDWIDTH",
2642 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2645 /* Get minimum number of connections from configuration */
2646 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2647 "MLP_MIN_CONNECTIONS",
2651 n_min = MLP_DEFAULT_MIN_CONNECTIONS;
2653 /* Init network quotas */
2654 for (c = 0; c < GNUNET_NT_COUNT; c++)
2656 mlp->pv.quota_index[c] = c;
2657 mlp->pv.quota_out[c] = env->out_quota[c];
2658 mlp->pv.quota_in[c] = env->in_quota[c];
2660 LOG (GNUNET_ERROR_TYPE_INFO,
2661 "Quota for network `%s' (in/out) %llu/%llu\n",
2662 GNUNET_NT_to_string (c),
2663 mlp->pv.quota_out[c],
2664 mlp->pv.quota_in[c]);
2665 /* Check if defined quota could make problem unsolvable */
2666 if ((n_min * b_min) > mlp->pv.quota_out[c])
2668 LOG (GNUNET_ERROR_TYPE_INFO,
2669 _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2670 GNUNET_NT_to_string(mlp->pv.quota_index[c]),
2671 mlp->pv.quota_out[c],
2673 mlp->pv.quota_out[c] = (n_min * b_min);
2675 if ((n_min * b_min) > mlp->pv.quota_in[c])
2677 LOG (GNUNET_ERROR_TYPE_INFO,
2678 _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2679 GNUNET_NT_to_string(mlp->pv.quota_index[c]),
2680 mlp->pv.quota_in[c],
2682 mlp->pv.quota_in[c] = (n_min * b_min);
2684 /* Check if bandwidth is too big to make problem solvable */
2685 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
2687 LOG (GNUNET_ERROR_TYPE_INFO,
2688 _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
2689 GNUNET_NT_to_string(mlp->pv.quota_index[c]),
2690 mlp->pv.quota_out[c],
2692 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
2694 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
2696 LOG (GNUNET_ERROR_TYPE_INFO,
2697 _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
2698 GNUNET_NT_to_string(mlp->pv.quota_index[c]),
2699 mlp->pv.quota_in[c],
2701 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
2706 sf.s_add = &GAS_mlp_address_add;
2707 sf.s_address_update_property = &GAS_mlp_address_property_changed;
2708 sf.s_get = &GAS_mlp_get_preferred_address;
2709 sf.s_get_stop = &GAS_mlp_stop_get_preferred_address;
2710 sf.s_pref = &GAS_mlp_address_change_preference;
2711 sf.s_feedback = &GAS_mlp_address_preference_feedback;
2712 sf.s_del = &GAS_mlp_address_delete;
2713 sf.s_bulk_start = &GAS_mlp_bulk_start;
2714 sf.s_bulk_stop = &GAS_mlp_bulk_stop;
2716 /* Setting MLP Input variables */
2717 mlp->pv.b_min = b_min;
2718 mlp->pv.n_min = n_min;
2719 mlp->pv.m_q = RQ_QUALITY_METRIC_COUNT;
2720 mlp->stat_mlp_prob_changed = GNUNET_NO;
2721 mlp->stat_mlp_prob_updated = GNUNET_NO;
2722 mlp->opt_mlp_auto_solve = GNUNET_YES;
2723 mlp->requested_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2724 mlp->stat_bulk_requests = 0;
2725 mlp->stat_bulk_lock = 0;
2728 /* Redirect GLPK output to GNUnet logging */
2729 glp_term_hook (&mlp_term_hook, (void *) mlp);
2731 /* Init LP solving parameters */
2732 glp_init_smcp(&mlp->control_param_lp);
2733 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
2734 if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
2735 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
2737 mlp->control_param_lp.it_lim = max_iterations;
2738 mlp->control_param_lp.tm_lim = max_duration.rel_value_us / 1000LL;
2740 /* Init MLP solving parameters */
2741 glp_init_iocp(&mlp->control_param_mlp);
2742 /* Setting callback function */
2743 mlp->control_param_mlp.cb_func = &mlp_branch_and_cut_cb;
2744 mlp->control_param_mlp.cb_info = mlp;
2745 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
2746 mlp->control_param_mlp.mip_gap = mlp->pv.mip_gap;
2747 if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
2748 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
2749 mlp->control_param_mlp.tm_lim = max_duration.rel_value_us / 1000LL;
2751 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
2756 /* end of plugin_ats_mlp.c */