[debug] Trying to save filling state

This commit is contained in:
2020-03-20 14:53:01 +03:00
parent 0d284b7bfc
commit a51386a890

View File

@@ -12,8 +12,8 @@
using namespace std; using namespace std;
typedef uint16_t Storage; typedef uint32_t Storage;
const size_t ARGS_COUNT = 2; const size_t ARGS_COUNT = 5;
const size_t FUNCTION_LEN = 1ll << ARGS_COUNT; const size_t FUNCTION_LEN = 1ll << ARGS_COUNT;
const size_t FUNCTIONS_COUNT = 1ll << FUNCTION_LEN; const size_t FUNCTIONS_COUNT = 1ll << FUNCTION_LEN;
typedef Function<Storage, FUNCTION_LEN> MyFunction; typedef Function<Storage, FUNCTION_LEN> MyFunction;
@@ -182,12 +182,20 @@ vector<MyFunction> get_linear_components() {
res.push_back(MyFunction("0101010101010101")); // f = x_4 res.push_back(MyFunction("0101010101010101")); // f = x_4
function_formulas[res.back().value()] = "x_4"; function_formulas[res.back().value()] = "x_4";
} else if constexpr ( ARGS_COUNT == 5 ) { } else if constexpr ( ARGS_COUNT == 5 ) {
res.push_back(MyFunction("00000000000000000000000000000000")); // f = 0
function_formulas[res.back().value()] = "0";
res.push_back(MyFunction("11111111111111111111111111111111")); // f = 1 res.push_back(MyFunction("11111111111111111111111111111111")); // f = 1
function_formulas[res.back().value()] = "1";
res.push_back(MyFunction("00000000000000001111111111111111")); // f = x_1 res.push_back(MyFunction("00000000000000001111111111111111")); // f = x_1
function_formulas[res.back().value()] = "x_1";
res.push_back(MyFunction("00000000111111110000000011111111")); // f = x_2 res.push_back(MyFunction("00000000111111110000000011111111")); // f = x_2
function_formulas[res.back().value()] = "x_2";
res.push_back(MyFunction("00001111000011110000111100001111")); // f = x_3 res.push_back(MyFunction("00001111000011110000111100001111")); // f = x_3
function_formulas[res.back().value()] = "x_3";
res.push_back(MyFunction("00110011001100110011001100110011")); // f = x_4 res.push_back(MyFunction("00110011001100110011001100110011")); // f = x_4
function_formulas[res.back().value()] = "x_4";
res.push_back(MyFunction("01010101010101010101010101010101")); // f = x_5 res.push_back(MyFunction("01010101010101010101010101010101")); // f = x_5
function_formulas[res.back().value()] = "x_5";
} else { } else {
assert (("bad args_count", false)); assert (("bad args_count", false));
} }
@@ -227,9 +235,11 @@ vector<MyFunction> get_all_monomials(const vector<MyFunction> &linear_combinatio
for (auto el_second: res) { for (auto el_second: res) {
bool is_added_now = res.insert(el_first and el_second).second; bool is_added_now = res.insert(el_first and el_second).second;
if ( is_added_now ) { if ( is_added_now ) {
Storage cur_value = (el_first and el_second).value(); if constexpr ( ARGS_COUNT < 5 ) {
function_formulas[cur_value] = preprocess_factor(function_formulas[el_first.value()]) Storage cur_value = (el_first and el_second).value();
+ " * " + preprocess_factor(function_formulas[el_second.value()]); function_formulas[cur_value] = preprocess_factor(function_formulas[el_first.value()])
+ " * " + preprocess_factor(function_formulas[el_second.value()]);
}
} }
is_added = is_added or is_added_now; is_added = is_added or is_added_now;
} }
@@ -241,6 +251,41 @@ string preprocess_monom(string monom) {
return monom; return monom;
} }
inline bool exists_test0 (const std::string& name) {
ifstream f(name.c_str());
return f.good();
}
string get_out_file_name(size_t rank_ind) {
return string("base_" + to_string(ARGS_COUNT) + "_rank_" + to_string(rank_ind) + ".txt");
}
size_t recover_ranks(vector< vector<MyFunction> >& ranks, vector<int8_t>& used_map, size_t& functions_remains) {
for (size_t rank_ind = 2; true; ++rank_ind) {
ifstream f_in(get_out_file_name(rank_ind).c_str(), std::ios::binary);
if ( not f_in.good() )
return rank_ind;
ranks.push_back(vector<MyFunction>());
while ( not f_in.eof() ) {
Storage function_value;
f_in >> function_value;
MyFunction f(function_value);
used_map.at(f.value()) = rank_ind;
--functions_remains;
ranks.at(rank_ind).push_back(f);
}
}
}
void save_rank(size_t rank_ind, vector<MyFunction>& rank_items) {
ofstream f_out(get_out_file_name(rank_ind).c_str(), ios::binary);
for (auto f: rank_items) {
Storage function_value = f.value();
f_out << reinterpret_cast<char*>(&function_value), sizeof(function_value);
}
}
void fill_ranks(vector<MyFunction> monomials) { void fill_ranks(vector<MyFunction> monomials) {
vector<int8_t> used_map(FUNCTIONS_COUNT, 0); vector<int8_t> used_map(FUNCTIONS_COUNT, 0);
size_t functions_remains = FUNCTIONS_COUNT; size_t functions_remains = FUNCTIONS_COUNT;
@@ -255,7 +300,8 @@ void fill_ranks(vector<MyFunction> monomials) {
--functions_remains; --functions_remains;
used_map.at(el.value()) = 1; used_map.at(el.value()) = 1;
} }
size_t total_ranks = 1; size_t total_ranks = recover_ranks(ranks, used_map, functions_remains);
cout << " total_ranks = " << total_ranks << endl;
for (total_ranks = 1; functions_remains; ++total_ranks) { for (total_ranks = 1; functions_remains; ++total_ranks) {
ranks.push_back(vector<MyFunction>()); ranks.push_back(vector<MyFunction>());
cout << "rank index = " << total_ranks << " remains: " << functions_remains << endl; cout << "rank index = " << total_ranks << " remains: " << functions_remains << endl;
@@ -265,9 +311,11 @@ void fill_ranks(vector<MyFunction> monomials) {
if ( used_map[res_el.value()] == 0 ) { if ( used_map[res_el.value()] == 0 ) {
--functions_remains; --functions_remains;
function_formulas[res_el.value()] = if constexpr ( ARGS_COUNT < 5 ) {
preprocess_monom(function_formulas[el_first.value()]) + function_formulas[res_el.value()] =
" + " + preprocess_monom(function_formulas[el_second.value()]); preprocess_monom(function_formulas[el_first.value()]) +
" + " + preprocess_monom(function_formulas[el_second.value()]);
}
used_map.at(res_el.value()) = total_ranks; used_map.at(res_el.value()) = total_ranks;
ranks.at(total_ranks).push_back(res_el); ranks.at(total_ranks).push_back(res_el);
@@ -277,6 +325,17 @@ void fill_ranks(vector<MyFunction> monomials) {
if ( total_ranks - 1 > 1 ) if ( total_ranks - 1 > 1 )
ranks.at(total_ranks - 1).clear(); // больше не нужен ranks.at(total_ranks - 1).clear(); // больше не нужен
cout << "size for rank " << total_ranks << " is " << ranks.at(total_ranks).size() << endl; cout << "size for rank " << total_ranks << " is " << ranks.at(total_ranks).size() << endl;
save_rank(total_ranks, ranks.at(total_ranks));
}
if ( ranks.at(ranks.size() - 1).size() != 0 ) {
ranks.push_back(vector<MyFunction>());
total_ranks += 2;
}
cout << "ranks=" << total_ranks << endl;
for (size_t i = 0; i < ranks.size(); ++i) {
cout << "i=" << i << endl;
for (auto v: ranks.at(i))
cout << " " << v.string() << endl;
} }
auto possible_tranformations = get_good_matrices(); auto possible_tranformations = get_good_matrices();
@@ -306,6 +365,7 @@ void fill_ranks(vector<MyFunction> monomials) {
used_map[marked_function.value()] = 0; used_map[marked_function.value()] = 0;
++total_functions; ++total_functions;
} }
cout << "cur_rank - 1 =" << cur_rank - 1 << endl;
ranks.at(cur_rank - 1).push_back(current_fn); ranks.at(cur_rank - 1).push_back(current_fn);
} }
if ( total_functions != FUNCTIONS_COUNT) if ( total_functions != FUNCTIONS_COUNT)