update the overflow check in factors, still needs more work tbd

This commit is contained in:
Tiago Gomes 2012-05-04 14:28:07 +01:00
parent 0e01a51929
commit 660e15e2ac
3 changed files with 40 additions and 18 deletions

View File

@ -90,9 +90,8 @@ class TFactor
for (unsigned i = 0; i < g_args.size(); i++) {
int idx = indexOf (g_args[i]);
if (idx == -1) {
Params::size_type newSize = params_.size() * g_ranges[i];
if (newSize > Util::maxUnsigned()) {
// factor will become to bigger, is not worth to continue
ullong newSize = params_.size() * g_ranges[i];
if (newSize > params_.max_size()) {
cerr << "error: an overflow occurred on factor multiplication" ;
cerr << endl;
abort();
@ -211,7 +210,7 @@ class TFactor
return true;
}
double& operator[] (unsigned idx)
double& operator[] (psize_t idx)
{
assert (idx < params_.size());
return params_[idx];

View File

@ -16,15 +16,17 @@ class Factor;
class VarNode;
class FacNode;
typedef vector<double> Params;
typedef unsigned VarId;
typedef vector<VarId> VarIds;
typedef vector<Var*> Vars;
typedef vector<VarNode*> VarNodes;
typedef vector<FacNode*> FacNodes;
typedef vector<Factor*> Factors;
typedef vector<string> States;
typedef vector<unsigned> Ranges;
typedef vector<double> Params;
typedef unsigned VarId;
typedef vector<VarId> VarIds;
typedef vector<Var*> Vars;
typedef vector<VarNode*> VarNodes;
typedef vector<FacNode*> FacNodes;
typedef vector<Factor*> Factors;
typedef vector<string> States;
typedef vector<unsigned> Ranges;
typedef Params::size_type psize_t;
typedef unsigned long long ullong;
enum InfAlgorithms

View File

@ -547,6 +547,7 @@ Parfactor::print (bool printParams) const
if (printParams == false) {
cout << "Params: " ;
if (params_.size() <= 32) {
cout.precision(10);
cout << params_ << endl;
} else {
cout << "|" << params_.size() << "|" << endl;
@ -595,9 +596,8 @@ Parfactor::expandPotential (
unsigned newRange,
const vector<unsigned>& sumIndexes)
{
Params::size_type newSize = (params_.size() / ranges_[fIdx]) * newRange;
if (newSize > Util::maxUnsigned()) {
// factor will become to bigger, is not worth to continue
ullong newSize = (params_.size() / ranges_[fIdx]) * newRange;
if (newSize > params_.max_size()) {
cerr << "error: an overflow occurred when performing expansion" ;
cerr << endl;
abort();
@ -675,6 +675,10 @@ Parfactor::alignAndExponentiate (Parfactor* g1, Parfactor* g2)
unsigned condCount2 = g2->constr()->getConditionalCount (Y_2);
LogAware::pow (g1->params(), 1.0 / condCount2);
LogAware::pow (g2->params(), 1.0 / condCount1);
//cout << "g1::::::::::::::::::::::::" << endl;
//g1->print();
//cout << "g2::::::::::::::::::::::::" << endl;
//g2->print();
// the alignment should be done in the end or else X_1 and X_2
// will refer to the old log var names on the code above
align (g1, X_1, g2, X_2);
@ -719,8 +723,25 @@ Parfactor::align (
++ freeLogVar;
}
}
// cout << "theta1: " << theta1 << endl;
// cout << "theta2: " << theta2 << endl;
//cout << "theta1: " << theta1 << endl;
//cout << "theta2: " << theta2 << endl;
LogVars discardedLvs1 = theta1.getDiscardedLogVars();
for (unsigned i = 0; i < discardedLvs1.size(); i++) {
unsigned condCount = g1->constr()->getConditionalCount (discardedLvs1[i]);
cout << "discarding g1" << discardedLvs1[i];
cout << " cc = " << condCount << endl;
LogAware::pow (g1->params(), condCount);
}
LogVars discardedLvs2 = theta2.getDiscardedLogVars();
for (unsigned i = 0; i < discardedLvs2.size(); i++) {
unsigned condCount = g2->constr()->getConditionalCount (discardedLvs2[i]);
cout << "discarding g2" << discardedLvs2[i];
cout << " cc = " << condCount << endl;
//if (condCount != 1) {
// theta2.rename (discardedLvs2[i], freeLogVar);
//}
LogAware::pow (g2->params(), condCount);
}
g1->applySubstitution (theta1);
g2->applySubstitution (theta2);
}