Skip to content

Commit

Permalink
NetworkRefactor: IO tests work
Browse files Browse the repository at this point in the history
TODO: Activity driven model test
  • Loading branch information
amritagos committed Mar 15, 2024
1 parent 29bb411 commit d2d3919
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 70 deletions.
133 changes: 65 additions & 68 deletions include/util/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,83 +37,80 @@ void network_to_dot_file( const Network<AgentT> & network, const std::string & f
fs.close();
}

// @TODO: Nteowrk now contains the agents, so there is no need for simulation.
// @TODO: Network now contains the agents, so there is no need for simulation.
template<typename AgentT>
void opinions_to_file( const Network<AgentT> & network, const std::string & file_path )
{
// std::fstream fs;
// fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc );

// auto & network = simulation.network;
// auto & model = simulation.model;
// size_t n_agents = network->n_agents();

// fmt::print( fs, "# idx_agent, opinion[...]\n" );
// for( size_t idx_agent = 0; idx_agent < n_agents; idx_agent++ )
// {
// std::string row = fmt::format( "{:>5}, {:>25}\n", idx_agent, model->get_agent( idx_agent )->to_string() );
// fs << row;
// }
// fs.close();
std::fstream fs;
fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc );

size_t n_agents = network->n_agents();

fmt::print( fs, "# idx_agent, opinion[...]\n" );
for( size_t idx_agent = 0; idx_agent < n_agents; idx_agent++ )
{
std::string row = fmt::format( "{:>5}, {:>25}\n", idx_agent, network.agents[idx_agent].to_string() );
fs << row;
}
fs.close();
}

// @TODO: There is no need to use simulation anymore since network should have the agents
template<typename AgentT>
void network_to_file( const Network<AgentT> & network, const std::string & file_path )
{
// std::fstream fs;
// fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc );

// auto & network = *simulation.network;
// size_t n_agents = network.n_agents();

// fmt::print( fs, "# idx_agent, n_neighbours_in, indices_neighbours_in[...], weights_in[...]\n" );

// for( size_t idx_agent = 0; idx_agent < n_agents; idx_agent++ )
// {
// auto buffer_neighbours = network.get_neighbours( idx_agent );
// auto buffer_weights = network.get_weights( idx_agent );

// std::string row = fmt::format( "{:>5}, {:>5}", idx_agent, buffer_neighbours.size() );

// if( buffer_neighbours.empty() )
// {
// row += "\n";
// }
// else
// {
// row += ", ";
// }

// for( const auto & idx_neighbour : buffer_neighbours )
// {
// row += fmt::format( "{:>5}, ", idx_neighbour );
// }

// const auto n_weights = buffer_weights.size();
// for( size_t i_weight = 0; i_weight < n_weights; i_weight++ )
// {
// const auto & weight = buffer_weights[i_weight];
// if( i_weight == n_weights - 1 ) // At the end of a row
// {
// if( idx_agent == n_agents - 1 ) // At the end of the file
// {
// row += fmt::format( "{:>25}", weight );
// }
// else
// {
// row += fmt::format( "{:>25}\n", weight );
// }
// }
// else
// {
// row += fmt::format( "{:>25}, ", weight );
// }
// }

// fs << row;
// }
// fs.close();
std::fstream fs;
fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc );

size_t n_agents = network.n_agents();

fmt::print( fs, "# idx_agent, n_neighbours_in, indices_neighbours_in[...], weights_in[...]\n" );

for( size_t idx_agent = 0; idx_agent < n_agents; idx_agent++ )
{
auto buffer_neighbours = network.get_neighbours( idx_agent );
auto buffer_weights = network.get_weights( idx_agent );

std::string row = fmt::format( "{:>5}, {:>5}", idx_agent, buffer_neighbours.size() );

if( buffer_neighbours.empty() )
{
row += "\n";
}
else
{
row += ", ";
}

for( const auto & idx_neighbour : buffer_neighbours )
{
row += fmt::format( "{:>5}, ", idx_neighbour );
}

const auto n_weights = buffer_weights.size();
for( size_t i_weight = 0; i_weight < n_weights; i_weight++ )
{
const auto & weight = buffer_weights[i_weight];
if( i_weight == n_weights - 1 ) // At the end of a row
{
if( idx_agent == n_agents - 1 ) // At the end of the file
{
row += fmt::format( "{:>25}", weight );
}
else
{
row += fmt::format( "{:>25}\n", weight );
}
}
else
{
row += fmt::format( "{:>25}, ", weight );
}
}

fs << row;
}
fs.close();
}

} // namespace IO
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tests = [
['Test_Network', 'test/test_network.cpp'],
['Test_Network Generation', 'test/test_network_generation.cpp'],
['Test_Sampling', 'test/test_sampling.cpp'],
# ['Test_IO', 'test/test_io.cpp'],
['Test_IO', 'test/test_io.cpp'],
]

Catch2 = dependency('Catch2', method : 'cmake', modules : ['Catch2::Catch2WithMain', 'Catch2::Catch2'])
Expand Down
4 changes: 3 additions & 1 deletion test/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ TEST_CASE( "Test reading in the network from a file", "[io_network]" )
{
using namespace Seldon;
using namespace Catch::Matchers;
using AgentT = ActivityAgentModel::AgentT;
using Network = Network<AgentT>;

auto proj_root_path = fs::current_path();
auto network_file = proj_root_path / fs::path( "test/res/network.txt" );

auto network = Seldon::NetworkGeneration::generate_from_file( network_file );
auto network = Seldon::NetworkGeneration::generate_from_file<AgentT>( network_file );

REQUIRE( network->n_agents() == 3 );

Expand Down

0 comments on commit d2d3919

Please sign in to comment.