diff --git a/examples/agent-functionalities/Reliability-functionality-agent-context/CMakeLists.txt b/examples/agent-functionalities/Reliability-functionality-agent-context/CMakeLists.txt index 2045c27..ebb331a 100644 --- a/examples/agent-functionalities/Reliability-functionality-agent-context/CMakeLists.txt +++ b/examples/agent-functionalities/Reliability-functionality-agent-context/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable(Reliability-agents Reliability-agents.cpp) ROSA_add_library_dependencies(Reliability-agents ROSAConfig) ROSA_add_library_dependencies(Reliability-agents ROSACore) ROSA_add_library_dependencies(Reliability-agents ROSAAgent) +ROSA_add_library_dependencies(Reliability-agents ROSADeluxe) 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 b283ce6..6fe7775 100644 --- a/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp +++ b/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp @@ -1,289 +1,304 @@ //===- 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 +#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 = std::tuple; + using type = DeluxeTuple; }; -template -using unrolled_data_type = typename conversion::type; - - - - - -using namespace rosa::agent; +template +using unrolled_data_type = + typename conversion, std::tuple<>>::type; 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); + 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...) + + 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(); + + AgentHandle FloatAgent = C->createAgent( + LowLevelAgentName1, + // Master-input handler. + FloatMasterHandler([&LowLevelAgentName1](LowLevelReturnFromMaster I) + -> LowLevelAgentMasterResult { + + }), + // Slave-input handler. + FloatHandler( + [&LowLevelAgentName1]( + 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)); + return {ret}; + })); + + //------------------------- lookup copy of rel------------------------------ + //-------------------------------------------------------------------------- + /* ----------------------------- 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); } } delete lowlevel; delete lowlevel2; } \ No newline at end of file diff --git a/examples/agent-functionalities/Reliability-functionality-agent-context/helper.h b/examples/agent-functionalities/Reliability-functionality-agent-context/helper.h new file mode 100644 index 0000000..17bbf7f --- /dev/null +++ b/examples/agent-functionalities/Reliability-functionality-agent-context/helper.h @@ -0,0 +1,87 @@ +#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 + +using namespace rosa::agent; +using namespace rosa; +using namespace rosa::deluxe; +using namespace rosa::terminal; + + +auto create_lowlevel_func() { + 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); + + return lowlevel; +} \ No newline at end of file