-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from seldon-code/develop
Develop
- Loading branch information
Showing
29 changed files
with
1,192 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,6 @@ dependencies: | |
- cmake | ||
- ninja | ||
- cpp-argparse | ||
- clang-format | ||
- clang-format=18.1.1 | ||
- tomlplusplus | ||
- catch2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[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 | ||
|
||
[network] | ||
number_of_agents = 1000 | ||
connections_per_agent = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include "agent.hpp" | ||
#include "agent_io.hpp" | ||
#include "util/misc.hpp" | ||
#include <cstddef> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace Seldon | ||
{ | ||
|
||
struct DiscreteVectorAgentData | ||
{ | ||
std::vector<int> opinion{}; | ||
}; | ||
|
||
using DiscreteVectorAgent = Agent<DiscreteVectorAgentData>; | ||
|
||
template<> | ||
inline std::string agent_to_string<DiscreteVectorAgent>( const DiscreteVectorAgent & agent ) | ||
{ | ||
if( agent.data.opinion.empty() ) | ||
return ""; | ||
|
||
auto res = fmt::format( "{}", agent.data.opinion[0] ); | ||
for( size_t i = 1; i < agent.data.opinion.size(); i++ ) | ||
{ | ||
res += fmt::format( ", {}", agent.data.opinion[i] ); | ||
} | ||
return res; | ||
} | ||
|
||
template<> | ||
inline std::string opinion_to_string<DiscreteVectorAgent>( const DiscreteVectorAgent & agent ) | ||
{ | ||
return agent_to_string( agent ); | ||
} | ||
|
||
template<> | ||
inline DiscreteVectorAgent agent_from_string<DiscreteVectorAgent>( const std::string & str ) | ||
{ | ||
DiscreteVectorAgent res{}; | ||
|
||
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; | ||
}; | ||
|
||
// template<> | ||
// inline std::vector<std::string> agent_to_string_column_names<ActivityAgent>() | ||
// { | ||
// return { "opinion", "activity", "reluctance" }; | ||
// } | ||
|
||
} // namespace Seldon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include "config_parser.hpp" | ||
#include "model.hpp" | ||
#include "models/ActivityDrivenModel.hpp" | ||
#include "models/DeGroot.hpp" | ||
#include "models/DeffuantModel.hpp" | ||
#include "network.hpp" | ||
#include <memory> | ||
#include <random> | ||
#include <stdexcept> | ||
#include <type_traits> | ||
|
||
namespace Seldon::ModelFactory | ||
{ | ||
|
||
using ModelVariantT = Config::SimulationOptions::ModelVariantT; | ||
|
||
template<typename AgentT, typename ModelT, typename FuncT> | ||
auto check_agent_type( FuncT func ) | ||
{ | ||
if constexpr( std::is_same_v<AgentT, typename ModelT::AgentT> ) | ||
{ | ||
return func(); | ||
} | ||
else | ||
{ | ||
throw std::runtime_error( "Incompatible agent and model type!" ); | ||
return std::unique_ptr<Model<AgentT>>{}; | ||
} | ||
} | ||
|
||
template<typename AgentT> | ||
inline auto create_model_degroot( Network<AgentT> & network, const ModelVariantT & model_settings ) | ||
{ | ||
if constexpr( std::is_same_v<AgentT, DeGrootModel::AgentT> ) | ||
{ | ||
auto degroot_settings = std::get<Config::DeGrootSettings>( model_settings ); | ||
auto model = std::make_unique<DeGrootModel>( degroot_settings, network ); | ||
return model; | ||
} | ||
else | ||
{ | ||
throw std::runtime_error( "Incompatible agent and model type!" ); | ||
return std::unique_ptr<Model<AgentT>>{}; | ||
} | ||
} | ||
|
||
template<typename AgentT> | ||
inline auto | ||
create_model_activity_driven( Network<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen ) | ||
{ | ||
if constexpr( std::is_same_v<AgentT, ActivityDrivenModel::AgentT> ) | ||
{ | ||
auto activitydriven_settings = std::get<Config::ActivityDrivenSettings>( model_settings ); | ||
auto model = std::make_unique<ActivityDrivenModel>( activitydriven_settings, network, gen ); | ||
return model; | ||
} | ||
else | ||
{ | ||
throw std::runtime_error( "Incompatible agent and model type!" ); | ||
return std::unique_ptr<Model<AgentT>>{}; | ||
} | ||
} | ||
|
||
template<typename AgentT> | ||
inline auto create_model_deffuant( Network<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen ) | ||
{ | ||
if constexpr( std::is_same_v<AgentT, DeffuantModel::AgentT> ) | ||
{ | ||
auto deffuant_settings = std::get<Config::DeffuantSettings>( model_settings ); | ||
auto model = std::make_unique<DeffuantModel>( deffuant_settings, network, gen ); | ||
model->initialize_agents( deffuant_settings.dim ); | ||
return model; | ||
} | ||
else | ||
{ | ||
throw std::runtime_error( "Incompatible agent and model type!" ); | ||
return std::unique_ptr<Model<AgentT>>{}; | ||
} | ||
} | ||
|
||
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( deffuant_settings.dim ); | ||
return model; | ||
} | ||
else | ||
{ | ||
throw std::runtime_error( "Incompatible agent and model type!" ); | ||
return std::unique_ptr<Model<AgentT>>{}; | ||
} | ||
} | ||
|
||
} // namespace Seldon::ModelFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.