From b04c5fcc5a393504c83bb1a4f06e2e81a2a288bd Mon Sep 17 00:00:00 2001 From: Amrita Goswami Date: Sat, 23 Mar 2024 13:48:30 +0000 Subject: [PATCH] InertialModel: Created InertialAgent The InertialAgent has a velocity also. Co-authored-by: Moritz Sallermann --- include/agents/inertial_agent.hpp | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 include/agents/inertial_agent.hpp diff --git a/include/agents/inertial_agent.hpp b/include/agents/inertial_agent.hpp new file mode 100644 index 0000000..469376a --- /dev/null +++ b/include/agents/inertial_agent.hpp @@ -0,0 +1,68 @@ +#pragma once + +#include "agent.hpp" +#include "agent_io.hpp" +#include + +namespace Seldon +{ + +struct InertialAgentData +{ + double opinion = 0; // x_i + double activity = 0; // alpha_i + double reluctance = 1.0; // m_i + double velocity = 0.0; // d(x_i)/dt +}; + +using InertialAgent = Agent; + +template<> +inline std::string agent_to_string( const InertialAgent & agent ) +{ + return fmt::format( + "{}, {}, {}, {}", agent.data.opinion, agent.data.velocity, agent.data.activity, agent.data.reluctance ); +} + +template<> +inline std::string opinion_to_string( const InertialAgent & agent ) +{ + return fmt::format( "{}", agent.data.opinion ); +} + +template<> +inline InertialAgent agent_from_string( const std::string & str ) +{ + InertialAgent res{}; + + auto callback = [&]( int idx_list, std::string & substr ) + { + if( idx_list == 0 ) + { + res.data.opinion = std::stod( substr ); + } + else if( idx_list == 1 ) + { + res.data.velocity = std::stod( substr ); + } + else if( idx_list == 2 ) + { + res.data.activity = std::stod( substr ); + } + else if( idx_list == 3 ) + { + res.data.reluctance = std::stod( substr ); + } + }; + + Seldon::parse_comma_separated_list( str, callback ); + + return res; +}; + +template<> +inline std::vector agent_to_string_column_names() +{ + return { "opinion", "velocity", "activity", "reluctance" }; +} +} // namespace Seldon \ No newline at end of file