summaryrefslogtreecommitdiff
path: root/src/transport/gnunet-service-transport_ats.c
blob: 87aa34bf4fd7945aebe54a51671b8b4e77ea04d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
/*
     This file is part of GNUnet.
     (C) 2009, 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file transport/transport_ats.c
 * @brief automatic transport selection
 * @author Matthias Wachs
 *
 */


#include "gnunet-service-transport_ats.h"
#include "gnunet_transport_service.h"
#include "gnunet_statistics_service.h"
#include "gnunet_container_lib.h"





/* LP/MIP problem object */

#if !HAVE_LIBGLPK

#ifndef GLP_PROB_DEFINED
#define GLP_PROB_DEFINED
typedef struct
{
  double _opaque_prob[100];
} glp_prob;
#endif

typedef struct
{                               /* integer optimizer control parameters */
  int msg_lev;                  /* message level (see glp_smcp) */
  int br_tech;                  /* branching technique: */
#define GLP_BR_FFV         1    /* first fractional variable */
#define GLP_BR_LFV         2    /* last fractional variable */
#define GLP_BR_MFV         3    /* most fractional variable */
#define GLP_BR_DTH         4    /* heuristic by Driebeck and Tomlin */
#define GLP_BR_PCH         5    /* hybrid pseudocost heuristic */
  int bt_tech;                  /* backtracking technique: */
#define GLP_BT_DFS         1    /* depth first search */
#define GLP_BT_BFS         2    /* breadth first search */
#define GLP_BT_BLB         3    /* best local bound */
#define GLP_BT_BPH         4    /* best projection heuristic */
  double tol_int;               /* mip.tol_int */
  double tol_obj;               /* mip.tol_obj */
  int tm_lim;                   /* mip.tm_lim (milliseconds) */
  int out_frq;                  /* mip.out_frq (milliseconds) */
  int out_dly;                  /* mip.out_dly (milliseconds) */
  /* mip.cb_func */
  void *cb_info;                /* mip.cb_info */
  int cb_size;                  /* mip.cb_size */
  int pp_tech;                  /* preprocessing technique: */
#define GLP_PP_NONE        0    /* disable preprocessing */
#define GLP_PP_ROOT        1    /* preprocessing only on root level */
#define GLP_PP_ALL         2    /* preprocessing on all levels */
  double mip_gap;               /* relative MIP gap tolerance */
  int mir_cuts;                 /* MIR cuts       (GLP_ON/GLP_OFF) */
  int gmi_cuts;                 /* Gomory's cuts  (GLP_ON/GLP_OFF) */
  int cov_cuts;                 /* cover cuts     (GLP_ON/GLP_OFF) */
  int clq_cuts;                 /* clique cuts    (GLP_ON/GLP_OFF) */
  int presolve;                 /* enable/disable using MIP presolver */
  int binarize;                 /* try to binarize integer variables */
  int fp_heur;                  /* feasibility pump heuristic */
#if 1                           /* 28/V-2010 */
  int alien;                    /* use alien solver */
#endif
  double foo_bar[29];           /* (reserved) */
} glp_iocp;

typedef struct
{                               /* simplex method control parameters */
  int msg_lev;                  /* message level: */
#define GLP_MSG_OFF        0    /* no output */
#define GLP_MSG_ERR        1    /* warning and error messages only */
#define GLP_MSG_ON         2    /* normal output */
#define GLP_MSG_ALL        3    /* full output */
#define GLP_MSG_DBG        4    /* debug output */
  int meth;                     /* simplex method option: */
#define GLP_PRIMAL         1    /* use primal simplex */
#define GLP_DUALP          2    /* use dual; if it fails, use primal */
#define GLP_DUAL           3    /* use dual simplex */
  int pricing;                  /* pricing technique: */
#define GLP_PT_STD      0x11    /* standard (Dantzig rule) */
#define GLP_PT_PSE      0x22    /* projected steepest edge */
  int r_test;                   /* ratio test technique: */
#define GLP_RT_STD      0x11    /* standard (textbook) */
#define GLP_RT_HAR      0x22    /* two-pass Harris' ratio test */
  double tol_bnd;               /* spx.tol_bnd */
  double tol_dj;                /* spx.tol_dj */
  double tol_piv;               /* spx.tol_piv */
  double obj_ll;                /* spx.obj_ll */
  double obj_ul;                /* spx.obj_ul */
  int it_lim;                   /* spx.it_lim */
  int tm_lim;                   /* spx.tm_lim (milliseconds) */
  int out_frq;                  /* spx.out_frq */
  int out_dly;                  /* spx.out_dly (milliseconds) */
  int presolve;                 /* enable/disable using LP presolver */
  double foo_bar[36];           /* (reserved) */
} glp_smcp;

/* optimization direction flag: */
#define GLP_MIN            1    /* minimization */
#define GLP_MAX            2    /* maximization */

/* kind of structural variable: */
#define GLP_CV             1    /* continuous variable */
#define GLP_IV             2    /* integer variable */
#define GLP_BV             3    /* binary variable */

/* type of auxiliary/structural variable: */
#define GLP_FR             1    /* free variable */
#define GLP_LO             2    /* variable with lower bound */
#define GLP_UP             3    /* variable with upper bound */
#define GLP_DB             4    /* double-bounded variable */
#define GLP_FX             5    /* fixed variable */

/* solution indicator: */
#define GLP_SOL            1    /* basic solution */
#define GLP_IPT            2    /* interior-point solution */
#define GLP_MIP            3    /* mixed integer solution */

/* solution status: */
#define GLP_UNDEF          1    /* solution is undefined */
#define GLP_FEAS           2    /* solution is feasible */
#define GLP_INFEAS         3    /* solution is infeasible */
#define GLP_NOFEAS         4    /* no feasible solution exists */
#define GLP_OPT            5    /* solution is optimal */
#define GLP_UNBND          6    /* solution is unbounded */

