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
algo.cpp
#include "algo.hpp"
#include "Binding.hpp"
#include "Bundling.hpp"
#include "AssociativeMap.hpp"
#include <map>
namespace macrovsa {
const Symbol& algo::reduce(const Symbol& symbol)
{
static Symbol::Symbols symbols;
return reduce(symbol, symbols);
}
const Symbol& algo::reduce(const Symbol& symbol, Symbol::Symbols& symbols)
{
switch(symbol.getType()) {
case Symbol::bundling:
{
const Bundling& s_ = dynamic_cast < const Bundling& > (symbol);
// Expands the reduction through a bundling arguments
Bundling *result = new Bundling();
for(auto it = s_.get().cbegin(); it != s_.get().cend(); it++) {
result->add(reduce(*(it->second), symbols));
}
return reduce(result, symbols);
}
case Symbol::binding:
{
const Binding& s_ = dynamic_cast < const Binding& > (symbol);
if(s_.y().getType() == Symbol::bundling) {
// Expands binding on the y bundling argument
const Bundling& y_ = dynamic_cast < const Bundling& > (s_.y());
Bundling *result = new Bundling();
for(auto it = y_.get().cbegin(); it != y_.get().cend(); it++) {
Binding b_it(*(it->second), s_.x(), s_.b(), s_.normalized());
result->add(reduce(b_it, symbols));
}
return reduce(result, symbols);
} else {
const Symbol& x_ = reduce(s_.x(), symbols);
switch(x_.getType()) {
case Symbol::bundling:
{
// Expands binding on the x bundling argument
const Bundling& x__ = dynamic_cast < const Bundling& > (x_);
Bundling *result = new Bundling();
for(auto it = x__.get().cbegin(); it != x__.get().cend(); it++) {
Binding b_it(s_.y(), *(it->second), s_.b(), s_.normalized());
result->add(reduce(b_it, symbols));
}
return reduce(result, symbols);
}
case Symbol::binding:
{
// Reduces dual binding unbinding operation
const Binding& x__ = dynamic_cast < const Binding& > (x_);
if(s_.y().equals(x__.y()) && s_.b() != x__.b()) {
Symbol& result = *Symbol::clone(reduce(x__.x(), symbols), symbols);
result.setBelief(s_.getBelief().tau * x__.getBelief().tau, s_.getBelief().sigma + x__.getBelief().sigma + algo::sigma_0);
return result;
}
}
default: // Symbol::atomic:
return symbol;
}
}
}
case Symbol::associativemap:
return reduce(dynamic_cast < const AssociativeMap& > (symbol).getBundling());
default:
return symbol;
}
}
const Symbol& algo::reduce(Bundling *bundling, Symbol::Symbols& symbols)
{
// Empty bundling, returns the ``empty´´ symbol
if(bundling->get().size() == 0) {
Symbol *result = new Symbol("", bundling->getBelief().tau, bundling->getBelief().sigma);
symbols.add(result);
return *result;
}
// Bundling singleton, returns the unique symbol
if(bundling->get().size() == 1) {
Symbol *result = Symbol::clone(*(bundling->get().cbegin()->second), symbols);
return *result;
}
symbols.add(bundling);
return *bundling;
}
Belief algo::sim(const Symbol& s1, const Symbol& s2, bool reduce)
{
if(reduce) {
return sim(algo::reduce(s1), algo::reduce(s2), false);
}
if(s1.getType() == Symbol::bundling) {
const Bundling& s1_ = dynamic_cast < const Bundling& > (s1);
Belief result(0, 0);
for(auto it = s1_.get().cbegin(); it != s1_.get().cend(); it++) {
Belief result_it = sim(*(it->second), s2, false);
result.tau += result_it.tau;
result.sigma += result_it.sigma;
}
return result;
} else if(s1.getType() == Symbol::associativemap) {
const AssociativeMap& s1_ = dynamic_cast < const AssociativeMap& > (s1);
Belief result(0, 0);
for(auto it = s1_.get().cbegin(); it != s1_.get().cend(); it++) {
for(auto jt = it->second.second.cbegin(); jt != it->second.second.cend(); jt++) {
Binding binding(*(it->second.first), *(jt->second));
Belief result_it = sim(binding, s2, false);
result.tau += result_it.tau;
result.sigma += result_it.sigma;
}
}
return result;
} else if(s2.getType() == Symbol::bundling) {
return sim(s2, s1, false);
} else if(s2.getType() == Symbol::associativemap) {
return sim(s2, s1, false);
}
Belief result(s1.getID() == s2.getID() ? s1.getBelief().tau *s2.getBelief().tau : 0, fabs (s1.getBelief().tau *s2.getBelief().tau) *sigma_0 + fabs (s2.getBelief().tau) *s1.getBelief().sigma + fabs (s1.getBelief().tau) *s2.getBelief().sigma);
return result;
}
double algo::msim(const Symbol& s1, const Symbol& s2)
{
const double *v1 = s1.getVector(), *v2 = s2.getVector();
double r = 0;
for(unsigned int i = 0; i < Symbol::getDimension(); i++) {
r += v1[i] * v2[i];
}
return r;
}
Belief algo::conj(const Belief& a1, const Belief& a2)
{
Belief belief(fmax (0, fmin(a1.tau, a2.tau)), a1.sigma + a2.sigma);
return belief;
}
Belief algo::conj(const Belief& a1, const Belief& a2, const Belief& a3)
{
Belief belief(fmax (0, fmin(a1.tau, fmin(a2.tau, a3.tau))), a1.sigma + a2.sigma + a3.sigma);
return belief;
}
Belief algo::conj(const Belief& a1, const Belief& a2, const Belief& a3, const Belief& a4)
{
Belief belief(fmax (0, fmin(a1.tau, fmin(a2.tau, fmin(a3.tau, a4.tau)))), a1.sigma + a2.sigma + a3.sigma + a4.sigma);
return belief;
}
Belief algo::conj(const Belief& a1, const Belief& a2, const Belief& a3, const Belief& a4, const Belief& a5)
{
Belief belief(fmax (0, fmin(a1.tau, fmin(a2.tau, fmin(a3.tau, fmin(a4.tau, a5.tau))))), a1.sigma + a2.sigma + a3.sigma + a4.sigma + a5.sigma);
return belief;
}
Belief algo::conj(const Belief& a1, const Belief& a2, const Belief& a3, const Belief& a4, const Belief& a5, const Belief& a6)
{
Belief belief(fmax (0, fmin(a1.tau, fmin(a2.tau, fmin(a3.tau, fmin(a4.tau, fmin(a5.tau, a6.tau)))))), a1.sigma + a2.sigma + a3.sigma + a4.sigma + a5.sigma + a6.sigma);
return belief;
}
Belief algo::conj(const Belief& a1, const Belief& a2, const Belief& a3, const Belief& a4, const Belief& a5, const Belief& a6, const Belief& a7)
{
Belief belief(fmax (0, fmin(a1.tau, fmin(a2.tau, fmin(a3.tau, fmin(a4.tau, fmin(a5.tau, fmin(a6.tau, a7.tau))))))), a1.sigma + a2.sigma + a3.sigma + a4.sigma + a5.sigma + a6.sigma + a7.sigma);
return belief;
}
Belief algo::conj(const Belief& a1, const Belief& a2, const Belief& a3, const Belief& a4, const Belief& a5, const Belief& a6, const Belief& a7, const Belief& a8)
{
Belief belief(fmax (0, fmin(a1.tau, fmin(a2.tau, fmin(a3.tau, fmin(a4.tau, fmin(a5.tau, fmin(a6.tau, fmin(a7.tau, a8.tau)))))))), a1.sigma + a2.sigma + a3.sigma + a4.sigma + a5.sigma + a6.sigma + a7.sigma + a8.sigma);
return belief;
}
double algo::sigma_0 = 0, algo::sigma_04 = 0;
}
