summaryrefslogtreecommitdiff
path: root/src/ats/plugin_ats_mlp.c
blob: 544b8d97f1911916beb7db6fab880d0b4aca1113 (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
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
/*
     This file is part of GNUnet.
     Copyright (C) 2011-2014 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     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
     Affero General Public License for more details.
    
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file ats/plugin_ats_mlp.c
 * @brief ats mlp problem solver
 * @author Matthias Wachs
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_ats_service.h"
#include "gnunet_ats_plugin.h"
#include "gnunet-service-ats_addresses.h"
#include "gnunet_statistics_service.h"
#include <float.h>
#include <glpk.h>


#define BIG_M_VALUE (UINT32_MAX) /10
#define BIG_M_STRING "unlimited"

#define MLP_AVERAGING_QUEUE_LENGTH 3

#define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
#define MLP_MAX_ITERATIONS      4096

#define MLP_DEFAULT_D 1.0
#define MLP_DEFAULT_R 1.0
#define MLP_DEFAULT_U 1.0
#define MLP_DEFAULT_QUALITY 1.0
#define MLP_DEFAULT_MIN_CONNECTIONS 4
#define MLP_DEFAULT_PEER_PREFERENCE 1.0

#define MLP_NaN -1
#define MLP_UNDEFINED 0
#define GLP_YES 1.0
#define GLP_NO  0.0

enum MLP_Output_Format
{
  MLP_MPS,
  MLP_CPLEX,
  MLP_GLPK
};


enum QualityMetrics
{
  RQ_QUALITY_METRIC_DELAY = 0,
  RQ_QUALITY_METRIC_DISTANCE = 1,
  RQ_QUALITY_METRIC_COUNT = 2
};


static const char *
print_quality_type (enum QualityMetrics qm)
{
  switch (qm){
  case RQ_QUALITY_METRIC_DELAY:
    return "delay";
  case RQ_QUALITY_METRIC_DISTANCE:
    return "distance";
  default:
    GNUNET_break (0);
    return NULL;
  }
}


struct MLP_Solution
{
  int lp_res;
  int lp_presolv;
  int mip_res;
  int mip_presolv;

  double lp_objective_value;
  double mlp_objective_value;
  double mlp_gap;
  double lp_mlp_gap;

  int p_elements;
  int p_cols;
  int p_rows;

  int n_peers;
  int n_addresses;

};

struct ATS_Peer
{
  struct GNUNET_PeerIdentity id;

  /* Was this peer already added to the current problem? */
  int processed;

  /* constraint 2: 1 address per peer*/
  unsigned int r_c2;

  /* constraint 9: relativity */
  unsigned int r_c9;

  /* Legacy preference value */
  double f;
};

struct MLP_Problem
{
  /**
   * GLPK (MLP) problem object
   */
  glp_prob *prob;

  /* Number of addresses in problem */
  unsigned int num_addresses;
  /* Number of peers in problem */
  unsigned int num_peers;
  /* Number of elements in problem matrix */
  unsigned int num_elements;

  /* Row index constraint 2: */
  unsigned int r_c2;
  /* Row index constraint 4: minimum connections */
  unsigned int r_c4;
  /* Row index constraint 6: maximize diversity */
  unsigned int r_c6;
  /* Row index constraint 8: utilization*/
  unsigned int r_c8;
  /* Row index constraint 9: relativity*/
  unsigned int r_c9;
  /* Row indices quality metrics  */
  int r_q[RQ_QUALITY_METRIC_COUNT];
  /* Row indices ATS network quotas */
  int r_quota[GNUNET_ATS_NetworkTypeCount];

  /* Column index Diversity (D) column */
  int c_d;
  /* Column index Utilization (U) column */
  int c_u;
  /* Column index Proportionality (R) column */
  int c_r;
  /* Column index quality metrics  */
  int c_q[RQ_QUALITY_METRIC_COUNT];

  /* Problem matrix */
  /* Current index */
  unsigned int ci;
  /* Row index array */
  int *ia;
  /* Column index array */
  int *ja;
  /* Column index value */
  double *ar;

};

struct MLP_Variables
{
  /* Big M value for bandwidth capping */
  double BIG_M;

  /* MIP Gap */
  double mip_gap;

  /* LP MIP Gap */
  double lp_mip_gap;

  /* Number of quality metrics @deprecated, use RQ_QUALITY_METRIC_COUNT */
  int m_q;

  /* Number of quality metrics */
  int m_rc;

  /* Quality metric coefficients*/
  double co_Q[RQ_QUALITY_METRIC_COUNT];

  /* Ressource costs coefficients*/
  double co_RC[RQ_QUALITY_METRIC_COUNT];

  /* Diversity coefficient */
  double co_D;

  /* Utility coefficient */
  double co_U;

  /* Relativity coefficient */
  double co_R;

  /* Minimum bandwidth assigned to an address */
  unsigned int b_min;

  /* Minimum number of addresses with bandwidth assigned */
  unsigned int n_min;

  /* Quotas */
  /* Array mapping array index to ATS network */
  int quota_index[GNUNET_ATS_NetworkTypeCount];
  /* Outbound quotas */
  unsigned long long quota_out[GNUNET_ATS_NetworkTypeCount];
  /* Inbound quotas */

  unsigned long long quota_in[GNUNET_ATS_NetworkTypeCount];

  /* ATS ressource costs
   * array with GNUNET_ATS_QualityPropertiesCount elements
   * contains mapping to GNUNET_ATS_Property
   * */
  int rc[RQ_QUALITY_METRIC_COUNT];

};

/**
 * MLP Handle
 */
struct GAS_MLP_Handle
{
  struct GNUNET_ATS_PluginEnvironment *env;

  /**
   * Exclude peer from next result propagation
   */
  const struct GNUNET_PeerIdentity *exclude_peer;

  /**
   * Encapsulation for the MLP problem
   */
  struct MLP_Problem p;

  /**
   * Encapsulation for the MLP problem variables
   */
  struct MLP_Variables pv;

  /**
   * Encapsulation for the MLP solution
   */
  struct MLP_Solution ps;

  /**
   * Bulk lock
   */
  int stat_bulk_lock;

  /**
   * Number of changes while solver was locked
   */
  int stat_bulk_requests;

  /**
   * GLPK LP control parameter
   */
  glp_smcp control_param_lp;

  /**
   * GLPK LP control parameter
   */
  glp_iocp control_param_mlp;

  /**
   * Peers with pending address requests
   */
  struct GNUNET_CONTAINER_MultiPeerMap *requested_peers;

  /**
   * Was the problem updated since last solution
   */
  int stat_mlp_prob_updated;

  /**
   * Has the problem size changed since last solution
   */
  int stat_mlp_prob_changed;

  /**
   * Solve the problem automatically when updates occur?
   * Default: GNUNET_YES
   * Can be disabled for test and measurements
   */
  int opt_mlp_auto_solve;

  /**
   * Write all MILP problems to a MPS file
   */
  int opt_dump_problem_all;

  /**
   * Write all MILP problem solutions to a file
   */
  int opt_dump_solution_all;

  /**
   * Write MILP problems to a MPS file when solver fails
   */
  int opt_dump_problem_on_fail;

  /**
   * Write MILP problem solutions to a file when solver fails
   */
  int opt_dump_solution_on_fail;

  /**
   * solve feasibility only
   */
  int opt_dbg_feasibility_only;

  /**
   * solve autoscale the problem
   */
  int opt_dbg_autoscale_problem;

  /**
   * use the intopt presolver instead of simplex
   */
  int opt_dbg_intopt_presolver;

  /**
   * Print GLPK output
   */
  int opt_dbg_glpk_verbose;

  /**
   * solve autoscale the problem
   */
  int opt_dbg_optimize_relativity;

  /**
   * solve autoscale the problem
   */
  int opt_dbg_optimize_diversity;

  /**
   * solve autoscale the problem
   */
  int opt_dbg_optimize_quality;

  /**
   * solve autoscale the problem
   */
  int opt_dbg_optimize_utility;


  /**
   * Output format
   */
  enum MLP_Output_Format opt_log_format;
};

/**
 * Address specific MLP information
 */
struct MLP_information
{

  /**
   * Bandwidth assigned outbound
   */
  uint32_t b_out;

  /**
   * Bandwidth assigned inbound
   */
  uint32_t b_in;

  /**
   * Address selected
   */
  int n;

  /**
   * bandwidth column index
   */
  signed int c_b;

  /**
   * address usage column
   */
  signed int c_n;

  /* row indexes */

  /**
   * constraint 1: bandwidth capping
   */
  unsigned int r_c1;

  /**
   * constraint 3: minimum bandwidth
   */
  unsigned int r_c3;
};



/**
 *
 * NOTE: Do not modify this documentation. This documentation is based on
 * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
 * use build_txt.sh to generate plaintext output
 *
 *    The MLP solver (mlp) tries to finds an optimal bandwidth assignmentby
 *    optimizing an mixed integer programming problem. The MLP solver uses a
 *    number of constraints to find the best adddress for a peer and an optimal
 *    bandwidth assignment. mlp uses the GNU Linear Programming Kit to solve the
 *    MLP problem.
 *
 *    We defined a constraint system to find an optimal bandwidth assignment.
 *    This constraint system uses as an input data addresses, bandwidth quotas,
 *    preferences and quality values. This constraint system is stored in an
 *    matrix based equotation system.
 *
 *   5 Using GLPK
 *
 *    A (M)LP problem consists of a target function to optimizes, constraints
 *    and rows and columns. FIXME GLP uses three arrays to index the matrix: two
 *    integer arrays storing the row and column indices in the matrix and an
 *    float array to store the coeeficient.
 *
 *    To solve the problem we first find an initial solution for the LP problem
 *    using the LP solver and then find an MLP solution based on this solution
 *    using the MLP solver.
 *
 *    Solving (M)LP problems has the property that finding an initial solution
 *    for the LP problem is computationally expensive and finding the MLP
 *    solution is cheaper. This is especially interesting an existing LP
 *    solution can be reused if only coefficients in the matrix have changed
 *    (addresses updated). Only when the problem size changes (addresses added
 *    or deleted) a new LP solution has to be found.
 *
 *    Intended usage
 *    The mlp solver solves the bandwidth assignment problem only on demand when
 *    an address suggestion is requested. When an address is requested mlp the
 *    solves the mlp problem and if the active address or the bandwidth assigned
 *    changes it calls the callback to addresses. The mlp solver gets notified
 *    about new addresses (adding sessions), removed addresses (address
 *    deletions) and address updates. To benefit from the mlp properties
 *    mentioned in section 5 the solver rembers if since the last solution
 *    addresses were added or deleted (problem size changed, problem has to be
 *    rebuild and solved from sratch) or if addresses were updated and the
 *    existing solution can be reused.
 *
 *     5.1 Input data
 *
 *    The quotas for each network segment are passed by addresses. MLP can be
 *    adapted using configuration settings and uses the following parameters:
 *      * MLP_MAX_DURATION:
 *        Maximum duration for a MLP solution procees (default: 3 sec.)
 *      * MLP_MAX_ITERATIONS:
 *        Maximum number of iterations for a MLP solution process (default:
 *        1024)
 *      * MLP_MIN_CONNECTIONS:
 *        Minimum number of desired connections (default: 4)
 *      * MLP_MIN_BANDWIDTH:
 *        Minimum amount of bandwidth assigned to an address (default: 1024)
 *      * MLP_COEFFICIENT_D:
 *        Diversity coefficient (default: 1.0)
 *      * MLP_COEFFICIENT_R:
 *        Relativity coefficient (default: 1.0)
 *      * MLP_COEFFICIENT_U:
 *        Utilization coefficient (default: 1.0)
 *      * MLP_COEFFICIENT_D:
 *        Diversity coefficient (default: 1.0)
 *      * MLP_COEFFICIENT_QUALITY_DELAY:
 *        Quality delay coefficient (default: 1.0)
 *      * MLP_COEFFICIENT_QUALITY_DISTANCE:
 *        Quality distance coefficient (default: 1.0)
 *      * MLP_COEFFICIENT_QUALITY_DISTANCE:
 *        Quality distance coefficient (default: 1.0)
 *      * MLP_COEFFICIENT_QUALITY_DISTANCE:
 *        Quality distance coefficient (default: 1.0)
 *      * MLP_COEFFICIENT_QUALITY_DISTANCE:
 *        Quality distance coefficient (default: 1.0)
 *
 *     5.2 Data structures used
 *
 *    mlp has for each known peer a struct ATS_Peer containing information about
 *    a specific peer. The address field solver_information contains information
 *    about the mlp properties of this address.
 *
 *     5.3 Initializing
 *
 *    During initialization mlp initializes the GLPK libray used to solve the
 *    MLP problem: it initializes the glpk environment and creates an initial LP
 *    problem. Next it loads the configuration values from the configuration or
 *    uses the default values configured in -addresses_mlp.h. The quotas used
 *    are given by addresses but may have to be adjusted. mlp uses a upper limit
 *    for the bandwidth assigned called BIG M and a minimum amount of bandwidth
 *    an address gets assigned as well as a minium desired number of
 *    connections. If the configured quota is bigger than BIG M, it is reduced
 *    to BIG M. If the configured quota is smaller than MLP_MIN_CONNECTIONS
 *    *MLP_MIN_BANDWIDTH it is increased to this value.
 *
 *     5.4 Shutdown

 */