/* return codes: */
#define GLP_EBADB       0x01    /* invalid basis */
#define GLP_ESING       0x02    /* singular matrix */
#define GLP_ECOND       0x03    /* ill-conditioned matrix */
#define GLP_EBOUND      0x04    /* invalid bounds */
#define GLP_EFAIL       0x05    /* solver failed */
#define GLP_EOBJLL      0x06    /* objective lower limit reached */
#define GLP_EOBJUL      0x07    /* objective upper limit reached */
#define GLP_EITLIM      0x08    /* iteration limit exceeded */
#define GLP_ETMLIM      0x09    /* time limit exceeded */
#define GLP_ENOPFS      0x0A    /* no primal feasible solution */
#define GLP_ENODFS      0x0B    /* no dual feasible solution */
#define GLP_EROOT       0x0C    /* root LP optimum not provided */
#define GLP_ESTOP       0x0D    /* search terminated by application */
#define GLP_EMIPGAP     0x0E    /* relative mip gap tolerance reached */
#define GLP_ENOFEAS     0x0F    /* no primal/dual feasible solution */
#define GLP_ENOCVG      0x10    /* no convergence */
#define GLP_EINSTAB     0x11    /* numerical instability */
#define GLP_EDATA       0x12    /* invalid data */
#define GLP_ERANGE      0x13    /* result out of range */

/* enable/disable flag: */
#define GLP_ON             1    /* enable something */
#define GLP_OFF            0    /* disable something */

#endif

/*
 * Wrappers for GLPK Functions
 */


void *
_lp_create_prob (void)
{
#if HAVE_LIBGLPK
  return glp_create_prob ();
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return NULL;
}

