Skip to content

Commit

Permalink
Deffuant: Move initialize_agents from constructor
Browse files Browse the repository at this point in the history
We now call initialize_agents for the Deffuant model out of the
constructor, so that we can pass it the dimensions for the
DeffuantVector model

Co-authored-by: Moritz Sallermann <[email protected]>
  • Loading branch information
amritagos and MSallermann committed Mar 22, 2024
1 parent 0014712 commit 8940c15
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
24 changes: 24 additions & 0 deletions examples/DeffuantVector/conf.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[simulation]
model = "Deffuant"
# rng_seed = 120 # Leaving this empty will pick a random seed

[io]
# n_output_network = 20 # Write the network every 20 iterations
n_output_agents = 1 # Write the opinions of agents after every iteration
print_progress = true # Print the iteration time ; if not set, then does not prints
output_initial = true # Print the initial opinions and network file from step 0. If not set, this is true by default.
start_output = 1 # Start writing out opinions and/or network files from this iteration. If not set, this is 1.

[model]
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
binary_vector = true # If true, this will be the multi-dimensional binary vector Deffuant model
dim = 5 # For the multi-dimensional binary vector Deffuant model, define the number of dimensions in each opinion vector

[network]
number_of_agents = 1000
connections_per_agent = 0
2 changes: 1 addition & 1 deletion include/connectivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TarjanConnectivityAlgo
{
lowest[v] = std::min( lowest[v], num[u] );
} // u not processed
} // u has been visited
} // u has been visited
}

// Now v has been processed
Expand Down
18 changes: 18 additions & 0 deletions include/model_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,22 @@ inline auto create_model_deffuant( Network<AgentT> & network, const ModelVariant
}
}

template<typename AgentT>
inline auto
create_model_deffuant_vector( Network<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
{
if constexpr( std::is_same_v<AgentT, DeffuantModelVector::AgentT> )
{
auto deffuant_settings = std::get<Config::DeffuantSettings>( model_settings );
auto model = std::make_unique<DeffuantModelVector>( deffuant_settings, network, gen );
model.initialize_agents();
return model;
}
else
{
throw std::runtime_error( "Incompatible agent and model type!" );
return std::unique_ptr<Model<AgentT>>{};
}
}

} // namespace Seldon::ModelFactory
2 changes: 0 additions & 2 deletions include/models/DeffuantModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class DeffuantModelAbstract : public Model<AgentT_>
}
network = NetworkGeneration::generate_square_lattice<AgentT>( n_edge );
}

initialize_agents();
}

std::vector<std::size_t> select_interacting_agent_pair()
Expand Down
2 changes: 1 addition & 1 deletion test/test_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ TEST_CASE( "Testing the network class" )
auto weight = buffer_w[i_neighbour];
std::tuple<size_t, size_t, Network::WeightT> edge{
neighbour, i_agent, weight
}; // Note that i_agent and neighbour are flipped compared to before
}; // Note that i_agent and neighbour are flipped compared to before
REQUIRE( old_edges.contains( edge ) ); // can we find the transposed edge?
old_edges.extract( edge ); // extract the edge afterwards
}
Expand Down

0 comments on commit 8940c15

Please sign in to comment.