Repository navigation
Expand file tree
/
Copy pathbinary_parser.cpp
More file actions
228 lines (212 loc) · 7.29 KB
/
Copy pathbinary_parser.cpp
File metadata and controls
228 lines (212 loc) · 7.29 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
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
#include <iostream>
#include <fstream>
#include <stdint.h>
#include "binary_parser.h"
// 64-bit big endian to little endian
# define bswap_64(x) \
((((x) & 0xff00000000000000ull) >> 56) \
| (((x) & 0x00ff000000000000ull) >> 40) \
| (((x) & 0x0000ff0000000000ull) >> 24) \
| (((x) & 0x000000ff00000000ull) >> 8) \
| (((x) & 0x00000000ff000000ull) << 8) \
| (((x) & 0x0000000000ff0000ull) << 24) \
| (((x) & 0x000000000000ff00ull) << 40) \
| (((x) & 0x00000000000000ffull) << 56))
// 16-bit big endian to little endian
#define bswap_16(x) \
((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
// Read meta data file, return Meta struct
Meta read_meta(string meta_file)
{
ifstream file;
file.open(meta_file.c_str());
string buf;
Meta meta;
getline(file, buf, ',');
meta.num_weights = atoll(buf.c_str());
getline(file, buf, ',');
meta.num_variables = atoll(buf.c_str());
getline(file, buf, ',');
meta.num_factors = atoll(buf.c_str());
getline(file, buf, ',');
meta.num_edges = atoll(buf.c_str());
getline(file, meta.weights_file, ',');
getline(file, meta.variables_file, ',');
getline(file, meta.factors_file, ',');
getline(file, meta.edges_file, ',');
file.close();
return meta;
}
// Read weights and load into factor graph
long long read_weights(string filename, dd::FactorGraph &fg)
{
ifstream file;
file.open(filename.c_str(), ios::in | ios::binary);
long long count = 0;
long long id;
bool isfixed;
char padding;
double initial_value;
while (file.good()) {
// read fields
file.read((char *)&id, 8);
file.read((char *)&padding, 1);
if (!file.read((char *)&initial_value, 8)) break;
// convert endian
id = bswap_64(id);
isfixed = padding;
long long tmp = bswap_64(*(uint64_t *)&initial_value);
initial_value = *(double *)&tmp;
// load into factor graph
fg.weights[fg.c_nweight] = dd::Weight(id, initial_value, isfixed);
fg.c_nweight++;
count++;
}
file.close();
return count;
}
// Read variables
long long read_variables(string filename, dd::FactorGraph &fg)
{
ifstream file;
file.open(filename.c_str(), ios::in | ios::binary);
long long count = 0;
long long id;
bool isevidence;
char padding1;
double initial_value;
short type;
long long edge_count;
long long cardinality;
while (file.good()) {
// read fields
file.read((char *)&id, 8);
file.read((char *)&padding1, 1);
file.read((char *)&initial_value, 8);
file.read((char *)&type, 2);
file.read((char *)&edge_count, 8);
if (!file.read((char *)&cardinality, 8)) break;
// convert endian
id = bswap_64(id);
isevidence = padding1;
type = bswap_16(type);
long long tmp = bswap_64(*(uint64_t *)&initial_value);
initial_value = *(double *)&tmp;
edge_count = bswap_64(edge_count);
cardinality = bswap_64(cardinality);
count++;
// add to factor graph
if (type == 0){ // boolean
if (isevidence) {
fg.variables[fg.c_nvar] = dd::Variable(id, DTYPE_BOOLEAN, true, 0, 1,
initial_value, initial_value, edge_count);
fg.c_nvar++;
fg.n_evid++;
} else {
fg.variables[fg.c_nvar] = dd::Variable(id, DTYPE_BOOLEAN, false, 0, 1,
0, 0, edge_count);
fg.c_nvar++;
fg.n_query++;
}
} else if (type == 1) { // multinomial
if (isevidence) {
fg.variables[fg.c_nvar] = dd::Variable(id, DTYPE_MULTINOMIAL, true, 0,
cardinality-1, initial_value, initial_value, edge_count);
fg.c_nvar ++;
fg.n_evid ++;
} else {
fg.variables[fg.c_nvar] = dd::Variable(id, DTYPE_MULTINOMIAL, false, 0,
cardinality-1, 0, 0, edge_count);
fg.c_nvar ++;
fg.n_query ++;
}
} else if (type == 3){
if (isevidence) {
fg.variables[fg.c_nvar] = dd::Variable(id, DTYPE_REAL, true, 0, cardinality,
initial_value, initial_value, edge_count);
fg.c_nvar++;
fg.n_evid++;
}else{
fg.variables[fg.c_nvar] = dd::Variable(id, DTYPE_REAL, true, 0, cardinality,
initial_value, initial_value, edge_count);
fg.c_nvar++;
fg.n_evid++;
}
}else {
cout << "[ERROR] Only Boolean and Multinomial variables are supported now!" << endl;
exit(1);
}
}
file.close();
return count;
}
long long read_factors(string filename, dd::FactorGraph &fg)
{
ifstream file;
file.open(filename.c_str(), ios::in | ios::binary);
long long count = 0;
long long id;
long long weightid;
short type;
long long edge_count;
while (file.good()) {
file.read((char *)&id, 8);
file.read((char *)&weightid, 8);
file.read((char *)&type, 2);
if (!file.read((char *)&edge_count, 8)) break;
id = bswap_64(id);
weightid = bswap_64(weightid);
type = bswap_16(type);
edge_count = bswap_64(edge_count);
count++;
fg.factors[fg.c_nfactor] = dd::Factor(id, weightid, type, edge_count);
fg.c_nfactor ++;
}
file.close();
return count;
}
long long read_edges(string filename, dd::FactorGraph &fg)
{
ifstream file;
file.open(filename.c_str(), ios::in | ios::binary);
long long count = 0;
long long variable_id;
long long factor_id;
long long position;
bool ispositive;
char padding;
long long equal_predicate;
while (file.good()) {
// read fields
file.read((char *)&variable_id, 8);
file.read((char *)&factor_id, 8);
file.read((char *)&position, 8);
file.read((char *)&padding, 1);
if (!file.read((char *)&equal_predicate, 8)) break;
variable_id = bswap_64(variable_id);
factor_id = bswap_64(factor_id);
position = bswap_64(position);
ispositive = padding;
equal_predicate = bswap_64(equal_predicate);
count++;
// wrong id
if(variable_id >= fg.n_var || variable_id < 0){
assert(false);
}
if(factor_id >= fg.n_factor || factor_id < 0){
std::cout << "wrong fid = " << factor_id << std::endl;
assert(false);
}
// add variables to factors
if (fg.variables[variable_id].domain_type == DTYPE_BOOLEAN) {
fg.factors[factor_id].tmp_variables.push_back(
dd::VariableInFactor(variable_id, fg.variables[variable_id].upper_bound, variable_id, position, ispositive));
} else {
fg.factors[factor_id].tmp_variables.push_back(
dd::VariableInFactor(variable_id, position, ispositive, equal_predicate));
}
fg.variables[variable_id].tmp_factor_ids.push_back(factor_id);
}
file.close();
return count;
}