void
_lp_set_obj_dir (glp_prob *P, int dir)
{
#if HAVE_LIBGLPK
  return glp_set_obj_dir (P, dir);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

void
_lp_set_prob_name (glp_prob *P, const char *name)
{
#if HAVE_LIBGLPK
  glp_set_prob_name (P, name);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

int
_lp_add_cols (glp_prob *P, int ncs)
{
#if HAVE_LIBGLPK
  return glp_add_cols (P, ncs);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0;
}

int
_lp_add_rows (glp_prob *P, int nrs)
{
#if HAVE_LIBGLPK
  return glp_add_rows (P, nrs);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0;
}


void
_lp_set_row_bnds (glp_prob *P, int i, int type, double lb, double ub)
{
#if HAVE_LIBGLPK
  glp_set_row_bnds (P, i, type, lb, ub);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

void
_lp_init_smcp (void *parm)
{
#if HAVE_LIBGLPK
  glp_init_smcp (parm);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

void
_lp_set_col_name (glp_prob *P, int j, const char *name)
{
#if HAVE_LIBGLPK
  glp_set_col_name (P, j, name);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

void
_lp_set_col_bnds (glp_prob *P, int j, int type, double lb, double ub)
{
#if HAVE_LIBGLPK
  glp_set_col_bnds (P, j, type, lb, ub);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

void
_lp_set_obj_coef (glp_prob *P, int j, double coef)
{
#if HAVE_LIBGLPK
  glp_set_obj_coef (P, j, coef);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

void
_lp_delete_prob (void *P)
{
#if HAVE_LIBGLPK
  glp_delete_prob (P);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

static int
_lp_simplex (glp_prob *P, void *parm)
{
#if HAVE_LIBGLPK
  return glp_simplex (P, parm);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0;
}

static void
_lp_load_matrix (glp_prob *P, int ne, const int ia[], const int ja[],
                 const double ar[])
{
#if HAVE_LIBGLPK
  glp_load_matrix (P, ne, ia, ja, ar);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

static void
_lp_set_mat_row (glp_prob *P, int i, int len, const int ind[],
                 const double val[])
{
#if HAVE_LIBGLPK
  glp_set_mat_row (P, i, len, ind, val);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

static int
_lp_write_lp (glp_prob *P, const void *parm, const char *fname)
{
#if HAVE_LIBGLPK
  return glp_write_lp (P, parm, fname);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0;
}

static void
_lp_init_iocp (void *parm)
{
#if HAVE_LIBGLPK
  glp_init_iocp (parm);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

static int
_lp_intopt (glp_prob *P, const void *parm)
{
#if HAVE_LIBGLPK
  return glp_intopt (P, parm);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0;
}

static int
_lp_get_status (glp_prob *P)
{
#if HAVE_LIBGLPK
  return glp_get_status (P);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0;
}

static int
_lp_mip_status (glp_prob *P)
{
#if HAVE_LIBGLPK
  return glp_mip_status (P);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0;
}

static void
_lp_set_col_kind (glp_prob *P, int j, int kind)
{
#if HAVE_LIBGLPK
  glp_set_col_kind (P, j, kind);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

static void
_lp_free_env (void)
{
#if HAVE_LIBGLPK
  glp_free_env ();
#else
  // Function not implemented
  GNUNET_break (0);
#endif
}

static const char *
_lp_get_col_name (glp_prob *P, int j)
{
#if HAVE_LIBGLPK
  return glp_get_col_name (P, j);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return NULL;
}

static double
_lp_mip_obj_val (glp_prob *P)
{
#if HAVE_LIBGLPK
  return glp_mip_obj_val (P);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0.0;
}


static double
_lp_get_col_prim (glp_prob *P, int j)
{
#if HAVE_LIBGLPK
  return glp_get_col_prim (P, j);
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0.0;
}

static int
_lp_print_sol (glp_prob *P, const char *fname)
{
#if HAVE_LIBGLPK
#else
  // Function not implemented
  GNUNET_break (0);
#endif
  return 0;
}

/*
 * Dummy functions for CFLAGS
 */

static void
_dummy2 ();
static void
_dummy ()
{
  return;
  _lp_get_col_name (NULL, 0);
  _lp_mip_obj_val (NULL);
  _lp_get_col_prim (NULL, 0);
  _lp_set_mat_row (NULL, 0, 0, NULL, NULL);
  _dummy2 ();
}



static void
_dummy2 ()
{
  ats_modify_problem_state (NULL, 0);
  qm[1].atis_index = 0;
  _dummy ();
  int t = ATS_COST_UPDATED + ATS_MODIFIED + ATS_NEW;

  t++;
}

/*
 * ATS Functions
 */


/**
 * Initialize ATS
 * @param cfg configuration handle to retrieve configuration (to be removed)
 * @return
 */

struct ATS_Handle *
ats_init (double D, double U, double R, int v_b_min, int v_n_min,
          int max_iterations, struct GNUNET_TIME_Relative max_duration,
          GNUNET_TRANSPORT_ATS_AddressNotification address_not,
          GNUNET_TRANSPORT_ATS_ResultCallback res_cb)
{
  struct ATS_Handle *ats = NULL;

  ats = GNUNET_malloc (sizeof (struct ATS_Handle));

  ats->prob = NULL;

  ats->addr_notification = address_not;
  ats->result_cb = res_cb;

  ats->max_iterations = max_iterations;
  ats->max_exec_duration = max_duration;

  ats->D = D;
  ats->U = U;
  ats->R = R;
  ats->v_b_min = v_b_min;
  ats->v_n_min = v_n_min;
  ats->dump_min_peers = 0;
  ats->dump_min_addr = 0;
  ats->dump_overwrite = GNUNET_NO;
  ats->mechanisms = NULL;
  ats->peers = NULL;
  ats->successful_executions = 0;
  ats->invalid_executions = 0;

  return ats;
}


/** solve the bandwidth distribution problem
 * @param max_it maximum iterations
 * @param max_dur maximum duration in ms
 * @param D     weight for diversity
 * @param U weight for utility
 * @param R weight for relativity
 * @param v_b_min minimal bandwidth per peer
 * @param v_n_min minimum number of connections
 * @param stat result struct
 * @return GNUNET_SYSERR if glpk is not available, number of mechanisms used
 */
int
ats_create_problem (struct ATS_Handle *ats, struct ATS_internals *stat,
                    struct ATS_peer *peers, int c_p,
                    struct ATS_mechanism *mechanisms, int c_m)
{
  if ((c_p == 0) || (c_m == 0))
    return GNUNET_SYSERR;

  ats->prob = _lp_create_prob ();

  int c;
  int c_c_ressources = available_ressources;
  int c_q_metrics = available_quality_metrics;

  double M = VERY_BIG_DOUBLE_VALUE;
  double Q[c_q_metrics + 1];

  for (c = 1; c <= c_q_metrics; c++)
  {
    Q[c] = 1;
  }

  if (ats->v_n_min > c_p)
    ats->v_n_min = c_p;
#if VERBOSE_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
              "Creating problem with: %i peers, %i mechanisms, %i resource entries, %i quality metrics \n",
              c_p, c_m, c_c_ressources, c_q_metrics);
#endif

  int size =
      1 + 3 + 10 * c_m + c_p + (c_q_metrics * c_m) + c_q_metrics +
      c_c_ressources * c_m;
  int row_index;
  int array_index = 1;
  int *ia = GNUNET_malloc (size * sizeof (int));
  int *ja = GNUNET_malloc (size * sizeof (int));
  double *ar = GNUNET_malloc (size * sizeof (double));

  _lp_set_prob_name (ats->prob, "gnunet ats bandwidth distribution");
  _lp_set_obj_dir (ats->prob, GLP_MAX);

  /* adding columns */
  char *name;

  _lp_add_cols (ats->prob, 2 * c_m);
  /* adding b_t cols */
  for (c = 1; c <= c_m; c++)
  {
    GNUNET_asprintf (&name, "p_%s_b%i",
                     GNUNET_i2s (&(mechanisms[c].peer->peer)), c);
    _lp_set_col_name (ats->prob, c, name);
    GNUNET_free (name);
    _lp_set_col_bnds (ats->prob, c, GLP_LO, 0.0, 0.0);
    _lp_set_col_kind (ats->prob, c, GLP_CV);
    _lp_set_obj_coef (ats->prob, c, 0);
  }

  /* adding n_t cols */
  for (c = c_m + 1; c <= 2 * c_m; c++)
  {
    GNUNET_asprintf (&name, "p_%s_n%i",
                     GNUNET_i2s (&(mechanisms[c - c_m].peer->peer)), (c - c_m));
    _lp_set_col_name (ats->prob, c, name);
    GNUNET_free (name);
    _lp_set_col_bnds (ats->prob, c, GLP_DB, 0.0, 1.0);
    _lp_set_col_kind (ats->prob, c, GLP_IV);
    _lp_set_obj_coef (ats->prob, c, 0);
  }

  /* feasibility constraints */
  /* Constraint 1: one address per peer */
  row_index = 1;

  _lp_add_rows (ats->prob, c_p);

  for (c = 1; c <= c_p; c++)
  {
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n", row_index);
#endif

    _lp_set_row_bnds (ats->prob, row_index, GLP_FX, 1.0, 1.0);
    struct ATS_mechanism *m = peers[c].m_head;

    while (m != NULL)
    {
      ia[array_index] = row_index;
      ja[array_index] = (c_m + m->col_index);
      ar[array_index] = 1;
#if VERBOSE_ATS
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                  array_index, ia[array_index], ja[array_index],
                  ar[array_index]);
#endif
      array_index++;
      m = m->next;
    }
    row_index++;
  }

  /* Constraint 2: only active mechanism gets bandwidth assigned */
  _lp_add_rows (ats->prob, c_m);
  for (c = 1; c <= c_m; c++)
  {
    /* b_t - n_t * M <= 0 */
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n", row_index);
#endif
    _lp_set_row_bnds (ats->prob, row_index, GLP_UP, 0.0, 0.0);
    ia[array_index] = row_index;
    ja[array_index] = mechanisms[c].col_index;
    ar[array_index] = 1;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
    ia[array_index] = row_index;
    ja[array_index] = c_m + mechanisms[c].col_index;
    ar[array_index] = -M;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
    row_index++;
  }

  /* Constraint 3: minimum bandwidth */
  _lp_add_rows (ats->prob, c_m);

  for (c = 1; c <= c_m; c++)
  {
    /* b_t - n_t * b_min <= 0 */
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n", row_index);
#endif
#if HAVE_LIBGLPK
    _lp_set_row_bnds (ats->prob, row_index, GLP_LO, 0.0, 0.0);
#endif
    ia[array_index] = row_index;
    ja[array_index] = mechanisms[c].col_index;
    ar[array_index] = 1;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
    ia[array_index] = row_index;
    ja[array_index] = c_m + mechanisms[c].col_index;
    ar[array_index] = -ats->v_b_min;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
    row_index++;
  }
  int c2;

  /* Constraint 4: max ressource capacity */
  /* V cr: bt * ct_r <= cr_max
   * */

  _lp_add_rows (ats->prob, available_ressources);

  double ct_max = VERY_BIG_DOUBLE_VALUE;
  double ct_min = 0.0;

  stat->begin_cr = array_index;

  for (c = 0; c < available_ressources; c++)
  {
    ct_max = ressources[c].c_max;
    ct_min = ressources[c].c_min;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",
                row_index, ct_min, ct_max);
#endif
#if HAVE_LIBGLPK
    _lp_set_row_bnds (ats->prob, row_index, GLP_DB, ct_min, ct_max);
#endif
    for (c2 = 1; c2 <= c_m; c2++)
    {
      double value = 0;

      ia[array_index] = row_index;
      ja[array_index] = c2;
      value = mechanisms[c2].ressources[c].c;
      ar[array_index] = value;
#if VERBOSE_ATS
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                  array_index, ia[array_index], ja[array_index],
                  ar[array_index]);
#endif
      array_index++;
    }
    row_index++;
  }
  stat->end_cr = array_index--;

  /* Constraint 5: min number of connections */
  _lp_add_rows (ats->prob, 1);

  for (c = 1; c <= c_m; c++)
  {
    // b_t - n_t * b_min >= 0
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n", row_index);
#endif
    _lp_set_row_bnds (ats->prob, row_index, GLP_LO, ats->v_n_min, 0.0);
    ia[array_index] = row_index;
    ja[array_index] = c_m + mechanisms[c].col_index;
    ar[array_index] = 1;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
  }
  row_index++;

  // optimisation constraints

  // adding columns

  // Constraint 6: optimize for diversity
  int col_d;

  col_d = _lp_add_cols (ats->prob, 1);

  _lp_set_col_name (ats->prob, col_d, "d");
  _lp_set_obj_coef (ats->prob, col_d, ats->D);
  _lp_set_col_bnds (ats->prob, col_d, GLP_LO, 0.0, 0.0);
  _lp_add_rows (ats->prob, 1);
  _lp_set_row_bnds (ats->prob, row_index, GLP_FX, 0.0, 0.0);

  stat->col_d = col_d;
#if VERBOSE_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n", row_index);
#endif
  for (c = 1; c <= c_m; c++)
  {
    ia[array_index] = row_index;
    ja[array_index] = c_m + mechanisms[c].col_index;
    ar[array_index] = 1;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
  }
  ia[array_index] = row_index;
  ja[array_index] = col_d;
  ar[array_index] = -1;
#if VERBOSE_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
              array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
  array_index++;
  row_index++;

  // Constraint 7: optimize for quality
  int col_qm;

  col_qm = _lp_add_cols (ats->prob, c_q_metrics);

  stat->col_qm = col_qm;
  //GNUNET_assert (col_qm == (2*c_mechs) + 3 + 1);
  for (c = 0; c < c_q_metrics; c++)
  {
    GNUNET_asprintf (&name, "Q_%s", qm[c].name);
    _lp_set_col_name (ats->prob, col_qm + c, name);
    _lp_set_col_bnds (ats->prob, col_qm + c, GLP_LO, 0.0, 0.0);
    GNUNET_free (name);
    _lp_set_obj_coef (ats->prob, col_qm + c, Q[c]);
  }

  _lp_add_rows (ats->prob, available_quality_metrics);

  stat->begin_qm = row_index;
  for (c = 1; c <= c_q_metrics; c++)
  {
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n", row_index);
#endif
    double value = 1;

    _lp_set_row_bnds (ats->prob, row_index, GLP_FX, 0.0, 0.0);
    for (c2 = 1; c2 <= c_m; c2++)
    {
      ia[array_index] = row_index;
      ja[array_index] = c2;
      if (qm[c - 1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
      {
        double v0 = 0, v1 = 0, v2 = 0;

        v0 = mechanisms[c2].quality[c - 1].values[0];
        if (v1 < 1)
          v0 = 0.1;
        v1 = mechanisms[c2].quality[c - 1].values[1];
        if (v1 < 1)
          v0 = 0.1;
        v2 = mechanisms[c2].quality[c - 1].values[2];
        if (v1 < 1)
          v0 = 0.1;
        value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
        value = 1;
      }
      if (qm[c - 1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
      {
        double v0 = 0, v1 = 0, v2 = 0;

        v0 = mechanisms[c2].quality[c - 1].values[0];
        if (v0 < 1)
          v0 = 1;
        v1 = mechanisms[c2].quality[c - 1].values[1];
        if (v1 < 1)
          v1 = 1;
        v2 = mechanisms[c2].quality[c - 1].values[2];
        if (v2 < 1)
          v2 = 1;
        value = (v0 + 2 * v1 + 3 * v2) / 6.0;
        if (value >= 1)
          value = (double) 10 / value;
        else
          value = 10;
      }
      ar[array_index] = (mechanisms[c2].peer->f) * value;
#if VERBOSE_ATS
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: %s [%i,%i]=%f \n",
                  array_index, qm[c - 1].name, ia[array_index], ja[array_index],
                  ar[array_index]);
#endif
      array_index++;
    }
    ia[array_index] = row_index;
    ja[array_index] = col_qm + c - 1;
    ar[array_index] = -1;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
    row_index++;
  }
  stat->end_qm = row_index - 1;

  // Constraint 8: optimize bandwidth utility
  int col_u;

  col_u = _lp_add_cols (ats->prob, 1);

  _lp_set_col_name (ats->prob, col_u, "u");
  _lp_set_obj_coef (ats->prob, col_u, ats->U);
  _lp_set_col_bnds (ats->prob, col_u, GLP_LO, 0.0, 0.0);
  _lp_add_rows (ats->prob, 1);
  stat->col_u = col_u;
#if VERBOSE_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n", row_index);
#endif
  _lp_set_row_bnds (ats->prob, row_index, GLP_FX, 0.0, 0.0);
  for (c = 1; c <= c_m; c++)
  {
    ia[array_index] = row_index;
    ja[array_index] = c;
    ar[array_index] = mechanisms[c].peer->f;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
  }
  ia[array_index] = row_index;
  ja[array_index] = col_u;
  ar[array_index] = -1;
#if VERBOSE_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
              array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif

  array_index++;
  row_index++;

  // Constraint 9: optimize relativity
  int col_r;

  col_r = _lp_add_cols (ats->prob, 1);

  _lp_set_col_name (ats->prob, col_r, "r");
  _lp_set_obj_coef (ats->prob, col_r, ats->R);
  _lp_set_col_bnds (ats->prob, col_r, GLP_LO, 0.0, 0.0);
  _lp_add_rows (ats->prob, c_p);

  stat->col_r = col_r;
  for (c = 1; c <= c_p; c++)
  {
    _lp_set_row_bnds (ats->prob, row_index, GLP_LO, 0.0, 0.0);
    struct ATS_mechanism *m = peers[c].m_head;

    while (m != NULL)
    {
      ia[array_index] = row_index;
      ja[array_index] = m->col_index;
      ar[array_index] = 1 / mechanisms[c].peer->f;
#if VERBOSE_ATS
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                  array_index, ia[array_index], ja[array_index],
                  ar[array_index]);
#endif
      array_index++;
      m = m->next;
    }
    ia[array_index] = row_index;
    ja[array_index] = col_r;
    ar[array_index] = -1;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, ia[array_index], ja[array_index], ar[array_index]);
#endif
    array_index++;
    row_index++;
  }

  /* Loading the matrix */
  _lp_load_matrix (ats->prob, array_index - 1, ia, ja, ar);

  stat->c_mechs = c_m;
  stat->c_peers = c_p;
  stat->solution = 0;
  stat->valid = GNUNET_YES;

  /* clean up */
  GNUNET_free (ja);
  GNUNET_free (ia);
  GNUNET_free (ar);

  return GNUNET_OK;
}


void
ats_delete_problem (struct ATS_Handle *ats)
{
#if DEBUG_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Deleting problem\n");
#endif
  int c;

  for (c = 0; c < (ats->internal).c_mechs; c++)
    GNUNET_free_non_null (ats->mechanisms[c].rc);
  if (ats->mechanisms != NULL)
  {
    GNUNET_free (ats->mechanisms);
    ats->mechanisms = NULL;
  }

  if (ats->peers != NULL)
  {
    GNUNET_free (ats->peers);
    ats->peers = NULL;
  }

  if (ats->prob != NULL)
  {
    _lp_delete_prob (ats->prob);
    ats->prob = NULL;
  }

  ats->internal.begin_cr = GNUNET_SYSERR;
  ats->internal.begin_qm = GNUNET_SYSERR;
  ats->internal.c_mechs = 0;
  ats->internal.c_peers = 0;
  ats->internal.end_cr = GNUNET_SYSERR;
  ats->internal.end_qm = GNUNET_SYSERR;
  ats->internal.solution = GNUNET_SYSERR;
  ats->internal.valid = GNUNET_SYSERR;
}

void
ats_modify_problem_state (struct ATS_Handle *ats, enum ATS_problem_state s)
{
  if (ats == NULL)
    return;
  switch (s)
  {
  case ATS_NEW:
    ats->internal.recreate_problem = GNUNET_NO;
    ats->internal.modified_quality = GNUNET_NO;
    ats->internal.modified_resources = GNUNET_NO;
    break;
  case ATS_MODIFIED:
    ats->internal.recreate_problem = GNUNET_YES;
    break;
  case ATS_QUALITY_UPDATED:
    ats->internal.modified_quality = GNUNET_YES;
    break;
  case ATS_COST_UPDATED:
    ats->internal.modified_resources = GNUNET_YES;
    break;
  case ATS_QUALITY_COST_UPDATED:
    ats->internal.modified_resources = GNUNET_YES;
    ats->internal.modified_quality = GNUNET_YES;
    break;
  default:
    return;
  }



}

void
ats_solve_problem (struct ATS_Handle *ats, unsigned int max_it,
                   unsigned int max_dur, unsigned int c_peers,
                   unsigned int c_mechs, struct ATS_internals *stat)
{
  int result = GNUNET_SYSERR;
  int lp_solution = GNUNET_SYSERR;
  int mlp_solution = GNUNET_SYSERR;

  // Solving simplex

  glp_smcp opt_lp;

  _lp_init_smcp (&opt_lp);
#if VERBOSE_ATS
  opt_lp.msg_lev = GLP_MSG_ALL;
#else
  opt_lp.msg_lev = GLP_MSG_OFF;
#endif
  // setting iteration limit
  opt_lp.it_lim = max_it;
  // maximum duration
  opt_lp.tm_lim = max_dur;

  if (ats->internal.recreate_problem == GNUNET_YES)
    opt_lp.presolve = GLP_ON;

  result = _lp_simplex (ats->prob, &opt_lp);
  lp_solution = _lp_get_status (ats->prob);

  if ((result == GLP_ETMLIM) || (result == GLP_EITLIM))
  {
    ats->internal.valid = GNUNET_NO;
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "ATS exceeded time or iteration limit!\n");
    return;
  }

  if (ats_evaluate_results (result, lp_solution, "LP") == GNUNET_YES)
  {
    stat->valid = GNUNET_YES;
  }
  else
  {
    ats->internal.simplex_rerun_required = GNUNET_YES;
    opt_lp.presolve = GLP_ON;
    result = _lp_simplex (ats->prob, &opt_lp);
    lp_solution = _lp_get_status (ats->prob);

    // TODO: Remove if this does not appear until release
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "" "EXECUTED SIMPLEX WITH PRESOLVER! %i \n", lp_solution);

    if (ats_evaluate_results (result, lp_solution, "LP") != GNUNET_YES)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "After execution simplex with presolver: STILL INVALID!\n");
      char *filename;

      GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%llu.mlp",
                       ats->internal.c_peers, ats->internal.c_mechs,
                       GNUNET_TIME_absolute_get ().abs_value);
      _lp_write_lp ((void *) ats->prob, NULL, filename);
      GNUNET_free (filename);
      stat->valid = GNUNET_NO;
      ats->internal.recreate_problem = GNUNET_YES;
      return;
    }
    stat->valid = GNUNET_YES;
  }

  // Solving mlp
  glp_iocp opt_mlp;

  _lp_init_iocp (&opt_mlp);
  // maximum duration
  opt_mlp.tm_lim = max_dur;
  // output level
#if VERBOSE_ATS
  opt_mlp.msg_lev = GLP_MSG_ALL;
#else
  opt_mlp.msg_lev = GLP_MSG_OFF;
#endif

  result = _lp_intopt (ats->prob, &opt_mlp);
  mlp_solution = _lp_mip_status (ats->prob);
  stat->solution = mlp_solution;

  if (ats_evaluate_results (result, mlp_solution, "MLP") == GNUNET_YES)
  {
    stat->valid = GNUNET_YES;
  }
  else
  {
    // TODO: Remove if this does not appear until release
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "MLP solution for %i peers, %i mechs is invalid: %i\n",
                ats->internal.c_peers, ats->internal.c_mechs, mlp_solution);
    stat->valid = GNUNET_NO;
  }

#if VERBOSE_ATS
  if (_lp_get_col_prim (ats->prob, 2 * c_mechs + 1) != 1)
  {
    int c;

    for (c = 1; c <= available_quality_metrics; c++)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n",
                  _lp_get_col_name (ats->prob, 2 * c_mechs + 3 + c),
                  _lp_get_col_prim (ats->prob, 2 * c_mechs + 3 + c));
    }
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n",
                _lp_get_col_name (ats->prob, 2 * c_mechs + 1),
                _lp_get_col_prim (ats->prob, 2 * c_mechs + 1));
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n",
                _lp_get_col_name (ats->prob, 2 * c_mechs + 2),
                _lp_get_col_prim (ats->prob, 2 * c_mechs + 2));
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n",
                _lp_get_col_name (ats->prob, 2 * c_mechs + 3),
                _lp_get_col_prim (ats->prob, 2 * c_mechs + 3));
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "objective value:  %f\n",
                _lp_mip_obj_val (ats->prob));
  }
