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 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.
17 * @file ats/plugin_ats_mlp.c
18 * @brief ats mlp problem solver
19 * @author Matthias Wachs
20 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
24 #include "gnunet_ats_service.h"
25 #include "gnunet_ats_plugin.h"
26 #include "gnunet-service-ats_addresses.h"
27 #include "gnunet_statistics_service.h"
32 #define BIG_M_VALUE (UINT32_MAX) /10
33 #define BIG_M_STRING "unlimited"
35 #define MLP_AVERAGING_QUEUE_LENGTH 3
37 #define MLP_MAX_EXEC_DURATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
38 #define MLP_MAX_ITERATIONS 4096
40 #define MLP_DEFAULT_D 1.0
41 #define MLP_DEFAULT_R 1.0
42 #define MLP_DEFAULT_U 1.0
43 #define MLP_DEFAULT_QUALITY 1.0
44 #define MLP_DEFAULT_MIN_CONNECTIONS 4
45 #define MLP_DEFAULT_PEER_PREFERENCE 1.0
48 #define MLP_UNDEFINED 0
52 enum MLP_Output_Format
62 RQ_QUALITY_METRIC_DELAY = 0,
63 RQ_QUALITY_METRIC_DISTANCE = 1,
64 RQ_QUALITY_METRIC_COUNT = 2
69 print_quality_type (enum QualityMetrics qm)
72 case RQ_QUALITY_METRIC_DELAY:
74 case RQ_QUALITY_METRIC_DISTANCE:
90 double lp_objective_value;
91 double mlp_objective_value;
106 struct GNUNET_PeerIdentity id;
108 /* Was this peer already added to the current problem? */
111 /* constraint 2: 1 address per peer*/
114 /* constraint 9: relativity */
117 /* Legacy preference value */
124 * GLPK (MLP) problem object
128 /* Number of addresses in problem */
129 unsigned int num_addresses;
130 /* Number of peers in problem */
131 unsigned int num_peers;
132 /* Number of elements in problem matrix */
133 unsigned int num_elements;
135 /* Row index constraint 2: */
137 /* Row index constraint 4: minimum connections */
139 /* Row index constraint 6: maximize diversity */
141 /* Row index constraint 8: utilization*/
143 /* Row index constraint 9: relativity*/
145 /* Row indices quality metrics */
146 int r_q[RQ_QUALITY_METRIC_COUNT];
147 /* Row indices ATS network quotas */
148 int r_quota[GNUNET_ATS_NetworkTypeCount];
150 /* Column index Diversity (D) column */
152 /* Column index Utilization (U) column */
154 /* Column index Proportionality (R) column */
156 /* Column index quality metrics */
157 int c_q[RQ_QUALITY_METRIC_COUNT];
162 /* Row index array */
164 /* Column index array */
166 /* Column index value */
173 /* Big M value for bandwidth capping */
182 /* Number of quality metrics @deprecated, use RQ_QUALITY_METRIC_COUNT */
185 /* Number of quality metrics */
188 /* Quality metric coefficients*/
189 double co_Q[RQ_QUALITY_METRIC_COUNT];
191 /* Ressource costs coefficients*/
192 double co_RC[RQ_QUALITY_METRIC_COUNT];
194 /* Diversity coefficient */
197 /* Utility coefficient */
200 /* Relativity coefficient */
203 /* Minimum bandwidth assigned to an address */
206 /* Minimum number of addresses with bandwidth assigned */
210 /* Array mapping array index to ATS network */
211 int quota_index[GNUNET_ATS_NetworkTypeCount];
212 /* Outbound quotas */
213 unsigned long long quota_out[GNUNET_ATS_NetworkTypeCount];
216 unsigned long long quota_in[GNUNET_ATS_NetworkTypeCount];
218 /* ATS ressource costs
219 * array with GNUNET_ATS_QualityPropertiesCount elements
220 * contains mapping to GNUNET_ATS_Property
222 int rc[RQ_QUALITY_METRIC_COUNT];
229 struct GAS_MLP_Handle
231 struct GNUNET_ATS_PluginEnvironment *env;
234 * Exclude peer from next result propagation
236 const struct GNUNET_PeerIdentity *exclude_peer;
239 * Encapsulation for the MLP problem
241 struct MLP_Problem p;
244 * Encapsulation for the MLP problem variables
246 struct MLP_Variables pv;
249 * Encapsulation for the MLP solution
251 struct MLP_Solution ps;
259 * Number of changes while solver was locked
261 int stat_bulk_requests;
264 * GLPK LP control parameter
266 glp_smcp control_param_lp;
269 * GLPK LP control parameter
271 glp_iocp control_param_mlp;
274 * Peers with pending address requests
276 struct GNUNET_CONTAINER_MultiPeerMap *requested_peers;
279 * Was the problem updated since last solution
281 int stat_mlp_prob_updated;
284 * Has the problem size changed since last solution
286 int stat_mlp_prob_changed;
289 * Solve the problem automatically when updates occur?
290 * Default: GNUNET_YES
291 * Can be disabled for test and measurements
293 int opt_mlp_auto_solve;
296 * Write all MILP problems to a MPS file
298 int opt_dump_problem_all;
301 * Write all MILP problem solutions to a file
303 int opt_dump_solution_all;
306 * Write MILP problems to a MPS file when solver fails
308 int opt_dump_problem_on_fail;
311 * Write MILP problem solutions to a file when solver fails
313 int opt_dump_solution_on_fail;
316 * solve feasibility only
318 int opt_dbg_feasibility_only;
321 * solve autoscale the problem
323 int opt_dbg_autoscale_problem;
326 * use the intopt presolver instead of simplex
328 int opt_dbg_intopt_presolver;
333 int opt_dbg_glpk_verbose;
336 * solve autoscale the problem
338 int opt_dbg_optimize_relativity;
341 * solve autoscale the problem
343 int opt_dbg_optimize_diversity;
346 * solve autoscale the problem
348 int opt_dbg_optimize_quality;
351 * solve autoscale the problem
353 int opt_dbg_optimize_utility;
359 enum MLP_Output_Format opt_log_format;
363 * Address specific MLP information
365 struct MLP_information
369 * Bandwidth assigned outbound
374 * Bandwidth assigned inbound
384 * bandwidth column index
389 * address usage column
396 * constraint 1: bandwidth capping
401 * constraint 3: minimum bandwidth
410 * NOTE: Do not modify this documentation. This documentation is based on
411 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
412 * use build_txt.sh to generate plaintext output
414 * The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
415 * optimizing an mixed integer programming problem. The MLP solver uses a
416 * number of constraints to find the best adddress for a peer and an optimal
417 * bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
420 * We defined a constraint system to find an optimal bandwidth assignment.
421 * This constraint system uses as an input data addresses, bandwidth quotas,
422 * preferences and quality values. This constraint system is stored in an
423 * matrix based equotation system.
427 * A (M)LP problem consists of a target function to optimizes, constraints
428 * and rows and columns. FIXME GLP uses three arrays to index the matrix: two
429 * integer arrays storing the row and column indices in the matrix and an
430 * float array to store the coeeficient.
432 * To solve the problem we first find an initial solution for the LP problem
433 * using the LP solver and then find an MLP solution based on this solution
434 * using the MLP solver.
436 * Solving (M)LP problems has the property that finding an initial solution
437 * for the LP problem is computationally expensive and finding the MLP
438 * solution is cheaper. This is especially interesting an existing LP
439 * solution can be reused if only coefficients in the matrix have changed
440 * (addresses updated). Only when the problem size changes (addresses added
441 * or deleted) a new LP solution has to be found.
444 * The mlp solver solves the bandwidth assignment problem only on demand when
445 * an address suggestion is requested. When an address is requested mlp the
446 * solves the mlp problem and if the active address or the bandwidth assigned
447 * changes it calls the callback to addresses. The mlp solver gets notified
448 * about new addresses (adding sessions), removed addresses (address
449 * deletions) and address updates. To benefit from the mlp properties
450 * mentioned in section 5 the solver rembers if since the last solution
451 * addresses were added or deleted (problem size changed, problem has to be
452 * rebuild and solved from sratch) or if addresses were updated and the
453 * existing solution can be reused.
457 * The quotas for each network segment are passed by addresses. MLP can be
458 * adapted using configuration settings and uses the following parameters:
459 * * MLP_MAX_DURATION:
460 * Maximum duration for a MLP solution procees (default: 3 sec.)
461 * * MLP_MAX_ITERATIONS:
462 * Maximum number of iterations for a MLP solution process (default:
464 * * MLP_MIN_CONNECTIONS:
465 * Minimum number of desired connections (default: 4)
466 * * MLP_MIN_BANDWIDTH:
467 * Minimum amount of bandwidth assigned to an address (default: 1024)
468 * * MLP_COEFFICIENT_D:
469 * Diversity coefficient (default: 1.0)
470 * * MLP_COEFFICIENT_R:
471 * Relativity coefficient (default: 1.0)
472 * * MLP_COEFFICIENT_U:
473 * Utilization coefficient (default: 1.0)
474 * * MLP_COEFFICIENT_D:
475 * Diversity coefficient (default: 1.0)
476 * * MLP_COEFFICIENT_QUALITY_DELAY:
477 * Quality delay coefficient (default: 1.0)
478 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
479 * Quality distance coefficient (default: 1.0)
480 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
481 * Quality distance coefficient (default: 1.0)
482 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
483 * Quality distance coefficient (default: 1.0)
484 * * MLP_COEFFICIENT_QUALITY_DISTANCE:
485 * Quality distance coefficient (default: 1.0)
487 * 5.2 Data structures used
489 * mlp has for each known peer a struct ATS_Peer containing information about
490 * a specific peer. The address field solver_information contains information
491 * about the mlp properties of this address.
495 * During initialization mlp initializes the GLPK libray used to solve the
496 * MLP problem: it initializes the glpk environment and creates an initial LP
497 * problem. Next it loads the configuration values from the configuration or
498 * uses the default values configured in -addresses_mlp.h. The quotas used
499 * are given by addresses but may have to be adjusted. mlp uses a upper limit
500 * for the bandwidth assigned called BIG M and a minimum amount of bandwidth
501 * an address gets assigned as well as a minium desired number of
502 * connections. If the configured quota is bigger than BIG M, it is reduced
503 * to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
504 * *MLP_MIN_BANDWIDTH it is increased to this value.
510 #define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
513 * Print debug output for mlp problem creation
515 #define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO
519 * Intercept GLPK terminal output
520 * @param info the mlp handle
521 * @param s the string to print
522 * @return 0: glpk prints output on terminal, 0 != surpress output
525 mlp_term_hook (void *info, const char *s)
527 struct GAS_MLP_Handle *mlp = info;
529 if (mlp->opt_dbg_glpk_verbose)
530 LOG (GNUNET_ERROR_TYPE_ERROR, "%s", s);
536 * Reset peers for next problem creation
538 * @param cls not used
540 * @param value ATS_Peer
544 reset_peers (void *cls,
545 const struct GNUNET_PeerIdentity *key,
548 struct ATS_Peer *peer = value;
549 peer->processed = GNUNET_NO;
554 * Delete the MLP problem and free the constrain matrix
556 * @param mlp the MLP handle
559 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
564 if (mlp->p.prob != NULL)
566 glp_delete_prob(mlp->p.prob);
570 /* delete row index */
571 if (mlp->p.ia != NULL)
573 GNUNET_free (mlp->p.ia);
577 /* delete column index */
578 if (mlp->p.ja != NULL)
580 GNUNET_free (mlp->p.ja);
584 /* delete coefficients */
585 if (mlp->p.ar != NULL)
587 GNUNET_free (mlp->p.ar);
593 mlp->p.c_d = MLP_UNDEFINED;
594 mlp->p.c_r = MLP_UNDEFINED;
595 mlp->p.r_c2 = MLP_UNDEFINED;
596 mlp->p.r_c4 = MLP_UNDEFINED;
597 mlp->p.r_c6 = MLP_UNDEFINED;
598 mlp->p.r_c9 = MLP_UNDEFINED;
599 for (c = 0; c < RQ_QUALITY_METRIC_COUNT ; c ++)
600 mlp->p.r_q[c] = MLP_UNDEFINED;
601 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c ++)
602 mlp->p.r_quota[c] = MLP_UNDEFINED;
603 mlp->p.ci = MLP_UNDEFINED;
606 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
612 * Translate glpk status error codes to text
613 * @param retcode return code
614 * @return string with result
617 mlp_status_to_string (int retcode)
621 return "solution is undefined";
623 return "solution is feasible";
625 return "solution is infeasible";
627 return "no feasible solution exists";
629 return "solution is optimal";
631 return "solution is unbounded";
634 return "unknown error";
640 * Translate glpk solver error codes to text
641 * @param retcode return code
642 * @return string with result
645 mlp_solve_to_string (int retcode)
651 return "invalid basis";
653 return "singular matrix";
655 return "ill-conditioned matrix";
657 return "invalid bounds";
659 return "solver failed";
661 return "objective lower limit reached";
663 return "objective upper limit reached";
665 return "iteration limit exceeded";
667 return "time limit exceeded";
669 return "no primal feasible solution";
671 return "no dual feasible solution";
673 return "root LP optimum not provided";
675 return "search terminated by application";
677 return "relative mip gap tolerance reached";
679 return "no dual feasible solution";
681 return "no convergence";
683 return "numerical instability";
685 return "invalid data";
687 return "result out of range";
690 return "unknown error";
697 const struct GNUNET_CONTAINER_MultiPeerMap *map;
702 mlp_create_problem_count_addresses_it (void *cls,
703 const struct GNUNET_PeerIdentity *key,
706 struct CountContext *cctx = cls;
708 /* Check if we have to add this peer due to a pending request */
709 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
716 mlp_create_problem_count_addresses (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
717 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
719 struct CountContext cctx;
721 cctx.map = requested_peers;
723 GNUNET_CONTAINER_multipeermap_iterate (addresses,
724 &mlp_create_problem_count_addresses_it, &cctx);
730 mlp_create_problem_count_peers_it (void *cls,
731 const struct GNUNET_PeerIdentity *key,
734 struct CountContext *cctx = cls;
736 /* Check if we have to addresses for the requested peer */
737 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
744 mlp_create_problem_count_peers (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
745 const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
747 struct CountContext cctx;
749 cctx.map = addresses;
751 GNUNET_CONTAINER_multipeermap_iterate (requested_peers,
752 &mlp_create_problem_count_peers_it, &cctx);
758 * Updates an existing value in the matrix
760 * Extract the row, updates the value and updates the row in the problem
762 * @param p the mlp problem
763 * @param row the row to create the value in
764 * @param col the column to create the value in
765 * @param val the value to set
766 * @param line calling line for debbuging
767 * @return GNUNET_YES value changed, GNUNET_NO value did not change, GNUNET_SYSERR
771 mlp_create_problem_update_value (struct MLP_Problem *p,
772 int row, int col, double val,
783 GNUNET_assert (NULL != p->prob);
785 /* Get number of columns and prepare data structure */
786 c_cols = glp_get_num_cols(p->prob);
788 return GNUNET_SYSERR;
790 val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
791 GNUNET_assert (NULL != val_array);
792 ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
793 GNUNET_assert (NULL != ind_array);
794 /* Extract the row */
796 /* Update the value */
797 c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array);
799 for (c1 = 1; c1 < (c_elems+1); c1++)
801 if (ind_array[c1] == col)
807 if (GNUNET_NO == found)
809 ind_array[c_elems+1] = col;
810 val_array[c_elems+1] = val;
811 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Setting value in [%s : %s] to `%.2f'\n",
812 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
814 glp_set_mat_row (p->prob, row, c_elems+1, ind_array, val_array);
815 GNUNET_free (ind_array);
816 GNUNET_free (val_array);
822 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Updating value in [%s : %s] from `%.2f' to `%.2f'\n",
823 glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
825 if (val != val_array[c1])
830 /* Update the row in the matrix */
831 glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
834 GNUNET_free (ind_array);
835 GNUNET_free (val_array);
840 * Creates a new value in the matrix
842 * Sets the row and column index in the problem array and increments the
845 * @param p the mlp problem
846 * @param row the row to create the value in
847 * @param col the column to create the value in
848 * @param val the value to set
849 * @param line calling line for debbuging
852 mlp_create_problem_set_value (struct MLP_Problem *p,
853 int row, int col, double val,
856 if ((p->ci) >= p->num_elements)
858 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
859 line, p->ci + 1, p->num_elements);
863 if ((0 == row) || (0 == col))
866 LOG (GNUNET_ERROR_TYPE_ERROR, "[P]: Invalid call from line %u: row = %u, col = %u\n",
872 #if DEBUG_MLP_PROBLEM_CREATION
873 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u == %.2f\n",
874 line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
880 mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
881 unsigned int type, unsigned int bound, double lb, double ub,
884 int col = glp_add_cols (p->prob, 1);
885 glp_set_col_name (p->prob, col, name);
886 glp_set_col_bnds (p->prob, col, bound, lb, ub);
887 glp_set_col_kind (p->prob, col, type);
888 glp_set_obj_coef (p->prob, col, coef);
889 #if DEBUG_MLP_PROBLEM_CREATION
890 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
897 mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
898 unsigned int bound, double lb, double ub)
901 int row = glp_add_rows (p->prob, 1);
903 glp_set_row_name (p->prob, row, name);
904 /* set row bounds: <= 0 */
905 glp_set_row_bnds (p->prob, row, bound, lb, ub);
909 GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
912 GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
915 GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
918 GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
921 GNUNET_asprintf(&op, "ERROR");
924 #if DEBUG_MLP_PROBLEM_CREATION
925 LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
934 * - address columns b and n
935 * - address dependent constraint rows c1, c3
936 * - peer dependent rows c2 and c9
937 * - Set address dependent entries in problem matrix as well
940 mlp_create_problem_add_address_information (void *cls,
941 const struct GNUNET_PeerIdentity *key,
944 struct GAS_MLP_Handle *mlp = cls;
945 struct MLP_Problem *p = &mlp->p;
946 struct ATS_Address *address = value;
947 struct ATS_Peer *peer;
948 struct MLP_information *mlpi;
952 uint32_t addr_net_index;
953 unsigned long long max_quota;
956 /* Check if we have to add this peer due to a pending request */
957 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(mlp->requested_peers, key))
960 mlpi = address->solver_information;
963 fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
968 addr_net = address->properties.scope;
969 for (addr_net_index = 0; addr_net_index < GNUNET_ATS_NetworkTypeCount; addr_net_index++)
971 if (mlp->pv.quota_index[addr_net_index] == addr_net)
975 if (addr_net_index >= GNUNET_ATS_NetworkTypeCount)
982 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
984 if (mlp->pv.quota_out[c] > max_quota)
985 max_quota = mlp->pv.quota_out[c];
986 if (mlp->pv.quota_in[c] > max_quota)
987 max_quota = mlp->pv.quota_in[c];
989 if (max_quota > mlp->pv.BIG_M)
990 cur_bigm = (double) mlp->pv.BIG_M;
992 cur_bigm = max_quota;
996 peer = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, key);
997 GNUNET_assert (NULL != peer);
998 if (peer->processed == GNUNET_NO)
1000 /* Add peer dependent constraints */
1001 /* Add c2) One address active per peer */
1002 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
1003 peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
1005 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1007 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1009 /* Add c9) Relativity */
1010 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
1011 peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
1013 /* c9) set coefficient */
1014 mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f , __LINE__);
1017 peer->processed = GNUNET_YES;
1020 /* Reset addresses' solver information */
1027 /* Add bandwidth column */
1028 GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
1029 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1031 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
1035 /* Maximize for bandwidth assignment in feasibility testing */
1036 mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
1040 /* Add address active column */
1041 GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
1042 mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
1045 /* Add address dependent constraints */
1046 /* Add c1) bandwidth capping: b_t + (-M) * n_t <= 0 */
1047 GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
1048 mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
1050 /* c1) set b = 1 coefficient */
1051 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
1052 /* c1) set n = - min (M, quota) coefficient */
1053 cur_bigm = (double) mlp->pv.quota_out[addr_net_index];
1054 if (cur_bigm > mlp->pv.BIG_M)
1055 cur_bigm = (double) mlp->pv.BIG_M;
1056 mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -cur_bigm, __LINE__);
1058 /* Add constraint c 3) minimum bandwidth
1059 * b_t + (-n_t * b_min) >= 0
1061 GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
1062 mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
1065 /* c3) set b = 1 coefficient */
1066 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
1067 /* c3) set n = -b_min coefficient */
1068 mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);
1071 /* Set coefficient entries in invariant rows */
1075 /* c 4) minimum connections */
1076 mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
1077 /* c 2) 1 address peer peer */
1078 mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
1079 /* c 10) obey network specific quotas
1080 * (1)*b_1 + ... + (1)*b_m <= quota_n
1082 mlp_create_problem_set_value (p, p->r_quota[addr_net_index], mlpi->c_b, 1, __LINE__);
1085 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1087 /* c 6) maximize diversity */
1088 mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
1089 /* c 9) relativity */
1090 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1091 mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
1093 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1094 mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
1095 /* c 7) Optimize quality */
1096 /* For all quality metrics, set quality of this address */
1097 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1099 mlp_create_problem_set_value (p,
1100 p->r_q[RQ_QUALITY_METRIC_DELAY],
1102 address->norm_delay.norm,
1104 mlp_create_problem_set_value (p,
1105 p->r_q[RQ_QUALITY_METRIC_DISTANCE],
1107 address->norm_distance.norm,
1117 * Create the invariant columns c4, c6, c10, c8, c7
1120 mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
1126 /* Row for c4) minimum connection */
1127 /* Number of minimum connections is min(|Peers|, n_min) */
1128 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);
1130 /* Rows for c 10) Enforce network quotas */
1131 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1134 GNUNET_asprintf(&text, "c10_quota_ats_%s",
1135 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
1136 p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
1141 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1144 /* Add row for c6) Maximize for diversity */
1145 if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
1147 p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
1148 /* Set c6 ) Setting -D */
1149 mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
1152 /* Adding rows for c 8) Maximize utility */
1153 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1155 p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
1157 mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
1160 /* For all quality metrics:
1161 * c 7) Maximize quality, austerity */
1162 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1164 for (c = 0; c < mlp->pv.m_q; c++)
1166 GNUNET_asprintf (&name,
1168 print_quality_type (c));
1169 p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
1171 mlp_create_problem_set_value (p,
1173 p->c_q[c], -1, __LINE__);
1181 * Create the invariant columns d, u, r, q0 ... qm
1184 mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
1186 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
1191 /* Diversity d column */
1192 if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
1193 p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);
1195 /* Utilization u column */
1196 if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
1197 p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);
1199 /* Relativity r column */
1200 if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
1201 p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);
1203 /* Quality metric columns */
1204 if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
1206 for (c = 0; c < mlp->pv.m_q; c++)
1208 GNUNET_asprintf (&name, "q_%u", c);
1209 p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
1218 * Create the MLP problem
1220 * @param mlp the MLP handle
1221 * @return #GNUNET_OK or #GNUNET_SYSERR
1224 mlp_create_problem (struct GAS_MLP_Handle *mlp)
1226 struct MLP_Problem *p = &mlp->p;
1227 int res = GNUNET_OK;
1229 GNUNET_assert (p->prob == NULL);
1230 GNUNET_assert (p->ia == NULL);
1231 GNUNET_assert (p->ja == NULL);
1232 GNUNET_assert (p->ar == NULL);
1233 /* Reset MLP problem struct */
1235 /* create the glpk problem */
1236 p->prob = glp_create_prob ();
1237 GNUNET_assert (NULL != p->prob);
1238 p->num_peers = mlp_create_problem_count_peers (mlp->requested_peers, mlp->env->addresses);
1239 p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers,
1240 mlp->env->addresses);
1242 /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
1243 p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses +
1244 mlp->pv.m_q + p->num_peers + 2 + 1);
1245 LOG (GNUNET_ERROR_TYPE_DEBUG,
1246 "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
1252 /* Set a problem name */
1253 glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
1254 /* Set optimization direction to maximize */
1255 glp_set_obj_dir (p->prob, GLP_MAX);
1257 /* Create problem matrix */
1258 /* last +1 caused by glpk index starting with one: [1..elements]*/
1261 p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
1263 p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
1265 p->ar = GNUNET_malloc (p->num_elements * sizeof (double));
1267 if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
1269 LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
1270 return GNUNET_SYSERR;
1273 /* Adding invariant columns */
1274 mlp_create_problem_add_invariant_columns (mlp, p);
1276 /* Adding address independent constraint rows */
1277 mlp_create_problem_add_invariant_rows (mlp, p);
1279 /* Adding address dependent columns constraint rows */
1280 GNUNET_CONTAINER_multipeermap_iterate (mlp->env->addresses,
1281 &mlp_create_problem_add_address_information,
1284 /* Load the matrix */
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
1286 glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
1287 if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
1289 glp_scale_prob (p->prob, GLP_SF_AUTO);
1297 * Solves the LP problem
1299 * @param mlp the MLP Handle
1300 * @return #GNUNET_OK if could be solved, #GNUNET_SYSERR on failure
1303 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
1307 res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
1309 LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: %s\n",
1310 mlp_solve_to_string (res));
1312 LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem failed: %s\n",
1313 mlp_solve_to_string (res));
1315 /* Analyze problem status */
1316 res_status = glp_get_status (mlp->p.prob);
1317 switch (res_status) {
1318 case GLP_OPT: /* solution is optimal */
1319 LOG (GNUNET_ERROR_TYPE_INFO,
1320 "Solving LP problem: %s, %s\n",
1321 mlp_solve_to_string(res),
1322 mlp_status_to_string(res_status));
1325 LOG (GNUNET_ERROR_TYPE_ERROR,
1326 "Solving LP problem failed: %s %s\n",
1327 mlp_solve_to_string(res),
1328 mlp_status_to_string(res_status));
1329 return GNUNET_SYSERR;
1335 * Propagates the results when MLP problem was solved
1337 * @param cls the MLP handle
1338 * @param key the peer identity
1339 * @param value the address
1340 * @return #GNUNET_OK to continue
1343 mlp_propagate_results (void *cls,
1344 const struct GNUNET_PeerIdentity *key,
1347 struct GAS_MLP_Handle *mlp = cls;
1348 struct ATS_Address *address;
1349 struct MLP_information *mlpi;
1350 double mlp_bw_in = MLP_NaN;
1351 double mlp_bw_out = MLP_NaN;
1352 double mlp_use = MLP_NaN;
1354 /* Check if we have to add this peer due to a pending request */
1355 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mlp->requested_peers,
1361 GNUNET_assert (address->solver_information != NULL);
1362 mlpi = address->solver_information;
1364 mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
1365 if (mlp_bw_in > (double) UINT32_MAX)
1367 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
1368 mlp_bw_in = (double) UINT32_MAX;
1370 mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
1371 if (mlp_bw_out > (double) UINT32_MAX)
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
1374 mlp_bw_out = (double) UINT32_MAX;
1376 mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);
1380 * LOG (GNUNET_ERROR_TYPE_INFO, "MLP result address: `%s' `%s' length %u session %u, mlp use %.3f\n",
1381 * GNUNET_i2s(&address->peer), address->plugin,
1382 * address->addr_len, address->session_id);
1385 if (GLP_YES == mlp_use)
1387 /* This address was selected by the solver to be used */
1388 mlpi->n = GNUNET_YES;
1389 if (GNUNET_NO == address->active)
1391 /* Address was not used before, enabling address */
1392 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n",
1393 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1394 address->active = GNUNET_YES;
1395 address->assigned_bw_in = mlp_bw_in;
1396 mlpi->b_in = mlp_bw_in;
1397 address->assigned_bw_out = mlp_bw_out;
1398 mlpi->b_out = mlp_bw_out;
1399 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1400 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
1403 else if (GNUNET_YES == address->active)
1405 /* Address was used before, check for bandwidth change */
1406 if ((mlp_bw_out != address->assigned_bw_out) ||
1407 (mlp_bw_in != address->assigned_bw_in))
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n",
1410 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1411 address->assigned_bw_in = mlp_bw_in;
1412 mlpi->b_in = mlp_bw_in;
1413 address->assigned_bw_out = mlp_bw_out;
1414 mlpi->b_out = mlp_bw_out;
1415 if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
1416 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
1423 else if (GLP_NO == mlp_use)
1425 /* This address was selected by the solver to be not used */
1426 mlpi->n = GNUNET_NO;
1427 if (GNUNET_NO == address->active)
1429 /* Address was not used before, nothing to do */
1430 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n",
1431 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1434 else if (GNUNET_YES == address->active)
1436 /* Address was used before, disabling address */
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n",
1438 (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
1439 address->active = GNUNET_NO;
1440 /* Set bandwidth to 0 */
1441 address->assigned_bw_in = 0;
1443 address->assigned_bw_out = 0;
1458 notify (struct GAS_MLP_Handle *mlp,
1459 enum GAS_Solver_Operation op,
1460 enum GAS_Solver_Status stat,
1461 enum GAS_Solver_Additional_Information add)
1463 mlp->env->info_cb (mlp->env->cls,
1471 mlp_branch_and_cut_cb (glp_tree *tree, void *info)
1473 struct GAS_MLP_Handle *mlp = info;
1476 switch (glp_ios_reason (tree))
1479 /* Do nothing here */
1482 /* Do nothing here */
1485 /* Do nothing here */
1488 /* Do nothing here */
1491 /* Do nothing here */
1494 /* Do nothing here */
1497 /* A better solution was found */
1498 mlp->ps.mlp_gap = glp_ios_mip_gap (tree);
1499 mlp_obj = glp_mip_obj_val (mlp->p.prob);
1500 mlp->ps.lp_mlp_gap = (abs(mlp_obj - mlp->ps.lp_objective_value)) / (abs(mlp_obj) + DBL_EPSILON);
1502 LOG (GNUNET_ERROR_TYPE_INFO,
1503 "Found better integer solution, current gaps: %.3f <= %.3f, %.3f <= %.3f\n",
1504 mlp->ps.mlp_gap, mlp->pv.mip_gap,
1505 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1507 if (mlp->ps.mlp_gap <= mlp->pv.mip_gap)
1509 LOG (GNUNET_ERROR_TYPE_INFO,
1510 "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
1511 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1512 glp_ios_terminate (tree);
1515 if (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap)
1517 LOG (GNUNET_ERROR_TYPE_INFO,
1518 "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
1519 mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
1520 glp_ios_terminate (tree);
1532 * Solves the MLP problem
1534 * @param solver the MLP Handle
1535 * @return #GNUNET_OK if could be solved, #GNUNET_SYSERR on failure
1538 GAS_mlp_solve_problem (void *solver)
1540 struct GAS_MLP_Handle *mlp = solver;
1546 struct GNUNET_TIME_Absolute start_total;
1547 struct GNUNET_TIME_Absolute start_cur_op;
1548 struct GNUNET_TIME_Relative dur_total;
1549 struct GNUNET_TIME_Relative dur_setup;
1550 struct GNUNET_TIME_Relative dur_lp;
1551 struct GNUNET_TIME_Relative dur_mlp;
1553 GNUNET_assert(NULL != solver);
1555 if (GNUNET_YES == mlp->stat_bulk_lock)
1557 mlp->stat_bulk_requests++;
1560 notify(mlp, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS,
1561 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1562 start_total = GNUNET_TIME_absolute_get();
1564 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->requested_peers))
1566 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1567 return GNUNET_OK; /* No pending requests */
1569 if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->env->addresses))
1571 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1572 return GNUNET_OK; /* No addresses available */
1575 if ((GNUNET_NO == mlp->stat_mlp_prob_changed)
1576 && (GNUNET_NO == mlp->stat_mlp_prob_updated))
1578 LOG(GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
1579 notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
1582 if (GNUNET_YES == mlp->stat_mlp_prob_changed)
1584 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
1585 notify(mlp, GAS_OP_SOLVE_SETUP_START, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1586 mlp_delete_problem (mlp);
1587 if (GNUNET_SYSERR == mlp_create_problem (mlp))
1589 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_FAIL, GAS_INFO_FULL);
1590 return GNUNET_SYSERR;
1592 notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_SUCCESS, GAS_INFO_FULL);
1593 if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
1595 mlp->control_param_lp.presolve = GLP_YES; /* LP presolver, we need lp solution */
1596 mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
1600 mlp->control_param_lp.presolve = GNUNET_NO; /* LP presolver, we need lp solution */
1601 mlp->control_param_mlp.presolve = GLP_YES; /* No presolver, we have LP solution */
1602 dur_lp = GNUNET_TIME_UNIT_ZERO;
1607 LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
1610 /* Reset solution info */
1611 mlp->ps.lp_objective_value = 0.0;
1612 mlp->ps.mlp_gap = 1.0;
1613 mlp->ps.mlp_objective_value = 0.0;
1614 mlp->ps.lp_mlp_gap = 0.0;
1616 dur_setup = GNUNET_TIME_absolute_get_duration (start_total);
1619 if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
1621 notify(mlp, GAS_OP_SOLVE_MLP_LP_START, GAS_STAT_SUCCESS,
1622 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1623 LOG(GNUNET_ERROR_TYPE_DEBUG,
1624 "Running LP solver %s\n",
1625 (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
1626 start_cur_op = GNUNET_TIME_absolute_get();
1629 /* Only for debugging:
1630 * Always use LP presolver:
1631 * mlp->control_param_lp.presolve = GLP_YES; */
1632 res_lp = mlp_solve_lp_problem(mlp);
1633 if (GNUNET_OK == res_lp)
1635 mlp->ps.lp_objective_value = glp_get_obj_val (mlp->p.prob);
1636 LOG (GNUNET_ERROR_TYPE_DEBUG,
1637 "LP solution was: %.3f\n",
1638 mlp->ps.lp_objective_value);
1641 dur_lp = GNUNET_TIME_absolute_get_duration (start_cur_op);
1642 notify(mlp, GAS_OP_SOLVE_MLP_LP_STOP,
1643 (GNUNET_OK == res_lp) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1644 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1647 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
1650 /* Run MLP solver */
1651 if ((GNUNET_OK == res_lp) || (GNUNET_YES == mlp->opt_dbg_intopt_presolver))
1653 LOG(GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
1654 notify(mlp, GAS_OP_SOLVE_MLP_MLP_START, GAS_STAT_SUCCESS,
1655 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1656 start_cur_op = GNUNET_TIME_absolute_get();
1660 /* Only for debugging, always use LP presolver */
1661 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
1662 mlp->control_param_mlp.presolve = GNUNET_YES;
1664 mip_res = glp_intopt (mlp->p.prob, &mlp->control_param_mlp);
1669 LOG (GNUNET_ERROR_TYPE_INFO,
1670 "Solving MLP problem: %s\n",
1671 mlp_solve_to_string (mip_res));
1673 case GLP_ETMLIM: /* Time limit reached */
1674 case GLP_EMIPGAP: /* MIP gap tolerance limit reached */
1675 case GLP_ESTOP: /* Solver was instructed to stop*/
1676 /* Semi-successful */
1677 LOG (GNUNET_ERROR_TYPE_INFO,
1678 "Solving MLP problem solution was interupted: %s\n",
1679 mlp_solve_to_string (mip_res));
1688 LOG (GNUNET_ERROR_TYPE_INFO,
1689 "Solving MLP problem failed: %s\n",
1690 mlp_solve_to_string (mip_res));
1694 /* Analyze problem status */
1695 mip_status = glp_mip_status(mlp->p.prob);
1698 case GLP_OPT: /* solution is optimal */
1699 LOG (GNUNET_ERROR_TYPE_WARNING,
1700 "Solution of MLP problem is optimal: %s, %s\n",
1701 mlp_solve_to_string (mip_res),
1702 mlp_status_to_string (mip_status));
1703 mip_res = GNUNET_OK;
1705 case GLP_FEAS: /* solution is feasible but not proven optimal */
1707 if ( (mlp->ps.mlp_gap <= mlp->pv.mip_gap) ||
1708 (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap) )
1710 LOG (GNUNET_ERROR_TYPE_INFO,
1711 "Solution of MLP problem is feasible and solution within gap constraints: %s, %s\n",
1712 mlp_solve_to_string (mip_res),
1713 mlp_status_to_string (mip_status));
1714 mip_res = GNUNET_OK;
1718 LOG (GNUNET_ERROR_TYPE_WARNING,
1719 "Solution of MLP problem is feasible but solution not within gap constraints: %s, %s\n",
1720 mlp_solve_to_string (mip_res),
1721 mlp_status_to_string (mip_status));
1722 mip_res = GNUNET_SYSERR;
1725 case GLP_UNDEF: /* Solution undefined */
1726 case GLP_NOFEAS: /* No feasible solution */
1728 LOG (GNUNET_ERROR_TYPE_ERROR,
1729 "Solving MLP problem failed: %s %s\n",
1730 mlp_solve_to_string (mip_res),
1731 mlp_status_to_string (mip_status));
1732 mip_res = GNUNET_SYSERR;
1736 dur_mlp = GNUNET_TIME_absolute_get_duration (start_cur_op);
1737 dur_total = GNUNET_TIME_absolute_get_duration (start_total);
1739 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP,
1740 (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1741 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1745 /* Do not execute mip solver since lp solution is invalid */
1746 dur_mlp = GNUNET_TIME_UNIT_ZERO;
1747 dur_total = GNUNET_TIME_absolute_get_duration (start_total);
1749 notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP, GAS_STAT_FAIL,
1750 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1751 mip_res = GNUNET_SYSERR;
1754 /* Notify about end */
1755 notify(mlp, GAS_OP_SOLVE_STOP,
1756 ((GNUNET_OK == mip_res) && (GNUNET_OK == mip_res)) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1757 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
1759 LOG (GNUNET_ERROR_TYPE_DEBUG,
1760 "Execution time for %s solve: (total/setup/lp/mlp) : %llu %llu %llu %llu\n",
1761 (GNUNET_YES == mlp->stat_mlp_prob_changed) ? "full" : "updated",
1762 (unsigned long long) dur_total.rel_value_us,
1763 (unsigned long long) dur_setup.rel_value_us,
1764 (unsigned long long) dur_lp.rel_value_us,
1765 (unsigned long long) dur_mlp.rel_value_us);
1768 mlp->ps.lp_res = res_lp;
1769 mlp->ps.mip_res = mip_res;
1770 mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
1771 mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
1772 mlp->ps.p_cols = glp_get_num_cols(mlp->p.prob);
1773 mlp->ps.p_rows = glp_get_num_rows(mlp->p.prob);
1774 mlp->ps.p_elements = mlp->p.num_elements;
1776 /* Propagate result*/
1777 notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
1778 (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1780 if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
1782 GNUNET_CONTAINER_multipeermap_iterate(mlp->env->addresses,
1783 &mlp_propagate_results, mlp);
1785 notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
1786 (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
1789 struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
1790 if ( (GNUNET_YES == mlp->opt_dump_problem_all) ||
1791 (mlp->opt_dump_problem_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
1793 /* Write problem to disk */
1794 switch (mlp->opt_log_format) {
1796 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.cplex", mlp->p.num_peers,
1797 mlp->p.num_addresses, time.abs_value_us);
1798 glp_write_lp (mlp->p.prob, NULL, filename);
1801 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.glpk", mlp->p.num_peers,
1802 mlp->p.num_addresses, time.abs_value_us);
1803 glp_write_prob (mlp->p.prob, 0, filename);
1806 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers,
1807 mlp->p.num_addresses, time.abs_value_us);
1808 glp_write_mps (mlp->p.prob, GLP_MPS_FILE, NULL, filename);
1813 LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped problem to file: `%s' \n", filename);
1814 GNUNET_free(filename);
1816 if ( (mlp->opt_dump_solution_all) ||
1817 (mlp->opt_dump_solution_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
1819 /* Write solution to disk */
1820 GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers,
1821 mlp->p.num_addresses, time.abs_value_us);
1822 glp_print_mip(mlp->p.prob, filename);
1823 LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped solution to file: `%s' \n", filename);
1824 GNUNET_free(filename);
1827 /* Reset change and update marker */
1828 mlp->control_param_lp.presolve = GLP_NO;
1829 mlp->stat_mlp_prob_updated = GNUNET_NO;
1830 mlp->stat_mlp_prob_changed = GNUNET_NO;
1832 if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
1835 return GNUNET_SYSERR;
1839 * Add a single address to the solve
1841 * @param solver the solver Handle
1842 * @param address the address to add
1843 * @param network network type of this address
1846 GAS_mlp_address_add (void *solver,
1847 struct ATS_Address *address,
1850 struct GAS_MLP_Handle *mlp = solver;
1852 if (GNUNET_ATS_NetworkTypeCount <= network)
1858 if (NULL == address->solver_information)
1860 address->solver_information = GNUNET_new (struct MLP_information);
1863 LOG (GNUNET_ERROR_TYPE_ERROR,
1864 _("Adding address for peer `%s' multiple times\n"),
1865 GNUNET_i2s(&address->peer));
1867 /* Is this peer included in the problem? */
1869 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1872 /* FIXME: should this be an error? */
1873 LOG (GNUNET_ERROR_TYPE_DEBUG,
1874 "Adding address for peer `%s' without address request\n",
1875 GNUNET_i2s(&address->peer));
1879 LOG (GNUNET_ERROR_TYPE_DEBUG,
1880 "Adding address for peer `%s' with address request \n",
1881 GNUNET_i2s(&address->peer));
1882 /* Problem size changed: new address for peer with pending request */
1883 mlp->stat_mlp_prob_changed = GNUNET_YES;
1884 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1885 GAS_mlp_solve_problem (solver);
1890 * Transport properties for this address have changed
1892 * @param solver solver handle
1893 * @param address the address
1896 GAS_mlp_address_property_changed (void *solver,
1897 struct ATS_Address *address)
1899 struct MLP_information *mlpi = address->solver_information;
1900 struct GAS_MLP_Handle *mlp = solver;
1902 if (NULL == mlp->p.prob)
1903 return; /* There is no MLP problem to update yet */
1907 LOG (GNUNET_ERROR_TYPE_INFO,
1908 _("Updating address property for peer `%s' %p not added before\n"),
1909 GNUNET_i2s (&address->peer),
1915 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
1918 /* Peer is not requested, so no need to update problem */
1921 LOG (GNUNET_ERROR_TYPE_DEBUG,
1922 "Updating properties for peer `%s'\n",
1923 GNUNET_i2s(&address->peer));
1925 if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
1928 /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index] */
1930 mlp_create_problem_update_value (&mlp->p,
1931 mlp->p.r_q[RQ_QUALITY_METRIC_DELAY],
1933 address->norm_delay.norm,
1936 mlp_create_problem_update_value (&mlp->p,
1937 mlp->p.r_q[RQ_QUALITY_METRIC_DISTANCE],
1939 address->norm_distance.norm,
1942 mlp->stat_mlp_prob_updated = GNUNET_YES;
1943 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
1944 GAS_mlp_solve_problem (solver);
1951 * Find the active address in the set of addresses of a peer
1952 * @param cls destination
1953 * @param key peer id
1954 * @param value address
1955 * @return #GNUNET_OK
1958 mlp_get_preferred_address_it (void *cls,
1959 const struct GNUNET_PeerIdentity *key,
1962 static int counter = 0;
1963 struct ATS_Address **aa = cls;
1964 struct ATS_Address *addr = value;
1965 struct MLP_information *mlpi = addr->solver_information;
1972 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973 * "MLP [%u] Peer `%s' %s length %u session %u active %s mlp active %s\n",
1974 * counter, GNUNET_i2s (&addr->peer), addr->plugin, addr->addr_len, addr->session_id,
1975 * (GNUNET_YES == addr->active) ? "active" : "inactive",
1976 * (GNUNET_YES == mlpi->n) ? "active" : "inactive");
1979 if (GNUNET_YES == mlpi->n)
1983 (*aa)->assigned_bw_in = mlpi->b_in;
1984 (*aa)->assigned_bw_out = mlpi->b_out;
1993 get_peer_pref_value (struct GAS_MLP_Handle *mlp,
1994 const struct GNUNET_PeerIdentity *peer)
1997 const double *preferences;
2000 preferences = mlp->env->get_preferences (mlp->env->cls, peer);
2002 for (c = 0; c < GNUNET_ATS_PREFERENCE_END; c++)
2004 /* fprintf (stderr, "VALUE[%u] %s %.3f \n",
2005 * c, GNUNET_i2s (&cur->addr->peer), t[c]); */
2006 res += preferences[c];
2009 res /= GNUNET_ATS_PREFERENCE_END;
2012 LOG (GNUNET_ERROR_TYPE_DEBUG,
2013 "Peer preference for peer `%s' == %.2f\n",
2014 GNUNET_i2s(peer), res);
2021 * Get the preferred address for a specific peer
2023 * @param solver the MLP Handle
2024 * @param peer the peer
2027 GAS_mlp_get_preferred_address (void *solver,
2028 const struct GNUNET_PeerIdentity *peer)
2030 struct GAS_MLP_Handle *mlp = solver;
2032 struct ATS_Address *res;
2034 LOG (GNUNET_ERROR_TYPE_DEBUG,
2035 "Getting preferred address for `%s'\n",
2038 /* Is this peer included in the problem? */
2040 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
2043 LOG (GNUNET_ERROR_TYPE_INFO, "Adding peer `%s' to list of requested_peers with requests\n",
2046 p = GNUNET_new (struct ATS_Peer);
2048 p->f = get_peer_pref_value (mlp, peer);
2049 GNUNET_CONTAINER_multipeermap_put (mlp->requested_peers,
2051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2053 /* Added new peer, we have to rebuild problem before solving */
2054 mlp->stat_mlp_prob_changed = GNUNET_YES;
2056 if ((GNUNET_YES == mlp->opt_mlp_auto_solve)&&
2057 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(mlp->env->addresses,
2060 mlp->exclude_peer = peer;
2061 GAS_mlp_solve_problem (mlp);
2062 mlp->exclude_peer = NULL;
2065 /* Get prefered address */
2067 GNUNET_CONTAINER_multipeermap_get_multiple (mlp->env->addresses, peer,
2068 &mlp_get_preferred_address_it, &res);
2070 mlp->env->bandwidth_changed_cb (mlp->env->cls,
2076 * Deletes a single address in the MLP problem
2078 * The MLP problem has to be recreated and the problem has to be resolved
2080 * @param solver the MLP Handle
2081 * @param address the address to delete
2084 GAS_mlp_address_delete (void *solver,
2085 struct ATS_Address *address)
2087 struct GAS_MLP_Handle *mlp = solver;
2088 struct MLP_information *mlpi;
2089 struct ATS_Address *res;
2092 mlpi = address->solver_information;
2095 /* Remove full address */
2097 address->solver_information = NULL;
2099 was_active = address->active;
2100 address->active = GNUNET_NO;
2101 address->assigned_bw_in = 0;
2102 address->assigned_bw_out = 0;
2104 /* Is this peer included in the problem? */
2106 GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
2109 LOG (GNUNET_ERROR_TYPE_INFO,
2110 "Deleting address for peer `%s' without address request \n",
2111 GNUNET_i2s(&address->peer));
2114 LOG (GNUNET_ERROR_TYPE_INFO,
2115 "Deleting address for peer `%s' with address request \n",
2116 GNUNET_i2s (&address->peer));
2118 /* Problem size changed: new address for peer with pending request */
2119 mlp->stat_mlp_prob_changed = GNUNET_YES;
2120 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2122 GAS_mlp_solve_problem (solver);
2124 if (GNUNET_YES == was_active)
2126 GAS_mlp_get_preferred_address (solver, &address->peer);
2128 GNUNET_CONTAINER_multipeermap_get_multiple (mlp->env->addresses,
2130 &mlp_get_preferred_address_it,
2134 /* No alternative address, disconnecting peer */
2135 mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
2142 * Start a bulk operation
2144 * @param solver the solver
2147 GAS_mlp_bulk_start (void *solver)
2149 struct GAS_MLP_Handle *s = solver;
2151 LOG (GNUNET_ERROR_TYPE_DEBUG,
2152 "Locking solver for bulk operation ...\n");
2153 GNUNET_assert (NULL != solver);
2154 s->stat_bulk_lock ++;
2159 GAS_mlp_bulk_stop (void *solver)
2161 struct GAS_MLP_Handle *s = solver;
2163 LOG (GNUNET_ERROR_TYPE_DEBUG,
2164 "Unlocking solver from bulk operation ...\n");
2165 GNUNET_assert (NULL != solver);
2167 if (s->stat_bulk_lock < 1)
2172 s->stat_bulk_lock --;
2174 if (0 < s->stat_bulk_requests)
2176 GAS_mlp_solve_problem (solver);
2177 s->stat_bulk_requests= 0;
2184 * Stop notifying about address and bandwidth changes for this peer
2186 * @param solver the MLP handle
2187 * @param peer the peer
2190 GAS_mlp_stop_get_preferred_address (void *solver,
2191 const struct GNUNET_PeerIdentity *peer)
2193 struct GAS_MLP_Handle *mlp = solver;
2194 struct ATS_Peer *p = NULL;
2196 GNUNET_assert (NULL != solver);
2197 GNUNET_assert (NULL != peer);
2198 if (NULL != (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
2200 GNUNET_assert (GNUNET_YES ==
2201 GNUNET_CONTAINER_multipeermap_remove (mlp->requested_peers, peer, p));
2204 mlp->stat_mlp_prob_changed = GNUNET_YES;
2205 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2207 GAS_mlp_solve_problem (solver);
2214 * Changes the preferences for a peer in the MLP problem
2216 * @param solver the MLP Handle
2217 * @param peer the peer
2218 * @param kind the kind to change the preference
2219 * @param pref_rel the relative score
2222 GAS_mlp_address_change_preference (void *solver,
2223 const struct GNUNET_PeerIdentity *peer,
2224 enum GNUNET_ATS_PreferenceKind kind,
2227 struct GAS_MLP_Handle *mlp = solver;
2230 LOG (GNUNET_ERROR_TYPE_DEBUG,
2231 "Changing preference for address for peer `%s' to %.2f\n",
2235 GNUNET_STATISTICS_update (mlp->env->stats,
2236 "# LP address preference changes", 1, GNUNET_NO);
2237 /* Update the constraints with changed preferences */
2241 /* Update relativity constraint c9 */
2242 if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
2244 LOG (GNUNET_ERROR_TYPE_INFO,
2245 "Updating preference for unknown peer `%s'\n",
2250 if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
2252 p->f = get_peer_pref_value (mlp, peer);
2253 mlp_create_problem_update_value (&mlp->p,
2259 /* Problem size changed: new address for peer with pending request */
2260 mlp->stat_mlp_prob_updated = GNUNET_YES;
2261 if (GNUNET_YES == mlp->opt_mlp_auto_solve)
2262 GAS_mlp_solve_problem (solver);
2268 * Get application feedback for a peer
2270 * @param solver the solver handle
2271 * @param application the application
2272 * @param peer the peer to change the preference for
2273 * @param scope the time interval for this feedback: [now - scope .. now]
2274 * @param kind the kind to change the preference
2275 * @param score the score
2278 GAS_mlp_address_preference_feedback (void *solver,
2279 struct GNUNET_SERVICE_Client *application,
2280 const struct GNUNET_PeerIdentity *peer,
2281 const struct GNUNET_TIME_Relative scope,
2282 enum GNUNET_ATS_PreferenceKind kind,
2285 struct GAS_PROPORTIONAL_Handle *s = solver;
2287 GNUNET_assert (NULL != solver);
2288 GNUNET_assert (NULL != peer);
2289 GNUNET_assert (NULL != s);
2294 mlp_free_peers (void *cls,
2295 const struct GNUNET_PeerIdentity *key, void *value)
2297 struct GNUNET_CONTAINER_MultiPeerMap *map = cls;
2298 struct ATS_Peer *p = value;
2300 GNUNET_assert (GNUNET_YES ==
2301 GNUNET_CONTAINER_multipeermap_remove (map, key, value));
2309 * Shutdown the MLP problem solving component
2311 * @param cls the solver handle
2315 libgnunet_plugin_ats_mlp_done (void *cls)
2317 struct GNUNET_ATS_SolverFunctions *sf = cls;
2318 struct GAS_MLP_Handle *mlp = sf->cls;
2320 LOG (GNUNET_ERROR_TYPE_DEBUG,
2321 "Shutting down mlp solver\n");
2322 mlp_delete_problem (mlp);
2323 GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
2325 mlp->requested_peers);
2326 GNUNET_CONTAINER_multipeermap_destroy (mlp->requested_peers);
2327 mlp->requested_peers = NULL;
2329 /* Clean up GLPK environment */
2333 LOG (GNUNET_ERROR_TYPE_DEBUG,
2334 "Shutdown down of mlp solver complete\n");
2340 libgnunet_plugin_ats_mlp_init (void *cls)
2342 static struct GNUNET_ATS_SolverFunctions sf;
2343 struct GNUNET_ATS_PluginEnvironment *env = cls;
2344 struct GAS_MLP_Handle * mlp = GNUNET_new (struct GAS_MLP_Handle);
2346 unsigned long long tmp;
2352 struct GNUNET_TIME_Relative max_duration;
2353 long long unsigned int max_iterations;
2355 /* Init GLPK environment */
2356 int res = glp_init_env();
2359 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
2360 "initialization successful");
2363 LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
2364 "environment is already initialized");
2367 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
2368 "initialization failed (insufficient memory)");
2373 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
2374 "initialization failed (unsupported programming model)");
2382 mlp->opt_dump_problem_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2383 "ats", "MLP_DUMP_PROBLEM_ALL");
2384 if (GNUNET_SYSERR == mlp->opt_dump_problem_all)
2385 mlp->opt_dump_problem_all = GNUNET_NO;
2387 mlp->opt_dump_solution_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2388 "ats", "MLP_DUMP_SOLUTION_ALL");
2389 if (GNUNET_SYSERR == mlp->opt_dump_solution_all)
2390 mlp->opt_dump_solution_all = GNUNET_NO;
2392 mlp->opt_dump_problem_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2393 "ats", "MLP_DUMP_PROBLEM_ON_FAIL");
2394 if (GNUNET_SYSERR == mlp->opt_dump_problem_on_fail)
2395 mlp->opt_dump_problem_on_fail = GNUNET_NO;
2397 mlp->opt_dump_solution_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2398 "ats", "MLP_DUMP_SOLUTION_ON_FAIL");
2399 if (GNUNET_SYSERR == mlp->opt_dump_solution_on_fail)
2400 mlp->opt_dump_solution_on_fail = GNUNET_NO;
2402 mlp->opt_dbg_glpk_verbose = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2403 "ats", "MLP_DBG_GLPK_VERBOSE");
2404 if (GNUNET_SYSERR == mlp->opt_dbg_glpk_verbose)
2405 mlp->opt_dbg_glpk_verbose = GNUNET_NO;
2407 mlp->opt_dbg_feasibility_only = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2408 "ats", "MLP_DBG_FEASIBILITY_ONLY");
2409 if (GNUNET_SYSERR == mlp->opt_dbg_feasibility_only)
2410 mlp->opt_dbg_feasibility_only = GNUNET_NO;
2411 if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
2412 LOG (GNUNET_ERROR_TYPE_WARNING,
2413 "MLP solver is configured to check feasibility only!\n");
2415 mlp->opt_dbg_autoscale_problem = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2416 "ats", "MLP_DBG_AUTOSCALE_PROBLEM");
2417 if (GNUNET_SYSERR == mlp->opt_dbg_autoscale_problem)
2418 mlp->opt_dbg_autoscale_problem = GNUNET_NO;
2419 if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
2420 LOG (GNUNET_ERROR_TYPE_WARNING,
2421 "MLP solver is configured automatically scale the problem!\n");
2423 mlp->opt_dbg_intopt_presolver = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2424 "ats", "MLP_DBG_INTOPT_PRESOLVE");
2425 if (GNUNET_SYSERR == mlp->opt_dbg_intopt_presolver)
2426 mlp->opt_dbg_intopt_presolver = GNUNET_NO;
2427 if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
2428 LOG (GNUNET_ERROR_TYPE_WARNING,
2429 "MLP solver is configured use the mlp presolver\n");
2431 mlp->opt_dbg_optimize_diversity = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2432 "ats", "MLP_DBG_OPTIMIZE_DIVERSITY");
2433 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_diversity)
2434 mlp->opt_dbg_optimize_diversity = GNUNET_YES;
2435 if (GNUNET_NO == mlp->opt_dbg_optimize_diversity)
2436 LOG (GNUNET_ERROR_TYPE_WARNING,
2437 "MLP solver is not optimizing for diversity\n");
2439 mlp->opt_dbg_optimize_relativity= GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2440 "ats", "MLP_DBG_OPTIMIZE_RELATIVITY");
2441 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_relativity)
2442 mlp->opt_dbg_optimize_relativity = GNUNET_YES;
2443 if (GNUNET_NO == mlp->opt_dbg_optimize_relativity)
2444 LOG (GNUNET_ERROR_TYPE_WARNING,
2445 "MLP solver is not optimizing for relativity\n");
2447 mlp->opt_dbg_optimize_quality = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2448 "ats", "MLP_DBG_OPTIMIZE_QUALITY");
2449 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_quality)
2450 mlp->opt_dbg_optimize_quality = GNUNET_YES;
2451 if (GNUNET_NO == mlp->opt_dbg_optimize_quality)
2452 LOG (GNUNET_ERROR_TYPE_WARNING,
2453 "MLP solver is not optimizing for quality\n");
2455 mlp->opt_dbg_optimize_utility = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2456 "ats", "MLP_DBG_OPTIMIZE_UTILITY");
2457 if (GNUNET_SYSERR == mlp->opt_dbg_optimize_utility)
2458 mlp->opt_dbg_optimize_utility = GNUNET_YES;
2459 if (GNUNET_NO == mlp->opt_dbg_optimize_utility)
2460 LOG (GNUNET_ERROR_TYPE_WARNING,
2461 "MLP solver is not optimizing for utility\n");
2463 if ( (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
2464 (GNUNET_NO == mlp->opt_dbg_optimize_quality) &&
2465 (GNUNET_NO == mlp->opt_dbg_optimize_relativity) &&
2466 (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
2467 (GNUNET_NO == mlp->opt_dbg_feasibility_only))
2469 LOG (GNUNET_ERROR_TYPE_ERROR,
2470 _("MLP solver is not optimizing for anything, changing to feasibility check\n"));
2471 mlp->opt_dbg_feasibility_only = GNUNET_YES;
2474 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2475 "ats", "MLP_LOG_FORMAT", &outputformat))
2476 mlp->opt_log_format = MLP_CPLEX;
2479 GNUNET_STRINGS_utf8_toupper(outputformat, outputformat);
2480 if (0 == strcmp (outputformat, "MPS"))
2482 mlp->opt_log_format = MLP_MPS;
2484 else if (0 == strcmp (outputformat, "CPLEX"))
2486 mlp->opt_log_format = MLP_CPLEX;
2488 else if (0 == strcmp (outputformat, "GLPK"))
2490 mlp->opt_log_format = MLP_GLPK;
2494 LOG (GNUNET_ERROR_TYPE_WARNING,
2495 "Invalid log format `%s' in configuration, using CPLEX!\n",
2497 mlp->opt_log_format = MLP_CPLEX;
2499 GNUNET_free (outputformat);
2502 mlp->pv.BIG_M = (double) BIG_M_VALUE;
2504 mlp->pv.mip_gap = (double) 0.0;
2505 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2506 "MLP_MAX_MIP_GAP", &f_tmp))
2508 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2510 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2515 mlp->pv.mip_gap = f_tmp;
2516 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
2521 mlp->pv.lp_mip_gap = (double) 0.0;
2522 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2523 "MLP_MAX_LP_MIP_GAP", &f_tmp))
2525 if ((f_tmp < 0.0) || (f_tmp > 1.0))
2527 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2532 mlp->pv.lp_mip_gap = f_tmp;
2533 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2538 /* Get timeout for iterations */
2539 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats",
2540 "MLP_MAX_DURATION", &max_duration))
2542 max_duration = MLP_MAX_EXEC_DURATION;
2545 /* Get maximum number of iterations */
2546 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(env->cfg, "ats",
2547 "MLP_MAX_ITERATIONS", &max_iterations))
2549 max_iterations = MLP_MAX_ITERATIONS;
2552 /* Get diversity coefficient from configuration */
2553 mlp->pv.co_D = MLP_DEFAULT_D;
2554 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2555 "MLP_COEFFICIENT_D", &f_tmp))
2559 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2560 "MLP_COEFFICIENT_D", f_tmp);
2564 mlp->pv.co_D = f_tmp;
2565 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2566 "MLP_COEFFICIENT_D", f_tmp);
2570 /* Get relativity coefficient from configuration */
2571 mlp->pv.co_R = MLP_DEFAULT_R;
2572 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2573 "MLP_COEFFICIENT_R", &f_tmp))
2577 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2578 "MLP_COEFFICIENT_R", f_tmp);
2582 mlp->pv.co_R = f_tmp;
2583 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2584 "MLP_COEFFICIENT_R", f_tmp);
2589 /* Get utilization coefficient from configuration */
2590 mlp->pv.co_U = MLP_DEFAULT_U;
2591 if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
2592 "MLP_COEFFICIENT_U", &f_tmp))
2596 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
2597 "MLP_COEFFICIENT_U", f_tmp);
2601 mlp->pv.co_U = f_tmp;
2602 LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
2603 "MLP_COEFFICIENT_U", f_tmp);
2607 /* Get quality metric coefficients from configuration */
2608 for (c = 0; c < RQ_QUALITY_METRIC_COUNT; c++)
2610 /* initialize quality coefficients with default value 1.0 */
2611 mlp->pv.co_Q[c] = MLP_DEFAULT_QUALITY;
2616 GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2617 "MLP_COEFFICIENT_QUALITY_DELAY",
2619 mlp->pv.co_Q[RQ_QUALITY_METRIC_DELAY] = (double) tmp / 100;
2621 mlp->pv.co_Q[RQ_QUALITY_METRIC_DELAY] = MLP_DEFAULT_QUALITY;
2624 GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2625 "MLP_COEFFICIENT_QUALITY_DISTANCE",
2627 mlp->pv.co_Q[RQ_QUALITY_METRIC_DISTANCE] = (double) tmp / 100;
2629 mlp->pv.co_Q[RQ_QUALITY_METRIC_DISTANCE] = MLP_DEFAULT_QUALITY;
2631 /* Get minimum bandwidth per used address from configuration */
2632 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2633 "MLP_MIN_BANDWIDTH",
2638 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
2641 /* Get minimum number of connections from configuration */
2642 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
2643 "MLP_MIN_CONNECTIONS",
2647 n_min = MLP_DEFAULT_MIN_CONNECTIONS;
2649 /* Init network quotas */
2650 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
2652 mlp->pv.quota_index[c] = c;
2653 mlp->pv.quota_out[c] = env->out_quota[c];
2654 mlp->pv.quota_in[c] = env->in_quota[c];
2656 LOG (GNUNET_ERROR_TYPE_INFO,
2657 "Quota for network `%s' (in/out) %llu/%llu\n",
2658 GNUNET_ATS_print_network_type (c),
2659 mlp->pv.quota_out[c],
2660 mlp->pv.quota_in[c]);
2661 /* Check if defined quota could make problem unsolvable */
2662 if ((n_min * b_min) > mlp->pv.quota_out[c])
2664 LOG (GNUNET_ERROR_TYPE_INFO,
2665 _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2666 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2667 mlp->pv.quota_out[c],
2669 mlp->pv.quota_out[c] = (n_min * b_min);
2671 if ((n_min * b_min) > mlp->pv.quota_in[c])
2673 LOG (GNUNET_ERROR_TYPE_INFO,
2674 _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
2675 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2676 mlp->pv.quota_in[c],
2678 mlp->pv.quota_in[c] = (n_min * b_min);
2680 /* Check if bandwidth is too big to make problem solvable */
2681 if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
2683 LOG (GNUNET_ERROR_TYPE_INFO,
2684 _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
2685 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2686 mlp->pv.quota_out[c],
2688 mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
2690 if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
2692 LOG (GNUNET_ERROR_TYPE_INFO,
2693 _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
2694 GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
2695 mlp->pv.quota_in[c],
2697 mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
2702 sf.s_add = &GAS_mlp_address_add;
2703 sf.s_address_update_property = &GAS_mlp_address_property_changed;
2704 sf.s_get = &GAS_mlp_get_preferred_address;
2705 sf.s_get_stop = &GAS_mlp_stop_get_preferred_address;
2706 sf.s_pref = &GAS_mlp_address_change_preference;
2707 sf.s_feedback = &GAS_mlp_address_preference_feedback;
2708 sf.s_del = &GAS_mlp_address_delete;
2709 sf.s_bulk_start = &GAS_mlp_bulk_start;
2710 sf.s_bulk_stop = &GAS_mlp_bulk_stop;
2712 /* Setting MLP Input variables */
2713 mlp->pv.b_min = b_min;
2714 mlp->pv.n_min = n_min;
2715 mlp->pv.m_q = RQ_QUALITY_METRIC_COUNT;
2716 mlp->stat_mlp_prob_changed = GNUNET_NO;
2717 mlp->stat_mlp_prob_updated = GNUNET_NO;
2718 mlp->opt_mlp_auto_solve = GNUNET_YES;
2719 mlp->requested_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2720 mlp->stat_bulk_requests = 0;
2721 mlp->stat_bulk_lock = 0;
2724 /* Redirect GLPK output to GNUnet logging */
2725 glp_term_hook (&mlp_term_hook, (void *) mlp);
2727 /* Init LP solving parameters */
2728 glp_init_smcp(&mlp->control_param_lp);
2729 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
2730 if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
2731 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
2733 mlp->control_param_lp.it_lim = max_iterations;
2734 mlp->control_param_lp.tm_lim = max_duration.rel_value_us / 1000LL;
2736 /* Init MLP solving parameters */
2737 glp_init_iocp(&mlp->control_param_mlp);
2738 /* Setting callback function */
2739 mlp->control_param_mlp.cb_func = &mlp_branch_and_cut_cb;
2740 mlp->control_param_mlp.cb_info = mlp;
2741 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
2742 mlp->control_param_mlp.mip_gap = mlp->pv.mip_gap;
2743 if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
2744 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
2745 mlp->control_param_mlp.tm_lim = max_duration.rel_value_us / 1000LL;
2747 LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");
2752 /* end of plugin_ats_mlp.c */