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 LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)
36 #define WRITE_MLP GNUNET_NO
37 #define DEBUG_ATS GNUNET_NO
38 #define VERBOSE_GLPK GNUNET_NO
40 #define ENABLE_C8 GNUNET_YES
41 #define ENABLE_C9 GNUNET_YES
43 * Translate glpk solver error codes to text
44 * @param retcode return code
45 * @return string with result
48 mlp_solve_to_string (int retcode)
54 return "invalid basis";
56 return "singular matrix";
58 return "ill-conditioned matrix";
60 return "invalid bounds";
62 return "solver failed";
64 return "objective lower limit reached";
66 return "objective upper limit reached";
68 return "iteration limit exceeded";
70 return "time limit exceeded";
72 return "no primal feasible solution";
74 return "root LP optimum not provided";
76 return "search terminated by application";
78 return "relative mip gap tolerance reached";
80 return "no dual feasible solution";
82 return "no convergence";
84 return "numerical instability";
86 return "invalid data";
88 return "result out of range";
91 return "unknown error";
97 * Translate glpk status error codes to text
98 * @param retcode return code
99 * @return string with result
102 mlp_status_to_string (int retcode)
106 return "solution is undefined";
108 return "solution is feasible";
110 return "solution is infeasible";
112 return "no feasible solution exists";
114 return "solution is optimal";
116 return "solution is unbounded";
119 return "unknown error";
124 * Translate ATS properties to text
125 * Just intended for debugging
127 * @param ats_index the ATS index
128 * @return string with result
131 mlp_ats_to_string (int ats_index)
134 case GNUNET_ATS_ARRAY_TERMINATOR:
135 return "GNUNET_ATS_ARRAY_TERMINATOR";
136 case GNUNET_ATS_UTILIZATION_UP:
137 return "GNUNET_ATS_UTILIZATION_UP";
138 case GNUNET_ATS_UTILIZATION_DOWN:
139 return "GNUNET_ATS_UTILIZATION_DOWN";
140 case GNUNET_ATS_COST_LAN:
141 return "GNUNET_ATS_COST_LAN";
142 case GNUNET_ATS_COST_WAN:
143 return "GNUNET_ATS_COST_LAN";
144 case GNUNET_ATS_COST_WLAN:
145 return "GNUNET_ATS_COST_WLAN";
146 case GNUNET_ATS_NETWORK_TYPE:
147 return "GNUNET_ATS_NETWORK_TYPE";
148 case GNUNET_ATS_QUALITY_NET_DELAY:
149 return "GNUNET_ATS_QUALITY_NET_DELAY";
150 case GNUNET_ATS_QUALITY_NET_DISTANCE:
151 return "GNUNET_ATS_QUALITY_NET_DISTANCE";
159 * Find a peer in the DLL
161 * @param mlp the mlp handle
162 * @param peer the peer to find
163 * @return the peer struct
165 static struct ATS_Peer *
166 mlp_find_peer (struct GAS_MLP_Handle *mlp, const struct GNUNET_PeerIdentity *peer)
168 struct ATS_Peer *res = mlp->peer_head;
171 if (0 == memcmp (peer, &res->id, sizeof (struct GNUNET_PeerIdentity)))
179 * Intercept GLPK terminal output
180 * @param info the mlp handle
181 * @param s the string to print
182 * @return 0: glpk prints output on terminal, 0 != surpress output
185 mlp_term_hook (void *info, const char *s)
187 /* Not needed atm struct MLP_information *mlp = info; */
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s", s);
193 * Delete the MLP problem and free the constrain matrix
195 * @param mlp the MLP handle
198 mlp_delete_problem (struct GAS_MLP_Handle *mlp)
202 if (mlp->prob != NULL)
203 glp_delete_prob(mlp->prob);
205 /* delete row index */
208 GNUNET_free (mlp->ia);
212 /* delete column index */
215 GNUNET_free (mlp->ja);
219 /* delete coefficients */
222 GNUNET_free (mlp->ar);
231 * Add constraints that are iterating over "forall addresses"
232 * and collects all existing peers for "forall peers" constraints
234 * @param cls GAS_MLP_Handle
235 * @param key Hashcode
236 * @param value ATS_Address
238 * @return GNUNET_OK to continue
241 create_constraint_it (void *cls, const struct GNUNET_HashCode * key, void *value)
243 struct GAS_MLP_Handle *mlp = cls;
244 struct ATS_Address *address = value;
245 struct MLP_information *mlpi;
246 unsigned int row_index;
249 GNUNET_assert (address->mlp_information != NULL);
250 mlpi = (struct MLP_information *) address->mlp_information;
252 /* c 1) bandwidth capping
253 * b_t + (-M) * n_t <= 0
255 row_index = glp_add_rows (mlp->prob, 1);
256 mlpi->r_c1 = row_index;
258 GNUNET_asprintf(&name, "c1_%s_%s", GNUNET_i2s(&address->peer), address->plugin);
259 glp_set_row_name (mlp->prob, row_index, name);
261 /* set row bounds: <= 0 */
262 glp_set_row_bnds (mlp->prob, row_index, GLP_UP, 0.0, 0.0);
263 mlp->ia[mlp->ci] = row_index;
264 mlp->ja[mlp->ci] = mlpi->c_b;
265 mlp->ar[mlp->ci] = 1;
268 mlp->ia[mlp->ci] = row_index;
269 mlp->ja[mlp->ci] = mlpi->c_n;
270 mlp->ar[mlp->ci] = -mlp->BIG_M;
273 /* c 3) minimum bandwidth
274 * b_t + (-n_t * b_min) >= 0
277 row_index = glp_add_rows (mlp->prob, 1);
279 GNUNET_asprintf(&name, "c3_%s_%s", GNUNET_i2s(&address->peer), address->plugin);
280 glp_set_row_name (mlp->prob, row_index, name);
282 mlpi->r_c3 = row_index;
283 /* set row bounds: >= 0 */
284 glp_set_row_bnds (mlp->prob, row_index, GLP_LO, 0.0, 0.0);
286 mlp->ia[mlp->ci] = row_index;
287 mlp->ja[mlp->ci] = mlpi->c_b;
288 mlp->ar[mlp->ci] = 1;
291 mlp->ia[mlp->ci] = row_index;
292 mlp->ja[mlp->ci] = mlpi->c_n;
293 mlp->ar[mlp->ci] = - (double) mlp->b_min;
296 /* c 4) minimum connections
297 * (1)*n_1 + ... + (1)*n_m >= n_min
299 mlp->ia[mlp->ci] = mlp->r_c4;
300 mlp->ja[mlp->ci] = mlpi->c_n;
301 mlp->ar[mlp->ci] = 1;
304 /* c 6) maximize diversity
305 * (1)*n_1 + ... + (1)*n_m - d == 0
307 mlp->ia[mlp->ci] = mlp->r_c6;
308 mlp->ja[mlp->ci] = mlpi->c_n;
309 mlp->ar[mlp->ci] = 1;
312 /* c 10) obey network specific quotas
313 * (1)*b_1 + ... + (1)*b_m <= quota_n
318 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
320 if (mlp->quota_index[c] == address->atsp_network_type)
322 cur_row = mlp->r_quota[c];
329 mlp->ia[mlp->ci] = cur_row;
330 mlp->ja[mlp->ci] = mlpi->c_b;
331 mlp->ar[mlp->ci] = 1;
343 * Find the required ATS information for an address
345 * @param addr the address
346 * @param ats_index the desired ATS index
348 * @return the index on success, otherwise GNUNET_SYSERR
352 mlp_lookup_ats (struct ATS_Address *addr, int ats_index)
354 struct GNUNET_ATS_Information * ats = addr->ats;
356 int found = GNUNET_NO;
357 for (c = 0; c < addr->ats_count; c++)
359 if (ats[c].type == ats_index)
365 if (found == GNUNET_YES)
368 return GNUNET_SYSERR;
372 * Adds the problem constraints for all addresses
373 * Required for problem recreation after address deletion
375 * @param mlp the mlp handle
376 * @param addresses all addresses
380 mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
382 unsigned int n_addresses;
387 n_addresses = GNUNET_CONTAINER_multihashmap_size(addresses);
389 /* Required indices in the constrain matrix
391 * feasibility constraints:
393 * c 1) bandwidth capping
394 * #rows: |n_addresses|
395 * #indices: 2 * |n_addresses|
397 * c 2) one active address per peer
399 * #indices: |n_addresses|
401 * c 3) minium bandwidth assigned
402 * #rows: |n_addresses|
403 * #indices: 2 * |n_addresses|
405 * c 4) minimum number of active connections
407 * #indices: |n_addresses|
409 * c 5) maximum ressource consumption
410 * #rows: |ressources|
411 * #indices: |n_addresses|
413 * c 10) obey network specific quota
414 * #rows: |network types
415 * #indices: |n_addresses|
417 * Sum for feasibility constraints:
418 * #rows: 3 * |n_addresses| + |ressources| + |peers| + 1
419 * #indices: 7 * |n_addresses|
421 * optimality constraints:
425 * #indices: |n_addresses| + 1
428 * #rows: |quality properties|
429 * #indices: |n_addresses| + |quality properties|
433 * #indices: |n_addresses| + 1
437 * #indices: |n_addresses| + |peers|
440 /* last +1 caused by glpk index starting with one: [1..pi]*/
441 int pi = ((7 * n_addresses) + (5 * n_addresses + mlp->m_q + mlp->c_p + 2) + 1);
446 int *ia = GNUNET_malloc (pi * sizeof (int));
450 int *ja = GNUNET_malloc (pi * sizeof (int));
454 double *ar= GNUNET_malloc (pi * sizeof (double));
457 /* Adding constraint rows
458 * This constraints are kind of "for all addresses"
459 * Feasibility constraints:
461 * c 1) bandwidth capping
462 * c 3) minimum bandwidth
463 * c 4) minimum number of connections
464 * c 6) maximize diversity
465 * c 10) obey network specific quota
468 /* Row for c4) minimum connection */
469 int min = mlp->n_min;
470 /* Number of minimum connections is min(|Peers|, n_min) */
471 if (mlp->n_min > mlp->c_p)
474 mlp->r_c4 = glp_add_rows (mlp->prob, 1);
475 glp_set_row_name (mlp->prob, mlp->r_c4, "c4");
476 glp_set_row_bnds (mlp->prob, mlp->r_c4, GLP_LO, min, min);
478 /* Add row for c6) */
480 mlp->r_c6 = glp_add_rows (mlp->prob, 1);
481 /* Set type type to fix */
482 glp_set_row_bnds (mlp->prob, mlp->r_c6, GLP_FX, 0.0, 0.0);
484 ia[mlp->ci] = mlp->r_c6 ;
485 ja[mlp->ci] = mlp->c_d;
489 /* Add rows for c 10) */
490 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
492 mlp->r_quota[c] = glp_add_rows (mlp->prob, 1);
494 GNUNET_asprintf(&text, "quota_ats_%i", mlp->quota_index[c]);
495 glp_set_row_name (mlp->prob, mlp->r_quota[c], text);
497 /* Set bounds to 0 <= x <= quota_out */
498 glp_set_row_bnds (mlp->prob, mlp->r_quota[c], GLP_UP, 0.0, mlp->quota_out[c]);
501 GNUNET_CONTAINER_multihashmap_iterate (addresses, create_constraint_it, mlp);
503 /* Adding constraint rows
504 * This constraints are kind of "for all peers"
505 * Feasibility constraints:
507 * c 2) 1 address per peer
508 * sum (n_p1_1 + ... + n_p1_n) = 1
511 * sum (f_p * b_p1_1 + ... + f_p * b_p1_n) - u = 0
514 * V p : sum (bt_1 + ... +bt_n) - f_p * r = 0
517 /* Adding rows for c 8) */
518 mlp->r_c8 = glp_add_rows (mlp->prob, mlp->c_p);
519 glp_set_row_name (mlp->prob, mlp->r_c8, "c8");
520 /* Set row bound == 0 */
521 glp_set_row_bnds (mlp->prob, mlp->r_c8, GLP_FX, 0.0, 0.0);
524 ia[mlp->ci] = mlp->r_c8;
525 ja[mlp->ci] = mlp->c_u;
529 struct ATS_Peer * peer = mlp->peer_head;
533 struct ATS_Address *addr = peer->head;
534 struct MLP_information *mlpi = NULL;
536 /* Adding rows for c 2) */
537 peer->r_c2 = glp_add_rows (mlp->prob, 1);
538 GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&peer->id));
539 glp_set_row_name (mlp->prob, peer->r_c2, name);
541 /* Set row bound == 1 */
542 glp_set_row_bnds (mlp->prob, peer->r_c2, GLP_FX, 1.0, 1.0);
544 /* Adding rows for c 9) */
546 peer->r_c9 = glp_add_rows (mlp->prob, 1);
547 GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&peer->id));
548 glp_set_row_name (mlp->prob, peer->r_c9, name);
550 /* Set row bound == 0 */
551 glp_set_row_bnds (mlp->prob, peer->r_c9, GLP_LO, 0.0, 0.0);
554 ia[mlp->ci] = peer->r_c9;
555 ja[mlp->ci] = mlp->c_r;
556 ar[mlp->ci] = -peer->f;
559 /* For all addresses of this peer */
562 mlpi = (struct MLP_information *) addr->mlp_information;
564 /* coefficient for c 2) */
565 ia[mlp->ci] = peer->r_c2;
566 ja[mlp->ci] = mlpi->c_n;
570 /* coefficient for c 8) */
571 ia[mlp->ci] = mlp->r_c8;
572 ja[mlp->ci] = mlpi->c_b;
573 ar[mlp->ci] = peer->f;
577 /* coefficient for c 9) */
578 ia[mlp->ci] = peer->r_c9;
579 ja[mlp->ci] = mlpi->c_b;
589 /* c 7) For all quality metrics */
590 for (c = 0; c < mlp->m_q; c++)
593 struct ATS_Address *ta;
594 struct MLP_information * mlpi;
597 /* Adding rows for c 7) */
598 mlp->r_q[c] = glp_add_rows (mlp->prob, 1);
599 GNUNET_asprintf(&name, "c7_q%i_%s", c, mlp_ats_to_string(mlp->q[c]));
600 glp_set_row_name (mlp->prob, mlp->r_q[c], name);
602 /* Set row bound == 0 */
603 glp_set_row_bnds (mlp->prob, mlp->r_q[c], GLP_FX, 0.0, 0.0);
605 ia[mlp->ci] = mlp->r_q[c];
606 ja[mlp->ci] = mlp->c_q[c];
610 for (tp = mlp->peer_head; tp != NULL; tp = tp->next)
611 for (ta = tp->head; ta != NULL; ta = ta->next)
613 mlpi = ta->mlp_information;
614 value = mlpi->q_averaged[c];
616 mlpi->r_q[c] = mlp->r_q[c];
618 ia[mlp->ci] = mlp->r_q[c];
619 ja[mlp->ci] = mlpi->c_b;
620 ar[mlp->ci] = tp->f_q[c] * value;
628 * Add columns for all addresses
630 * @param cls GAS_MLP_Handle
631 * @param key Hashcode
632 * @param value ATS_Address
634 * @return GNUNET_OK to continue
637 create_columns_it (void *cls, const struct GNUNET_HashCode * key, void *value)
639 struct GAS_MLP_Handle *mlp = cls;
640 struct ATS_Address *address = value;
641 struct MLP_information *mlpi;
645 GNUNET_assert (address->mlp_information != NULL);
646 mlpi = address->mlp_information;
648 /* Add bandwidth column */
649 col = glp_add_cols (mlp->prob, 2);
654 GNUNET_asprintf (&name, "b_%s_%s", GNUNET_i2s (&address->peer), address->plugin);
655 glp_set_col_name (mlp->prob, mlpi->c_b , name);
657 /* Lower bound == 0 */
658 glp_set_col_bnds (mlp->prob, mlpi->c_b , GLP_LO, 0.0, 0.0);
659 /* Continuous value*/
660 glp_set_col_kind (mlp->prob, mlpi->c_b , GLP_CV);
661 /* Objective function coefficient == 0 */
662 glp_set_obj_coef (mlp->prob, mlpi->c_b , 0);
665 /* Add usage column */
666 GNUNET_asprintf (&name, "n_%s_%s", GNUNET_i2s (&address->peer), address->plugin);
667 glp_set_col_name (mlp->prob, mlpi->c_n, name);
669 /* Limit value : 0 <= value <= 1 */
670 glp_set_col_bnds (mlp->prob, mlpi->c_n, GLP_DB, 0.0, 1.0);
672 glp_set_col_kind (mlp->prob, mlpi->c_n, GLP_IV);
673 /* Objective function coefficient == 0 */
674 glp_set_obj_coef (mlp->prob, mlpi->c_n, 0);
682 * Create the MLP problem
684 * @param mlp the MLP handle
685 * @param addresses the hashmap containing all adresses
686 * @return GNUNET_OK or GNUNET_SYSERR
689 mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
696 GNUNET_assert (mlp->prob == NULL);
698 /* create the glpk problem */
699 mlp->prob = glp_create_prob ();
701 /* Set a problem name */
702 glp_set_prob_name (mlp->prob, "gnunet ats bandwidth distribution");
704 /* Set optimization direction to maximize */
705 glp_set_obj_dir (mlp->prob, GLP_MAX);
707 /* Adding invariant columns */
709 /* Diversity d column */
710 col = glp_add_cols (mlp->prob, 1);
713 glp_set_col_name (mlp->prob, col, "d");
714 /* Column objective function coefficient */
715 glp_set_obj_coef (mlp->prob, col, mlp->co_D);
716 /* Column lower bound = 0.0 */
717 glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
719 /* Utilization u column */
720 col = glp_add_cols (mlp->prob, 1);
723 glp_set_col_name (mlp->prob, col, "u");
724 /* Column objective function coefficient */
725 glp_set_obj_coef (mlp->prob, col, mlp->co_U);
726 /* Column lower bound = 0.0 */
727 glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
730 /* Relativity r column */
731 col = glp_add_cols (mlp->prob, 1);
734 glp_set_col_name (mlp->prob, col, "r");
735 /* Column objective function coefficient */
736 glp_set_obj_coef (mlp->prob, col, mlp->co_R);
737 /* Column lower bound = 0.0 */
738 glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
741 /* Quality metric columns */
742 col = glp_add_cols(mlp->prob, mlp->m_q);
743 for (c = 0; c < mlp->m_q; c++)
745 mlp->c_q[c] = col + c;
746 GNUNET_asprintf (&name, "q_%u", mlp->q[c]);
747 glp_set_col_name (mlp->prob, col + c, name);
748 /* Column lower bound = 0.0 */
749 glp_set_col_bnds (mlp->prob, col + c, GLP_LO, 0.0, 0.0);
751 /* Coefficient == Qm */
752 glp_set_obj_coef (mlp->prob, col + c, mlp->co_Q[c]);
755 /* Add columns for addresses */
756 GNUNET_CONTAINER_multihashmap_iterate (addresses, create_columns_it, mlp);
758 /* Add constraints */
759 mlp_add_constraints_all_addresses (mlp, addresses);
761 /* Load the matrix */
762 glp_load_matrix(mlp->prob, (mlp->ci-1), mlp->ia, mlp->ja, mlp->ar);
769 * Solves the LP problem
771 * @param mlp the MLP Handle
772 * @param s_ctx context to return results
773 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
776 mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp, struct GAS_MLP_SolutionContext *s_ctx)
779 struct GNUNET_TIME_Relative duration;
780 struct GNUNET_TIME_Absolute end;
781 struct GNUNET_TIME_Absolute start = GNUNET_TIME_absolute_get();
784 * Presolver is required if the problem was modified and an existing
785 * valid basis is now invalid */
786 if (mlp->presolver_required == GNUNET_YES)
787 mlp->control_param_lp.presolve = GLP_ON;
789 mlp->control_param_lp.presolve = GLP_OFF;
791 /* Solve LP problem to have initial valid solution */
793 res = glp_simplex(mlp->prob, &mlp->control_param_lp);
796 /* The LP problem instance has been successfully solved. */
798 else if (res == GLP_EITLIM)
800 /* simplex iteration limit has been exceeded. */
801 // TODO Increase iteration limit?
803 else if (res == GLP_ETMLIM)
805 /* Time limit has been exceeded. */
806 // TODO Increase time limit?
810 /* Problem was ill-defined, retry with presolver */
811 if (mlp->presolver_required == GNUNET_NO)
813 mlp->presolver_required = GNUNET_YES;
818 /* Problem was ill-defined, no way to handle that */
819 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
821 "Solving LP problem failed: %i %s\n", res, mlp_solve_to_string(res));
822 return GNUNET_SYSERR;
826 end = GNUNET_TIME_absolute_get ();
827 duration = GNUNET_TIME_absolute_get_difference (start, end);
829 mlp->lp_total_duration += duration.rel_value;
830 s_ctx->lp_duration = duration;
832 GNUNET_STATISTICS_update (mlp->stats,"# LP problem solved", 1, GNUNET_NO);
833 GNUNET_STATISTICS_set (mlp->stats,"# LP execution time (ms)", duration.rel_value, GNUNET_NO);
834 GNUNET_STATISTICS_set (mlp->stats,"# LP execution time average (ms)",
835 mlp->lp_total_duration / mlp->lp_solved, GNUNET_NO);
837 /* Analyze problem status */
838 res = glp_get_status (mlp->prob);
840 /* solution is optimal */
842 /* solution is feasible */
846 /* Problem was ill-defined, no way to handle that */
848 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
850 "Solving LP problem failed, no solution: %s\n", mlp_status_to_string(res));
851 return GNUNET_SYSERR;
855 /* solved sucessfully, no presolver required next time */
856 mlp->presolver_required = GNUNET_NO;
863 * Solves the MLP problem
865 * @param mlp the MLP Handle
866 * @param s_ctx context to return results
867 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
870 mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp, struct GAS_MLP_SolutionContext *s_ctx)
873 struct GNUNET_TIME_Relative duration;
874 struct GNUNET_TIME_Absolute end;
875 struct GNUNET_TIME_Absolute start = GNUNET_TIME_absolute_get();
877 /* solve MLP problem */
878 res = glp_intopt(mlp->prob, &mlp->control_param_mlp);
882 /* The MLP problem instance has been successfully solved. */
884 else if (res == GLP_EITLIM)
886 /* simplex iteration limit has been exceeded. */
887 // TODO Increase iteration limit?
889 else if (res == GLP_ETMLIM)
891 /* Time limit has been exceeded. */
892 // TODO Increase time limit?
896 /* Problem was ill-defined, no way to handle that */
897 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
899 "Solving MLP problem failed: %s\n", mlp_solve_to_string(res));
900 return GNUNET_SYSERR;
903 end = GNUNET_TIME_absolute_get ();
904 duration = GNUNET_TIME_absolute_get_difference (start, end);
906 mlp->mlp_total_duration += duration.rel_value;
907 s_ctx->mlp_duration = duration;
909 GNUNET_STATISTICS_update (mlp->stats,"# MLP problem solved", 1, GNUNET_NO);
910 GNUNET_STATISTICS_set (mlp->stats,"# MLP execution time (ms)", duration.rel_value, GNUNET_NO);
911 GNUNET_STATISTICS_set (mlp->stats,"# MLP execution time average (ms)",
912 mlp->mlp_total_duration / mlp->mlp_solved, GNUNET_NO);
914 /* Analyze problem status */
915 res = glp_mip_status(mlp->prob);
917 /* solution is optimal */
919 /* solution is feasible */
923 /* Problem was ill-defined, no way to handle that */
925 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
927 "Solving MLP problem failed, %s\n\n", mlp_status_to_string(res));
928 return GNUNET_SYSERR;
935 int GAS_mlp_solve_problem (void *solver, struct GAS_MLP_SolutionContext *ctx);
939 mlp_scheduler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
941 struct GAS_MLP_Handle *mlp = cls;
942 struct GAS_MLP_SolutionContext ctx;
944 mlp->mlp_task = GNUNET_SCHEDULER_NO_TASK;
946 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduled problem solving\n");
951 if (mlp->addr_in_problem != 0)
952 GAS_mlp_solve_problem(mlp, &ctx);
957 * Solves the MLP problem
959 * @param solver the MLP Handle
960 * @param ctx solution context
961 * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
964 GAS_mlp_solve_problem (void *solver, struct GAS_MLP_SolutionContext *ctx)
966 struct GAS_MLP_Handle *mlp = solver;
968 /* Check if solving is already running */
969 if (GNUNET_YES == mlp->semaphore)
971 if (mlp->mlp_task != GNUNET_SCHEDULER_NO_TASK)
973 GNUNET_SCHEDULER_cancel(mlp->mlp_task);
974 mlp->mlp_task = GNUNET_SCHEDULER_NO_TASK;
976 mlp->mlp_task = GNUNET_SCHEDULER_add_delayed (mlp->exec_interval, &mlp_scheduler, mlp);
977 return GNUNET_SYSERR;
979 mlp->semaphore = GNUNET_YES;
981 mlp->last_execution = GNUNET_TIME_absolute_get ();
983 ctx->lp_result = GNUNET_SYSERR;
984 ctx->mlp_result = GNUNET_SYSERR;
985 ctx->lp_duration = GNUNET_TIME_UNIT_FOREVER_REL;
986 ctx->mlp_duration = GNUNET_TIME_UNIT_FOREVER_REL;
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Solve LP problem\n");
993 GNUNET_asprintf(&name, "problem_%i", i);
994 glp_write_lp (mlp->prob, 0, name);
998 res = mlp_solve_lp_problem (mlp, ctx);
999 ctx->lp_result = res;
1000 if (res != GNUNET_OK)
1002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "LP Problem solving failed\n");
1003 mlp->semaphore = GNUNET_NO;
1004 return GNUNET_SYSERR;
1008 GNUNET_asprintf(&name, "problem_%i_lp_solution", i);
1009 glp_print_sol (mlp->prob, name);
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Solve MLP problem\n");
1015 res = mlp_solve_mlp_problem (mlp, ctx);
1016 ctx->mlp_result = res;
1017 if (res != GNUNET_OK)
1019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP Problem solving failed\n");
1020 mlp->semaphore = GNUNET_NO;
1021 return GNUNET_SYSERR;
1024 GNUNET_asprintf(&name, "problem_%i_mlp_solution", i);
1025 glp_print_mip (mlp->prob, name);
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem solved %s (LP duration %llu / MLP duration %llu)\n",
1030 (GNUNET_OK == res) ? "successfully" : "failed", ctx->lp_duration.rel_value, ctx->mlp_duration.rel_value);
1031 /* Process result */
1032 struct ATS_Peer *p = NULL;
1033 struct ATS_Address *a = NULL;
1034 struct MLP_information *mlpi = NULL;
1036 for (p = mlp->peer_head; p != NULL; p = p->next)
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s'\n", GNUNET_i2s (&p->id));
1039 for (a = p->head; a != NULL; a = a->next)
1044 mlpi = a->mlp_information;
1046 b = glp_mip_col_val(mlp->prob, mlpi->c_b);
1049 n = glp_mip_col_val(mlp->prob, mlpi->c_n);
1051 mlpi->n = GNUNET_YES;
1053 mlpi->n = GNUNET_NO;
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tAddress %s %f\n",
1056 (n == 1.0) ? "[x]" : "[ ]", b);
1060 if (mlp->mlp_task != GNUNET_SCHEDULER_NO_TASK)
1062 GNUNET_SCHEDULER_cancel(mlp->mlp_task);
1063 mlp->mlp_task = GNUNET_SCHEDULER_NO_TASK;
1065 mlp->mlp_task = GNUNET_SCHEDULER_add_delayed (mlp->exec_interval, &mlp_scheduler, mlp);
1066 mlp->semaphore = GNUNET_NO;
1071 * Init the MLP problem solving component
1073 * @param cfg the GNUNET_CONFIGURATION_Handle handle
1074 * @param stats the GNUNET_STATISTICS handle
1075 * @return struct GAS_MLP_Handle on success, NULL on fail
1078 GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1079 const struct GNUNET_STATISTICS_Handle *stats,
1081 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1083 struct GAS_MLP_Handle * mlp = GNUNET_malloc (sizeof (struct GAS_MLP_Handle));
1088 unsigned long long tmp;
1091 struct GNUNET_TIME_Relative i_exec;
1093 char * quota_out_str;
1094 char * quota_in_str;
1096 struct GNUNET_TIME_Relative max_duration;
1097 long long unsigned int max_iterations;
1099 /* Init GLPK environment */
1100 int res = glp_init_env();
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1104 "initialization successful");
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
1108 "environment is already initialized");
1111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1112 "initialization failed (insufficient memory)");
1117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
1118 "initialization failed (unsupported programming model)");
1126 /* Create initial MLP problem */
1127 mlp->prob = glp_create_prob();
1128 GNUNET_assert (mlp->prob != NULL);
1130 mlp->BIG_M = (double) BIG_M_VALUE;
1132 /* Get timeout for iterations */
1133 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(cfg, "ats", "MAX_DURATION", &max_duration))
1135 max_duration = MLP_MAX_EXEC_DURATION;
1138 /* Get maximum number of iterations */
1139 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "MAX_ITERATIONS", &max_iterations))
1141 max_iterations = MLP_MAX_ITERATIONS;
1144 /* Get diversity coefficient from configuration */
1145 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1148 D = (double) tmp / 100;
1152 /* Get proportionality coefficient from configuration */
1153 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1156 R = (double) tmp / 100;
1160 /* Get utilization coefficient from configuration */
1161 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1164 U = (double) tmp / 100;
1168 /* Get quality metric coefficients from configuration */
1170 int i_distance = -1;
1171 int q[GNUNET_ATS_QualityPropertiesCount] = GNUNET_ATS_QualityProperties;
1172 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1174 /* initialize quality coefficients with default value 1.0 */
1178 if (q[c] == GNUNET_ATS_QUALITY_NET_DELAY)
1180 if (q[c] == GNUNET_ATS_QUALITY_NET_DISTANCE)
1184 if ((i_delay != -1) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1185 "COEFFICIENT_QUALITY_DELAY",
1188 mlp->co_Q[i_delay] = (double) tmp / 100;
1190 mlp->co_Q[i_delay] = 1.0;
1192 if ((i_distance != -1) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1193 "COEFFICIENT_QUALITY_DISTANCE",
1195 mlp->co_Q[i_distance] = (double) tmp / 100;
1197 mlp->co_Q[i_distance] = 1.0;
1199 /* Get minimum bandwidth per used address from configuration */
1200 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1206 b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
1209 /* Get minimum number of connections from configuration */
1210 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
1217 /* Init network quotas */
1218 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1219 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1221 mlp->quota_index[c] = quotas[c];
1222 static char * entry_in = NULL;
1223 static char * entry_out = NULL;
1224 unsigned long long quota_in = 0;
1225 unsigned long long quota_out = 0;
1227 switch (quotas[c]) {
1228 case GNUNET_ATS_NET_UNSPECIFIED:
1229 entry_out = "UNSPECIFIED_QUOTA_OUT";
1230 entry_in = "UNSPECIFIED_QUOTA_IN";
1232 case GNUNET_ATS_NET_LOOPBACK:
1233 entry_out = "LOOPBACK_QUOTA_OUT";
1234 entry_in = "LOOPBACK_QUOTA_IN";
1236 case GNUNET_ATS_NET_LAN:
1237 entry_out = "LAN_QUOTA_OUT";
1238 entry_in = "LAN_QUOTA_IN";
1240 case GNUNET_ATS_NET_WAN:
1241 entry_out = "WAN_QUOTA_OUT";
1242 entry_in = "WAN_QUOTA_IN";
1244 case GNUNET_ATS_NET_WLAN:
1245 entry_out = "WLAN_QUOTA_OUT";
1246 entry_in = "WLAN_QUOTA_IN";
1252 if ((entry_in == NULL) || (entry_out == NULL))
1255 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
1257 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
1258 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, "a_out)))
1259 quota_out = mlp->BIG_M;
1261 GNUNET_free (quota_out_str);
1262 quota_out_str = NULL;
1264 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
1266 quota_out = mlp->BIG_M;
1270 quota_out = mlp->BIG_M;
1273 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
1275 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
1276 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, "a_in)))
1277 quota_in = mlp->BIG_M;
1279 GNUNET_free (quota_in_str);
1280 quota_in_str = NULL;
1282 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
1284 quota_in = mlp->BIG_M;
1288 quota_in = mlp->BIG_M;
1291 /* Check if defined quota could make problem unsolvable */
1292 if (((n_min * b_min) > quota_out) && (GNUNET_ATS_NET_UNSPECIFIED != quotas[c]))
1294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Inconsistent quota configuration value `%s': "
1295 "outbound quota (%u Bps) too small for combination of minimum connections and minimum bandwidth per peer (%u * %u Bps = %u)\n", entry_out, quota_out, n_min, b_min, n_min * b_min);
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' quota %llu and `%s' quota %llu\n",
1303 entry_out, quota_out, entry_in, quota_in);
1304 GNUNET_STATISTICS_update ((struct GNUNET_STATISTICS_Handle *) stats, entry_out, quota_out, GNUNET_NO);
1305 GNUNET_STATISTICS_update ((struct GNUNET_STATISTICS_Handle *) stats, entry_in, quota_in, GNUNET_NO);
1306 mlp->quota_out[c] = quota_out;
1307 mlp->quota_in[c] = quota_in;
1310 /* Get minimum number of connections from configuration */
1311 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_time (cfg, "ats",
1312 "ATS_EXEC_INTERVAL",
1314 mlp->exec_interval = i_exec;
1316 mlp->exec_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30);
1318 mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1319 mlp->max_iterations = max_iterations;
1320 mlp->max_exec_duration = max_duration;
1321 mlp->auto_solve = GNUNET_YES;
1323 /* Redirect GLPK output to GNUnet logging */
1324 glp_error_hook((void *) mlp, &mlp_term_hook);
1326 /* Init LP solving parameters */
1327 glp_init_smcp(&mlp->control_param_lp);
1329 mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
1331 mlp->control_param_lp.msg_lev = GLP_MSG_ALL;
1334 mlp->control_param_lp.it_lim = max_iterations;
1335 mlp->control_param_lp.tm_lim = max_duration.rel_value;
1337 /* Init MLP solving parameters */
1338 glp_init_iocp(&mlp->control_param_mlp);
1340 mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
1342 mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
1344 mlp->control_param_mlp.tm_lim = max_duration.rel_value;
1346 mlp->last_execution = GNUNET_TIME_UNIT_FOREVER_ABS;
1353 mlp->m_q = GNUNET_ATS_QualityPropertiesCount;
1354 mlp->semaphore = GNUNET_NO;
1359 update_quality (struct GAS_MLP_Handle *mlp, struct ATS_Address * address)
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating quality metrics for peer `%s'\n",
1362 GNUNET_i2s (&address->peer));
1364 GNUNET_assert (NULL != address);
1365 GNUNET_assert (NULL != address->mlp_information);
1366 GNUNET_assert (NULL != address->ats);
1368 struct MLP_information *mlpi = address->mlp_information;
1369 struct GNUNET_ATS_Information *ats = address->ats;
1370 GNUNET_assert (mlpi != NULL);
1373 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1375 int index = mlp_lookup_ats(address, mlp->q[c]);
1377 if (index == GNUNET_SYSERR)
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' value `%s': %f\n",
1381 GNUNET_i2s (&address->peer),
1382 mlp_ats_to_string(mlp->q[c]),
1383 (double) ats[index].value);
1385 int i = mlpi->q_avg_i[c];
1386 double * qp = mlpi->q[c];
1387 qp[i] = (double) ats[index].value;
1390 for (t = 0; t < MLP_AVERAGING_QUEUE_LENGTH; t++)
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' queue[%u]: %f\n",
1393 GNUNET_i2s (&address->peer),
1394 mlp_ats_to_string(mlp->q[c]),
1399 if (mlpi->q_avg_i[c] + 1 < (MLP_AVERAGING_QUEUE_LENGTH))
1400 mlpi->q_avg_i[c] ++;
1402 mlpi->q_avg_i[c] = 0;
1410 case GNUNET_ATS_QUALITY_NET_DELAY:
1412 for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
1414 if (mlpi->q[c][c2] != -1)
1416 double * t2 = mlpi->q[c] ;
1421 if ((c3 > 0) && (avg > 0))
1422 /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
1423 mlpi->q_averaged[c] = (double) c3 / avg;
1425 mlpi->q_averaged[c] = 0.0;
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' average sum: %f, average: %f, weight: %f\n",
1428 GNUNET_i2s (&address->peer),
1429 mlp_ats_to_string(mlp->q[c]),
1432 mlpi->q_averaged[c]);
1435 case GNUNET_ATS_QUALITY_NET_DISTANCE:
1437 for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
1439 if (mlpi->q[c][c2] != -1)
1441 double * t2 = mlpi->q[c] ;
1446 if ((c3 > 0) && (avg > 0))
1447 /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
1448 mlpi->q_averaged[c] = (double) c3 / avg;
1450 mlpi->q_averaged[c] = 0.0;
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' average sum: %f, average: %f, weight: %f\n",
1453 GNUNET_i2s (&address->peer),
1454 mlp_ats_to_string(mlp->q[c]),
1457 mlpi->q_averaged[c]);
1464 if ((mlpi->c_b != 0) && (mlpi->r_q[c] != 0))
1467 /* Get current number of columns */
1468 int found = GNUNET_NO;
1469 int cols = glp_get_num_cols(mlp->prob);
1470 int *ind = GNUNET_malloc (cols * sizeof (int) + 1);
1471 double *val = GNUNET_malloc (cols * sizeof (double) + 1);
1473 /* Get the matrix row of quality */
1474 int length = glp_get_mat_row(mlp->prob, mlp->r_q[c], ind, val);
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "cols %i, length %i c_b %i\n", cols, length, mlpi->c_b);
1477 /* Get the index if matrix row of quality */
1478 for (c4 = 1; c4 <= length; c4++ )
1480 if (mlpi->c_b == ind[c4])
1482 /* Update the value */
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating quality `%s' column `%s' row `%s' : %f -> %f\n",
1484 mlp_ats_to_string(mlp->q[c]),
1485 glp_get_col_name (mlp->prob, ind[c4]),
1486 glp_get_row_name (mlp->prob, mlp->r_q[c]),
1488 mlpi->q_averaged[c]);
1489 val[c4] = mlpi->q_averaged[c];
1495 if (found == GNUNET_NO)
1498 ind[length+1] = mlpi->c_b;
1499 val[length+1] = mlpi->q_averaged[c];
1500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%i ind[%i] val[%i]: %i %f\n", length+1, length+1, length+1, mlpi->c_b, mlpi->q_averaged[c]);
1501 glp_set_mat_row (mlp->prob, mlpi->r_q[c], length+1, ind, val);
1505 /* Get the index if matrix row of quality */
1506 glp_set_mat_row (mlp->prob, mlpi->r_q[c], length, ind, val);
1516 * Updates a single address in the MLP problem
1518 * If the address did not exist before in the problem:
1519 * The MLP problem has to be recreated and the problem has to be resolved
1521 * Otherwise the addresses' values can be updated and the existing base can
1524 * @param solver the MLP Handle
1525 * @param addresses the address hashmap
1526 * the address has to be already removed from the hashmap
1527 * @param address the address to update
1530 GAS_mlp_address_update (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
1532 struct GAS_MLP_Handle *mlp = solver;
1534 struct MLP_information *mlpi;
1535 struct GAS_MLP_SolutionContext ctx;
1537 GNUNET_STATISTICS_update (mlp->stats, "# MLP address updates", 1, GNUNET_NO);
1539 /* We add a new address */
1540 if (address->mlp_information == NULL)
1546 if (new == GNUNET_YES)
1548 mlpi = GNUNET_malloc (sizeof (struct MLP_information));
1551 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1555 for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
1556 mlpi->q[c][c2] = -1.0; /* -1.0: invalid value */
1557 mlpi->q_avg_i[c] = 0;
1558 mlpi->q_averaged[c] = 0.0;
1561 address->mlp_information = mlpi;
1562 mlp->addr_in_problem ++;
1563 GNUNET_STATISTICS_update (mlp->stats, "# addresses in MLP", 1, GNUNET_NO);
1565 /* Check for and add peer */
1566 struct ATS_Peer *peer = mlp_find_peer (mlp, &address->peer);
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer `%s'\n",
1571 GNUNET_i2s (&address->peer));
1573 peer = GNUNET_malloc (sizeof (struct ATS_Peer));
1578 for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
1584 memcpy (&peer->id, &address->peer, sizeof (struct GNUNET_PeerIdentity));
1585 GNUNET_assert(address->prev == NULL);
1586 GNUNET_assert(address->next == NULL);
1587 GNUNET_CONTAINER_DLL_insert (peer->head, peer->tail, address);
1588 GNUNET_CONTAINER_DLL_insert (mlp->peer_head, mlp->peer_tail, peer);
1590 GNUNET_STATISTICS_update (mlp->stats, "# peers in MLP", 1, GNUNET_NO);
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address to peer `%s'\n",
1596 GNUNET_i2s (&address->peer));
1598 GNUNET_CONTAINER_DLL_insert (peer->head, peer->tail, address);
1600 update_quality (mlp, address);
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating existing address to peer `%s'\n",
1605 GNUNET_i2s (&address->peer));
1607 update_quality (mlp, address);
1611 if (new == GNUNET_YES)
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Recreating problem: new address\n");
1615 mlp_delete_problem (mlp);
1616 mlp_create_problem (mlp, addresses);
1617 mlp->presolver_required = GNUNET_YES;
1619 if (mlp->auto_solve == GNUNET_YES)
1620 GAS_mlp_solve_problem (mlp, &ctx);
1624 * Deletes a single address in the MLP problem
1626 * The MLP problem has to be recreated and the problem has to be resolved
1628 * @param solver the MLP Handle
1629 * @param addresses the address hashmap
1630 * the address has to be already removed from the hashmap
1631 * @param address the address to delete
1634 GAS_mlp_address_delete (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
1636 struct GAS_MLP_Handle *mlp = solver;
1637 GNUNET_STATISTICS_update (mlp->stats,"# LP address deletions", 1, GNUNET_NO);
1638 struct GAS_MLP_SolutionContext ctx;
1640 /* Free resources */
1641 if (address->mlp_information != NULL)
1643 GNUNET_free (address->mlp_information);
1644 address->mlp_information = NULL;
1646 mlp->addr_in_problem --;
1647 GNUNET_STATISTICS_update (mlp->stats, "# addresses in MLP", -1, GNUNET_NO);
1650 /* Remove from peer list */
1651 struct ATS_Peer *head = mlp_find_peer (mlp, &address->peer);
1652 GNUNET_assert (head != NULL);
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting address for `%s'\n", GNUNET_i2s (&address->peer));
1656 GNUNET_CONTAINER_DLL_remove (head->head, head->tail, address);
1657 if ((head->head == NULL) && (head->tail == NULL))
1659 /* No address for peer left, remove peer */
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%s'\n", GNUNET_i2s (&address->peer));
1663 GNUNET_CONTAINER_DLL_remove (mlp->peer_head, mlp->peer_tail, head);
1666 GNUNET_STATISTICS_update (mlp->stats, "# peers in MLP", -1, GNUNET_NO);
1669 /* Update problem */
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Recreating problem: new address\n");
1672 mlp_delete_problem (mlp);
1673 if ((GNUNET_CONTAINER_multihashmap_size (addresses) > 0) && (mlp->c_p > 0))
1675 mlp_create_problem (mlp, addresses);
1678 mlp->presolver_required = GNUNET_YES;
1679 if (mlp->auto_solve == GNUNET_YES)
1680 GAS_mlp_solve_problem (mlp, &ctx);
1685 mlp_get_preferred_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1688 struct ATS_PreferedAddress *aa = (struct ATS_PreferedAddress *) cls;
1689 struct ATS_Address *addr = value;
1690 struct MLP_information *mlpi = addr->mlp_information;
1693 if (mlpi->n == GNUNET_YES)
1696 if (mlpi->b > (double) UINT32_MAX)
1697 aa->bandwidth_out = UINT32_MAX;
1699 aa->bandwidth_out = (uint32_t) mlpi->b;
1700 aa->bandwidth_in = 0;
1708 * Get the preferred address for a specific peer
1710 * @param solver the MLP Handle
1711 * @param addresses address hashmap
1712 * @param peer the peer
1713 * @return suggested address
1715 struct ATS_Address *
1716 GAS_mlp_get_preferred_address (void *solver,
1717 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1718 const struct GNUNET_PeerIdentity *peer)
1720 struct ATS_Address * aa = NULL;
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting preferred address for `%s'\n", GNUNET_i2s (peer));
1722 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, mlp_get_preferred_address_it, aa);
1728 * Changes the preferences for a peer in the MLP problem
1730 * @param solver the MLP Handle
1731 * @param peer the peer
1732 * @param kind the kind to change the preference
1733 * @param score the score
1736 GAS_mlp_address_change_preference (void *solver,
1737 const struct GNUNET_PeerIdentity *peer,
1738 enum GNUNET_ATS_PreferenceKind kind,
1741 struct GAS_MLP_Handle *mlp = solver;
1742 GNUNET_STATISTICS_update (mlp->stats,"# LP address preference changes", 1, GNUNET_NO);
1744 //struct ATS_Peer *p = mlp_find_peer (mlp, peer);
1745 //FIXME to finish implementation
1746 /* Here we have to do the matching */
1751 * Shutdown the MLP problem solving component
1753 * @param solver the solver handle
1756 GAS_mlp_done (void *solver)
1758 struct GAS_MLP_Handle *mlp = solver;
1759 struct ATS_Peer * peer;
1760 struct ATS_Address *addr;
1762 GNUNET_assert (mlp != NULL);
1764 if (mlp->mlp_task != GNUNET_SCHEDULER_NO_TASK)
1766 GNUNET_SCHEDULER_cancel(mlp->mlp_task);
1767 mlp->mlp_task = GNUNET_SCHEDULER_NO_TASK;
1770 /* clean up peer list */
1771 peer = mlp->peer_head;
1772 while (peer != NULL)
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up peer `%s'\n", GNUNET_i2s (&peer->id));
1775 GNUNET_CONTAINER_DLL_remove(mlp->peer_head, mlp->peer_tail, peer);
1776 for (addr = peer->head; NULL != addr; addr = peer->head)
1778 GNUNET_CONTAINER_DLL_remove(peer->head, peer->tail, addr);
1779 GNUNET_free (addr->mlp_information);
1780 addr->mlp_information = NULL;
1783 peer = mlp->peer_head;
1785 mlp_delete_problem (mlp);
1787 /* Clean up GLPK environment */
1794 /* end of gnunet-service-ats_addresses_mlp.c */