#endif
}


void
ats_shutdown (struct ATS_Handle *ats)
{
#if DEBUG_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS shutdown\n");
#endif
  ats_delete_problem (ats);
  _lp_free_env ();

  GNUNET_free (ats);
}

void
ats_update_problem_qm (struct ATS_Handle *ats)
{
  int array_index;
  int row_index;
  int c, c2;
  int c_q_metrics = available_quality_metrics;

  int *ja =
      GNUNET_malloc ((1 + ats->internal.c_mechs * 2 + 3 +
                      available_quality_metrics) * sizeof (int));
  double *ar =
      GNUNET_malloc ((1 + ats->internal.c_mechs * 2 + 3 +
                      available_quality_metrics) * sizeof (double));
#if DEBUG_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics\n");
#endif
  row_index = ats->internal.begin_qm;

  for (c = 1; c <= c_q_metrics; c++)
  {
    array_index = 1;
    double value = 1;

#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n", row_index);
#endif
    _lp_set_row_bnds (ats->prob, row_index, GLP_FX, 0.0, 0.0);
    for (c2 = 1; c2 <= ats->internal.c_mechs; c2++)
    {
      ja[array_index] = c2;
      GNUNET_assert (ats->mechanisms[c2].addr != NULL);
      GNUNET_assert (ats->mechanisms[c2].peer != NULL);

      if (qm[c - 1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
      {
        double v0 = 0, v1 = 0, v2 = 0;

        v0 = ats->mechanisms[c2].quality[c - 1].values[0];
        if (v1 < 1)
          v0 = 0.1;
        v1 = ats->mechanisms[c2].quality[c - 1].values[1];
        if (v1 < 1)
          v0 = 0.1;
        v2 = ats->mechanisms[c2].quality[c - 1].values[2];
        if (v1 < 1)
          v0 = 0.1;
        value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
        //value = 1;
      }
      if (qm[c - 1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
      {
        double v0 = 0, v1 = 0, v2 = 0;

        v0 = ats->mechanisms[c2].quality[c - 1].values[0];
        if (v0 < 1)
          v0 = 1;
        v1 = ats->mechanisms[c2].quality[c - 1].values[1];
        if (v1 < 1)
          v1 = 1;
        v2 = ats->mechanisms[c2].quality[c - 1].values[2];
        if (v2 < 1)
          v2 = 1;
        value = (v0 + 2 * v1 + 3 * v2) / 6.0;
        if (value >= 1)
          value = (double) 10 / value;
        else
          value = 10;
      }
      ar[array_index] = (ats->mechanisms[c2].peer->f) * value;
#if VERBOSE_ATS
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: %s [%i,%i]=%f \n",
                  array_index, qm[c - 1].name, row_index, ja[array_index],
                  ar[array_index]);
#endif
      array_index++;
    }
    ja[array_index] = ats->internal.col_qm + c - 1;
    ar[array_index] = -1;

#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                array_index, row_index, ja[array_index], ar[array_index]);
#endif
    _lp_set_mat_row (ats->prob, row_index, array_index, ja, ar);
    array_index = 1;
    row_index++;
  }
  GNUNET_free_non_null (ja);
  GNUNET_free_non_null (ar);

}


void
ats_calculate_bandwidth_distribution (struct ATS_Handle *ats)
{
  struct GNUNET_TIME_Absolute start;
  struct GNUNET_TIME_Relative creation;
  struct GNUNET_TIME_Relative solving;
  int c_m;
  int c_p;
  char *text = "unmodified";

#if FIXME_WACHS
  int dur;

  if (INT_MAX < ats->max_exec_duration.rel_value)
    dur = INT_MAX;
  else
    dur = (int) ats->max_exec_duration.rel_value;
#endif

  ats->internal.simplex_rerun_required = GNUNET_NO;
  start = GNUNET_TIME_absolute_get ();
  if ((ats->internal.recreate_problem == GNUNET_YES) || (ats->prob == NULL) ||
      (ats->internal.valid == GNUNET_NO))
  {
    text = "new";
    ats->internal.recreate_problem = GNUNET_YES;
    ats_delete_problem (ats);
    ats->addr_notification (&ats->peers, &c_p, &ats->mechanisms, &c_m);
#if DEBUG_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Service returned: %i peer, %i mechs\n", c_p, c_m);
#endif
    ats_create_problem (ats, &ats->internal, ats->peers, c_p, ats->mechanisms,
                        c_m);


#if DEBUG_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Peers/Addresses were modified... new problem: %i peer, %i mechs\n",
                ats->internal.c_peers, ats->internal.c_mechs);
#endif
  }

  else if ((ats->internal.recreate_problem == GNUNET_NO) &&
           (ats->internal.modified_resources == GNUNET_YES) &&
           (ats->internal.valid == GNUNET_YES))
  {
    text = "modified resources";
    ats_update_problem_cr (ats);
  }
  else if ((ats->internal.recreate_problem == GNUNET_NO) &&
           (ats->internal.modified_quality == GNUNET_YES) &&
           (ats->internal.valid == GNUNET_YES))
  {
    text = "modified quality";
    ats_update_problem_qm (ats);
    //ats_update_problem_qm_TEST ();
  }
#if DEBUG_ATS
  else
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem is %s\n", text);
#endif

  creation =
      GNUNET_TIME_absolute_get_difference (start, GNUNET_TIME_absolute_get ());
  start = GNUNET_TIME_absolute_get ();

  ats->internal.solution = GLP_UNDEF;
  if (ats->internal.valid == GNUNET_YES)
  {
    ats_solve_problem (ats, ats->max_iterations,
                       ats->max_exec_duration.rel_value, ats->internal.c_peers,
                       ats->internal.c_mechs, &ats->internal);
  }
  solving =
      GNUNET_TIME_absolute_get_difference (start, GNUNET_TIME_absolute_get ());

  if (ats->internal.valid == GNUNET_YES)
  {
    /* Telling about new distribution */
    ats->result_cb ();

    int msg_type = GNUNET_ERROR_TYPE_DEBUG;

#if DEBUG_ATS
    msg_type = GNUNET_ERROR_TYPE_ERROR;
#endif
    GNUNET_log (msg_type,
                "MLP %s: creation time: %llu, execution time: %llu, %i peers, %i mechanisms, simplex rerun: %s, solution %s\n",
                text, creation.rel_value, solving.rel_value,
                ats->internal.c_peers, ats->internal.c_mechs,
                (ats->internal.simplex_rerun_required ==
                 GNUNET_NO) ? "NO" : "YES",
                (ats->internal.solution == 5) ? "OPTIMAL" : "INVALID");
    ats->successful_executions++;
    GNUNET_STATISTICS_set (ats->stats, "# ATS successful executions",
                           ats->successful_executions, GNUNET_NO);

    if ((ats->internal.recreate_problem == GNUNET_YES) || (ats->prob == NULL))
      GNUNET_STATISTICS_set (ats->stats, "ATS state", ATS_NEW, GNUNET_NO);
    else if ((ats->internal.modified_resources == GNUNET_YES) &&
             (ats->internal.modified_quality == GNUNET_NO))
      GNUNET_STATISTICS_set (ats->stats, "ATS state", ATS_COST_UPDATED,
                             GNUNET_NO);
    else if ((ats->internal.modified_resources == GNUNET_NO) &&
             (ats->internal.modified_quality == GNUNET_YES) &&
             (ats->internal.simplex_rerun_required == GNUNET_NO))
      GNUNET_STATISTICS_set (ats->stats, "ATS state", ATS_QUALITY_UPDATED,
                             GNUNET_NO);
    else if ((ats->internal.modified_resources == GNUNET_YES) &&
             (ats->internal.modified_quality == GNUNET_YES) &&
             (ats->internal.simplex_rerun_required == GNUNET_NO))
      GNUNET_STATISTICS_set (ats->stats, "ATS state", ATS_QUALITY_COST_UPDATED,
                             GNUNET_NO);
    else if (ats->internal.simplex_rerun_required == GNUNET_NO)
      GNUNET_STATISTICS_set (ats->stats, "ATS state", ATS_UNMODIFIED,
                             GNUNET_NO);
  }
  else
  {
    if (ats->internal.c_peers != 0)
    {
      ats->invalid_executions++;
      GNUNET_STATISTICS_set (ats->stats, "# ATS invalid executions",
                             ats->invalid_executions, GNUNET_NO);
    }
    else
    {
      GNUNET_STATISTICS_set (ats->stats, "# ATS successful executions",
                             ats->successful_executions, GNUNET_NO);
    }
  }

  GNUNET_STATISTICS_set (ats->stats, "ATS duration",
                         solving.rel_value + creation.rel_value, GNUNET_NO);
  GNUNET_STATISTICS_set (ats->stats, "ATS mechanisms", ats->internal.c_mechs,
                         GNUNET_NO);
  GNUNET_STATISTICS_set (ats->stats, "ATS peers", ats->internal.c_peers,
                         GNUNET_NO);
  GNUNET_STATISTICS_set (ats->stats, "ATS solution", ats->internal.solution,
                         GNUNET_NO);
  GNUNET_STATISTICS_set (ats->stats, "ATS timestamp", start.abs_value,
                         GNUNET_NO);

  if ((ats->save_mlp == GNUNET_YES) &&
      (ats->internal.c_mechs >= ats->dump_min_peers) &&
      (ats->internal.c_mechs >= ats->dump_min_addr))
  {
    char *filename;

    if (ats->dump_overwrite == GNUNET_NO)
    {
      GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%s_%llu.mlp",
                       ats->internal.c_peers, ats->internal.c_mechs, text,
                       GNUNET_TIME_absolute_get ().abs_value);
      _lp_write_lp ((void *) ats->prob, NULL, filename);
    }
    else
    {
      GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i.mlp", ats->internal.c_peers,
                       ats->internal.c_mechs);
      _lp_write_lp ((void *) ats->prob, NULL, filename);
    }
    GNUNET_free (filename);
  }
  if ((ats->save_solution == GNUNET_YES) &&
      (ats->internal.c_mechs >= ats->dump_min_peers) &&
      (ats->internal.c_mechs >= ats->dump_min_addr))
  {
    char *filename;

    if (ats->dump_overwrite == GNUNET_NO)
    {
      GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%s_%llu.sol",
                       ats->internal.c_peers, ats->internal.c_mechs, text,
                       GNUNET_TIME_absolute_get ().abs_value);
      _lp_print_sol (ats->prob, filename);
    }
    else
    {
      GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i.sol", ats->internal.c_peers,
                       ats->internal.c_mechs);
      _lp_print_sol (ats->prob, filename);
    }
    GNUNET_free (filename);
  }

  ats->internal.recreate_problem = GNUNET_NO;
  ats->internal.modified_resources = GNUNET_NO;
  ats->internal.modified_quality = GNUNET_NO;
}

