Skip to content

Commit

Permalink
Simulation: Reinstate unique_ptr for model
Browse files Browse the repository at this point in the history
Since model actually relies on polymorphic behaviour, it was wrong to
not access it via a pointer to the base class.

Co-authored-by: Amrita Goswami <[email protected]>
  • Loading branch information
MSallermann and amritagos committed Mar 15, 2024
1 parent faa0a1e commit 2b4a816
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions include/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Simulation
std::mt19937 gen;

public:
Model<AgentType> model;
std::unique_ptr<Model<AgentType>> model;
Network<AgentType> network;

Config::OutputSettings output_settings;
Expand Down Expand Up @@ -78,49 +78,50 @@ class Simulation
auto degroot_settings = std::get<Config::DeGrootSettings>( options.model_settings );

// DeGroot specific parameters
auto model_DeGroot = DeGrootModel( network );
model_DeGroot.max_iterations = degroot_settings.max_iterations;
model_DeGroot.convergence_tol = degroot_settings.convergence_tol;
model = [&]() {
auto model = std::make_unique<DeGrootModel>( network );
model->max_iterations = degroot_settings.max_iterations;
model->convergence_tol = degroot_settings.convergence_tol;
return model;
}();

if( cli_agent_file.has_value() )
{
network.agents = AgentGeneration::generate_from_file<DeGrootModel::AgentT>( cli_agent_file.value() );
}

model = std::move( model_DeGroot );
}
else if constexpr( std::is_same<AgentType, ActivityAgentModel::AgentT>::value )
{
auto activitydriven_settings = std::get<Config::ActivityDrivenSettings>( options.model_settings );

auto model_activityDriven = ActivityAgentModel( network, gen );
model_activityDriven.dt = activitydriven_settings.dt;
model_activityDriven.m = activitydriven_settings.m;
model_activityDriven.eps = activitydriven_settings.eps;
model_activityDriven.gamma = activitydriven_settings.gamma;
model_activityDriven.homophily = activitydriven_settings.homophily;
model_activityDriven.reciprocity = activitydriven_settings.reciprocity;
model_activityDriven.alpha = activitydriven_settings.alpha;
model_activityDriven.K = activitydriven_settings.K;
model_activityDriven.mean_activities = activitydriven_settings.mean_activities;
model_activityDriven.mean_weights = activitydriven_settings.mean_weights;
model_activityDriven.max_iterations = activitydriven_settings.max_iterations;

// bot
model_activityDriven.n_bots = activitydriven_settings.n_bots;
model_activityDriven.bot_opinion = activitydriven_settings.bot_opinion;
model_activityDriven.bot_m = activitydriven_settings.bot_m;
model_activityDriven.bot_homophily = activitydriven_settings.bot_homophily;
model_activityDriven.bot_activity = activitydriven_settings.bot_activity;
model_activityDriven.get_agents_from_power_law();
model = [&]() {
auto model = std::make_unique<ActivityAgentModel>( network, gen );
model->dt = activitydriven_settings.dt;
model->m = activitydriven_settings.m;
model->eps = activitydriven_settings.eps;
model->gamma = activitydriven_settings.gamma;
model->homophily = activitydriven_settings.homophily;
model->reciprocity = activitydriven_settings.reciprocity;
model->alpha = activitydriven_settings.alpha;
model->K = activitydriven_settings.K;
model->mean_activities = activitydriven_settings.mean_activities;
model->mean_weights = activitydriven_settings.mean_weights;
model->max_iterations = activitydriven_settings.max_iterations;
// bot
model->n_bots = activitydriven_settings.n_bots;
model->bot_opinion = activitydriven_settings.bot_opinion;
model->bot_m = activitydriven_settings.bot_m;
model->bot_homophily = activitydriven_settings.bot_homophily;
model->bot_activity = activitydriven_settings.bot_activity;
model->get_agents_from_power_law();
return model;
}();

if( cli_agent_file.has_value() )
{
network.agents
= AgentGeneration::generate_from_file<ActivityAgentModel::AgentT>( cli_agent_file.value() );
}

model = std::move( model_activityDriven );
}
}

Expand All @@ -139,11 +140,11 @@ class Simulation

typedef std::chrono::milliseconds ms;
auto t_simulation_start = std::chrono::high_resolution_clock::now();
while( !this->model.finished() )
while( !this->model->finished() )
{
auto t_iter_start = std::chrono::high_resolution_clock::now();

this->model.iteration();
this->model->iteration();

auto t_iter_end = std::chrono::high_resolution_clock::now();
auto iter_time = std::chrono::duration_cast<ms>( t_iter_end - t_iter_start );
Expand All @@ -152,21 +153,21 @@ class Simulation
if( this->output_settings.print_progress )
{
fmt::print(
"Iteration {} iter_time = {:%Hh %Mm %Ss} \n", this->model.n_iterations,
"Iteration {} iter_time = {:%Hh %Mm %Ss} \n", this->model->n_iterations,
std::chrono::floor<ms>( iter_time ) );
}

// Write out the opinion?
if( n_output_agents.has_value() && ( this->model.n_iterations % n_output_agents.value() == 0 ) )
if( n_output_agents.has_value() && ( this->model->n_iterations % n_output_agents.value() == 0 ) )
{
auto filename = fmt::format( "opinions_{}.txt", this->model.n_iterations );
auto filename = fmt::format( "opinions_{}.txt", this->model->n_iterations );
Seldon::IO::opinions_to_file( network, ( output_dir_path / fs::path( filename ) ).string() );
}

// Write out the network?
if( n_output_network.has_value() && ( this->model.n_iterations % n_output_network.value() == 0 ) )
if( n_output_network.has_value() && ( this->model->n_iterations % n_output_network.value() == 0 ) )
{
auto filename = fmt::format( "network_{}.txt", this->model.n_iterations );
auto filename = fmt::format( "network_{}.txt", this->model->n_iterations );
Seldon::IO::network_to_file( network, ( output_dir_path / fs::path( filename ) ).string() );
}
}
Expand All @@ -176,7 +177,7 @@ class Simulation

fmt::print( "-----------------------------------------------------------------\n" );
fmt::print(
"Finished after {} iterations, total time = {:%Hh %Mm %Ss}\n", this->model.n_iterations,
"Finished after {} iterations, total time = {:%Hh %Mm %Ss}\n", this->model->n_iterations,
std::chrono::floor<ms>( total_time ) );
fmt::print( "=================================================================\n" );
}
Expand Down

0 comments on commit 2b4a816

Please sign in to comment.