From 4303d50b0711e824ef6588a5f132cd92437b6eb4 Mon Sep 17 00:00:00 2001 From: Moritz Sallermann Date: Mon, 18 Mar 2024 18:56:14 +0000 Subject: [PATCH] agent_io: agents_to_file and agents_from_file Removed the corresponding functions in `agent_generation` and `io`. It makes more sense to localize the agent_io in a single header. Co-authored-by: Amrita Goswami --- include/agent_generation.hpp | 41 -------------------- include/agent_io.hpp | 72 ++++++++++++++++++++++++++++++++++++ include/simulation.hpp | 10 ++--- include/util/io.hpp | 27 -------------- test/test_io.cpp | 2 +- 5 files changed, 77 insertions(+), 75 deletions(-) diff --git a/include/agent_generation.hpp b/include/agent_generation.hpp index 068330d..58f99cb 100644 --- a/include/agent_generation.hpp +++ b/include/agent_generation.hpp @@ -1,5 +1,4 @@ #pragma once -#include "agent_io.hpp" #include "util/misc.hpp" #include #include @@ -7,44 +6,4 @@ namespace Seldon::AgentGeneration { -template -std::vector generate_from_file( const std::string & file ) -{ - std::vector agents{}; - - std::string file_contents = get_file_contents( file ); - bool finished = false; - size_t start_of_line = 0; - - while( !finished ) - { - // Find the end of the current line - auto end_of_line = file_contents.find( '\n', start_of_line ); - if( end_of_line == std::string::npos ) - { - finished = true; - } - // Get the current line as a substring - auto line = file_contents.substr( start_of_line, end_of_line - start_of_line ); - start_of_line = end_of_line + 1; - - if( line.empty() ) - { - break; - } - if( line[0] == '#' ) - { - continue; - } - - // First column is the index of the agent - auto end_of_first_column = line.find( ',', 0 ); - auto opinion_substring = line.substr( end_of_first_column + 1, end_of_line ); - - agents.push_back( agent_from_string( opinion_substring ) ); - } - - return agents; -} - } // namespace Seldon::AgentGeneration \ No newline at end of file diff --git a/include/agent_io.hpp b/include/agent_io.hpp index 8dba47e..f5a16c4 100644 --- a/include/agent_io.hpp +++ b/include/agent_io.hpp @@ -1,5 +1,11 @@ #pragma once +#include "fstream" +#include "network.hpp" +#include "util/misc.hpp" +#include #include +#include +#include #include namespace Seldon @@ -29,4 +35,70 @@ std::vector agent_to_string_column_names() return { "agent_data[...]" }; } +template +void agents_to_file( const Network & network, const std::string & file_path ) +{ + std::fstream fs; + fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc ); + + size_t n_agents = network.n_agents(); + + auto column_names = agent_to_string_column_names(); + + std::string header = "# idx_agent"; + for( auto col : column_names ) + { + header += ", " + col; + } + header += "\n"; + + fmt::print( fs, "{}", header ); + for( size_t idx_agent = 0; idx_agent < n_agents; idx_agent++ ) + { + std::string row = fmt::format( "{:>5}, {:>25}\n", idx_agent, agent_to_string( network.agents[idx_agent] ) ); + fs << row; + } + fs.close(); +} + +template +std::vector agents_from_file( const std::string & file ) +{ + std::vector agents{}; + + std::string file_contents = get_file_contents( file ); + bool finished = false; + size_t start_of_line = 0; + + while( !finished ) + { + // Find the end of the current line + auto end_of_line = file_contents.find( '\n', start_of_line ); + if( end_of_line == std::string::npos ) + { + finished = true; + } + // Get the current line as a substring + auto line = file_contents.substr( start_of_line, end_of_line - start_of_line ); + start_of_line = end_of_line + 1; + + if( line.empty() ) + { + break; + } + if( line[0] == '#' ) + { + continue; + } + + // First column is the index of the agent + auto end_of_first_column = line.find( ',', 0 ); + auto opinion_substring = line.substr( end_of_first_column + 1, end_of_line ); + + agents.push_back( agent_from_string( opinion_substring ) ); + } + + return agents; +} + } // namespace Seldon \ No newline at end of file diff --git a/include/simulation.hpp b/include/simulation.hpp index bf4c2d1..c8d1af2 100644 --- a/include/simulation.hpp +++ b/include/simulation.hpp @@ -4,7 +4,6 @@ #include "network.hpp" #include #include -#include #include #include #include @@ -95,7 +94,7 @@ class Simulation : public SimulationInterface if( cli_agent_file.has_value() ) { - network.agents = AgentGeneration::generate_from_file( cli_agent_file.value() ); + network.agents = agents_from_file( cli_agent_file.value() ); } } else if constexpr( std::is_same_v ) @@ -133,8 +132,7 @@ class Simulation : public SimulationInterface if( cli_agent_file.has_value() ) { - network.agents - = AgentGeneration::generate_from_file( cli_agent_file.value() ); + network.agents = agents_from_file( cli_agent_file.value() ); } } } @@ -154,7 +152,7 @@ class Simulation : public SimulationInterface { 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() ); + Seldon::agents_to_file( network, ( output_dir_path / fs::path( filename ) ).string() ); } this->model->initialize_iterations(); @@ -182,7 +180,7 @@ class Simulation : public SimulationInterface && ( 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() ); + Seldon::agents_to_file( network, ( output_dir_path / fs::path( filename ) ).string() ); } // Write out the network? diff --git a/include/util/io.hpp b/include/util/io.hpp index ed48f49..eeeb1ad 100644 --- a/include/util/io.hpp +++ b/include/util/io.hpp @@ -1,5 +1,4 @@ #pragma once -#include "agent_io.hpp" #include "fstream" #include "network.hpp" #include @@ -38,32 +37,6 @@ void network_to_dot_file( const Network & network, const std::string & f fs.close(); } -template -void opinions_to_file( const Network & network, const std::string & file_path ) -{ - std::fstream fs; - fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc ); - - size_t n_agents = network.n_agents(); - - auto column_names = agent_to_string_column_names(); - - std::string header = "# idx_agent"; - for( auto col : column_names ) - { - header += ", " + col; - } - header += "\n"; - - fmt::print( fs, "{}", header ); - for( size_t idx_agent = 0; idx_agent < n_agents; idx_agent++ ) - { - std::string row = fmt::format( "{:>5}, {:>25}\n", idx_agent, agent_to_string( network.agents[idx_agent] ) ); - fs << row; - } - fs.close(); -} - template void network_to_file( const Network & network, const std::string & file_path ) { diff --git a/test/test_io.cpp b/test/test_io.cpp index e79fd21..f49ab58 100644 --- a/test/test_io.cpp +++ b/test/test_io.cpp @@ -46,7 +46,7 @@ TEST_CASE( "Test reading in the agents from a file", "[io_agents]" ) auto proj_root_path = fs::current_path(); auto network_file = proj_root_path / fs::path( "test/res/opinions.txt" ); - auto agents = Seldon::AgentGeneration::generate_from_file( network_file ); + auto agents = Seldon::agents_from_file( network_file ); std::vector opinions_expected = { 2.1127107987061544, 0.8088982488089491, -0.8802809369462433 }; std::vector activities_expected = { 0.044554683389757696, 0.015813166022685163, 0.015863953902810535 };