📄 discretepotential.cc.svn-base
字号:
SingleDiscretePotential::~SingleDiscretePotential(){}// Virtual methodsvoid SingleDiscretePotential::setup_potential_values (const std::vector <double> & a){ //cout << a[0] << " ;; " << a[1] << endl; if (a.size() != single_potential_table.size() ) { cout << "Mismatch. In order to instantiate the SingleDiscretePotential, the exact number of values is requested.\n"; cout << "That value is equal to " << single_potential_table.size() << endl; cout << "(I received " << a.size() << ")" << endl; } else { std::copy ( a.begin(), a.end(), single_potential_table.begin() ); }}double SingleDiscretePotential::sum_product(unsigned int, unsigned int, vector <double> &){ cout << "Error, we shouldn't call the Sum Product on a SDP" << endl; return 1.0;}Potential * SingleDiscretePotential::clone (){ return new SingleDiscretePotential(*this);}void SingleDiscretePotential::set_observed_variable (const RandomVariable &){}void SingleDiscretePotential::unset_observed_variable (const RandomVariable &){}// **************** End of SingleDiscretePotential Implementation ***********//UniqueDiscretePotential::UniqueDiscretePotential(): setup(false){}void UniqueDiscretePotential::setup_potential_values (const std::vector <double> & a){ if (setup) { return; } if (a.size() != single_potential_table.size() ) { cout << "Mismatch. In order to instantiate the UniquePotential, the exact number of values is requested.\n"; cout << "That value is equal to " << single_potential_table.size() << endl; cout << "(I received " << a.size() << ")" << endl; } else // We use an insert adaptator, which means this function shouldn't be used if the potential table has been somehow altered in any way // NO LONGER USING AN INSERT ADAPTOR { //std::copy ( a.begin(), a.end(), inserter (potential_table,potential_table.begin()) ); std::copy ( a.begin(), a.end(), single_potential_table.begin() ); } setup = true;}void UniqueDiscretePotential::add_variable (const DiscreteRandomVariable & a){ // We have to put that first, because of the ONE difference between the size and the actual index of our variables... // cout << "a.get_index()" << a.get_index() << ", size: " << number_of_variable_values.size() << endl; correspondance_table[ a.get_index() ] = number_of_variable_values.size() ; if (number_of_variable_values.empty()) { help_vector.push_back(1); } else { help_vector.push_back( help_vector.back() * number_of_variable_values.back() ); } number_of_variable_values.push_back(a.get_number_values()); variable_values.push_back(a.last_sampled_value); fixed_variable.push_back(false); //fixed_variable_values.push_back(a.last_sampled_value); single_potential_table.resize(help_vector.back() * number_of_variable_values.back(), 0.0);}std::vector <double> UniqueDiscretePotential::single_potential_table;UniqueDiscretePotential::~UniqueDiscretePotential(){}double UniqueDiscretePotential::recursive_internal_sum_product_messages_fixed (unsigned int index, const vector <double> & msg_product){ double sum (0.0); if (index == number_of_variable_values.size() -1) { //cout << "size: " << msg_product.size() << endl; //cout << "size: " << potential_table.size() << endl; if (fixed_variable[index]) { //cout << "Fixed: " << index << endl; //variable_values[index] = fixed_variable_values[index]; unsigned int a = inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0); sum += single_potential_table[a] * msg_product[a]; } else { variable_values[index] = 0; unsigned int a = inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0); for (unsigned int i = 0 ; i < number_of_variable_values[index] ; ++i) { sum += single_potential_table[a] * msg_product[a]; a += help_vector.back(); } } return sum; } if (fixed_variable[index]) { //cout << "Fixed: " << index << endl; //variable_values[index] = fixed_variable_values[index]; sum += recursive_internal_sum_product_messages_fixed(index+1, msg_product); } else { for (unsigned int i = 0 ; i < number_of_variable_values[index] ; ++i) { variable_values[index] = i; sum += recursive_internal_sum_product_messages_fixed(index+1, msg_product); } } return sum; }// ********** End of UniqueDiscretePotential Implementation ********** //QMRDiscretePotential::QMRDiscretePotential(): table_size(1){}void QMRDiscretePotential::setup_potential_values (const std::vector <double> & a){ if (a.size() != (thetha.size() +1 ) ) { cout << "Mismatch. In order to instantiate the QMRPotential, the exact number of values is requested.\n"; cout << "That value is equal to " << thetha.size() +1<< endl; cout << "(I received " << a.size() << ")" << endl; } else { //cout << "We are OK for instantiating QMRPotential." << endl; std::vector <double>::const_iterator it = a.begin(); thetha_0 = -log (*it); ++it; for (std::vector <double>::iterator jt = thetha.begin(); jt != thetha.end(); ++jt) { *jt = -log(*it); ++it; } } }void QMRDiscretePotential::add_variable (const DiscreteRandomVariable & a){ // We have to put that first, because of the ONE difference between the size and the actual index of our variables... //cout << "a.get_index()" << a.get_index() << ", size: " << number_of_variable_values.size() << endl; correspondance_table[ a.get_index() ] = number_of_variable_values.size() ; if (number_of_variable_values.empty()) { help_vector.push_back(1); } else { help_vector.push_back( help_vector.back() * number_of_variable_values.back() ); } table_size = 2* table_size; assert (number_of_variable_values.size() < 20); assert (table_size != 0); //assert (table_size < 1000000000 ); //cout << "table_size " << table_size; number_of_variable_values.push_back(a.get_number_values()); variable_values.push_back(a.last_sampled_value); fixed_variable.push_back(false); thetha.resize(number_of_variable_values.size(), 0.0);}QMRDiscretePotential::~QMRDiscretePotential(){}double QMRDiscretePotential::recursive_internal_sum_product_messages_fixed (unsigned int index, const vector <double> & msg_product){ double sum (0.0); //cout << number_of_variable_values.size() << endl; if (index == number_of_variable_values.size() -1) { //cout << "size: " << msg_product.size() << endl; //cout << "size: " << potential_table.size() << endl; if (fixed_variable[index]) { //cout << "Last var. is FIXED" << endl; /* cout << "State of Variables: " ; vector<bool>::iterator jt = fixed_variable.begin(); for (vector<unsigned int>::iterator it = variable_values.begin(); it != variable_values.end(); ++it) { cout << *it; if (*jt) { cout << " (FIXED)"; } cout << ", "; ++jt; } cout << endl; */ unsigned int a = inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0); double b = inner_product(variable_values.begin(), variable_values.end(), thetha.begin(), 0.0); sum += (1 - exp(-thetha_0 -b) ) * msg_product[a]; //cout << "msgs: " << msg_product[a] << ", potential: " << (1 - exp(-thetha_0 -b) ) << ", sum: " << sum << endl; } else { //cout << "Last var. is not FIXED" << endl; variable_values[index] = 0; unsigned int a = inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0); //double b = inner_product(variable_values.begin(), variable_values.end(), thetha.begin(), 0.0); for (unsigned int i = 0 ; i < number_of_variable_values[index] ; ++i) { // Here we must explicitely do the loop. variable_values[index] = i; /* cout << "State of Variables: " ; vector<bool>::iterator jt = fixed_variable.begin(); for (vector<unsigned int>::iterator it = variable_values.begin(); it != variable_values.end(); ++it) { cout << *it; if (*jt) { cout << " (FIXED)"; } cout << ", "; ++jt; } cout << endl; */ //cout << "var in loop: " << i; double b = inner_product(variable_values.begin(), variable_values.end(), thetha.begin(), 0.0); sum += (1 - exp(-thetha_0 -b) ) * msg_product[a]; //cout << "msgs: " << msg_product[a] << ", potential: " << (1 - exp(-thetha_0 -b) ) << ", sum: " << (1 - exp(-thetha_0 -b) ) * msg_product[a] << ", total: " << sum << endl; a += help_vector.back(); } } return sum; } if (fixed_variable[index]) { //cout << "Fixed: " << index << endl; //variable_values[index] = fixed_variable_values[index]; sum += recursive_internal_sum_product_messages_fixed(index+1, msg_product); } else { for (unsigned int i = 0 ; i < number_of_variable_values[index] ; ++i) { variable_values[index] = i; sum += recursive_internal_sum_product_messages_fixed(index+1, msg_product); } } return sum; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -