Skip to content

Commit

Permalink
Deffuant: Implemented the update rule in the vector model
Browse files Browse the repository at this point in the history
Also hacked in the generation of the initial agents by hardcoding the
dimensionality <--- TODO

Co-authored-by: Amrita Goswami <[email protected]>
  • Loading branch information
MSallermann and amritagos committed Mar 21, 2024
1 parent c19efbc commit 42a1837
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
5 changes: 2 additions & 3 deletions include/agents/discrete_vector_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ inline DiscreteVectorAgent agent_from_string<DiscreteVectorAgent>( const std::st
{
DiscreteVectorAgent res{};

auto callback = [&]( int idx_list [[maybe_unused]], const auto & substring ) {
res.data.opinion.push_back( std::stoi( substring ) );
};
auto callback = [&]( int idx_list [[maybe_unused]], const auto & substring )
{ res.data.opinion.push_back( std::stoi( substring ) ); };

parse_comma_separated_list( str, callback );
return res;
Expand Down
48 changes: 40 additions & 8 deletions src/models/DeffuantModelVector.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "agent.hpp"
#include "agents/discrete_vector_agent.hpp"
#include "models/DeffuantModel.hpp"
#include <cmath>
#include "util/math.hpp"
#include <cstddef>
#include <random>
#include <vector>

namespace Seldon
Expand All @@ -10,23 +12,53 @@ namespace Seldon
template<>
void DeffuantModelAbstract<DiscreteVectorAgent>::initialize_agents()
{
std::uniform_int_distribution<int> dist( 0, 1 );
int dim = 5;

for( size_t i = 0; i < network.agents.size(); i++ )
{
// network.agents[i].data.opinion = double( i ) / double( network.agents.size() );
auto & opinion = network.agents[i].data.opinion;
opinion.resize( dim );
for( auto & o : opinion )
{
o = dist( gen );
}
}
}

template<>
void DeffuantModelAbstract<DiscreteVectorAgent>::update_rule( AgentT & agent1, AgentT & agent2 )
{
size_t dim = agent1.data.opinion.size();
auto & opinion1 = agent1.data.opinion;
auto & opinion2 = agent2.data.opinion;

auto distance = hamming_distance( std::span( opinion1 ), std::span( opinion2 ) );

std::uniform_int_distribution<> dist_pair( 0, 1 );
std::uniform_real_distribution<> dist_convince( 0, 1 );

// Update rule
// auto opinion_diff = agent1.data.opinion - agent2.data.opinion;
// Only if less than homophily_threshold
// if( std::abs( opinion_diff ) < homophily_threshold )
// {
// agent1.data.opinion -= mu * opinion_diff;
// agent2.data.opinion += mu * opinion_diff;
// }
if( distance < homophily_threshold )
{
for( size_t idx_opinion = 0; idx_opinion < dim; idx_opinion++ )
{
if( opinion1[idx_opinion] != opinion2[idx_opinion] )
{
// randomly select one of the
auto idx_selected = dist_pair( gen );
if( idx_selected == 0 && mu < dist_convince( gen ) )
{
opinion1[idx_opinion] = opinion2[idx_opinion];
}
else
{
opinion2[idx_opinion] = opinion1[idx_opinion];
}
}
}
}
}

template class DeffuantModelAbstract<DiscreteVectorAgent>;
Expand Down
3 changes: 2 additions & 1 deletion test/test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ TEST_CASE( "Test parse_comma_separated_list", "[util_parse_list]" )
std::vector<double> dbl_vec_expected = { -2.0, 13.0 };
std::vector<int> int_vec_expected = { 12, 10 };

auto callback = [&]( int idx_list, std::string & substr ) {
auto callback = [&]( int idx_list, std::string & substr )
{
if( idx_list == 0 || idx_list == 3 )
{
int_vec.push_back( std::stoi( substr ) );
Expand Down

0 comments on commit 42a1837

Please sign in to comment.