2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/gnunet-service-ats_addresses_mlp.c
23 * @brief ats mlp problem solver
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_addresses_mlp.h"
31 #include "gnunet_statistics_service.h"
34 #define WRITE_MLP GNUNET_NO
35 #define DEBUG_ATS GNUNET_NO
36 #define VERBOSE_GLPK GNUNET_NO
39 * Translate glpk solver error codes to text
40 * @param retcode return code
41 * @return string with result
44 mlp_solve_to_string (int retcode)
51 return "invalid basis";
54 return "singular matrix";
57 return "ill-conditioned matrix";
60 return "invalid bounds";
63 return "solver failed";
66 return "objective lower limit reached";
69 return "objective upper limit reached";
72 return "iteration limit exceeded";
75 return "time limit exceeded";
78 return "no primal feasible solution";
81 return "root LP optimum not provided";
84 return "search terminated by application";
87 return "relative mip gap tolerance reached";
90 return "no dual feasible solution";
93 return "no convergence";
96 return "numerical instability";
99 return "invalid data";
102 return "result out of range";
106 return "unknown error";
110 return "unknown error";
115 * Translate glpk status error codes to text
116 * @param retcode return code
117 * @return string with result
120 mlp_status_to_string (int retcode)
124 return "solution is undefined";
127 return "solution is feasible";
130 return "solution is infeasible";
133 return "no feasible solution exists";
136 return "solution is optimal";
139 return "solution is unbounded";
143 return "unknown error";
147 return "unknown error";
151 * Translate ATS properties to text
152 * Just intended for debugging
154 * @param retcode return code
155 * @return string with result
158 mlp_ats_to_string (int ats_index)
161 case GNUNET_ATS_ARRAY_TERMINATOR:
162 return "GNUNET_ATS_ARRAY_TERMINATOR";
164 case GNUNET_ATS_UTILIZATION_UP:
165 return "GNUNET_ATS_UTILIZATION_UP";
167 case GNUNET_ATS_UTILIZATION_DOWN:
168 return "GNUNET_ATS_UTILIZATION_DOWN";
170 case GNUNET_ATS_COST_LAN:
171 return "GNUNET_ATS_COST_LAN";
173 case GNUNET_ATS_COST_WAN:
174 return "GNUNET_ATS_COST_LAN";
176 case GNUNET_ATS_COST_WLAN:
177 return "GNUNET_ATS_COST_WLAN";
179 case GNUNET_ATS_NETWORK_TYPE:
180 return "GNUNET_ATS_NETWORK_TYPE";
182 case GNUNET_ATS_QUALITY_NET_DELAY:
183 return "GNUNET_ATS_QUALITY_NET_DELAY";
185 case GNUNET_ATS_QUALITY_NET_DISTANCE:
186 return "GNUNET_ATS_QUALITY_NET_DISTANCE";
193 return "unknown error";
197 * Find a peer in the DLL
198 * @param the peer to find
199 * @return the peer struct
201 static struct ATS_Peer *
202 mlp_find_peer (struct GAS_MLP_Handle *mlp, const struct GNUNET_PeerIdentity *peer)
204 struct ATS_Peer *res = mlp->peer_head;
207 if (0 == memcmp (peer, &res->id, sizeof (struct GNUNET_PeerIdentity)))
215 * Intercept GLPK terminal output
216 * @param info the mlp handle
217 * @param s the string to print
218 * @return 0: glpk prints output on terminal, 0 != surpress output
221 mlp_term_hook (void *info, const char *s)
223 /* Not needed atm struct MLP_information *mlp = info; */
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", s);
229 * Delete the MLP problem and free the constrain matrix
231 * @param mlp the MLP handle
234 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
238 if (mlp->prob != NULL)
239 glp_delete_prob(mlp->prob);
241 /* delete row index */
244 GNUNET_free (mlp->ia);
248 /* delete column index */
251 GNUNET_free (mlp->ja);
255 /* delete coefficients */
258 GNUNET_free (mlp->ar);
267 * Add constraints that are iterating over "forall addresses"
268 * and collects all existing peers for "forall peers" constraints
270 * @param cls GAS_MLP_Handle
271 * @param key Hashcode
272 * @param value ATS_Address
274 * @return GNUNET_OK to continue
277 create_constraint_it (void *cls, const GNUNET_HashCode * key, void *value)
279 struct GAS_MLP_Handle *mlp = cls;
280 struct ATS_Address *address = value;
281 struct MLP_information *mlpi;
282 unsigned int row_index;
285 GNUNET_assert (address->mlp_information != NULL);
286 mlpi = (struct MLP_information *) address->mlp_information;
288 /* c 1) bandwidth capping
289 * b_t + (-M) * n_t <= 0
291 row_index = glp_add_rows (mlp->prob, 1);
292 mlpi->r_c1 = row_index;
294 GNUNET_asprintf(&name, "c1_%s_%s", GNUNET_i2s(&address->peer), address->plugin);
295 glp_set_row_name (mlp->prob, row_index, name);
297 /* set row bounds: <= 0 */
298 glp_set_row_bnds (mlp->prob, row_index, GLP_UP, 0.0, 0.0);
299 mlp->ia[mlp->ci] = row_index;
300 mlp->ja[mlp->ci] = mlpi->c_b;
301 mlp->ar[mlp->ci] = 1;
304 mlp->ia[mlp->ci] = row_index;
305 mlp->ja[mlp->ci] = mlpi->c_n;
306 mlp->ar[mlp->ci] = -mlp->BIG_M;
309 /* c 3) minimum bandwidth
310 * b_t + (-n_t * b_min) >= 0
313 row_index = glp_add_rows (mlp->prob, 1);
315 GNUNET_asprintf(&name, "c3_%s_%s", GNUNET_i2s(&address->peer), address->plugin);
316 glp_set_row_name (mlp->prob, row_index, name);
318 mlpi->r_c3 = row_index;
319 /* set row bounds: >= 0 */
320 glp_set_row_bnds (mlp->prob, row_index, GLP_LO, 0.0, 0.0);
322 mlp->ia[mlp->ci] = row_index;
323 mlp->ja[mlp->ci] = mlpi->c_b;
324 mlp->ar[mlp->ci] = 1;
327 mlp->ia[mlp->ci] = row_index;
328 mlp->ja[mlp->ci] = mlpi->c_n;
329 mlp->ar[mlp->ci] = - (double) mlp->b_min;
332 /* c 4) minimum connections
333 * (1)*n_1 + ... + (1)*n_m >= n_min
335 mlp->ia[mlp->ci] = mlp->r_c4;
336 mlp->ja[mlp->ci] = mlpi->c_n;
337 mlp->ar[mlp->ci] = 1;
340 /* c 6) maximize diversity
341 * (1)*n_1 + ... + (1)*n_m - d == 0
343 mlp->ia[mlp->ci] = mlp->r_c6;
344 mlp->ja[mlp->ci] = mlpi->c_n;
345 mlp->ar[mlp->ci] = 1;
352 * Find the required ATS information for an address
354 * @param addr the address
355 * @param ats_index the desired ATS index
357 * @return the index on success, otherwise GNUNET_SYSERR
361 mlp_lookup_ats (struct ATS_Address *addr, int ats_index)
363 struct GNUNET_ATS_Information * ats = addr->ats;
365 int found = GNUNET_NO;
366 for (c = 0; c < addr->ats_count; c++)
368 if (ats[c].type == ats_index)
374 if (found == GNUNET_YES)
377 return GNUNET_SYSERR;
381 * Adds the problem constraints for all addresses
382 * Required for problem recreation after address deletion
384 * @param addresses all addresses
388 mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
390 unsigned int n_addresses;
395 n_addresses = GNUNET_CONTAINER_multihashmap_size(addresses);
397 /* Required indices in the constrain matrix
399 * feasibility constraints:
401 * c 1) bandwidth capping
402 * #rows: |n_addresses|
403 * #indices: 2 * |n_addresses|
405 * c 2) one active address per peer
407 * #indices: |n_addresses|
409 * c 3) minium bandwidth assigned
410 * #rows: |n_addresses|
411 * #indices: 2 * |n_addresses|
413 * c 4) minimum number of active connections
415 * #indices: |n_addresses|
417 * c 5) maximum ressource consumption
418 * #rows: |ressources|
419 * #indices: |n_addresses|
421 * Sum for feasibility constraints:
422 * #rows: 3 * |n_addresses| + |ressources| + |peers| + 1
423 * #indices: 7 * |n_addresses|
425 * optimality constraints:
429 * #indices: |n_addresses| + 1
432 * #rows: |quality properties|
433 * #indices: |n_addresses| + |quality properties|
437 * #indices: |n_addresses| + 1
441 * #indices: |n_addresses| + |peers|
444 /* last +1 caused by glpk index starting with one */
445 int pi = ((7 * n_addresses) + (4 * n_addresses + mlp->m_q + mlp->c_p + 2) + 1);
450 int *ia = GNUNET_malloc (pi * sizeof (int));
454 int *ja = GNUNET_malloc (pi * sizeof (int));
458 double *ar= GNUNET_malloc (pi * sizeof (double));
461 /* Adding constraint rows
462 * This constraints are kind of "for all addresses"
463 * Feasibility constraints:
465 * c 1) bandwidth capping
466 * c 3) minimum bandwidth
467 * c 4) minimum number of connections
468 * c 6) maximize diversity
471 int min = mlp->n_min;
472 if (mlp->n_min > mlp->c_p)
475 mlp->r_c4 = glp_add_rows (mlp->prob, 1);
476 glp_set_row_name (mlp->prob, mlp->r_c4, "c4");
477 glp_set_row_bnds (mlp->prob, mlp->r_c4, GLP_LO, min, min);
479 /* Add row for c6) */
481 mlp->r_c6 = glp_add_rows (mlp->prob, 1);
482 /* Set type type to fix */
483 glp_set_row_bnds (mlp->prob, mlp->r_c6, GLP_FX, 0.0, 0.0);
485 ia[mlp->ci] = mlp->r_c6 ;
486 ja[mlp->ci] = mlp->c_d;
490 GNUNET_CONTAINER_multihashmap_iterate (addresses, create_constraint_it, mlp);
492 /* Adding constraint rows
493 * This constraints are kind of "for all peers"
494 * Feasibility constraints:
496 * c 2) 1 address per peer
497 * sum (n_p1_1 + ... + n_p1_n) = 1
500 * sum (f_p * b_p1_1 + ... + f_p * b_p1_n) - u = 0
503 * V p : sum (bt_1 + ... +bt_n) - f_p * r = 0
506 /* Adding rows for c 8) */
507 mlp->r_c8 = glp_add_rows (mlp->prob, mlp->c_p);
508 glp_set_row_name (mlp->prob, mlp->r_c8, "c8");
509 /* Set row bound == 0 */
510 glp_set_row_bnds (mlp->prob, mlp->r_c8, GLP_FX, 0.0, 0.0);
512 ia[mlp->ci] = mlp->r_c8;
513 ja[mlp->ci] = mlp->c_u;
517 struct ATS_Peer * peer = mlp->peer_head;
520 struct ATS_Address *addr = peer->head;
521 struct MLP_information *mlpi = NULL;
523 /* Adding rows for c 2) */
524 peer->r_c2 = glp_add_rows (mlp->prob, 1);
525 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&peer->id));
526 glp_set_row_name (mlp->prob, peer->r_c2, name);
528 /* Set row bound == 1 */
529 glp_set_row_bnds (mlp->prob, peer->r_c2, GLP_FX, 1.0, 1.0);
531 /* Adding rows for c 9) */
532 peer->r_c9 = glp_add_rows (mlp->prob, 1);
533 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&peer->id));
534 glp_set_row_name (mlp->prob, peer->r_c9, name);
536 /* Set row bound == 0 */
537 glp_set_row_bnds (mlp->prob, peer->r_c9, GLP_LO, 0.0, 0.0);
540 ia[mlp->ci] = peer->r_c9;
541 ja[mlp->ci] = mlp->c_r;
547 mlpi = (struct MLP_information *) addr->mlp_information;
549 ia[mlp->ci] = peer->r_c2;
550 ja[mlp->ci] = mlpi->c_n;
554 ia[mlp->ci] = mlp->r_c8;
555 ja[mlp->ci] = mlpi->c_b;
556 ar[mlp->ci] = peer->f;
559 ia[mlp->ci] = peer->r_c9;
560 ja[mlp->ci] = mlpi->c_b;
569 /* c 7) For all quality metrics */
571 for (c = 0; c < mlp->m_q; c++)
573 struct ATS_Peer *p = mlp->peer_head;
574 struct ATS_Address *addr = p->head;
575 struct MLP_information * mlpi;
580 /* Adding rows for c 7) */
581 mlp->r_q[c] = glp_add_rows (mlp->prob, 1);
582 GNUNET_asprintf(&name, "c7_q%i_atsi_%i", c, mlp->q[c]);
583 glp_set_row_name (mlp->prob, mlp->r_q[c], name);
585 /* Set row bound == 0 */
586 glp_set_row_bnds (mlp->prob, mlp->r_q[c], GLP_LO, 0.0, 0.0);
589 ia[mlp->ci] = mlp->r_q[c];
590 ja[mlp->ci] = mlp->c_q[c];
596 /* lookup ATS information */
597 int index = mlp_lookup_ats(addr, mlp->q[c]);
599 if (index != GNUNET_SYSERR)
601 value = (double) addr->ats[index].value;
603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Quality %i with ATS property `%s' has index %i in addresses ats information has value %f\n", c, mlp_ats_to_string(mlp->q[c]), index, (double) addr->ats[index].value);
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Quality %i with ATS property `%s' not existing\n", c, mlp_ats_to_string(mlp->q[c]), index);
611 mlpi = addr->mlp_information;
612 ia[mlp->ci] = mlp->r_q[c];
613 ja[mlp->ci] = mlpi->c_b;
614 ar[mlp->ci] = p->f * value;
626 * Add columns for all addresses
628 * @param cls GAS_MLP_Handle
629 * @param key Hashcode
630 * @param value ATS_Address
632 * @return GNUNET_OK to continue
635 create_columns_it (void *cls, const GNUNET_HashCode * key, void *value)
637 struct GAS_MLP_Handle *mlp = cls;
638 struct ATS_Address *address = value;
639 struct MLP_information *mlpi;
643 GNUNET_assert (address->mlp_information != NULL);
644 mlpi = address->mlp_information;
646 /* Add bandwidth column */
647 col = glp_add_cols (mlp->prob, 2);
651 GNUNET_asprintf (&name, "b_%s_%s", GNUNET_i2s (&address->peer), address->plugin);
652 glp_set_col_name (mlp->prob, mlpi->c_b , name);
654 /* Lower bound == 0 */
655 glp_set_col_bnds (mlp->prob, mlpi->c_b , GLP_LO, 0.0, 0.0);
656 /* Continuous value*/
657 glp_set_col_kind (mlp->prob, mlpi->c_b , GLP_CV);
658 /* Objective function coefficient == 0 */
659 glp_set_obj_coef (mlp->prob, mlpi->c_b , 0);
662 /* Add usage column */
663 GNUNET_asprintf (&name, "n_%s_%s", GNUNET_i2s (&address->peer), address->plugin);
664 glp_set_col_name (mlp->prob, mlpi->c_n, name);
666 /* Limit value : 0 <= value <= 1 */
667 glp_set_col_bnds (mlp->prob, mlpi->c_n, GLP_DB, 0.0, 1.0);
669 glp_set_col_kind (mlp->prob, mlpi->c_n, GLP_IV);
670 /* Objective function coefficient == 0 */
671 glp_set_obj_coef (mlp->prob, mlpi->c_n, 0);
679 * Create the MLP problem
681 * @param mlp the MLP handle
682 * @return GNUNET_OK or GNUNET_SYSERR
685 mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
692 GNUNET_assert (mlp->prob == NULL);
694 /* create the glpk problem */
695 mlp->prob = glp_create_prob ();
697 /* Set a problem name */
698 glp_set_prob_name (mlp->prob, "gnunet ats bandwidth distribution");
700 /* Set optimization direction to maximize */
701 glp_set_obj_dir (mlp->prob, GLP_MAX);
703 /* Adding invariant columns */
705 /* Diversity d column */
707 col = glp_add_cols (mlp->prob, 1);
710 glp_set_col_name (mlp->prob, col, "d");
711 /* Column objective function coefficient */
712 glp_set_obj_coef (mlp->prob, col, mlp->co_D);
713 /* Column lower bound = 0.0 */
714 glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
716 /* Utilization u column */
718 col = glp_add_cols (mlp->prob, 1);
721 glp_set_col_name (mlp->prob, col, "u");
722 /* Column objective function coefficient */
723 glp_set_obj_coef (mlp->prob, col, mlp->co_U);
724 /* Column lower bound = 0.0 */
725 glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
727 /* Relativity r column */
728 col = glp_add_cols (mlp->prob, 1);
731 glp_set_col_name (mlp->prob, col, "r");
732 /* Column objective function coefficient */
733 glp_set_obj_coef (mlp->prob, col, mlp->co_R);
734 /* Column lower bound = 0.0 */
735 glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
737 /* Quality metric columns */
738 col = glp_add_cols(mlp->prob, mlp->m_q);
739 for (c = 0; c < mlp->m_q; c++)
741 mlp->c_q[c] = col + c;
742 GNUNET_asprintf (&name, "q_%u", mlp->q[c]);
743 glp_set_col_name (mlp->prob, col + c, name);
744 /* Column lower bound = 0.0 */
745 glp_set_col_bnds (mlp->prob, col + c, GLP_LO, 0.0, 0.0);
747 /* Coefficient == Qm */
748 glp_set_obj_coef (mlp->prob, col + c, mlp->co_Q[c]);
751 /* Add columns for addresses */
752 GNUNET_CONTAINER_multihashmap_iterate (addresses, create_columns_it, mlp);
754 /* Add constraints */
755 mlp_add_constraints_all_addresses (mlp, addresses);
757 /* Load the matrix */
758 glp_load_matrix(mlp->prob, (mlp->ci-1), mlp->ia, mlp->ja, mlp->ar);
764 * Solves the LP problem
766 * @param mlp the MLP Handle
767 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
770 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
773 struct GNUNET_TIME_Relative duration;
774 struct GNUNET_TIME_Absolute end;
775 struct GNUNET_TIME_Absolute start = GNUNET_TIME_absolute_get();
778 * Presolver is required if the problem was modified and an existing
779 * valid basis is now invalid */
780 if (mlp->presolver_required == GNUNET_YES)
781 mlp->control_param_lp.presolve = GLP_ON;
783 mlp->control_param_lp.presolve = GLP_OFF;
785 /* Solve LP problem to have initial valid solution */
787 res = glp_simplex(mlp->prob, &mlp->control_param_lp);
790 /* The LP problem instance has been successfully solved. */
792 else if (res == GLP_EITLIM)
794 /* simplex iteration limit has been exceeded. */
795 // TODO Increase iteration limit?
797 else if (res == GLP_ETMLIM)
799 /* Time limit has been exceeded. */
800 // TODO Increase time limit?
804 /* Problem was ill-defined, retry with presolver */
805 if (mlp->presolver_required == GNUNET_NO)
807 mlp->presolver_required = GNUNET_YES;
812 /* Problem was ill-defined, no way to handle that */
813 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
815 "Solving LP problem failed: %i %s\n", res, mlp_solve_to_string(res));
816 return GNUNET_SYSERR;
820 end = GNUNET_TIME_absolute_get ();
821 duration = GNUNET_TIME_absolute_get_difference (start, end);
823 mlp->lp_total_duration =+ duration.rel_value;
825 GNUNET_STATISTICS_update (mlp->stats,"# LP problem solved", 1, GNUNET_NO);
826 GNUNET_STATISTICS_set (mlp->stats,"# LP execution time", duration.rel_value, GNUNET_NO);
827 GNUNET_STATISTICS_set (mlp->stats,"# LP execution time average",
828 mlp->lp_total_duration / mlp->lp_solved, GNUNET_NO);
831 /* Analyze problem status */
832 res = glp_get_status (mlp->prob);
834 /* solution is optimal */
836 /* solution is feasible */
840 /* Problem was ill-defined, no way to handle that */
842 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
844 "Solving LP problem failed, no solution: %s\n", mlp_status_to_string(res));
845 return GNUNET_SYSERR;
849 /* solved sucessfully, no presolver required next time */
850 mlp->presolver_required = GNUNET_NO;
857 * Solves the MLP problem
859 * @param mlp the MLP Handle
860 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
863 mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
866 struct GNUNET_TIME_Relative duration;
867 struct GNUNET_TIME_Absolute end;
868 struct GNUNET_TIME_Absolute start = GNUNET_TIME_absolute_get();
870 /* solve MLP problem */
871 res = glp_intopt(mlp->prob, &mlp->control_param_mlp);
875 /* The MLP problem instance has been successfully solved. */
877 else if (res == GLP_EITLIM)
879 /* simplex iteration limit has been exceeded. */
880 // TODO Increase iteration limit?
882 else if (res == GLP_ETMLIM)
884 /* Time limit has been exceeded. */
885 // TODO Increase time limit?
889 /* Problem was ill-defined, no way to handle that */
890 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
892 "Solving MLP problem failed: %s\n", mlp_solve_to_string(res));
893 return GNUNET_SYSERR;
896 end = GNUNET_TIME_absolute_get ();
897 duration = GNUNET_TIME_absolute_get_difference (start, end);
899 mlp->mlp_total_duration =+ duration.rel_value;
901 GNUNET_STATISTICS_update (mlp->stats,"# MLP problem solved", 1, GNUNET_NO);
902 GNUNET_STATISTICS_set (mlp->stats,"# MLP execution time", duration.rel_value, GNUNET_NO);
903 GNUNET_STATISTICS_set (mlp->stats,"# MLP execution time average",
904 mlp->mlp_total_duration / mlp->mlp_solved, GNUNET_NO);
906 /* Analyze problem status */
907 res = glp_mip_status(mlp->prob);
909 /* solution is optimal */
911 /* solution is feasible */
915 /* Problem was ill-defined, no way to handle that */
917 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
919 "Solving MLP problem failed, %s\n\n", mlp_status_to_string(res));
920 return GNUNET_SYSERR;
927 int mlp_solve_problem (struct GAS_MLP_Handle *mlp);
930 mlp_scheduler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
932 struct GAS_MLP_Handle *mlp = cls;
934 mlp->mlp_task = GNUNET_SCHEDULER_NO_TASK;
936 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduled problem solving\n");
942 if (mlp->addr_in_problem != 0)
943 mlp_solve_problem(mlp);
948 * Solves the MLP problem
950 * @param mlp the MLP Handle
951 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
954 mlp_solve_problem (struct GAS_MLP_Handle *mlp)
957 mlp->last_execution = GNUNET_TIME_absolute_get ();
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem solving\n");
966 GNUNET_asprintf(&name, "problem_%i", i);
967 glp_write_lp (mlp->prob, 0, name);
971 res = mlp_solve_lp_problem (mlp);
974 GNUNET_asprintf(&name, "problem_%i_lp_solution", i);
975 glp_print_sol (mlp->prob, name);
979 if (res != GNUNET_OK)
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "LP Problem solving failed\n");
984 return GNUNET_SYSERR;
987 res = mlp_solve_mlp_problem (mlp);
990 GNUNET_asprintf(&name, "problem_%i_mlp_solution", i);
991 glp_print_mip (mlp->prob, name);
994 if (res != GNUNET_OK)
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP Problem solving failed\n");
999 return GNUNET_SYSERR;
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem solved\n");
1006 /* Process result */
1008 if (mlp->mlp_task != GNUNET_SCHEDULER_NO_TASK)
1010 GNUNET_SCHEDULER_cancel(mlp->mlp_task);
1011 mlp->mlp_task = GNUNET_SCHEDULER_NO_TASK;
1013 mlp->mlp_task = GNUNET_SCHEDULER_add_delayed (mlp->exec_interval, &mlp_scheduler, mlp);
1018 * Init the MLP problem solving component
1020 * @param stats the GNUNET_STATISTICS handle
1021 * @param max_duration maximum numbers of iterations for the LP/MLP Solver
1022 * @param max_iterations maximum time limit for the LP/MLP Solver
1023 * @return struct GAS_MLP_Handle * on success, NULL on fail
1025 struct GAS_MLP_Handle *
1026 GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1027 const struct GNUNET_STATISTICS_Handle *stats,
1028 struct GNUNET_TIME_Relative max_duration,
1029 unsigned int max_iterations)
1031 struct GAS_MLP_Handle * mlp = GNUNET_malloc (sizeof (struct GAS_MLP_Handle));
1036 long long unsigned int tmp;
1039 struct GNUNET_TIME_Relative i_exec;
1041 /* Init GLPK environment */
1042 GNUNET_assert (glp_init_env() == 0);
1044 /* Create initial MLP problem */
1045 mlp->prob = glp_create_prob();
1046 GNUNET_assert (mlp->prob != NULL);
1048 /* Get diversity coefficient from configuration */
1049 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1052 D = (double) tmp / 100;
1056 /* Get proportionality coefficient from configuration */
1057 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1060 R = (double) tmp / 100;
1064 /* Get utilization coefficient from configuration */
1065 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1068 U = (double) tmp / 100;
1072 /* Get quality metric coefficients from configuration */
1074 int i_distance = -1;
1075 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
1077 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1079 /* initialize quality coefficients with default value 1.0 */
1083 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
1085 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
1089 if ((i_delay != -1) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1090 "COEFFICIENT_QUALITY_DELAY",
1093 mlp->co_Q[i_delay] = (double) tmp / 100;
1095 mlp->co_Q[i_delay] = 1.0;
1097 if ((i_distance != -1) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1098 "COEFFICIENT_QUALITY_DISTANCE",
1100 mlp->co_Q[i_distance] = (double) tmp / 100;
1102 mlp->co_Q[i_distance] = 1.0;
1104 /* Get minimum bandwidth per used address from configuration */
1105 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1112 /* Get minimum number of connections from configuration */
1113 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1120 /* Get minimum number of connections from configuration */
1121 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_time (cfg, "ats",
1122 "ATS_EXEC_INTERVAL",
1124 mlp->exec_interval = i_exec;
1126 mlp->exec_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30);
1128 mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1129 mlp->max_iterations = max_iterations;
1130 mlp->max_exec_duration = max_duration;
1132 /* Redirect GLPK output to GNUnet logging */
1133 glp_error_hook((void *) mlp, &mlp_term_hook);
1135 /* Init LP solving parameters */
1136 glp_init_smcp(&mlp->control_param_lp);
1138 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
1140 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
1142 mlp->control_param_lp.it_lim = max_iterations;
1143 mlp->control_param_lp.tm_lim = max_duration.rel_value;
1145 /* Init MLP solving parameters */
1146 glp_init_iocp(&mlp->control_param_mlp);
1148 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
1150 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
1152 mlp->control_param_mlp.tm_lim = max_duration.rel_value;
1154 mlp->last_execution = GNUNET_TIME_absolute_get_forever();
1157 mlp->BIG_M = (double) UINT32_MAX;
1163 mlp->m_q = GNUNET_ATS_QualityPropertiesCount;
1169 * Updates a single address in the MLP problem
1171 * If the address did not exist before in the problem:
1172 * The MLP problem has to be recreated and the problem has to be resolved
1174 * Otherwise the addresses' values can be updated and the existing base can
1177 * @param mlp the MLP Handle
1178 * @param addresses the address hashmap
1179 * the address has to be already removed from the hashmap
1180 * @param address the address to update
1183 GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
1186 struct MLP_information *mlpi;
1189 GNUNET_STATISTICS_update (mlp->stats,"# LP address updates", 1, GNUNET_NO);
1191 /* We add a new address */
1192 if (address->mlp_information == NULL)
1198 if (new == GNUNET_YES)
1200 mlpi = GNUNET_malloc (sizeof (struct MLP_information));
1201 address->mlp_information = mlpi;
1202 mlp->addr_in_problem ++;
1204 /* Check for and add peer */
1205 struct ATS_Peer *peer = mlp_find_peer (mlp, &address->peer);
1209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding new peer `%s'\n", GNUNET_i2s (&address->peer));
1211 peer = GNUNET_malloc (sizeof (struct ATS_Peer));
1215 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1221 memcpy (&peer->id, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1222 GNUNET_assert(address->prev == NULL);
1223 GNUNET_assert(address->next == NULL);
1224 GNUNET_CONTAINER_DLL_insert (peer->head, peer->tail, address);
1225 GNUNET_CONTAINER_DLL_insert (mlp->peer_head, mlp->peer_tail, peer);
1231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding address to peer `%s'\n", GNUNET_i2s (&address->peer));
1233 GNUNET_CONTAINER_DLL_insert (peer->head, peer->tail, address);
1239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating existing address to peer `%s'\n", GNUNET_i2s (&address->peer));
1241 mlpi = address->mlp_information;
1246 if (new == GNUNET_YES)
1248 mlp_delete_problem (mlp);
1249 mlp_create_problem (mlp, addresses);
1250 mlp->presolver_required = GNUNET_YES;
1252 mlp_solve_problem (mlp);
1256 * Deletes a single address in the MLP problem
1258 * The MLP problem has to be recreated and the problem has to be resolved
1260 * @param mlp the MLP Handle
1261 * @param addresses the address hashmap
1262 * the address has to be already removed from the hashmap
1263 * @param address the address to delete
1266 GAS_mlp_address_delete (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
1268 GNUNET_STATISTICS_update (mlp->stats,"# LP address deletions", 1, GNUNET_NO);
1270 /* Free resources */
1271 if (address->mlp_information != NULL)
1273 GNUNET_free (address->mlp_information);
1274 address->mlp_information = NULL;
1276 mlp->addr_in_problem --;
1279 /* Remove from peer list */
1280 struct ATS_Peer *head = mlp_find_peer (mlp, &address->peer);
1281 GNUNET_assert (head != NULL);
1283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Deleting address for `%s'\n", GNUNET_i2s (&address->peer));
1285 GNUNET_CONTAINER_DLL_remove (head->head, head->tail, address);
1286 if ((head->head == NULL) && (head->tail == NULL))
1288 /* No address for peer left, remove peer */
1290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Deleting peer `%s'\n", GNUNET_i2s (&address->peer));
1292 GNUNET_CONTAINER_DLL_remove (mlp->peer_head, mlp->peer_tail, head);
1297 /* Update problem */
1298 mlp_delete_problem (mlp);
1299 if ((GNUNET_CONTAINER_multihashmap_size (addresses) > 0) && (mlp->c_p > 0))
1301 mlp_create_problem (mlp, addresses);
1304 mlp->presolver_required = GNUNET_YES;
1305 mlp_solve_problem (mlp);
1310 * Changes the preferences for a peer in the MLP problem
1312 * @param mlp the MLP Handle
1313 * @param peer the peer
1314 * @param kind the kind to change the preference
1315 * @param float the score
1318 GAS_mlp_address_change_preference (struct GAS_MLP_Handle *mlp,
1319 const struct GNUNET_PeerIdentity *peer,
1320 enum GNUNET_ATS_PreferenceKind kind,
1323 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1325 struct ATS_Peer *p = mlp_find_peer (mlp, peer);
1327 /* Here we have to do the matching */
1331 * Shutdown the MLP problem solving component
1332 * @param mlp the MLP handle
1335 GAS_mlp_done (struct GAS_MLP_Handle *mlp)
1337 struct ATS_Peer * peer;
1338 struct ATS_Peer * tmp;
1340 if (mlp->mlp_task != GNUNET_SCHEDULER_NO_TASK)
1342 GNUNET_SCHEDULER_cancel(mlp->mlp_task);
1343 mlp->mlp_task = GNUNET_SCHEDULER_NO_TASK;
1346 /* clean up peer list */
1349 peer = mlp->peer_head;
1350 while (peer != NULL)
1352 GNUNET_CONTAINER_DLL_remove(mlp->peer_head, mlp->peer_tail, peer);
1358 mlp_delete_problem (mlp);
1360 /* Clean up GLPK environment */
1367 /* end of gnunet-service-ats_addresses_mlp.c */