diff --git a/include/rosa/deluxe/CrossReliability.h b/include/rosa/deluxe/CrossReliability.h index 8d34ffd..396e3c4 100644 --- a/include/rosa/deluxe/CrossReliability.h +++ b/include/rosa/deluxe/CrossReliability.h @@ -1,246 +1,240 @@ #pragma once //===-- 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. -/// \note This file includes things that are just to make the relationships -/// clearer of the methods/functions +/// \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 //===----------------------------------------------------------------------===// // nedded headers #include //assert #include // for static methods #include #include +#include -template class ReliableAgent { - //------------------------------------------------------------------------------ - // Reliability or Confidence -public: - // at=0 should be the most reliable score - Type getReliability(std::size_t at) { return PossibleScores.at(at)->second; } - void setReliability(std::size_t at, Type Reliability) { - this->Reliability = PossibleScores.at(at)->second; - } +// LinearFunction +#include "rosa/agent/LinearFunctions.hpp" - Type getScore(std::size_t at) { return PossibleScores.at(at)->first; } - void setScore(std::size_t at, Type Score) { - this->Reliability = PossibleScores.at(at)->first; - } +#include "CrossReliability_helper_file.h" - void createPossibleScore(Type Score, Type Reliability) { - PossibleScores.push_back(new std::pair(Score, Reliability)); - } - std::size_t getNumberOfPossibleScores() { return PossibleScores.size(); } +template class CrossReliabilityModule { -protected: - Type crossReliabilityParameter = 1; + using LinearFunction = typename rosa::agent::LinearFunction; + using ReliableAgent = typename ReliableAgent; + + struct Functionblock { + bool exists = false; + std::string nameA; + std::string nameB; + LinearFunction *Funct; + }; -private: - std::vector *> - PossibleScores; // first: Score second: Reliability or Confidence - //-------------------------------------------------------------------------------- - // ReliableAgent public: - /// This is purely a testfunction - void makeReliability(double Method(std::vector values)); + /// adds a Function to the module + /// \param nameA + /// \param nameB these parameters have to be unique + /// \param Function should be uniquely owned by the Module and will be deleted + /// upon Deletion of the object + void addCrossReliabilityProfile(std::string nameA, std::string nameB, + LinearFunction *Function); + /// sets the cross Reliability Parameter + void setcrossReliabilityParameter(Type val) { + crossReliabilityParameter = val; + } - std::vector Slaves; - ReliableAgent *Master; + /// Deconstructor deletes all stored LinearFunctions ! + ~CrossReliabilityModule() { + for (auto tmp : Functions) + delete tmp.Funct; + } -private: +protected: //-------------------------------------------------------------------------------- // Relevvant methods /// Calculets the CrossReliability /// \param mainagent pointer of the relevant SlaveAgent /// \param SlaveAgents pointers to all Slave Agents of the /// \note all the given Agents need to have at least 1 possibleScore /// \param Method a function pointer to the used combination method + /// \note im currently using getScore(0) this isn't correct maxi uses + /// getPossibleScoreCount() which returns possibleScoreCount. + /// possibleScoreCount isn't reset/set/inc inside or outside the use of this + /// function Type CrossReliability(ReliableAgent *MainAgent, std::vector SlaveAgents, Type Method(std::vector values)); /// Calculets the CrossReliability /// \param mainagent pointer of the relevant SlaveAgent /// \param SlaveAgents pointers to all Slave Agents of the /// \note all the given Agents need to have at least 1 possibleScore /// \param Method a function pointer to the used combination method /// \param TheoreticalValue uses this value instead of the MainAgents score /// needed for playing "what if" Type CrossConfidence(ReliableAgent *mainAgent, std::vector SlaveAgents, Type Method(std::vector values), Type TheoreticalValue); //-------------------------------------------------------------------------------- // helper functions - + /// evalues the absolute distance between two values Type AbsuluteValue(Type A, Type B) { return ((A - B) < 0) ? B - A : A - B; } + /// 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(std::string nameA, std::string 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); + } + + //-------------------------------------------------------------------------------- + // stored Data + + Type crossReliabilityParameter = 1; + + /// this could be changed to use a template + /// template class + /// CrossReliabilityModule [...] + /// storagecontainer Functions; + std::vector Functions; + + /// verry inefficient searchFunction + Functionblock (*searchFunction)(std::vector vect, + const std::string nameA, + const std::string nameB) = + [](std::vector vect, const std::string nameA, + const std::string nameB) -> Functionblock { + for (Functionblock tmp : vect) { + if (tmp.nameA == nameA && tmp.nameB == nameB) + return tmp; + if (tmp.nameA == nameB && tmp.nameB == nameA) + return tmp; + } + return Functionblock(); + }; + //-------------------------------------------------------------------------------- // pre defined combination methods public: + /// predefined combination method static Type CONJUNCTION(std::vector values) { static_assert(std::is_arithmetic::value); // sanitny check return *std::min_element(values.begin(), values.end()); } + + /// predefined combination method static Type AVERAGE(std::vector values) { static_assert(std::is_arithmetic::value); // sanitny check return std::accumulate(values.begin(), values.end(), 0.0) / values.size(); } + + /// predefined combination method static Type DISJUNCTION(std::vector values) { static_assert(std::is_arithmetic::value); // sanitny check return *std::max_element(values.begin(), values.end()); } //------------------------------------------------------------------------------- }; -/// This would be an Function block and should be costomizable for each agent-agent "cross" -template Type CrossReliabilityFunctionGetY(Type value) { - return value; +template +inline void CrossReliabilityModule::addCrossReliabilityProfile( + std::string nameA, std::string nameB, LinearFunction *Function) { + Functions.push_back({true, nameA, nameB, Function}); } /* The following things are needed inside this method - getScore -- crossReliabilityParameter -- AbsuluteValue -- CrossReliabilityFunctionGetY +- ReliableAgent */ template -inline Type ReliableAgent::CrossReliability( - ReliableAgent *mainAgent, - std::vector *> SlaveAgents, +inline Type CrossReliabilityModule::CrossReliability( + ReliableAgent *mainAgent, std::vector SlaveAgents, Type Method(std::vector values)) { static_assert(std::is_arithmetic::value); // sanitny check Type crossReliabiability; std::vector values; for (ReliableAgent *SlaveAgent : SlaveAgents) { if (SlaveAgent == mainAgent) continue; if (mainAgent->getScore(0) == SlaveAgent->getScore(0)) crossReliabiability = 1; else crossReliabiability = 1 / (crossReliabilityParameter * AbsuluteValue(mainAgent->getScore(0), SlaveAgent->getScore(0))); // profile reliability - Type crossReliabilityFromProfile = CrossReliabilityFunctionGetY( + Type crossReliabilityFromProfile = getCrossReliabilityFromProfile( + mainAgent->getName(), SlaveAgent->getName(), AbsuluteValue(mainAgent->getScore(0), SlaveAgent->getScore(0))); values.push_back( std::max(crossReliabiability, crossReliabilityFromProfile)); } return Method(values); } /* The following things are needed inside this method - getScore -- crossReliabilityParameter -- AbsuluteValue -- CrossReliabilityFunctionGetY +- ReliableAgent */ template -inline Type ReliableAgent::CrossConfidence( +inline Type CrossReliabilityModule::CrossConfidence( ReliableAgent *mainAgent, std::vector SlaveAgents, Type Method(std::vector values), Type TheoreticalValue) { Type crossReliabiability; std::vector values; for (ReliableAgent *SlaveAgent : SlaveAgents) { if (SlaveAgent == mainAgent) continue; if (TheoreticalValue == SlaveAgent->getScore(0)) crossReliabiability = 1; else crossReliabiability = 1 / (crossReliabilityParameter * AbsuluteValue(TheoreticalValue, SlaveAgent->getScore(0))); // profile reliability - Type crossReliabilityFromProfile = CrossReliabilityFunctionGetY( - AbsuluteValue(TheoreticalValue, SlaveAgent->getScore(0))); + Type crossReliabilityFromProfile = getCrossReliabilityFromProfile( + mainAgent->getName(), SlaveAgent->getName(), + AbsuluteValue(mainAgent->getScore(0), SlaveAgent->getScore(0))); values.push_back( std::max(crossReliabiability, crossReliabilityFromProfile)); } return Method(values); } - - -// pure testmethod -template -inline void ReliableAgent::makeReliability( - double Method(std::vector values)) { - std::cout << "score" - << "\tCR\t"; - for (std::size_t tmp = 0; tmp < 10; tmp++) - std::cout << "\t" << ((Type)tmp) / 10; - std::cout << "\n"; - for (ReliableAgent *mainagent : Slaves) { - std::cout << mainagent->getScore(0) << "\t" // carefull not fale save - << CrossReliability(mainagent, Slaves, Method) << "\t"; - for (std::size_t tmp = 0; tmp < 10; tmp++) - std::cout << "\t" - << CrossConfidence(mainagent, Slaves, Method, ((Type)tmp) / 10); - std::cout << "\n"; - } -} - -/* -//test file include correct path - -#include - -int main(void) -{ - std::vector* > Slaves; - std::size_t size=20; - for (std::size_t a = 0; a < size; a++) - { - auto tmp = new ReliableAgent(); - tmp->createPossibleScore(((double)a) / size, ((double)a)/size); - Slaves.push_back(tmp); - } - - ReliableAgent Master; - Master.Slaves.insert(Master.Slaves.begin(), Slaves.begin(), -Slaves.end()); - - Master.makeReliability(ReliableAgent::AVERAGE); - - - - - - //cleanup - for (auto tmp : Slaves) - delete tmp; - Slaves.clear(); - Master.Slaves.clear(); - - return 0; -} -*/ \ No newline at end of file diff --git a/include/rosa/deluxe/CrossReliability_helper_file.h b/include/rosa/deluxe/CrossReliability_helper_file.h new file mode 100644 index 0000000..66bf5bd --- /dev/null +++ b/include/rosa/deluxe/CrossReliability_helper_file.h @@ -0,0 +1,152 @@ +#pragma once +//===-- rosa/delux/CrossReliability_helper_file.h ----------------*- C++ +//-*-===// +// +// The RoSA Framework +// +//===----------------------------------------------------------------------===// +/// +/// \file rosa/delux/CrossReliability_helper_file.h +/// +/// \author Daniel Schnoell +/// +/// \date 2019 +/// +/// \brief This file shouldn't exist when everything is finished. It is only a +/// temporary part to hava all needed references and it also contains source +/// code for test compiling +//===----------------------------------------------------------------------===// + +#ifdef __fake__LinearFunction +namespace rosa { +namespace agent { +template class LinearFunction { +public: + virtual A operator()(B val) = 0; +}; +template +class testFunction : public LinearFunction { +public: + virtual A operator()(B val) { return val; } +}; +} // namespace agent +} // namespace rosa +#endif + +template class ReliableAgent { + //------------------------------------------------------------------------------ + // Reliability or Confidence +public: + // at=0 should be the most reliable score + Type getReliability(std::size_t at) { return PossibleScores.at(at)->second; } + void setReliability(std::size_t at, Type Reliability) { + this->Reliability = PossibleScores.at(at)->second; + } + + Type getScore(std::size_t at) { return PossibleScores.at(at)->first; } + void setScore(std::size_t at, Type Score) { + this->Reliability = PossibleScores.at(at)->first; + } + + void createPossibleScore(Type Score, Type Reliability) { + PossibleScores.push_back(new std::pair(Score, Reliability)); + } + std::size_t getNumberOfPossibleScores() { return PossibleScores.size(); } + void setName(std::string nam) { Name = nam; } + std::string getName() { return Name; } + +protected: + Type crossReliabilityParameter = 1; + +private: + std::string Name; + std::vector *> + PossibleScores; // first: Score second: Reliability or Confidence + //-------------------------------------------------------------------------------- + // ReliableAgent +public: + std::vector Slaves; + ReliableAgent *Master; + +private: +}; + +/* +//test file include correct path + +#define __fake__LinearFunction +#include "CrossReliability.h" +#include + +class tester :public ReliableAgent, public +CrossReliabilityModule +{ +public: + // pure testmethod + void makeReliability( + double Method(std::vector values)) { + std::cout << "score" + << "\tCR\t"; + for (std::size_t tmp = 0; tmp < 10; tmp++) + std::cout << "\t" << ((double)tmp) / 10; + std::cout << "\n"; + for ( auto mainagent : Slaves) { + std::cout << mainagent->getScore(0) << "\t" // carefull +not fale save + << CrossReliability(mainagent, Slaves, Method) +<< "\t"; for (std::size_t tmp = 0; tmp < 10; tmp++) std::cout << "\t" + << CrossConfidence(mainagent, Slaves, Method, +((double)tmp) / 10); std::cout << "\n"; + } + } + + +}; + +int main(void) +{ + std::vector Slaves; + std::size_t size = 20; + for (std::size_t a = 0; a < size; a++) + { + auto tmp = new tester(); + tmp->createPossibleScore(((double)a) / size, + ((double)a) / size); + tmp->setName(std::to_string(a)); + Slaves.push_back(tmp); + } + + tester Master; + Master.Slaves.insert(Master.Slaves.begin(), Slaves.begin(), + Slaves.end()); + + for (std::size_t a = 0; a < size; a++) + { + for (std::size_t b = 0; b < size; b++) + { + Master.addCrossReliabilityProfile(std::to_string(a), +std::to_string(b),new rosa::agent::testFunction()); + } + } + + Master.makeReliability(tester::AVERAGE); + + + + + + //cleanup + for (auto tmp : Slaves) + delete tmp; + Slaves.clear(); + Master.Slaves.clear(); + + Master.~Master(); + + long q; + std::cin >> q; + + return 0; +} + +*/ \ No newline at end of file