diff --git a/include/simulation.hpp b/include/simulation.hpp index 8ad7dd5..76ce10c 100644 --- a/include/simulation.hpp +++ b/include/simulation.hpp @@ -26,7 +26,7 @@ class Simulation std::mt19937 gen; public: - Model model; + std::unique_ptr> model; Network network; Config::OutputSettings output_settings; @@ -78,49 +78,50 @@ class Simulation auto degroot_settings = std::get( 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( 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( cli_agent_file.value() ); } - - model = std::move( model_DeGroot ); } else if constexpr( std::is_same::value ) { auto activitydriven_settings = std::get( 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( 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( cli_agent_file.value() ); } - - model = std::move( model_activityDriven ); } } @@ -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( t_iter_end - t_iter_start ); @@ -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( 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() ); } } @@ -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( total_time ) ); fmt::print( "=================================================================\n" ); }