#define LOG(kind,...) GNUNET_log_from (kind, "ats-mlp",__VA_ARGS__)

/**
 * Print debug output for mlp problem creation
 */
#define DEBUG_MLP_PROBLEM_CREATION GNUNET_NO


/**
 * Intercept GLPK terminal output
 * @param info the mlp handle
 * @param s the string to print
 * @return 0: glpk prints output on terminal, 0 != surpress output
 */
static int
mlp_term_hook (void *info, const char *s)
{
  struct GAS_MLP_Handle *mlp = info;

  if (mlp->opt_dbg_glpk_verbose)
    LOG (GNUNET_ERROR_TYPE_ERROR, "%s", s);
  return 1;
}


/**
 * Reset peers for next problem creation
 *
 * @param cls not used
 * @param key the key
 * @param value ATS_Peer
 * @return #GNUNET_OK
 */
static int
reset_peers (void *cls,
	     const struct GNUNET_PeerIdentity *key,
	     void *value)
 {
   struct ATS_Peer *peer = value;
   peer->processed = GNUNET_NO;
   return GNUNET_OK;
 }

/**
 * Delete the MLP problem and free the constrain matrix
 *
 * @param mlp the MLP handle
 */
static void
mlp_delete_problem (struct GAS_MLP_Handle *mlp)
{
  int c;
  if (mlp == NULL)
    return;
  if (mlp->p.prob != NULL)
  {
    glp_delete_prob(mlp->p.prob);
    mlp->p.prob = NULL;
  }

  /* delete row index */
  if (mlp->p.ia != NULL)
  {
    GNUNET_free (mlp->p.ia);
    mlp->p.ia = NULL;
  }

  /* delete column index */
  if (mlp->p.ja != NULL)
  {
    GNUNET_free (mlp->p.ja);
    mlp->p.ja = NULL;
  }

  /* delete coefficients */
  if (mlp->p.ar != NULL)
  {
    GNUNET_free (mlp->p.ar);
    mlp->p.ar = NULL;
  }
  mlp->p.ci = 0;
  mlp->p.prob = NULL;

  mlp->p.c_d = MLP_UNDEFINED;
  mlp->p.c_r = MLP_UNDEFINED;
  mlp->p.r_c2 = MLP_UNDEFINED;
  mlp->p.r_c4 = MLP_UNDEFINED;
  mlp->p.r_c6 = MLP_UNDEFINED;
  mlp->p.r_c9 = MLP_UNDEFINED;
  for (c = 0; c < RQ_QUALITY_METRIC_COUNT ; c ++)
    mlp->p.r_q[c] = MLP_UNDEFINED;
  for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c ++)
    mlp->p.r_quota[c] = MLP_UNDEFINED;
  mlp->p.ci = MLP_UNDEFINED;


  GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
					 &reset_peers, NULL);
}


/**
 * Translate glpk status error codes to text
 * @param retcode return code
 * @return string with result
 */
static const char *
mlp_status_to_string (int retcode)
{
  switch (retcode) {
    case GLP_UNDEF:
      return "solution is undefined";
    case GLP_FEAS:
      return "solution is feasible";
    case GLP_INFEAS:
      return "solution is infeasible";
    case GLP_NOFEAS:
      return "no feasible solution exists";
    case GLP_OPT:
      return "solution is optimal";
    case GLP_UNBND:
      return "solution is unbounded";
    default:
      GNUNET_break (0);
      return "unknown error";
  }
}


/**
 * Translate glpk solver error codes to text
 * @param retcode return code
 * @return string with result
 */
static const char *
mlp_solve_to_string (int retcode)
{
  switch (retcode) {
    case 0:
      return "ok";
    case GLP_EBADB:
      return "invalid basis";
    case GLP_ESING:
      return "singular matrix";
    case GLP_ECOND:
      return "ill-conditioned matrix";
    case GLP_EBOUND:
      return "invalid bounds";
    case GLP_EFAIL:
      return "solver failed";
    case GLP_EOBJLL:
      return "objective lower limit reached";
    case GLP_EOBJUL:
      return "objective upper limit reached";
    case GLP_EITLIM:
      return "iteration limit exceeded";
    case GLP_ETMLIM:
      return "time limit exceeded";
    case GLP_ENOPFS:
      return "no primal feasible solution";
    case GLP_ENODFS:
      return "no dual feasible solution";
    case GLP_EROOT:
      return "root LP optimum not provided";
    case GLP_ESTOP:
      return "search terminated by application";
    case GLP_EMIPGAP:
      return "relative mip gap tolerance reached";
    case GLP_ENOFEAS:
      return "no dual feasible solution";
    case GLP_ENOCVG:
      return "no convergence";
    case GLP_EINSTAB:
      return "numerical instability";
    case GLP_EDATA:
      return "invalid data";
    case GLP_ERANGE:
      return "result out of range";
    default:
      GNUNET_break (0);
      return "unknown error";
  }
}


struct CountContext
{
  const struct GNUNET_CONTAINER_MultiPeerMap *map;
  int result;
};

static int
mlp_create_problem_count_addresses_it (void *cls,
				       const struct GNUNET_PeerIdentity *key,
				       void *value)
{
  struct CountContext *cctx = cls;

  /* Check if we have to add this peer due to a pending request */
  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
    cctx->result++;
  return GNUNET_OK;
}


static int
mlp_create_problem_count_addresses (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
				    const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
{
  struct CountContext cctx;

  cctx.map = requested_peers;
  cctx.result = 0;
  GNUNET_CONTAINER_multipeermap_iterate (addresses,
           &mlp_create_problem_count_addresses_it, &cctx);
  return cctx.result;
}


static int
mlp_create_problem_count_peers_it (void *cls,
                                   const struct GNUNET_PeerIdentity *key,
                                   void *value)
{
  struct CountContext *cctx = cls;

  /* Check if we have to addresses for the requested peer */
  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (cctx->map, key))
    cctx->result++;
  return GNUNET_OK;
}


static int
mlp_create_problem_count_peers (const struct GNUNET_CONTAINER_MultiPeerMap *requested_peers,
    const struct GNUNET_CONTAINER_MultiPeerMap *addresses)
{
  struct CountContext cctx;

  cctx.map = addresses;
  cctx.result = 0;
  GNUNET_CONTAINER_multipeermap_iterate (requested_peers,
           &mlp_create_problem_count_peers_it, &cctx);
  return cctx.result;
}


/**
 * Updates an existing value in the matrix
 *
 * Extract the row, updates the value and updates the row in the problem
 *
 * @param p the mlp problem
 * @param row the row to create the value in
 * @param col the column to create the value in
 * @param val the value to set
 * @param line calling line for debbuging
 * @return GNUNET_YES value changed, GNUNET_NO value did not change, GNUNET_SYSERR
 * on error
 */
static int
mlp_create_problem_update_value (struct MLP_Problem *p,
                              int row, int col, double val,
                              int line)
{
  int c_cols;
  int c_elems;
  int c1;
  int res;
  int found;
  double *val_array;
  int *ind_array;

  GNUNET_assert (NULL != p->prob);

  /* Get number of columns and prepare data structure */
  c_cols = glp_get_num_cols(p->prob);
  if (0 >= c_cols)
    return GNUNET_SYSERR;

  val_array = GNUNET_malloc ((c_cols +1)* sizeof (double));
  GNUNET_assert (NULL != val_array);
  ind_array = GNUNET_malloc ((c_cols+1) * sizeof (int));
  GNUNET_assert (NULL != ind_array);
  /* Extract the row */

  /* Update the value */
  c_elems = glp_get_mat_row (p->prob, row, ind_array, val_array);
  found = GNUNET_NO;
  for (c1 = 1; c1 < (c_elems+1); c1++)
  {
    if (ind_array[c1] == col)
    {
      found = GNUNET_YES;
      break;
    }
  }
  if (GNUNET_NO == found)
  {
    ind_array[c_elems+1] = col;
    val_array[c_elems+1] = val;
    LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Setting value in [%s : %s] to `%.2f'\n",
        glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
        val);
    glp_set_mat_row (p->prob, row, c_elems+1, ind_array, val_array);
    GNUNET_free (ind_array);
    GNUNET_free (val_array);
    return GNUNET_YES;
  }
  else
  {
    /* Update value */
    LOG (GNUNET_ERROR_TYPE_DEBUG, "[P] Updating value in [%s : %s] from `%.2f' to `%.2f'\n",
        glp_get_row_name (p->prob, row), glp_get_col_name (p->prob, col),
        val_array[c1], val);
    if (val != val_array[c1])
      res = GNUNET_YES;
    else
      res = GNUNET_NO;
    val_array[c1] = val;
    /* Update the row in the matrix */
    glp_set_mat_row (p->prob, row, c_elems, ind_array, val_array);
  }

  GNUNET_free (ind_array);
  GNUNET_free (val_array);
  return res;
}

