Skip to content

Commit

Permalink
Network: removed get_weight and get_neighbour and added non-const ver…
Browse files Browse the repository at this point in the history
…sions of get_weights and get_neighbours
  • Loading branch information
MSallermann committed Mar 8, 2024
1 parent 0f98eb1 commit 19f7ab0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
6 changes: 2 additions & 4 deletions include/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ class Network
std::size_t n_agents() const;

std::span<const size_t> get_neighbours( std::size_t agent_idx ) const;
std::span<size_t> get_neighbours( std::size_t agent_idx );

std::span<const WeightT> get_weights( std::size_t agent_idx ) const;
std::span<WeightT> get_weights( std::size_t agent_idx );

std::size_t get_n_edges( std::size_t agent_idx ) const;

WeightT & get_weight( std::size_t agent_idx, std::size_t i_weight );

std::size_t & get_neighbour( std::size_t agent_idx, std::size_t i_neighbour );

void set_weights( std::size_t agent_idx, const std::vector<WeightT> & weights );

void set_neighbours_and_weights(
Expand Down
4 changes: 2 additions & 2 deletions src/models/ActivityDrivenModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ void Seldon::ActivityAgentModel::update_network_mean()
double prob_contact_ji = contact_prob_list[j][idx_agent];

// Set the incoming agent weight, j-i in weight list
auto & win_ji = network.get_weight( j, idx_agent );
double & win_ji = network.get_weights( j )[idx_agent];
win_ji += prob_contact_ij;

// Handle the reciprocity for j->i
// Update incoming weight i-j
auto & win_ij = network.get_weight( idx_agent, j );
double & win_ij = network.get_weights( idx_agent )[j];

// The probability of reciprocating is
win_ij += ( 1.0 - prob_contact_ji ) * reciprocity * prob_contact_ij;
Expand Down
28 changes: 9 additions & 19 deletions src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ std::span<const size_t> Seldon::Network::get_neighbours( std::size_t agent_idx )
return std::span( neighbour_list[agent_idx].cbegin(), neighbour_list[agent_idx].cend() );
}

std::span<size_t> Seldon::Network::get_neighbours( std::size_t agent_idx )
{
return std::span( neighbour_list[agent_idx].begin(), neighbour_list[agent_idx].end() );
}

void Seldon::Network::set_neighbours_and_weights(
std::size_t agent_idx, const std::vector<size_t> & buffer_neighbours, const WeightT & weight )
{
Expand Down Expand Up @@ -69,29 +74,14 @@ std::span<const Seldon::Network::WeightT> Seldon::Network::get_weights( std::siz
return std::span<const Seldon::Network::WeightT>( weight_list[agent_idx].cbegin(), weight_list[agent_idx].cend() );
}

std::size_t Seldon::Network::get_n_edges( std::size_t agent_idx ) const
{
return neighbour_list[agent_idx].size();
}

Seldon::Network::WeightT & Seldon::Network::get_weight( std::size_t agent_idx, std::size_t i_weight )
std::span<Seldon::Network::WeightT> Seldon::Network::get_weights( std::size_t agent_idx )
{
if( i_weight >= weight_list[agent_idx].size() )
[[unlikely]]
{
throw std::runtime_error( "Network::get_weight: requested weight does not exist!" );
}
return weight_list[agent_idx][i_weight];
return std::span<Seldon::Network::WeightT>( weight_list[agent_idx].begin(), weight_list[agent_idx].end() );
}

std::size_t & Seldon::Network::get_neighbour( std::size_t agent_idx, std::size_t i_neighbour )
std::size_t Seldon::Network::get_n_edges( std::size_t agent_idx ) const
{
if( i_neighbour >= neighbour_list[agent_idx].size() )
[[unlikely]]
{
throw std::runtime_error( "Network::get_neighbour: requested neighbour does not exist!" );
}
return neighbour_list[agent_idx][i_neighbour];
return neighbour_list[agent_idx].size();
}

void Seldon::Network::set_weights( std::size_t agent_idx, const std::vector<Seldon::Network::WeightT> & weights )
Expand Down
8 changes: 4 additions & 4 deletions test/test_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ TEST_CASE( "Testing the network class" )
REQUIRE_THAT( weight, Catch::Matchers::UnorderedRangeEquals( buffer_w_get ) );
REQUIRE( network->get_n_edges( 3 ) == 2 );

size_t & n = network->get_neighbour( 3, 0 );
size_t & n = network->get_neighbours( 3 )[0];
REQUIRE( n == neigh[0] );
n = 2;
REQUIRE( network->get_neighbour( 3, 0 ) == 2 );
REQUIRE( network->get_neighbours( 3 )[0] == 2 );

Seldon::Network::WeightT & w = network->get_weight( 3, 1 );
Seldon::Network::WeightT & w = network->get_weights( 3 )[1];
REQUIRE( w == 0.55 );
w = 0.9;
REQUIRE( network->get_weight( 3, 1 ) == w );
REQUIRE( network->get_weights( 3 )[1] == w );
}

SECTION( "Checking that set_neighbours_and_weights works with a vector of weights, push_back and transpose" )
Expand Down

0 comments on commit 19f7ab0

Please sign in to comment.