diff --git a/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp b/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp index 6fe7775..b509b8d 100644 --- a/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp +++ b/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp @@ -1,304 +1,315 @@ //===- examples/agent-functionalities/Reliability-functionality.cpp *C++-*-===// // // The RoSA Framework // //===----------------------------------------------------------------------===// /// /// \file examples/agent-functionalities/Reliability-functionality.cpp /// /// \author Daniel Schnoell (daniel.schnoell@tuwien.ac.at ) /// /// \date 2019 /// /// \brief A simple example on defining Relianility Functionalities inside a /// master slave context. /// //===----------------------------------------------------------------------===// #define Reliability_trace_level 5 #include "rosa/config/version.h" #include "rosa/support/log.h" #include "rosa/agent/CrossReliability.h" #include "rosa/agent/RangeConfidence.hpp" #include "rosa/agent/Reliability.h" #include "rosa/deluxe/DeluxeContext.hpp" #include #include +typedef double SensorValueType; +typedef long StateType; +typedef double ReliabilityType; + #include "./helper.h" // just stuff from Rel-func to increase readability using namespace rosa::agent; using namespace rosa; using namespace rosa::deluxe; using namespace rosa::terminal; /// Helper structs for conversion template struct conversion; template class A, typename... TypesB, template class B> struct conversion, B> { using type = typename conversion, std::tuple>::type; }; template class A, typename... TypesB, template class B> struct conversion<0, A, B> { using type = DeluxeTuple; }; template using unrolled_data_type = typename conversion, std::tuple<>>::type; +template +struct __convert_to_vector { +void operator()( + std::vector> &feedback, + unrolled_data_type I) { + __convert_to_vector(feedback, I); + feedback.push_back({std::get(I.first), std::get(I.first)}); +} +} +; +template struct __convert_to_vector { + void operator()(std::vector> &feedback, + unrolled_data_type I) { + feedback.push_back({std::get<0>(I.first), std::get<1>(I.first)}); + } +}; + +template +void convert_to_vector( + std::vector> &feedback, + unrolled_data_type I) { + __convert_to_vector()(feedback, I); +} + + int main(void) { - typedef double SensorValueType; - typedef long StateType; - typedef double ReliabilityType; const std::size_t number_of_states = 3; std::unique_ptr C = DeluxeContext::create("Deluxe"); //---------------------- Sensors ------------------------------------- //-------------------------------------------------------------------- const std::string SensorName1 = "Sensor1"; const std::string SensorName2 = "Sensor2"; const std::string SensorName3 = "Sensor3"; AgentHandle Sensor1 = C->createSensor( SensorName1, [&SensorName1](std::pair I) { LOG_INFO_STREAM << "\n******\n" << SensorName1 << " master-input " << (I.second ? "" : "") << " value: " << I.first << "\n******\n"; }); AgentHandle Sensor2 = C->createSensor( SensorName2, [&SensorName2](std::pair I) { LOG_INFO_STREAM << "\n******\n" << SensorName2 << " master-input " << (I.second ? "" : "") << " value: " << I.first << "\n******\n"; }); AgentHandle Sensor3 = C->createSensor( SensorName3, [&SensorName3](std::pair I) { LOG_INFO_STREAM << "\n******\n" << SensorName3 << " master-input " << (I.second ? "" : "") << " value: " << I.first << "\n******\n"; }); //------------------------- lowlevel agents -------------------------------- //-------------------------------------------------------------------------- const std::string LowLevelAgentName1 = "LowLevelAgent1"; const std::string LowLevelAgentName2 = "LowLevelAgent2"; const std::string LowLevelAgentName3 = "LowLevelAgent3"; using conf = unrolled_data_type< number_of_states, StateType, - ReliabilityType>; // this is the confidence expressed as one tuple it uses - // the format (first.state,first.rel,second.sate...) + ReliabilityType>; // this is the confidence expressed as one tuple it + // uses the format + // (first.state,first.rel,second.sate...) using LowLevelAgentMasterResult = void; // no return using LowLevelReturnFromMaster = std::pair; using FloatMasterHandler = std::function; using FloatResult = Optional>; using FloatHandler = std::function, bool>)>; auto lowlevel1 = create_lowlevel_func(); + auto lowlevel2 = create_lowlevel_func(); + auto lowlevel3 = create_lowlevel_func(); - AgentHandle FloatAgent = C->createAgent( + AgentHandle SlaveAgent = C->createAgent( LowLevelAgentName1, // Master-input handler. - FloatMasterHandler([&LowLevelAgentName1](LowLevelReturnFromMaster I) - -> LowLevelAgentMasterResult { - - }), + FloatMasterHandler( + [&LowLevelAgentName1, + lowlevel1](LowLevelReturnFromMaster I) -> LowLevelAgentMasterResult { + LOG_INFO_STREAM << "inside: " << LowLevelAgentName1 << "feedback\n"; + if (I.second) { + std::vector> feedback; + convert_to_vector(feedback, I.first); + lowlevel1->feedback(feedback); + } + }), // Slave-input handler. FloatHandler( - [&LowLevelAgentName1]( + [&LowLevelAgentName1, lowlevel1]( std::pair, bool> I) -> FloatResult { LOG_INFO_STREAM << "\n******\n" << LowLevelAgentName1 << " " << (I.second ? "" : "") << " value: " << std::get<0>(I.first) << "\n******\n"; - DeluxeTuple ret(0, std::get<0>(I.first)); + + auto tmp = lowlevel1->operator()(std::get<0>(I.first)); + DeluxeTuple ret(tmp.score, tmp.Reliability); return {ret}; })); - //------------------------- lookup copy of rel------------------------------ - //-------------------------------------------------------------------------- - + AgentHandle SlaveAgent2 = C->createAgent( + LowLevelAgentName2, + // Master-input handler. + FloatMasterHandler( + [&LowLevelAgentName2, + lowlevel2](LowLevelReturnFromMaster I) -> LowLevelAgentMasterResult { + LOG_INFO_STREAM << "inside: " << LowLevelAgentName2 << "feedback\n"; + if (I.second) { + std::vector> feedback; + convert_to_vector(feedback, I.first); + lowlevel2->feedback(feedback); + } + }), + // Slave-input handler. + FloatHandler( + [&LowLevelAgentName2, lowlevel2]( + std::pair, bool> I) -> FloatResult { + LOG_INFO_STREAM + << "\n******\n" + << LowLevelAgentName2 << " " << (I.second ? "" : "") + << " value: " << std::get<0>(I.first) << "\n******\n"; - /* ----------------------------- Do Something - * ---------------------------------------------------------------- */ - std::cout << "Testing the lowlevel component with static feedback telling it " - "that the most lickely state is 2.\n"; - for (int a = 0; a < 30; a++) - std::cout << "a: " << a << "\n" - << (lowlevel->feedback({{0, 0}, {1, 0.3}, {2, 0.8}}), - lowlevel->operator()(a)) - << "\n"; + auto tmp = lowlevel2->operator()(std::get<0>(I.first)); + DeluxeTuple ret(tmp.score, tmp.Reliability); + return {ret}; + })); + + AgentHandle SlaveAgent3 = C->createAgent( + LowLevelAgentName3, + // Master-input handler. + FloatMasterHandler( + [&LowLevelAgentName3, + lowlevel3](LowLevelReturnFromMaster I) -> LowLevelAgentMasterResult { + LOG_INFO_STREAM << "inside: " << LowLevelAgentName3 << "feedback\n"; + if (I.second) { + std::vector> feedback; + convert_to_vector(feedback, I.first); + lowlevel3->feedback(feedback); + } + }), + // Slave-input handler. + FloatHandler( + [&LowLevelAgentName3, lowlevel3]( + std::pair, bool> I) -> FloatResult { + LOG_INFO_STREAM + << "\n******\n" + << LowLevelAgentName3 << " " << (I.second ? "" : "") + << " value: " << std::get<0>(I.first) << "\n******\n"; + + auto tmp = lowlevel3->operator()(std::get<0>(I.first)); + DeluxeTuple ret(tmp.score, tmp.Reliability); + return {ret}; + })); - /* ----------------------------- Cleanup - * --------------------------------------------------------------------- */ + //------------------------- lookup copy of rel------------------------------ + //-------------------------------------------------------------------------- std::cout << "---------------------------------------------------------------" "---------------------------------\n"; std::cout << "------------------------------------High level " "Test---------------------------------------------\n"; std::cout << "Configured in a way that the Master thinks that both Sensors " "should have the same State.\n While feeding both the \"opposite\" " "values one acending the other decending from the maximum.\n"; - std::unique_ptr> - Confidence2(new RangeConfidence( - {{0, PartialFunction( - { - {{0, 3}, - std::make_shared>( - 0, 1.0 / 3)}, - {{3, 6}, - std::make_shared>(1, 0)}, - {{6, 9}, - std::make_shared>( - 3.0, -1.0 / 3)}, - }, - 0)}, - {1, PartialFunction( - { - {{6, 9}, - std::make_shared>( - -2, 1.0 / 3)}, - {{9, 12}, - std::make_shared>(1, 0)}, - {{12, 15}, - std::make_shared>( - 5, -1.0 / 3)}, - }, - 0)}, - {2, PartialFunction( - { - {{12, 15}, - std::make_shared>( - -4, 1.0 / 3)}, - {{15, 18}, - std::make_shared>(1, 0)}, - {{18, 21}, - std::make_shared>( - 7, -1.0 / 3)}, - }, - 0)}})); - - std::unique_ptr> Reliability2( - new LinearFunction(1, -1.0 / 9)); - - std::unique_ptr> - ReliabilitySlope2( - new LinearFunction(1, -1.0 / 9)); - - std::unique_ptr> TimeConfidence2( - new LinearFunction(1, -1.0 / 9)); - - auto lowlevel2 = new ReliabilityForLowLevelAgents(); - - std::vector states2; - states2.push_back(0); - states2.push_back(1); - states2.push_back(2); - - lowlevel2->setConfidenceFunction(Confidence2); - lowlevel2->setReliabilityFunction(Reliability2); - lowlevel2->setReliabilitySlopeFunction(ReliabilitySlope2); - lowlevel2->setTimeConfidenceFunction(TimeConfidence2); - lowlevel2->setStates(states2); - lowlevel2->setHistoryLength(2); - lowlevel2->setValueSetCounter(1); - ReliabilityForHighLevelAgents *highlevel = new ReliabilityForHighLevelAgents(); std::unique_ptr> CrossReliability1(new CrossReliability()); std::unique_ptr> func1( new PartialFunction( { {{0, 1}, std::make_shared>(1, 0)}, {{1, 2}, std::make_shared>(2, -1.0)}, }, 0)); CrossReliability1->addCrossReliabilityProfile(0, 1, func1); CrossReliability1->setCrossReliabilityMethod( CrossReliability::AVERAGE); CrossReliability1->setCrossReliabilityParameter(1); std::unique_ptr> CrossConfidence1( new CrossConfidence()); std::unique_ptr> func2( new PartialFunction( { {{0, 1}, std::make_shared>(1, 0)}, {{1, 2}, std::make_shared>(2, -1.0)}, }, 0)); CrossConfidence1->addCrossReliabilityProfile(0, 1, func2); CrossConfidence1->setCrossReliabilityMethod( CrossConfidence::AVERAGE); CrossConfidence1->setCrossReliabilityParameter(1); highlevel->setCrossConfidence(CrossConfidence1); highlevel->setCrossReliability(CrossReliability1); highlevel->addStates(0, states); highlevel->addStates(1, states); for (int a = 0; a < 21; a++) { auto out1 = lowlevel->operator()(a), out2 = lowlevel2->operator()((int)21 - a); std::cout << "s1: " << out1 << "\ns2:" << out2 << "\n"; std::vector> tmp2; tmp2.push_back({0, out1.score, out1.Reliability}); tmp2.push_back({1, out2.score, out2.Reliability}); auto out_o = highlevel->operator()(tmp2); std::cout << "it: " << a << "\t rel: " << out_o.CrossReliability << "\n"; std::cout << "\t subs:\n"; for (auto q : out_o.CrossConfidence) { std::cout << "\t\t id:" << q.first << "\n"; /* for(auto z: q.second) { std::cout << "\t\t\t score: " << z.score << "\tRel: " << z.Reliability << "\n"; tmp.push_back({z.score,z.Reliability}); } */ for (auto z : q.second) { std::cout << "\t\t\t score: " << z.score << "\tRel: " << z.Reliability << "\n"; } if (q.first == 0) lowlevel->feedback(q.second); else lowlevel2->feedback(q.second); } } - delete lowlevel; + delete lowlevel1; delete lowlevel2; } \ No newline at end of file diff --git a/examples/agent-functionalities/Reliability-functionality/Reliability-functionality.cpp b/examples/agent-functionalities/Reliability-functionality/Reliability-functionality.cpp index a80318a..1e1ae48 100644 --- a/examples/agent-functionalities/Reliability-functionality/Reliability-functionality.cpp +++ b/examples/agent-functionalities/Reliability-functionality/Reliability-functionality.cpp @@ -1,263 +1,264 @@ //===- examples/agent-functionalities/Reliability-functionality.cpp *C++-*-===// // // The RoSA Framework // //===----------------------------------------------------------------------===// /// /// \file examples/agent-functionalities/Reliability-functionality.cpp /// /// \author Daniel Schnoell (daniel.schnoell@tuwien.ac.at ) /// /// \date 2019 /// /// \brief A simple example on defining Relianility Functionalities. /// //===----------------------------------------------------------------------===// #define Reliability_trace_level 5 #include "rosa/config/version.h" #include "rosa/support/log.h" #include "rosa/agent/CrossReliability.h" #include "rosa/agent/RangeConfidence.hpp" #include "rosa/agent/Reliability.h" #include #include using namespace rosa::agent; int main(void) { typedef double SensorValueType; typedef long StateType; typedef double ReliabilityType; std::unique_ptr> Confidence(new RangeConfidence( {{0, PartialFunction( { {{0, 3}, std::make_shared>( 0, 1.0 / 3)}, {{3, 6}, std::make_shared>(1, 0)}, {{6, 9}, std::make_shared>( 3.0, -1.0 / 3)}, }, 0)}, {1, PartialFunction( { {{6, 9}, std::make_shared>( -2, 1.0 / 3)}, {{9, 12}, std::make_shared>(1, 0)}, {{12, 15}, std::make_shared>( 5, -1.0 / 3)}, }, 0)}, {2, PartialFunction( { {{12, 15}, std::make_shared>( -4, 1.0 / 3)}, {{15, 18}, std::make_shared>(1, 0)}, {{18, 21}, std::make_shared>( 7, -1.0 / 3)}, }, 0)}})); std::unique_ptr> Reliability( new LinearFunction(1, -1.0 / 3)); std::unique_ptr> ReliabilitySlope( new LinearFunction(1, -1.0 / 3)); std::unique_ptr> TimeConfidence( new LinearFunction(1, -1.0 / 3)); auto lowlevel = new ReliabilityForLowLevelAgents(); std::vector states; states.push_back(0); states.push_back(1); states.push_back(2); lowlevel->setConfidenceFunction(Confidence); lowlevel->setReliabilityFunction(Reliability); lowlevel->setReliabilitySlopeFunction(ReliabilitySlope); lowlevel->setTimeConfidenceFunction(TimeConfidence); lowlevel->setStates(states); lowlevel->setHistoryLength(2); lowlevel->setValueSetCounter(1); /* ----------------------------- Do Something * ---------------------------------------------------------------- */ std::cout << "Testing the lowlevel component with static feedback telling it " "that the most lickely state is 2.\n"; for (int a = 0; a < 30; a++) std::cout << "a: " << a << "\n" << (lowlevel->feedback({{0, 0}, {1, 0.3}, {2, 0.8}}), lowlevel->operator()(a)) << "\n"; - /* ----------------------------- Cleanup - * --------------------------------------------------------------------- */ std::cout << "---------------------------------------------------------------" "---------------------------------\n"; std::cout << "------------------------------------High level " "Test---------------------------------------------\n"; std::cout << "Configured in a way that the Master thinks that both Sensors " "should have the same State.\n While feeding both the \"opposite\" " "values one acending the other decending from the maximum.\n"; std::unique_ptr> Confidence2(new RangeConfidence( {{0, PartialFunction( { {{0, 3}, std::make_shared>( 0, 1.0 / 3)}, {{3, 6}, std::make_shared>(1, 0)}, {{6, 9}, std::make_shared>( 3.0, -1.0 / 3)}, }, 0)}, {1, PartialFunction( { {{6, 9}, std::make_shared>( -2, 1.0 / 3)}, {{9, 12}, std::make_shared>(1, 0)}, {{12, 15}, std::make_shared>( 5, -1.0 / 3)}, }, 0)}, {2, PartialFunction( { {{12, 15}, std::make_shared>( -4, 1.0 / 3)}, {{15, 18}, std::make_shared>(1, 0)}, {{18, 21}, std::make_shared>( 7, -1.0 / 3)}, }, 0)}})); std::unique_ptr> Reliability2( new LinearFunction(1, -1.0 / 9)); std::unique_ptr> ReliabilitySlope2( new LinearFunction(1, -1.0 / 9)); std::unique_ptr> TimeConfidence2( new LinearFunction(1, -1.0 / 9)); auto lowlevel2 = new ReliabilityForLowLevelAgents(); std::vector states2; states2.push_back(0); states2.push_back(1); states2.push_back(2); lowlevel2->setConfidenceFunction(Confidence2); lowlevel2->setReliabilityFunction(Reliability2); lowlevel2->setReliabilitySlopeFunction(ReliabilitySlope2); lowlevel2->setTimeConfidenceFunction(TimeConfidence2); lowlevel2->setStates(states2); lowlevel2->setHistoryLength(2); lowlevel2->setValueSetCounter(1); ReliabilityForHighLevelAgents *highlevel = new ReliabilityForHighLevelAgents(); std::unique_ptr> CrossReliability1(new CrossReliability()); std::unique_ptr> func1(new PartialFunction( { {{0, 1}, std::make_shared>(1, 0)}, {{1, 2}, std::make_shared>(2, -1.0)}, }, 0)); CrossReliability1->addCrossReliabilityProfile(0, 1, func1); CrossReliability1->setCrossReliabilityMethod( CrossReliability::AVERAGE); CrossReliability1->setCrossReliabilityParameter(1); std::unique_ptr> CrossConfidence1( new CrossConfidence()); std::unique_ptr> func2(new PartialFunction( { {{0, 1}, std::make_shared>(1, 0)}, {{1, 2}, std::make_shared>(2, -1.0)}, }, 0)); CrossConfidence1->addCrossReliabilityProfile(0, 1, func2); CrossConfidence1->setCrossReliabilityMethod( CrossConfidence::AVERAGE); CrossConfidence1->setCrossReliabilityParameter(1); highlevel->setCrossConfidence(CrossConfidence1); highlevel->setCrossReliability(CrossReliability1); highlevel->addStates(0, states); highlevel->addStates(1, states); for (int a = 0; a < 21; a++) { auto out1 = lowlevel->operator()(a), out2 = lowlevel2->operator()((int)21 - a); std::cout << "s1: " << out1 << "\ns2:" << out2 << "\n"; std::vector> tmp2; tmp2.push_back({0, out1.score, out1.Reliability}); tmp2.push_back({1, out2.score, out2.Reliability}); auto out_o = highlevel->operator()(tmp2); std::cout << "it: " << a << "\t rel: " << out_o.CrossReliability << "\n"; std::cout << "\t subs:\n"; for (auto q : out_o.CrossConfidence) { std::cout << "\t\t id:" << q.first << "\n"; /* for(auto z: q.second) { std::cout << "\t\t\t score: " << z.score << "\tRel: " << z.Reliability << "\n"; tmp.push_back({z.score,z.Reliability}); } */ for (auto z : q.second) { std::cout << "\t\t\t score: " << z.score << "\tRel: " << z.Reliability << "\n"; } if (q.first == 0) lowlevel->feedback(q.second); else lowlevel2->feedback(q.second); } } + /* ----------------------------- Cleanup + * --------------------------------------------------------------------- */ + delete highlevel; delete lowlevel; delete lowlevel2; } \ No newline at end of file