-
Notifications
You must be signed in to change notification settings - Fork 3
/
Best_MCM.cpp
609 lines (511 loc) · 22.5 KB
/
Best_MCM.cpp
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
#include <map>
#include <fstream>
#include <bitset>
#include <vector>
#include "data.h"
/******************************************************************************/
/**************** Log-likelihood (LogL), Geometric Complexity *****************/
/************************* and Log-evidence (LogE) ***************************/
/******************************************************************************/
double LogE_MCM(vector<pair<uint32_t, unsigned int>> Kset, map<uint32_t, uint32_t> Partition, unsigned int N);
double Complexity_MCM(map<uint32_t, uint32_t> Partition, unsigned int N, double *C_param, double *C_geom);
/********************************************************************/
/************* CHECK if "Partition" IS A PARTITION *************/
/********************************************************************/
//check if *Partition* is an actual partition of r basis elements,
// i.e., that no basis element appears in more than 1 part of the partition.
// i.e., that each basis element only appears in a single part of the partition.
bool check_partition(map<uint32_t, uint32_t> Partition);
/********************************************************************/
/********************** PRINT PARTITION ************************/
/********************************************************************/
void Print_Partition(uint32_t *a)
{
for (int i=0; i<n; i++) { cout << a[i]; }
}
void Print_Partition_Converted(map<uint32_t, uint32_t> partition)
{
for (map<uint32_t, uint32_t>::iterator i = partition.begin(); i != partition.end(); i++)
{ cout << (*i).second << " = " << bitset<n>((*i).second) << "\n"; }
cout << endl;
}
/********************************************************************/
/**************** CONVERSION of a partition ******************/
/********************** SPECIFIC TO MCM ************************/
/********************************************************************/
// *** map<uint32_t, uint32_t> --> .first = i = index --> .second = a[i] = number of element in the part
map<uint32_t, uint32_t> Convert_Partition_forMCM(uint32_t *a, unsigned int r=n)
{
map<uint32_t, uint32_t> Partition;
uint32_t element = 1;
for (int i=r-1; i>=0; i--) // read element from last to first
{
Partition[(a[i])] += element;
element = element << 1; //cout << a[i] << "\t :"; Print_Partition_Converted(Partition);
}
// cout << "Convert, " << Partition.size() << " parts: \t " ;
// bool ok = check_partition(Partition);
// cout << " \t --> Partition Ok? " << ok << endl << endl;
return Partition;
}
// *** map<uint32_t, uint32_t> --> .first = i = index of the partition --> .second = a[i] = number of element in the part
map<uint32_t, uint32_t> Convert_Partition_forMCM_withSubPart(uint32_t *a, bool *keep_SubPartition, unsigned int r=n)
{
map<uint32_t, uint32_t> Partition;
uint32_t element = 1;
bool switch_ = false;
*keep_SubPartition = true;
for (int i=r-1; i>=0; i--) // read element from last to first
{
Partition[(a[i])] += element; // cout << a[i] << "\t ";
element = element << 1;
if(switch_ == true && a[i] != 0) { *keep_SubPartition = false; }
else if(a[i] == 0) { switch_ = true; }
}
// cout << "Convert, " << Partition.size() << " parts: \t " ;
// bool ok = check_partition(Partition);
// cout << " \t --> Partition Ok? " << ok << endl << endl;
return Partition;
}
/******************************************************************************/
/********************* Compute all Partitions of a set *********************/
/*************************** with Algorithm H *****************************/
/******************************************************************************/
// *** find the first index j (from the right) such that a[j] != b[j]
int find_j(uint32_t *a, uint32_t *b, unsigned int r)
{
int j = r-2;
while (a[j] == b[j]) { j--; }
return j;
}
/******************************************************************************/
// *** Version 1:
// *** Compare all the MCM of rank r,
// *** based on the r first elements of the basis used to build Kset:
/******************************************************************************/
map<uint32_t, uint32_t> MCM_GivenRank_r(vector<pair<uint32_t, unsigned int>> Kset, unsigned int N, double *LogE_best, unsigned int r=n, bool print_bool=false)
{
cout << "--->> Search for the best MCM.." << endl << endl;
int counter = 0, i = 0;
string xx_st = "";
for(int i=0; i<n-r; i++)
{ xx_st += "_"; }
// *** Print in file Best MCMs:
fstream file_BestMCM((OUTPUT_directory + "BestMCM_Rank_r=" + to_string(r) + ".dat").c_str(), ios::out);
file_BestMCM << "# 1:Partition \t 2:LogE " << endl;
// *** Print in file all MCMs:
fstream file_MCM_Rank_r((OUTPUT_directory + "AllMCMs_Rank_r" + to_string(r) + ".dat").c_str(), ios::out);
if(print_bool)
{
cout << "--> Print the LogE-value of all the MCM of rank r=" << r << " in the file '";
cout << (OUTPUT_directory + "AllMCMs_Rank_r=" + to_string(r) + ".dat") << "'" << endl << endl;
file_MCM_Rank_r << "# 1:Partition \t 2:LogE \t 3:C_K \t 4:C_geom \t 5:C_tot \t 6:counter" << endl;
}
else
{
file_MCM_Rank_r << "To activate the prints for all the MCMs of rank r="<< r << ","<< endl;
file_MCM_Rank_r << " specify `print_bool=true` in the last argument of the function MCM_GivenRank_r();";
}
// *** H1: Initialisation:
uint32_t *a = (uint32_t *)malloc(r*sizeof(uint32_t));
uint32_t *b = (uint32_t *)malloc(r*sizeof(uint32_t));
for (int i=0; i<r; i++)
{ a[i]=0; b[i]=1; }
int j = r-1;
// *** LogE and Complexity
double LogE = 0;
double C_param = 0, C_geom = 0;
map<uint32_t, uint32_t> Partition;
// *** Save Best MCMs:
uint32_t *aBest = (uint32_t *)malloc(n*sizeof(uint32_t));
for(int i=0; i<r; i++) { aBest[i]=a[i]; }
*LogE_best = LogE_MCM(Kset, Convert_Partition_forMCM(a, r), N);
// *** ALGO H:
while(j != 0)
{
// *** H2: Visit:
counter++; //file_MCM_Rank_r << counter << ": \t";
Partition = Convert_Partition_forMCM(a, r);
LogE = LogE_MCM(Kset, Partition, N); //LogE
// *** Print in file:
if(print_bool)
{
file_MCM_Rank_r << xx_st;
for (i=0; i<r; i++) { file_MCM_Rank_r << a[i]; } //Print_Partition(a);
Complexity_MCM(Partition, N, &C_param, &C_geom); //Complexity
file_MCM_Rank_r << " \t" << LogE << " \t" << C_param << " \t" << C_geom << " \t" << (C_param + C_geom) << " \t" << counter << endl;
}
// *** Best MCM LogE:
if ( LogE > (*LogE_best))
{
*LogE_best = LogE; //Best_MCM.clear(); Best_MCM.push_back(a);
file_BestMCM << xx_st;
for (i=0; i<r; i++) { file_BestMCM << a[i]; aBest[i]=a[i]; }
file_BestMCM << "\t " << LogE << " \t New \t " << counter << endl;
}
else if ( LogE == (*LogE_best) )
{
file_BestMCM << xx_st;
for (i=0; i<r; i++) { file_BestMCM << a[i]; aBest[i]=a[i]; }
file_BestMCM << "\t " << LogE << " \t Idem \t " << counter << endl;
}
if(a[r-1] != b[r-1]) { a[r-1] += 1; } // H3: increase a[r-1] up to reaching b[r-1]
else
{
j = find_j(a,b,r); //H4: find first index j (from the right) such that a[j] != b[j]
if (j==0) { break; } //H5: Increase a[j] unless j=0 [Terminate]
else
{
a[j] += 1;
b[r-1] = b[j] + ((a[j]==b[j])?1:0); // m
j++; //H6: zero out a[j+1], ..., a[r-1]
while ( j < (r-1) )
{
a[j] = 0;
b[j] = b[r-1]; // = m
j++;
}
a[r-1] = 0;
}
}
}
file_BestMCM.close();
file_MCM_Rank_r.close();
cout << "--> Number of MCModels (of rank r=" << r << ") that were compared: " << counter << endl;
cout << endl << "********** Best MCM: **********";
cout << endl << "\t !! The first operator of the basis provided corresponds to the bit the most on the right !!";
cout << endl << "\t !! The last operator corresponds to the bit the most on the left !!" << endl << endl;;
cout << "\t >> Best Model = ";
cout << xx_st;
for(int i=0; i<r; i++) { cout << aBest[i]; }
cout << "\t \t LogE = " << (*LogE_best) << endl << endl;
Partition = Convert_Partition_forMCM(aBest, r);
free(a); free(b); free(aBest);
return Partition;
}
/******************************************************************************/
// *** Version 2:
// *** Compare all the MCM
// *** based on the k first elements of the basis used to build Kset
// *** for all k=1 to r, where r <= basis.size()
// *** By default: - r=n
// *** - the function doesn't print the logE-values for all the tested MCMs. To activate --> print_bool = true
/******************************************************************************/
map<uint32_t, uint32_t> MCM_AllRank_SmallerThan_r_Ordered(vector<pair<uint32_t, unsigned int>> Kset, unsigned int N, double *LogE_best, unsigned int r=n, bool print_bool=false)
{
int counter = 0, i = 0;
int counter_subMCM = 0;
string xx_st = "";
for(int i=0; i<n-r; i++)
{ xx_st += "_"; }
// *** Print in file Best MCMs:
fstream file_BestMCM(OUTPUT_directory + "BestMCM_Rank_r<=" + to_string(r) + "_Ordered.dat", ios::out);
file_BestMCM << "# 1:Partition \t 2:LogE " << endl;
// *** Print in file all models:
fstream file_allMCM_r((OUTPUT_directory +"AllMCMs_Rank_r=" + to_string(r) + ".dat").c_str(), ios::out);
fstream file_allSubMCM((OUTPUT_directory +"AllMCMs_Rank_r<" + to_string(r) + "_Ordered.dat").c_str(), ios::out);
if(print_bool)
{
cout << "--> Print the LogE-value of all the MCM of rank r=" << r << " in the file '";
cout << (OUTPUT_directory +"AllMCMs_Rank_r=" + to_string(r) + ".dat") << "'" << endl << endl;
cout << "--> Print the LogE-value of all the MCM of rank k<" << r << " in the file '";
cout << (OUTPUT_directory +"AllMCMs_Rank_r<" + to_string(r) + "_Ordered.dat") << "'" << endl << endl;
file_allMCM_r << "# 1:Partition \t 2:LogE \t 3:C_K \t 4:C_geom \t 5:C_tot \t 6:counter" << endl;
file_allSubMCM << "# 1:Partition \t 2:LogE \t 3:C_K \t 4:C_geom \t 5:C_tot \t 6:counter" << endl;
}
else
{
file_allMCM_r << "To activate the prints for all the MCMs of rank r<="<< r << ","<< endl;
file_allMCM_r << " specify `print_bool=true` in the last argument of the function MCM_AllRank_SmallerThan_r_Ordered();";
file_allSubMCM << "To activate the prints for all the MCMs of rank r<="<< r << ","<< endl;
file_allSubMCM << " specify `print_bool=true` in the last argument of the function MCM_AllRank_SmallerThan_r_Ordered();";
}
// *** H1: Initialisation:
uint32_t *a = (uint32_t *)malloc(r*sizeof(uint32_t));
uint32_t *b = (uint32_t *)malloc(r*sizeof(uint32_t));
for (int i=0; i<r; i++)
{ a[i]=0; b[i]=1; }
int j = r-1;
// *** LogE and Complexity
double LogE = 0;
double C_param = 0, C_geom = 0;
map<uint32_t, uint32_t> Partition;
// *** Save Best MCMs:
uint32_t *aBest = (uint32_t *)malloc(r*sizeof(uint32_t));
for(int i=0; i<r; i++) { aBest[i]=a[i]; }
*LogE_best = LogE_MCM(Kset, Convert_Partition_forMCM(a, r), N);
// *** SubPartitions (rank < n):
bool keep_SubPartition = false;
// *** ALGO H:
while(j != 0)
{
// *** H2: Visit:
counter++; //file_allMCM_r << counter << ": \t";
// *** Original Partition:
Partition = Convert_Partition_forMCM_withSubPart(a, &keep_SubPartition, r); //Print_Partition_Converted(Partition);
LogE = LogE_MCM(Kset, Partition, N); //LogE
// *** Print in file:
if(print_bool)
{
file_allMCM_r << xx_st;
for (i=0; i<r; i++) { file_allMCM_r << a[i]; } //Print_Partition(a);
Complexity_MCM(Partition, N, &C_param, &C_geom); //Complexity
file_allMCM_r << " \t" << LogE << " \t" << C_param << " \t" << C_geom << " \t" << (C_param + C_geom) << " \t" << counter << endl;
}
// *** Best MCM LogE:
if ( LogE > (*LogE_best))
{
*LogE_best = LogE; //Best_MCM.clear(); Best_MCM.push_back(a);
file_BestMCM << xx_st;
for (i=0; i<r; i++) { file_BestMCM << a[i]; aBest[i]=a[i]; }
file_BestMCM << "\t " << LogE << " \t New \t " << counter << endl;
}
else if ( LogE == (*LogE_best) )
{
file_BestMCM << xx_st;
for (i=0; i<r; i++) { file_BestMCM << a[i]; aBest[i]=a[i]; }
file_BestMCM << "\t " << LogE << " \t Idem \t " << counter << endl;
}
// *** Sub-Partition:
if (keep_SubPartition)
{
counter_subMCM++;
Partition.erase(0); //Print_Partition_Converted(Partition);
LogE = LogE_MCM(Kset, Partition, N); //LogE
// *** Print in file:
if(print_bool)
{
file_allSubMCM << xx_st;
for (i=0; i<r; i++) //Print_Partition(a);
{
if (a[i] == 0 ) { file_allSubMCM << "x"; }
else { file_allSubMCM << (a[i]-1); }
}
Complexity_MCM(Partition, N, &C_param, &C_geom); //Complexity
file_allSubMCM << " \t" << LogE << " \t" << C_param << " \t" << C_geom << " \t" << (C_param + C_geom) << " \t" << counter_subMCM << endl;
}
// *** Best MCM LogE:
if ( LogE > (*LogE_best) )
{
*LogE_best = LogE; //Best_MCM.clear(); Best_MCM.push_back(a);
file_BestMCM << xx_st;
for (i=0; i<r; i++)
{
if (a[i] != 0 ) { file_BestMCM << (a[i]-1); aBest[i] = (a[i]-1); }
else { file_BestMCM << "x"; aBest[i] = -1; }
}
file_BestMCM << "\t " << LogE << " \t New" << endl;
}
else if ( LogE == (*LogE_best) )
{
file_BestMCM << xx_st;
for (i=0; i<r; i++)
{
if (a[i] != 0 ) { file_BestMCM << (a[i]-1); aBest[i] = (a[i]-1); }
else { file_BestMCM << "x"; aBest[i] = -1; }
}
file_BestMCM << "\t " << LogE << " \t Idem" << endl;
}
}
if(a[r-1] != b[r-1]) { a[r-1] += 1; } // H3: increase a[n-1] up to reaching b[n-1]
else
{
j = find_j(a,b,r); //H4: find first index j (from the right) such that a[j] != b[j]
if (j==0) { break; } //H5: Increase a[j] unless j=0 [Terminate]
else
{
a[j] += 1;
b[r-1] = b[j] + ((a[j]==b[j])?1:0); // m
j++; //H6: zero out a[j+1], ..., a[n-1]
while ( j < (r-1) )
{
a[j] = 0;
b[j] = b[r-1]; // = m
j++;
}
a[r-1] = 0;
}
}
}
file_BestMCM.close();
file_allMCM_r.close();
file_allSubMCM.close();
cout << "--> Number of MCM models (of rank <=" << r << ") that have been compared: " << counter + counter_subMCM << endl << endl;
cout << endl << "********** Best MCM: **********";
cout << endl << "\t !! The first operator of the basis provided corresponds to the bit the most on the right !!";
cout << endl << "\t !! The last operator corresponds to the bit the most on the left !!" << endl << endl;;
cout << "\t >> Best Model = ";
cout << xx_st;
for(int i=0; i<r; i++) { if(aBest[i] != -1) {cout << aBest[i];} else {cout << "x";} }
cout << "\t \t LogE = " << (*LogE_best) << endl << endl;
Partition = Convert_Partition_forMCM(aBest, r);
free(a); free(b); free(aBest);
return Partition;
}
/******************************************************************************/
// *** Version 3:
// *** Compare all the MCMs based on any subset of k elements
// *** of the of r first elements of the basis used to build Kset
// *** for all k=1 to r, where r <= basis.size()
// *** By default: - r=n
// *** - the function doesn't print the logE-values for all the tested MCMs. To activate --> print_bool = true
/******************************************************************************/
map<uint32_t, uint32_t> MCM_AllRank_SmallerThan_r_nonOrdered(vector<pair<uint32_t, unsigned int>> Kset, unsigned int N, double *LogE_best, unsigned int r=n, bool print_bool=false)
{
cout << "All MCM based on all subsets of r operators among n chosen independent operators, r<=n: " << endl;
int counter = 0, i = 0;
int counter_subMCM = 0;
string xx_st = "";
for(int i=0; i<n-r; i++)
{ xx_st += "_"; }
// *** Print in file Best MCMs:
fstream file_BestMCM(OUTPUT_directory + "BestMCM_Rank_r<=" + to_string(r) + "_NonOrdered.dat", ios::out);
file_BestMCM << "# 1:Partition \t 2:LogE " << endl;
// *** Print in file:
fstream file_allMCM_r((OUTPUT_directory +"AllMCMs_Rank_r=" + to_string(r) + ".dat").c_str(), ios::out);
fstream file_allSubMCM((OUTPUT_directory +"AllMCMs_Rank_r<" + to_string(r) + "_NonOrdered.dat").c_str(), ios::out);
if(print_bool)
{
cout << "--> Print the LogE-value of all the MCM of rank r=" << r << " in the file '";
cout << (OUTPUT_directory +"AllMCMs_Rank_r=" + to_string(r) + ".dat") << "'" << endl << endl;
cout << "--> Print the LogE-value of all the MCM of rank k<" << r << " in the file '";
cout << (OUTPUT_directory +"AllMCMs_Rank_r<" + to_string(r) + "_Ordered.dat") << "'" << endl << endl;
file_allMCM_r << "# 1:Partition \t 2:LogE \t 3:C_K \t 4:C_geom \t 5:C_tot \t 6:counter" << endl;
file_allSubMCM << "# 1:Partition \t 2:LogE \t 3:C_K \t 4:C_geom \t 5:C_tot \t 6:counter" << endl;
}
else
{
file_allMCM_r << "To activate the prints for all the MCMs of rank r<="<< r << ","<< endl;
file_allMCM_r << " specify `print_bool=true` in the last argument of the function MCM_AllRank_SmallerThan_r_Ordered();";
file_allSubMCM << "To activate the prints for all the MCMs of rank r<="<< r << ","<< endl;
file_allSubMCM << " specify `print_bool=true` in the last argument of the function MCM_AllRank_SmallerThan_r_Ordered();";
}
// *** H1: Initialisation:
uint32_t *a = (uint32_t *)malloc(r*sizeof(uint32_t));
uint32_t *b = (uint32_t *)malloc(r*sizeof(uint32_t));
for (int i=0; i<r; i++)
{ a[i]=0; b[i]=1; }
int j = r-1;
// *** LogE and Complexity
double LogE = 0;
double C_param = 0, C_geom = 0;
map<uint32_t, uint32_t> Partition, Partition_buffer;
// *** Save Best MCMs:
uint32_t *aBest = (uint32_t *)malloc(r*sizeof(uint32_t));
for(int i=0; i<r; i++) { aBest[i]=a[i]; }
*LogE_best = LogE_MCM(Kset, Convert_Partition_forMCM(a, r), N);
// *** for SubModels:
uint32_t amax = 0, atest = 0;
//SubPartitions (rank < n):
//ALGO H:
while(j != 0) // && counter < 200)
{
// *** H2: Visit: ******
counter++;
// *** Partition:
Partition = Convert_Partition_forMCM(a, r);
LogE = LogE_MCM(Kset, Partition, N); //LogE
Complexity_MCM(Partition, N, &C_param, &C_geom); //Complexity
// *** Print in file:
if(print_bool)
{
file_allMCM_r << xx_st;
for (i=0; i<r; i++) { file_allMCM_r << a[i]; }
file_allMCM_r << " \t" << LogE << " \t" << C_param << " \t" << C_geom << " \t" << (C_param + C_geom) << " \t" << counter << endl;
}
// *** Best MCM LogE:
if ( LogE > (*LogE_best))
{
*LogE_best = LogE;
file_BestMCM << xx_st;
for (i=0; i<r; i++) { file_BestMCM << a[i]; aBest[i]=a[i]; }
file_BestMCM << "\t " << LogE << " \t New" << endl;
}
else if ( LogE == (*LogE_best))
{
file_BestMCM << xx_st;
for (i=0; i<r; i++) { file_BestMCM << a[i]; aBest[i]=a[i]; }
file_BestMCM << "\t " << LogE << " \t Idem" << endl;
}
// *** Find max value in a[]: //amax=0; for(i=0; i<n; i++) { if (a[i] > amax) { amax = a[i]; } }
if ( a[r-1] == b[r-1] ) { amax = b[r-1]; } else { amax = b[r-1]-1; }
// *** Sub-Partition: ***************************** //
for(atest=0; atest<=amax; atest++)
{
counter_subMCM++;
// *** Partition:
Partition_buffer = Partition;
Partition_buffer.erase(atest);
LogE = LogE_MCM(Kset, Partition_buffer, N); //LogE
Complexity_MCM(Partition_buffer, N, &C_param, &C_geom); //Complexity
// *** Print in file:
if(print_bool)
{
file_allSubMCM << xx_st;
for (i=0; i<r; i++)
{
if (a[i] == atest ) { file_allSubMCM << "x"; }
else { file_allSubMCM << a[i]; }
}
file_allSubMCM << " \t" << LogE << " \t" << C_param << " \t" << C_geom << " \t" << (C_param + C_geom) << " \t" << counter_subMCM << endl;
}
// *** Best MCM LogE:
if ( LogE > (*LogE_best))
{
*LogE_best = LogE; //Best_MCM.clear(); Best_MCM.push_back(a);
file_BestMCM << xx_st;
for (i=0; i<r; i++)
{
if (a[i] > atest ) { file_BestMCM << (a[i]-1); aBest[i] = (a[i]-1); }
else if (a[i] < atest ) { file_BestMCM << a[i]; aBest[i] = a[i]; }
else { file_BestMCM << "x"; aBest[i] = -1; }
}
file_BestMCM << "\t " << LogE << " \t New" << endl;
}
else if ( LogE == (*LogE_best))
{
file_BestMCM << xx_st;
for (i=0; i<r; i++)
{
if (a[i] > atest ) { file_BestMCM << (a[i]-1); aBest[i] = (a[i]-1); }
else if (a[i] < atest ) { file_BestMCM << a[i]; aBest[i] = a[i]; }
else { file_BestMCM << "x"; aBest[i] = -1; }
}
file_BestMCM << "\t " << LogE << " \t Idem" << endl;
}
}
if(a[r-1] != b[r-1]) { a[r-1] += 1; } // H3: increase a[n-1] up to reaching b[n-1]
else
{
j = find_j(a,b,r); //H4: find first index j (from the right) such that a[j] != b[j]
if (j==0) { break; } //H5: Increase a[j] unless j=0 [Terminate]
else
{
a[j] += 1;
b[r-1] = b[j] + ((a[j]==b[j])?1:0); // m
j++; //H6: zero out a[j+1], ..., a[n-1]
while ( j < (r-1) )
{
a[j] = 0;
b[j] = b[r-1]; // = m
j++;
}
a[r-1] = 0;
}
}
}
file_BestMCM.close();
file_allMCM_r.close();
file_allSubMCM.close();
cout << "--> Number of MCM models (of rank <=" << r << ") that have been compared: " << counter + counter_subMCM << endl;
cout << endl << "********** Best MCM: **********";
cout << endl << "\t !! The first operator of the basis provided corresponds to the bit the most on the right !!";
cout << endl << "\t !! The last operator corresponds to the bit the most on the left !!" << endl << endl;;
cout << "\t >> Best Model = ";
cout << xx_st;
for(int i=0; i<n; i++) { if(aBest[i] != -1) {cout << aBest[i];} else {cout << "x";} }
cout << "\t \t LogE = " << (*LogE_best) << endl << endl;
Partition = Convert_Partition_forMCM(aBest, r);
free(a); free(b); free(aBest);
return Partition;
}