Skip to content

Commit

Permalink
Merge pull request #41 from seldon-code/renumber
Browse files Browse the repository at this point in the history
Output settings: Allow renumbering of output files
  • Loading branch information
amritagos authored Apr 26, 2024
2 parents 0eb8031 + a6d5f69 commit 047e1bf
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/ActivityDriven/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 print
output_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.
start_numbering_from = 0 # The initial step number, before the simulation runs, is this value. The first step would be (1+start_numbering_from). By default, 0

[model]
max_iterations = 500 # If not set, max iterations is infinite
Expand Down
5 changes: 3 additions & 2 deletions examples/ActivityDrivenBot/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ 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
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.
output_initial = true # Print the initial opinions and network file, before the simulation starts. 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 + start_numbering_from.
start_numbering_from = 0 # The initial step number, before the simulation runs, is this value. The first step would be (1+start_numbering_from). By default, 0

[model]
max_iterations = 1000 # If not set, max iterations is infinite
Expand Down
3 changes: 3 additions & 0 deletions examples/ActivityDrivenInertial/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ model = "ActivityDrivenInertial"
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 print
output_initial = true # Print the initial opinions and network file, before the simulation starts. 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.
start_numbering_from = 0 # The initial step number, before the simulation runs, is this value. The first step would be (1+start_numbering_from). By default, 0

[model]
max_iterations = 500 # If not set, max iterations is infinite
Expand Down
3 changes: 2 additions & 1 deletion examples/ActivityDrivenMeanField/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ 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 print
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.
start_output = 1 # Start writing out opinions and/or network files from this iteration. If not set, this is 1 + start_numbering_from.
start_numbering_from = 0 # The initial step number, before the simulation runs, is this value. The first step would be (1+start_numbering_from). By default, 0

[model]
max_iterations = 2000 # If not set, max iterations is infinite
Expand Down
3 changes: 3 additions & 0 deletions examples/ActivityDrivenReluctance/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ 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 does not print
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.
start_numbering_from = 0 # The initial step number, before the simulation runs, is this value. The first step would be (1+start_numbering_from). By default, 0

[model]
max_iterations = 500 # If not set, max iterations is infinite
Expand Down
3 changes: 2 additions & 1 deletion examples/DeGroot/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ 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 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.
start_output = 1 # Start writing out opinions and/or network files from this iteration. If not set, this is 1 + start_numbering_from.
start_numbering_from = 0 # The initial step number, before the simulation runs, is this value. The first step would be (1+start_numbering_from). By default, 0

[model]
max_iterations = 20 # If not set, max iterations is infinite
Expand Down
3 changes: 2 additions & 1 deletion examples/Deffuant/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ model = "Deffuant"
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.
start_output = 1 # Start writing out opinions and/or network files from this iteration. If not set, this is 1 + start_numbering_from.
start_numbering_from = 0 # The initial step number, before the simulation runs, is this value. The first step would be (1+start_numbering_from). By default, 0

[model]
max_iterations = 1000 # If not set, max iterations is infinite
Expand Down
3 changes: 2 additions & 1 deletion examples/DeffuantVector/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ model = "Deffuant"
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 print
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.
start_output = 1 # Start writing out opinions and/or network files from this iteration. If not set, this is 1 + start_numbering_from.
start_numbering_from = 0 # The initial step number, before the simulation runs, is this value. The first step would be (1+start_numbering_from). By default, 0

[model]
max_iterations = 1000 # If not set, max iterations is infinite
Expand Down
4 changes: 3 additions & 1 deletion include/config_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ struct OutputSettings
std::optional<size_t> n_output_network = std::nullopt;
bool print_progress = false; // Print the iteration time, by default does not print
bool output_initial = true; // Output initial opinions and network, by default always outputs.
size_t start_output = 1; // Start printing opinion and/or network files from this iteration number
size_t start_output = 1; // Start printing opinion and/or network files from this iteration number
size_t start_numbering_from = 0; // The initial step number, before the simulation runs, is this value. The first
// step would be (1+start_numbering_from). By default, 0
};

struct DeGrootSettings
Expand Down
19 changes: 11 additions & 8 deletions include/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,22 @@ class Simulation : public SimulationInterface

void run( const fs::path & output_dir_path ) override
{
auto n_output_agents = this->output_settings.n_output_agents;
auto n_output_network = this->output_settings.n_output_network;
auto start_output = this->output_settings.start_output;
auto output_initial = this->output_settings.output_initial;
auto n_output_agents = this->output_settings.n_output_agents;
auto n_output_network = this->output_settings.n_output_network;
auto start_output = this->output_settings.start_output;
auto initial_step_number = this->output_settings.start_numbering_from;
auto output_initial = this->output_settings.output_initial;

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

if( output_initial )
{
Seldon::network_to_file( network, ( output_dir_path / fs::path( "network_0.txt" ) ).string() );
auto filename = fmt::format( "opinions_{}.txt", 0 );
Seldon::network_to_file(
network,
( output_dir_path / fs::path( fmt::format( "network_{}.txt", initial_step_number ) ) ).string() );
auto filename = fmt::format( "opinions_{}.txt", initial_step_number );
Seldon::agents_to_file( network, ( output_dir_path / fs::path( filename ) ).string() );
}
this->model->initialize_iterations();
Expand Down Expand Up @@ -147,15 +150,15 @@ class Simulation : public SimulationInterface
if( n_output_agents.has_value() && ( this->model->n_iterations() >= start_output )
&& ( 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() + initial_step_number );
Seldon::agents_to_file( network, ( output_dir_path / fs::path( filename ) ).string() );
}

// Write out the network?
if( n_output_network.has_value() && ( this->model->n_iterations() >= start_output )
&& ( 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() + initial_step_number );
Seldon::network_to_file( network, ( output_dir_path / fs::path( filename ) ).string() );
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ SimulationOptions parse_config_file( std::string_view config_file_path )
set_if_specified( options.output_settings.print_progress, tbl["io"]["print_progress"] );
set_if_specified( options.output_settings.output_initial, tbl["io"]["output_initial"] );
set_if_specified( options.output_settings.start_output, tbl["io"]["start_output"] );
set_if_specified( options.output_settings.start_numbering_from, tbl["io"]["start_numbering_from"] );

// Check if the 'model' keyword exists
std::optional<std::string> model_string = tbl["simulation"]["model"].value<std::string>();
Expand Down Expand Up @@ -200,6 +201,7 @@ void validate_settings( const SimulationOptions & options )

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

auto validate_activity = [&]( const auto & model_settings )
{
Expand Down Expand Up @@ -338,6 +340,7 @@ void print_settings( const SimulationOptions & options )
fmt::print( " print_progress {}\n", options.output_settings.print_progress );
fmt::print( " output_initial {}\n", options.output_settings.output_initial );
fmt::print( " start_output {}\n", options.output_settings.start_output );
fmt::print( " start_numbering_from {}\n", options.output_settings.start_numbering_from );
}

} // namespace Seldon::Config

0 comments on commit 047e1bf

Please sign in to comment.