https://gitlab.inria.fr/line/aide-group/macrovsa
Tip revision: 31a87d848f8ab28a06ccf77d0b359fc966974138 authored by vthierry on 15 December 2025, 21:31:50 UTC
sync from makefile
sync from makefile
Tip revision: 31a87d8
Bundling.cpp
#include "Bundling.hpp"
#include <set>
namespace macrovsa {
Bundling::Bundling(const Bundling& symbol) : Symbol(symbol.getName(), bundling)
{
for(auto it = symbol.values.cbegin(); it != symbol.values.cend(); it++) {
add(*(it->second));
}
}
const Belief& Bundling::getBelief() const
{
static Belief belief;
belief.tau = 0, belief.sigma = sigma0;
for(auto it = values.cbegin(); it != values.cend(); it++) {
const Symbol& symbol = *(it->second);
belief.tau += symbol.getBelief().tau * symbol.getBelief().tau, belief.sigma += symbol.getBelief().sigma;
}
belief.tau = sqrt(belief.tau);
return belief;
}
void Bundling::setBelief(double tau, double sigma)
{
getBelief();
double g_tau = getBelief().tau != 0 ? tau / getBelief().tau : 0, g_sigma = getBelief().sigma > 0 ? sigma / getBelief().sigma : 0;
for(auto it = values.cbegin(); it != values.cend(); it++) {
Symbol& symbol = *(it->second);
const Belief& b = symbol.getBelief();
symbol.setBelief(g_tau * b.tau, g_sigma * b.sigma);
}
}
bool Bundling::equals(const Symbol& symbol, char what) const
{
if(symbol.getType() == bundling) {
const Bundling& s = dynamic_cast < const Bundling& > (symbol);
return equals(values, s.values, what);
}
return false;
}
bool Bundling::equals(const std::unordered_map < unsigned int, Symbol * > &v1, const std::unordered_map < unsigned int, Symbol * > &v2, char what)
{
std::set < unsigned int > ids;
// Registers all IDs
{
for(auto it = v1.cbegin(); it != v1.cend(); it++) {
ids.insert(it->first);
}
for(auto it = v2.cbegin(); it != v2.cend(); it++) {
ids.insert(it->first);
}
}
// Loops on all ids
for(auto it = ids.cbegin(); it != ids.cend(); it++) {
auto jt1 = v1.find(*it), jt2 = v2.find(*it);
if(jt1 == v1.cend() || jt2 == v2.cend()) {
return false;
}
if(!(jt1->second->equals(*(jt2->second), what))) {
return false;
}
}
return true;
}
void Bundling::add(const Symbol& symbol)
{
if(fabs(symbol.getBelief().tau) > 2 * symbol.getBelief().sigma) {
if(symbol.getType() == bundling) {
const Bundling& s = dynamic_cast < const Bundling& > (symbol);
for(auto it = s.values.cbegin(); it != s.values.cend(); it++) {
add(*(it->second));
}
} else {
add(values, clone(symbol, symbols));
}
} else {
sigma0 += symbol.getBelief().sigma;
}
values_changed = true;
}
void Bundling::add(std::unordered_map < unsigned int, Symbol * > &values, Symbol *symbol)
{
auto it = values.find(symbol->getID());
if(it == values.cend()) {
values.insert(std::pair < unsigned int, Symbol * > (symbol->getID(), symbol));
} else {
it->second->setBelief(it->second->getBelief().tau + symbol->getBelief().tau,
it->second->getBelief().sigma + symbol->getBelief().sigma);
if(fabs(it->second->getBelief().tau) < it->second->getBelief().sigma) {
values.erase(it->first);
}
}
}
std::vector < const Symbol * > Bundling::getSorted(unsigned int count) const {
// Sorts the bundling symbols in decreasing tau value order
struct symbol_order { bool operator()(const Symbol *a, const Symbol *b) const {
return a->getBelief().tau > b->getBelief().tau;
}
};
std::set < const Symbol *, symbol_order > symbols;
for(auto it = values.cbegin(); it != values.cend(); it++) {
symbols.insert(it->second);
}
// Uses a static value to return a temporary value
static std::vector < const Symbol * > result;
{
// Selects the count first values
result.clear();
unsigned int c = 0;
for(auto it = symbols.cbegin(); it != symbols.cend() && (count == 0 || c < count); it++, c++) {
result.push_back(*it);
}
}
return result;
}
void Bundling::erase(const Symbol& symbol)
{
values.erase(symbol.getID());
values_changed = true;
}
void Bundling::clear()
{
values.clear();
values_changed = true;
}
void Bundling::setVector(double *vector) const
{
for(unsigned int i = 0; i < dimension; i++) {
vector[i] = 0;
}
for(auto it = values.cbegin(); it != values.cend(); it++) {
const Symbol& symbol = *(it->second);
const double *vector_n = symbol.getVector();
for(unsigned int i = 0; i < dimension; i++) {
vector[i] += vector_n[i];
}
}
}
std::string Bundling::asString() const
{
std::string result = "[";
for(auto it = values.cbegin(); it != values.cend(); it++) {
const Symbol& symbol = *(it->second);
result += " " + symbol.asString();
}
return result + " ]" + asStringTail();
}
double Bundling::getComputationTime()
{
return 0.001 + 0.009 * dimension * values.size();
}
}
