Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Main.cpp « mira - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5586fe297f42f7110e5ab6ffdc42230a65b244f (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
/***********************************************************************
 Moses - factored phrase-based language decoder
 Copyright (C) 2010 University of Edinburgh

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

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***********************************************************************/

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <map>

#include <boost/program_options.hpp>
#include <boost/algorithm/string.hpp>

#ifdef MPI_ENABLE
#include <boost/mpi.hpp>
namespace mpi = boost::mpi;
#endif

#include "Main.h"
#include "FeatureVector.h"
#include "StaticData.h"
#include "ChartTrellisPathList.h"
#include "ChartTrellisPath.h"
#include "ScoreComponentCollection.h"
#include "Optimiser.h"
#include "Hildreth.h"
#include "ThreadPool.h"
#include "DummyScoreProducers.h"
#include "LexicalReordering.h"
#include "BleuScorer.h"
#include "HypothesisQueue.h"

using namespace Mira;
using namespace std;
using namespace Moses;
namespace po = boost::program_options;

int main(int argc, char** argv) {
  size_t rank = 0;
  size_t size = 1;
#ifdef MPI_ENABLE
  mpi::environment env(argc,argv);
  mpi::communicator world;
  rank = world.rank();
  size = world.size();
#endif
  
  bool help;
  int verbosity;
  string mosesConfigFile;
  string inputFile;
  vector<string> referenceFiles;
  vector<string> mosesConfigFilesFolds, inputFilesFolds, referenceFilesFolds;
  //  string coreWeightFile, startWeightFile;
  size_t epochs;
  string learner;
  bool shuffle;
  size_t mixingFrequency;
  size_t weightDumpFrequency;
  string weightDumpStem;
  bool scale_margin, scale_margin_precision;
  bool scale_update, scale_update_precision;
  size_t n;
  size_t batchSize;
  bool distinctNbest;
  bool accumulateWeights;
  float historySmoothing;
  bool scaleByInputLength, scaleByAvgInputLength;
  bool scaleByInverseLength, scaleByAvgInverseLength;
  float scaleByX;
  float slack;
  bool averageWeights;
  bool weightConvergence;
  float learning_rate;
  float mira_learning_rate;
  float perceptron_learning_rate;
  string decoder_settings;
  float min_weight_change;
  bool normaliseWeights, normaliseMargin;
  bool print_feature_values;
  bool historyBleu   ;
  bool sentenceBleu;
  bool perceptron_update;
  bool hope_fear, hope_model;
  bool model_hope_fear, rank_only;
  int hope_n, fear_n, rank_n;
  size_t bleu_smoothing_scheme;
  float min_oracle_bleu;
  float minBleuRatio, maxBleuRatio;
  bool boost;
  bool decode_hope, decode_fear, decode_model;
  string decode_filename;
  bool batchEqualsShard;
  bool sparseAverage, dumpMixedWeights, sparseNoAverage;
  int featureCutoff;
  bool pruneZeroWeights;
  bool megam;
  bool printFeatureCounts, printNbestWithFeatures;
  bool avgRefLength;
  bool print_weights, print_core_weights, clear_static, debug_model, scale_lm, scale_wp;
  float scale_lm_factor, scale_wp_factor;
  bool sample;
  string moses_src;
  float sigmoidParam;
  float bleuWeight, bleuWeight_hope, bleuWeight_fear;
  bool bleu_weight_lm, bleu_weight_lm_adjust;
  float bleu_weight_lm_factor;
  bool scale_all;
  float scale_all_factor;
  bool l1_regularize, l2_regularize;
  float l1_lambda, l2_lambda;
  bool most_violated, all_violated, max_bleu_diff, one_against_all;
  bool feature_confidence, signed_counts;
  float decay_core, decay_sparse, core_r0, sparse_r0;
  bool selective, summed, add2hope, skip_hope, skip_model, skip_fear;
  float bleu_weight_fear_factor, scaling_constant;
  bool hildreth;
  float add2lm;
  bool realBleu, disableBleuFeature;
  bool rescaleSlack, rewardHope;
  bool makePairs;
  po::options_description desc("Allowed options");
  desc.add_options()
    ("make-pairs", po::value<bool>(&makePairs)->default_value(true), "Make pairs of hypotheses for 1slack")
    ("reward-hope", po::value<bool>(&rewardHope)->default_value(false), "Reward hope features over fear features")
    ("rescale-slack", po::value<bool>(&rescaleSlack)->default_value(false), "Rescale slack in 1-slack formulation")
    ("disable-bleu-feature", po::value<bool>(&disableBleuFeature)->default_value(false), "Disable the Bleu feature")
    ("real-bleu", po::value<bool>(&realBleu)->default_value(false), "Compute real sentence Bleu on complete translations") 
    ("add2lm", po::value<float>(&add2lm)->default_value(0.0), "Add the specified amount to all LM weights")
    ("hildreth", po::value<bool>(&hildreth)->default_value(false), "Prefer Hildreth over analytical update")
    ("skip-hope", po::value<bool>(&skip_hope)->default_value(false), "Sample without hope translations")
    ("skip-model", po::value<bool>(&skip_model)->default_value(false), "Sample without model translations")
    ("skip-fear", po::value<bool>(&skip_fear)->default_value(false), "Sample without fear translations")
    ("add2hope", po::value<bool>(&add2hope)->default_value(false), "Add 2 hope translations instead of 1")
    ("scaling-constant", po::value<float>(&scaling_constant)->default_value(1.0), "Scale all core values by a constant at beginning of training")
    ("selective", po::value<bool>(&selective)->default_value(false), "Build constraints for every feature")       
    ("summed", po::value<bool>(&summed)->default_value(false), "Sum up all constraints")
    
    ("bleu-weight", po::value<float>(&bleuWeight)->default_value(1.0), "Bleu weight used in decoder objective")
    ("bw-hope", po::value<float>(&bleuWeight_hope)->default_value(-1.0), "Bleu weight used in decoder objective for hope")
    ("bw-fear", po::value<float>(&bleuWeight_fear)->default_value(-1.0), "Bleu weight used in decoder objective for fear")
    
    ("core-r0", po::value<float>(&core_r0)->default_value(1.0), "Start learning rate for core features")
    ("sparse-r0", po::value<float>(&sparse_r0)->default_value(1.0), "Start learning rate for sparse features")

    ("tie-bw-to-lm", po::value<bool>(&bleu_weight_lm)->default_value(false), "Make bleu weight depend on lm weight")   
    ("adjust-bw", po::value<bool>(&bleu_weight_lm_adjust)->default_value(false), "Adjust bleu weight when lm weight changes")       
    ("bw-lm-factor", po::value<float>(&bleu_weight_lm_factor)->default_value(2.0), "Make bleu weight depend on lm weight by this factor")
    ("bw-factor-fear", po::value<float>(&bleu_weight_fear_factor)->default_value(1.0), "Multiply fear weight by this factor")

    ("scale-all", po::value<bool>(&scale_all)->default_value(false), "Scale all core features")
    ("scaling-factor", po::value<float>(&scale_all_factor)->default_value(2), "Scaling factor for all core features")
    
    ("accumulate-weights", po::value<bool>(&accumulateWeights)->default_value(false), "Accumulate and average weights over all epochs")
    ("average-weights", po::value<bool>(&averageWeights)->default_value(false), "Set decoder weights to average weights after each update")
    ("avg-ref-length", po::value<bool>(&avgRefLength)->default_value(false), "Use average reference length instead of shortest for BLEU score feature")
    ("batch-equals-shard", po::value<bool>(&batchEqualsShard)->default_value(false), "Batch size is equal to shard size (purely batch)")
    ("batch-size,b", po::value<size_t>(&batchSize)->default_value(1), "Size of batch that is send to optimiser for weight adjustments")
    ("bleu-smoothing-scheme", po::value<size_t>(&bleu_smoothing_scheme)->default_value(1), "Set a smoothing scheme for sentence-Bleu: +1 (1), +0.1 (2), papineni (3) (default:1)")
    ("boost", po::value<bool>(&boost)->default_value(false), "Apply boosting factor to updates on misranked candidates")
    ("clear-static", po::value<bool>(&clear_static)->default_value(false), "Clear static data before every translation")
    ("config,f", po::value<string>(&mosesConfigFile), "Moses ini-file")
    ("configs-folds", po::value<vector<string> >(&mosesConfigFilesFolds), "Moses ini-files, one for each fold")
    //("core-weights", po::value<string>(&coreWeightFile)->default_value(""), "Weight file containing the core weights (already tuned, have to be non-zero)")
    ("decay-core", po::value<float>(&decay_core)->default_value(0.001), "Decay factor for updating core feature learning rates")
    ("decay-sparse", po::value<float>(&decay_sparse)->default_value(0.001), "Decay factor for updating sparse feature learning rates")
    ("debug-model", po::value<bool>(&debug_model)->default_value(false), "Get best model translation for debugging purposes")
    ("decode-hope", po::value<bool>(&decode_hope)->default_value(false), "Decode dev input set according to hope objective")
    ("decode-fear", po::value<bool>(&decode_fear)->default_value(false), "Decode dev input set according to fear objective")
    ("decode-model", po::value<bool>(&decode_model)->default_value(false), "Decode dev input set according to normal objective")
    ("decode-filename", po::value<string>(&decode_filename), "Filename for Bleu objective translations")
    ("decoder-settings", po::value<string>(&decoder_settings)->default_value(""), "Decoder settings for tuning runs")
    ("distinct-nbest", po::value<bool>(&distinctNbest)->default_value(true), "Use n-best list with distinct translations in inference step")
    ("dump-mixed-weights", po::value<bool>(&dumpMixedWeights)->default_value(false), "Dump mixed weights instead of averaged weights")
    ("epochs,e", po::value<size_t>(&epochs)->default_value(10), "Number of epochs")
    ("feature-confidence", po::value<bool>(&feature_confidence)->default_value(false), "Use feature weight confidence in weight updates")
    ("feature-cutoff", po::value<int>(&featureCutoff)->default_value(-1), "Feature cutoff as additional regularization for sparse features")
    ("fear-n", po::value<int>(&fear_n)->default_value(1), "Number of fear translations used")
    ("help", po::value(&help)->zero_tokens()->default_value(false), "Print this help message and exit")
    ("history-bleu", po::value<bool>(&historyBleu)->default_value(false), "Use 1best translations to update the history")
    ("history-smoothing", po::value<float>(&historySmoothing)->default_value(0.9), "Adjust the factor for history smoothing")
    ("hope-fear", po::value<bool>(&hope_fear)->default_value(true), "Use only hope and fear translations for optimisation (not model)")
    ("hope-model", po::value<bool>(&hope_model)->default_value(false), "Use only hope and model translations for optimisation (use --fear-n to set number of model translations)")
    ("hope-n", po::value<int>(&hope_n)->default_value(2), "Number of hope translations used")
    ("input-file,i", po::value<string>(&inputFile), "Input file containing tokenised source")
    ("input-files-folds", po::value<vector<string> >(&inputFilesFolds), "Input files containing tokenised source, one for each fold")
    ("learner,l", po::value<string>(&learner)->default_value("mira"), "Learning algorithm")
    ("l1-lambda", po::value<float>(&l1_lambda)->default_value(0.0001), "Lambda for l1-regularization (w_i +/- lambda)")
    ("l2-lambda", po::value<float>(&l2_lambda)->default_value(0.01), "Lambda for l2-regularization (w_i * (1 - lambda))")
    ("l1-reg", po::value<bool>(&l1_regularize)->default_value(false), "L1-regularization")
    ("l2-reg", po::value<bool>(&l2_regularize)->default_value(false), "L2-regularization")
    ("min-bleu-ratio", po::value<float>(&minBleuRatio)->default_value(-1), "Set a minimum BLEU ratio between hope and fear")
    ("max-bleu-ratio", po::value<float>(&maxBleuRatio)->default_value(-1), "Set a maximum BLEU ratio between hope and fear")
    ("max-bleu-diff", po::value<bool>(&max_bleu_diff)->default_value(true), "For 'sampling': select hope/fear with maximum Bleu difference")
    ("megam", po::value<bool>(&megam)->default_value(false), "Use megam for optimization step")
    ("min-oracle-bleu", po::value<float>(&min_oracle_bleu)->default_value(0), "Set a minimum oracle BLEU score")
    ("min-weight-change", po::value<float>(&min_weight_change)->default_value(0.01), "Set minimum weight change for stopping criterion")
    ("mira-learning-rate", po::value<float>(&mira_learning_rate)->default_value(1), "Learning rate for MIRA (fixed or flexible)")
    ("mixing-frequency", po::value<size_t>(&mixingFrequency)->default_value(1), "How often per epoch to mix weights, when using mpi")
    ("model-hope-fear", po::value<bool>(&model_hope_fear)->default_value(false), "Use model, hope and fear translations for optimisation")
    ("moses-src", po::value<string>(&moses_src)->default_value(""), "Moses source directory")
    ("most-violated", po::value<bool>(&most_violated)->default_value(false), "Pick pair of hypo and hope that violates constraint the most")
    ("all-violated", po::value<bool>(&all_violated)->default_value(false), "Pair all hypos with hope translation that violate constraint")
    ("one-against-all", po::value<bool>(&one_against_all)->default_value(false), "Pick best Bleu as hope and all others are fear")
    ("nbest,n", po::value<size_t>(&n)->default_value(1), "Number of translations in n-best list")
    ("normalise-weights", po::value<bool>(&normaliseWeights)->default_value(false), "Whether to normalise the updated weights before passing them to the decoder")
    ("normalise-margin", po::value<bool>(&normaliseMargin)->default_value(false), "Normalise the margin: squash between 0 and 1")
    ("perceptron-learning-rate", po::value<float>(&perceptron_learning_rate)->default_value(0.01), "Perceptron learning rate")
    ("print-feature-values", po::value<bool>(&print_feature_values)->default_value(false), "Print out feature values")
    ("print-feature-counts", po::value<bool>(&printFeatureCounts)->default_value(false), "Print out feature values, print feature list with hope counts after 1st epoch")
    ("print-nbest-with-features", po::value<bool>(&printNbestWithFeatures)->default_value(false), "Print out feature values, print feature list with hope counts after 1st epoch")
    ("print-weights", po::value<bool>(&print_weights)->default_value(false), "Print out current weights")
    ("print-core-weights", po::value<bool>(&print_core_weights)->default_value(true), "Print out current core weights")
    ("prune-zero-weights", po::value<bool>(&pruneZeroWeights)->default_value(false), "Prune zero-valued sparse feature weights")	    
    ("rank-n", po::value<int>(&rank_n)->default_value(-1), "Number of translations used for ranking")
    ("rank-only", po::value<bool>(&rank_only)->default_value(false), "Use only model translations for optimisation")
    ("reference-files,r", po::value<vector<string> >(&referenceFiles), "Reference translation files for training")
    ("reference-files-folds", po::value<vector<string> >(&referenceFilesFolds), "Reference translation files for training, one for each fold")	       
    ("sample", po::value<bool>(&sample)->default_value(false), "Sample a translation pair from hope/(model)/fear translations") 
    ("scale-by-inverse-length", po::value<bool>(&scaleByInverseLength)->default_value(false), "Scale BLEU by (history of) inverse input length")
    ("scale-by-input-length", po::value<bool>(&scaleByInputLength)->default_value(true), "Scale BLEU by (history of) input length")
    ("scale-by-avg-input-length", po::value<bool>(&scaleByAvgInputLength)->default_value(false), "Scale BLEU by average input length")
    ("scale-by-avg-inverse-length", po::value<bool>(&scaleByAvgInverseLength)->default_value(false), "Scale BLEU by average inverse input length")
    ("scale-by-x", po::value<float>(&scaleByX)->default_value(1), "Scale the BLEU score by value x")
    ("scale-lm", po::value<bool>(&scale_lm)->default_value(false), "Scale the language model feature") 
    ("scale-factor-lm", po::value<float>(&scale_lm_factor)->default_value(2), "Scale the language model feature by this factor")
    ("scale-wp", po::value<bool>(&scale_wp)->default_value(false), "Scale the word penalty feature") 
    ("scale-factor-wp", po::value<float>(&scale_wp_factor)->default_value(2), "Scale the word penalty feature by this factor")
    ("scale-margin", po::value<bool>(&scale_margin)->default_value(0), "Scale the margin by the Bleu score of the oracle translation")
    ("scale-margin-precision", po::value<bool>(&scale_margin_precision)->default_value(0), "Scale margin by precision of oracle")
    ("scale-update", po::value<bool>(&scale_update)->default_value(0), "Scale update by Bleu score of oracle") 
    ("scale-update-precision", po::value<bool>(&scale_update_precision)->default_value(0), "Scale update by precision of oracle")	
    ("sentence-level-bleu", po::value<bool>(&sentenceBleu)->default_value(true), "Use a sentences level Bleu scoring function")
    ("shuffle", po::value<bool>(&shuffle)->default_value(false), "Shuffle input sentences before processing")
    ("signed-counts", po::value<bool>(&signed_counts)->default_value(false), "Use signed counts for feature learning rates")
    ("sigmoid-param", po::value<float>(&sigmoidParam)->default_value(1), "y=sigmoidParam is the axis that this sigmoid approaches")
    ("slack", po::value<float>(&slack)->default_value(0.01), "Use slack in optimiser")
    ("sparse-average", po::value<bool>(&sparseAverage)->default_value(false), "Average weights by the number of processes")
    ("sparse-no-average", po::value<bool>(&sparseNoAverage)->default_value(false), "Don't average sparse weights, just sum")
    //("start-weights", po::value<string>(&startWeightFile)->default_value(""), "Weight file containing start weights")
    ("stop-weights", po::value<bool>(&weightConvergence)->default_value(true), "Stop when weights converge")
    ("verbosity,v", po::value<int>(&verbosity)->default_value(0), "Verbosity level")
    ("weight-dump-frequency", po::value<size_t>(&weightDumpFrequency)->default_value(1), "How often per epoch to dump weights (mpi)")
    ("weight-dump-stem", po::value<string>(&weightDumpStem)->default_value("weights"), "Stem of filename to use for dumping weights");
  
  po::options_description cmdline_options;
  cmdline_options.add(desc);
  po::variables_map vm;
  po::store(po::command_line_parser(argc, argv). options(cmdline_options).run(), vm);
  po::notify(vm);
  
  if (help) {
    std::cout << "Usage: " + string(argv[0])
      + " -f mosesini-file -i input-file -r reference-file(s) [options]" << std::endl;
    std::cout << desc << std::endl;
    return 0;
  }

  cerr << "l1-reg: " << l1_regularize << endl;
  cerr << "featureCutoff: " << featureCutoff << endl;
  cerr << "featureConfidence: " << feature_confidence << endl;
  
  const StaticData &staticData = StaticData::Instance();

  bool trainWithMultipleFolds = false; 
  if (mosesConfigFilesFolds.size() > 0 || inputFilesFolds.size() > 0 || referenceFilesFolds.size() > 0) {
  	if (rank == 0)
  		cerr << "Training with " << mosesConfigFilesFolds.size() << " folds" << endl;
  	trainWithMultipleFolds = true;
  }

  cerr << "test 1" << endl;
  if (dumpMixedWeights && (mixingFrequency != weightDumpFrequency)) {
	  cerr << "Set mixing frequency = weight dump frequency for dumping mixed weights!" << endl;
	  exit(1);
  }

  if ((sparseAverage || sparseNoAverage) && averageWeights) {
	  cerr << "Parameters --sparse-average 1/--sparse-no-average 1 and --average-weights 1 are incompatible (not implemented)" << endl;
	  exit(1);
  }

  cerr << "test 2" << endl;
  if (trainWithMultipleFolds) {
	  if (!mosesConfigFilesFolds.size()) {
		  cerr << "Error: No moses ini files specified for training with folds" << endl;
		  exit(1);
	  }
	  
	  if (!inputFilesFolds.size()) {
		  cerr << "Error: No input files specified for training with folds" << endl;
		  exit(1);
	  }

	  if (!referenceFilesFolds.size()) {
		  cerr << "Error: No reference files specified for training with folds" << endl;
		  exit(1);
	  }
  }
  else {
	  if (mosesConfigFile.empty()) {
		  cerr << "Error: No moses ini file specified" << endl;
		  return 1;
	  }
	  
	  if (inputFile.empty()) {
		  cerr << "Error: No input file specified" << endl;
		  return 1;
	  }

	  if (!referenceFiles.size()) {
		  cerr << "Error: No reference files specified" << endl;
		  return 1;
	  }
  }

	// load input and references
  cerr << "test 3" << endl;
  	vector<string> inputSentences;
  	size_t inputSize = trainWithMultipleFolds? inputFilesFolds.size(): 0;
  	size_t refSize = trainWithMultipleFolds? referenceFilesFolds.size(): referenceFiles.size(); 
  	vector<vector<string> > inputSentencesFolds(inputSize);
  	vector<vector<string> > referenceSentences(refSize);
  	
	// number of cores for each fold
  	size_t coresPerFold = 0, myFold = 0;
  	if (trainWithMultipleFolds) {
  		if (mosesConfigFilesFolds.size() > size) {
  			cerr << "Number of cores has to be a multiple of the number of folds" << endl;
  			exit(1);
  		}
  		coresPerFold = size/mosesConfigFilesFolds.size();
  		if (size % coresPerFold > 0) {
  			cerr << "Number of cores has to be a multiple of the number of folds" << endl;
  			exit(1);
  		} 
		
  		if (rank == 0)
  			cerr << "Number of cores per fold: " << coresPerFold << endl;		
  		myFold = rank/coresPerFold;
  		cerr << "Rank " << rank << ", my fold: " << myFold << endl;
  	}
  	
  	// NOTE: we do not actually need the references here, because we are reading them in from StaticData
  	if (trainWithMultipleFolds) {
  		if (!loadSentences(inputFilesFolds[myFold], inputSentencesFolds[myFold])) {
  			cerr << "Error: Failed to load input sentences from " << inputFilesFolds[myFold] << endl;
  			exit(1);
  		}
  		VERBOSE(1, "Rank " << rank << " reading inputs from " << inputFilesFolds[myFold] << endl);
  		
  		if (!loadSentences(referenceFilesFolds[myFold], referenceSentences[myFold])) {
  			cerr << "Error: Failed to load reference sentences from " << referenceFilesFolds[myFold] << endl;
  			exit(1);
  		}
  		if (referenceSentences[myFold].size() != inputSentencesFolds[myFold].size()) {
  			cerr << "Error: Input file length (" << inputSentencesFolds[myFold].size() << ") != ("
  				<< referenceSentences[myFold].size() << ") reference file length (rank " << rank << ")" << endl;
  			exit(1);
  		}
  		VERBOSE(1, "Rank " << rank << " reading references from " << referenceFilesFolds[myFold] << endl);  		
  	}
  	else {
  		if (!loadSentences(inputFile, inputSentences)) {
  			cerr << "Error: Failed to load input sentences from " << inputFile << endl;
  			return 1;
  		}
  		
  		for (size_t i = 0; i < referenceFiles.size(); ++i) {
  			if (!loadSentences(referenceFiles[i], referenceSentences[i])) {
  				cerr << "Error: Failed to load reference sentences from "
  						<< referenceFiles[i] << endl;
  				return 1;
  			}
  			if (referenceSentences[i].size() != inputSentences.size()) {
  				cerr << "Error: Input file length (" << inputSentences.size() << ") != ("
  						<< referenceSentences[i].size() << ") length of reference file " << i
  						<< endl;
  				return 1;
  			}
  		}
  	}

	if (scaleByAvgInputLength ||  scaleByInverseLength || scaleByAvgInverseLength)
		scaleByInputLength = false;

	if (historyBleu) {
	  sentenceBleu = false;
	  cerr << "Using history Bleu. " << endl;
	}

	// initialise Moses
	// add initial Bleu weight and references to initialize Bleu feature
	boost::trim(decoder_settings);
	decoder_settings += " -mira -distinct-nbest -weight-bl 1 -references";
	cerr << "test 4" << endl;
	if (trainWithMultipleFolds) {
		decoder_settings += " ";
		decoder_settings += referenceFilesFolds[myFold];
	}
	else {
		for (size_t i=0; i < referenceFiles.size(); ++i) {
			decoder_settings += " ";
			decoder_settings += referenceFiles[i];
		}
	}
	
	cerr << "test 5" << endl;
	vector<string> decoder_params;
	boost::split(decoder_params, decoder_settings, boost::is_any_of("\t "));
	
	string configFile = trainWithMultipleFolds? mosesConfigFilesFolds[myFold] : mosesConfigFile;
	VERBOSE(1, "Rank " << rank << " reading config file from " << configFile << endl);
	cerr << "test 6" << endl;
	MosesDecoder* decoder = new MosesDecoder(configFile, verbosity, decoder_params.size(), decoder_params);
	cerr << "test 7" << endl;
	decoder->setBleuParameters(disableBleuFeature, sentenceBleu, scaleByInputLength, scaleByAvgInputLength,
			scaleByInverseLength, scaleByAvgInverseLength,
			scaleByX, historySmoothing, bleu_smoothing_scheme);
	cerr << "test 8" << endl;
	SearchAlgorithm searchAlgorithm = staticData.GetSearchAlgorithm();
	bool chartDecoding = (searchAlgorithm == ChartDecoding);
	cerr << "test 9" << endl;

	// Optionally shuffle the sentences
	vector<size_t> order;
	if (trainWithMultipleFolds) {  	
	  for (size_t i = 0; i < inputSentencesFolds[myFold].size(); ++i) {
	    order.push_back(i);
	  }
	}
	else {
	  if (rank == 0) {
	    for (size_t i = 0; i < inputSentences.size(); ++i) {
	      order.push_back(i);
	    }	   
	  }
	}

	// initialise optimizer
	Optimiser* optimiser = NULL;
	if (learner == "mira") {
		if (rank == 0) {
			cerr << "Optimising using Mira" << endl;
			cerr << "slack: " << slack << ", learning rate: " << mira_learning_rate << endl;
			cerr << "selective: " << selective << endl;
			if (normaliseMargin) 
			  cerr << "sigmoid parameter: " << sigmoidParam << endl;
		}
		optimiser = new MiraOptimiser(slack, scale_margin, scale_margin_precision,
					      scale_update, scale_update_precision, boost, normaliseMargin, sigmoidParam);
		learning_rate = mira_learning_rate;
		perceptron_update = false;
	} else if (learner == "perceptron") {
		if (rank == 0) {
			cerr << "Optimising using Perceptron" << endl;
		}
		optimiser = new Perceptron();
		learning_rate = perceptron_learning_rate;
		perceptron_update = true;
		model_hope_fear = false; // mira only
		rank_only = false; // mira only
		hope_fear = false; // mira only
		hope_model = false; // mira only
		n = 1;
		hope_n = 1;
		fear_n = 1;
	} else {
		cerr << "Error: Unknown optimiser: " << learner << endl;
		return 1;
	}

	// resolve parameter dependencies
	if (batchSize > 1 && perceptron_update) {
		batchSize = 1;
		cerr << "Info: Setting batch size to 1 for perceptron update" << endl;
	}

	if (hope_n == -1)
	  hope_n = n;
	if (fear_n == -1)
	  fear_n = n;
	if (rank_n == -1)
	  rank_n = n;

	if (sample)
	  model_hope_fear = true;
	if (model_hope_fear || hope_model || rank_only || megam)
	  hope_fear = false; // is true by default
	if (learner == "mira" && !(hope_fear || hope_model || model_hope_fear || rank_only || megam)) {
	  cerr << "Error: Need to select one of parameters --hope-fear/--model-hope-fear for mira update." << endl;
	  return 1;
	}

#ifdef MPI_ENABLE
	if (!trainWithMultipleFolds)
	  mpi::broadcast(world, order, 0);
#endif

	// Create shards according to the number of processes used
	vector<size_t> shard;
	if (trainWithMultipleFolds) {			
		float shardSize = (float) (order.size())/coresPerFold;
		size_t shardStart = (size_t) (shardSize * (rank % coresPerFold));
		size_t shardEnd = shardStart + shardSize;
		if (rank % coresPerFold == coresPerFold - 1) { // last rank of each fold 
			shardEnd = order.size();
			shardSize = shardEnd - shardStart;
		}		
		VERBOSE(1, "Rank: " << rank << ", shard size: " << shardSize << endl);
		VERBOSE(1, "Rank: " << rank << ", shard start: " << shardStart << " shard end: " << shardEnd << endl);
		shard.resize(shardSize);
		copy(order.begin() + shardStart, order.begin() + shardEnd, shard.begin());
		batchSize = 1;
	}
	else {
		float shardSize = (float) (order.size()) / size;
		size_t shardStart = (size_t) (shardSize * rank);
		size_t shardEnd = (size_t) (shardSize * (rank + 1));
		if (rank == size - 1) {
			shardEnd = order.size();
			shardSize = shardEnd - shardStart;
		}
		VERBOSE(1, "Shard size: " << shardSize << endl);
		VERBOSE(1, "Rank: " << rank << " Shard start: " << shardStart << " Shard end: " << shardEnd << endl);
		shard.resize(shardSize);
		copy(order.begin() + shardStart, order.begin() + shardEnd, shard.begin());
		if (batchEqualsShard)
			batchSize = shardSize;
	}

	// get reference to feature functions
	const vector<const ScoreProducer*> featureFunctions =
			staticData.GetTranslationSystem(TranslationSystem::DEFAULT).GetFeatureFunctions();
	//const vector<FactorType> &inputFactorOrder = staticData.GetInputFactorOrder();

	//ProducerWeightMap coreWeightMap, startWeightMap;
	ScoreComponentCollection initialWeights = decoder->getWeights();
	// read start weight file                                                                                
	/*if (!startWeightFile.empty()) {
	  if (!loadCoreWeights(startWeightFile, startWeightMap, featureFunctions)) {
	    cerr << "Error: Failed to load start weights from " << startWeightFile << endl;
	    return 1;
	  }
	  else
	    cerr << "Loaded start weights from " << startWeightFile << "." << endl;
	       
	  // set start weights                                                                                                          
	  if (startWeightMap.size() > 0) {
	    ProducerWeightMap::iterator p;
	    for(p = startWeightMap.begin(); p!=startWeightMap.end(); ++p)
              initialWeights.Assign(p->first, p->second);
	  }
	}

	// read core weight file
	if (!coreWeightFile.empty()) {
	  if (!loadCoreWeights(coreWeightFile, coreWeightMap, featureFunctions)) {
	    cerr << "Error: Failed to load core weights from " << coreWeightFile << endl;
	    return 1;
	  }
	  else
	    cerr << "Loaded core weights from " << coreWeightFile << "." << endl;
		
	  // set core weights
	  if (coreWeightMap.size() > 0) {
	    ProducerWeightMap::iterator p;
	    for(p = coreWeightMap.begin(); p!=coreWeightMap.end(); ++p)
	      initialWeights.Assign(p->first, p->second);
	  }
	  }*/
        cerr << "Rank " << rank << ", initial weights: " << initialWeights << endl;
	if (scaling_constant != 1.0) {
	  initialWeights.MultiplyEquals(scaling_constant);
	  cerr << "Rank " << rank << ", scaled initial weights: " << initialWeights << endl;
	}

	if (add2lm != 0) {
	  const LMList& lmList_new = staticData.GetLMList();
	  for (LMList::const_iterator i = lmList_new.begin(); i != lmList_new.end(); ++i) {
	    float lmWeight = initialWeights.GetScoreForProducer(*i) + add2lm;
	    initialWeights.Assign(*i, lmWeight);
	    cerr << "Rank " << rank << ", add " << add2lm << " to lm weight." << endl;
	  }
	}
	
	if (normaliseWeights) {
	  initialWeights.L1Normalise();
	  cerr << "Rank " << rank << ", normalised initial weights: " << initialWeights << endl;
	}

	decoder->setWeights(initialWeights);

	if (scale_all) {
	  cerr << "Scale all core features by factor " << scale_all_factor << endl;
	  scale_lm = true;
	  scale_wp = true;
	  scale_lm_factor = scale_all_factor;
	  scale_wp_factor = scale_all_factor;
	}

	// set bleu weight to twice the size of the language model weight(s)
	const LMList& lmList = staticData.GetLMList();
	if (bleu_weight_lm) {
	  float lmSum = 0;
	  for (LMList::const_iterator i = lmList.begin(); i != lmList.end(); ++i) 
	    lmSum += abs(initialWeights.GetScoreForProducer(*i));
	  bleuWeight = lmSum * bleu_weight_lm_factor;
	  cerr << "Set bleu weight to lm weight * " << bleu_weight_lm_factor << endl;
	}

	if (bleuWeight_hope == -1) {
	  bleuWeight_hope = bleuWeight;
	}
	if (bleuWeight_fear == -1) {
	  bleuWeight_fear = bleuWeight;
	}
	bleuWeight_fear *= bleu_weight_fear_factor;
	cerr << "Bleu weight: " << bleuWeight << endl;
	cerr << "Bleu weight fear: " << bleuWeight_fear << endl;

	if (decode_hope || decode_fear || decode_model) {
	  size_t decode = 1;
	  if (decode_fear) decode = 2;
	  if (decode_model) decode = 3;
	  decodeHopeOrFear(rank, size, decode, decode_filename, inputSentences, decoder, n, bleuWeight);
	}

	//Main loop:	
	ScoreComponentCollection cumulativeWeights; // collect weights per epoch to produce an average
	ScoreComponentCollection cumulativeWeightsBinary;
	size_t numberOfUpdates = 0;
	size_t numberOfUpdatesThisEpoch = 0;

	time_t now;
	time(&now);
	cerr << "Rank " << rank << ", " << ctime(&now);

	float avgInputLength = 0;
	float sumOfInputs = 0;
	size_t numberOfInputs = 0;

	ScoreComponentCollection mixedWeights;
	ScoreComponentCollection mixedWeightsPrevious;
	ScoreComponentCollection mixedWeightsBeforePrevious;
	ScoreComponentCollection mixedAverageWeights;
	ScoreComponentCollection mixedAverageWeightsPrevious;
	ScoreComponentCollection mixedAverageWeightsBeforePrevious;

	// log feature counts and/or hope/fear translations with features
	/*string f1 = "decode_hope_epoch0";
	string f2 = "decode_fear_epoch0";
	ofstream hopePlusFeatures(f1.c_str());
	ofstream fearPlusFeatures(f2.c_str());
	if (!hopePlusFeatures || !fearPlusFeatures) {
	  ostringstream msg;
	  msg << "Unable to open file";
	  throw runtime_error(msg.str());
	  }*/

	bool stop = false;
//	int sumStillViolatedConstraints;
	float epsilon = 0.0001;

	// variables for feature confidence
	ScoreComponentCollection confidenceCounts, mixedConfidenceCounts, featureLearningRates;
       	featureLearningRates.UpdateLearningRates(decay_core, decay_sparse, confidenceCounts, core_r0, sparse_r0); //initialise core learning rates
	cerr << "Initial learning rates, core: " << core_r0 << ", sparse: " << sparse_r0 << endl;

	for (size_t epoch = 0; epoch < epochs && !stop; ++epoch) {
	  if (shuffle) {
	    if (trainWithMultipleFolds || rank == 0) {
	      cerr << "Rank " << rank << ", epoch " << epoch << ", shuffling input sentences.." << endl;
	      RandomIndex rindex;
	      random_shuffle(order.begin(), order.end(), rindex);
	    }

#ifdef MPI_ENABLE
	    if (!trainWithMultipleFolds)
	      mpi::broadcast(world, order, 0);
#endif

	    // redo shards 
	    if (trainWithMultipleFolds) {			
	      float shardSize = (float) (order.size())/coresPerFold;
	      size_t shardStart = (size_t) (shardSize * (rank % coresPerFold));
	      size_t shardEnd = shardStart + shardSize;
	      if (rank % coresPerFold == coresPerFold - 1) { // last rank of each fold 
		shardEnd = order.size();
		shardSize = shardEnd - shardStart;
	      }		
	      VERBOSE(1, "Rank: " << rank << ", shard size: " << shardSize << endl);
	      VERBOSE(1, "Rank: " << rank << ", shard start: " << shardStart << " shard end: " << shardEnd << endl);
	      shard.resize(shardSize);
	      copy(order.begin() + shardStart, order.begin() + shardEnd, shard.begin());
	      batchSize = 1;
	    }
	    else {
	      float shardSize = (float) (order.size()) / size;
	      size_t shardStart = (size_t) (shardSize * rank);
	      size_t shardEnd = (size_t) (shardSize * (rank + 1));
	      if (rank == size - 1) {
		shardEnd = order.size();
		shardSize = shardEnd - shardStart;
	      }
	      VERBOSE(1, "Shard size: " << shardSize << endl);
	      VERBOSE(1, "Rank: " << rank << " Shard start: " << shardStart << " Shard end: " << shardEnd << endl);
	      shard.resize(shardSize);
	      copy(order.begin() + shardStart, order.begin() + shardEnd, shard.begin());
	      if (batchEqualsShard)
		batchSize = shardSize;
	    }
	  }
	  
	  // sum of violated constraints in an epoch
	  // sumStillViolatedConstraints = 0;

	  numberOfUpdatesThisEpoch = 0;
	  // Sum up weights over one epoch, final average uses weights from last epoch
	  if (!accumulateWeights) {
	    cumulativeWeights.ZeroAll();
	    cumulativeWeightsBinary.ZeroAll();
	  }
	  
	  // number of weight dumps this epoch
	  size_t weightMixingThisEpoch = 0;
	  size_t weightEpochDump = 0;
	  
	  size_t shardPosition = 0;
	  vector<size_t>::const_iterator sid = shard.begin();
	  while (sid != shard.end()) {
			// feature values for hypotheses i,j (matrix: batchSize x 3*n x featureValues)
			vector<vector<ScoreComponentCollection> > featureValues;
			vector<vector<float> > bleuScores;
			vector<vector<float> > modelScores;

			// variables for hope-fear/perceptron setting
			vector<vector<ScoreComponentCollection> > featureValuesHope, featureValuesHopeSample;
			vector<vector<ScoreComponentCollection> > featureValuesFear, featureValuesFearSample;
			vector<vector<float> > bleuScoresHope, bleuScoresHopeSample;
			vector<vector<float> > bleuScoresFear, bleuScoresFearSample;
			vector<vector<float> > modelScoresHope, modelScoresHopeSample;
			vector<vector<float> > modelScoresFear, modelScoresFearSample;
			
			// get moses weights
			ScoreComponentCollection mosesWeights = decoder->getWeights();
			VERBOSE(1, "\nRank " << rank << ", epoch " << epoch << ", weights: " << mosesWeights << endl);

			// BATCHING: produce nbest lists for all input sentences in batch
			vector<float> oracleBleuScores;
			vector<float> oracleModelScores;
			vector<vector<const Word*> > oneBests;
			vector<ScoreComponentCollection> oracleFeatureValues;
			vector<size_t> inputLengths;
			vector<size_t> ref_ids;
			size_t actualBatchSize = 0;

			string nbestFileMegam, referenceFileMegam;
			vector<size_t>::const_iterator current_sid_start = sid;
			size_t examples_in_batch = 0;
			bool skip_sample = false;
			for (size_t batchPosition = 0; batchPosition < batchSize && sid
			    != shard.end(); ++batchPosition) {
				string input;
				if (trainWithMultipleFolds) 
					input = inputSentencesFolds[myFold][*sid];
				else
					input = inputSentences[*sid];
//				const vector<string>& refs = referenceSentences[*sid];
				cerr << "\nRank " << rank << ", epoch " << epoch << ", input sentence " << *sid << ": \"" << input << "\"" << " (batch pos " << batchPosition << ")" << endl;

				Moses::Sentence *sentence = new Sentence();
			    stringstream in(input + "\n");
			    const vector<FactorType> inputFactorOrder = staticData.GetInputFactorOrder();
			    sentence->Read(in,inputFactorOrder);
			    size_t current_input_length = (*sentence).GetSize();

				if (epoch == 0 && (scaleByAvgInputLength || scaleByAvgInverseLength)) {
					sumOfInputs += current_input_length;
					++numberOfInputs;
					avgInputLength = sumOfInputs/numberOfInputs;
					decoder->setAvgInputLength(avgInputLength);
					cerr << "Rank " << rank << ", epoch 0, average input length: " << avgInputLength << endl;
				}

				vector<ScoreComponentCollection> newFeatureValues;
				vector<float> newScores;
				if (model_hope_fear || rank_only) {
					featureValues.push_back(newFeatureValues);
					bleuScores.push_back(newScores);
					modelScores.push_back(newScores);
				}
				if (hope_fear || hope_model || perceptron_update) {
					featureValuesHope.push_back(newFeatureValues);
					featureValuesFear.push_back(newFeatureValues);
					bleuScoresHope.push_back(newScores);
					bleuScoresFear.push_back(newScores);
					modelScoresHope.push_back(newScores);
					modelScoresFear.push_back(newScores);
					if (historyBleu || debug_model) {
						featureValues.push_back(newFeatureValues);
						bleuScores.push_back(newScores);
						modelScores.push_back(newScores);
					}
				}
				if (sample) {
					featureValuesHopeSample.push_back(newFeatureValues);
					featureValuesFearSample.push_back(newFeatureValues);
					bleuScoresHopeSample.push_back(newScores);
					bleuScoresFearSample.push_back(newScores);
					modelScoresHopeSample.push_back(newScores);
					modelScoresFearSample.push_back(newScores);
				}

				size_t ref_length;
				float avg_ref_length;

				// preparation for MegaM
				if (megam) {
					ostringstream hope_nbest_filename, fear_nbest_filename, model_nbest_filename, ref_filename;
					ofstream dummy;
					cerr << "Generate nbest lists for megam.." << endl;
					hope_nbest_filename << "decode_hope_sent" << *sid << "." << hope_n << "best";
					fear_nbest_filename << "decode_fear_sent" << *sid << "." << fear_n << "best";
					model_nbest_filename << "decode_model_sent" << *sid << "." << n << "best";
					
					cerr << "Writing file " << hope_nbest_filename.str() << endl;
					decoder->outputNBestList(input, *sid, hope_n, 1, bleuWeight_hope, distinctNbest,
							avgRefLength, hope_nbest_filename.str(), dummy);
					decoder->cleanup(chartDecoding);
					
					cerr << "Writing file " << fear_nbest_filename.str() << endl;
					decoder->outputNBestList(input, *sid, fear_n, -1, bleuWeight_fear, distinctNbest,
							avgRefLength, fear_nbest_filename.str(), dummy);
					decoder->cleanup(chartDecoding);
					
					cerr << "Writing file " << model_nbest_filename.str() << endl;
					decoder->outputNBestList(input, *sid, n, 0, bleuWeight, distinctNbest,
							avgRefLength, model_nbest_filename.str(), dummy);
					decoder->cleanup(chartDecoding);

					// save reference
					ref_filename <<  "decode_ref_sent" << *sid;
					referenceFileMegam = ref_filename.str();
					ofstream ref_out(referenceFileMegam.c_str());
					if (!ref_out) {
						ostringstream msg;
						msg << "Unable to open " << referenceFileMegam;
						throw runtime_error(msg.str());
					}
					ref_out << referenceSentences[decoder->getShortestReferenceIndex(*sid)][*sid] << "\n";
					ref_out.close();

					// concatenate nbest files (use hope and fear lists to extract samples from)
					stringstream nbestStreamMegam, catCmd, sortCmd;
					nbestStreamMegam << "decode_hope-model-fear_sent" << *sid << "." << (hope_n+n+fear_n) << "best";
					string tmp = nbestStreamMegam.str();
					//nbestFileMegam = tmp+".sorted";
					nbestFileMegam = tmp;
					catCmd << "cat " << hope_nbest_filename.str() << " " << model_nbest_filename.str() 
							<< " " << fear_nbest_filename.str() << " > " << tmp;
					//sortCmd << "sort -k 1 " << tmp << " > " << nbestFileMegam;
					system(catCmd.str().c_str());
					//system(sortCmd.str().c_str());
				}

				if (print_weights)
				  cerr << "Rank " << rank << ", epoch " << epoch << ", current weights: " << mosesWeights << endl;
				if (print_core_weights) {
				  cerr << "Rank " << rank << ", epoch " << epoch << ", current weights: ";
				  mosesWeights.PrintCoreFeatures(); 
				  cerr << endl;
				}
				 								  
				// check LM weight
				const LMList& lmList_new = staticData.GetLMList();
				for (LMList::const_iterator i = lmList_new.begin(); i != lmList_new.end(); ++i) {
				  float lmWeight = mosesWeights.GetScoreForProducer(*i);
				  cerr << "Rank " << rank << ", epoch " << epoch << ", lm weight: " << lmWeight << endl;
				  if (lmWeight <= 0) {
				    cerr << "Rank " << rank << ", epoch " << epoch << ", ERROR: language model weight should never be <= 0." << endl;
				    mosesWeights.Assign(*i, 0.1);
				    cerr << "Rank " << rank << ", epoch " << epoch << ", assign lm weights of 0.1" << endl;
				  }
				}
				
				// select inference scheme
				cerr << "Rank " << rank << ", epoch " << epoch << ", real Bleu? " << realBleu << endl;
				if (hope_fear || perceptron_update) {				  
				  if (clear_static) {
				    delete decoder;
				    StaticData::ClearDataStatic();
				    decoder = new MosesDecoder(configFile, verbosity, decoder_params.size(), decoder_params);
				    decoder->setBleuParameters(disableBleuFeature, sentenceBleu, scaleByInputLength, scaleByAvgInputLength, scaleByInverseLength, scaleByAvgInverseLength, scaleByX, historySmoothing, bleu_smoothing_scheme);
				    decoder->setWeights(mosesWeights);
				  }    
					
					// HOPE		 				  
					cerr << "Rank " << rank << ", epoch " << epoch << ", " << hope_n << "best hope translations" << endl;
					vector< vector<const Word*> > outputHope = decoder->getNBest(input, *sid, hope_n, 1.0, bleuWeight_hope,
							featureValuesHope[batchPosition], bleuScoresHope[batchPosition], modelScoresHope[batchPosition],
							1, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
					vector<const Word*> oracle = outputHope[0];
					decoder->cleanup(chartDecoding);
					ref_length = decoder->getClosestReferenceLength(*sid, oracle.size());
					avg_ref_length = ref_length;
					float hope_length_ratio = (float)oracle.size()/ref_length;
					int oracleSize = (int)oracle.size();
					cerr << endl;

					// count sparse features occurring in hope translation
					featureValuesHope[batchPosition][0].IncrementSparseHopeFeatures();

					/*if (epoch == 0 && printNbestWithFeatures) {
						decoder->outputNBestList(input, *sid, hope_n, 1, bleuWeight_hope, distinctNbest,
								avgRefLength, "", hopePlusFeatures);
						decoder->cleanup(chartDecoding);
					}*/

					
					float precision = bleuScoresHope[batchPosition][0];
					if (historyBleu) {
					  precision /= decoder->getTargetLengthHistory();
					}
					else {
						if (scaleByAvgInputLength) precision /= decoder->getAverageInputLength();
						else if (scaleByAvgInverseLength) precision /= (100/decoder->getAverageInputLength());
						precision /= scaleByX;
					}
					if (scale_margin_precision || scale_update_precision) {
						if (historyBleu || scaleByAvgInputLength || scaleByAvgInverseLength) {
							cerr << "Rank " << rank << ", epoch " << epoch << ", set hope precision: " << precision << endl;
							((MiraOptimiser*) optimiser)->setPrecision(precision);
						}
					}
				
					vector<const Word*> bestModel;
					if (debug_model || historyBleu) {
						// MODEL (for updating the history only, using dummy vectors)
					  if (clear_static) {
                                            delete decoder;
					    StaticData::ClearDataStatic();
                                            decoder = new MosesDecoder(configFile, verbosity, decoder_params.size(), decoder_params);
                                            decoder->setBleuParameters(disableBleuFeature, sentenceBleu, scaleByInputLength, scaleByAvgInputLength, scaleByInverseLength, scaleByAvgInverseLength, scaleByX, historySmoothing, bleu_smoothing_scheme);
                                            decoder->setWeights(mosesWeights);
                                          }

						cerr << "Rank " << rank << ", epoch " << epoch << ", 1best wrt model score (debug or history)" << endl;
						vector< vector<const Word*> > outputModel = decoder->getNBest(input, *sid, n, 0.0, bleuWeight,
								featureValues[batchPosition], bleuScores[batchPosition], modelScores[batchPosition],
								1, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
						bestModel = outputModel[0];
						decoder->cleanup(chartDecoding);
						cerr << endl;
						ref_length = decoder->getClosestReferenceLength(*sid, bestModel.size());
					}

					// FEAR
					float fear_length_ratio = 0;
					float bleuRatioHopeFear = 0;
					int fearSize = 0;
					if (clear_static) {
						delete decoder;
					    StaticData::ClearDataStatic();
					    decoder = new MosesDecoder(configFile, verbosity, decoder_params.size(), decoder_params);
					    decoder->setBleuParameters(disableBleuFeature, sentenceBleu, scaleByInputLength, scaleByAvgInputLength, scaleByInverseLength, scaleByAvgInverseLength, scaleByX, historySmoothing, bleu_smoothing_scheme);
					    decoder->setWeights(mosesWeights);
					}

					cerr << "Rank " << rank << ", epoch " << epoch << ", " << fear_n << "best fear translations" << endl;
					vector< vector<const Word*> > outputFear = decoder->getNBest(input, *sid, fear_n, -1.0, bleuWeight_fear,
							featureValuesFear[batchPosition], bleuScoresFear[batchPosition], modelScoresFear[batchPosition],
							1, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
					vector<const Word*> fear = outputFear[0];
					decoder->cleanup(chartDecoding);
					ref_length = decoder->getClosestReferenceLength(*sid, fear.size());
					avg_ref_length += ref_length;
					avg_ref_length /= 2;
					fear_length_ratio = (float)fear.size()/ref_length;
					fearSize = (int)fear.size();
					cerr << endl;
					for (size_t i = 0; i < fear.size(); ++i)
						delete fear[i];

					// count sparse features occurring in fear translation
					featureValuesFear[batchPosition][0].IncrementSparseFearFeatures();
					
					/*if (epoch == 0 && printNbestWithFeatures) {
					  decoder->outputNBestList(input, *sid, fear_n, -1, bleuWeight_fear, distinctNbest,
								   avgRefLength, "", fearPlusFeatures);
					  decoder->cleanup(chartDecoding);
					}*/

					// Bleu-related example selection
					bool skip = false;
					bleuRatioHopeFear = bleuScoresHope[batchPosition][0] / bleuScoresFear[batchPosition][0];
					if (minBleuRatio != -1 && bleuRatioHopeFear < minBleuRatio)
						skip = true;
					if(maxBleuRatio != -1 && bleuRatioHopeFear > maxBleuRatio)
						skip = true;					

					// sanity check
					if (historyBleu) {
					  if (bleuScores[batchPosition][0] > bleuScoresHope[batchPosition][0] &&
					      modelScores[batchPosition][0] > modelScoresHope[batchPosition][0]) {
					    if (abs(bleuScores[batchPosition][0] - bleuScoresHope[batchPosition][0]) > epsilon &&
						abs(modelScores[batchPosition][0] - modelScoresHope[batchPosition][0]) > epsilon) {
					      cerr << "Rank " << rank << ", epoch " << epoch << ", ERROR: MODEL translation better than HOPE translation." << endl;
					      skip = true;
					    }
					  }
					  if (bleuScoresFear[batchPosition][0] > bleuScores[batchPosition][0] &&
					      modelScoresFear[batchPosition][0] > modelScores[batchPosition][0]) {
					    if (abs(bleuScoresFear[batchPosition][0] - bleuScores[batchPosition][0]) > epsilon && 
						abs(modelScoresFear[batchPosition][0] - modelScores[batchPosition][0]) > epsilon) {
					      cerr << "Rank " << rank << ", epoch " << epoch << ", ERROR: FEAR translation better than MODEL translation." << endl;
					      skip = true;
					    }					  
					  }
					}
					if (bleuScoresFear[batchPosition][0] > bleuScoresHope[batchPosition][0]) {
					  if (abs(bleuScoresFear[batchPosition][0] - bleuScoresHope[batchPosition][0]) > epsilon) {
					    // check if it's an error or a warning
					    skip = true;
					    if (modelScoresFear[batchPosition][0] > modelScoresHope[batchPosition][0] && abs(modelScoresFear[batchPosition][0] - modelScoresHope[batchPosition][0]) > epsilon) {
					      cerr << "Rank " << rank << ", epoch " << epoch << ", ERROR: FEAR translation better than HOPE translation. (abs-diff: " << abs(bleuScoresFear[batchPosition][0] - bleuScoresHope[batchPosition][0]) << ")" <<endl;			   
					    }
					    else {
					      cerr << "Rank " << rank << ", epoch " << epoch << ", WARNING: FEAR translation has better Bleu than HOPE translation. (abs-diff: " << abs(bleuScoresFear[batchPosition][0] - bleuScoresHope[batchPosition][0]) << ")" <<endl;
					    }
					  }
					}
									
					if (skip) {
					  cerr << "Rank " << rank << ", epoch " << epoch << ", skip example (" << hope_length_ratio << ", " << bleuRatioHopeFear << ").. " << endl;
					  featureValuesHope[batchPosition].clear();
					  featureValuesFear[batchPosition].clear();
					  bleuScoresHope[batchPosition].clear();
					  bleuScoresFear[batchPosition].clear();
					  if (historyBleu || debug_model) {
					    featureValues[batchPosition].clear();
					    bleuScores[batchPosition].clear();
					  }
					}
					else {
					  examples_in_batch++;

					  // needed for history
					  if (historyBleu)  {
					    inputLengths.push_back(current_input_length);
					    ref_ids.push_back(*sid);					
					    oneBests.push_back(bestModel);
					  }  
					}					
				}
				if (hope_model) {
					// HOPE
					cerr << "Rank " << rank << ", epoch " << epoch << ", " << hope_n << "best hope translations" << endl;
					vector< vector<const Word*> > outputHope = decoder->getNBest(input, *sid, hope_n, 1.0, bleuWeight_hope,
							featureValuesHope[batchPosition], bleuScoresHope[batchPosition], modelScoresHope[batchPosition],
							1, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
					vector<const Word*> oracle = outputHope[0];
					decoder->cleanup(chartDecoding);
					cerr << endl;
					
					// count sparse features occurring in hope translation
					featureValuesHope[batchPosition][0].IncrementSparseHopeFeatures();

					vector<const Word*> bestModel;
					// MODEL (for updating the history only, using dummy vectors)
					cerr << "Rank " << rank << ", epoch " << epoch << ", " << fear_n << "best wrt model score" << endl;
					vector< vector<const Word*> > outputModel = decoder->getNBest(input, *sid, fear_n, 0.0, bleuWeight_fear,
							featureValuesFear[batchPosition], bleuScoresFear[batchPosition], modelScoresFear[batchPosition],
							realBleu, 1, distinctNbest, avgRefLength, rank, epoch, "");
					bestModel = outputModel[0];
					decoder->cleanup(chartDecoding);
					cerr << endl;

					// needed for history
					if (historyBleu) {
						inputLengths.push_back(current_input_length);
						ref_ids.push_back(*sid);
						oneBests.push_back(bestModel);
					}						
					
					examples_in_batch++;
				}
				if (rank_only) {
					// MODEL
					cerr << "Rank " << rank << ", epoch " << epoch << ", " << rank_n << "best wrt model score" << endl;
					vector< vector<const Word*> > outputModel = decoder->getNBest(input, *sid, rank_n, 0.0, bleuWeight,
							featureValues[batchPosition], bleuScores[batchPosition], modelScores[batchPosition],
							1, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
					vector<const Word*> bestModel = outputModel[0];
					decoder->cleanup(chartDecoding);
					oneBests.push_back(bestModel);
					ref_length = decoder->getClosestReferenceLength(*sid, bestModel.size());
					float model_length_ratio = (float)bestModel.size()/ref_length;
					cerr << ", l-ratio model: " << model_length_ratio << endl;
					
					// count sparse features occurring in best model translation
					featureValues[batchPosition][0].IncrementSparseHopeFeatures();

					examples_in_batch++;
				}
				if (model_hope_fear) {		
				  // HOPE
				  if (!skip_hope) {
				    cerr << "Rank " << rank << ", epoch " << epoch << ", " << n << "best hope translations" << endl;
				    size_t oraclePos = featureValues[batchPosition].size();
				    decoder->getNBest(input, *sid, n, 1.0, bleuWeight_hope,
						      featureValues[batchPosition], bleuScores[batchPosition], modelScores[batchPosition],
						      0, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
				    //vector<const Word*> oracle = outputHope[0];
				    // needed for history
				    inputLengths.push_back(current_input_length);
				    ref_ids.push_back(*sid);
				    decoder->cleanup(chartDecoding);
				    //ref_length = decoder->getClosestReferenceLength(*sid, oracle.size());
				    //float hope_length_ratio = (float)oracle.size()/ref_length;
				    cerr << endl;
				    
				    oracleFeatureValues.push_back(featureValues[batchPosition][oraclePos]);
				    oracleBleuScores.push_back(bleuScores[batchPosition][oraclePos]);
				    oracleModelScores.push_back(modelScores[batchPosition][oraclePos]);
				  }

				  // MODEL
				  if (!skip_model) {
				    cerr << "Rank " << rank << ", epoch " << epoch << ", " << n << "best wrt model score" << endl;
				    if (historyBleu) {
				      vector< vector<const Word*> > outputModel = decoder->getNBest(input, *sid, n, 0.0, bleuWeight,
					   featureValues[batchPosition], bleuScores[batchPosition], modelScores[batchPosition],
					   1, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
				      vector<const Word*> bestModel = outputModel[0];
				      oneBests.push_back(bestModel);
				    }
				    else {
				      decoder->getNBest(input, *sid, n, 0.0, bleuWeight,
						featureValues[batchPosition], bleuScores[batchPosition], modelScores[batchPosition],
						0, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
				    }
				    decoder->cleanup(chartDecoding);
				    //ref_length = decoder->getClosestReferenceLength(*sid, bestModel.size());
				    //float model_length_ratio = (float)bestModel.size()/ref_length;
				    cerr << endl;
				  }

				  // FEAR			
				  if (!skip_fear) {
				    cerr << "Rank " << rank << ", epoch " << epoch << ", " << n << "best fear translations" << endl;
				    decoder->getNBest(input, *sid, n, -1.0, bleuWeight_fear,
						featureValues[batchPosition], bleuScores[batchPosition], modelScores[batchPosition],
						0, realBleu, distinctNbest, avgRefLength, rank, epoch, "");
				    decoder->cleanup(chartDecoding);
				    //ref_length = decoder->getClosestReferenceLength(*sid, fear.size());
				    //float fear_length_ratio = (float)fear.size()/ref_length;
				  }

				  examples_in_batch++;
				  
				  if (sample) {
				    float bleuHope = -1000;
				    float bleuFear = 1000;
				    size_t indexHope = -1;
				    size_t indexFear = -1;
				    vector<float> bleuHopeList;
				    vector<float> bleuFearList;
				    vector<float> indexHopeList;
				    vector<float> indexFearList;
				    
				    HypothesisQueue queueHope(hope_n);
				    HypothesisQueue queueFear(fear_n);
				    
				    cerr << endl;					    
				    if (most_violated || all_violated || one_against_all) {
				      bleuHope = -1000;
				      bleuFear = 1000;
				      indexHope = -1;
				      indexFear = -1;
				      if (most_violated)
					cerr << "Rank " << rank << ", epoch " << epoch << ", pick pair with most violated constraint" << endl;
				      else if (all_violated)
					cerr << "Rank " << rank << ", epoch " << epoch << ", pick all pairs with violated constraints";
				      else 
					cerr << "Rank " << rank << ", epoch " << epoch << ", pick all pairs with hope";
					    
				      // find best hope, then find fear that violates our constraint most
				      for (size_t i=0; i<bleuScores[batchPosition].size(); ++i) {
					if (abs(bleuScores[batchPosition][i] - bleuHope) < epsilon) { // equal bleu scores          
					  if (modelScores[batchPosition][i] > modelScores[batchPosition][indexHope]) {
					    if (abs(modelScores[batchPosition][i] - modelScores[batchPosition][indexHope]) > epsilon) {
					      // better model score
					      bleuHope = bleuScores[batchPosition][i];
					      indexHope = i;
					    }
					  }
					}
					else if (bleuScores[batchPosition][i] > bleuHope) { // better than current best         
					  bleuHope = bleuScores[batchPosition][i];
					  indexHope = i;
					}
				      }
					    
				      float currentViolation = 0;
				      float minimum_bleu_diff = 0.01;
				      for (size_t i=0; i<bleuScores[batchPosition].size(); ++i) {
					float bleuDiff = bleuHope - bleuScores[batchPosition][i];
					float modelDiff = modelScores[batchPosition][indexHope] - modelScores[batchPosition][i];
					if (bleuDiff > epsilon) {
					  if (one_against_all && bleuDiff > minimum_bleu_diff) {
					    cerr << ".. adding pair";
					    bleuHopeList.push_back(bleuHope);
					    bleuFearList.push_back(bleuScores[batchPosition][i]);
					    indexHopeList.push_back(indexHope);
					    indexFearList.push_back(i);
					  }
					  else if (modelDiff < bleuDiff) {
					    float diff = bleuDiff - modelDiff;
					    if (diff > epsilon) { 
					      if (all_violated) {
						cerr << ".. adding pair";
						bleuHopeList.push_back(bleuHope);
						bleuFearList.push_back(bleuScores[batchPosition][i]);
						indexHopeList.push_back(indexHope);
						indexFearList.push_back(i);
					      }
					      else if (most_violated && diff > currentViolation) {
						currentViolation = diff;
						bleuFear = bleuScores[batchPosition][i];
						indexFear = i;
						cerr << "Rank " << rank << ", epoch " << epoch << ", current violation: " << currentViolation << " (" << modelDiff << " >= " << bleuDiff << ")" << endl;
					      }						    
					    }
					  }
					}						
				      }
					    
				      if (most_violated) {
					if (currentViolation > 0) {
					  cerr << "Rank " << rank << ", epoch " << epoch << ", adding pair with violation " << currentViolation << endl;
					  bleuHopeList.push_back(bleuHope);
					  bleuFearList.push_back(bleuFear);
					  indexHopeList.push_back(indexHope);
					  indexFearList.push_back(indexFear);
					}
					else 
					  cerr << "Rank " << rank << ", epoch " << epoch << ", no violated constraint found." << endl;
				      }
				      else cerr << endl;
				    }
				    if (max_bleu_diff) {
				      cerr << "Rank " << rank << ", epoch " << epoch << ", pick pair with max Bleu diff from list: " << bleuScores[batchPosition].size() << endl;
				      for (size_t i=0; i<bleuScores[batchPosition].size(); ++i) {
					BleuIndexPair hope(bleuScores[batchPosition][i], i);
					queueHope.Push(hope);
					BleuIndexPair fear(-1*(bleuScores[batchPosition][i]), i);
					queueFear.Push(fear);
				      }				      
				    }
				    
				    cerr << endl;
				    
				    vector<BleuIndexPair> hopeList, fearList;
				    for (size_t i=0; i<hope_n && !queueHope.Empty(); ++i) hopeList.push_back(queueHope.Pop());
				    for (size_t i=0; i<fear_n && !queueFear.Empty(); ++i) fearList.push_back(queueFear.Pop());
				    
				    for (size_t i=0; i<hopeList.size(); ++i) {
				      float hopeBleu = hopeList[i].first;
				      size_t hopeIndex = hopeList[i].second;
				      for (size_t j=0; j<fearList.size(); ++j) {
					float fearBleu = -1*(fearList[j].first);
					size_t fearIndex = fearList[j].second;
					cerr << "Rank " << rank << ", epoch " << epoch << ", hope: " << hopeBleu << " (" << hopeIndex  << "), fear: " << fearBleu << " (" << fearIndex << ")" << endl;
					bleuScoresHopeSample[batchPosition].push_back(hopeBleu);                                     
					bleuScoresFearSample[batchPosition].push_back(fearBleu);                                       
					featureValuesHopeSample[batchPosition].push_back(featureValues[batchPosition][hopeIndex]);       
					featureValuesFearSample[batchPosition].push_back(featureValues[batchPosition][fearIndex]);       
					modelScoresHopeSample[batchPosition].push_back(modelScores[batchPosition][hopeIndex]);           
					modelScoresFearSample[batchPosition].push_back(modelScores[batchPosition][fearIndex]); 
					
					featureValues[batchPosition][hopeIndex].IncrementSparseHopeFeatures();                 
					featureValues[batchPosition][fearIndex].IncrementSparseFearFeatures();
				      }
				    }
				    if (!makePairs)
				      cerr << "Rank " << rank << ", epoch " << epoch << "summing up hope and fear vectors, no pairs" << endl;
				  }
				}
							  				  
				// next input sentence
				++sid;
				++actualBatchSize;
				++shardPosition;
			} // end of batch loop

			if (megam) {
				// extract features and scores
				string scoreDataFile = "decode_hope-model-fear.scores.dat";
				string featureDataFile = "decode_hope-model-fear.features.dat";
				stringstream extractorCmd;
				//extractorCmd << "/afs/inf.ed.ac.uk/user/s07/s0787953/mosesdecoder_github_saxnot/dist/bin/extractor"
				extractorCmd << "/home/eva/mosesdecoder_github_saxnot/dist/bin/extractor"
						" --scconfig case:true --scfile " << scoreDataFile << " --ffile " << featureDataFile <<
						" -r " << referenceFileMegam << " -n " << nbestFileMegam;
				system(extractorCmd.str().c_str());
				
				/*
				// pro --> select training examples
				stringstream proCmd;
				string proDataFile = "decode_hope-fear.pro.data";
				//proCmd << "/afs/inf.ed.ac.uk/user/s07/s0787953/mosesdecoder_github_saxnot/dist/bin/pro"
				proCmd << "/home/eva/mosesdecoder_github_saxnot/dist/bin/pro"
						" --ffile " << featureDataFile << " --scfile " << scoreDataFile << " -o " << proDataFile;
				system(proCmd.str().c_str());

				// megam
				stringstream megamCmd;
				string megamOut = "decode_hope-fear.megam-weights";
				string megamErr = "decode_hope-fear.megam-log";
				//megamCmd << "/afs/inf.ed.ac.uk/user/s07/s0787953/mosesdecoder_github_saxnot/mert/megam_i686.opt"
				megamCmd << "/home/eva/mosesdecoder_github_saxnot/mert/megam_i686.opt"
						" -fvals -maxi 30 -nobias binary " << proDataFile << " 1> " << megamOut <<
						" 2> " << megamErr;
				system(megamCmd.str().c_str());

				// read feature order
				ifstream f_in(featureDataFile.c_str());
				if (!f_in)
					return false;
				string line;
				vector<string> coreFeats;
				if (getline(f_in, line)) {
					line = boost::algorithm::replace_all_copy(line, "  ", " ");
					boost::split(coreFeats, line, boost::is_any_of(" "));
				}
				else {
					cerr << "Error.." << endl;
					exit(1);
				}
				for (size_t i=0; i < coreFeats.size(); ++i)
					cerr << "from feature data file: " << coreFeats[i] << endl;

				// read megam optimized weights
				ifstream w_in(megamOut.c_str());
				if (!w_in)
					return false;

				while (getline(w_in, line)) {
					vector<string> keyValue;

					boost::split(keyValue, line, boost::is_any_of("\t "));

					// regular features
					if (keyValue[0].substr(0, 1).compare("F") == 0) {
						cerr << "core weight: " << keyValue[0] << " " << keyValue[1] << endl;
						//$WEIGHT[$1] = $2;
						//$sum += abs($2);
					}
					// sparse features
					else {
						//$$sparse_weights{$1} = $2;
						cerr << "sparse weight: " << keyValue[0] << " " << keyValue[1] << endl;
					}
				}

				// normalize weights
				//foreach (@WEIGHT) { $_ /= $sum; }
		    //foreach (keys %{$sparse_weights}) { $$sparse_weights{$_} /= $sum; } */

			}
			else if (examples_in_batch == 0 || (sample && skip_sample)) {
			  cerr << "Rank " << rank << ", epoch " << epoch << ", batch is empty." << endl;
			}
			else {
				vector<vector<float> > losses(actualBatchSize);
				if (model_hope_fear && !skip_hope) {
					// Set loss for each sentence as BLEU(oracle) - BLEU(hypothesis)
					for (size_t batchPosition = 0; batchPosition < actualBatchSize; ++batchPosition) {
						for (size_t j = 0; j < bleuScores[batchPosition].size(); ++j) {
							losses[batchPosition].push_back(oracleBleuScores[batchPosition] - bleuScores[batchPosition][j]);
						}
					}
				}
				
				// set weight for bleu feature to 0 before optimizing
				vector<const ScoreProducer*>::const_iterator iter;
				const vector<const ScoreProducer*> featureFunctions2 = staticData.GetTranslationSystem(TranslationSystem::DEFAULT).GetFeatureFunctions();
				for (iter = featureFunctions2.begin(); iter != featureFunctions2.end(); ++iter) {
				  if ((*iter)->GetScoreProducerWeightShortName() == "bl") {
				    mosesWeights.Assign(*iter, 0);
				    break;
				  }
				}

				// scale LM feature (to avoid rapid changes)
				if (scale_lm) {
				  cerr << "scale lm" << endl;
				  const LMList& lmList_new = staticData.GetLMList();
				  for (LMList::const_iterator iter = lmList_new.begin(); iter != lmList_new.end(); ++iter) {
				    // scale down score
				    if (sample) {
				    	scaleFeatureScore(*iter, scale_lm_factor, featureValuesHopeSample, rank, epoch);
				    	scaleFeatureScore(*iter, scale_lm_factor, featureValuesFearSample, rank, epoch);
				    }
				    else {
				    	scaleFeatureScore(*iter, scale_lm_factor, featureValuesHope, rank, epoch);
				    	scaleFeatureScore(*iter, scale_lm_factor, featureValuesFear, rank, epoch);
				    	scaleFeatureScore(*iter, scale_lm_factor, featureValues, rank, epoch);
				    }
				  }
				}

				// scale WP
				if (scale_wp) {
				  // scale up weight  
				  WordPenaltyProducer *wp = staticData.GetFirstWordPenaltyProducer();

				  // scale down score
				  if (sample) {
				    scaleFeatureScore(wp, scale_wp_factor, featureValuesHopeSample, rank, epoch);
				    scaleFeatureScore(wp, scale_wp_factor, featureValuesFearSample, rank, epoch);
				  }
				  else {
				    scaleFeatureScore(wp, scale_wp_factor, featureValuesHope, rank, epoch);
				    scaleFeatureScore(wp, scale_wp_factor, featureValuesFear, rank, epoch);
				    scaleFeatureScore(wp, scale_wp_factor, featureValues, rank, epoch);
				  }
				}

				if (scale_all) {
				  // scale distortion
				  DistortionScoreProducer *dp = staticData.GetDistortionScoreProducer();
				  
                                  // scale down score                                                                                      
                                  if (sample) {
                                    scaleFeatureScore(dp, scale_all_factor, featureValuesHopeSample, rank, epoch);
                                    scaleFeatureScore(dp, scale_all_factor, featureValuesFearSample, rank, epoch);
                                  }
                                  else {
                                    scaleFeatureScore(dp, scale_all_factor, featureValuesHope, rank, epoch);
                                    scaleFeatureScore(dp, scale_all_factor, featureValuesFear, rank, epoch);
                                    scaleFeatureScore(dp, scale_all_factor, featureValues, rank, epoch);
                                  }

				  // scale lexical reordering models
				  vector<LexicalReordering*> lrVec = staticData.GetLexicalReorderModels();
                                  for (size_t i=0; i<lrVec.size(); ++i) {
				    LexicalReordering* lr = lrVec[i];
				   
				    // scale down score                                                                                  
				    if (sample) {
				      scaleFeatureScores(lr, scale_all_factor, featureValuesHopeSample, rank, epoch);
				      scaleFeatureScores(lr, scale_all_factor, featureValuesFearSample, rank, epoch);
				    }
				    else {
				      scaleFeatureScores(lr, scale_all_factor, featureValuesHope, rank, epoch);
				      scaleFeatureScores(lr, scale_all_factor, featureValuesFear, rank, epoch);
				      scaleFeatureScores(lr, scale_all_factor, featureValues, rank, epoch);
				    }
				  }
				  
				  // scale phrase table models
				  vector<PhraseDictionaryFeature*> pdVec = staticData.GetPhraseDictionaryModels();
                                  for (size_t i=0; i<pdVec.size(); ++i) {
				    PhraseDictionaryFeature* pd = pdVec[i];

                                    // scale down score                                                                                     
                                    if (sample) {
                                      scaleFeatureScores(pd, scale_all_factor, featureValuesHopeSample, rank, epoch);
                                      scaleFeatureScores(pd, scale_all_factor, featureValuesFearSample, rank, epoch);
                                    }
                                    else {
                                      scaleFeatureScores(pd, scale_all_factor, featureValuesHope, rank, epoch);
                                      scaleFeatureScores(pd, scale_all_factor, featureValuesFear, rank, epoch);
                                      scaleFeatureScores(pd, scale_all_factor, featureValues, rank, epoch);
                                    }
                                  }
				}
					
				// print out the feature values
				if (print_feature_values) {
					cerr << "\nRank " << rank << ", epoch " << epoch << ", feature values: " << endl;
					if (sample) {
						cerr << "hope: " << endl;
						printFeatureValues(featureValuesHopeSample);
						cerr << "fear: " << endl;
						printFeatureValues(featureValuesFearSample);
					}
					else if (model_hope_fear || rank_only) printFeatureValues(featureValues);
					else {
						cerr << "hope: " << endl;
						printFeatureValues(featureValuesHope);
						cerr << "fear: " << endl;
						printFeatureValues(featureValuesFear);
					}
				}

				// apply learning rates to feature vectors before optimization
				if (feature_confidence) {
				  cerr << "Rank " << rank << ", epoch " << epoch << ", apply feature learning rates with decays " << decay_core << "/" << decay_sparse << ": " << featureLearningRates << endl;
				  if (sample) {
				    cerr << "Rank " << rank << ", epoch " << epoch << ", feature values before: " << featureValuesHopeSample[0][0] << endl;
				    applyPerFeatureLearningRates(featureValuesHopeSample, featureLearningRates, sparse_r0);
				    cerr << "Rank " << rank << ", epoch " << epoch << ", feature values after: " << featureValuesHopeSample[0][0] << endl;
				    applyPerFeatureLearningRates(featureValuesFearSample, featureLearningRates, sparse_r0);
				  }
				  else {
				    applyPerFeatureLearningRates(featureValuesHope, featureLearningRates, sparse_r0);
				    applyPerFeatureLearningRates(featureValuesFear, featureLearningRates, sparse_r0);
				    applyPerFeatureLearningRates(featureValues, featureLearningRates, sparse_r0);
				  }			  
				}
				else {
				  // apply fixed learning rates
				  cerr << "Rank " << rank << ", epoch " << epoch << ", apply fixed learning rates, core: " << core_r0 << ", sparse: " << sparse_r0 << endl;
				  if (core_r0 != 1.0 || sparse_r0 != 1.0) {
				    if (sample) {
				      cerr << "Rank " << rank << ", epoch " << epoch << ", feature values before: " << featureValuesHopeSample[0][0] << endl;
				      applyLearningRates(featureValuesHopeSample, core_r0, sparse_r0);
				      cerr << "Rank " << rank << ", epoch " << epoch << ", feature values after: " << featureValuesHopeSample[0][0] << endl;
				      applyLearningRates(featureValuesFearSample, core_r0, sparse_r0);
                                    }
                                    else {
                                      applyLearningRates(featureValuesHope, core_r0, sparse_r0);
				      applyLearningRates(featureValuesFear, core_r0, sparse_r0);
                                      applyLearningRates(featureValues, core_r0, sparse_r0);
                                    }
				  }
				}

				// if we scaled up the weights, scale down model scores now
				if (scaling_constant != 1.0) {
				  if (hope_fear || hope_model || perceptron_update) {
				    for (size_t i = 0; i < modelScoresHope.size(); ++i)
				      for (size_t j = 0; j < modelScoresHope[i].size(); ++j) {
					modelScoresHope[i][j] /= scaling_constant;
					modelScoresFear[i][j] /= scaling_constant;
				      }
				  }
				  else if (model_hope_fear || rank_only) {
				    if (sample) {
				      cerr << "Rank " << rank << ", epoch " << epoch << ", scale down model scores for sampling.. " << endl;
				      for (size_t i = 0; i < modelScoresHopeSample.size(); ++i)
					for (size_t j = 0; j < modelScoresHopeSample[i].size(); ++j) {
					  modelScoresHopeSample[i][j] /= scaling_constant;
					  modelScoresFearSample[i][j] /= scaling_constant;
					}
				    }
				    else { 
				      for (size_t i = 0; i < modelScores.size(); ++i)
                                        for (size_t j = 0; j < modelScores[i].size(); ++j) 
                                          modelScores[i][j] /= scaling_constant;
				    }
				  }
				}

				// Run optimiser on batch:
				VERBOSE(1, "\nRank " << rank << ", epoch " << epoch << ", run optimiser:" << endl);
				size_t update_status = 1;
				ScoreComponentCollection weightUpdate;
				if (perceptron_update) {
					vector<vector<float> > dummy1;
					update_status = optimiser->updateWeightsHopeFear( weightUpdate,
							featureValuesHope, featureValuesFear, dummy1, dummy1, dummy1, dummy1, learning_rate, rank, epoch);
				}
				else if (hope_fear || hope_model) {
					if (bleuScoresHope[0][0] >= min_oracle_bleu) {
						if (hope_n == 1 && fear_n ==1 && batchSize == 1 && !hildreth) {
							update_status = ((MiraOptimiser*) optimiser)->updateWeightsAnalytically(weightUpdate,
									featureValuesHope[0][0], featureValuesFear[0][0], bleuScoresHope[0][0], bleuScoresFear[0][0],
									modelScoresHope[0][0], modelScoresFear[0][0], learning_rate, rank, epoch);
						}
						else 						  
						  update_status = optimiser->updateWeightsHopeFear(weightUpdate,
									featureValuesHope, featureValuesFear, bleuScoresHope, bleuScoresFear,
									modelScoresHope, modelScoresFear, learning_rate, rank, epoch);				
					}
					else
						update_status = 1;
				}
				else if (rank_only) {
				  // learning ranking of model translations
				  if (summed)
				    update_status = ((MiraOptimiser*) optimiser)->updateWeightsRankModelSummed(weightUpdate,
							featureValues, bleuScores, modelScores, learning_rate, rank, epoch);
				  else
				    update_status = ((MiraOptimiser*) optimiser)->updateWeightsRankModel(weightUpdate,
							featureValues, bleuScores, modelScores, learning_rate, rank, epoch);
				}
				else {
					// model_hope_fear
					if (sample) {
					  if (selective) 
					    update_status = ((MiraOptimiser*)optimiser)->updateWeightsHopeFearSelective(weightUpdate, 
							     featureValuesHopeSample, featureValuesFearSample, 
							     bleuScoresHopeSample, bleuScoresFearSample, modelScoresHopeSample, 
							     modelScoresFearSample, learning_rate, rank, epoch);
					  else if (summed)
					    update_status = ((MiraOptimiser*)optimiser)->updateWeightsHopeFearSummed(weightUpdate,
							     featureValuesHopeSample, featureValuesFearSample,
							     bleuScoresHopeSample, bleuScoresFearSample, modelScoresHopeSample,
							     modelScoresFearSample, learning_rate, rank, epoch, rescaleSlack, rewardHope, makePairs);
					  else {
					    if (batchSize == 1 && featureValuesHopeSample[0].size() == 1 && !hildreth) {
					      cerr << "Rank " << rank << ", epoch " << epoch << ", model score hope: " << modelScoresHopeSample[0][0] << endl;
					      cerr << "Rank " << rank << ", epoch " << epoch << ", model score fear: " << modelScoresFearSample[0][0] << endl;
					      update_status = ((MiraOptimiser*) optimiser)->updateWeightsAnalytically(weightUpdate, 
                                                             featureValuesHopeSample[0][0], featureValuesFearSample[0][0], 
							     bleuScoresHopeSample[0][0], bleuScoresFearSample[0][0], 
							     modelScoresHopeSample[0][0], modelScoresFearSample[0][0], 
							     learning_rate, rank, epoch);
					    }
					    else {
					      cerr << "Rank " << rank << ", epoch " << epoch << ", model score hope: " << modelScoresHopeSample[0][0] << endl;
                                              cerr << "Rank " << rank << ", epoch " << epoch << ", model score fear: " << modelScoresFearSample[0][0] << endl;
					      update_status = optimiser->updateWeightsHopeFear(weightUpdate,
								featureValuesHopeSample, featureValuesFearSample, 
								bleuScoresHopeSample, bleuScoresFearSample,
								modelScoresHopeSample, modelScoresFearSample, learning_rate, rank, epoch);
					    }
					  }
					}
					else {
					  if (summed) {
					    // don't differentiate between hope and model/fear, treat all the same and sum constraints
					    update_status = ((MiraOptimiser*) optimiser)->updateWeightsRankModelSummed(weightUpdate,
							     featureValues, bleuScores, modelScores, learning_rate, rank, epoch);
					  }
					  else
					    update_status = ((MiraOptimiser*) optimiser)->updateWeights(weightUpdate,
							     featureValues, losses, bleuScores, modelScores, oracleFeatureValues, oracleBleuScores, oracleModelScores, learning_rate, rank, epoch);
					}
				}

//			sumStillViolatedConstraints += update_status;

				if (update_status == 0) {	 // if weights were updated
					// apply weight update
				        cerr << "Rank " << rank << ", epoch " << epoch << ", update: " << weightUpdate << endl;
					
					if (feature_confidence) {
					  // update confidence counts based on weight update
					  confidenceCounts.UpdateConfidenceCounts(weightUpdate, signed_counts);
					  				  
					  // update feature learning rates
					  featureLearningRates.UpdateLearningRates(decay_core, decay_sparse, confidenceCounts, core_r0, sparse_r0); 
					}

					mosesWeights.PlusEquals(weightUpdate);

					if (normaliseWeights)
					  mosesWeights.L1Normalise();

					cumulativeWeights.PlusEquals(mosesWeights);
					if (sparseAverage) {
					  ScoreComponentCollection binary;
					  binary.SetToBinaryOf(mosesWeights);
					  cumulativeWeightsBinary.PlusEquals(binary);
					}

					++numberOfUpdates;
					++numberOfUpdatesThisEpoch;
					if (averageWeights) {
						ScoreComponentCollection averageWeights(cumulativeWeights);
						if (accumulateWeights) {
							averageWeights.DivideEquals(numberOfUpdates);
						} else {
							averageWeights.DivideEquals(numberOfUpdatesThisEpoch);
						}

						mosesWeights = averageWeights;
					}

					// set new Moses weights
					decoder->setWeights(mosesWeights);
					cerr << "Rank " << rank << ", epoch " << epoch << ", new weights: " << mosesWeights << endl;

					// adjust bleu weight
					if (bleu_weight_lm_adjust) {
					  float lmSum = 0;
					  const LMList& lmList_new = staticData.GetLMList();
					  for (LMList::const_iterator i = lmList_new.begin(); i != lmList_new.end(); ++i)
					    lmSum += abs(mosesWeights.GetScoreForProducer(*i));
					  bleuWeight = lmSum * bleu_weight_lm_factor;
					  cerr << "Rank " << rank << ", epoch " << epoch << ", adjusting Bleu weight to " << bleuWeight << " (factor " << bleu_weight_lm_factor << ")" << endl;
					  
					  if (bleuWeight_hope == -1) {
					    bleuWeight_hope = bleuWeight;
					  }
					  if (bleuWeight_fear == -1) {
					    bleuWeight_fear = bleuWeight;
					  }
					}
				}

				// update history (for approximate document Bleu)
				if (historyBleu) {
					for (size_t i = 0; i < oneBests.size(); ++i) 
						cerr << "Rank " << rank << ", epoch " << epoch << ", update history with 1best length: " << oneBests[i].size() << " ";
					decoder->updateHistory(oneBests, inputLengths, ref_ids, rank, epoch);
					deleteTranslations(oneBests);
				}				
			} // END TRANSLATE AND UPDATE BATCH

			size_t mixing_base = mixingFrequency == 0 ? 0 : shard.size() / mixingFrequency;
			size_t dumping_base = weightDumpFrequency ==0 ? 0 : shard.size() / weightDumpFrequency;
			bool mix = evaluateModulo(shardPosition, mixing_base, actualBatchSize);

			// mix weights?
			if (mix) {
#ifdef MPI_ENABLE
			  cerr << "Rank " << rank << ", epoch " << epoch << ", mixing weights.. " << endl;
				// collect all weights in mixedWeights and divide by number of processes
				mpi::reduce(world, mosesWeights, mixedWeights, SCCPlus(), 0);

				// mix confidence counts
				//mpi::reduce(world, confidenceCounts, mixedConfidenceCounts, SCCPlus(), 0);
				ScoreComponentCollection totalBinary;
				if (sparseAverage) {
					ScoreComponentCollection binary;
					binary.SetToBinaryOf(mosesWeights);
					mpi::reduce(world, binary, totalBinary, SCCPlus(), 0);
				}
				if (rank == 0) {
					// divide by number of processes
					if (sparseNoAverage)
					  mixedWeights.CoreDivideEquals(size); // average only core weights
					else if (sparseAverage)
					  mixedWeights.DivideEquals(totalBinary);
					else
					  mixedWeights.DivideEquals(size);
					
					// divide confidence counts
					//mixedConfidenceCounts.DivideEquals(size);
					
					// normalise weights after averaging
					if (normaliseWeights) {
						mixedWeights.L1Normalise();
					}
								
					++weightMixingThisEpoch;
					
					if (pruneZeroWeights) {
					  size_t pruned = mixedWeights.PruneZeroWeightFeatures();
					  cerr << "Rank " << rank << ", epoch " << epoch << ", " 
					       << pruned << " zero-weighted features pruned from mixedWeights." << endl;
					  
					  pruned = cumulativeWeights.PruneZeroWeightFeatures();
					  cerr << "Rank " << rank << ", epoch " << epoch << ", " 
					       << pruned << " zero-weighted features pruned from cumulativeWeights." << endl;
					}
					
					if (featureCutoff != -1 && weightMixingThisEpoch == mixingFrequency) {
					  size_t pruned = mixedWeights.PruneSparseFeatures(featureCutoff);
					  cerr << "Rank " << rank << ", epoch " << epoch << ", " 
					       << pruned << " features pruned from mixedWeights." << endl;
						
					  pruned = cumulativeWeights.PruneSparseFeatures(featureCutoff);
					  cerr << "Rank " << rank << ", epoch " << epoch << ", " 
					       << pruned << " features pruned from cumulativeWeights." << endl;
					}
					
					if (weightMixingThisEpoch == mixingFrequency) {
					  if (l1_regularize) { 
					    size_t pruned = mixedWeights.SparseL1Regularize(l1_lambda);
					    cerr << "Rank " << rank << ", epoch " << epoch << ", " 
					       << "l1-reg. on mixedWeights with lambda=" << l1_lambda << ", pruned: " << pruned << endl;
					  					  }
					  if (l2_regularize) {
					    mixedWeights.SparseL2Regularize(l2_lambda);
					    cerr << "Rank " << rank << ", epoch " << epoch << ", " 
						 << "l2-reg. on mixedWeights with lambda=" << l2_lambda << endl;  
					  }
					}										
				}

				// broadcast average weights from process 0
				mpi::broadcast(world, mixedWeights, 0);
				decoder->setWeights(mixedWeights);
				mosesWeights = mixedWeights;

				// broadcast summed confidence counts
				//mpi::broadcast(world, mixedConfidenceCounts, 0);
				//confidenceCounts = mixedConfidenceCounts;
				
#endif
#ifndef MPI_ENABLE
				//cerr << "\nRank " << rank << ", no mixing, weights: " << mosesWeights << endl;
				mixedWeights = mosesWeights;
#endif
			} // end mixing

			// Dump weights?
			if (dumpMixedWeights) {
			  if (mix && rank == 0 && !weightDumpStem.empty()) {
			    // dump mixed weights instead of average weights
			    ostringstream filename;
			    if (epoch < 10)
				  filename << weightDumpStem << "_0" << epoch;
			    else
				  filename << weightDumpStem << "_" << epoch;

			    if (weightDumpFrequency > 1)
				  filename << "_" << weightEpochDump;

			    cerr << "Dumping mixed weights during epoch " << epoch << " to " << filename.str() << endl << endl;
			    mixedWeights.Save(filename.str());
			    ++weightEpochDump;
			  }
			}
			else {
			if (evaluateModulo(shardPosition, dumping_base, actualBatchSize)) {
			  ScoreComponentCollection tmpAverageWeights(cumulativeWeights);
			  bool proceed = false;
			  if (accumulateWeights) {
			    if (numberOfUpdates > 0) {
			      tmpAverageWeights.DivideEquals(numberOfUpdates);
			      proceed = true;
			    }
			  } else {
			    if (numberOfUpdatesThisEpoch > 0) {
			    	if (sparseNoAverage) // average only core weights
			    		tmpAverageWeights.CoreDivideEquals(numberOfUpdatesThisEpoch);
			    	else if (sparseAverage)
			    		tmpAverageWeights.DivideEquals(cumulativeWeightsBinary);
			    	else
			    		tmpAverageWeights.DivideEquals(numberOfUpdatesThisEpoch);
			    	proceed = true;
			    }
			  }
			  
			  if (proceed) {
#ifdef MPI_ENABLE
			    // average across processes
			    mpi::reduce(world, tmpAverageWeights, mixedAverageWeights, SCCPlus(), 0);
			    ScoreComponentCollection totalBinary;
			    if (sparseAverage) {
			      ScoreComponentCollection binary;
			      binary.SetToBinaryOf(mosesWeights);
			      mpi::reduce(world, binary, totalBinary, SCCPlus(), 0);
			    }
#endif
#ifndef MPI_ENABLE
			    mixedAverageWeights = tmpAverageWeights;
#endif
			    if (rank == 0 && !weightDumpStem.empty()) {
			      // divide by number of processes
			      if (sparseNoAverage)
			    	  mixedAverageWeights.CoreDivideEquals(size); // average only core weights
			      else if (sparseAverage)
			    	  mixedAverageWeights.DivideEquals(totalBinary);
			      else
			    	  mixedAverageWeights.DivideEquals(size);

			      // normalise weights after averaging
			      if (normaliseWeights) {
			      	mixedAverageWeights.L1Normalise();
			      }
			      
			      // dump final average weights
			      ostringstream filename;
			      if (epoch < 10) {
			      	filename << weightDumpStem << "_0" << epoch;
			      } else {
			      	filename << weightDumpStem << "_" << epoch;
			      }
			      
			      if (weightDumpFrequency > 1) {
			      	filename << "_" << weightEpochDump;
			      }
			      
/*			      if (accumulateWeights) {
				  	cerr << "\nMixed average weights (cumulative) during epoch "	<< epoch << ": " << mixedAverageWeights << endl;
			      } else {
				  	cerr << "\nMixed average weights during epoch " << epoch << ": " << mixedAverageWeights << endl;
			      }*/
			      
			      cerr << "Dumping mixed average weights during epoch " << epoch << " to " << filename.str() << endl << endl;
			      mixedAverageWeights.Save(filename.str());
			      ++weightEpochDump;
			      
			      if (weightEpochDump == weightDumpFrequency) {
				if (l1_regularize) { 
				  size_t pruned = mixedAverageWeights.SparseL1Regularize(l1_lambda);
				  cerr << "Rank " << rank << ", epoch " << epoch << ", " 
				       << "l1-reg. on mixedAverageWeights with lambda=" << l1_lambda << ", pruned: " << pruned << endl;
				}
				if (l2_regularize) {
				  mixedAverageWeights.SparseL2Regularize(l2_lambda);
				  cerr << "Rank " << rank << ", epoch " << epoch << ", " 
				       << "l2-reg. on mixedAverageWeights with lambda=" << l2_lambda << endl;  
				}
				
				if (l1_regularize || l2_regularize) {
				  filename << "_reg";
				  cerr << "Dumping regularized mixed average weights during epoch " << epoch << " to " << filename.str() << endl << endl;
				  mixedAverageWeights.Save(filename.str());
				}
			      }										

			      if (weightEpochDump == weightDumpFrequency && printFeatureCounts) {
			      	// print out all features with counts
			      	stringstream s1, s2;
			      	s1 << "sparse_feature_hope_counts" << "_" << epoch;
			      	s2 << "sparse_feature_fear_counts" << "_" << epoch;
			      	ofstream sparseFeatureCountsHope(s1.str().c_str());
			      	ofstream sparseFeatureCountsFear(s2.str().c_str());

			      	mixedAverageWeights.PrintSparseHopeFeatureCounts(sparseFeatureCountsHope);
			      	mixedAverageWeights.PrintSparseFearFeatureCounts(sparseFeatureCountsFear);
			      	sparseFeatureCountsHope.close();
			      	sparseFeatureCountsFear.close();
			      }
			    }
			  }
			}// end dumping
			}

		} // end of shard loop, end of this epoch

	        /*if (printNbestWithFeatures && rank == 0 && epoch == 0) {
		  cerr << "Writing out hope/fear nbest list with features: " << f1 << ", " << f2 << endl;
		  hopePlusFeatures.close();
		  fearPlusFeatures.close();
		}*/

		if (historyBleu) {
			cerr << "Bleu feature history after epoch " <<  epoch << endl;
			decoder->printBleuFeatureHistory(cerr);
		}
//		cerr << "Rank " << rank << ", epoch " << epoch << ", sum of violated constraints: " << sumStillViolatedConstraints << endl;

		// Check whether there were any weight updates during this epoch
		size_t sumUpdates;
		size_t *sendbuf_uint, *recvbuf_uint;
		sendbuf_uint = (size_t *) malloc(sizeof(size_t));
		recvbuf_uint = (size_t *) malloc(sizeof(size_t));
#ifdef MPI_ENABLE
		//mpi::reduce(world, numberOfUpdatesThisEpoch, sumUpdates, MPI_SUM, 0);
		sendbuf_uint[0] = numberOfUpdatesThisEpoch;
		recvbuf_uint[0] = 0;
		MPI_Reduce(sendbuf_uint, recvbuf_uint, 1, MPI_UNSIGNED, MPI_SUM, 0, world);
		sumUpdates = recvbuf_uint[0];
#endif
#ifndef MPI_ENABLE
		sumUpdates = numberOfUpdatesThisEpoch;
#endif
		if (rank == 0 && sumUpdates == 0) {
		  cerr << "\nNo weight updates during this epoch.. stopping." << endl;
		  stop = true;
#ifdef MPI_ENABLE
		  mpi::broadcast(world, stop, 0);
#endif
		}

		if (!stop) {
			// Test if weights have converged
			if (weightConvergence) {
				bool reached = true;
				if (rank == 0 && (epoch >= 2)) {
					ScoreComponentCollection firstDiff, secondDiff;
					if (dumpMixedWeights) {
						firstDiff = mixedWeights;
						firstDiff.MinusEquals(mixedWeightsPrevious);
						secondDiff = mixedWeights;
						secondDiff.MinusEquals(mixedWeightsBeforePrevious);
					}
					else {
						firstDiff = mixedAverageWeights;
						firstDiff.MinusEquals(mixedAverageWeightsPrevious);
						secondDiff = mixedAverageWeights;
						secondDiff.MinusEquals(mixedAverageWeightsBeforePrevious);
					}
					VERBOSE(1, "Average weight changes since previous epoch: " << firstDiff << " (max: " << firstDiff.GetLInfNorm() << ")" << endl);
					VERBOSE(1, "Average weight changes since before previous epoch: " << secondDiff << " (max: " << secondDiff.GetLInfNorm() << ")" << endl << endl);

					// check whether stopping criterion has been reached
					// (both difference vectors must have all weight changes smaller than min_weight_change)
					if (firstDiff.GetLInfNorm() >= min_weight_change)
					  reached = false;
					if (secondDiff.GetLInfNorm() >= min_weight_change)
					  reached = false;
					if (reached) {
						// stop MIRA
						stop = true;
						cerr << "\nWeights have converged after epoch " << epoch << ".. stopping MIRA." << endl;
						ScoreComponentCollection dummy;
						ostringstream endfilename;
						endfilename << "stopping";
						dummy.Save(endfilename.str());
					}
				}

				mixedWeightsBeforePrevious = mixedWeightsPrevious;
				mixedWeightsPrevious = mixedWeights;
				mixedAverageWeightsBeforePrevious = mixedAverageWeightsPrevious;
				mixedAverageWeightsPrevious = mixedAverageWeights;
#ifdef MPI_ENABLE
				mpi::broadcast(world, stop, 0);
#endif
			} //end if (weightConvergence)
		}
	} // end of epoch loop

#ifdef MPI_ENABLE
	MPI_Finalize();
#endif

	time(&now);
	cerr << "Rank " << rank << ", " << ctime(&now);

	if (rank == 0) {
	  ScoreComponentCollection dummy;
	  ostringstream endfilename;
	  endfilename << "finished";
	  dummy.Save(endfilename.str());
	}

	delete decoder;
	exit(0);
}

bool loadSentences(const string& filename, vector<string>& sentences) {
	ifstream in(filename.c_str());
	if (!in)
		return false;
	string line;
	while (getline(in, line))
		sentences.push_back(line);
	return true;
}

/*bool loadCoreWeights(const string& filename, ProducerWeightMap& coreWeightMap, const vector<const ScoreProducer*> &featureFunctions) {
	ifstream in(filename.c_str());
	if (!in)
		return false;
	string line;
	vector< float > store_weights;
	while (getline(in, line)) {
		// split weight name from value
		vector<string> split_line;
		boost::split(split_line, line, boost::is_any_of(" "));
		float weight;
		if(!from_string<float>(weight, split_line[1], std::dec))
		{
			cerr << "reading in float failed.." << endl;
			return false;
		}

		// find producer for this score
		string name = split_line[0];
		for (size_t i=0; i < featureFunctions.size(); ++i) {
			std::string prefix = featureFunctions[i]->GetScoreProducerDescription();
			if (name.substr( 0, prefix.length() ).compare( prefix ) == 0) {
				if (featureFunctions[i]->GetNumScoreComponents() == 1) {
					vector< float > weights;
					weights.push_back(weight);
					coreWeightMap.insert(ProducerWeightPair(featureFunctions[i], weights));
					//cerr << "insert 1 weight for " << featureFunctions[i]->GetScoreProducerDescription();
					//cerr << " (" << weight << ")" << endl;
				}
				else {
					store_weights.push_back(weight);
					if (store_weights.size() == featureFunctions[i]->GetNumScoreComponents()) {
						coreWeightMap.insert(ProducerWeightPair(featureFunctions[i], store_weights));
						cerr << "insert " << store_weights.size() << " weights for " << featureFunctions[i]->GetScoreProducerDescription() << " (";
						for (size_t j=0; j < store_weights.size(); ++j)
							cerr << store_weights[j] << " ";
							cerr << ")" << endl;
						store_weights.clear();
					}
				}
			}
		}
	}
	return true;
}*/

bool evaluateModulo(size_t shard_position, size_t mix_or_dump_base, size_t actual_batch_size) {
	if (mix_or_dump_base == 0) return 0;
	if (actual_batch_size > 1) {
		bool mix_or_dump = false;
		size_t numberSubtracts = actual_batch_size;
		do {
			if (shard_position % mix_or_dump_base == 0) {
				mix_or_dump = true;
				break;
			}
			--shard_position;
			--numberSubtracts;
		} while (numberSubtracts > 0);
		return mix_or_dump;
	}
	else {
		return ((shard_position % mix_or_dump_base) == 0);
	}
}

void printFeatureValues(vector<vector<ScoreComponentCollection> > &featureValues) {
	for (size_t i = 0; i < featureValues.size(); ++i) {
		for (size_t j = 0; j < featureValues[i].size(); ++j) {
			cerr << featureValues[i][j] << endl;
		}
	}
	cerr << endl;
}

void ignoreCoreFeatures(vector<vector<ScoreComponentCollection> > &featureValues, ProducerWeightMap &coreWeightMap) {
	for (size_t i = 0; i < featureValues.size(); ++i)
		for (size_t j = 0; j < featureValues[i].size(); ++j) {
			// set all core features to 0
			ProducerWeightMap::iterator p;
			for(p = coreWeightMap.begin(); p!=coreWeightMap.end(); ++p) {
				if ((p->first)->GetNumScoreComponents() == 1)
					featureValues[i][j].Assign(p->first, 0);
				else {
					vector< float > weights;
					for (size_t k=0; k < (p->first)->GetNumScoreComponents(); ++k)
						weights.push_back(0);
					featureValues[i][j].Assign(p->first, weights);
				}
			}
		}
}

void deleteTranslations(vector<vector<const Word*> > &translations) {
	for (size_t i = 0; i < translations.size(); ++i) {
		for (size_t j = 0; j < translations[i].size(); ++j) {
			delete translations[i][j];
		}
	}
}

void decodeHopeOrFear(size_t rank, size_t size, size_t decode, string filename, vector<string> &inputSentences, MosesDecoder* decoder, size_t n, float bleuWeight) {
	if (decode == 1)
		cerr << "Rank " << rank << ", decoding dev input set according to hope objective.. " << endl;
	else if (decode == 2)
		cerr << "Rank " << rank << ", decoding dev input set according to fear objective.. " << endl;
	else
		cerr << "Rank " << rank << ", decoding dev input set according to normal objective.. " << endl;

	// Create shards according to the number of processes used
	vector<size_t> order;
	for (size_t i = 0; i < inputSentences.size(); ++i)
		order.push_back(i);

	vector<size_t> shard;
	float shardSize = (float) (order.size()) / size;
	size_t shardStart = (size_t) (shardSize * rank);
	size_t shardEnd = (size_t) (shardSize * (rank + 1));
	if (rank == size - 1) {
		shardEnd = inputSentences.size();
		shardSize = shardEnd - shardStart;
	}
	VERBOSE(1, "Rank " << rank << ", shard start: " << shardStart << " Shard end: " << shardEnd << endl);
	VERBOSE(1, "Rank " << rank << ", shard size: " << shardSize << endl);
	shard.resize(shardSize);
	copy(order.begin() + shardStart, order.begin() + shardEnd, shard.begin());

	// open files for writing
	stringstream fname;
	fname << filename << ".rank" << rank;
	filename = fname.str();
	ostringstream filename_nbest;
	filename_nbest << filename << "." << n << "best";
	ofstream out(filename.c_str());
	ofstream nbest_out((filename_nbest.str()).c_str());
	if (!out) {
		ostringstream msg;
		msg << "Unable to open " << fname.str();
		throw runtime_error(msg.str());
	}
	if (!nbest_out) {
		ostringstream msg;
		msg << "Unable to open " << filename_nbest;
		throw runtime_error(msg.str());
	}

	for (size_t i = 0; i < shard.size(); ++i) {
		size_t sid = shard[i];
		string& input = inputSentences[sid];

		vector<vector<ScoreComponentCollection> > dummyFeatureValues;
		vector<vector<float> > dummyBleuScores;
		vector<vector<float> > dummyModelScores;

		vector<ScoreComponentCollection> newFeatureValues;
		vector<float> newScores;
		dummyFeatureValues.push_back(newFeatureValues);
		dummyBleuScores.push_back(newScores);
		dummyModelScores.push_back(newScores);

		float factor = 0.0;
		if (decode == 1) factor = 1.0;
		if (decode == 2) factor = -1.0;
		cerr << "Rank " << rank << ", translating sentence " << sid << endl;
		bool realBleu = false;
		vector< vector<const Word*> > nbestOutput = decoder->getNBest(input, sid, n, factor, bleuWeight, dummyFeatureValues[0],
				dummyBleuScores[0], dummyModelScores[0], n, realBleu, true, false, rank, 0, "");
		cerr << endl;
		decoder->cleanup(StaticData::Instance().GetSearchAlgorithm() == ChartDecoding);

		for (size_t i = 0; i < nbestOutput.size(); ++i) {
			vector<const Word*> output = nbestOutput[i];
			stringstream translation;
			for (size_t k = 0; k < output.size(); ++k) {
				Word* w = const_cast<Word*>(output[k]);
				translation << w->GetString(0);
				translation << " ";
			}

			if (i == 0)
				out << translation.str() << endl;
			nbest_out << sid << " ||| " << translation.str() << " ||| " << dummyFeatureValues[0][i] <<
					" ||| " << dummyModelScores[0][i] << " ||| sBleu=" << dummyBleuScores[0][i] << endl;
		}
	}

	out.close();
	nbest_out.close();
	cerr << "Closing files " << filename << " and " << filename_nbest.str() << endl;

#ifdef MPI_ENABLE
	MPI_Finalize();
#endif

	time_t now;
	time(&now);
	cerr << "Rank " << rank << ", " << ctime(&now);

	delete decoder;
	exit(0);
}

void applyLearningRates(vector<vector<ScoreComponentCollection> > &featureValues, float core_r0, float sparse_r0) {
  for (size_t i=0; i<featureValues.size(); ++i) // each item in batch                                                           
    for (size_t j=0; j<featureValues[i].size(); ++j) // each item in nbest                                                        
      featureValues[i][j].MultiplyEquals(core_r0, sparse_r0);
}

void applyPerFeatureLearningRates(vector<vector<ScoreComponentCollection> > &featureValues, ScoreComponentCollection featureLearningRates, float sparse_r0) {
  for (size_t i=0; i<featureValues.size(); ++i) // each item in batch       
    for (size_t j=0; j<featureValues[i].size(); ++j) // each item in nbest                                                         
      featureValues[i][j].MultiplyEqualsBackoff(featureLearningRates, sparse_r0);
}

void scaleFeatureScore(ScoreProducer *sp, float scaling_factor, vector<vector<ScoreComponentCollection> > &featureValues, size_t rank, size_t epoch) {
  string name = sp->GetScoreProducerWeightShortName();

  // scale down score
  float featureScore;
  for (size_t i=0; i<featureValues.size(); ++i) { // each item in batch
    for (size_t j=0; j<featureValues[i].size(); ++j) { // each item in nbest
      featureScore = featureValues[i][j].GetScoreForProducer(sp);
      featureValues[i][j].Assign(sp, featureScore*scaling_factor);
      //cerr << "Rank " << rank << ", epoch " << epoch << ", " << name << " score scaled from " << featureScore << " to " << featureScore/scaling_factor << endl;
    }
  }
}

void scaleFeatureScores(ScoreProducer *sp, float scaling_factor, vector<vector<ScoreComponentCollection> > &featureValues, size_t rank, size_t epoch) {
  string name = sp->GetScoreProducerWeightShortName();

  // scale down score                                                                                                                         
  for (size_t i=0; i<featureValues.size(); ++i) { // each item in batch                                                                       
    for (size_t j=0; j<featureValues[i].size(); ++j) { // each item in nbest                                                               
      vector<float> featureScores = featureValues[i][j].GetScoresForProducer(sp);
      for (size_t k=0; k<featureScores.size(); ++k)
	featureScores[k] *= scaling_factor;
      featureValues[i][j].Assign(sp, featureScores);
      //cerr << "Rank " << rank << ", epoch " << epoch << ", " << name << " score scaled from " << featureScore << " to " << featureScore/scaling_factor << endl;                                                                                                                            
    }
  }
}