/**
 * Evaluate the result of the last simplex or mlp solving
 * @param result return value returned by the solver
 * @param solution solution state
 * @param problem mlp or lp
 * @return GNUNET_NO if solution is invalid, GNUNET_YES if solution is
 *      valid
 */

int
ats_evaluate_results (int result, int solution, char *problem)
{
  int cont = GNUNET_NO;

#if DEBUG_ATS || VERBOSE_ATS
  int error_kind = GNUNET_ERROR_TYPE_DEBUG;
#endif
#if VERBOSE_ATS
  error_kind = GNUNET_ERROR_TYPE_ERROR;
#endif
  switch (result)
  {
  case GNUNET_SYSERR:          /* GNUNET problem, not GLPK related */
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s, GLPK solving not executed\n", problem);
#endif
    break;
  case GLP_ESTOP:              /* search terminated by application */
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s , Search terminated by application\n", problem);
#endif
    break;
  case GLP_EITLIM:             /* iteration limit exceeded */
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s Iteration limit exceeded\n",
                problem);
#endif
    break;
  case GLP_ETMLIM:             /* time limit exceeded */
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s Time limit exceeded\n", problem);
#endif
    break;
  case GLP_ENOPFS:             /* no primal feasible solution */
  case GLP_ENODFS:             /* no dual feasible solution */
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s No feasible solution\n", problem);
#endif
    break;
  case GLP_EBADB:              /* invalid basis */
  case GLP_ESING:              /* singular matrix */
  case GLP_ECOND:              /* ill-conditioned matrix */
  case GLP_EBOUND:             /* invalid bounds */
  case GLP_EFAIL:              /* solver failed */
  case GLP_EOBJLL:             /* objective lower limit reached */
  case GLP_EOBJUL:             /* objective upper limit reached */
  case GLP_EROOT:              /* root LP optimum not provided */
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s Invalid Input data: %i\n", problem, result);
#endif
    break;
  case 0:
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s Problem has been solved\n", problem);
#endif
    break;
  }

  switch (solution)
  {
  case GLP_UNDEF:
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s solution is undefined\n", problem);
#endif
    break;
  case GLP_OPT:
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s solution is optimal\n", problem);
#endif
    cont = GNUNET_YES;
    break;
  case GLP_FEAS:
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind,
                "%s solution is %s feasible, however, its optimality (or non-optimality) has not been proven\n",
                problem, (0 == strcmp (problem, "LP") ? "" : "integer"));
