Skip to content

Commit

Permalink
Deffuant: neighbours in square lattice
Browse files Browse the repository at this point in the history
We now support an implementation of the Deffuant model on a square
lattice. TODO: unit test!
  • Loading branch information
amritagos committed Mar 20, 2024
1 parent d5baabb commit 23ead10
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/Deffuant/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ max_iterations = 1000 # If not set, max iterations is infinite
[Deffuant]
homophily_threshold = 0.2 # d in the paper; agents interact if difference in opinion is less than this value
mu = 0.5 # convergence parameter; similar to social interaction strength K (0,0.5]
use_network = false # If true, will use a square lattice Will throw if sqrt(n_agents) is not an integer

[network]
number_of_agents = 1000
Expand Down
5 changes: 3 additions & 2 deletions include/config_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ struct DeffuantSettings
{
std::optional<int> max_iterations = std::nullopt;
double homophily_threshold
= 0.2; // d in the paper; agents interact if difference in opinion is less than this value
double mu = 0.5; // convergence parameter; similar to social interaction strength K (0,0.5]
= 0.2; // d in the paper; agents interact if difference in opinion is less than this value
double mu = 0.5; // convergence parameter; similar to social interaction strength K (0,0.5]
bool use_network = false;
};

struct ActivityDrivenSettings
Expand Down
2 changes: 1 addition & 1 deletion include/models/DeffuantModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DeffuantModel : public Model<SimpleAgent>
double mu = 0.5; // convergence parameter
bool use_network = false; // for the basic Deffuant model

DeffuantModel( NetworkT & network, std::mt19937 & gen );
DeffuantModel( NetworkT & network, std::mt19937 & gen, bool use_network );

void iteration() override;
// bool finished() override;
Expand Down
2 changes: 1 addition & 1 deletion include/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Simulation : public SimulationInterface
// Deffuant model specific parameters
model = [&]()
{
auto model = std::make_unique<DeffuantModel>( network, gen );
auto model = std::make_unique<DeffuantModel>( network, gen, deffuant_settings.use_network );
model->max_iterations = deffuant_settings.max_iterations;
model->homophily_threshold = deffuant_settings.homophily_threshold;
model->mu = deffuant_settings.mu;
Expand Down
2 changes: 2 additions & 0 deletions src/config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ SimulationOptions parse_config_file( std::string_view config_file_path )
model_settings.max_iterations = tbl["model"]["max_iterations"].value<int>();
set_if_specified( model_settings.homophily_threshold, tbl[options.model_string]["homophily_threshold"] );
set_if_specified( model_settings.mu, tbl[options.model_string]["mu"] );
set_if_specified( model_settings.use_network, tbl[options.model_string]["use_network"] );
options.model_settings = model_settings;
}
else if( options.model == Model::ActivityDrivenModel )
Expand Down Expand Up @@ -270,6 +271,7 @@ void print_settings( const SimulationOptions & options )
fmt::print( " max_iterations {}\n", model_settings.max_iterations );
fmt::print( " homophily_threshold {}\n", model_settings.homophily_threshold );
fmt::print( " mu {}\n", model_settings.mu );
fmt::print( " use_network {}\n", model_settings.use_network );
}

fmt::print( "[Network]\n" );
Expand Down
18 changes: 16 additions & 2 deletions src/models/DeffuantModel.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#include "models/DeffuantModel.hpp"
#include "network.hpp"
#include "network_generation.hpp"
#include "util/math.hpp"
#include <cmath>
#include <cstddef>
#include <optional>
#include <random>
#include <stdexcept>
#include <vector>

namespace Seldon
{

DeffuantModel::DeffuantModel( NetworkT & network, std::mt19937 & gen )
: Model<DeffuantModel::AgentT>(), network( network ), gen( gen )
DeffuantModel::DeffuantModel( NetworkT & network, std::mt19937 & gen, bool use_network )
: Model<DeffuantModel::AgentT>(), use_network( use_network ), network( network ), gen( gen )
{
// Generate the network as a square lattice if use_network is true
if( use_network )
{
size_t n_edge = std::sqrt( network.n_agents() );
if( n_edge * n_edge != network.n_agents() )
{
throw std::runtime_error( "Number of agents is not a square number." );
}
network = NetworkGeneration::generate_square_lattice<AgentT>( n_edge );
}

for( size_t i = 0; i < network.agents.size(); i++ )
{
network.agents[i].data.opinion = double( i ) / double( network.agents.size() );
Expand Down

0 comments on commit 23ead10

Please sign in to comment.