/**
 * Creates a new value in the matrix
 *
 * Sets the row and column index in the problem array and increments the
 * position field
 *
 * @param p the mlp problem
 * @param row the row to create the value in
 * @param col the column to create the value in
 * @param val the value to set
 * @param line calling line for debbuging
 */
static void
mlp_create_problem_set_value (struct MLP_Problem *p,
                              int row, int col, double val,
                              int line)
{
  if ((p->ci) >= p->num_elements)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Request for index %u bigger than array size of %u\n",
        line, p->ci + 1, p->num_elements);
    GNUNET_break (0);
    return;
  }
  if ((0 == row) || (0 == col))
  {
    GNUNET_break (0);
    LOG (GNUNET_ERROR_TYPE_ERROR, "[P]: Invalid call from line %u: row = %u, col = %u\n",
        line, row, col);
  }
  p->ia[p->ci] = row ;
  p->ja[p->ci] = col;
  p->ar[p->ci] = val;
#if  DEBUG_MLP_PROBLEM_CREATION
  LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: line %u: Set value [%u,%u] in index %u ==  %.2f\n",
      line, p->ia[p->ci], p->ja[p->ci], p->ci, p->ar[p->ci]);
#endif
  p->ci++;
}

static int
mlp_create_problem_create_column (struct MLP_Problem *p, char *name,
    unsigned int type, unsigned int bound, double lb, double ub,
    double coef)
{
  int col = glp_add_cols (p->prob, 1);
  glp_set_col_name (p->prob, col, name);
  glp_set_col_bnds (p->prob, col, bound, lb, ub);
  glp_set_col_kind (p->prob, col, type);
  glp_set_obj_coef (p->prob, col, coef);
#if  DEBUG_MLP_PROBLEM_CREATION
  LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added column [%u] `%s': %.2f\n",
      col, name, coef);
#endif
  return col;
}

static int
mlp_create_problem_create_constraint (struct MLP_Problem *p, char *name,
    unsigned int bound, double lb, double ub)
{
  char * op;
  int row = glp_add_rows (p->prob, 1);
  /* set row name */
  glp_set_row_name (p->prob, row, name);
  /* set row bounds: <= 0 */
  glp_set_row_bnds (p->prob, row, bound, lb, ub);
  switch (bound)
  {
    case GLP_UP:
            GNUNET_asprintf(&op, "-inf <= x <= %.2f", ub);
            break;
    case GLP_DB:
            GNUNET_asprintf(&op, "%.2f <= x <= %.2f", lb, ub);
            break;
    case GLP_FX:
            GNUNET_asprintf(&op, "%.2f == x == %.2f", lb, ub);
            break;
    case GLP_LO:
            GNUNET_asprintf(&op, "%.2f <= x <= inf", lb);
            break;
    default:
            GNUNET_asprintf(&op, "ERROR");
            break;
  }
#if  DEBUG_MLP_PROBLEM_CREATION
    LOG (GNUNET_ERROR_TYPE_DEBUG, "[P]: Added row [%u] `%s': %s\n",
        row, name, op);
#endif
  GNUNET_free (op);
  return row;
}

/**
 * Create the
 * - address columns b and n
 * - address dependent constraint rows c1, c3
 * - peer dependent rows c2 and c9
 * - Set address dependent entries in problem matrix as well
 */
static int
mlp_create_problem_add_address_information (void *cls,
					    const struct GNUNET_PeerIdentity *key,
					    void *value)
{
  struct GAS_MLP_Handle *mlp = cls;
  struct MLP_Problem *p = &mlp->p;
  struct ATS_Address *address = value;
  struct ATS_Peer *peer;
  struct MLP_information *mlpi;
  char *name;
  double cur_bigm;
  uint32_t addr_net;
  uint32_t addr_net_index;
  unsigned long long max_quota;
  int c;

  /* Check if we have to add this peer due to a pending request */
  if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(mlp->requested_peers, key))
    return GNUNET_OK;

  mlpi = address->solver_information;
  if (NULL == mlpi)
  {
      fprintf (stderr, "%s %p\n",GNUNET_i2s (&address->peer), address);
      GNUNET_break (0);
      return GNUNET_OK;
  }

  addr_net = address->properties.scope;
  for (addr_net_index = 0; addr_net_index < GNUNET_ATS_NetworkTypeCount; addr_net_index++)
  {
    if (mlp->pv.quota_index[addr_net_index] == addr_net)
      break;
  }

  if (addr_net_index >= GNUNET_ATS_NetworkTypeCount)
  {
    GNUNET_break (0);
    return GNUNET_OK;
  }

  max_quota = 0;
  for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
  {
    if (mlp->pv.quota_out[c] > max_quota)
      max_quota = mlp->pv.quota_out[c];
    if (mlp->pv.quota_in[c] > max_quota)
      max_quota = mlp->pv.quota_in[c];
  }
  if (max_quota > mlp->pv.BIG_M)
    cur_bigm = (double) mlp->pv.BIG_M;
  else
    cur_bigm = max_quota;


  /* Get peer */
  peer = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, key);
  GNUNET_assert (NULL != peer);
  if (peer->processed == GNUNET_NO)
  {
      /* Add peer dependent constraints */
      /* Add c2) One address active per peer */
      GNUNET_asprintf(&name, "c2_%s", GNUNET_i2s(&address->peer));
      peer->r_c2 = mlp_create_problem_create_constraint (p, name, GLP_FX, 1.0, 1.0);
      GNUNET_free (name);
      if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
      {
        if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
        {
          /* Add c9) Relativity */
          GNUNET_asprintf(&name, "c9_%s", GNUNET_i2s(&address->peer));
          peer->r_c9 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
          GNUNET_free (name);
          /* c9) set coefficient */
          mlp_create_problem_set_value (p, peer->r_c9, p->c_r, -peer->f , __LINE__);
        }
      }
      peer->processed = GNUNET_YES;
  }

  /* Reset addresses' solver information */
  mlpi->c_b = 0;
  mlpi->c_n = 0;
  mlpi->n = 0;
  mlpi->r_c1 = 0;
  mlpi->r_c3 = 0;

  /* Add bandwidth column */
  GNUNET_asprintf (&name, "b_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
  if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
  {
    mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 0.0);
  }
  else
  {
    /* Maximize for bandwidth assignment in feasibility testing */
    mlpi->c_b = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, 1.0);
  }
  GNUNET_free (name);

  /* Add address active column */
  GNUNET_asprintf (&name, "n_%s_%s_%p", GNUNET_i2s (&address->peer), address->plugin, address);
  mlpi->c_n = mlp_create_problem_create_column (p, name, GLP_IV, GLP_DB, 0.0, 1.0, 0.0);
  GNUNET_free (name);

  /* Add address dependent constraints */
  /* Add c1) bandwidth capping: b_t  + (-M) * n_t <= 0 */
  GNUNET_asprintf(&name, "c1_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
  mlpi->r_c1 = mlp_create_problem_create_constraint (p, name, GLP_UP, 0.0, 0.0);
  GNUNET_free (name);
  /*  c1) set b = 1 coefficient */
  mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_b, 1, __LINE__);
  /*  c1) set n = - min (M, quota) coefficient */
  cur_bigm = (double) mlp->pv.quota_out[addr_net_index];
  if (cur_bigm > mlp->pv.BIG_M)
    cur_bigm = (double) mlp->pv.BIG_M;
  mlp_create_problem_set_value (p, mlpi->r_c1, mlpi->c_n, -cur_bigm, __LINE__);

  /* Add constraint c 3) minimum bandwidth
   * b_t + (-n_t * b_min) >= 0
   * */
  GNUNET_asprintf(&name, "c3_%s_%s_%p", GNUNET_i2s(&address->peer), address->plugin, address);
  mlpi->r_c3 = mlp_create_problem_create_constraint (p, name, GLP_LO, 0.0, 0.0);
  GNUNET_free (name);

  /*  c3) set b = 1 coefficient */
  mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_b, 1, __LINE__);
  /*  c3) set n = -b_min coefficient */
  mlp_create_problem_set_value (p, mlpi->r_c3, mlpi->c_n, - ((double )mlp->pv.b_min), __LINE__);


  /* Set coefficient entries in invariant rows */

  /* Feasbility */

  /* c 4) minimum connections */
  mlp_create_problem_set_value (p, p->r_c4, mlpi->c_n, 1, __LINE__);
  /* c 2) 1 address peer peer */
  mlp_create_problem_set_value (p, peer->r_c2, mlpi->c_n, 1, __LINE__);
  /* c 10) obey network specific quotas
   * (1)*b_1 + ... + (1)*b_m <= quota_n
   */
  mlp_create_problem_set_value (p, p->r_quota[addr_net_index], mlpi->c_b, 1, __LINE__);

  /* Optimality */
  if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
  {
    /* c 6) maximize diversity */
    mlp_create_problem_set_value (p, p->r_c6, mlpi->c_n, 1, __LINE__);
    /* c 9) relativity */
    if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
      mlp_create_problem_set_value (p, peer->r_c9, mlpi->c_b, 1, __LINE__);
    /* c 8) utility */
    if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
      mlp_create_problem_set_value (p, p->r_c8, mlpi->c_b, 1, __LINE__);
    /* c 7) Optimize quality */
    /* For all quality metrics, set quality of this address */
    if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
    {
      mlp_create_problem_set_value (p,
                                    p->r_q[RQ_QUALITY_METRIC_DELAY],
                                    mlpi->c_b,
                                    address->norm_delay.norm,
                                    __LINE__);
      mlp_create_problem_set_value (p,
                                    p->r_q[RQ_QUALITY_METRIC_DISTANCE],
                                    mlpi->c_b,
                                    address->norm_distance.norm,
                                    __LINE__);
    }
  }

  return GNUNET_OK;
}


/**
 * Create the invariant columns c4, c6, c10, c8, c7
 */
static void
mlp_create_problem_add_invariant_rows (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
{
  int c;

  /* Feasibility */

  /* Row for c4) minimum connection */
  /* Number of minimum connections is min(|Peers|, n_min) */
  p->r_c4 = mlp_create_problem_create_constraint (p, "c4", GLP_LO, (mlp->pv.n_min > p->num_peers) ? p->num_peers : mlp->pv.n_min, 0.0);

  /* Rows for c 10) Enforce network quotas */
  for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
  {
    char * text;
    GNUNET_asprintf(&text, "c10_quota_ats_%s",
        GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]));
    p->r_quota[c] = mlp_create_problem_create_constraint (p, text, GLP_DB, 0.0, mlp->pv.quota_out[c]);
    GNUNET_free (text);
  }

  /* Optimality */
  if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
  {
    char *name;
    /* Add row for c6) Maximize for diversity */
    if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
    {
      p->r_c6 = mlp_create_problem_create_constraint (p, "c6", GLP_FX, 0.0, 0.0);
      /* Set c6 ) Setting -D */
      mlp_create_problem_set_value (p, p->r_c6, p->c_d, -1, __LINE__);
    }

    /* Adding rows for c 8) Maximize utility */
    if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
    {
      p->r_c8 = mlp_create_problem_create_constraint (p, "c8", GLP_FX, 0.0, 0.0);
      /* -u */
      mlp_create_problem_set_value (p, p->r_c8, p->c_u, -1, __LINE__);
    }

    /* For all quality metrics:
     * c 7) Maximize quality, austerity */
    if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
    {
      for (c = 0; c < mlp->pv.m_q; c++)
      {
        GNUNET_asprintf (&name,
                         "c7_q%i_%s", c,
                         print_quality_type (c));
        p->r_q[c] = mlp_create_problem_create_constraint (p, name, GLP_FX, 0.0, 0.0);
        GNUNET_free (name);
        mlp_create_problem_set_value (p,
                                      p->r_q[c],
                                      p->c_q[c], -1, __LINE__);
      }
    }
  }
}


