forked from clelidm/MinCompSpin_Greedy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperations_OnData.cpp
More file actions
180 lines (154 loc) · 6.12 KB
/
Copy pathOperations_OnData.cpp
File metadata and controls
180 lines (154 loc) · 6.12 KB
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
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <list>
#include <bitset>
#include <map>
#include <cstring>
/********************************************************************/
/************************** CONSTANTS *************************/
/********************************************************************/
#include "data.h"
map<unsigned int, __int128_t> read_communities(string file)
{
map<unsigned int, __int128_t> Partition;
string line, line2;
__int128_t Op = 1;
Op <<= n - 1;
vector<int> comm;
ifstream myfile(file.c_str());
if (myfile.is_open())
{
while (getline(myfile, line))
{
stringstream ss(line);
while (getline(ss, line2, '\t'))
{
comm.push_back(stoi(line2));
}
Partition[comm[1]] += Op;
Op >>= 1;
comm.clear();
}
myfile.close();
}
return Partition;
}
/******************************************************************************/
/************************** READ FILE **********************************/
/******************************************************************************/
/************** READ DATA and STORE them in Nset ************************/
map<__int128_t, unsigned int> read_datafile(unsigned int *N, string file = datafilename) // O(N) where N = data set size
{
string line, line2; char c = '1';
__int128_t nb = 0, Op;
(*N) = 0; // N = dataset size
//cout << endl << "--->> Read \"" << datafilename << "\",\t Build Nset...";
// ***** data are store in Nset: ********************************
map<__int128_t, unsigned int> Nset; // Nset[mu] = #of time state mu appears in the data set
ifstream myfile (file.c_str());
if (myfile.is_open())
{
while ( getline (myfile,line))
{
line2 = line.substr (0,n); //take the n first characters of line
Op = un << (n - 1);
nb = 0;
for (auto &elem: line2) //convert string line2 into a binary integer
{
if (elem == c) { nb += Op; }
Op = Op >> 1;
}
Nset[nb] += 1;
//cout << line << endl; //cout << nb << " : " << bitset<n>(nb) << endl;
(*N)++;
}
myfile.close();
}
else
{
cout << endl << " ########## Unable to open file ##########" << endl << endl;
//(*N) = 0;
}
//cout << "\t\t data size N = " << (*N) << endl;
return Nset;
}
/******************************************************************************/
/************************** PRINT Nset **********************************/
/******************************************************************************/
void Print_File_Nset (map<__int128_t, unsigned int> Nset, unsigned int N, string OUTPUTfilename)
// map.second = nb of time that the state map.first appears in the data set
{
map<__int128_t, unsigned int>::iterator it;
int Ncontrol = 0;
__int128_t un = 1;
fstream file(OUTPUTfilename.c_str(), ios::out);
file << "#N = " << N << endl;
file << "#Total number of accessible states = 2^(" << n << ") - 1" << endl;
file << "#Number of visited states, Nset.size() = " << Nset.size() << endl;
file << "#" << endl;
file << "#1: state \t #2: nb of pts in state \t #3: Pba state" << endl;
for (it = Nset.begin(); it!=Nset.end(); ++it)
{
file << bitset<n>((*it).first) << " => " << (*it).second; // << endl;
file << " \t P = " << ((*it).second) / (float) N << endl;
Ncontrol += (*it).second;
}
if (Ncontrol != N) { cout << "Error function \'read_Nset\': Ncontrol != N" << endl; }
file.close();
}
/******************************************************************************/
/********************* CHANGE of BASIS: one datapoint ********************/
/******************************************************************************/
// Given a choice of a model (defined by the m basis vector) --> return the new m-state (state in the new m-basis)
// Rem: must have m <= n
__int128_t transform_mu_basis(__int128_t mu, list<__int128_t> basis)
{
__int128_t un_i = 1, proj;
__int128_t final_mu = 0;
list<__int128_t>::iterator phi_i;
for(phi_i = basis.begin(); phi_i != basis.end(); ++phi_i)
{
proj = (*phi_i) & mu;
bitset<n> hi{ static_cast<unsigned long long>(proj >> 64) },
lo{ static_cast<unsigned long long>(proj) },
bits{ (hi << 64) | lo };
if ( (bits.count() % 2) == 1) // odd number of 1, i.e. sig_i = 1
{
final_mu += un_i;
}
un_i = (un_i << 1);
}
return final_mu;
}
/******************************************************************************/
/************************** K_SET *********************************************/
/******************************************************************************/
// Build Kset for the states written in the basis of the m-chosen independent
// operator on which the SC model is based:
map<__int128_t, unsigned int> build_Kset(map<__int128_t, unsigned int> Nset, list<__int128_t> Basis, bool print_bool=false)
// sig_m = sig in the new basis and cut on the m first spins
// Kset[sig_m] = #of time state mu_m appears in the data set
{
map<__int128_t, unsigned int>::iterator it;
map<__int128_t, unsigned int > Kset;
__int128_t s; // initial state
__int128_t sig_m; // transformed state and to the m first spins
unsigned int ks=0; // number of time state s appear in the dataset
cout << endl << "--->> Build Kset..." << endl;
//Build Kset:
for (it = Nset.begin(); it!=Nset.end(); ++it)
{
s = it->first; // state s
ks = it->second; // # of times s appears in the data set
sig_m = transform_mu_basis(s, Basis);
// sig_m = bitset<m>(bitset<m>(mu).to_string()).to_ulong(); //bitset<m>(mu).to_ulong(); // mu|m
if (print_bool) { cout << bitset<n>(s) << " \t" << ": \t" << bitset<n>(sig_m) << endl; }
Kset[sig_m] += ks;
//Kset[mu_m].second.push_back(make_pair(mu, N_mu));
}
cout << endl;
return Kset;
}