Skip to content

Commit

Permalink
Deffuant: Model with square lattice neighbours
Browse files Browse the repository at this point in the history
Implemented the Deffuant model with a square lattice neighbour list.
TODO: Actually parse these options.

Co-authored-by: Moritz Sallermann <[email protected]>
  • Loading branch information
amritagos and MSallermann committed Mar 20, 2024
1 parent 82e88d1 commit d5baabb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
8 changes: 6 additions & 2 deletions include/models/DeffuantModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class DeffuantModel : public Model<SimpleAgent>
using AgentT = SimpleAgent;
using NetworkT = Network<AgentT>;

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 );

Expand All @@ -30,6 +31,9 @@ class DeffuantModel : public Model<SimpleAgent>
private:
NetworkT & network;
std::mt19937 & gen; // reference to simulation Mersenne-Twister engine

// Select interacting agents
std::vector<std::size_t> select_interacting_agent_pair();
};

} // namespace Seldon
38 changes: 34 additions & 4 deletions src/models/DeffuantModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ DeffuantModel::DeffuantModel( NetworkT & network, std::mt19937 & gen )
}
}

std::vector<std::size_t> DeffuantModel::select_interacting_agent_pair()
{

auto interacting_agents = std::vector<std::size_t>();

// 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<size_t>( 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<size_t>( 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<AgentT>::iteration(); // Update n_iterations
Expand All @@ -28,10 +61,7 @@ void DeffuantModel::iteration()
for( size_t i = 0; i < network.n_agents(); i++ )
{

auto interacting_agents = std::vector<std::size_t>();

// 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]];
Expand Down

0 comments on commit d5baabb

Please sign in to comment.