diff --git a/include/rosa/agent/ReliabilityConfidenceCombinator.h b/include/rosa/agent/ReliabilityConfidenceCombinator.h index e3315bb..08bc82b 100644 --- a/include/rosa/agent/ReliabilityConfidenceCombinator.h +++ b/include/rosa/agent/ReliabilityConfidenceCombinator.h @@ -1,745 +1,741 @@ //===-- rosa/agent/ReliabilityConfidenceCombinator.h ------------*- C++ -*-===// // // The RoSA Framework // //===----------------------------------------------------------------------===// /// /// \file rosa/agent/ReliabilityConfidenceCombinator.h /// /// \author Daniel Schnoell (daniel.schnoell@tuwien.ac.at) /// /// \date 2019 /// /// \brief Definition of *ReliabilityConfidenceCombinator* *functionality*. /// /// \note based on Maximilian Goetzinger (maxgot@utu.fi) code in /// CAM_Dirty_include SA-EWS2_Version... inside Agent.cpp /// /// \note By defining and setting Reliability_trace_level it is possible to /// change the level to which it should be traced. \note All classes throw /// runtime errors if not all things are set /// /// \note should the Reliability be capped? /// /// //===----------------------------------------------------------------------===// #ifndef ROSA_AGENT_ReliabilityConfidenceCombinator_H #define ROSA_AGENT_ReliabilityConfidenceCombinator_H #include "rosa/core/forward_declarations.h" // needed for id_t #include "rosa/support/log.h" #include "rosa/agent/FunctionAbstractions.hpp" #include "rosa/agent/Functionality.h" #include "rosa/agent/RangeConfidence.hpp" #include #include #include #include /// 0 everything /// 1 vectors /// 2 outputs #define trace_everything 0 #define trace_vectors 1 #define trace_outputs 2 #ifndef Reliability_trace_level #define Reliability_trace_level 0 #endif #define trace_end "\n\n\n" namespace rosa { namespace agent { /// This is a struct with a few methods that make Reliability Combinator /// more readable \tparam IdentifierType The datatype of the States \tparam /// ReliabilityType The datatype of the Reliability template struct ConfOrRel { /// making both Template Arguments readable to make a few things easier using _IdentifierType = IdentifierType; /// making both Template Arguments readable to make a few things easier using _ReliabilityType = ReliabilityType; /// The actual place where the data is stored IdentifierType Identifier; /// The actual place where the data is stored ReliabilityType Reliability; ConfOrRel(IdentifierType _Identifier, ReliabilityType _Reliability) : Identifier(_Identifier), Reliability(_Reliability){}; ConfOrRel(){}; /// Pushes the Data in a Human readable form /// \param out The stream where it is written to /// \param c The struct itself friend std::ostream &operator<<(std::ostream &out, const ConfOrRel &c) { out << "Identifier: " << c.Identifier << "\t Reliability: " << c.Reliability << " "; return out; } /// needed or it throws an clang diagnosic error using map = std::map; // needed or it throws an // clang diagnosic error /// Filles the vector with the data inside the map /// \param me The vector to be filled /// \param data The data wich is to be pushed into the vector friend std::vector &operator<<(std::vector &me, map &&data) { for (auto tmp : data) { me.push_back(ConfOrRel(tmp.first, tmp.second)); #if Reliability_trace_level <= trace_everything LOG_TRACE_STREAM << "\n" << ConfOrRel(tmp.first, tmp.second) << trace_end; #endif } return me; } /// This is to push the data inside a vector in a human readable way into the /// ostream \param out The ostream \param c The vector which is read friend std::ostream &operator<<(std::ostream &out, const std::vector &c) { std::size_t index = 0; for (ConfOrRel data : c) { out << index << " : " << data << "\n"; index++; } return out; } }; /// This is the combinator for Reliability and confidences it takes the /// Sensor value, its "History" and feedback from \c /// CrossCombinator to calculate different Reliabilities. /// \tparam SensorValueType Datatype of the Sensor value ( Typically /// double or float) \tparam IdentifierType Datatype of the State ( Typically /// long or int) /// \tparam ReliabilityType Datatype of the Reliability ( /// Typically double or float) /// /// \note more information about how it calculates /// the Reliabilities it should be considered feedback is a sort of Confidence /// \verbatim ///---------------------------------------------------------------------------------- /// /// /// ->Reliability---> getInputReliability() /// | | /// | V /// Sensor Value ---| PossibleIdentifierCombinationMethod -> next line /// | A | /// | | V /// ->Confidence--- getPossibleIdentifiers() /// ///----------------------------------------------------------------------------------- /// /// feedback /// | /// V /// ValuesFromMaster /// | -> History ---| /// V | V /// here -> FeedbackCombinatorMethod --------> HistoryCombinatorMethod->nextline /// | | /// V V /// getpossibleIdentifiersWithMasterFeedback()getPossibleIdentifiersWithHistory() /// ///---------------------------------------------------------------------------------- /// /// here -> sort -> most likely -> getmostLikelyIdentifierAndReliability() /// ///--------------------------------------------------------------------------------- /// \endverbatim /// the mentioned methods are early outs so if two ore more of them are run in /// the same step they will be interpreted as different time steps ///
 /// Default values for Combinators:
 ///	InputReliabilityCombinator		= combinationMin;
 ///	PossibleIdentifierCombinationMethod=PossibleIdentifierCombinationMethodMin;
 /// FeedbackCombinatorMethod		= FeedbackCombinatorMethodAverage;
 /// HistoryCombinatorMethod			= HistoryCombinatorMethodMax;
 ///	
