Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO: change type of rng_seed to int64 and added test for network_to_file #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/config_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fmt/ostream.h>
#include <fmt/ranges.h>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <random>
#include <string_view>
Expand Down Expand Up @@ -102,7 +103,7 @@ struct SimulationOptions
= std::variant<DeGrootSettings, ActivityDrivenSettings, ActivityDrivenInertialSettings, DeffuantSettings>;
Model model;
std::string model_string;
int rng_seed = std::random_device()();
int64_t rng_seed = std::random_device()();
OutputSettings output_settings;
ModelVariantT model_settings;
InitialNetworkSettings network_settings;
Expand Down
10 changes: 10 additions & 0 deletions include/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ class Network
{
}

Network(
const std::vector<std::vector<size_t>> & neighbour_list, const std::vector<std::vector<WeightT>> & weight_list,
EdgeDirection direction )
: agents( std::vector<AgentT>( neighbour_list.size() ) ),
neighbour_list( neighbour_list ),
weight_list( weight_list ),
_direction( direction )
{
}

Network(
std::vector<std::vector<size_t>> && neighbour_list, std::vector<std::vector<WeightT>> && weight_list,
EdgeDirection direction )
Expand Down
2 changes: 1 addition & 1 deletion src/config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ SimulationOptions parse_config_file( std::string_view config_file_path )
toml::table tbl;
tbl = toml::parse_file( config_file_path );

options.rng_seed = tbl["simulation"]["rng_seed"].value_or( int( options.rng_seed ) );
options.rng_seed = tbl["simulation"]["rng_seed"].value_or( int64_t( options.rng_seed ) );

// Parse output settings
options.output_settings.n_output_network = tbl["io"]["n_output_network"].value<size_t>();
Expand Down
33 changes: 33 additions & 0 deletions test/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "models/ActivityDrivenModel.hpp"
#include "network.hpp"
#include "network_generation.hpp"
#include "network_io.hpp"

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
Expand Down Expand Up @@ -38,6 +39,38 @@ TEST_CASE( "Test reading in the network from a file", "[io_network]" )
}
}

TEST_CASE( "Test writing the network to a file", "[io_network]" )
{
using namespace Seldon;
using namespace Catch::Matchers;
using AgentT = ActivityDrivenModel::AgentT;
using Network = Network<AgentT>;

std::vector<std::vector<size_t>> neighbours = { { 3, 1 }, {}, { 1 } };
std::vector<std::vector<Network::WeightT>> weights = { { -0.1, -0.5 }, {}, { -0.2 } };

// Construct a network
auto network = Network( neighbours, weights, Network::EdgeDirection::Incoming );

// Save the network to a file
auto proj_root_path = fs::current_path();
auto network_file = proj_root_path / fs::path( "test/network_out.txt" );
network_to_file( network, network_file );

// Read the network back in
auto network_from_file = Seldon::NetworkGeneration::generate_from_file<AgentT>( network_file );

for( size_t i = 0; i < network.n_agents(); i++ )
{
fmt::print( "{}", i );
REQUIRE_THAT(
network.get_neighbours( i ),
Catch::Matchers::UnorderedRangeEquals( network_from_file.get_neighbours( i ) ) );
REQUIRE_THAT(
network.get_weights( i ), Catch::Matchers::UnorderedRangeEquals( network_from_file.get_weights( i ) ) );
}
}

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