Page MenuHomePhorge

CrossReliability.h
No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None

CrossReliability.h

//===-- rosa/delux/CrossReliability.h ---------------------------*- C++ -*-===//
//
// The RoSA Framework
//
//===----------------------------------------------------------------------===//
///
/// \file rosa/delux/CrossReliability.h
///
/// \author Daniel Schnoell
///
/// \date 2019
///
/// \brief This file is temporarly for the functions/methods CrossConfidence and
/// CrossReliability.
/// \todo As soon as a Delux Reliabl Agent exist modify to this or add it to the
/// Agent.
/// \todo there is 1 exception that needs to be handled correctly.
/// \note the default search function is extremely slow maby this could be done
/// via template for storage class and the functions/methods to efficiently find
/// the correct LinearFunction
//===----------------------------------------------------------------------===//
#ifndef ROSA_AGENT_CROSSRELIABILITY_H
#define ROSA_AGENT_CROSSRELIABILITY_H
#include "rosa/agent/Functionality.h"
#include "rosa/agent/LinearFunctions.hpp"
#include "rosa/core/forward_declarations.h" // needed for id_t
// nedded headers
#include <type_traits> //assert
#include <vector>
// for static methods
#include <algorithm>
#include <numeric>
namespace rosa {
namespace agent {
template <typename Type> class CrossReliability : public Functionality {
using PartialFunction = typename rosa::agent::PartialFunction<Type, Type>;
struct Functionblock {
bool exists = false;
id_t A;
id_t B;
PartialFunction *Funct;
};
Type crossReliabilityParameter = 1;
std::vector<Functionblock> Functions;
Type (*Method)(std::vector<Type> values) = AVERAGE;
//--------------------------------------------------------------------------------
// helper functions
/// evalues the absolute distance between two values
Type AbsuluteValue(Type A, Type B) { return ((A - B) < 0) ? B - A : A - B; }
/// verry inefficient searchFunction
Functionblock (*searchFunction)(std::vector<Functionblock> vect,
const id_t nameA,
const id_t nameB) =
[](std::vector<Functionblock> vect, const id_t nameA,
const id_t nameB) -> Functionblock {
for (Functionblock tmp : vect) {
if (tmp.A == nameA && tmp.B == nameB)
return tmp;
if (tmp.A == nameB && tmp.B == nameA)
return tmp;
}
return Functionblock();
};
/// evaluest the corisponding LinearFunction thith the score difference
/// \param nameA
/// \param nameB these two parameters are the unique identifiers
/// for the LinerFunction \note it doesn't matter if they are swapped
Type getCrossReliabilityFromProfile(id_t nameA, id_t nameB,
Type scoreDifference) {
Functionblock block = searchFunction(Functions, nameA, nameB);
if (!block.exists)
throw "does not exist"; // exceptions are disabled in rosa Todo : handle
// this right
return block.Funct->operator()(scoreDifference);
}
public:
void addCrossReliabilityProfile(id_t idA, id_t idB,
PartialFunction *Function) {
Functions.push_back({true, idA, idB, Function});
}
void setCrossReliabilityParameter(Type val) {
crossReliabilityParameter = val;
}
void setCrossReliabilityMethod(Type (*Meth)(std::vector<Type> values)) {
Method = Meth;
}
~CrossReliability() {
for (auto tmp : Functions)
delete tmp.Funct;
}
/// Calculets the CrossReliability
/// \note both Main and Slaveagents are represented by there data and an
/// unique identifier
///
/// \param MainAgent defines the value pair around which the Cross Reliability
/// is calculated
/// \param SlaveAgents defines all value pairs of the connected Agents
Type operator()(std::pair<id_t, Type> &MainAgent,
std::vector<std::pair<id_t, Type>> &SlaveAgents);
/// predefined combination method
static Type CONJUNCTION(std::vector<Type> values) {
static_assert(std::is_arithmetic<Type>::value); // sanitny check
return *std::min_element(values.begin(), values.end());
}
/// predefined combination method
static Type AVERAGE(std::vector<Type> values) {
static_assert(std::is_arithmetic<Type>::value); // sanitny check
return std::accumulate(values.begin(), values.end(), 0.0) / values.size();
}
/// predefined combination method
static Type DISJUNCTION(std::vector<Type> values) {
static_assert(std::is_arithmetic<Type>::value); // sanitny check
return *std::max_element(values.begin(), values.end());
}
};
template <typename Type>
inline Type CrossReliability<Type>::
operator()(std::pair<id_t, Type> &MainAgent,
std::vector<std::pair<id_t, Type>> &SlaveAgents) {
static_assert(std::is_arithmetic<Type>::value); // sanitny check
Type crossReliabiability;
std::vector<Type> values;
for (std::pair<id_t, Type> SlaveAgent : SlaveAgents) {
if (SlaveAgent.first == MainAgent.first)
continue;
if (MainAgent.second == SlaveAgent.second)
crossReliabiability = 1;
else
crossReliabiability =
1 / (crossReliabilityParameter *
AbsuluteValue(MainAgent.second, SlaveAgent.second));
// profile reliability
Type crossReliabilityFromProfile = getCrossReliabilityFromProfile(
MainAgent.first, SlaveAgent.first,
AbsuluteValue(MainAgent.second, SlaveAgent.second));
values.push_back(
std::max(crossReliabiability, crossReliabilityFromProfile));
}
return Method(values);
}
template <typename Type> class CrossConfidence : public Functionality {
using PartialFunction = typename rosa::agent::PartialFunction<Type, Type>;
struct Functionblock {
bool exists = false;
id_t A;
id_t B;
PartialFunction *Funct;
};
Type crossReliabilityParameter = 1;
std::vector<Functionblock> Functions;
Type (*Method)(std::vector<Type> values) = AVERAGE;
//--------------------------------------------------------------------------------
// helper functions
/// evalues the absolute distance between two values
Type AbsuluteValue(Type A, Type B) { return ((A - B) < 0) ? B - A : A - B; }
/// verry inefficient searchFunction
Functionblock (*searchFunction)(std::vector<Functionblock> vect,
const id_t nameA, const id_t nameB) =
[](std::vector<Functionblock> vect, const id_t nameA,
const id_t nameB) -> Functionblock {
for (Functionblock tmp : vect) {
if (tmp.A == nameA && tmp.B == nameB)
return tmp;
if (tmp.A == nameB && tmp.B == nameA)
return tmp;
}
return Functionblock();
};
/// evaluest the corisponding LinearFunction thith the score difference
/// \param nameA
/// \param nameB these two parameters are the unique identifiers
/// for the LinerFunction \note it doesn't matter if they are swapped
Type getCrossReliabilityFromProfile(id_t nameA, id_t nameB,
Type scoreDifference) {
Functionblock block = searchFunction(Functions, nameA, nameB);
if (!block.exists)
throw "does not exist"; // exceptions are disabled in rosa Todo : handle
// this right
return block.Funct->operator()(scoreDifference);
}
public:
void addCrossReliabilityProfile(id_t idA, id_t idB,
PartialFunction *Function) {
Functions.push_back({true, idA, idB, Function});
}
void setCrossReliabilityParameter(Type val) {
crossReliabilityParameter = val;
}
void setCrossReliabilityMethod(Type (*Meth)(std::vector<Type> values)) {
Method = Meth;
}
~CrossReliabilityModule() {
for (auto tmp : Functions)
delete tmp.Funct;
}
Type operator()(id_t MainAgent, Type TheoreticalValue,
std::vector<std::pair<id_t, Type>> &SlaveAgents);
/// predefined combination method
static Type CONJUNCTION(std::vector<Type> values) {
static_assert(std::is_arithmetic<Type>::value); // sanitny check
return *std::min_element(values.begin(), values.end());
}
/// predefined combination method
static Type AVERAGE(std::vector<Type> values) {
static_assert(std::is_arithmetic<Type>::value); // sanitny check
return std::accumulate(values.begin(), values.end(), 0.0) / values.size();
}
/// predefined combination method
static Type DISJUNCTION(std::vector<Type> values) {
static_assert(std::is_arithmetic<Type>::value); // sanitny check
return *std::max_element(values.begin(), values.end());
}
};
template <typename Type>
inline Type CrossConfidence<Type>::
operator()(id_t MainAgent, Type TheoreticalValue,
std::vector<std::pair<id_t, Type>> &SlaveAgents) {
Type crossReliabiability;
std::vector<Type> values;
for (std::pair<id_t,Type> SlaveAgent : SlaveAgents) {
if (SlaveAgent.first == MainAgent)
continue;
if (TheoreticalValue == SlaveAgent.second)
crossReliabiability = 1;
else
crossReliabiability =
1 / (crossReliabilityParameter *
AbsuluteValue(TheoreticalValue, SlaveAgent.second));
// profile reliability
Type crossReliabilityFromProfile = getCrossReliabilityFromProfile(
MainAgent, SlaveAgent.first,
AbsuluteValue(TheoreticalValue, SlaveAgent.second));
values.push_back(
std::max(crossReliabiability, crossReliabilityFromProfile));
}
return Method(values);
}
} // End namespace agent
} // End namespace rosa
#endif // ROSA_AGENT_CROSSRELIABILITY_H

File Metadata

Mime Type
text/x-c++
Expires
Mon, Nov 10, 6:32 AM (12 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
235580
Default Alt Text
CrossReliability.h (9 KB)

Event Timeline