/**
 * Create the invariant columns d, u, r, q0 ... qm
 */
static void
mlp_create_problem_add_invariant_columns (struct GAS_MLP_Handle *mlp, struct MLP_Problem *p)
{
  if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
  {
    char *name;
    int c;

    /* Diversity d column  */
    if (GNUNET_YES == mlp->opt_dbg_optimize_diversity)
      p->c_d = mlp_create_problem_create_column (p, "d", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_D);

    /* Utilization u column  */
    if (GNUNET_YES == mlp->opt_dbg_optimize_utility)
      p->c_u = mlp_create_problem_create_column (p, "u", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_U);

    /* Relativity r column  */
    if (GNUNET_YES == mlp->opt_dbg_optimize_relativity)
      p->c_r = mlp_create_problem_create_column (p, "r", GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_R);

    /* Quality metric columns */
    if (GNUNET_YES == mlp->opt_dbg_optimize_quality)
    {
      for (c = 0; c < mlp->pv.m_q; c++)
      {
        GNUNET_asprintf (&name, "q_%u", c);
        p->c_q[c] = mlp_create_problem_create_column (p, name, GLP_CV, GLP_LO, 0.0, 0.0, mlp->pv.co_Q[c]);
        GNUNET_free (name);
      }
    }
  }
}


/**
 * Create the MLP problem
 *
 * @param mlp the MLP handle
 * @return #GNUNET_OK or #GNUNET_SYSERR
 */
static int
mlp_create_problem (struct GAS_MLP_Handle *mlp)
{
  struct MLP_Problem *p = &mlp->p;
  int res = GNUNET_OK;

  GNUNET_assert (p->prob == NULL);
  GNUNET_assert (p->ia == NULL);
  GNUNET_assert (p->ja == NULL);
  GNUNET_assert (p->ar == NULL);
  /* Reset MLP problem struct */

  /* create the glpk problem */
  p->prob = glp_create_prob ();
  GNUNET_assert (NULL != p->prob);
  p->num_peers = mlp_create_problem_count_peers (mlp->requested_peers, mlp->env->addresses);
  p->num_addresses = mlp_create_problem_count_addresses (mlp->requested_peers,
                                                         mlp->env->addresses);

  /* Create problem matrix: 10 * #addresses + #q * #addresses + #q, + #peer + 2 + 1 */
  p->num_elements = (10 * p->num_addresses + mlp->pv.m_q * p->num_addresses +
      mlp->pv.m_q + p->num_peers + 2 + 1);
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Rebuilding problem for %u peer(s) and %u addresse(s) and %u quality metrics == %u elements\n",
       p->num_peers,
       p->num_addresses,
       mlp->pv.m_q,
       p->num_elements);

  /* Set a problem name */
  glp_set_prob_name (p->prob, "GNUnet ATS bandwidth distribution");
  /* Set optimization direction to maximize */
  glp_set_obj_dir (p->prob, GLP_MAX);

  /* Create problem matrix */
  /* last +1 caused by glpk index starting with one: [1..elements]*/
  p->ci = 1;
  /* row index */
  p->ia = GNUNET_malloc (p->num_elements * sizeof (int));
  /* column index */
  p->ja = GNUNET_malloc (p->num_elements * sizeof (int));
  /* coefficient */
  p->ar = GNUNET_malloc (p->num_elements * sizeof (double));

  if ((NULL == p->ia) || (NULL == p->ja) || (NULL == p->ar))
  {
      LOG (GNUNET_ERROR_TYPE_ERROR, _("Problem size too large, cannot allocate memory!\n"));
      return GNUNET_SYSERR;
  }

  /* Adding invariant columns */
  mlp_create_problem_add_invariant_columns (mlp, p);

  /* Adding address independent constraint rows */
  mlp_create_problem_add_invariant_rows (mlp, p);

  /* Adding address dependent columns constraint rows */
  GNUNET_CONTAINER_multipeermap_iterate (mlp->env->addresses,
					 &mlp_create_problem_add_address_information,
					 mlp);

  /* Load the matrix */
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Loading matrix\n");
  glp_load_matrix(p->prob, (p->ci)-1, p->ia, p->ja, p->ar);
  if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
  {
    glp_scale_prob (p->prob, GLP_SF_AUTO);
  }

  return res;
}


/**
 * Solves the LP problem
 *
 * @param mlp the MLP Handle
 * @return #GNUNET_OK if could be solved, #GNUNET_SYSERR on failure
 */
static int
mlp_solve_lp_problem (struct GAS_MLP_Handle *mlp)
{
  int res = 0;
  int res_status = 0;
  res = glp_simplex(mlp->p.prob, &mlp->control_param_lp);
  if (0 == res)
    LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem: %s\n",
        mlp_solve_to_string (res));
  else
    LOG(GNUNET_ERROR_TYPE_DEBUG, "Solving LP problem failed: %s\n",
        mlp_solve_to_string (res));

  /* Analyze problem status  */
  res_status = glp_get_status (mlp->p.prob);
  switch (res_status) {
    case GLP_OPT: /* solution is optimal */
      LOG (GNUNET_ERROR_TYPE_INFO,
          "Solving LP problem: %s, %s\n",
          mlp_solve_to_string(res),
          mlp_status_to_string(res_status));
      return GNUNET_OK;
    default:
      LOG (GNUNET_ERROR_TYPE_ERROR,
          "Solving LP problem failed: %s %s\n",
          mlp_solve_to_string(res),
          mlp_status_to_string(res_status));
      return GNUNET_SYSERR;
  }
}


/**
 * Propagates the results when MLP problem was solved
 *
 * @param cls the MLP handle
 * @param key the peer identity
 * @param value the address
 * @return #GNUNET_OK to continue
 */
