This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
/
readConfigFile.cpp
273 lines (228 loc) · 9.69 KB
/
readConfigFile.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
#include "readConfigFile.h"
/****************************************************************************************************/
/* This function reads the configuration file (in the standard format) and returns an inputParameter type variable*/
inputParameters read_file(const string& fileName)
{
inputParameters newInput;
fstream myfile(fileName.c_str(), std::ios_base::in);
myfile >> newInput.nvar >> newInput.nlabel >> newInput.M >> newInput.m;
cout << "Parameters of the input file:" << endl << endl;
cout << "Number of variables = " << newInput.nvar << endl;
cout << "Number of labels = " << newInput.nlabel << endl;
cout << "M = " << newInput.M << " m = " << newInput.m << endl;
assert(("Number of variables should be positive" && newInput.nvar > 0));
assert(("Number of labels should be positive" && newInput.nlabel > 0));
assert(("m should be positive" && newInput.m > 0));
myfile.get();
for(int variable_count = 0; variable_count < newInput.nvar; variable_count++){
vector<double> unary_current_variable;
string line;
getline(myfile, line);
istringstream iss(line);
double unary;
while(iss >> unary){
unary_current_variable.push_back(unary);
}
assert("One or more labels not assigned unary potential" && (int) unary_current_variable.size() == newInput.nlabel);
newInput.unary_potentials.push_back(unary_current_variable);
}
myfile >> newInput.distance_type;
assert("Distance function type should be either 1 for linear or 2 for quadratic" && (newInput.distance_type== 1 || newInput.distance_type== 2));
if(newInput.distance_type == 1){
for(int i = 0; i < newInput.nlabel; i ++){
newInput.distance_func.push_back(double(i));
}
}
else{
for(int i = 0; i < newInput.nlabel; i ++){
newInput.distance_func.push_back(double(i*i));
}
}
myfile >> newInput.nclique;
cout << "Total number of cliques = " << newInput.nclique << endl;
for(int clique = 0; clique < newInput.nclique; clique++){
int size;
myfile >> size;
assert("Clique size must be greater than 0" && size > 0);
newInput.clique_sizes.push_back(size);
vector<int> current_clique_members;
for(int variable_count = 0; variable_count < size; variable_count++){
int variable_id;
myfile >> variable_id;
assert("Variable ids must be greater than non-negative and less than total number of variables" && variable_id > 0 && variable_id <= newInput.nvar);
current_clique_members.push_back(variable_id - 1);
}
newInput.clique_members.push_back(current_clique_members);
double weight;
myfile >> weight;
assert("Clique weights cannot be negative" && weight >= 0);
newInput.clique_weight.push_back(weight);
}
myfile.close();
return newInput;
}
/****************************************************************************************************/
/* This function reads the labeling contained in filename (separated by space) and returns a vector.
Its purpose is to read a labeling of another algorithm and compute its energy for debugging purposes.*/
vector<int> read_labeling(const string filename){
vector<int> labeling;
ifstream infile(filename);
int current_label;
while (infile >> current_label)
labeling.push_back(current_label - 1);
infile.close();
return labeling;
}
/****************************************************************************************************/
/* This function writes the given labeling (whether final or intermediate) to the given filename.
The labeling can then be visualized by being read by matlab or python code. */
void write_labeling(vector<int> labeling, const string filename){
ofstream out(filename);
int label_count = 0;
int max = (int) labeling.size();
while(label_count < max){
cout << labeling[label_count] << "\n";
label_count++;
}
out.close();
}
/****************************************************************************************************/
/* This function takes in the number of the unary file (1 for unary_1.txt, contained in ../Data/Unary/) and other parameters,
* assumes cliques as overlapping sliding windows, generates clique memberships and stores the corresponding information in the inputParameter type variable.*/
inputParameters generate_synthetic_input(double M, int m, double clique_weight, int unary_num, int distance_type){
//Initialize the values of parameters known already
inputParameters syntheticInput;
int sizex = 100, sizey = 100, size_window = 10;
syntheticInput.nvar = sizex * sizey;
syntheticInput.nlabel = 20;
syntheticInput.M = M;
syntheticInput.m = m;
//read unary file
string filename = "/home/pankaj/Max_of_convex_code_new/Data/Unary/unary_" + to_string(unary_num) + ".txt";
fstream input_file(filename.c_str(), ios_base::in);
for(int variable_count = 0; variable_count < syntheticInput.nvar; variable_count++){
vector<double> unary_current_variable;
string line;
getline(input_file, line);
istringstream iss(line);
double unary;
while(iss >> unary){
unary_current_variable.push_back(unary);
}
assert("One or more labels not assigned unary potential" && (int)unary_current_variable.size() == syntheticInput.nlabel);
syntheticInput.unary_potentials.push_back(unary_current_variable);
}
syntheticInput.distance_type = distance_type;
syntheticInput.nclique = (sizey - size_window + 1)*(sizex - size_window + 1);
if(syntheticInput.distance_type == 1){
for(int i = 0; i < syntheticInput.nlabel; i ++){
syntheticInput.distance_func.push_back(double(i));
}
}
else{
for(int i = 0; i < syntheticInput.nlabel; i ++){
syntheticInput.distance_func.push_back(double(i*i));
}
}
for(int i = 0; i < sizex - size_window + 1; i++){
for(int j = 0; j < sizey - size_window + 1; j++){
vector<int> members_current_clique;
for(int k = i; k < i + size_window; k ++){
for(int p = j; p < j + size_window; p++){
int var_num = k * sizey + p;
members_current_clique.push_back(var_num);
}
}
syntheticInput.clique_members.push_back(members_current_clique);
syntheticInput.clique_weight.push_back(clique_weight);
syntheticInput.clique_sizes.push_back(size_window * size_window);
}
}
//write to a file the parameter values and unary values
//write clique memberships and weights
//save file
return syntheticInput;
}
/****************************************************************************************************/
/* This function takes in the number of the unary file (1 for unary_1.txt, contained in ../Data/Unary/) and other parameters,
* assumes 4-connected pairwise relationships, generates clique memberships and stores the corresponding information in the inputParameter type variable.*/
inputParameters generate_synthetic_input_4connected(double M, int m, double clique_weight, int unary_num, int distance_type){
//Initialize the values of parameters known already
inputParameters syntheticInput;
int sizex = 100, sizey = 100;
syntheticInput.nvar = sizex * sizey;
syntheticInput.nlabel = 20;
syntheticInput.M = M;
syntheticInput.m = m;
int nclique = 0;
//read unary file
string filename = "../Data/Unary/unary_" + to_string(unary_num) + ".txt";
fstream input_file(filename.c_str(), ios_base::in);
for(int variable_count = 0; variable_count < syntheticInput.nvar; variable_count++){
vector<double> unary_current_variable;
string line;
getline(input_file, line);
istringstream iss(line);
double unary;
while(iss >> unary){
unary_current_variable.push_back(unary);
}
assert("One or more labels not assigned unary potential" && (int)unary_current_variable.size() == syntheticInput.nlabel);
syntheticInput.unary_potentials.push_back(unary_current_variable);
}
syntheticInput.distance_type = distance_type;
if(syntheticInput.distance_type == 1){
for(int i = 0; i < syntheticInput.nlabel; i ++){
syntheticInput.distance_func.push_back(double(i));
}
}
else{
for(int i = 0; i < syntheticInput.nlabel; i ++){
syntheticInput.distance_func.push_back(double(i*i));
}
}
for(int i = 0; i < sizex; i++){
for(int j = 0; j < sizey; j++){
if((i - 1) >= 0){
syntheticInput.clique_members.push_back({i*sizey + j, (i-1)*sizey + j});
syntheticInput.clique_weight.push_back(clique_weight);
syntheticInput.clique_sizes.push_back(2);
nclique = nclique + 1;
}
if((j + 1) < sizey){
syntheticInput.clique_members.push_back({i*sizey + j, i*sizey + j + 1});
syntheticInput.clique_weight.push_back(clique_weight);
syntheticInput.clique_sizes.push_back(2);
nclique = nclique + 1;
}
}
}
syntheticInput.nclique = nclique;
return syntheticInput;
}
/****************************************************************************************************/
/* This function takes in an inputParameter variable and prints the information contained in it (in the form of the configuration file).
* This can be useful to inspect the contents for small examples for debugging purposes.*/
void printInputParameter(inputParameters newInput){
cout << newInput.nvar << " " << newInput.nlabel << endl;
cout << newInput.M << endl;
cout << newInput.m << endl;
for(int i = 0; i < newInput.nvar; i++){
for(int j = 0; j < newInput.nlabel; j++){
cout << newInput.unary_potentials[i][j] << " ";
}
cout << endl;
}
// cout << newInput.distance_type << endl;
cout << newInput.nclique << endl;
for(int i = 0; i < newInput.nclique; i++){
int clique_size = newInput.clique_sizes[i];
cout << clique_size << " ";
for(int j = 0; j < clique_size; j++){
cout << newInput.clique_members[i][j] + 1 << " ";
}
cout << endl;
cout << newInput.clique_weight[i] << endl;
}
}
/****************************************************************************************************/