From d5baabbeda7775f76d89032a1961c92434e5d770 Mon Sep 17 00:00:00 2001 From: Amrita Goswami Date: Wed, 20 Mar 2024 16:17:59 +0000 Subject: [PATCH] Deffuant: Model with square lattice neighbours Implemented the Deffuant model with a square lattice neighbour list. TODO: Actually parse these options. Co-authored-by: Moritz Sallermann --- include/models/DeffuantModel.hpp | 8 +++++-- src/models/DeffuantModel.cpp | 38 ++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/include/models/DeffuantModel.hpp b/include/models/DeffuantModel.hpp index 2cba82d..3a472db 100644 --- a/include/models/DeffuantModel.hpp +++ b/include/models/DeffuantModel.hpp @@ -19,8 +19,9 @@ class DeffuantModel : public Model using AgentT = SimpleAgent; using NetworkT = Network; - double homophily_threshold = 0.2; // d in paper - double mu = 0.5; // convergence parameter + double homophily_threshold = 0.2; // d in paper + double mu = 0.5; // convergence parameter + bool use_network = false; // for the basic Deffuant model DeffuantModel( NetworkT & network, std::mt19937 & gen ); @@ -30,6 +31,9 @@ class DeffuantModel : public Model private: NetworkT & network; std::mt19937 & gen; // reference to simulation Mersenne-Twister engine + + // Select interacting agents + std::vector select_interacting_agent_pair(); }; } // namespace Seldon \ No newline at end of file diff --git a/src/models/DeffuantModel.cpp b/src/models/DeffuantModel.cpp index c4c6308..a717434 100644 --- a/src/models/DeffuantModel.cpp +++ b/src/models/DeffuantModel.cpp @@ -18,6 +18,39 @@ DeffuantModel::DeffuantModel( NetworkT & network, std::mt19937 & gen ) } } +std::vector DeffuantModel::select_interacting_agent_pair() +{ + + auto interacting_agents = std::vector(); + + // If the basic model is being used, then search from all possible agents + if( !use_network ) + { + + // Pick any two agents to interact, randomly, without repetition + draw_unique_k_from_n( std::nullopt, 2, network.n_agents(), interacting_agents, gen ); + + return interacting_agents; + } + else + { + // First select an agent randomly + auto dist = std::uniform_int_distribution( 0, network.n_agents() - 1 ); + auto agent1_idx = dist( gen ); + interacting_agents.push_back( agent1_idx ); + + // Choose a neighbour randomly from the neighbour list of agent1_idx + auto neighbours = network.get_neighbours( agent1_idx ); + auto n_neighbours = neighbours.size(); + auto dist_n = std::uniform_int_distribution( 0, n_neighbours - 1 ); + auto index_in_neigh = dist_n( gen ); // Index inside neighbours list + auto agent2_idx = neighbours[index_in_neigh]; + interacting_agents.push_back( agent2_idx ); + + return interacting_agents; + } +} + void DeffuantModel::iteration() { Model::iteration(); // Update n_iterations @@ -28,10 +61,7 @@ void DeffuantModel::iteration() for( size_t i = 0; i < network.n_agents(); i++ ) { - auto interacting_agents = std::vector(); - - // Pick any two agents to interact, randomly, without repetition - draw_unique_k_from_n( std::nullopt, 2, network.n_agents(), interacting_agents, gen ); + auto interacting_agents = select_interacting_agent_pair(); auto & agent1 = network.agents[interacting_agents[0]]; auto & agent2 = network.agents[interacting_agents[1]];