diff --git a/examples/agent-functionalities/agent-functionalities.cpp b/examples/agent-functionalities/agent-functionalities.cpp index c1d77a1..0266565 100644 --- a/examples/agent-functionalities/agent-functionalities.cpp +++ b/examples/agent-functionalities/agent-functionalities.cpp @@ -1,183 +1,190 @@ //===-- examples/agent-functionalities/agent-functionalities.cpp *-- C++-*-===// // // The RoSA Framework // //===----------------------------------------------------------------------===// /// /// \file examples/agent-functionalities/agent-functionalities.cpp /// /// \author David Juhasz (david.juhasz@tuwien.ac.at) /// /// \date 2017 /// /// \brief A simple example on defining \c rosa::Agent instances using /// \c rosa::agent::Functionality object as components. /// //===----------------------------------------------------------------------===// +// Make sure M_PI is available, needed for _WIN32 +#define _USE_MATH_DEFINES +#include + #include "rosa/agent/Abstraction.hpp" #include "rosa/agent/FunctionAbstractions.hpp" #include "rosa/agent/RangeConfidence.hpp" #include "rosa/agent/Confidence.hpp" #include "rosa/config/version.h" #include "rosa/core/Agent.hpp" #include "rosa/core/MessagingSystem.hpp" #include "rosa/support/log.h" #include "rosa/support/terminal_colors.h" #include using namespace rosa; using namespace rosa::agent; using namespace rosa::terminal; +// We use pi as float rather than double, which M_PI is. +constexpr float Pi = (float) M_PI; + /// A dummy wrapper for testing \c rosa::MessagingSystem. /// /// \note Since we test \c rosa::MessagingSystem directly here, we need to get /// access to its protected members. That we do by imitating to be a decent /// subclass of \c rosa::MessagingSystem, while calling protected member /// functions on an object of a type from which we actually don't inherit. struct SystemTester : protected MessagingSystem { template static AgentHandle createMyAgent(MessagingSystem *S, const std::string &Name, Funs &&... Fs) { return ((SystemTester *)S)->createAgent(Name, std::move(Fs)...); } static void destroyMyAgent(MessagingSystem *S, const AgentHandle &H) { ((SystemTester *)S)->destroyUnit(unwrapAgent(H)); } }; /// A special \c rosa::Agent with its own state. class MyAgent : public Agent { public: using Tick = AtomConstant; private: enum class Categories { Bad, Normal, Good }; static const std::map CategoryNames; History H; Confidence C; RangeAbstraction A; PartialFunction L; RangeConfidence RCL; RangeConfidence RCS; public: void handler(Tick, uint8_t V) noexcept { // Record \p V to the \c rosa::agent::History, then print state info. H << V; ASSERT(H.entry() == V); // Sanity check. LOG_INFO_STREAM << "\nNext value: " << PRINTABLE(V) << ", confidence: " << C(H) << ", category: " << CategoryNames.at(A(H.entry())) << ", partial: " << int(L(H.entry())) << ", range-confidence-linear: "; std::map res_lin = RCL(H.entry()); for (auto i : res_lin){ LOG_INFO_STREAM << " " << CategoryNames.at(i.first) << " " << i.second << "," ; } LOG_INFO_STREAM << " range-confidence-sine: "; std::map res_sine = RCS(H.entry()); for (auto i : res_sine){ LOG_INFO_STREAM << " " << CategoryNames.at(i.first) << " " << i.second << "," ; } LOG_INFO_STREAM << '\n'; } MyAgent(const AtomValue Kind, const rosa::id_t Id, const std::string &Name, MessagingSystem &S) : Agent(Kind, Id, Name, S, THISMEMBER(handler)), H(), C(5, 20, 1), A({{{(uint8_t)10, (uint8_t)14}, Categories::Normal}, {{(uint8_t)15, (uint8_t)17}, Categories::Good}, {{(uint8_t)18, (uint8_t)19}, Categories::Normal}}, Categories::Bad), L({{{0, 2}, std::make_shared>(0, 1)}, {{2, 4}, std::make_shared>(2, 0)}, {{4, 6}, std::make_shared>(6, -1)}}, 0), RCL({ {Categories::Bad, 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.f, 3.f}, std::make_shared> + (0.f, 1.f/3)}, + {{3.f, 6.f}, std::make_shared> + (1.f, 0.f)}, + {{6.f, 9.f}, std::make_shared> + (3.f, -1.f/3)}, },0)}, {Categories::Normal, 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)}, + {{6.f, 9.f}, std::make_shared> + (-2.f, 1.f/3)}, + {{9.f, 12.f}, std::make_shared> + (1.f, 0.f)}, + {{12.f, 15.f}, std::make_shared> + (5.f, -1.f/3)}, },0)}, {Categories::Good, 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)}, + {{12.f, 15.f}, std::make_shared> + (-4.f, 1.f/3)}, + {{15.f, 18.f}, std::make_shared> + (1.f, 0.f)}, + {{18.f, 21.f}, std::make_shared> + (7.f, -1.f/3)}, },0)} }), RCS({ {Categories::Bad, PartialFunction({ - {{0, 3}, std::make_shared> - (M_PI/3, 0.5, -M_PI/2, 0.5)}, - {{3, 6}, std::make_shared>(1, 0)}, - {{6, 9}, std::make_shared> - (M_PI/3, 0.5, -M_PI/2 + 3, 0.5)}, + {{0.f, 3.f}, std::make_shared> + (Pi/3, 0.5f, -Pi/2, 0.5f)}, + {{3.f, 6.f}, std::make_shared>(1.f, 0.f)}, + {{6.f, 9.f}, std::make_shared> + (Pi/3, 0.5f, -Pi/2 + 3, 0.5f)}, },0)}, {Categories::Normal, PartialFunction({ - {{6, 9}, std::make_shared> - (M_PI/3, 0.5, -M_PI/2, 0.5)}, - {{9, 12}, std::make_shared>(1, 0)}, - {{12, 15}, std::make_shared> - (M_PI/3, 0.5, -M_PI/2 + 3, 0.5)}, + {{6.f, 9.f}, std::make_shared> + (Pi/3, 0.5f, -Pi/2, 0.5f)}, + {{9.f, 12.f}, std::make_shared>(1.f, 0.f)}, + {{12.f, 15.f}, std::make_shared> + (Pi/3, 0.5f, -Pi/2 + 3, 0.5f)}, },0)}, {Categories::Good, PartialFunction({ - {{12, 15}, std::make_shared> - (M_PI/3, 0.5, -M_PI/2, 0.5)}, - {{15, 18}, std::make_shared>(1, 0)}, - {{18, 21}, std::make_shared> - (M_PI/3, 0.5, -M_PI/2 + 3, 0.5)}, + {{12.f, 15.f}, std::make_shared> + (Pi/3, 0.5f, -Pi/2, 0.5f)}, + {{15.f, 18.f}, std::make_shared>(1.f, 0.f)}, + {{18.f, 21.f}, std::make_shared> + (Pi/3, 0.5f, -Pi/2 + 3, 0.5f)}, },0)} }, true){} }; const std::map MyAgent::CategoryNames{ {Categories::Bad, "Bad"}, {Categories::Normal, "Normal"}, {Categories::Good, "Good"}}; int main(void) { LOG_INFO_STREAM << library_string() << " -- " << Color::Red << "agent-functionalities example" << Color::Default << '\n'; std::unique_ptr S = MessagingSystem::createSystem("Sys"); MessagingSystem *SP = S.get(); AgentHandle A = SystemTester::createMyAgent(SP, "MyAgent"); std::vector Vs{0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 15, 14, 15, 16, 19, 20, 21}; for (auto I = Vs.begin(); I != Vs.end(); ++I) { A.send(MyAgent::Tick::Value, *I); } SystemTester::destroyMyAgent(SP, A); return 0; }