Page MenuHomePhorge

Reliability.h
No OneTemporary

Size
14 KB
Referenced Files
None
Subscribers
None

Reliability.h

//===-- rosa/agent/Reliability.h --------------------------------*- C++ -*-===//
//
// The RoSA Framework
//
//===----------------------------------------------------------------------===//
///
/// \file rosa/agent/Reliability.h
///
/// \author Daniel Schnoell (danielschnoell@tuwien.ac.at)
///
/// \date 2019
///
/// \brief Definition of *reliability* *functionality*.
///
/// \note All classes throw runtime errors if not all things are set
///
//===----------------------------------------------------------------------===//
#ifndef ROSA_AGENT_RELIABILITY_H
#define ROSA_AGENT_RELIABILITY_H
#include "rosa/agent/FunctionAbstractions.hpp"
#include "rosa/agent/Functionality.h"
#include "rosa/agent/RangeConfidence.hpp"
#include "rosa/agent/CrossReliability.h"
#include <vector>
#include <algorithm>
#define __ostream_output true
namespace rosa {
namespace agent {
/// This is the Reliability Functionality for a low level Agent
/// \tparam SensorValueType Datatype of the Sensor value ( Typically double or float)
/// \tparam StateType Datatype of the State ( Typically long or int)
/// \tparam ReliabilityType Datatype of the Reliability ( Typically double or float)
///
/// use the () operator to get the reliability and feed the information from the master back to this
/// \note all pointer for the functionalities will be deleted when this is object ist destroyed
template <typename SensorValueType, typename StateType, typename ReliabilityType>
class LowLevel {
public:
struct ConfOrRel {
StateType score;
ReliabilityType Reliability;
ConfOrRel(StateType _score,ReliabilityType _Reliability):score(_score),Reliability(_Reliability){};
ConfOrRel() {};
#if __ostream_output
friend std::ostream & operator << (std::ostream &out, const ConfOrRel &c)
{
out << "Score: " << c.score << "\t Reliability: " << c.Reliability << " " ;
return out;
}
#endif
};
/// Calculates the Conf/ Reliability
/// \param SensorValue The current Values of the Sensor
///
/// \return Reliability of the current Value
ConfOrRel operator()(SensorValueType SensorValue) {
std::map<StateType, ReliabilityType> ActuallPosibleScores_tmp = Confidence->operator()(SensorValue);
std::vector<ConfOrRel> ActuallPossibleScores;
for (StateType state : States)
{
if(ActuallPosibleScores_tmp.find(state)!=ActuallPosibleScores_tmp.end())
{
ConfOrRel val;
val.score=state;
val.Reliability=ActuallPosibleScores_tmp.find(state)->second;
ActuallPossibleScores.push_back(val);
}
}
ReliabilityType inputReliability;
if (PreviousSensorValueExists)
inputReliability = getRelibility(SensorValue, previousSensorValue, valueSetCounter);
else
inputReliability = Reliability->operator()(SensorValue);
std::cout << "input Rel: " << inputReliability << "\n";
for (std::size_t at = 0; at < ActuallPossibleScores.size(); at++)
ActuallPossibleScores.at(at).Reliability = std::min(ActuallPossibleScores.at(at).Reliability, inputReliability);
for (std::size_t APS_at = 0; APS_at < ActuallPossibleScores.size(); APS_at++)
for (std::size_t VFM_at = 0; VFM_at < ValuesFromMaster.size(); VFM_at++) {
if (ActuallPossibleScores.at(APS_at).score == ValuesFromMaster.at(VFM_at).score) {
ActuallPossibleScores.at(APS_at).Reliability = ActuallPossibleScores.at(APS_at).Reliability + ValuesFromMaster.at(VFM_at).Reliability;
}
}
saveInHistory(ActuallPossibleScores);
std::vector<ConfOrRel> possibleScores;
possibleScores=getAllPossibleScoresBasedOnHistory(possibleScores);
std::sort(possibleScores.begin(), possibleScores.end(), [](ConfOrRel A, ConfOrRel B)-> bool {return A.Reliability > B.Reliability; });
previousSensorValue = SensorValue;
PreviousSensorValueExists = true;
for(auto val:ActuallPossibleScores)
std::cout <<"inside APS:" << val << "\n";
for(auto val:possibleScores)
std::cout <<"inside PS:" << val << "\n";
return possibleScores.at(0);
}
/// Needed feedback from the Master
/// \param ValuesFromMaster The Scores + Reliability from the Master for this Agent
void feedback(std::vector<ConfOrRel> ValuesFromMaster)
{
this->ValuesFromMaster = ValuesFromMaster;
}
/// This is the setter for Confidence Function
/// \param Confidence A pointer to the Functional for the Confidence
void setConfidenceFunction(RangeConfidence<ReliabilityType, StateType, SensorValueType>* Confidence)
{
this->Confidence = Confidence;
}
/// This is the setter for Reliability Function
/// \param Reliability A pointer to the Functional for the Reliability
void setReliabilityFunction(Abstraction<SensorValueType, ReliabilityType>* Reliability)
{
this->Reliability = Reliability;
}
/// This is the setter for ReliabilitySlope Function
/// \param ReliabilitySlope A pointer to the Functional for the ReliabilitySlope
void setReliabilitySlopeFunction(Abstraction<SensorValueType, ReliabilityType>* ReliabilitySlope)
{
this->ReliabilitySlope = ReliabilitySlope;
}
/// This is the setter for TimeConfidence Function
/// \param TimeConfidence A pointer to the Functional for the TimeConfidence
void setTimeConfidenceFunction(Abstraction<std::size_t, ReliabilityType>* TimeConfidence)
{
this->TimeConfidence = TimeConfidence;
}
/// This is the setter for all possible States
/// \param states A vertor for all states
void setStates(std::vector<StateType> states)
{
this->States=states;
}
/// This sets the Maximum length of the Histpry
/// \param length The length
void setHistoryLength(std::size_t length)
{
this->HistoryMaxSize=length;
}
/// This sets the Value set Counter
/// \param ValueSetCounter the new Value
void setValueSetCounter(unsigned int ValueSetCounter)
{
this->valueSetCounter=ValueSetCounter;
}
/// deletes all given pointers
~LowLevel()
{
delete Confidence;
Confidence = nullptr;
delete Reliability;
Reliability = nullptr;
delete ReliabilitySlope;
ReliabilitySlope = nullptr;
delete TimeConfidence;
TimeConfidence = nullptr;
}
private:
std::vector<std::vector<ConfOrRel>> History;
std::size_t HistoryMaxSize;
std::vector<ConfOrRel> ValuesFromMaster;
SensorValueType previousSensorValue;
unsigned int valueSetCounter;
std::vector<StateType> States;
bool PreviousSensorValueExists = false;
RangeConfidence<ReliabilityType, StateType, SensorValueType>* Confidence = nullptr;
Abstraction<SensorValueType, ReliabilityType>* Reliability = nullptr;
Abstraction<SensorValueType, ReliabilityType>* ReliabilitySlope = nullptr;
Abstraction<std::size_t, ReliabilityType>* TimeConfidence = nullptr;
/*--------------------------------- needed Funktions -----------------------------------------------------*/
/// returns the Reliability
/// \param actualValue The Value of the Sensor
/// \param lastValue of the Sensor this is stored in the class
/// \param valueSetCounter Todo
ReliabilityType getRelibility(SensorValueType actualValue, SensorValueType lastValue, unsigned int valueSetCounter) {
ReliabilityType relAbs = Reliability->operator()(actualValue);
if (PreviousSensorValueExists)
{
ReliabilityType relSlo = ReliabilitySlope->operator()((lastValue - actualValue) / (SensorValueType)valueSetCounter);
// calculate signal input reliability
// NOTE: options would be multiply, average, AND (best to worst:
// average = AND > multiply) rel = relAbs * relSlo; rel = (relAbs +
// relSlo)/2;
return std::min(relAbs, relSlo);
}
else
return relAbs;
}
/// adabts the possible Scores by checking the History and combines those values currently with max
/// \param possibleScores This is returned from the Master
std::vector<ConfOrRel> getAllPossibleScoresBasedOnHistory(std::vector<ConfOrRel> possibleScores) {
//iterate through all history entries
std::size_t posInHistory = 0;
for (auto pShE = History.begin(); pShE < History.end(); pShE++, posInHistory++) {
//iterate through all possible scores of each history entry
for (ConfOrRel& pSh : *pShE) {
//printf("a3\n");
StateType historyScore = pSh.score;
ReliabilityType historyConf = pSh.Reliability;
//combine each history score with the confidence of time
//NOTE: multiplication, AND, or average would be alternatives (best to worst: multiplication = AND = average)
historyConf = historyConf * TimeConfidence->operator()(posInHistory);
//historyConf = (historyConf + TimeConfidence(posInHistory)) / 2;
//historyConf = std::min(historyConf, TimeConfidence(posInHistory));
//printf("a4\n");
bool foundScore = false;
for (ConfOrRel& pS : possibleScores) {
if (pS.score == historyScore) {
//calculate confidence for score
//NOTE: multiplication, AND, or average would be alternatives (best to worst: AND >> average = multiplication )
//pS->confOrRel = pS->confOrRel * historyConf;
//pS->confOrRel = (pS->confOrRel + historyConf) / 2;
pS.Reliability = std::max(pS.Reliability, historyConf);
foundScore = true;
}
}
if (foundScore == false) {
ConfOrRel possibleScore;
possibleScore.score = historyScore;
possibleScore.Reliability = historyConf;
possibleScores.push_back(possibleScore);
}
}
}
return possibleScores;
}
/// saves the Scores in the History
/// \param actualPossibleScores The Scores which should be saved
void saveInHistory(std::vector<ConfOrRel> actualPossibleScores) {
//check if the reliability of at least one possible score is high enough
bool atLeastOneRelIsHigh = false;
for (ConfOrRel pS : actualPossibleScores) {
if (pS.Reliability > 0.5) {
atLeastOneRelIsHigh = true;
}
}
//save possible scores if at least one possible score is high enough (or if the history is empty)
if (History.size() < 1 || atLeastOneRelIsHigh == true) {
History.insert(History.begin(),actualPossibleScores);
//if history size is higher than allowed, savo oldest element
while (History.size() > HistoryMaxSize) {
//delete possibleScoreHistory.back();
History.pop_back();
}
}
}
};
/// This is the Reliability Functionality for the Highlevel Agent
/// \tparam StateType Datatype of the State ( Typically double or float)
/// \tparam ReliabilityType Datatype of the Reliability ( Typically long or int)
///
/// use the () operator to calculate the Reliability and all cross confidences for all slaves
/// \note all pouinter to Funcionalities get deleted upon termitation of the object
template<typename StateType, typename ReliabilityType>
class HighLevel
{
public:
struct ConfOrRel {
StateType score;
ReliabilityType Reliability;
ConfOrRel(StateType _score,ReliabilityType _Reliability):score(_score),Reliability(_Reliability){};
ConfOrRel() {};
#if __ostream_output
friend std::ostream & operator << (std::ostream &out, const ConfOrRel &c)
{
out << "Score: " << c.score << "\t Reliability: " << c.Reliability << " " ;
return out;
}
#endif
};
struct returnType {
ReliabilityType CrossReliability;
std::vector<std::pair<id_t, std::vector<ConfOrRel>>> CrossConfidence;
};
StateType MaximumState;
returnType operator()(std::vector<std::tuple<id_t, StateType, ReliabilityType>> &Values)
{
StateType EWS = 0;
ReliabilityType combinedInputRel = 1;
ReliabilityType combinedCrossRel = 1;
ReliabilityType outputReliability;
std::vector<std::pair<id_t, StateType>> Agents;
std::vector<std::pair<id_t, std::vector<ConfOrRel>>> output;
std::vector<ConfOrRel> output_temporary;
for (auto tmp : Values)
{
std::pair<id_t, StateType> tmp2;
tmp2.first = std::get<0>(tmp);
tmp2.second = std::get<1>(tmp);
Agents.push_back(tmp2);
}
for (auto Value : Values) {
id_t id = std::get<0>(Value);
StateType sc = std::get<1>(Value);
ReliabilityType rel = std::get<2>(Value);
EWS = EWS + sc;
combinedInputRel = std::min(combinedInputRel, rel);
//calculate the cross reliability for this slave agent
ReliabilityType realCrossReliabilityOfSlaveAgent = CrossReliability->operator()({id,sc }, Agents); //AVERAGE, MULTIPLICATION, CONJUNCTION (best to worst: AVERAGE = CONJUNCTION > MULTIPLICATION >> )
output_temporary.clear();
for (StateType theoreticalScore = 0; theoreticalScore <= MaximumState; theoreticalScore++) {
//calculate the cross reliability for this slave agent
ConfOrRel data;
data.score = theoreticalScore;
data.Reliability = CrossConfidence->operator()(id, theoreticalScore, Agents);
output_temporary.push_back(data);
}
output.push_back({ std::get<0>(Value),output_temporary });
combinedCrossRel = std::min(combinedCrossRel, realCrossReliabilityOfSlaveAgent);
}
//combine cross reliabilites and input reliabilites of all slave agents
//NOTE: options would be multiply, average, AND (best to worst: )
//outputReliability = combinedInputRel * combinedCrossRel;
//outputReliability = (combinedInputRel + combinedCrossRel) / 2;
outputReliability = std::min(combinedInputRel, combinedCrossRel);
return { outputReliability,output };
}
/// This is the setter for CrossReliability Function
/// param A pointer to the Functional for the CrossReliability
void setFunction(CrossReliability<StateType, ReliabilityType>* CrossReliability)
{
this->CrossReliability = CrossReliability;
}
/// This is the setter for CrossConfidence Function
/// param A pointer to the Functional for the CrossConfidence
void setFunction(CrossConfidence <StateType, ReliabilityType>* CrossConfidence)
{
this->CrossConfidence = CrossConfidence;
}
/// deletes all given pointers
~HighLevel()
{
delete CrossReliability;
CrossConfidence = nullptr;
delete CrossConfidence;
CrossConfidence = nullptr;
}
private:
CrossReliability<StateType, ReliabilityType>* CrossReliability = nullptr;
CrossConfidence <StateType, ReliabilityType>* CrossConfidence = nullptr;
};
} // namespace agent
}// namespace rosa
#endif // !ROSA_AGENT_RELIABILITY_H

File Metadata

Mime Type
text/x-c++
Expires
Wed, Jul 2, 1:06 PM (22 h, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
157141
Default Alt Text
Reliability.h (14 KB)

Event Timeline