Skip to content

Commit

Permalink
Test for reading agent opinions from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
MSallermann committed Mar 13, 2024
1 parent d9a0595 commit 13096bd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
4 changes: 2 additions & 2 deletions include/agent_generation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cstddef>
#include <vector>

namespace Seldon::Agents
namespace Seldon::AgentGeneration
{

template<typename AgentT>
Expand Down Expand Up @@ -47,4 +47,4 @@ std::vector<AgentT> generate_from_file( const std::string & file )
return agents;
}

} // namespace Seldon::Agents
} // namespace Seldon::AgentGeneration
4 changes: 2 additions & 2 deletions src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Seldon::Simulation::Simulation(

if( cli_agent_file.has_value() )
{
model_DeGroot->agents = Agents::generate_from_file<DeGrootModel::AgentT>( cli_agent_file.value() );
model_DeGroot->agents = AgentGeneration::generate_from_file<DeGrootModel::AgentT>( cli_agent_file.value() );
}

model = std::move( model_DeGroot );
Expand Down Expand Up @@ -104,7 +104,7 @@ Seldon::Simulation::Simulation(
if( cli_agent_file.has_value() )
{
model_activityDriven->agents
= Agents::generate_from_file<ActivityAgentModel::AgentT>( cli_agent_file.value() );
= AgentGeneration::generate_from_file<ActivityAgentModel::AgentT>( cli_agent_file.value() );
}

model = std::move( model_activityDriven );
Expand Down
8 changes: 5 additions & 3 deletions test/res/network.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# idx_agent,n_neighbors_in,indices_neighbors_in[...],weights_in[...]
0, 2, 2, 1, 0.1, -0.2
1, 0
2, 1, 1, 1.2
# comment
0, 2, 2, 1, 0.1, -0.2
1, 0
2, 1, 1, 1.2

25 changes: 25 additions & 0 deletions test/test_io.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "agent_generation.hpp"
#include "catch2/matchers/catch_matchers.hpp"
#include "models/ActivityDrivenModel.hpp"
#include "network.hpp"
#include "network_generation.hpp"

Expand Down Expand Up @@ -32,4 +34,27 @@ TEST_CASE( "Test reading in the network from a file", "[io_network]" )
REQUIRE_THAT( neighbours_expected[i], Catch::Matchers::UnorderedRangeEquals( network->get_neighbours( i ) ) );
REQUIRE_THAT( weights_expected[i], Catch::Matchers::UnorderedRangeEquals( network->get_weights( i ) ) );
}
}

TEST_CASE( "Test reading in the agents from a file", "[io_agents]" )
{
using namespace Seldon;
using namespace Catch::Matchers;

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<ActivityAgentModel::AgentT>( network_file );

std::vector<double> opinions_expected = { 2.1127107987061544, 0.8088982488089491, -0.8802809369462433 };
std::vector<double> activities_expected = { 0.044554683389757696, 0.015813166022685163, 0.015863953902810535 };

REQUIRE( agents.size() == 3 );

for( size_t i = 0; i < agents.size(); i++ )
{
fmt::print( "{}", i );
REQUIRE_THAT( agents[i].data.opinion, Catch::Matchers::WithinAbs( opinions_expected[i], 1e-16 ) );
REQUIRE_THAT( agents[i].data.activity, Catch::Matchers::WithinAbs( activities_expected[i], 1e-16 ) );
}
}

0 comments on commit 13096bd

Please sign in to comment.