static int
mlp_propagate_results (void *cls,
		       const struct GNUNET_PeerIdentity *key,
		       void *value)
{
  struct GAS_MLP_Handle *mlp = cls;
  struct ATS_Address *address;
  struct MLP_information *mlpi;
  double mlp_bw_in = MLP_NaN;
  double mlp_bw_out = MLP_NaN;
  double mlp_use = MLP_NaN;

  /* Check if we have to add this peer due to a pending request */
  if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mlp->requested_peers,
							   key))
  {
    return GNUNET_OK;
  }
  address = value;
  GNUNET_assert (address->solver_information != NULL);
  mlpi = address->solver_information;

  mlp_bw_in = glp_mip_col_val(mlp->p.prob, mlpi->c_b);/* FIXME */
  if (mlp_bw_in > (double) UINT32_MAX)
  {
      LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
      mlp_bw_in = (double) UINT32_MAX;
  }
  mlp_bw_out = glp_mip_col_val(mlp->p.prob, mlpi->c_b);
  if (mlp_bw_out > (double) UINT32_MAX)
  {
      LOG (GNUNET_ERROR_TYPE_DEBUG, "Overflow in assigned bandwidth, reducing ...\n" );
      mlp_bw_out = (double) UINT32_MAX;
  }
  mlp_use = glp_mip_col_val(mlp->p.prob, mlpi->c_n);

  /*
   * Debug: solution
   * LOG (GNUNET_ERROR_TYPE_INFO, "MLP result address: `%s' `%s' length %u session %u, mlp use %.3f\n",
   *    GNUNET_i2s(&address->peer), address->plugin,
   *    address->addr_len, address->session_id);
   */

  if (GLP_YES == mlp_use)
  {
    /* This address was selected by the solver to be used */
    mlpi->n = GNUNET_YES;
    if (GNUNET_NO == address->active)
    {
            /* Address was not used before, enabling address */
      LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : enabling address\n",
          (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
      address->active = GNUNET_YES;
      address->assigned_bw_in = mlp_bw_in;
      mlpi->b_in = mlp_bw_in;
      address->assigned_bw_out = mlp_bw_out;
      mlpi->b_out = mlp_bw_out;
      if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
        mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
      return GNUNET_OK;
    }
    else if (GNUNET_YES == address->active)
    {
      /* Address was used before, check for bandwidth change */
      if ((mlp_bw_out != address->assigned_bw_out) ||
              (mlp_bw_in != address->assigned_bw_in))
      {
          LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : bandwidth changed\n",
              (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
          address->assigned_bw_in = mlp_bw_in;
          mlpi->b_in = mlp_bw_in;
          address->assigned_bw_out = mlp_bw_out;
          mlpi->b_out = mlp_bw_out;
          if ((NULL == mlp->exclude_peer) || (0 != memcmp (&address->peer, mlp->exclude_peer, sizeof (address->peer))))
            mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
          return GNUNET_OK;
      }
    }
    else
      GNUNET_break (0);
  }
  else if (GLP_NO == mlp_use)
  {
    /* This address was selected by the solver to be not used */
    mlpi->n = GNUNET_NO;
    if (GNUNET_NO == address->active)
    {
      /* Address was not used before, nothing to do */
      LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : no change\n",
          (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
      return GNUNET_OK;
    }
    else if (GNUNET_YES == address->active)
    {
    /* Address was used before, disabling address */
    LOG (GNUNET_ERROR_TYPE_DEBUG, "%s %.2f : disabling address\n",
        (1 == mlp_use) ? "[x]": "[ ]", mlp_bw_out);
      address->active = GNUNET_NO;
      /* Set bandwidth to 0 */
      address->assigned_bw_in = 0;
      mlpi->b_in = 0;
      address->assigned_bw_out = 0;
      mlpi->b_out = 0;
      return GNUNET_OK;
    }
    else
      GNUNET_break (0);
  }
  else
    GNUNET_break (0);

  return GNUNET_OK;
}


static void
notify (struct GAS_MLP_Handle *mlp,
	enum GAS_Solver_Operation op,
	enum GAS_Solver_Status stat,
	enum GAS_Solver_Additional_Information add)
{
  mlp->env->info_cb (mlp->env->cls,
                     op,
                     stat,
                     add);
}


static void
mlp_branch_and_cut_cb (glp_tree *tree, void *info)
{
  struct GAS_MLP_Handle *mlp = info;
  double mlp_obj = 0;

  switch (glp_ios_reason (tree))
  {
    case GLP_ISELECT:
        /* Do nothing here */
      break;
    case GLP_IPREPRO:
        /* Do nothing here */
      break;
    case GLP_IROWGEN:
        /* Do nothing here */
      break;
    case GLP_IHEUR:
        /* Do nothing here */
      break;
    case GLP_ICUTGEN:
        /* Do nothing here */
      break;
    case GLP_IBRANCH:
        /* Do nothing here */
      break;
    case GLP_IBINGO:
        /* A better solution was found  */
      mlp->ps.mlp_gap = glp_ios_mip_gap (tree);
      mlp_obj = glp_mip_obj_val (mlp->p.prob);
      mlp->ps.lp_mlp_gap = (abs(mlp_obj - mlp->ps.lp_objective_value)) / (abs(mlp_obj) + DBL_EPSILON);

      LOG (GNUNET_ERROR_TYPE_INFO,
          "Found better integer solution, current gaps: %.3f <= %.3f, %.3f <= %.3f\n",
          mlp->ps.mlp_gap, mlp->pv.mip_gap,
          mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);

      if (mlp->ps.mlp_gap <= mlp->pv.mip_gap)
      {
        LOG (GNUNET_ERROR_TYPE_INFO,
          "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
          mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
        glp_ios_terminate (tree);
      }

      if (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap)
      {
        LOG (GNUNET_ERROR_TYPE_INFO,
          "Current LP/MLP gap of %.3f smaller than tolerated gap of %.3f, terminating search\n",
          mlp->ps.lp_mlp_gap, mlp->pv.lp_mip_gap);
        glp_ios_terminate (tree);
      }

      break;
    default:
      break;
  }
  //GNUNET_break (0);
}


/**
 * Solves the MLP problem
 *
 * @param solver the MLP Handle
 * @return #GNUNET_OK if could be solved, #GNUNET_SYSERR on failure
 */
static int
GAS_mlp_solve_problem (void *solver)
{
  struct GAS_MLP_Handle *mlp = solver;
  char *filename;
  int res_lp = 0;
  int mip_res = 0;
  int mip_status = 0;

  struct GNUNET_TIME_Absolute start_total;
  struct GNUNET_TIME_Absolute start_cur_op;
  struct GNUNET_TIME_Relative dur_total;
  struct GNUNET_TIME_Relative dur_setup;
  struct GNUNET_TIME_Relative dur_lp;
  struct GNUNET_TIME_Relative dur_mlp;

  GNUNET_assert(NULL != solver);
  dur_lp = GNUNET_TIME_UNIT_ZERO; 

  if (GNUNET_YES == mlp->stat_bulk_lock)
    {
      mlp->stat_bulk_requests++;
      return GNUNET_NO;
    }
  notify(mlp, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS,
      (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
  start_total = GNUNET_TIME_absolute_get();

  if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->requested_peers))
    {
      notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
      return GNUNET_OK; /* No pending requests */
    }
  if (0 == GNUNET_CONTAINER_multipeermap_size(mlp->env->addresses))
    {
      notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
      return GNUNET_OK; /* No addresses available */
    }

  if ((GNUNET_NO == mlp->stat_mlp_prob_changed)
      && (GNUNET_NO == mlp->stat_mlp_prob_updated))
    {
      LOG(GNUNET_ERROR_TYPE_DEBUG, "No changes to problem\n");
      notify(mlp, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_NONE);
      return GNUNET_OK;
    }
  if (GNUNET_YES == mlp->stat_mlp_prob_changed)
  {
    LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem size changed, rebuilding\n");
    notify(mlp, GAS_OP_SOLVE_SETUP_START, GAS_STAT_SUCCESS, GAS_INFO_FULL);
    mlp_delete_problem (mlp);
    if (GNUNET_SYSERR == mlp_create_problem (mlp))
      {
        notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_FAIL, GAS_INFO_FULL);
        return GNUNET_SYSERR;
      }
    notify(mlp, GAS_OP_SOLVE_SETUP_STOP, GAS_STAT_SUCCESS, GAS_INFO_FULL);
    if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
    {
    mlp->control_param_lp.presolve = GLP_YES; /* LP presolver, we need lp solution */
    mlp->control_param_mlp.presolve = GNUNET_NO; /* No presolver, we have LP solution */
    }
    else
    {
      mlp->control_param_lp.presolve = GNUNET_NO; /* LP presolver, we need lp solution */
      mlp->control_param_mlp.presolve = GLP_YES; /* No presolver, we have LP solution */
      dur_lp = GNUNET_TIME_UNIT_ZERO;
    }
  }
  else
  {
    LOG(GNUNET_ERROR_TYPE_DEBUG, "Problem was updated, resolving\n");
  }

  /* Reset solution info */
  mlp->ps.lp_objective_value = 0.0;
  mlp->ps.mlp_gap = 1.0;
  mlp->ps.mlp_objective_value = 0.0;
  mlp->ps.lp_mlp_gap = 0.0;

  dur_setup = GNUNET_TIME_absolute_get_duration (start_total);

  /* Run LP solver */
  if (GNUNET_NO == mlp->opt_dbg_intopt_presolver)
  {
    notify(mlp, GAS_OP_SOLVE_MLP_LP_START, GAS_STAT_SUCCESS,
        (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
    LOG(GNUNET_ERROR_TYPE_DEBUG,
        "Running LP solver %s\n",
        (GLP_YES == mlp->control_param_lp.presolve)? "with presolver": "without presolver");
    start_cur_op = GNUNET_TIME_absolute_get();

    /* Solve LP */
    /* Only for debugging:
     * Always use LP presolver:
     * mlp->control_param_lp.presolve = GLP_YES; */
    res_lp = mlp_solve_lp_problem(mlp);
    if (GNUNET_OK == res_lp)
    {
        mlp->ps.lp_objective_value = glp_get_obj_val (mlp->p.prob);
        LOG (GNUNET_ERROR_TYPE_DEBUG,
             "LP solution was: %.3f\n",
             mlp->ps.lp_objective_value);
    }

    dur_lp = GNUNET_TIME_absolute_get_duration (start_cur_op);
    notify(mlp, GAS_OP_SOLVE_MLP_LP_STOP,
        (GNUNET_OK == res_lp) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
        (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
  }

  if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
    res_lp = GNUNET_OK;

  /* Run MLP solver */
  if ((GNUNET_OK == res_lp) || (GNUNET_YES == mlp->opt_dbg_intopt_presolver))
  {
    LOG(GNUNET_ERROR_TYPE_DEBUG, "Running MLP solver \n");
    notify(mlp, GAS_OP_SOLVE_MLP_MLP_START, GAS_STAT_SUCCESS,
        (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
    start_cur_op = GNUNET_TIME_absolute_get();

    /* Solve MIP */

    /* Only for debugging, always use LP presolver */
    if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
      mlp->control_param_mlp.presolve = GNUNET_YES;

    mip_res = glp_intopt (mlp->p.prob, &mlp->control_param_mlp);
    switch (mip_res)
    {
        case 0:
          /* Successful */
          LOG (GNUNET_ERROR_TYPE_INFO,
               "Solving MLP problem: %s\n",
               mlp_solve_to_string (mip_res));
          break;
        case GLP_ETMLIM: /* Time limit reached */
        case GLP_EMIPGAP: /* MIP gap tolerance limit reached */
        case GLP_ESTOP: /* Solver was instructed to stop*/
          /* Semi-successful */
          LOG (GNUNET_ERROR_TYPE_INFO,
               "Solving MLP problem solution was interupted: %s\n",
               mlp_solve_to_string (mip_res));
          break;
        case GLP_EBOUND:
        case GLP_EROOT:
        case GLP_ENOPFS:
        case GLP_ENODFS:
        case GLP_EFAIL:
        default:
         /* Fail */
          LOG (GNUNET_ERROR_TYPE_INFO,
              "Solving MLP problem failed: %s\n",
              mlp_solve_to_string (mip_res));
        break;
    }

    /* Analyze problem status  */
    mip_status = glp_mip_status(mlp->p.prob);
    switch (mip_status)
    {
      case GLP_OPT: /* solution is optimal */
        LOG (GNUNET_ERROR_TYPE_WARNING,
            "Solution of MLP problem is optimal: %s, %s\n",
            mlp_solve_to_string (mip_res),
            mlp_status_to_string (mip_status));
        mip_res = GNUNET_OK;
        break;
      case GLP_FEAS: /* solution is feasible but not proven optimal */

        if ( (mlp->ps.mlp_gap <= mlp->pv.mip_gap) ||
             (mlp->ps.lp_mlp_gap <= mlp->pv.lp_mip_gap) )
        {
          LOG (GNUNET_ERROR_TYPE_INFO,
                 "Solution of MLP problem is feasible and solution within gap constraints: %s, %s\n",
                 mlp_solve_to_string (mip_res),
                 mlp_status_to_string (mip_status));
          mip_res = GNUNET_OK;
        }
        else
        {
          LOG (GNUNET_ERROR_TYPE_WARNING,
               "Solution of MLP problem is feasible but solution not within gap constraints: %s, %s\n",
               mlp_solve_to_string (mip_res),
               mlp_status_to_string (mip_status));
          mip_res = GNUNET_SYSERR;
        }
        break;
      case GLP_UNDEF: /* Solution undefined */
      case GLP_NOFEAS: /* No feasible solution */
      default:
        LOG (GNUNET_ERROR_TYPE_ERROR,
            "Solving MLP problem failed: %s %s\n",
            mlp_solve_to_string (mip_res),
            mlp_status_to_string (mip_status));
        mip_res = GNUNET_SYSERR;
        break;
    }

    dur_mlp = GNUNET_TIME_absolute_get_duration (start_cur_op);
    dur_total = GNUNET_TIME_absolute_get_duration (start_total);

    notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP,
        (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
        (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
  }
  else
  {
    /* Do not execute mip solver since lp solution is invalid */
    dur_mlp = GNUNET_TIME_UNIT_ZERO;
    dur_total = GNUNET_TIME_absolute_get_duration (start_total);

    notify(mlp, GAS_OP_SOLVE_MLP_MLP_STOP, GAS_STAT_FAIL,
        (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);
    mip_res = GNUNET_SYSERR;
  }

  /* Notify about end */
  notify(mlp, GAS_OP_SOLVE_STOP,
      ((GNUNET_OK == mip_res) && (GNUNET_OK == mip_res)) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
      (GNUNET_YES == mlp->stat_mlp_prob_changed) ? GAS_INFO_FULL : GAS_INFO_UPDATED);

  LOG (GNUNET_ERROR_TYPE_DEBUG,
      "Execution time for %s solve: (total/setup/lp/mlp) : %llu %llu %llu %llu\n",
      (GNUNET_YES == mlp->stat_mlp_prob_changed) ? "full" : "updated",
      (unsigned long long) dur_total.rel_value_us,
      (unsigned long long) dur_setup.rel_value_us,
      (unsigned long long) dur_lp.rel_value_us,
      (unsigned long long) dur_mlp.rel_value_us);

  /* Save stats */
  mlp->ps.lp_res = res_lp;
  mlp->ps.mip_res = mip_res;
  mlp->ps.lp_presolv = mlp->control_param_lp.presolve;
  mlp->ps.mip_presolv = mlp->control_param_mlp.presolve;
  mlp->ps.p_cols = glp_get_num_cols(mlp->p.prob);
  mlp->ps.p_rows = glp_get_num_rows(mlp->p.prob);
  mlp->ps.p_elements = mlp->p.num_elements;

  /* Propagate result*/
  notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
      (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
      GAS_INFO_NONE);
  if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
    {
      GNUNET_CONTAINER_multipeermap_iterate(mlp->env->addresses,
          &mlp_propagate_results, mlp);
    }
  notify (mlp, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
      (GNUNET_OK == res_lp) && (GNUNET_OK == mip_res) ? GAS_STAT_SUCCESS : GAS_STAT_FAIL,
      GAS_INFO_NONE);

  struct GNUNET_TIME_Absolute time = GNUNET_TIME_absolute_get();
  if ( (GNUNET_YES == mlp->opt_dump_problem_all) ||
      (mlp->opt_dump_problem_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
    {
      /* Write problem to disk */
      switch (mlp->opt_log_format) {
        case MLP_CPLEX:
          GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.cplex", mlp->p.num_peers,
              mlp->p.num_addresses, time.abs_value_us);
          glp_write_lp (mlp->p.prob, NULL, filename);
          break;
        case MLP_GLPK:
          GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.glpk", mlp->p.num_peers,
              mlp->p.num_addresses, time.abs_value_us);
          glp_write_prob (mlp->p.prob, 0, filename);
          break;
        case MLP_MPS:
          GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.mps", mlp->p.num_peers,
              mlp->p.num_addresses, time.abs_value_us);
          glp_write_mps (mlp->p.prob, GLP_MPS_FILE, NULL, filename);
          break;
        default:
          break;
      }
      LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped problem to file: `%s' \n", filename);
      GNUNET_free(filename);
    }
  if ( (mlp->opt_dump_solution_all) ||
      (mlp->opt_dump_solution_on_fail && ((GNUNET_OK != res_lp) || (GNUNET_OK != mip_res))) )
  {
    /* Write solution to disk */
    GNUNET_asprintf(&filename, "problem_p_%u_a%u_%llu.sol", mlp->p.num_peers,
        mlp->p.num_addresses, time.abs_value_us);
    glp_print_mip(mlp->p.prob, filename);
    LOG(GNUNET_ERROR_TYPE_ERROR, "Dumped solution to file: `%s' \n", filename);
    GNUNET_free(filename);
  }

  /* Reset change and update marker */
  mlp->control_param_lp.presolve = GLP_NO;
  mlp->stat_mlp_prob_updated = GNUNET_NO;
  mlp->stat_mlp_prob_changed = GNUNET_NO;

  if ((GNUNET_OK == res_lp) && (GNUNET_OK == mip_res))
    return GNUNET_OK;
  else
    return GNUNET_SYSERR;
}

/**
 * Add a single address to the solve
 *
 * @param solver the solver Handle
 * @param address the address to add
 * @param network network type of this address
 */
static void
GAS_mlp_address_add (void *solver,
                    struct ATS_Address *address,
                    uint32_t network)
{
  struct GAS_MLP_Handle *mlp = solver;

  if (GNUNET_ATS_NetworkTypeCount <= network)
  {
    GNUNET_break (0);
    return;
  }

  if (NULL == address->solver_information)
  {
      address->solver_information = GNUNET_new (struct MLP_information);
  }
  else
      LOG (GNUNET_ERROR_TYPE_ERROR,
	   _("Adding address for peer `%s' multiple times\n"),
	   GNUNET_i2s(&address->peer));

  /* Is this peer included in the problem? */
  if (NULL ==
      GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
                                         &address->peer))
  {
    /* FIXME: should this be an error? */
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Adding address for peer `%s' without address request\n",
         GNUNET_i2s(&address->peer));
    return;
  }

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Adding address for peer `%s' with address request \n",
       GNUNET_i2s(&address->peer));
  /* Problem size changed: new address for peer with pending request */
  mlp->stat_mlp_prob_changed = GNUNET_YES;
  if (GNUNET_YES == mlp->opt_mlp_auto_solve)
    GAS_mlp_solve_problem (solver);
}


/**
 * Transport properties for this address have changed
 *
 * @param solver solver handle
 * @param address the address
 */
static void
GAS_mlp_address_property_changed (void *solver,
                                  struct ATS_Address *address)
{
  struct MLP_information *mlpi = address->solver_information;
  struct GAS_MLP_Handle *mlp = solver;

  if (NULL == mlp->p.prob)
    return; /* There is no MLP problem to update yet */

  if (NULL == mlpi)
  {
    LOG (GNUNET_ERROR_TYPE_INFO,
         _("Updating address property for peer `%s' %p not added before\n"),
         GNUNET_i2s (&address->peer),
         address);
    GNUNET_break (0);
    return;
  }
  if (NULL ==
      GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
                                         &address->peer))
  {
    /* Peer is not requested, so no need to update problem */
    return;
  }
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Updating properties for peer `%s'\n",
       GNUNET_i2s(&address->peer));

  if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
    return;

  /* Update c7) [r_q[index]][c_b] = f_q * q_averaged[type_index] */
  if ( (GNUNET_YES ==
        mlp_create_problem_update_value (&mlp->p,
                                         mlp->p.r_q[RQ_QUALITY_METRIC_DELAY],
                                         mlpi->c_b,
                                         address->norm_delay.norm,
                                         __LINE__)) ||
       (GNUNET_YES ==
        mlp_create_problem_update_value (&mlp->p,
                                         mlp->p.r_q[RQ_QUALITY_METRIC_DISTANCE],
                                         mlpi->c_b,
                                         address->norm_distance.norm,
                                         __LINE__)) )
  {
    mlp->stat_mlp_prob_updated = GNUNET_YES;
    if (GNUNET_YES == mlp->opt_mlp_auto_solve)
      GAS_mlp_solve_problem (solver);
  }

}


/**
 * Find the active address in the set of addresses of a peer
 * @param cls destination
 * @param key peer id
 * @param value address
 * @return #GNUNET_OK
 */
static int
mlp_get_preferred_address_it (void *cls,
			      const struct GNUNET_PeerIdentity *key,
			      void *value)
{
  static int counter = 0;
  struct ATS_Address **aa = cls;
  struct ATS_Address *addr = value;
  struct MLP_information *mlpi = addr->solver_information;

  if (mlpi == NULL)
    return GNUNET_YES;

  /*
   * Debug output
   * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   *           "MLP [%u] Peer `%s' %s length %u session %u active %s mlp active %s\n",
   *           counter, GNUNET_i2s (&addr->peer), addr->plugin, addr->addr_len, addr->session_id,
   *           (GNUNET_YES == addr->active) ? "active" : "inactive",
   *           (GNUNET_YES == mlpi->n) ? "active" : "inactive");
   */

  if (GNUNET_YES == mlpi->n)
  {

    (*aa) = addr;
    (*aa)->assigned_bw_in = mlpi->b_in;
    (*aa)->assigned_bw_out = mlpi->b_out;
    return GNUNET_NO;
  }
  counter++;
  return GNUNET_YES;
}


static double
get_peer_pref_value (struct GAS_MLP_Handle *mlp,
                     const struct GNUNET_PeerIdentity *peer)
{
  double res;
  const double *preferences;
  int c;

  preferences = mlp->env->get_preferences (mlp->env->cls, peer);
  res = 0.0;
  for (c = 0; c < GNUNET_ATS_PREFERENCE_END; c++)
  {
    /* fprintf (stderr, "VALUE[%u] %s %.3f \n",
     *        c, GNUNET_i2s (&cur->addr->peer), t[c]); */
    res += preferences[c];
  }

  res /= GNUNET_ATS_PREFERENCE_END;
  res += 1.0;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Peer preference for peer  `%s' == %.2f\n",
       GNUNET_i2s(peer), res);

  return res;
}


/**
 * Get the preferred address for a specific peer
 *
 * @param solver the MLP Handle
 * @param peer the peer
 */
static void
GAS_mlp_get_preferred_address (void *solver,
                               const struct GNUNET_PeerIdentity *peer)
{
  struct GAS_MLP_Handle *mlp = solver;
  struct ATS_Peer *p;
  struct ATS_Address *res;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Getting preferred address for `%s'\n",
       GNUNET_i2s (peer));

  /* Is this peer included in the problem? */
  if (NULL ==
      GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
                                         peer))
    {
      LOG (GNUNET_ERROR_TYPE_INFO, "Adding peer `%s' to list of requested_peers with requests\n",
          GNUNET_i2s (peer));

      p = GNUNET_new (struct ATS_Peer);
      p->id = (*peer);
      p->f = get_peer_pref_value (mlp, peer);
      GNUNET_CONTAINER_multipeermap_put (mlp->requested_peers,
					 peer, p,
					 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);

      /* Added new peer, we have to rebuild problem before solving */
      mlp->stat_mlp_prob_changed = GNUNET_YES;

      if ((GNUNET_YES == mlp->opt_mlp_auto_solve)&&
          (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(mlp->env->addresses,
								peer)))
      {
        mlp->exclude_peer = peer;
        GAS_mlp_solve_problem (mlp);
        mlp->exclude_peer = NULL;
      }
  }
  /* Get prefered address */
  res = NULL;
  GNUNET_CONTAINER_multipeermap_get_multiple (mlp->env->addresses, peer,
                                              &mlp_get_preferred_address_it, &res);
  if (NULL != res)
    mlp->env->bandwidth_changed_cb (mlp->env->cls,
                                    res);
}


/**
 * Deletes a single address in the MLP problem
 *
 * The MLP problem has to be recreated and the problem has to be resolved
 *
 * @param solver the MLP Handle
 * @param address the address to delete
 */
static void
GAS_mlp_address_delete (void *solver,
			struct ATS_Address *address)
{
  struct GAS_MLP_Handle *mlp = solver;
  struct MLP_information *mlpi;
  struct ATS_Address *res;
  int was_active;

  mlpi = address->solver_information;
  if (NULL != mlpi)
  {
    /* Remove full address */
    GNUNET_free (mlpi);
    address->solver_information = NULL;
  }
  was_active = address->active;
  address->active = GNUNET_NO;
  address->assigned_bw_in = 0;
  address->assigned_bw_out = 0;

  /* Is this peer included in the problem? */
  if (NULL ==
      GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers,
                                         &address->peer))
  {
    LOG (GNUNET_ERROR_TYPE_INFO,
         "Deleting address for peer `%s' without address request \n",
         GNUNET_i2s(&address->peer));
    return;
  }
  LOG (GNUNET_ERROR_TYPE_INFO,
       "Deleting address for peer `%s' with address request \n",
      GNUNET_i2s (&address->peer));

  /* Problem size changed: new address for peer with pending request */
  mlp->stat_mlp_prob_changed = GNUNET_YES;
  if (GNUNET_YES == mlp->opt_mlp_auto_solve)
  {
    GAS_mlp_solve_problem (solver);
  }
  if (GNUNET_YES == was_active)
  {
    GAS_mlp_get_preferred_address (solver, &address->peer);
    res = NULL;
    GNUNET_CONTAINER_multipeermap_get_multiple (mlp->env->addresses,
                                                &address->peer,
                                                &mlp_get_preferred_address_it,
                                                &res);
    if (NULL == res)
    {
      /* No alternative address, disconnecting peer */
      mlp->env->bandwidth_changed_cb (mlp->env->cls, address);
    }
  }
}


/**
 * Start a bulk operation
 *
 * @param solver the solver
 */
static void
GAS_mlp_bulk_start (void *solver)
{
  struct GAS_MLP_Handle *s = solver;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Locking solver for bulk operation ...\n");
  GNUNET_assert (NULL != solver);
  s->stat_bulk_lock ++;
}


static void
GAS_mlp_bulk_stop (void *solver)
{
  struct GAS_MLP_Handle *s = solver;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Unlocking solver from bulk operation ...\n");
  GNUNET_assert (NULL != solver);

  if (s->stat_bulk_lock < 1)
  {
    GNUNET_break (0);
    return;
  }
  s->stat_bulk_lock --;

  if (0 < s->stat_bulk_requests)
  {
    GAS_mlp_solve_problem (solver);
    s->stat_bulk_requests= 0;
  }
}



/**
 * Stop notifying about address and bandwidth changes for this peer
 *
 * @param solver the MLP handle
 * @param peer the peer
 */
static void
GAS_mlp_stop_get_preferred_address (void *solver,
                                     const struct GNUNET_PeerIdentity *peer)
{
  struct GAS_MLP_Handle *mlp = solver;
  struct ATS_Peer *p = NULL;

  GNUNET_assert (NULL != solver);
  GNUNET_assert (NULL != peer);
  if (NULL != (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
  {
    GNUNET_assert (GNUNET_YES ==
                   GNUNET_CONTAINER_multipeermap_remove (mlp->requested_peers, peer, p));
    GNUNET_free (p);

    mlp->stat_mlp_prob_changed = GNUNET_YES;
    if (GNUNET_YES == mlp->opt_mlp_auto_solve)
    {
      GAS_mlp_solve_problem (solver);
    }
  }
}


/**
 * Changes the preferences for a peer in the MLP problem
 *
 * @param solver the MLP Handle
 * @param peer the peer
 * @param kind the kind to change the preference
 * @param pref_rel the relative score
 */
static void
GAS_mlp_address_change_preference (void *solver,
                   const struct GNUNET_PeerIdentity *peer,
                   enum GNUNET_ATS_PreferenceKind kind,
                   double pref_rel)
{
  struct GAS_MLP_Handle *mlp = solver;
  struct ATS_Peer *p;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Changing preference for address for peer `%s' to %.2f\n",
       GNUNET_i2s(peer),
       pref_rel);

  GNUNET_STATISTICS_update (mlp->env->stats,
                            "# LP address preference changes", 1, GNUNET_NO);
  /* Update the constraints with changed preferences */



  /* Update relativity constraint c9 */
  if (NULL == (p = GNUNET_CONTAINER_multipeermap_get (mlp->requested_peers, peer)))
  {
    LOG (GNUNET_ERROR_TYPE_INFO,
         "Updating preference for unknown peer `%s'\n",
         GNUNET_i2s(peer));
    return;
  }

  if (GNUNET_NO == mlp->opt_dbg_feasibility_only)
  {
    p->f = get_peer_pref_value (mlp, peer);
    mlp_create_problem_update_value (&mlp->p,
                                     p->r_c9,
                                     mlp->p.c_r,
                                     - p->f,
                                     __LINE__);

    /* Problem size changed: new address for peer with pending request */
    mlp->stat_mlp_prob_updated = GNUNET_YES;
    if (GNUNET_YES == mlp->opt_mlp_auto_solve)
      GAS_mlp_solve_problem (solver);
  }
}


/**
 * Get application feedback for a peer
 *
 * @param solver the solver handle
 * @param application the application
 * @param peer the peer to change the preference for
 * @param scope the time interval for this feedback: [now - scope .. now]
 * @param kind the kind to change the preference
 * @param score the score
 */
static void
GAS_mlp_address_preference_feedback (void *solver,
                                     struct GNUNET_SERVICE_Client *application,
                                     const struct GNUNET_PeerIdentity *peer,
                                     const struct GNUNET_TIME_Relative scope,
                                     enum GNUNET_ATS_PreferenceKind kind,
                                     double score)
{
  struct GAS_PROPORTIONAL_Handle *s = solver;

  GNUNET_assert (NULL != solver);
  GNUNET_assert (NULL != peer);
  GNUNET_assert (NULL != s);
}


static int
mlp_free_peers (void *cls,
		const struct GNUNET_PeerIdentity *key, void *value)
{
  struct GNUNET_CONTAINER_MultiPeerMap *map = cls;
  struct ATS_Peer *p = value;

  GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multipeermap_remove (map, key, value));
  GNUNET_free (p);

  return GNUNET_OK;
}


/**
 * Shutdown the MLP problem solving component
 *
 * @param cls the solver handle
 * @return NULL
 */
void *
libgnunet_plugin_ats_mlp_done (void *cls)
{
  struct GNUNET_ATS_SolverFunctions *sf = cls;
  struct GAS_MLP_Handle *mlp = sf->cls;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Shutting down mlp solver\n");
  mlp_delete_problem (mlp);
  GNUNET_CONTAINER_multipeermap_iterate (mlp->requested_peers,
					 &mlp_free_peers,
					 mlp->requested_peers);
  GNUNET_CONTAINER_multipeermap_destroy (mlp->requested_peers);
  mlp->requested_peers = NULL;

  /* Clean up GLPK environment */
  glp_free_env();
  GNUNET_free (mlp);

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Shutdown down of mlp solver complete\n");
  return NULL;
}


void *
libgnunet_plugin_ats_mlp_init (void *cls)
{
  static struct GNUNET_ATS_SolverFunctions sf;
  struct GNUNET_ATS_PluginEnvironment *env = cls;
  struct GAS_MLP_Handle * mlp = GNUNET_new (struct GAS_MLP_Handle);
  float f_tmp;
  unsigned long long tmp;
  unsigned int b_min;
  unsigned int n_min;
  int c;
  char *outputformat;

  struct GNUNET_TIME_Relative max_duration;
  long long unsigned int max_iterations;

  /* Init GLPK environment */
  int res = glp_init_env();
  switch (res) {
    case 0:
      LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
          "initialization successful");
      break;
    case 1:
      LOG (GNUNET_ERROR_TYPE_DEBUG, "GLPK: `%s'\n",
          "environment is already initialized");
      break;
    case 2:
      LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
          "initialization failed (insufficient memory)");
      GNUNET_free(mlp);
      return NULL;
      break;
    case 3:
      LOG (GNUNET_ERROR_TYPE_ERROR, "Could not init GLPK: `%s'\n",
          "initialization failed (unsupported programming model)");
      GNUNET_free(mlp);
      return NULL;
      break;
    default:
      break;
  }

  mlp->opt_dump_problem_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DUMP_PROBLEM_ALL");
  if (GNUNET_SYSERR == mlp->opt_dump_problem_all)
   mlp->opt_dump_problem_all = GNUNET_NO;

  mlp->opt_dump_solution_all = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DUMP_SOLUTION_ALL");
  if (GNUNET_SYSERR == mlp->opt_dump_solution_all)
   mlp->opt_dump_solution_all = GNUNET_NO;

  mlp->opt_dump_problem_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DUMP_PROBLEM_ON_FAIL");
  if (GNUNET_SYSERR == mlp->opt_dump_problem_on_fail)
   mlp->opt_dump_problem_on_fail = GNUNET_NO;

  mlp->opt_dump_solution_on_fail = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DUMP_SOLUTION_ON_FAIL");
  if (GNUNET_SYSERR == mlp->opt_dump_solution_on_fail)
   mlp->opt_dump_solution_on_fail = GNUNET_NO;

  mlp->opt_dbg_glpk_verbose = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DBG_GLPK_VERBOSE");
  if (GNUNET_SYSERR == mlp->opt_dbg_glpk_verbose)
   mlp->opt_dbg_glpk_verbose = GNUNET_NO;

  mlp->opt_dbg_feasibility_only = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DBG_FEASIBILITY_ONLY");
  if (GNUNET_SYSERR == mlp->opt_dbg_feasibility_only)
   mlp->opt_dbg_feasibility_only = GNUNET_NO;
  if (GNUNET_YES == mlp->opt_dbg_feasibility_only)
    LOG (GNUNET_ERROR_TYPE_WARNING,
        "MLP solver is configured to check feasibility only!\n");

  mlp->opt_dbg_autoscale_problem = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DBG_AUTOSCALE_PROBLEM");
  if (GNUNET_SYSERR == mlp->opt_dbg_autoscale_problem)
   mlp->opt_dbg_autoscale_problem = GNUNET_NO;
  if (GNUNET_YES == mlp->opt_dbg_autoscale_problem)
    LOG (GNUNET_ERROR_TYPE_WARNING,
        "MLP solver is configured automatically scale the problem!\n");

  mlp->opt_dbg_intopt_presolver = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DBG_INTOPT_PRESOLVE");
  if (GNUNET_SYSERR == mlp->opt_dbg_intopt_presolver)
   mlp->opt_dbg_intopt_presolver = GNUNET_NO;
  if (GNUNET_YES == mlp->opt_dbg_intopt_presolver)
    LOG (GNUNET_ERROR_TYPE_WARNING,
        "MLP solver is configured use the mlp presolver\n");

  mlp->opt_dbg_optimize_diversity = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DBG_OPTIMIZE_DIVERSITY");
  if (GNUNET_SYSERR == mlp->opt_dbg_optimize_diversity)
   mlp->opt_dbg_optimize_diversity = GNUNET_YES;
  if (GNUNET_NO == mlp->opt_dbg_optimize_diversity)
    LOG (GNUNET_ERROR_TYPE_WARNING,
        "MLP solver is not optimizing for diversity\n");

  mlp->opt_dbg_optimize_relativity= GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DBG_OPTIMIZE_RELATIVITY");
  if (GNUNET_SYSERR == mlp->opt_dbg_optimize_relativity)
   mlp->opt_dbg_optimize_relativity = GNUNET_YES;
  if (GNUNET_NO == mlp->opt_dbg_optimize_relativity)
    LOG (GNUNET_ERROR_TYPE_WARNING,
        "MLP solver is not optimizing for relativity\n");

  mlp->opt_dbg_optimize_quality = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DBG_OPTIMIZE_QUALITY");
  if (GNUNET_SYSERR == mlp->opt_dbg_optimize_quality)
   mlp->opt_dbg_optimize_quality = GNUNET_YES;
  if (GNUNET_NO == mlp->opt_dbg_optimize_quality)
    LOG (GNUNET_ERROR_TYPE_WARNING,
        "MLP solver is not optimizing for quality\n");

  mlp->opt_dbg_optimize_utility = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
     "ats", "MLP_DBG_OPTIMIZE_UTILITY");
  if (GNUNET_SYSERR == mlp->opt_dbg_optimize_utility)
   mlp->opt_dbg_optimize_utility = GNUNET_YES;
  if (GNUNET_NO == mlp->opt_dbg_optimize_utility)
    LOG (GNUNET_ERROR_TYPE_WARNING,
        "MLP solver is not optimizing for utility\n");

  if ( (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
       (GNUNET_NO == mlp->opt_dbg_optimize_quality) &&
       (GNUNET_NO == mlp->opt_dbg_optimize_relativity) &&
       (GNUNET_NO == mlp->opt_dbg_optimize_utility) &&
       (GNUNET_NO == mlp->opt_dbg_feasibility_only))
  {
    LOG (GNUNET_ERROR_TYPE_ERROR,
        _("MLP solver is not optimizing for anything, changing to feasibility check\n"));
    mlp->opt_dbg_feasibility_only = GNUNET_YES;
  }

  if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg,
     "ats", "MLP_LOG_FORMAT", &outputformat))
   mlp->opt_log_format = MLP_CPLEX;
  else
  {
    GNUNET_STRINGS_utf8_toupper(outputformat, outputformat);
    if (0 == strcmp (outputformat, "MPS"))
    {
      mlp->opt_log_format = MLP_MPS;
    }
    else if (0 == strcmp (outputformat, "CPLEX"))
    {
      mlp->opt_log_format = MLP_CPLEX;
    }
    else if (0 == strcmp (outputformat, "GLPK"))
    {
      mlp->opt_log_format = MLP_GLPK;
    }
    else
    {
      LOG (GNUNET_ERROR_TYPE_WARNING,
          "Invalid log format `%s' in configuration, using CPLEX!\n",
          outputformat);
      mlp->opt_log_format = MLP_CPLEX;
    }
    GNUNET_free (outputformat);
  }

  mlp->pv.BIG_M = (double) BIG_M_VALUE;

  mlp->pv.mip_gap = (double) 0.0;
  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
      "MLP_MAX_MIP_GAP", &f_tmp))
  {
    if ((f_tmp < 0.0) || (f_tmp > 1.0))
    {
      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
          "MIP gap", f_tmp);
    }
    else
    {
      mlp->pv.mip_gap = f_tmp;
      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
          "MIP gap", f_tmp);
    }
  }

  mlp->pv.lp_mip_gap = (double) 0.0;
  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
      "MLP_MAX_LP_MIP_GAP", &f_tmp))
  {
    if ((f_tmp < 0.0) || (f_tmp > 1.0))
    {
      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
          "LP/MIP", f_tmp);
    }
    else
    {
      mlp->pv.lp_mip_gap = f_tmp;
      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
          "LP/MIP", f_tmp);
    }
  }

  /* Get timeout for iterations */
  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(env->cfg, "ats",
      "MLP_MAX_DURATION", &max_duration))
  {
    max_duration = MLP_MAX_EXEC_DURATION;
  }

  /* Get maximum number of iterations */
  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_size(env->cfg, "ats",
      "MLP_MAX_ITERATIONS", &max_iterations))
  {
    max_iterations = MLP_MAX_ITERATIONS;
  }

  /* Get diversity coefficient from configuration */
  mlp->pv.co_D = MLP_DEFAULT_D;
  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
      "MLP_COEFFICIENT_D", &f_tmp))
  {
    if ((f_tmp < 0.0))
    {
      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
          "MLP_COEFFICIENT_D", f_tmp);
    }
    else
    {
      mlp->pv.co_D = f_tmp;
      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
          "MLP_COEFFICIENT_D", f_tmp);
    }
  }

  /* Get relativity coefficient from configuration */
  mlp->pv.co_R = MLP_DEFAULT_R;
  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
      "MLP_COEFFICIENT_R", &f_tmp))
  {
    if ((f_tmp < 0.0))
    {
      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
          "MLP_COEFFICIENT_R", f_tmp);
    }
    else
    {
      mlp->pv.co_R = f_tmp;
      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
          "MLP_COEFFICIENT_R", f_tmp);
    }
  }


  /* Get utilization coefficient from configuration */
  mlp->pv.co_U = MLP_DEFAULT_U;
  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
      "MLP_COEFFICIENT_U", &f_tmp))
  {
    if ((f_tmp < 0.0))
    {
      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
          "MLP_COEFFICIENT_U", f_tmp);
    }
    else
    {
      mlp->pv.co_U = f_tmp;
      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s gap of %.3f\n",
          "MLP_COEFFICIENT_U", f_tmp);
    }
  }

  /* Get quality metric coefficients from configuration */
  for (c = 0; c < RQ_QUALITY_METRIC_COUNT; c++)
  {
    /* initialize quality coefficients with default value 1.0 */
    mlp->pv.co_Q[c] = MLP_DEFAULT_QUALITY;
  }


  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
                                           "MLP_COEFFICIENT_QUALITY_DELAY",
                                           &tmp))
    mlp->pv.co_Q[RQ_QUALITY_METRIC_DELAY] = (double) tmp / 100;
  else
    mlp->pv.co_Q[RQ_QUALITY_METRIC_DELAY] = MLP_DEFAULT_QUALITY;

  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
                                           "MLP_COEFFICIENT_QUALITY_DISTANCE",
                                           &tmp))
    mlp->pv.co_Q[RQ_QUALITY_METRIC_DISTANCE] = (double) tmp / 100;
  else
    mlp->pv.co_Q[RQ_QUALITY_METRIC_DISTANCE] = MLP_DEFAULT_QUALITY;

  /* Get minimum bandwidth per used address from configuration */
  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
                                                      "MLP_MIN_BANDWIDTH",
                                                      &tmp))
    b_min = tmp;
  else
  {
    b_min = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
  }

  /* Get minimum number of connections from configuration */
  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (env->cfg, "ats",
                                                      "MLP_MIN_CONNECTIONS",
                                                      &tmp))
    n_min = tmp;
  else
    n_min = MLP_DEFAULT_MIN_CONNECTIONS;

  /* Init network quotas */
  for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
  {
    mlp->pv.quota_index[c] = c;
    mlp->pv.quota_out[c] = env->out_quota[c];
    mlp->pv.quota_in[c] = env->in_quota[c];

    LOG (GNUNET_ERROR_TYPE_INFO,
         "Quota for network `%s' (in/out) %llu/%llu\n",
         GNUNET_ATS_print_network_type (c),
         mlp->pv.quota_out[c],
         mlp->pv.quota_in[c]);
    /* Check if defined quota could make problem unsolvable */
    if ((n_min * b_min) > mlp->pv.quota_out[c])
    {
      LOG (GNUNET_ERROR_TYPE_INFO,
           _("Adjusting inconsistent outbound quota configuration for network `%s', is %llu must be at least %llu\n"),
           GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
           mlp->pv.quota_out[c],
           (n_min * b_min));
      mlp->pv.quota_out[c] = (n_min * b_min);
    }
    if ((n_min * b_min) > mlp->pv.quota_in[c])
    {
      LOG (GNUNET_ERROR_TYPE_INFO,
           _("Adjusting inconsistent inbound quota configuration for network `%s', is %llu must be at least %llu\n"),
           GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
           mlp->pv.quota_in[c],
           (n_min * b_min));
      mlp->pv.quota_in[c] = (n_min * b_min);
    }
    /* Check if bandwidth is too big to make problem solvable */
    if (mlp->pv.BIG_M < mlp->pv.quota_out[c])
    {
      LOG (GNUNET_ERROR_TYPE_INFO,
           _("Adjusting outbound quota configuration for network `%s'from %llu to %.0f\n"),
           GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
           mlp->pv.quota_out[c],
           mlp->pv.BIG_M);
      mlp->pv.quota_out[c] = mlp->pv.BIG_M ;
    }
    if (mlp->pv.BIG_M < mlp->pv.quota_in[c])
    {
      LOG (GNUNET_ERROR_TYPE_INFO,
           _("Adjusting inbound quota configuration for network `%s' from %llu to %.0f\n"),
           GNUNET_ATS_print_network_type(mlp->pv.quota_index[c]),
           mlp->pv.quota_in[c],
           mlp->pv.BIG_M);
      mlp->pv.quota_in[c] = mlp->pv.BIG_M ;
    }
  }
  mlp->env = env;
  sf.cls = mlp;
  sf.s_add = &GAS_mlp_address_add;
  sf.s_address_update_property = &GAS_mlp_address_property_changed;
  sf.s_get = &GAS_mlp_get_preferred_address;
  sf.s_get_stop = &GAS_mlp_stop_get_preferred_address;
  sf.s_pref = &GAS_mlp_address_change_preference;
  sf.s_feedback = &GAS_mlp_address_preference_feedback;
  sf.s_del = &GAS_mlp_address_delete;
  sf.s_bulk_start = &GAS_mlp_bulk_start;
  sf.s_bulk_stop = &GAS_mlp_bulk_stop;

  /* Setting MLP Input variables */
  mlp->pv.b_min = b_min;
  mlp->pv.n_min = n_min;
  mlp->pv.m_q = RQ_QUALITY_METRIC_COUNT;
  mlp->stat_mlp_prob_changed = GNUNET_NO;
  mlp->stat_mlp_prob_updated = GNUNET_NO;
  mlp->opt_mlp_auto_solve = GNUNET_YES;
  mlp->requested_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  mlp->stat_bulk_requests = 0;
  mlp->stat_bulk_lock = 0;

  /* Setup GLPK */
  /* Redirect GLPK output to GNUnet logging */
  glp_term_hook (&mlp_term_hook, (void *) mlp);

  /* Init LP solving parameters */
  glp_init_smcp(&mlp->control_param_lp);
  mlp->control_param_lp.msg_lev = GLP_MSG_OFF;
  if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
    mlp->control_param_lp.msg_lev = GLP_MSG_ALL;

  mlp->control_param_lp.it_lim = max_iterations;
  mlp->control_param_lp.tm_lim = max_duration.rel_value_us / 1000LL;

  /* Init MLP solving parameters */
  glp_init_iocp(&mlp->control_param_mlp);
  /* Setting callback function */
  mlp->control_param_mlp.cb_func = &mlp_branch_and_cut_cb;
  mlp->control_param_mlp.cb_info = mlp;
  mlp->control_param_mlp.msg_lev = GLP_MSG_OFF;
  mlp->control_param_mlp.mip_gap = mlp->pv.mip_gap;
  if (GNUNET_YES == mlp->opt_dbg_glpk_verbose)
    mlp->control_param_mlp.msg_lev = GLP_MSG_ALL;
  mlp->control_param_mlp.tm_lim = max_duration.rel_value_us / 1000LL;

  LOG (GNUNET_ERROR_TYPE_DEBUG, "solver ready\n");

  return &sf;
}

/* end of plugin_ats_mlp.c */