Skip to content

Commit

Permalink
Print iteration time in milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
amritagos committed Nov 19, 2023
1 parent a09b5c5 commit 18a605f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions examples/ActivityDriven/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ model = "ActivityDriven"

[io]
n_output_network = 20 # Write the network every 20 iterations
print_progress = true # Print the iteration time ; if not set, then always print

[model]
max_iterations = 500 # If not set, max iterations is infinite
Expand Down
9 changes: 5 additions & 4 deletions examples/ActivityDrivenMeanField/conf.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
[simulation]
model = "ActivityDriven"
# rng_seed = 120 # Leaving this empty will pick a random seed
rng_seed = 12345678 # Leaving this empty will pick a random seed

[io]
n_output_network = 20 # Write the network every 20 iterations
print_progress = true # Print the iteration time ; if not set, then always print

[model]
max_iterations = 500 # If not set, max iterations is infinite
max_iterations = 2000 # If not set, max iterations is infinite

[ActivityDriven]
dt = 0.01 # Timestep for the integration of the coupled ODEs
m = 10 # Number of agents contacted, when the agent is active
eps = 0.01 # Minimum activity epsilon; a_i belongs to [epsilon,1]
gamma = 2.1 # Exponent of activity power law distribution of activities
reciprocity = 0.5 # probability that when agent i contacts j via weighted reservoir sampling, j also sends feedback to i. So every agent can have more than m incoming connections
homophily = 0.5 # aka beta. if zero, agents pick their interaction partners at random
homophily = 0.0 # aka beta. if zero, agents pick their interaction partners at random
alpha = 3.0 # Controversialness of the issue, must be greater than 0.
K = 3.0 # Social interaction strength
mean_activities = true # Use the mean value of the powerlaw distribution for the activities of all agents
mean_activities = false # Use the mean value of the powerlaw distribution for the activities of all agents
mean_weights = true # Use the meanfield approximation of the network edges

[network]
Expand Down
3 changes: 3 additions & 0 deletions examples/DeGroot/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
model = "DeGroot"
# rng_seed = 120 # Leaving this empty will pick a random seed

[io]
print_progress = false # Print the iteration time ; if not set, then always prints

[model]
max_iterations = 20 # If not set, max iterations is infinite

Expand Down
1 change: 1 addition & 0 deletions include/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Simulation
// Write out the agents/network every n iterations, nullopt means never
std::optional<size_t> n_output_agents = 1;
std::optional<size_t> n_output_network = std::nullopt;
bool print_progress = true; // Print the iteration time, by default always prints
};

private:
Expand Down
15 changes: 9 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,23 @@ int main( int argc, char * argv[] )
const std::optional<size_t> n_output_agents = simulation.output_settings.n_output_agents;
const std::optional<size_t> n_output_network = simulation.output_settings.n_output_network;

typedef std::chrono::milliseconds ms;
auto t_simulation_start = std::chrono::high_resolution_clock::now();
auto print_progress = true;
do
{
auto t_iter_start = std::chrono::high_resolution_clock::now();

simulation.model->iteration();

auto t_iter_end = std::chrono::high_resolution_clock::now();
auto iter_time = std::chrono::duration_cast<std::chrono::seconds>( t_iter_end - t_iter_start );
auto iter_time = std::chrono::duration_cast<ms>( t_iter_end - t_iter_start );

// Print the iteration time?
if( print_progress )
if( simulation.output_settings.print_progress )
{
fmt::print( "Iteration {} iter_time = {:%Hh %Mm %Ss}\n", simulation.model->n_iterations, iter_time );
fmt::print(
"Iteration {} iter_time = {:%Hh %Mm %Ss} \n", simulation.model->n_iterations,
std::chrono::floor<ms>( iter_time ) );
}

// Write out the opinion?
Expand All @@ -87,10 +89,11 @@ int main( int argc, char * argv[] )
} while( !simulation.model->finished() );

auto t_simulation_end = std::chrono::high_resolution_clock::now();
auto total_time = std::chrono::duration_cast<std::chrono::seconds>( t_simulation_end - t_simulation_start );
auto total_time = std::chrono::duration_cast<ms>( t_simulation_end - t_simulation_start );

fmt::print( "-----------------------------------------------------------------\n" );
fmt::print(
"Finished after {} iterations, total time = {:%Hh %Mm %Ss}\n", simulation.model->n_iterations, total_time );
"Finished after {} iterations, total time = {:%Hh %Mm %Ss}\n", simulation.model->n_iterations,
std::chrono::floor<ms>( total_time ) );
return 0;
}
6 changes: 6 additions & 0 deletions src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Seldon::Simulation::Simulation(
output_settings.n_output_agents = n_output_agents.value();
}

auto print_progress = tbl["io"]["print_progress"].value<bool>();
if( print_progress.has_value() )
{
output_settings.print_progress = print_progress.value();
}

// Check if the 'model' keyword exists
std::optional<std::string> model_opt = tbl["simulation"]["model"].value<std::string>();
if( !model_opt.has_value() )
Expand Down

0 comments on commit 18a605f

Please sign in to comment.