-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactor.cpp
417 lines (380 loc) · 7.69 KB
/
Factor.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
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
#include <iostream>
#include <exception>
#include "Factor.h"
Factor::Factor()
{
}
Factor::Factor(FactorVarVector var, std::vector<double> val)
{
variables = var;
values = val;
updateIsValid();
if(!is_valid)
{
std::cerr << "Invalid factor" << std::endl;
}
}
Factor::~Factor()
{
}
void Factor::updateIsValid()
{
if(variables.size()==0 && values.size()==0)
{
is_valid = true;
return;
}
unsigned int valSize=1;
for(unsigned int i=0;i<variables.size();i++)
{
valSize=valSize*variables[i].second;
}
if(values.size()!=valSize)
{
is_valid = false;
return;
}
is_valid = true;
return;
}
void Factor::setCounter(unsigned int * a)
{
operationsCounter = a;
}
void Factor::setVariables(FactorVarVector _var)
{
variables = _var;
updateIsValid();
}
FactorVarVector Factor::getVariables()
{
return variables;
}
void Factor::setValues(std::vector<double> _val)
{
values = _val;
updateIsValid();
}
double Factor::getValue(FactorVarVector assignment)
{
if(!is_valid)
{
throw std::runtime_error("Invalid Factor: getValue assignment");
}
unsigned int idx;
try
{
idx = getIndex(assignment);
}
catch(std::invalid_argument& ia)
{
throw ia;
}
if(idx >= values.size())
{
throw std::out_of_range("Wrong assigment");
}
return values[idx];
}
double Factor::getValue(unsigned int idx)
{
if(!is_valid)
{
throw std::runtime_error("Invalid Factor: getValue uint");
}
if(idx >= values.size())
{
throw std::out_of_range("Index out of range");
}
return values[idx];
}
FactorVarVector Factor::getAssignment(unsigned int idx)
{
if(!is_valid)
{
throw std::runtime_error("Invalid Factor: getAssignment");
}
if(idx>=values.size())
{
throw std::out_of_range("Input out of Range");
}
FactorVarVector assRet = variables;
unsigned int den;
for(unsigned int i=0;i<assRet.size();i++)
{
den=1;
for(unsigned int j=0; j<i; j++)
{
den = den*variables[j].second;
}
assRet[i].second = idx / (den);
assRet[i].second = assRet[i].second % variables[i].second;
}
return assRet;
}
unsigned int Factor::getIndex(FactorVarVector assign)
{
unsigned int retIdx=0;
if(assign.size() != variables.size())
{
throw std::invalid_argument("Bad variables size");
}
for(unsigned int i=0;i<variables.size();i++)
{
if(assign[i].first!=variables[i].first || assign[i].second>=variables[i].second)
{
throw std::invalid_argument("Bad variables name or assignment value");
}
}
for(unsigned int i=0;i<assign.size();i++)
{
if(i==0)
{
retIdx = retIdx + assign[i].second;
}
else
{
unsigned int mul=1;
for(unsigned int j=0; j<i; j++)
{
mul = mul*variables[j].second;
}
retIdx = retIdx + assign[i].second*mul;
}
}
return retIdx;
}
bool Factor::isValid()
{
return is_valid;
}
void Factor::clear()
{
variables.clear();
values.clear();
}
void Factor::printFactor()
{
if(!is_valid)
{
std::cout << "Invalid Factor Print" << std::endl;
}
for(int i=((int)(variables.size())-1);i>=0;i--)
{
std::cout << variables[i].first << " ";
}
std::cout << std::endl;
FactorVarVector ass;
for(unsigned int i=0;i<values.size();i++)
{
ass = getAssignment(i);
for(int j=((int)(ass.size())-1);j>=0;j--)
{
std::cout << ass[j].second;
for(int k=0;k<((int)(variables[j].first.size())-1);k++)
{
std::cout << " ";
}
std::cout << " ";
}
std::cout << " | ";
std::cout << getValue(i) << std::endl;
}
}
double Factor::normalize()
{
if(!is_valid)
{
return 0;
}
double partitionZ = 0;
//double aux;
for(unsigned int i=0;i<values.size();i++)
{
/*aux = partitionZ + values[i];
if(aux < partitionZ)
{
std::cout << "Partition Function Overflow, diving factor by 2" << std::endl;
*this = *this/2;
i=0;
partitionZ = 0;
aux = 0;
continue;
}*/
partitionZ += values[i];
(*operationsCounter)++;
}
*this = *this/partitionZ;
return partitionZ;
}
Factor Factor::factorEliminationOperation(Factor *a, FactorVar *eliminateVar, std::function<double(Factor*,FactorVarVector*,unsigned int,unsigned int,unsigned int)> operation)
{
if(!a->isValid())
{
std::cerr << "Invalid factor factorEliminationOperation" << std::endl;
//Invalid Factor
return *a;
}
FactorVarVector new_ass, ass;
FactorVarVector new_variables;
std::vector<double> new_values;
ass = variables;
FactorVarVector::iterator iter = std::find_if(ass.begin(), ass.end(), comp(eliminateVar->first));
unsigned int eliminateIndex = (iter - ass.begin());
if(iter==ass.end())
{
//Variable not found
return *a;
}
for(unsigned int i=0; i<variables.size(); i++)
{
if(eliminateVar->first != variables[i].first)
{
new_variables.push_back(variables[i]);
}
}
unsigned int valSize=1;
for(unsigned int i=0;i<new_variables.size();i++)
{
valSize=valSize*new_variables[i].second;
}
new_values.resize(valSize,0);
Factor newFactor(new_variables, new_values);
for(unsigned int i=0;i<valSize;i++)
{
try
{
new_ass = newFactor.getAssignment(i);
}
catch (std::runtime_error& rte)
{
std::cerr << rte.what() << std::endl;
newFactor.clear();
break;
}
catch (std::out_of_range& oor)
{
std::cerr << oor.what() << std::endl;
newFactor.clear();
break;
}
for(unsigned int j=0;j<ass.size();j++)
{
if(j<eliminateIndex)
{
ass[j].second = new_ass[j].second;
}
else if(j>eliminateIndex)
{
ass[j].second = new_ass[j-1].second;
}
}
newFactor.values[i] = operation(a,&ass,eliminateIndex,variables[eliminateIndex].second,eliminateVar->second);
}
return newFactor;
}
Factor Factor::factorBinaryOperation(Factor *a, Factor *b, std::function<double(double,double)> operation)
{
Factor ret;
if(!a->is_valid || !b->is_valid)
{
std::cerr << "Invalid B or A: " << std::endl;
return ret;
}
ret.setCounter(a->operationsCounter);
for(unsigned int i=0;i<a->variables.size();i++)
{
ret.variables.push_back(a->variables[i]);
}
for(unsigned int i=0;i<b->variables.size();i++)
{
if(std::find(ret.variables.begin(), ret.variables.end(), b->variables[i])==ret.variables.end())
{
ret.variables.push_back(b->variables[i]);
}
}
unsigned int valSize=1;
for(unsigned int i=0;i<ret.variables.size();i++)
{
valSize=valSize*ret.variables[i].second;
}
FactorVarVector ass, assB, assA;
assB = b->variables;
assA = a->variables;
ret.values.resize(valSize, 0);
ret.updateIsValid();
for(unsigned int i=0;i<valSize;i++)
{
try
{
ass = ret.getAssignment(i);
}
catch (std::runtime_error& rte)
{
std::cerr << rte.what() << std::endl;
ret.clear();
break;
}
catch (std::out_of_range& oor)
{
std::cerr << oor.what() << std::endl;
ret.clear();
break;
}
for(unsigned int j=0; j<assB.size(); j++)
{
FactorVarVector::iterator iter = std::find_if(ass.begin(), ass.end(), comp(assB[j].first));
if(iter != ass.end())
{
assB[j].second = iter->second;
}
else
{
std::cerr << "ERROR! Variable B not found" << std::endl;
ret.clear();
return ret;
}
}
for(unsigned int j=0; j<assA.size(); j++)
{
FactorVarVector::iterator iter = std::find_if(ass.begin(), ass.end(), comp(assA[j].first));
if(iter != ass.end())
{
assA[j].second = iter->second;
}
else
{
std::cerr << "ERROR! Variable A not found" << std::endl;
ret.clear();
return ret;
}
}
try
{
double aValue = a->getValue(assA);
double bValue = b->getValue(assB);
(*ret.operationsCounter)++;
ret.values[i] = operation(bValue,aValue);
}
catch (std::invalid_argument& ia)
{
std::cerr << ia.what() << std::endl;
ret.clear();
break;
}
catch (std::runtime_error& rte)
{
std::cerr << rte.what() << std::endl;
ret.clear();
break;
}
catch (std::out_of_range& oor)
{
std::cerr << oor.what() << std::endl;
ret.clear();
break;
}
}
ret.updateIsValid();
return ret;
}