/// To understand the place where the combinator methods come into play a list /// for each getter which Methods are used. /// ///
 /// \c getInputReliability():
 ///		-InputReliabilityCombinator
 /// \c getPossibleIdentifiers():
 ///		-InputReliabilityCombinator
 ///		-PossibleIdentifierCombinationMethod
 /// \c getpossibleIdentifiersWithMasterFeedback():
 ///		-InputReliabilityCombinator
 ///		-PossibleIdentifierCombinationMethod
 ///		-FeedbackCombinatorMethod
 /// \c getPossibleIdentifiersWithHistory():
 ///		-InputReliabilityCombinator
 ///		-PossibleIdentifierCombinationMethod
 ///		-FeedbackCombinatorMethod
 ///		-HistoryCombinatorMethod
 /// \c getmostLikelyIdentifierAndReliability():
 ///		-InputReliabilityCombinator
 ///		-PossibleIdentifierCombinationMethod
 ///		-FeedbackCombinatorMethod
 ///		-HistoryCombinatorMethod
 /// 
-/// -/// -/// -/// template class ReliabilityAndConfidenceCombinator { public: static_assert(std::is_arithmetic::value, "LowLevel: SensorValueType has to an arithmetic type\n"); static_assert(std::is_arithmetic::value, "LowLevel: IdentifierType has to an arithmetic type\n"); static_assert(std::is_arithmetic::value, "LowLevel: ReliabilityType has to an arithmetic type\n"); /// Typedef to shorten the writing. /// \c ConfOrRel using ConfOrRel = ConfOrRel; /// Calculates the input reliability by combining Reliability of the Sensor /// and the Slope Reliability \param SensorValue The sensor Value \note to set /// the combination method \c setInputReliabilityCombinator() auto getInputReliability(SensorValueType SensorValue) { auto inputReliability = getReliability(SensorValue, previousSensorValue, valueSetCounter); previousSensorValue = SensorValue; PreviousSensorValueExists = true; return inputReliability; } /// Calculates the possible Identifiers /// \param SensorValue the Sensor Value /// \brief it combines the input reliability and the confidence of the Sensor. /// The use combination method can be set using \c /// setPossibleIdentifierCombinationMethod() auto getPossibleIdentifiers(SensorValueType SensorValue) { std::vector possibleIdentifiers; ReliabilityType inputReliability = getInputReliability(SensorValue); #if Reliability_trace_level <= trace_vectors LOG_TRACE_STREAM << "\ninput Rel: " << inputReliability << trace_end; #endif possibleIdentifiers << Confidence->operator()(SensorValue); possibleIdentifiers = PossibleIdentifierCombinationMethod( possibleIdentifiers, inputReliability); return possibleIdentifiers; } /// return the Possible Values with the feedback in mind /// \param SensorValue The sensor Value /// \brief it combines the input reliability and the confidence of the Sensor. /// The combines them with FeedbackCombinatorMethod and returns the result. auto getpossibleIdentifiersWithMasterFeedback(SensorValueType SensorValue) { std::vector possibleIdentifiers; ReliabilityType inputReliability = getInputReliability(SensorValue); #if Reliability_trace_level <= trace_vectors LOG_TRACE_STREAM << "\ninput Rel: " << inputReliability << trace_end; #endif possibleIdentifiers << Confidence->operator()(SensorValue); possibleIdentifiers = PossibleIdentifierCombinationMethod( possibleIdentifiers, inputReliability); possibleIdentifiers = FeedbackCombinatorMethod(possibleIdentifiers, ValuesFromMaster); return possibleIdentifiers; } /// returns all possible Identifiers and Reliabilities with the History in /// mind \param SensorValue the Sensor value how this is done is described at /// the class. auto getPossibleIdentifiersWithHistory(SensorValueType SensorValue) { std::vector ActuallPossibleIdentifiers; std::vector possibleIdentifiers; ReliabilityType inputReliability = getInputReliability(SensorValue); #if Reliability_trace_level <= trace_vectors LOG_TRACE_STREAM << "\ninput Rel: " << inputReliability << trace_end; #endif possibleIdentifiers << Confidence->operator()(SensorValue); possibleIdentifiers = PossibleIdentifierCombinationMethod( possibleIdentifiers, inputReliability); possibleIdentifiers = FeedbackCombinatorMethod(possibleIdentifiers, ValuesFromMaster); saveInHistory(possibleIdentifiers); #if Reliability_trace_level <= trace_vectors LOG_TRACE_STREAM << "\nActuallPossibleIdentifiers:\n" << possibleIdentifiers << trace_end; LOG_TRACE_STREAM << "\npossibleIdentifiers:\n" << possibleIdentifiers << trace_end; #endif possibleIdentifiers.clear(); return getAllPossibleIdentifiersBasedOnHistory(); } /// Calculates the Reliability /// \param SensorValue The current Values of the Sensor /// /// \return Reliability and Identifier of the current SensorValue /// ConfOrRel getmostLikelyIdentifierAndReliability(SensorValueType SensorValue) { #if Reliability_trace_level <= trace_outputs LOG_TRACE_STREAM << "\nTrace level is set to: " << Reliability_trace_level << "\n" << "Will trace: " << ((Reliability_trace_level == trace_outputs) ? "outputs" : (Reliability_trace_level == trace_vectors) ? "vectors" : (Reliability_trace_level == trace_everything) ? "everything" : "undefined") << trace_end; #endif std::vector ActuallPossibleIdentifiers; std::vector possibleIdentifiers; ReliabilityType inputReliability = getInputReliability(SensorValue); #if Reliability_trace_level <= trace_vectors LOG_TRACE_STREAM << "\ninput Rel: " << inputReliability << trace_end; #endif possibleIdentifiers << Confidence->operator()(SensorValue); possibleIdentifiers = PossibleIdentifierCombinationMethod( possibleIdentifiers, inputReliability); possibleIdentifiers = FeedbackCombinatorMethod(possibleIdentifiers, ValuesFromMaster); saveInHistory(possibleIdentifiers); #if Reliability_trace_level <= trace_vectors LOG_TRACE_STREAM << "\nActuallPossibleIdentifiers:\n" << possibleIdentifiers << trace_end; LOG_TRACE_STREAM << "\npossibleIdentifiers:\n" << possibleIdentifiers << trace_end; #endif possibleIdentifiers.clear(); possibleIdentifiers = getAllPossibleIdentifiersBasedOnHistory(); std::sort(possibleIdentifiers.begin(), possibleIdentifiers.end(), [](ConfOrRel A, ConfOrRel B) -> bool { return A.Reliability > B.Reliability; }); #if Reliability_trace_level <= trace_outputs LOG_TRACE_STREAM << "\noutput lowlevel: " << possibleIdentifiers.at(0) << trace_end; #endif return possibleIdentifiers.at(0); } /// feedback for this functionality most commonly it comes from a Master Agent /// \param ValuesFromMaster The Identifiers + Reliability for the feedback /// \brief This input kind of resembles a confidence but not /// directly it more or less says: compared to the other Identifiers inside /// the System these are the Identifiers with the Reliability that you have. void feedback(std::vector ValuesFromMaster) { this->ValuesFromMaster = ValuesFromMaster; } // // ----------------------Reliability and Confidence Function setters---------- // /// This is the setter for Confidence Function /// \param Confidence A pointer to the Functional for the \c Confidence of the /// Sensor value void setConfidenceFunction( std::unique_ptr> &Confidence) { this->Confidence = std::move(Confidence); } /// This is the setter for Reliability Function /// \param Reliability A pointer to the Functional for the Reliability /// \brief The Reliability takes the current Sensor value and return the /// Reliability of the value. void setReliabilityFunction( std::unique_ptr> &Reliability) { this->Reliability = std::move(Reliability); } /// This is the setter for ReliabilitySlope Function /// \param ReliabilitySlope A pointer to the Functional for the /// ReliabilitySlope /// \brief The ReliabilitySlope takes the difference of the current Sensor /// Value to the last one and tells you how likely the change is. void setReliabilitySlopeFunction( std::unique_ptr> &ReliabilitySlope) { this->ReliabilitySlope = std::move(ReliabilitySlope); } /// This is the setter for TimeConfidence Function /// \param TimeConfidence A pointer to the Functional for the TimeConfidence /// \brief The time function takes the position in the History with greater /// equals older and return a Reliability of how "relevant" it is. void setTimeConfidenceFunction( std::unique_ptr> &TimeConfidence) { this->TimeConfidence = std::move(TimeConfidence); } /// This is the setter for all possible States /// \param states A vector containing all states /// \brief This exists even though \c State Type is an arithmetic Type because /// the states do not need to be "next" to each other ( ex. states={ 1 7 24 }) void setStates(std::vector states) { this->States = states; } /// This sets the Maximum length of the History /// \param length The length void setHistoryLength(std::size_t length) { this->HistoryMaxSize = length; } /// This sets the Value set Counter /// \param ValueSetCounter the new Value /// \note This might actually be only an artifact. It is only used to get the /// reliability from the \c ReliabilitySlope [ ReliabilitySlope->operator()( /// (lastValue - actualValue) / (SensorValueType)valueSetCounter) ] void setValueSetCounter(unsigned int ValueSetCounter) { this->valueSetCounter = ValueSetCounter; } // // ----------------combinator setters----------------------------------------- // /// This sets the combination method used by the History /// \param Meth the method which should be used. predefined \c /// HistoryCombinatorMethodMin() \c HistoryCombinatorMethodMax() \c /// HistoryCombinatorMethodMult() \c HistoryCombinatorMethodAverage() void setHistoryCombinatorMethod( std::function Meth) { HistoryCombinatorMethod = Meth; } /// sets the predefined method for the combination of the possible Identifiers /// and the master \param Meth the method predefined ones are \c /// FeedbackCombinatorMethodAverage() \c FeedbackCombinatorMethodMin() \c /// FeedbackCombinatorMethodMax() \c FeedbackCombinatorMethodMult() void setFeedbackCombinatorMethod( std::function(std::vector, std::vector)> Meth) { FeedbackCombinatorMethod = Meth; } /// Sets the used combination method for Possible Identifiers /// \param Meth a Pointer for the used Method. Predefined methods \c /// PossibleIdentifierCombinationMethodMin() \c /// PossibleIdentifierCombinationMethodMax() \c /// PossibleIdentifierCombinationMethodAverage() void setPossibleIdentifierCombinationMethod( std::function(std::vector, ReliabilityType)> Meth) { PossibleIdentifierCombinationMethod = Meth; } /// sets the input reliability combinator method /// \param method the to be used method /// \note there are predefined methods \c combinationMin() \c combinationMax() /// \c combinationAverage() void setInputReliabilityCombinator( std::function method) { InputReliabilityCombinator = method; } // // ----------------predefined combinators------------------------------------ // struct predefinedMethods { /// predefined Method static ReliabilityType HistoryCombinatorMethodMin(ReliabilityType A, ReliabilityType B) { return std::min(A, B); } /// predefined Method static ReliabilityType HistoryCombinatorMethodMax(ReliabilityType A, ReliabilityType B) { return std::max(A, B); } /// predefined Method static ReliabilityType HistoryCombinatorMethodMult(ReliabilityType A, ReliabilityType B) { return A * B; } /// predefined Method static ReliabilityType HistoryCombinatorMethodAverage(ReliabilityType A, ReliabilityType B) { return (A + B) / 2; } /// predefined method static std::vector FeedbackCombinatorMethodAverage(std::vector A, std::vector B) { for (auto &tmp_me : A) for (auto &tmp_other : B) { if (tmp_me.Identifier == tmp_other.Identifier) { tmp_me.Reliability = (tmp_me.Reliability + tmp_other.Reliability) / 2; } } return A; } /// predefined method static std::vector FeedbackCombinatorMethodMin(std::vector A, std::vector B) { for (auto &tmp_me : A) for (auto &tmp_other : B) { if (tmp_me.Identifier == tmp_other.Identifier) { tmp_me.Reliability = std::min(tmp_me.Reliability + tmp_other.Reliability); } } return A; } /// predefined method static std::vector FeedbackCombinatorMethodMax(std::vector A, std::vector B) { for (auto &tmp_me : A) for (auto &tmp_other : B) { if (tmp_me.Identifier == tmp_other.Identifier) { tmp_me.Reliability = std::max(tmp_me.Reliability + tmp_other.Reliability); } } return A; } /// predefined method static std::vector FeedbackCombinatorMethodMult(std::vector A, std::vector B) { for (auto &tmp_me : A) for (auto &tmp_other : B) { if (tmp_me.Identifier == tmp_other.Identifier) { tmp_me.Reliability = tmp_me.Reliability * tmp_other.Reliability; } } return A; } /// Predefined combination method for possible Identifiers static std::vector PossibleIdentifierCombinationMethodMin(std::vector A, ReliabilityType B) { for (auto tmp : A) tmp.Reliability = std::min(tmp.Reliability, B); return A; } /// Predefined combination method for possible Identifiers static std::vector PossibleIdentifierCombinationMethodMax(std::vector A, ReliabilityType B) { for (auto tmp : A) tmp.Reliability = std::max(tmp.Reliability, B); return A; } /// Predefined combination method for possible Identifiers static std::vector PossibleIdentifierCombinationMethodAverage(std::vector A, ReliabilityType B) { for (auto tmp : A) tmp.Reliability = (tmp.Reliability + B) / 2; return A; } /// Predefined combination method for possible Identifiers static std::vector PossibleIdentifierCombinationMethodMult(std::vector A, ReliabilityType B) { for (auto tmp : A) tmp.Reliability = tmp.Reliability * B / 2; return A; } /// The predefined min combinator method static ReliabilityType combinationMin(ReliabilityType A, ReliabilityType B) { return std::min(A, B); } /// The predefined max combinator method static ReliabilityType combinationMax(ReliabilityType A, ReliabilityType B) { return std::max(A, B); } /// The predefined average combinator method static ReliabilityType combinationAverage(ReliabilityType A, ReliabilityType B) { return (A + B) / 2; } /// The predefined average combinator method static ReliabilityType combinationMult(ReliabilityType A, ReliabilityType B) { return A * B; } }; // ---------------------------------------------------------------- // Stored Values // ---------------------------------------------------------------- private: std::vector> History; std::size_t HistoryMaxSize; std::vector ValuesFromMaster; SensorValueType previousSensorValue; unsigned int valueSetCounter; std::vector States; bool PreviousSensorValueExists = false; std::unique_ptr< RangeConfidence> Confidence; std::unique_ptr> Reliability; std::unique_ptr> ReliabilitySlope; std::unique_ptr> TimeConfidence; // combination functions std::function InputReliabilityCombinator = predefinedMethods::combinationMin; std::function(std::vector, ReliabilityType)> PossibleIdentifierCombinationMethod = predefinedMethods::PossibleIdentifierCombinationMethodMin; std::function(std::vector, std::vector)> FeedbackCombinatorMethod = predefinedMethods::FeedbackCombinatorMethodAverage; std::function HistoryCombinatorMethod = predefinedMethods::HistoryCombinatorMethodMax; // --------------------------------------------------------------------------- // needed Functions // --------------------------------------------------------------------------- /// returns the Reliability /// \param actualValue The Value of the Sensor /// \param lastValue of the Sensor this is stored in the class /// \param valueSetCounter It has an effect on the difference of the current /// and last value This might not be needed anymore /// \brief it returns the combination the \c Reliability function and \c /// ReliabilitySlope if the previous value exists. if it doesn't it only /// returns the \c Reliability function value. ReliabilityType getReliability(SensorValueType actualValue, SensorValueType lastValue, unsigned int valueSetCounter) { ReliabilityType relAbs = Reliability->operator()(actualValue); if (PreviousSensorValueExists) { ReliabilityType relSlo = ReliabilitySlope->operator()( (lastValue - actualValue) / (SensorValueType)valueSetCounter); return InputReliabilityCombinator(relAbs, relSlo); } else return relAbs; } /// adapts the possible Identifiers by checking the History and combines those /// values. currently with max /// \brief combines the historic values with the \c TimeConfidence function /// and returns the maximum Reliability for all Identifiers. std::vector getAllPossibleIdentifiersBasedOnHistory() { // iterate through all history entries std::size_t posInHistory = 0; std::vector possibleIdentifiers; for (auto pShE = History.begin(); pShE < History.end(); pShE++, posInHistory++) { // iterate through all possible Identifiers of each history entry for (ConfOrRel &pSh : *pShE) { IdentifierType historyIdentifier = pSh.Identifier; ReliabilityType historyConf = pSh.Reliability; historyConf = historyConf * TimeConfidence->operator()(posInHistory); bool foundIdentifier = false; for (ConfOrRel &pS : possibleIdentifiers) { if (pS.Identifier == historyIdentifier) { pS.Reliability = HistoryCombinatorMethod(pS.Reliability, historyConf); foundIdentifier = true; } } if (foundIdentifier == false) { ConfOrRel possibleIdentifier; possibleIdentifier.Identifier = historyIdentifier; possibleIdentifier.Reliability = historyConf; possibleIdentifiers.push_back(possibleIdentifier); } } } return possibleIdentifiers; } /// saves the Identifiers in the History /// \brief It checks the incoming Identifiers if any have a Reliability /// greater than 0.5 all of them get saved inside the History and then the /// History get shortened to the maximal length. It only saves the Value if /// the History is empty. /// /// \param actualPossibleIdentifiers The Identifiers which should be saved /// /// \note Does the History really make sense if the values are to small it /// only stores something if it's empty and not if it isn't completely filled void saveInHistory(std::vector actualPossibleIdentifiers) { // check if the reliability of at least one possible Identifier is high // enough bool atLeastOneRelIsHigh = false; for (ConfOrRel pS : actualPossibleIdentifiers) { if (pS.Reliability > 0.5) { atLeastOneRelIsHigh = true; } } // save possible Identifiers if at least one possible Identifier is high // enough (or if the history is empty) if (History.size() < 1 || atLeastOneRelIsHigh == true) { History.insert(History.begin(), actualPossibleIdentifiers); // if history size is higher than allowed, save oldest element while (History.size() > HistoryMaxSize) { // delete possibleIdentifierHistory.back(); History.pop_back(); } } } }; } // namespace agent } // namespace rosa #endif // !ROSA_AGENT_ReliabilityConfidenceCombinator_H