Skip to content

Commit

Permalink
DeGroot/Deffuant: Construct from settings
Browse files Browse the repository at this point in the history
Easier and clearer. Also more private members.

Co-authored-by: Amrita Goswami <[email protected]>
  • Loading branch information
MSallermann and amritagos committed Mar 22, 2024
1 parent b822f8e commit 2ab75f9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 30 deletions.
3 changes: 1 addition & 2 deletions include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class Model
public:
using AgentT = AgentT_;

std::optional<size_t> max_iterations = std::nullopt;

Model() = default;
Model( std::optional<size_t> max_iterations ) : max_iterations( max_iterations ){};

Expand Down Expand Up @@ -48,6 +46,7 @@ class Model
virtual ~Model() = default;

private:
std::optional<size_t> max_iterations = std::nullopt;
size_t _n_iterations{};
};

Expand Down
9 changes: 5 additions & 4 deletions include/models/DeGroot.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "agents/simple_agent.hpp"
#include "config_parser.hpp"
#include "model.hpp"
#include "network.hpp"
#include <optional>
Expand All @@ -11,16 +12,16 @@ namespace Seldon
class DeGrootModel : public Model<SimpleAgent>
{
public:
using AgentT = SimpleAgent;
using NetworkT = Network<AgentT>;
double convergence_tol = 1e-12;
using AgentT = SimpleAgent;
using NetworkT = Network<AgentT>;

DeGrootModel( NetworkT & network );
DeGrootModel( Config::DeGrootSettings settings, NetworkT & network );

void iteration() override;
bool finished() override;

private:
double convergence_tol{};
std::optional<double> max_opinion_diff = std::nullopt;
NetworkT & network;
std::vector<AgentT> agents_current_copy;
Expand Down
21 changes: 12 additions & 9 deletions include/models/DeffuantModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

#include "agents/discrete_vector_agent.hpp"
#include "agents/simple_agent.hpp"
#include "config_parser.hpp"
#include "model.hpp"
#include "network.hpp"
#include "util/math.hpp"
#include <cstddef>
#include <random>

#include "network_generation.hpp"
#include <set>
#include <string>
#include <utility>

#include <vector>

Expand All @@ -25,12 +23,14 @@ class DeffuantModelAbstract : public Model<AgentT_>
using AgentT = AgentT_;
using NetworkT = Network<AgentT>;

double homophily_threshold = 0.2; // d in paper
double mu = 0.5; // convergence parameter
bool use_network = false; // for the basic Deffuant model

DeffuantModelAbstract( NetworkT & network, std::mt19937 & gen, bool use_network )
: Model<AgentT>(), use_network( use_network ), network( network ), gen( gen )
DeffuantModelAbstract(
const Config::DeffuantSettings & settings, NetworkT & network, std::mt19937 & gen, bool use_network )
: Model<AgentT>( settings.max_iterations ),
homophily_threshold( settings.homophily_threshold ),
mu( settings.mu ),
use_network( settings.use_network ),
network( network ),
gen( gen )
{
// Generate the network as a square lattice if use_network is true
if( use_network )
Expand Down Expand Up @@ -103,6 +103,9 @@ class DeffuantModelAbstract : public Model<AgentT_>
// bool finished() override;

private:
double homophily_threshold{}; // d in paper
double mu{}; // convergence parameter
bool use_network{}; // for the basic Deffuant model
NetworkT & network;
std::mt19937 & gen; // reference to simulation Mersenne-Twister engine
};
Expand Down
10 changes: 3 additions & 7 deletions include/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ class Simulation : public SimulationInterface
// DeGroot specific parameters
model = [&]()
{
auto model = std::make_unique<DeGrootModel>( network );
model->max_iterations = degroot_settings.max_iterations;
model->convergence_tol = degroot_settings.convergence_tol;
auto model = std::make_unique<DeGrootModel>( degroot_settings, network );
return model;
}();

Expand All @@ -88,10 +86,8 @@ class Simulation : public SimulationInterface
// Deffuant model specific parameters
model = [&]()
{
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;
auto model = std::make_unique<DeffuantModel>(
deffuant_settings, network, gen, deffuant_settings.use_network );
return model;
}();

Expand Down
3 changes: 2 additions & 1 deletion src/models/ActivityDrivenModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ void ActivityDrivenModel::update_network_mean()
contact_prob_list[idx_agent] = weights; // set to zero
}

auto probability_helper = []( double omega, size_t m ) {
auto probability_helper = []( double omega, size_t m )
{
double p = 0;
for( size_t i = 1; i <= m; i++ )
p += ( std::pow( -omega, i + 1 ) + omega ) / ( omega + 1 );
Expand Down
8 changes: 6 additions & 2 deletions src/models/DeGroot.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include "models/DeGroot.hpp"
#include "config_parser.hpp"
#include <cmath>
#include <iterator>

namespace Seldon
{

DeGrootModel::DeGrootModel( NetworkT & network )
: Model<AgentT>(), network( network ), agents_current_copy( network.agents )
DeGrootModel::DeGrootModel( Config::DeGrootSettings settings, NetworkT & network )
: Model<AgentT>( settings.max_iterations ),
convergence_tol( settings.convergence_tol ),
network( network ),
agents_current_copy( network.agents )
{
// For a strongly connected network, the number of SCCs should be 1
// Print a warning if this is not true
Expand Down
15 changes: 10 additions & 5 deletions test/test_deGroot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ TEST_CASE( "Test the DeGroot Model Symmetric", "[DeGroot]" )
{ 0.2, 0.8 },
};

auto settings = Config::DeGrootSettings();

auto network = Network( std::move( neighbour_list ), std::move( weight_list ), Network::EdgeDirection::Incoming );
auto model = DeGrootModel( network );

model.convergence_tol = 1e-6;
model.max_iterations = 100;
settings.convergence_tol = 1e-6;
settings.max_iterations = 100;

auto model = DeGrootModel( settings, network );

network.agents[0].data.opinion = 0.0;
network.agents[1].data.opinion = 1.0;

Expand All @@ -36,10 +40,11 @@ TEST_CASE( "Test the DeGroot Model Symmetric", "[DeGroot]" )
model.iteration();
}

INFO( fmt::format( "N_iterations = {} (with convergence_tol {})\n", model.n_iterations(), model.convergence_tol ) );
INFO( fmt::format(
"N_iterations = {} (with convergence_tol {})\n", model.n_iterations(), settings.convergence_tol ) );
for( size_t i = 0; i < n_agents; i++ )
{
INFO( fmt::format( "Opinion {} = {}\n", i, network.agents[i].data.opinion ) );
REQUIRE_THAT( network.agents[i].data.opinion, WithinAbs( 0.5, model.convergence_tol * 10.0 ) );
REQUIRE_THAT( network.agents[i].data.opinion, WithinAbs( 0.5, settings.convergence_tol * 10.0 ) );
}
}

0 comments on commit 2ab75f9

Please sign in to comment.