diff --git a/include/rosa/agent/Reliability.h b/include/rosa/agent/Reliability.h index 2f33665..ec68be6 100644 --- a/include/rosa/agent/Reliability.h +++ b/include/rosa/agent/Reliability.h @@ -1,242 +1,257 @@ //===-- rosa/agent/Reliability.h --------------------------------*- C++ -*-===// // // The RoSA Framework // //===----------------------------------------------------------------------===// /// /// \file rosa/agent/Reliability.h /// /// \author Daniel Schnoell (danielschnoell@tuwien.ac.at) /// /// \date 2019 /// /// \brief Declaration of `rosa::Reliability` base-class. /// //===----------------------------------------------------------------------===// #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 namespace rosa { namespace agent { - template - class LowLevel { + template + class LowLevel { struct ConfOrRel { StateType score; ReliabilityType Reliability; }; std::vector> History; std::size_t HistoryMaxSize; std::vector ValuesFromMaster; SensorValueType previousSensorValue; unsigned int valueSetCounter; std::vector States; bool PreviousSensorValueExists = false; RangeConfidence* Confidence; Abstraction* Reliability; Abstraction* ReliabilitySlope; Abstraction* TimeConfidence; ReliabilityType getRelibility(SensorValueType actualValue, SensorValueType lastValue, unsigned int valueSetCounter) { ReliabilityType relAbs = Reliability->operator()(actualValue); 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); } std::vector getAllPossibleScoresBasedOnHistory(std::vector possibleScores) { //iterate through all history entries std::size_t posInHistory = 0; typedef typename std::vector>::iterator iter; for (iter pShE = History.begin(); pShE < History.end(); pShE++, posInHistory++) { //iterate through all possible scores of each history entry for (typename std::vector::iterator pSh : *pShE) { //printf("a3\n"); int historyScore = pSh->score; float 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(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->confOrRel = std::max(pS->confOrRel, historyConf); foundScore = true; } } if (foundScore == false) { ConfOrRel possibleScore; possibleScore.score = historyScore; possibleScore.Reliability = historyConf; possibleScores->push_back(possibleScore); } } } return possibleScores; } void saveInHistory(std::vector 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.push_front(actualPossibleScores); //if history size is higher than allowed, savo oldest element while (History.size() > HistoryMaxSize) { //delete possibleScoreHistory.back(); History.pop_back(); } } } ConfOrRel operator()(SensorValueType SensorValue) { std::map ActuallPosibleScores_tmp = Confidence(SensorValue); std::vector ActuallPossibleScores; for (auto state : States) ActuallPossibleScores.push_back({ state, ActuallPosibleScores_tmp.find(state) }); ReliabilityType inputReliability; if (PreviousSensorValueExists) inputReliability = getRelibility(SensorValue, previousSensorValue, valueSetCounter); else inputReliability = Reliability(SensorValue); for (std::size_t at = 0; at < ActuallPossibleScores.size(); at++) ActuallPossibleScores.at(at) = std::min(ActuallPossibleScores.at(at), 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 possibleScores; getAllPossibleScoresBasedOnHistory(&possibleScores); std::sort(possibleScores.begin(), possibleScores.end(), [](ReliabilityType A, ReliabilityType B)-> bool {return A > B; }); previousSensorValue = SensorValue; PreviousSensorValueExists = true; return possibleScores.at(0); } }; - template + template class HighLevel { struct ConfOrRel { StateType score; ReliabilityType Reliability; }; - CrossReliability + struct returnType { + ReliabilityType CrossReliability; + std::vector>> CrossConfidence; + }; + + CrossReliability* CrossReliability = nullptr; + CrossConfidence * CrossConfidence = nullptr; - std::vector operator(std::vector Values) + returnType operator()(std::vector> Values) { - int EWS = 0; - float combinedInputRel = 1; - float combinedCrossRel = 1; - float outputReliability; + StateType EWS = 0; + ReliabilityType combinedInputRel = 1; + ReliabilityType combinedCrossRel = 1; + ReliabilityType outputReliability; + + std::vector> Agents; + std::vector>> output; + std::vector output_temporary; + + for (auto tmp : Values) + { + std::pair tmp2; + tmp.first = std::get<0>(tmp); + tmp.second = std::get<1>(tmp); + Agents.push_back(tmp); + } - unsigned int numOfSlaveAgentsWereChecked = 0; - for (auto Value:Values) { - StateType sc=Value.score; - ReliabilityType rel=Value.Reliability; + for (auto Value : Values) { + 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 - float realCrossReliabilityOfSlaveAgent = crossReliabilityNEW(sA, mountedSlaveAgents, CONJUNCTION, 0); //AVERAGE, MULTIPLICATION, CONJUNCTION (best to worst: AVERAGE = CONJUNCTION > MULTIPLICATION >> ) - - //send all theoritcally possible scores to slave agent + ReliabilityType realCrossReliabilityOfSlaveAgent = CrossReliability({ std::get<0>(Value),std::get<1>(Value) }, Agents); //AVERAGE, MULTIPLICATION, CONJUNCTION (best to worst: AVERAGE = CONJUNCTION > MULTIPLICATION >> ) - sA->pass_msgToSendBuffer(ISA_SuggestedAbstractedSensoryDataAndReliability); + output_temporary.clear(); for (int theoreticalScore = 0; theoreticalScore <= 3; theoreticalScore++) { //calculate the cross reliability for this slave agent - float theoreticalCrossReliabilityOfSlaveAgent = crossReliabilityNEW(sA, mountedSlaveAgents, CONJUNCTION_THEORETICAL, theoreticalScore); //AVERAGE_THEORETICAL, MULTIPLICATION_THEORETICAL, CONJUNCTION_THEORETICAL (best to worst: AVERAGE_THEORETICAL = CONJUNCTION_THEORETICAL > MULTIPLICATION_THEORETICAL >> ) - sA->pass_msgToSendBuffer(theoreticalScore); - sA->pass_msgToSendBuffer(theoreticalCrossReliabilityOfSlaveAgent); + ConfOrRel data; + data.score = theoreticalScore; + data.Reliability = CrossConfidence(std::get<0>(Value), theoreticalScore, Agents); + output_temporary.push_back(data); } - sA->send_msgs(); - + output.push_back({ std::get<0>(Value),output_temporary }); combinedCrossRel = std::min(combinedCrossRel, realCrossReliabilityOfSlaveAgent); - numOfSlaveAgentsWereChecked++; } //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 }; } }; } // namespace agent }// namespace rosa #endif // !ROSA_AGENT_RELIABILITY_H diff --git a/include/rosa/agent/bs_file.h b/include/rosa/agent/bs_file.h deleted file mode 100644 index 1e1007e..0000000 --- a/include/rosa/agent/bs_file.h +++ /dev/null @@ -1,91 +0,0 @@ -class Reliability_LowLevel : public Functionality { - float Agent::getInputReliability(SensorSlotOfAgent *mountedAgent, - float actualSensorValue) { - float inputReliability; - float previousSensorValue; - -// calculate input reliability -#ifdef PRINT - printf("%s - ", name.c_str()); -#endif // PRINT - if (mountedAgent->getPreviousSensorValue(&previousSensorValue)) { - inputReliability = reliabilityModule->getRelibility( - actualSensorValue, previousSensorValue, - mountedAgent->getValueSetCounter()); - } else { - inputReliability = - reliabilityModule->getAbsoluteReliability(actualSensorValue); - } -#ifdef PRINT - printf("rel = %f\n", inputReliability); -#endif // PRINT - - return inputReliability; - } - - void Agent::combineConfidenceAndReliabilityOfActualPossibleScores( - vector *actualPossibleScores, float inputReliability) { - for (auto &pS : *actualPossibleScores) { - // combine input reliability with score confidence - // NOTE: multiplication, AND, or average would be alternatives (best to - // worst: AND = Multiplication >> Avg) pS->confOrRel = pS->confOrRel * - // inputReliability; pS->confOrRel = (pS->confOrRel + inputReliability) / - // 2; - pS->confOrRel = std::min(pS->confOrRel, inputReliability); - combineConfidenceAndReliabilityOfActualPossibleScores( - actualPossibleScores, inputReliability); - - for (auto &sS : *suggestedScores) { - for (auto &aPs : *actualPossibleScores) { - if (aPs->score == sS->score) { - // combine suggested score with actual score - // NOTE: multiplication, AND, or average would be alternatives (best - // to worst: AND > multiplication >> average) aPs->confOrRel = - // std::min(aPs->confOrRel, sS->confOrRel); - aPs->confOrRel = (aPs->confOrRel + sS->confOrRel); - // aPs->confOrRel = aPs->confOrRel * sS->confOrRel; - } - } - } - - // save actual possible scores in history - saveActualPossibleScoresInHistory(actualPossibleScores); - - // evaluate all possible scores (based on the history) - vector possibleScores; - - // printf("SIZE BEFORE: %u\n", possibleScores.size()); - - getAllPossibleScoresBasedOnHistory(&possibleScores); - - // printf("SIZE AFTER: %u\n", possibleScores.size()); - - // sort all possible scores so that outputRel_i >= outputRel_i+1 - std::sort(possibleScores.begin(), possibleScores.end(), compareByConfRel); - - // send possible scores to high level agent - // sendPossibleScoresToMaster(&possibleScores); - - for (auto &pS : possibleScores) { -#ifdef PRINT - printf(" > possible Score %i / %f\n", pS->score, pS->confOrRel); -#endif // PRINT - } - - // send most likely score to high level agent - sendMostLikelyScoreToMaster(&possibleScores); - } - } - - void run() { mountedAgent->get_sensorValue(&actualSensorValue); - vector *actualPossibleScores = - scoreConfidenceModule->getScoresWithConfidences(actualSensorValue); - inputReliability = getInputReliability(mountedAgent, actualSensorValue); - - masterAgentHandlerOfAgent->clearSuggestedScores(); - masterAgentHandlerOfAgent->read_masterAgentValues(); - - vector *suggestedScores = - masterAgentHandlerOfAgent->getSuggestedScores(); - } -}; \ No newline at end of file