-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpchecker.hpp
1595 lines (1488 loc) · 49.6 KB
/
cpchecker.hpp
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
/**
* @file mmchecker.hpp: Cube_Product_Checker template class
* and its implementation.
*
* Class is for finding Strassen-like algorithms for cube product.
*
* @author Eugene Petkevich
* @version pre-alpha
*/
#ifndef MMCHECKER_HPP_INCLUDED
#define MMCHECKER_HPP_INCLUDED
#include <algorithm>
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <numeric>
#include <omp.h>
#include <boost/dynamic_bitset.hpp>
#include <boost/algorithm/string/join.hpp>
#include "slae.hpp"
#include "utils.hpp"
using namespace std;
//=============================================================================
class Solution_Properties;
//=============================================================================
//=============================================================================
/**
* Main class, calculates all nesessary information
* for given template parameters:
*
* @param N: size of the cube;
* @param D: dimension of the cube;
* @param NM: number of bits in multiplication vectors ((N^D)^D);
* @param NMH: number of elements in the array (N^D).
*/
template <int N, int D, size_t NM, size_t NMH>
class Cube_Product_Checker {
public:
typedef mm_bitset<NM> Multiplication_Vector;
typedef mm_bitset<NMH> Multiplication_Part_Vector;
typedef set<int> Candidate;
int length; /// size of cube (N)
int dimension; /// dimension of cube (D)
int element_count; /// number of elements in cube (N^D)
mm_vector_with_properties_options vector_options; /// options for vector properties
mm_vector_with_properties<NM>* r_vectors; /// result vectors
mm_vector_with_properties<NM>* m_vectors; /// multiplication vectors
bool owns_arrays; /// If arrays are created by the object.
int m_count; /// number of non-zero multiplication vectors = (2^(N^D)-1)^D
int m_length; /// number non-zero element sums = 2^(N^D)-1
int f_count; /// number of vectors to choose for a basis (for minimal improvement it is N^(D+1)-1)
set<int> good_vectors_indexes; /// set of indexes for vectors that are in the current span
set<int> n_vectors_indexes; /// set of indexes of current chosen vectors
set<Candidate> neighbours; /// set of neighbours;
set<set<int>> solutions; /// set of found unique solutions
map<set<int>, int> solution_distribution; /// set of all found solutions
int iteration_count; /// number of finished iterations
int local_max_iterations; /// number of iterations in probable local maximum
int restarts; /// number of restarts
int local_iterations; /// number of iterations
int checked_sets_count; /// number of sets checked
int raw_solution_count; /// number of found solutions, including duplicates
int best_result; /// maximum number of vectors in the span found
int lin_dependent_sets;
Timewatch tw; /// timer that is used for getting calculation time
Random rnd; /// random number generator
int thread_number; /// thread number
map<Candidate, int> candidate_cache; /// cache of already checked candidates
int cache_limit; /// limit of the cache size
int cache_hits; /// cache hits
bool* stop_signal; /// if execution should be stopped
int bit_check_hits; /// number of bit check hits
int gaussian_eliminations; /// number of gaussian eliminations
set<set<int>> top_best_solutions; /// best solutions found so far
int top_best_result; /// best result so far
set<set<int>> candidate_space; /// candidate space
//=============--- constructors and destructors
Cube_Product_Checker();
~Cube_Product_Checker();
//=============--- index operations
/// return linear index in a bit vector by its indexes in matrices
int get_vector_index(int ai, int aj, int bi, int bj) const; /// for 2-dimensional case
int get_vector_index(int ai, int aj, int ak, int bi, int bj, int bk, int ci, int cj, int ck) const; /// for 3-d case
/// return linear index in a bit vector by combined indexes in matrices
int get_vector_index(int a, int b) const; /// for 2-d case
int get_vector_index(int a, int b, int c) const; /// for 3-d case
/// return indices from bit index
void decode_indices_from_index(int index, int& ai, int& aj, int& bi, int& bj) const; /// for 2-d case
void decode_indices_from_index(int index, int& ai, int& aj, int& ak, int& bi, int& bj, int& bk, int& ci, int& cj, int& ck) const; /// for 3-d case
/// return linear index of an element in a matrix
int get_element_index(int i, int j) const; /// for 2-d case
int get_element_index(int i, int j, int k) const; /// for 3-d case
/// return index of multiplication vector in the set
int get_m_index(int i, int j) const; /// for 2-d case
int get_m_index(int i, int j, int k) const; /// for 3-d case
vector<int> decode_m_index(int index) const; /// return the coefficients from m-vector index
//=============--- Initial calculations
void init(int mult_count, const string& filename, const string& space_filename); /// calculate all properties
void init(const Cube_Product_Checker& cpc); /// Link to all properties in other object.
void calculate_r_vectors(); /// write result vectors to array
void calculate_m_vectors(); /// write multiplication vectors to array
//=============--- checking routines
void clear_sets(); /// clear current sets of vectors
void add_vector_to_set(int index); /// add vector to current sets
bool check_vectors_for_goodness(); /// check current set of vectors
void clear_statistics(); /// clear statistics of solutions
void make_random_candidate(); /// make random candidate solution
void make_candidate_from_space(); /// make random candidate from restricted space
bool check_cache(); /// check if candidate result is in cache
void update_cache(); /// update the cache with new result
//=============--- searching for solution
bool check_for_good_vectors(); /// check all solution space
bool check_for_good_vectors_randomized(); /// do random search
bool solve_hill_climbing(int local_max_limit, bool use_space); /// do local search
void start_neighbourhood(); /// make list of neighbours
//=============--- utilities
void output_vector(Multiplication_Vector v) const; /// output vector to screen
void output_vector_text(Multiplication_Vector v) const; /// output vector to screen in letters
void save_random_samples(int size, const char* filename) const; /// save random sets to a file (for testing later)
void read_samples_and_check(const char* filename, const char* filenameout) const; /// check sets from a file
void output_current_state() const; // output current state of the checker
void read_m_vectors(const string& filename); /// read m_vectors from file
void read_candidate_space(const string& filename); /// read candidate space from file
void write_m_vectors(const string& filename); /// write m_vectors into file
//=============--- Statistics and results
void save_results(const char* filename); /// save results to a file
bool check_solution(set<int> s, Solution_Properties& sp); /// check if a solution is valid
void save_solution_properties(const Solution_Properties& sp, const char* filename); /// output solution properties to a file
vector<int> sum_operations_cube(int index); /// number of summation operations inside cubes
};
//=============================================================================
class Solution_Properties {
public:
set<int> multiplication_vectors; /// multiplication vector indices
vector<boost::dynamic_bitset<>> coefficients; /// result vector coefficients
int operation_count; /// number of addition operations used for calculating result matrix overall
};
//=============================================================================
//=============================================================================
/**
* Class constructor.
*
* Create an object for work with D-dimensional cubes of size N.
*/
template <int N, int D, size_t NM, size_t NMH>
Cube_Product_Checker<N, D, NM, NMH>::
Cube_Product_Checker() :
owns_arrays(false)
{
length = N;
dimension = D;
element_count = power(length, dimension);
f_count = power(length,dimension+1)-1;
m_length = power(2,element_count)-1;
m_count = power(m_length,dimension);
cache_limit = 3000000;
cache_hits = 0;
bit_check_hits = 0;
gaussian_eliminations = 0;
rnd.init(0, m_count-1);
#ifdef VERBOSE_OUTPUT
cout << "Cube Product Checker has been created." << endl;
#endif // VERBOSE_OUTPUT
}
//=============================================================================
/**
* Destructor.
*/
template <int N, int D, size_t NM, size_t NMH>
Cube_Product_Checker<N, D, NM, NMH>::
~Cube_Product_Checker()
{
if (owns_arrays) {
delete [] r_vectors;
delete [] m_vectors;
delete stop_signal;
}
}
//=============================================================================
/**
* Initialize the object with all necessary properties.
*
* @param mult_count: number of multiplications in resulting algorithm.
*/
template <int N, int D, size_t NM, size_t NMH>
void
Cube_Product_Checker<N, D, NM, NMH>::
init(int mult_count, const string& filename, const string& space_filename)
{
f_count = mult_count;
stop_signal = new bool(false);
r_vectors = new mm_vector_with_properties<NM>[element_count];
m_vectors = new mm_vector_with_properties<NM>[m_count];
mm_vector_with_properties<NM>::make_options(vector_options);
owns_arrays = true;
#ifdef VERBOSE_OUTPUT
tw.watch();
#endif // VERBOSE_OUTPUT
calculate_r_vectors();
for (int i = 0; i < element_count; ++i) {
r_vectors[i].calculate_properties(vector_options);
}
#ifdef VERBOSE_OUTPUT
cout << "[" << tw.watch() << " s] Result vectors calculated." << endl;
#endif // VERBOSE_OUTPUT
if (filename.length() > 0) {
read_m_vectors(filename);
} else {
calculate_m_vectors();
for (int i = 0; i < m_count; ++i) {
m_vectors[i].calculate_properties(vector_options);
}
}
if (space_filename.length() > 0) {
read_candidate_space(space_filename);
}
#ifdef VERBOSE_OUTPUT
cout << "[" << tw.watch() << " s] Multiplication vectors calculated." << endl;
#endif // VERBOSE_OUTPUT
}
/**
* Initialize the object with all necessary properties from other object.
*
* Link to all big data fields in the other object.
*
* @param cpc: the object to get main arrays from.
*/
template <int N, int D, size_t NM, size_t NMH>
void
Cube_Product_Checker<N, D, NM, NMH>::
init(const Cube_Product_Checker& cpc)
{
f_count = cpc.f_count;
r_vectors = cpc.r_vectors;
m_vectors = cpc.m_vectors;
vector_options = cpc.vector_options;
stop_signal = cpc.stop_signal;
candidate_space = cpc.candidate_space;
}
//=============================================================================
/**
* Get bit index in a multiplication vector (2-dimensional case).
*
* @param ai: element's first index in the first matrix;
* @param aj: element's second index in the first matrix;
* @param bi: element's first index in the second matrix;
* @param bj: element's second index in the second matrix;
*
* @return bit index.
*/
template <int N, int D, size_t NM, size_t NMH>
inline int
Cube_Product_Checker<N, D, NM, NMH>::
get_vector_index(int ai, int aj,
int bi, int bj) const
{
int result = ai;
result *= length;
result += aj;
result *= length;
result += bi;
result *= length;
result += bj;
return result;
}
/**
* Get bit index in a multiplication vector (3-dimensional case).
*
* @param ai: element's first index in the first cube;
* @param aj: element's second index in the first cube;
* @param ak: element's third index in the first cube;
* @param bi: element's first index in the second cube;
* @param bj: element's second index in the second cube;
* @param bk: element's third index in the second cube;
* @param ci: element's first index in the third cube;
* @param cj: element's second index in the third cube;
* @param ck: element's third index in the third cube.
*
* @return bit index.
*/
template <int N, int D, size_t NM, size_t NMH>
inline int
Cube_Product_Checker<N, D, NM, NMH>::
get_vector_index(int ai, int aj, int ak,
int bi, int bj, int bk,
int ci, int cj, int ck) const
{
int result = ai;
result *= length;
result += aj;
result *= length;
result += ak;
result *= length;
result += bi;
result *= length;
result += bj;
result *= length;
result += bk;
result *= length;
result += ci;
result *= length;
result += cj;
result *= length;
result += ck;
return result;
}
//=============================================================================
/**
* Get bit index in a multiplication vector (2-dimensional case).
*
* @param a: element'S index in the first matrix;
* @param b: element'S index in the second matrix.
*
* @return bit index.
*/
template <int N, int D, size_t NM, size_t NMH>
inline int
Cube_Product_Checker<N, D, NM, NMH>::
get_vector_index(int a, int b) const
{
int result = a;
result *= element_count;
result += b;
return result;
}
/**
* Get bit index in a multiplication vector (3-dimensional case).
*
* @param a: element's index in the first cube;
* @param b: element's index in the second cube;
* @param c: element's index in the third cube.
*
* @return bit index.
*/
template <int N, int D, size_t NM, size_t NMH>
inline int
Cube_Product_Checker<N, D, NM, NMH>::
get_vector_index(int a, int b, int c) const
{
int result = a;
result *= element_count;
result += b;
result *= element_count;
result += c;
return result;
}
//=============================================================================
/**
* Get element indices in matrices from bit index in the multiplication vector
* (2-dimensional case).
*
* @param index: bit index in a multiplication vector;
*
* @param ai: element's first index in the first matrix;
* @param aj: element's second index in the first matrix;
* @param bi: element's first index in the second matrix;
* @param bj: element's second index in the second matrix.
*/
template <int N, int D, size_t NM, size_t NMH>
inline void
Cube_Product_Checker<N, D, NM, NMH>::
decode_indices_from_index(int index,
int& ai, int& aj,
int& bi, int& bj) const
{
bj = index % length;
index /= length;
bi = index % length;;
index /= length;
aj = index % length;
index /= length;
ai = index;
return;
}
/**
* Get element indices in cubes from bit index in a multiplication vector
* (2-dimensional case).
*
* @param index: bit index in the multiplication vector;
*
* @param ai: element's first index in the first cube;
* @param aj: element's second index in the first cube;
* @param ak: element's third index in the first cube;
* @param bi: element's first index in the second cube;
* @param bj: element's second index in the second cube;
* @param bk: element's third index in the second cube;
* @param ci: element's first index in the third cube;
* @param cj: element's second index in the third cube;
* @param ck: element's third index in the third cube.
*/
template <int N, int D, size_t NM, size_t NMH>
inline void
Cube_Product_Checker<N, D, NM, NMH>::
decode_indices_from_index(int index,
int& ai, int& aj, int& ak,
int& bi, int& bj, int& bk,
int& ci, int& cj, int& ck) const
{
ck = index % length;
index /= length;
cj = index % length;
index /= length;
ci = index % length;
index /= length;
bk = index % length;
index /= length;
bj = index % length;
index /= length;
bi = index % length;
index /= length;
ak = index % length;
index /= length;
aj = index % length;
index /= length;
ai = index;
return;
}
//=============================================================================
/**
* Get element's index in a matrix (2-dimensional case).
*
* @param i: element's first index in the cube;
* @param j: element's second index in the cube;
*
* @return element's index in the cube.
*/
template <int N, int D, size_t NM, size_t NMH>
inline int
Cube_Product_Checker<N, D, NM, NMH>::
get_element_index(int i, int j) const
{
return ((i*length) + j);
}
/**
* Get element's index in a cube (3-dimensional case).
*
* @param i: element's first index in the cube;
* @param j: element's second index in the cube;
* @param k: element's third index in the cube;
*
* @return element's index in the cube.
*/
template <int N, int D, size_t NM, size_t NMH>
inline int
Cube_Product_Checker<N, D, NM, NMH>::
get_element_index(int i, int j, int k) const
{
return (((i*length) + j)*length + k);
}
//=============================================================================
/**
* Get index of a multiplication vector by its sum indices.
*
* This index is a unique number of the vector
* in the set of all non-zero multiplication vectors.
*
* @param i: vector's first index;
* @param j: vector's second index;
*
* @return multiplication vector index.
*/
template <int N, int D, size_t NM, size_t NMH>
inline int
Cube_Product_Checker<N, D, NM, NMH>::
get_m_index(int i, int j) const
{
return ((i*m_length) + j);
}
/**
* Get index of a multiplication vector by its sum indices.
*
* This index is a unique number of the vector
* in the set of all non-zero multiplication vectors.
*
* @param i: vector's first index;
* @param j: vector's second index;
* @param k: vector's third index;
*
* @return multiplication vector index.
*/
template <int N, int D, size_t NM, size_t NMH>
inline int
Cube_Product_Checker<N, D, NM, NMH>::
get_m_index(int i, int j, int k) const
{
return (((i*m_length) + j)*m_length + k);
}
//=============================================================================
/**
* Get coefficients of cube elements by number of multiplication vector.
*
* @param multiplication vector index.
*
* @return
*/
template <int N, int D, size_t NM, size_t NMH>
inline vector<int>
Cube_Product_Checker<N, D, NM, NMH>::
decode_m_index(int index) const
{
int last, previous;
last = index % m_length;
index /= m_length;
previous = index % m_length;
vector<int> result;
if (dimension == 3) {
int first = index / m_length;
result.push_back(first);
}
result.push_back(previous);
result.push_back(last);
return result;
}
//=============================================================================
/**
* Calculate product result vectors (2x2 case).
*/
template <>
void
Cube_Product_Checker<2, 2, 16, 4>::
calculate_r_vectors()
{
for (int i = 0; i < length; ++i) {
for (int j = 0; j < length; ++j) {
int index = get_element_index(i, j);
r_vectors[index].v.reset();
for (int l = 0; l < length; ++l) {
r_vectors[index].v[get_vector_index(i, l, l, j)] = 1;
}
}
}
}
/**
* Calculate product result vectors (3x3 case).
*/
template <>
void
Cube_Product_Checker<3, 2, 81, 9>::
calculate_r_vectors()
{
for (int i = 0; i < length; ++i) {
for (int j = 0; j < length; ++j) {
int index = get_element_index(i, j);
r_vectors[index].v.reset();
for (int l = 0; l < length; ++l) {
r_vectors[index].v[get_vector_index(i, l, l, j)] = 1;
}
}
}
}
/**
* Calculate product result vectors (2x2x2 case).
*/
template <>
void
Cube_Product_Checker<2, 3, 512, 8>::
calculate_r_vectors()
{
for (int i = 0; i < length; ++i) {
for (int j = 0; j < length; ++j) {
for (int k = 0; k < length; ++k) {
int index = get_element_index(i, j, k);
r_vectors[index].v.reset();
for (int l = 0; l < length; ++l) {
r_vectors[index].v[get_vector_index(i, j, l, i, l, k, l, j, k)] = 1;
}
}
}
}
}
//=============================================================================
/**
* Calculate non-zero multiplication vectors (2x2 case).
*/
template <>
void
Cube_Product_Checker<2, 2, 16, 4>::
calculate_m_vectors()
{
for (int i = 1; i < power(2,element_count); ++i) {
for (int j = 1; j < power(2,element_count); ++j) {
Multiplication_Part_Vector av(i);
Multiplication_Part_Vector bv(j);
int index = get_m_index(i-1, j-1);
m_vectors[index].v.reset();
for (int k = 0; k < element_count; ++k) {
for (int l = 0; l < element_count; ++l) {
if (av[k] && bv[l]) {
m_vectors[index].v.set(get_vector_index(k, l));
}
}
}
}
}
}
/**
* Calculate non-zero multiplication vectors (3x3 case).
*/
template <>
void
Cube_Product_Checker<3, 2, 81, 9>::
calculate_m_vectors()
{
for (int i = 1; i < power(2,element_count); ++i) {
for (int j = 1; j < power(2,element_count); ++j) {
Multiplication_Part_Vector av(i);
Multiplication_Part_Vector bv(j);
int index = get_m_index(i-1, j-1);
m_vectors[index].v.reset();
for (int k = 0; k < element_count; ++k) {
for (int l = 0; l < element_count; ++l) {
if (av[k] && bv[l]) {
m_vectors[index].v.set(get_vector_index(k, l));
}
}
}
}
}
}
/**
* Calculate non-zero multiplication vectors (3-d case).
*/
template <>
void
Cube_Product_Checker<2, 3, 512, 8>::
calculate_m_vectors()
{
for (int i = 1; i < power(2,element_count); ++i) {
for (int j = 1; j < power(2,element_count); ++j) {
for (int k = 1; k < power(2,element_count); ++k) {
Multiplication_Part_Vector av(i);
Multiplication_Part_Vector bv(j);
Multiplication_Part_Vector cv(k);
int index = get_m_index(i-1, j-1, k-1);
m_vectors[index].v.reset();
for (int l = 0; l < element_count; ++l) {
for (int o = 0; o < element_count; ++o) {
for (int p = 0; p < element_count; ++p) {
if (av[l] & bv[o] & cv[p]) {
m_vectors[index].v.set(get_vector_index(l, o, p));
}
}
}
}
}
}
}
}
//=============================================================================
/**
* Clear the current sets.
*/
template <int N, int D, size_t NM, size_t NMH>
void
Cube_Product_Checker<N, D, NM, NMH>::
clear_sets()
{
n_vectors_indexes.clear();
good_vectors_indexes.clear();
}
//=============================================================================
/**
* Add a multiplication vector to the current set.
*
* @param index: index of the multiplication vector.
*/
template <int N, int D, size_t NM, size_t NMH>
void
Cube_Product_Checker<N, D, NM, NMH>::
add_vector_to_set(int index)
{
n_vectors_indexes.insert(index);
}
//=============================================================================
/**
* Clear statistics.
*/
template <int N, int D, size_t NM, size_t NMH>
void
Cube_Product_Checker<N, D, NM, NMH>::
clear_statistics()
{
checked_sets_count = 0;
iteration_count = 0;
best_result = 0;
lin_dependent_sets = 0;
#ifdef OUTPUT_STATISTICS
raw_solution_count = 0;
solutions.clear();
solution_distribution.clear();
#endif // OUTPUT_STATISTICS
}
//=============================================================================
/**
* Make a random candidate.
*/
template <int N, int D, size_t NM, size_t NMH>
void
Cube_Product_Checker<N, D, NM, NMH>::
make_random_candidate()
{
clear_sets();
for (int i = 0; i < (f_count-element_count); ++i) {
int cc = rnd.next();
while (n_vectors_indexes.count(cc) > 0) {
cc = rnd.next();
}
add_vector_to_set(cc);
}
}
//=============================================================================
/**
* Make a random candidate.
*/
template <int N, int D, size_t NM, size_t NMH>
void
Cube_Product_Checker<N, D, NM, NMH>::
make_candidate_from_space()
{
clear_sets();
n_vectors_indexes = (*candidate_space.begin());
}
//=============================================================================
/**
* If current candidate is in cache, than get info from cache.
*
* @return true if the current candidate is in cache.
*/
template <int N, int D, size_t NM, size_t NMH>
bool
Cube_Product_Checker<N, D, NM, NMH>::
check_cache()
{
map<Candidate, int>::const_iterator it;
if ((it = candidate_cache.find(n_vectors_indexes)) != candidate_cache.end()) {
best_result = it->second;
++cache_hits;
return true;
} else {
return false;
}
}
//=============================================================================
/**
* Update the cache with new result.
*/
template <int N, int D, size_t NM, size_t NMH>
void
Cube_Product_Checker<N, D, NM, NMH>::
update_cache()
{
if (candidate_cache.size() >= cache_limit) {
candidate_cache.clear();
//cout << "------------->> Cache cleared" << endl;
}
candidate_cache[n_vectors_indexes] = best_result;
}
//=============================================================================
/**
* Check the current set for being a solution.
*
* @return true if the current set is a solution.
*/
template <int N, int D, size_t NM, size_t NMH>
bool
Cube_Product_Checker<N, D, NM, NMH>::
check_vectors_for_goodness()
{
#ifdef VERY_DETAILED_OUTPUT
cout << "<" << thread_number << "> ";
cout << "Checking of vectors { ";
for (int i: n_vectors_indexes) {
cout << i << " ";
}
cout << "} has started..." << endl;
tw.watch();
#endif // VERY_DETAILED_OUTPUT
++checked_sets_count;
#ifdef USE_CACHE
if (check_cache()) {
if (best_result == f_count) {
return true;
} else {
return false;
}
}
#endif // USE_CACHE
vector<mm_vector_with_properties<NM>> nvwp; /// set of span vectors for SLAE
vector<mm_vector_with_properties<NM>> gvwp; /// set of good vectors for SLAE
Vectors_Presolve_Data<NM> v; /// vector presolve data for span vectors
Gauss_WP_Presolve_Data<NM> pwp(0); /// presolve data for SLAE for span vectors
Gauss_WP_Presolve_Data<NM> pwpg(0); /// presolve data for SLAE for good vectors
for (set<int>::iterator i = n_vectors_indexes.begin(); i != n_vectors_indexes.end(); ++i) {
nvwp.push_back(m_vectors[*i]);
v.add_vector(m_vectors[*i].v);
}
for (int i = 0; i < element_count; ++i) {
nvwp.push_back(r_vectors[i]);
v.add_vector(r_vectors[i].v);
}
if (!gauss_wp_presolve(nvwp, pwp)) { // vectors are linearly dependent
++lin_dependent_sets;
#ifdef USE_CACHE
update_cache();
#endif // USE_CACHE
return false;
}
gauss_wp_presolve(gvwp, pwpg);
v.presolve();
for (int i = 0; i < m_count; ++i) {
//if (!v.check(m_vectors[i].v)) { // discard vector by checking bits
// ++bit_check_hits;
// continue;
//}
#ifdef OUTPUT_STATISTICS
++gaussian_eliminations;
#endif // OUTPUT_STATISTICS
if (gauss_wp_solve(pwp, m_vectors[i])) { // if vector is in the current span
#ifdef OUTPUT_STATISTICS
++gaussian_eliminations;
#endif // OUTPUT_STATISTICS
if (!gauss_wp_solve(pwpg, m_vectors[i])) { // if not linearly dependent
good_vectors_indexes.insert(i);
gvwp.push_back(m_vectors[i]);
gauss_wp_presolve(gvwp, pwpg);
}
if (good_vectors_indexes.size() >= f_count) // we have found solution
break;
}
}
#ifdef VERY_DETAILED_OUTPUT
cout << " [" << tw.watch() << " s] Done." << endl;
#endif // VERY_DETAILED_OUTPUT
best_result = good_vectors_indexes.size();
if (best_result > (f_count-element_count)) {
if (best_result >= top_best_result) {
if (best_result > top_best_result) {
top_best_result = best_result;
top_best_solutions.clear();
#ifdef SAVE_BEST_RESULTS_TO_FILE
ofstream fout(to_string(thread_number)+string("bestsofar.txt"), ios_base::app);
fout << top_best_result << " vectors" << endl;
fout.close();
#endif // SAVE_BEST_RESULTS_TO_FILE
}
top_best_solutions.insert(n_vectors_indexes);
#ifdef SAVE_BEST_RESULTS_TO_FILE
ofstream fout(to_string(thread_number)+string("bestsofar.txt"), ios_base::app);
for (set<int>::iterator cc = n_vectors_indexes.begin(); cc != n_vectors_indexes.end(); ++cc) {
fout << *cc << " ";
}
fout << endl;
fout.close();
#endif // SAVE_BEST_RESULTS_TO_FILE
}
}
#ifdef USE_CACHE
update_cache();
#endif // USE_CACHE
if (good_vectors_indexes.size() >= f_count) { // there is a solution
#ifdef VERBOSE_OUTPUT
cout << " Good vectors have been found: { ";
for (set<int>::iterator cc = good_vectors_indexes.begin(); cc != good_vectors_indexes.end(); ++cc) {
cout << *cc << " ";
}
cout << "}" << endl;
#endif // VERBOSE_OUTPUT
#ifdef OUTPUT_SOLUTIONS_TO_FILE
ofstream mvfile("good.txt");
mvfile << "Found good vectors!!!\n";
for (set<int>::iterator cc = good_vectors_indexes.begin(); cc != good_vectors_indexes.end(); ++cc) {
mvfile << *cc << " ";
}
mvfile << "\n";
#endif // OUTPUT_SOLUTIONS_TO_FILE
return true;
}
return false;
}
//=============================================================================
/**
* Check all solution space for solutions (2x2 case).
*
* @return true if at least one solution was found.
*/
template <>
bool
Cube_Product_Checker<2, 2, 16, 4>::
check_for_good_vectors()
{
clear_statistics();
for (int c1 = 0; c1 < m_count-2; ++c1) {
//cout << c1 << endl;
for (int c2 = c1+1; c2 < m_count-1; ++c2) {
for (int c3 = c2+1; c3 < m_count; ++c3) {
if (*stop_signal)
return false;
clear_sets();
add_vector_to_set(c1);
add_vector_to_set(c2);
add_vector_to_set(c3);
if (check_vectors_for_goodness()) {
#ifdef OUTPUT_STATISTICS
solutions.insert(good_vectors_indexes);
++solution_distribution[good_vectors_indexes];
++raw_solution_count;
#endif // OUTPUT_STATISTICS
}
}
}
}
return true;
}
/**
* Check all solution space for solutions (2x2x2 case).
*
* @return true if a solution was found.
*/
template <>
bool
Cube_Product_Checker<2, 3, 512, 8>::
check_for_good_vectors()