From beb47129a7a300766f5e2cf0f67efa91cb919b16 Mon Sep 17 00:00:00 2001 From: Moritz Sallermann Date: Wed, 27 Mar 2024 20:54:30 +0000 Subject: [PATCH] Network: Rename Network to DirectedNetwork Since, we have started to introduce an undirected network as well, this just makes sense. Later we can have a base class that is just called Network. Co-authored-by: Amrita Goswami --- include/agent_io.hpp | 4 +- include/{network.hpp => directed_network.hpp} | 24 ++++---- include/model_factory.hpp | 17 +++--- include/models/ActivityDrivenModel.hpp | 20 +++---- include/models/DeGroot.hpp | 10 ++-- include/models/DeffuantModel.hpp | 12 ++-- include/models/InertialModel.hpp | 11 ++-- include/network_generation.hpp | 56 ++++++++++--------- include/network_io.hpp | 6 +- include/simulation.hpp | 9 +-- include/undirected_network.hpp | 42 +++++++------- meson.build | 2 +- src/models/ActivityDrivenModel.cpp | 2 +- src/models/DeGroot.cpp | 2 +- src/models/InertialModel.cpp | 4 +- test/test_deGroot.cpp | 7 ++- test/test_io.cpp | 14 ++--- test/test_network.cpp | 33 +++++------ test/test_network_generation.cpp | 8 +-- 19 files changed, 148 insertions(+), 135 deletions(-) rename include/{network.hpp => directed_network.hpp} (93%) diff --git a/include/agent_io.hpp b/include/agent_io.hpp index 98ed526..b9d309a 100644 --- a/include/agent_io.hpp +++ b/include/agent_io.hpp @@ -1,6 +1,6 @@ #pragma once +#include "directed_network.hpp" #include "fstream" -#include "network.hpp" #include "util/misc.hpp" #include #include @@ -36,7 +36,7 @@ template } template -void agents_to_file( const Network & network, const std::string & file_path ) +void agents_to_file( const DirectedNetwork & network, const std::string & file_path ) { std::fstream fs; fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc ); diff --git a/include/network.hpp b/include/directed_network.hpp similarity index 93% rename from include/network.hpp rename to include/directed_network.hpp index 7aeafd0..5dbc2d4 100644 --- a/include/network.hpp +++ b/include/directed_network.hpp @@ -28,7 +28,7 @@ namespace Seldon Note: switch is equivalent to toggle + transpose, but much cheaper! */ template -class Network +class DirectedNetwork { public: enum class EdgeDirection @@ -42,23 +42,23 @@ class Network // @TODO: Make this private later std::vector agents{}; // List of agents of type AgentType - Network() = default; + DirectedNetwork() = default; - Network( size_t n_agents ) + DirectedNetwork( size_t n_agents ) : agents( std::vector( n_agents ) ), neighbour_list( std::vector>( n_agents, std::vector{} ) ), weight_list( std::vector>( n_agents, std::vector{} ) ) { } - Network( std::vector agents ) + DirectedNetwork( std::vector agents ) : agents( agents ), neighbour_list( std::vector>( agents.size(), std::vector{} ) ), weight_list( std::vector>( agents.size(), std::vector{} ) ) { } - Network( + DirectedNetwork( std::vector> && neighbour_list, std::vector> && weight_list, EdgeDirection direction ) : agents( std::vector( neighbour_list.size() ) ), @@ -136,7 +136,7 @@ class Network { if( neighbour_list[agent_idx].size() != weights.size() ) { - throw std::runtime_error( "Network::set_weights: tried to set weights of the wrong size!" ); + throw std::runtime_error( "DirectedNetwork::set_weights: tried to set weights of the wrong size!" ); } weight_list[agent_idx].assign( weights.begin(), weights.end() ); } @@ -144,10 +144,9 @@ class Network /* Sets the neighbour indices */ - void set_edge( - std::size_t agent_idx, std::size_t index_neighbour, std::size_t agent_jdx ) + void set_edge( std::size_t agent_idx, std::size_t index_neighbour, std::size_t agent_jdx ) { - neighbour_list[agent_idx][index_neighbour]=agent_jdx; + neighbour_list[agent_idx][index_neighbour] = agent_jdx; } /* @@ -170,7 +169,7 @@ class Network if( buffer_neighbours.size() != buffer_weights.size() ) { throw std::runtime_error( - "Network::set_neighbours_and_weights: both buffers need to have the same length!" ); + "DirectedNetwork::set_neighbours_and_weights: both buffers need to have the same length!" ); } neighbour_list[agent_idx].assign( buffer_neighbours.begin(), buffer_neighbours.end() ); @@ -180,14 +179,15 @@ class Network /* Sets the weight for agent_idx, for a neighbour index */ - void set_edge_weight(std::size_t agent_idx, std::size_t index_neighbour, WeightT weight){ + void set_edge_weight( std::size_t agent_idx, std::size_t index_neighbour, WeightT weight ) + { weight_list[agent_idx][index_neighbour] = weight; } /* Gets the weight for agent_idx, for a neighbour index */ - const WeightT get_edge_weight(std::size_t agent_idx, std::size_t index_neighbour) const + const WeightT get_edge_weight( std::size_t agent_idx, std::size_t index_neighbour ) const { return weight_list[agent_idx][index_neighbour]; } diff --git a/include/model_factory.hpp b/include/model_factory.hpp index a87f9dd..6fa7236 100644 --- a/include/model_factory.hpp +++ b/include/model_factory.hpp @@ -1,10 +1,10 @@ #include "config_parser.hpp" +#include "directed_network.hpp" #include "model.hpp" #include "models/ActivityDrivenModel.hpp" #include "models/DeGroot.hpp" #include "models/DeffuantModel.hpp" #include "models/InertialModel.hpp" -#include "network.hpp" #include #include #include @@ -30,7 +30,7 @@ auto check_agent_type( FuncT func ) } template -inline auto create_model_degroot( Network & network, const ModelVariantT & model_settings ) +inline auto create_model_degroot( DirectedNetwork & network, const ModelVariantT & model_settings ) { if constexpr( std::is_same_v ) { @@ -46,8 +46,8 @@ inline auto create_model_degroot( Network & network, const ModelVariantT } template -inline auto -create_model_activity_driven( Network & network, const ModelVariantT & model_settings, std::mt19937 & gen ) +inline auto create_model_activity_driven( + DirectedNetwork & network, const ModelVariantT & model_settings, std::mt19937 & gen ) { if constexpr( std::is_same_v ) { @@ -64,7 +64,7 @@ create_model_activity_driven( Network & network, const ModelVariantT & m template inline auto create_model_activity_driven_inertial( - Network & network, const ModelVariantT & model_settings, std::mt19937 & gen ) + DirectedNetwork & network, const ModelVariantT & model_settings, std::mt19937 & gen ) { if constexpr( std::is_same_v ) { @@ -80,7 +80,8 @@ inline auto create_model_activity_driven_inertial( } template -inline auto create_model_deffuant( Network & network, const ModelVariantT & model_settings, std::mt19937 & gen ) +inline auto +create_model_deffuant( DirectedNetwork & network, const ModelVariantT & model_settings, std::mt19937 & gen ) { if constexpr( std::is_same_v ) { @@ -97,8 +98,8 @@ inline auto create_model_deffuant( Network & network, const ModelVariant } template -inline auto -create_model_deffuant_vector( Network & network, const ModelVariantT & model_settings, std::mt19937 & gen ) +inline auto create_model_deffuant_vector( + DirectedNetwork & network, const ModelVariantT & model_settings, std::mt19937 & gen ) { if constexpr( std::is_same_v ) { diff --git a/include/models/ActivityDrivenModel.hpp b/include/models/ActivityDrivenModel.hpp index 9a1f6dd..fba3c2c 100644 --- a/include/models/ActivityDrivenModel.hpp +++ b/include/models/ActivityDrivenModel.hpp @@ -3,8 +3,8 @@ #include "agents/activity_agent.hpp" #include "agents/inertial_agent.hpp" #include "config_parser.hpp" +#include "directed_network.hpp" #include "model.hpp" -#include "network.hpp" #include "network_generation.hpp" #include #include @@ -20,12 +20,12 @@ template class ActivityDrivenModelAbstract : public Model { public: - using AgentT = AgentT_; - using NetworkT = Network; - using WeightT = typename NetworkT::WeightT; + using AgentT = AgentT_; + using DirectedNetworkT = DirectedNetwork; + using WeightT = typename DirectedNetworkT::WeightT; ActivityDrivenModelAbstract( - const Config::ActivityDrivenSettings & settings, NetworkT & network, std::mt19937 & gen ) + const Config::ActivityDrivenSettings & settings, DirectedNetworkT & network, std::mt19937 & gen ) : Model( settings.max_iterations ), network( network ), contact_prob_list( std::vector>( network.n_agents() ) ), @@ -56,7 +56,7 @@ class ActivityDrivenModelAbstract : public Model if( mean_weights ) { auto agents_copy = network.agents; - network = NetworkGeneration::generate_fully_connected( network.n_agents() ); + network = DirectedNetworkGeneration::generate_fully_connected( network.n_agents() ); network.agents = agents_copy; } } @@ -64,7 +64,7 @@ class ActivityDrivenModelAbstract : public Model void iteration() override {}; protected: - NetworkT & network; + DirectedNetworkT & network; private: std::vector> contact_prob_list; // Probability of choosing i in 1 to m rounds @@ -291,9 +291,9 @@ class ActivityDrivenModelAbstract : public Model double prob_contact_ji = contact_prob_list[j][idx_agent]; // Set the incoming agent weight, j-i in weight list - double win_ji = network.get_edge_weight( j ,idx_agent); + double win_ji = network.get_edge_weight( j, idx_agent ); win_ji += prob_contact_ij; - network.set_edge_weight(j, idx_agent, win_ji); + network.set_edge_weight( j, idx_agent, win_ji ); // Handle the reciprocity for j->i // Update incoming weight i-j @@ -301,7 +301,7 @@ class ActivityDrivenModelAbstract : public Model // The probability of reciprocating is win_ij += ( 1.0 - prob_contact_ji ) * reciprocity * prob_contact_ij; - network.set_edge_weight(idx_agent, j, win_ij); + network.set_edge_weight( idx_agent, j, win_ij ); } } } diff --git a/include/models/DeGroot.hpp b/include/models/DeGroot.hpp index f6c49ce..0a2a750 100644 --- a/include/models/DeGroot.hpp +++ b/include/models/DeGroot.hpp @@ -1,8 +1,8 @@ #pragma once #include "agents/simple_agent.hpp" #include "config_parser.hpp" +#include "directed_network.hpp" #include "model.hpp" -#include "network.hpp" #include #include @@ -12,10 +12,10 @@ namespace Seldon class DeGrootModel : public Model { public: - using AgentT = SimpleAgent; - using NetworkT = Network; + using AgentT = SimpleAgent; + using DirectedNetworkT = DirectedNetwork; - DeGrootModel( Config::DeGrootSettings settings, NetworkT & network ); + DeGrootModel( Config::DeGrootSettings settings, DirectedNetworkT & network ); void iteration() override; bool finished() override; @@ -23,7 +23,7 @@ class DeGrootModel : public Model private: double convergence_tol{}; std::optional max_opinion_diff = std::nullopt; - NetworkT & network; + DirectedNetworkT & network; std::vector agents_current_copy; }; diff --git a/include/models/DeffuantModel.hpp b/include/models/DeffuantModel.hpp index 7f90baf..0b94ab7 100644 --- a/include/models/DeffuantModel.hpp +++ b/include/models/DeffuantModel.hpp @@ -3,8 +3,8 @@ #include "agents/discrete_vector_agent.hpp" #include "agents/simple_agent.hpp" #include "config_parser.hpp" +#include "directed_network.hpp" #include "model.hpp" -#include "network.hpp" #include "util/math.hpp" #include #include @@ -20,10 +20,10 @@ template class DeffuantModelAbstract : public Model { public: - using AgentT = AgentT_; - using NetworkT = Network; + using AgentT = AgentT_; + using DirectedNetworkT = DirectedNetwork; - DeffuantModelAbstract( const Config::DeffuantSettings & settings, NetworkT & network, std::mt19937 & gen ) + DeffuantModelAbstract( const Config::DeffuantSettings & settings, DirectedNetworkT & network, std::mt19937 & gen ) : Model( settings.max_iterations ), homophily_threshold( settings.homophily_threshold ), mu( settings.mu ), @@ -39,7 +39,7 @@ class DeffuantModelAbstract : public Model { throw std::runtime_error( "Number of agents is not a square number." ); } - network = NetworkGeneration::generate_square_lattice( n_edge ); + network = DirectedNetworkGeneration::generate_square_lattice( n_edge ); } } @@ -104,7 +104,7 @@ class DeffuantModelAbstract : public Model double homophily_threshold{}; // d in paper double mu{}; // convergence parameter bool use_network{}; // for the basic Deffuant model - NetworkT & network; + DirectedNetworkT & network; std::mt19937 & gen; // reference to simulation Mersenne-Twister engine }; diff --git a/include/models/InertialModel.hpp b/include/models/InertialModel.hpp index ba95a0b..f517982 100644 --- a/include/models/InertialModel.hpp +++ b/include/models/InertialModel.hpp @@ -3,9 +3,9 @@ #include "agents/activity_agent.hpp" #include "agents/inertial_agent.hpp" #include "config_parser.hpp" +#include "directed_network.hpp" #include "model.hpp" #include "models/ActivityDrivenModel.hpp" -#include "network.hpp" #include "network_generation.hpp" #include #include @@ -20,11 +20,12 @@ namespace Seldon class InertialModel : public ActivityDrivenModelAbstract { public: - using AgentT = InertialAgent; - using NetworkT = Network; - using WeightT = typename NetworkT::WeightT; + using AgentT = InertialAgent; + using DirectedNetworkT = DirectedNetwork; + using WeightT = typename DirectedNetworkT::WeightT; - InertialModel( const Config::ActivityDrivenInertialSettings & settings, NetworkT & network, std::mt19937 & gen ); + InertialModel( + const Config::ActivityDrivenInertialSettings & settings, DirectedNetworkT & network, std::mt19937 & gen ); void iteration() override; diff --git a/include/network_generation.hpp b/include/network_generation.hpp index e71b50f..8c84d98 100644 --- a/include/network_generation.hpp +++ b/include/network_generation.hpp @@ -1,22 +1,22 @@ #pragma once -#include "network.hpp" +#include "directed_network.hpp" #include #include #include #include #include -namespace Seldon::NetworkGeneration +namespace Seldon::DirectedNetworkGeneration { /* Constructs a new network with n_connections per agent If self_interaction=true, a connection of the agent with itself is included, which is *not* counted in n_connections */ template -Network +DirectedNetwork generate_n_connections( size_t n_agents, size_t n_connections, bool self_interaction, std::mt19937 & gen ) { - using NetworkT = Network; - using WeightT = typename NetworkT::WeightT; + using DirectedNetworkT = DirectedNetwork; + using WeightT = typename DirectedNetworkT::WeightT; std::vector> neighbour_list; // Neighbour list for the connections std::vector> weight_list; // List for the interaction weights of each connection @@ -70,15 +70,17 @@ generate_n_connections( size_t n_agents, size_t n_connections, bool self_interac } // end of loop through n_agents - return NetworkT( std::move( neighbour_list ), std::move( weight_list ), NetworkT::EdgeDirection::Incoming ); + return DirectedNetworkT( + std::move( neighbour_list ), std::move( weight_list ), DirectedNetworkT::EdgeDirection::Incoming ); } // @TODO generate_fully_connected does not need to be overloaded..perhaps a std::optional instead to reduce code duplication? template -Network generate_fully_connected( size_t n_agents, typename Network::WeightT weight = 0.0 ) +DirectedNetwork +generate_fully_connected( size_t n_agents, typename DirectedNetwork::WeightT weight = 0.0 ) { - using NetworkT = Network; - using WeightT = typename NetworkT::WeightT; + using DirectedNetworkT = DirectedNetwork; + using WeightT = typename DirectedNetworkT::WeightT; std::vector> neighbour_list; // Neighbour list for the connections std::vector> weight_list; // List for the interaction weights of each connection @@ -103,14 +105,15 @@ Network generate_fully_connected( size_t n_agents, typename Network -Network generate_fully_connected( size_t n_agents, std::mt19937 & gen ) +DirectedNetwork generate_fully_connected( size_t n_agents, std::mt19937 & gen ) { - using NetworkT = Network; - using WeightT = typename NetworkT::WeightT; + using DirectedNetworkT = DirectedNetwork; + using WeightT = typename DirectedNetworkT::WeightT; std::vector> neighbour_list; // Neighbour list for the connections std::vector> weight_list; // List for the interaction weights of each connection @@ -154,14 +157,15 @@ Network generate_fully_connected( size_t n_agents, std::mt19937 & gen } // end of loop through n_agents - return NetworkT( std::move( neighbour_list ), std::move( weight_list ), NetworkT::EdgeDirection::Incoming ); + return DirectedNetworkT( + std::move( neighbour_list ), std::move( weight_list ), DirectedNetworkT::EdgeDirection::Incoming ); } template -Network generate_from_file( const std::string & file ) +DirectedNetwork generate_from_file( const std::string & file ) { - using NetworkT = Network; - using WeightT = typename NetworkT::WeightT; + using DirectedNetworkT = DirectedNetwork; + using WeightT = typename DirectedNetworkT::WeightT; std::vector> neighbour_list; // Neighbour list for the connections std::vector> weight_list; // List for the interaction weights of each connection @@ -235,19 +239,21 @@ Network generate_from_file( const std::string & file ) } } - return NetworkT( std::move( neighbour_list ), std::move( weight_list ), NetworkT::EdgeDirection::Incoming ); + return DirectedNetworkT( + std::move( neighbour_list ), std::move( weight_list ), DirectedNetworkT::EdgeDirection::Incoming ); } /* Constructs a new network on a square lattice of edge length n_edge (with PBCs)*/ template -Network generate_square_lattice( size_t n_edge, typename Network::WeightT weight = 0.0 ) +DirectedNetwork +generate_square_lattice( size_t n_edge, typename DirectedNetwork::WeightT weight = 0.0 ) { - using NetworkT = Network; - using WeightT = typename NetworkT::WeightT; - auto n_agents = n_edge * n_edge; + using DirectedNetworkT = DirectedNetwork; + using WeightT = typename DirectedNetworkT::WeightT; + auto n_agents = n_edge * n_edge; - // Create an empty Network - auto network = NetworkT( n_agents ); + // Create an empty DirectedNetwork + auto network = DirectedNetworkT( n_agents ); auto wrap_edge_index = [&]( int k ) { @@ -294,4 +300,4 @@ Network generate_square_lattice( size_t n_edge, typename Network #include #include @@ -10,7 +10,7 @@ namespace Seldon { template -void network_to_dot_file( const Network & network, const std::string & file_path ) +void network_to_dot_file( const DirectedNetwork & network, const std::string & file_path ) { std::fstream fs; fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc ); @@ -36,7 +36,7 @@ void network_to_dot_file( const Network & network, const std::string & f } template -void network_to_file( const Network & network, const std::string & file_path ) +void network_to_file( const DirectedNetwork & network, const std::string & file_path ) { std::fstream fs; fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc ); diff --git a/include/simulation.hpp b/include/simulation.hpp index 118b824..55b116d 100644 --- a/include/simulation.hpp +++ b/include/simulation.hpp @@ -1,9 +1,9 @@ #pragma once #include "config_parser.hpp" +#include "directed_network.hpp" #include "fmt/core.h" #include "model_factory.hpp" -#include "network.hpp" #include #include #include @@ -36,7 +36,7 @@ class Simulation : public SimulationInterface public: std::unique_ptr> model; - Network network; + DirectedNetwork network; Config::OutputSettings output_settings; @@ -50,13 +50,14 @@ class Simulation : public SimulationInterface if( file.has_value() ) { - network = NetworkGeneration::generate_from_file( file.value() ); + network = DirectedNetworkGeneration::generate_from_file( file.value() ); } else { int n_agents = options.network_settings.n_agents; auto n_connections = options.network_settings.n_connections; - network = NetworkGeneration::generate_n_connections( n_agents, n_connections, true, gen ); + network + = DirectedNetworkGeneration::generate_n_connections( n_agents, n_connections, true, gen ); } } diff --git a/include/undirected_network.hpp b/include/undirected_network.hpp index 80c400b..49b0f66 100644 --- a/include/undirected_network.hpp +++ b/include/undirected_network.hpp @@ -19,7 +19,6 @@ template class UndirectedNetwork { public: - using WeightT = WeightType; using AgentT = AgentType; // @TODO: Make this private later @@ -42,7 +41,7 @@ class UndirectedNetwork } UndirectedNetwork( - std::vector> && neighbour_list, std::vector> && weight_list) + std::vector> && neighbour_list, std::vector> && weight_list ) : agents( std::vector( neighbour_list.size() ) ), neighbour_list( neighbour_list ), weight_list( weight_list ) @@ -69,10 +68,11 @@ class UndirectedNetwork } else { - // Return the number of edges in the undirected graph, which is half effectively half the edges - return 0.5*std::transform_reduce( - neighbour_list.cbegin(), neighbour_list.cend(), 0, std::plus{}, - []( const auto & neigh_list ) { return neigh_list.size(); } ); + // Return the number of edges in the undirected graph, which is half effectively half the edges + return 0.5 + * std::transform_reduce( + neighbour_list.cbegin(), neighbour_list.cend(), 0, std::plus{}, + []( const auto & neigh_list ) { return neigh_list.size(); } ); } } @@ -107,25 +107,28 @@ class UndirectedNetwork /* Sets the weight for agent_idx, for an existing neighbour index */ - void set_edge_weight(std::size_t agent_idx, std::size_t index_neighbour, WeightT weight){ + void set_edge_weight( std::size_t agent_idx, std::size_t index_neighbour, WeightT weight ) + { weight_list[agent_idx][index_neighbour] = weight; - auto agent_jdx = neighbour_list[agent_idx][index_neighbour]; - auto it = std::find(neighbour_list[agent_jdx].begin(), neighbour_list[agent_jdx].end(), agent_idx); - // If agent_idx is not in the neighbour list of agent_jdx, add it to the list and add the weight - if(it==neighbour_list[agent_jdx].end()){ - neighbour_list[agent_jdx].push_back(agent_idx); - weight_list[agent_jdx].push_back(weight); - } - // If found then update the weight - else{ - weight_list[agent_jdx][*it]= weight; + auto agent_jdx = neighbour_list[agent_idx][index_neighbour]; + auto it = std::find( neighbour_list[agent_jdx].begin(), neighbour_list[agent_jdx].end(), agent_idx ); + // If agent_idx is not in the neighbour list of agent_jdx, add it to the list and add the weight + if( it == neighbour_list[agent_jdx].end() ) + { + neighbour_list[agent_jdx].push_back( agent_idx ); + weight_list[agent_jdx].push_back( weight ); + } + // If found then update the weight + else + { + weight_list[agent_jdx][*it] = weight; } } /* Gets the weight for agent_idx, for a neighbour index */ - const WeightT get_edge_weight(std::size_t agent_idx, std::size_t index_neighbour) const + const WeightT get_edge_weight( std::size_t agent_idx, std::size_t index_neighbour ) const { return weight_list[agent_idx][index_neighbour]; } @@ -135,7 +138,7 @@ class UndirectedNetwork This could cause double counting, if not carefully called. */ void push_back_neighbour_and_weight( size_t agent_idx_i, size_t agent_idx_j, WeightT w ) - { + { // agent_idx_j is a neighbour of agent_idx_i neighbour_list[agent_idx_i].push_back( agent_idx_j ); weight_list[agent_idx_i].push_back( w ); @@ -144,7 +147,6 @@ class UndirectedNetwork weight_list[agent_idx_j].push_back( w ); } - /* Sorts the neighbours by index and removes doubly counted edges by summing the weights */ diff --git a/meson.build b/meson.build index c9fd40d..6200fb2 100644 --- a/meson.build +++ b/meson.build @@ -29,7 +29,7 @@ tests = [ ['Test_Activity_Driven', 'test/test_activity.cpp'], ['Test_Activity_Driven_Inertial', 'test/test_activity_inertial.cpp'], ['Test_Deffuant', 'test/test_deffuant.cpp'], - ['Test_Network', 'test/test_network.cpp'], + ['Test_DirectedNetwork', 'test/test_network.cpp'], ['Test_Network_Generation', 'test/test_network_generation.cpp'], ['Test_Sampling', 'test/test_sampling.cpp'], ['Test_IO', 'test/test_io.cpp'], diff --git a/src/models/ActivityDrivenModel.cpp b/src/models/ActivityDrivenModel.cpp index 0b0dd9c..b46b715 100644 --- a/src/models/ActivityDrivenModel.cpp +++ b/src/models/ActivityDrivenModel.cpp @@ -1,5 +1,5 @@ #include "models/ActivityDrivenModel.hpp" -#include "network.hpp" +#include "directed_network.hpp" #include "util/math.hpp" #include #include diff --git a/src/models/DeGroot.cpp b/src/models/DeGroot.cpp index 1373f42..33c960c 100644 --- a/src/models/DeGroot.cpp +++ b/src/models/DeGroot.cpp @@ -6,7 +6,7 @@ namespace Seldon { -DeGrootModel::DeGrootModel( Config::DeGrootSettings settings, NetworkT & network ) +DeGrootModel::DeGrootModel( Config::DeGrootSettings settings, DirectedNetworkT & network ) : Model( settings.max_iterations ), convergence_tol( settings.convergence_tol ), network( network ), diff --git a/src/models/InertialModel.cpp b/src/models/InertialModel.cpp index df46cc1..884016a 100644 --- a/src/models/InertialModel.cpp +++ b/src/models/InertialModel.cpp @@ -1,6 +1,6 @@ #include "models/InertialModel.hpp" #include "agents/inertial_agent.hpp" -#include "network.hpp" +#include "directed_network.hpp" #include "util/math.hpp" #include #include @@ -10,7 +10,7 @@ namespace Seldon { InertialModel::InertialModel( - const Config::ActivityDrivenInertialSettings & settings, NetworkT & network, std::mt19937 & gen ) + const Config::ActivityDrivenInertialSettings & settings, DirectedNetworkT & network, std::mt19937 & gen ) : ActivityDrivenModelAbstract( settings, network, gen ), friction_coefficient( settings.friction_coefficient ) { diff --git a/test/test_deGroot.cpp b/test/test_deGroot.cpp index 862c9a4..3a59014 100644 --- a/test/test_deGroot.cpp +++ b/test/test_deGroot.cpp @@ -2,15 +2,15 @@ #include #include "config_parser.hpp" +#include "directed_network.hpp" #include "models/DeGroot.hpp" -#include "network.hpp" #include TEST_CASE( "Test the DeGroot Model Symmetric", "[DeGroot]" ) { using namespace Seldon; using namespace Catch::Matchers; - using Network = Network; + using DirectedNetwork = DirectedNetwork; size_t n_agents = 2; auto neighbour_list = std::vector>{ @@ -25,7 +25,8 @@ TEST_CASE( "Test the DeGroot Model Symmetric", "[DeGroot]" ) auto settings = Config::DeGrootSettings(); - auto network = Network( std::move( neighbour_list ), std::move( weight_list ), Network::EdgeDirection::Incoming ); + auto network = DirectedNetwork( + std::move( neighbour_list ), std::move( weight_list ), DirectedNetwork::EdgeDirection::Incoming ); settings.convergence_tol = 1e-6; settings.max_iterations = 100; diff --git a/test/test_io.cpp b/test/test_io.cpp index f49ab58..4bad353 100644 --- a/test/test_io.cpp +++ b/test/test_io.cpp @@ -1,14 +1,14 @@ #include "agent_generation.hpp" #include "catch2/matchers/catch_matchers.hpp" +#include "directed_network.hpp" #include "models/ActivityDrivenModel.hpp" -#include "network.hpp" #include "network_generation.hpp" #include #include #include -#include +#include #include "directed_network.hpp" #include #include namespace fs = std::filesystem; @@ -17,18 +17,18 @@ TEST_CASE( "Test reading in the network from a file", "[io_network]" ) { using namespace Seldon; using namespace Catch::Matchers; - using AgentT = ActivityDrivenModel::AgentT; - using Network = Network; + using AgentT = ActivityDrivenModel::AgentT; + using DirectedNetwork = DirectedNetwork; auto proj_root_path = fs::current_path(); auto network_file = proj_root_path / fs::path( "test/res/network.txt" ); - auto network = Seldon::NetworkGeneration::generate_from_file( network_file ); + auto network = Seldon::DirectedNetworkGeneration::generate_from_file( network_file ); REQUIRE( network.n_agents() == 3 ); - std::vector> neighbours_expected = { { 2, 1 }, {}, { 1 } }; - std::vector> weights_expected = { { 0.1, -0.2 }, {}, { 1.2 } }; + std::vector> neighbours_expected = { { 2, 1 }, {}, { 1 } }; + std::vector> weights_expected = { { 0.1, -0.2 }, {}, { 1.2 } }; for( size_t i = 0; i < network.n_agents(); i++ ) { diff --git a/test/test_network.cpp b/test/test_network.cpp index 218060c..db133a9 100644 --- a/test/test_network.cpp +++ b/test/test_network.cpp @@ -1,4 +1,4 @@ -#include "network.hpp" +#include "directed_network.hpp" #include "network_generation.hpp" #include #include @@ -9,13 +9,13 @@ TEST_CASE( "Testing the network class" ) { using namespace Seldon; - using Network = Network; + using DirectedNetwork = DirectedNetwork; // Generate some network const size_t n_agents = 20; const size_t n_connections = 10; std::mt19937 gen( 0 ); - auto network = NetworkGeneration::generate_n_connections( n_agents, n_connections, false, gen ); + auto network = DirectedNetworkGeneration::generate_n_connections( n_agents, n_connections, false, gen ); // Does n_agents work? REQUIRE( network.n_agents() == n_agents ); @@ -24,8 +24,8 @@ TEST_CASE( "Testing the network class" ) // Check that the function for setting neighbours and a single weight work // Agent 3 - std::vector neigh{ { 0, 10 } }; // new neighbours - std::vector weight{ 0.5, 0.5 }; // new weights (const) + std::vector neigh{ { 0, 10 } }; // new neighbours + std::vector weight{ 0.5, 0.5 }; // new weights (const) network.set_neighbours_and_weights( 3, neigh, 0.5 ); auto buffer_w_get = network.get_weights( 3 ); @@ -44,21 +44,21 @@ TEST_CASE( "Testing the network class" ) REQUIRE( n == neigh[0] ); n = 2; // Set the neighbour - network.set_edge(3, 0, n ); + network.set_edge( 3, 0, n ); REQUIRE( network.get_neighbours( 3 )[0] == 2 ); - Network::WeightT w = network.get_weights( 3 )[1]; + DirectedNetwork::WeightT w = network.get_weights( 3 )[1]; REQUIRE( w == 0.55 ); w = 0.9; - network.set_edge_weight(3, 1, w); + network.set_edge_weight( 3, 1, w ); REQUIRE( network.get_weights( 3 )[1] == w ); } SECTION( "Checking that set_neighbours_and_weights works with a vector of weights, push_back and transpose" ) { // Change the connections for agent 3 - std::vector buffer_n{ { 0, 10, 15 } }; // new neighbours - std::vector buffer_w{ 0.1, 0.2, 0.3 }; // new weights + std::vector buffer_n{ { 0, 10, 15 } }; // new neighbours + std::vector buffer_w{ 0.1, 0.2, 0.3 }; // new weights network.set_neighbours_and_weights( 3, buffer_n, buffer_w ); // Make sure the changes worked @@ -81,7 +81,7 @@ TEST_CASE( "Testing the network class" ) // Now we test the toggle_incoming_outgoing() function // First record all the old edges as tuples (i,j,w) where this edge goes from j -> i with weight w - std::set> old_edges; + std::set> old_edges; for( size_t i_agent = 0; i_agent < network.n_agents(); i_agent++ ) { auto buffer_n = network.get_neighbours( i_agent ); @@ -91,7 +91,7 @@ TEST_CASE( "Testing the network class" ) { auto neighbour = buffer_n[i_neighbour]; auto weight = buffer_w[i_neighbour]; - std::tuple edge{ i_agent, neighbour, weight }; + std::tuple edge{ i_agent, neighbour, weight }; old_edges.insert( edge ); } } @@ -113,7 +113,7 @@ TEST_CASE( "Testing the network class" ) { auto neighbour = buffer_n[i_neighbour]; auto weight = buffer_w[i_neighbour]; - std::tuple edge{ + std::tuple edge{ neighbour, i_agent, weight }; // Note that i_agent and neighbour are flipped compared to before REQUIRE( old_edges.contains( edge ) ); // can we find the transposed edge? @@ -160,8 +160,9 @@ TEST_CASE( "Testing the network class" ) }; // clang-format on - auto network = Seldon::Network( - std::move( neighbour_list ), std::move( weight_list ), Seldon::Network::EdgeDirection::Incoming ); + auto network = Seldon::DirectedNetwork( + std::move( neighbour_list ), std::move( weight_list ), + Seldon::DirectedNetwork::EdgeDirection::Incoming ); network.remove_double_counting(); @@ -191,7 +192,7 @@ TEST_CASE( "Testing the network class" ) }; // clang-format on - auto network = Seldon::NetworkGeneration::generate_square_lattice( 3 ); + auto network = Seldon::DirectedNetworkGeneration::generate_square_lattice( 3 ); for( size_t i_agent = 0; i_agent < network.n_agents(); i_agent++ ) { diff --git a/test/test_network_generation.cpp b/test/test_network_generation.cpp index 5dc4baf..02ff988 100644 --- a/test/test_network_generation.cpp +++ b/test/test_network_generation.cpp @@ -1,4 +1,4 @@ -#include "network.hpp" +#include "directed_network.hpp" #include "network_generation.hpp" #include #include @@ -9,8 +9,8 @@ TEST_CASE( "Testing the network generation functions" ) { using namespace Seldon; - using Network = Network; - using WeightT = Network::WeightT; + using DirectedNetwork = DirectedNetwork; + using WeightT = DirectedNetwork::WeightT; std::vector buffer_n_get{}; // buffer for getting neighbours std::vector buffer_w_get{}; // buffer for getting the weights @@ -24,7 +24,7 @@ TEST_CASE( "Testing the network generation functions" ) { WeightT weight = 0.25; std::vector weights{ weight, weight, weight }; // Weights to set to - auto network = NetworkGeneration::generate_fully_connected( n_agents, weight ); + auto network = DirectedNetworkGeneration::generate_fully_connected( n_agents, weight ); // Make sure that the network has been generated correctly REQUIRE( network.n_agents() == n_agents ); // There should be n_agents in the new network