#endif
    cont = GNUNET_YES;
    break;
  case GLP_NOFEAS:
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s problem has no %sfeasible solution\n", problem,
                (0 == strcmp (problem, "LP") ? "" : "integer "));
#endif
    break;
  case GLP_INFEAS:
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s problem is infeasible \n", problem);
#endif
    break;
  case GLP_UNBND:
#if DEBUG_ATS || VERBOSE_ATS
    GNUNET_log (error_kind, "%s problem is unbounded \n", problem);
#endif
  default:
    break;
  }
  return cont;
}

void
ats_update_problem_cr (struct ATS_Handle *ats)
{
  int array_index;
  int row_index;
  int c, c2;
  double ct_max, ct_min;

  int *ja =
      GNUNET_malloc ((1 + ats->internal.c_mechs * 2 + 3 +
                      available_quality_metrics) * sizeof (int));
  double *ar =
      GNUNET_malloc ((1 + ats->internal.c_mechs * 2 + 3 +
                      available_quality_metrics) * sizeof (double));

  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics\n");
  row_index = ats->internal.begin_cr;
  array_index = 1;

  for (c = 0; c < available_ressources; c++)
  {
    ct_max = ressources[c].c_max;
    ct_min = ressources[c].c_min;
#if VERBOSE_ATS
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",
                row_index, ct_min, ct_max);
#endif
    _lp_set_row_bnds (ats->prob, row_index, GLP_DB, ct_min, ct_max);
    for (c2 = 1; c2 <= ats->internal.c_mechs; c2++)
    {
      double value = 0;

      GNUNET_assert (ats->mechanisms[c2].addr != NULL);
      GNUNET_assert (ats->mechanisms[c2].peer != NULL);

      ja[array_index] = c2;
      value = ats->mechanisms[c2].ressources[c].c;
      ar[array_index] = value;
#if VERBOSE_ATS
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",
                  array_index, row_index, ja[array_index], ar[array_index]);
#endif
      array_index++;
    }
    _lp_set_mat_row (ats->prob, row_index, array_index, ja, ar);
    row_index++;
  }
  GNUNET_free_non_null (ja);
  GNUNET_free_non_null (ar);

}

