Skip to content

Commit

Permalink
Merge pull request #27 from seldon-code/small_io_feature
Browse files Browse the repository at this point in the history
Small io feature
  • Loading branch information
amritagos authored Mar 17, 2024
2 parents 0b91ba6 + cfbc66c commit f6dec98
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions examples/ActivityDriven/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ model = "ActivityDriven"
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 always print
print_initial = true # Print the initial opinions and network file from step 0. If not set, this is true by default.
start_output = 2 # Start writing out opinions and/or network files from this iteration. If not set, this is 1.

[model]
max_iterations = 500 # If not set, max iterations is infinite
Expand Down
2 changes: 2 additions & 0 deletions examples/ActivityDrivenBot/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ rng_seed = 120 # Leaving this empty will pick a random seed
n_output_network = 1 # 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 always print
print_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
Expand Down
2 changes: 2 additions & 0 deletions examples/ActivityDrivenMeanField/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ rng_seed = 12345678 # Leaving this empty will pick a random seed
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 always print
print_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 = 2000 # If not set, max iterations is infinite
Expand Down
2 changes: 2 additions & 0 deletions examples/DeGroot/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ model = "DeGroot"
n_output_network = 20 # Write the network every 20 iterations
n_output_agents = 1 # Write the opinions of agents after every iteration
print_progress = false # Print the iteration time ; if not set, then always prints
print_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 = 20 # If not set, max iterations is infinite
Expand Down
2 changes: 2 additions & 0 deletions include/config_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct OutputSettings
std::optional<size_t> n_output_agents = std::nullopt;
std::optional<size_t> n_output_network = std::nullopt;
bool print_progress = true; // Print the iteration time, by default always prints
bool print_initial = true; // Output initial opinions and network, by default always outputs.
int start_output = 1; // Start printing opinion and/or network files from this iteration number
};

struct DeGrootSettings
Expand Down
18 changes: 12 additions & 6 deletions include/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,19 @@ class Simulation : public SimulationInterface
{
auto n_output_agents = this->output_settings.n_output_agents;
auto n_output_network = this->output_settings.n_output_network;
auto print_iter_start = this->output_settings.start_output;
auto print_initial = this->output_settings.print_initial;

fmt::print( "-----------------------------------------------------------------\n" );
fmt::print( "Starting simulation\n" );
fmt::print( "-----------------------------------------------------------------\n" );

Seldon::IO::network_to_file( network, ( output_dir_path / fs::path( "network_0.txt" ) ).string() );
auto filename = fmt::format( "opinions_{}.txt", 0 );
Seldon::IO::opinions_to_file( network, ( output_dir_path / fs::path( filename ) ).string() );

if( print_initial )
{
Seldon::IO::network_to_file( network, ( output_dir_path / fs::path( "network_0.txt" ) ).string() );
auto filename = fmt::format( "opinions_{}.txt", 0 );
Seldon::IO::opinions_to_file( network, ( output_dir_path / fs::path( filename ) ).string() );
}
this->model->initialize_iterations();

typedef std::chrono::milliseconds ms;
Expand All @@ -169,14 +173,16 @@ class Simulation : public SimulationInterface
}

// 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() >= print_iter_start )
&& ( this->model->n_iterations() % n_output_agents.value() == 0 ) )
{
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() >= print_iter_start )
&& ( this->model->n_iterations() % n_output_network.value() == 0 ) )
{
auto filename = fmt::format( "network_{}.txt", this->model->n_iterations() );
Seldon::IO::network_to_file( network, ( output_dir_path / fs::path( filename ) ).string() );
Expand Down
7 changes: 7 additions & 0 deletions src/config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ SimulationOptions parse_config_file( std::string_view config_file_path )
options.output_settings.n_output_agents = tbl["io"]["n_output_agents"].value<size_t>();
options.output_settings.print_progress
= tbl["io"]["print_progress"].value_or<bool>( bool( options.output_settings.print_progress ) );
options.output_settings.print_initial
= tbl["io"]["print_initial"].value_or<bool>( bool( options.output_settings.print_initial ) );
// @TODO: default value should not be hard-coded here
options.output_settings.start_output = tbl["io"]["start_output"].value_or<int>( 1 );

// Check if the 'model' keyword exists
std::optional<std::string> model_string = tbl["simulation"]["model"].value<std::string>();
Expand Down Expand Up @@ -147,6 +151,9 @@ void validate_settings( const SimulationOptions & options )
auto g_zero = []( auto x ) { return x > 0; };
auto geq_zero = []( auto x ) { return x >= 0; };

// @TODO: Check that start_output is less than the max_iterations?
check( name_and_var( options.output_settings.start_output ), g_zero );

if( options.model == Model::ActivityDrivenModel )
{
auto model_settings = std::get<ActivityDrivenSettings>( options.model_settings );
Expand Down

0 comments on commit f6dec98

Please sign in to comment.