void
ats_set_logging_options (struct ATS_Handle *ats,
                         struct GNUNET_STATISTICS_Handle *stats,
                         const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  int minimum_addresses;
  int minimum_peers;
  int overwrite_dump;
  int log_solution;
  int log_problem;
  unsigned long long value;

  if (ats == NULL)
    return;
  log_problem =
      GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport", "DUMP_MLP");
  log_solution =
      GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport", "DUMP_SOLUTION");
  overwrite_dump =
      GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport", "DUMP_OVERWRITE");
  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_number (cfg, "transport", "DUMP_MIN_PEERS",
                                             &value))
    minimum_peers = (int) value;
  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_number (cfg, "transport", "DUMP_MIN_ADDRS",
                                             &value))
    minimum_addresses = (int) value;


  ats->stats = stats;
  ats->dump_min_addr = minimum_addresses;
  ats->dump_min_peers = minimum_peers;
  ats->dump_overwrite = overwrite_dump;
  ats->save_mlp = log_problem;
  ats->save_solution = log_solution;
}

#if 0
static void
ats_update_problem_qm_TEST ()
{
  int row_index;
  int c int c2;
  int c_old;
  int changed = 0;

  int old_ja[ats->internal.c_mechs + 2];
  double old_ar[ats->internal.c_mechs + 2];

  int *ja =
      GNUNET_malloc ((1 + ats->internal.c_mechs * 2 + 3 +
                      available_quality_metrics) * sizeof (int));
  double *ar =
      GNUNET_malloc ((1 + ats->internal.c_mechs * 2 + 3 +
                      available_quality_metrics) * sizeof (double));
#if DEBUG_ATS
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
              "Updating problem quality metrics TEST\n");
#endif
  if (ats->internal.begin_qm > 0)
    row_index = ats->internal.begin_qm;
  else
    return;
  for (c = 0; c < available_quality_metrics; c++)
  {
    c_old = _lp_get_mat_row (ats->prob, row_index, old_ja, old_ar);
    _lp_set_row_bnds (ats->prob, row_index, GLP_FX, 0.0, 0.0);
    for (c2 = 1; c2 <= c_old; c2++)
    {
      ja[c2] = old_ja[c2];
      if ((changed < 3) && (c2 > 2) && (old_ar[c2] != -1))
      {
        ar[c2] = old_ar[c2] + 5 - changed;
        changed++;
      }
      else
        ar[c2] = old_ar[c2];
#if VERBOSE_ATS
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "[index]=[%i]: old [%i,%i]=%f  new [%i,%i]=%f\n", c2,
                  row_index, old_ja[c2], old_ar[c2], row_index, ja[c2], ar[c2]);
#endif
    }
    _lp_set_mat_row (ats->prob, row_index, c_old, ja, ar);
    row_index++;
  }
  GNUNET_free_non_null (ja);
  GNUNET_free_non_null (ar);
}
#endif



/* end of transport_ats.c */