Skip to content

Commit

Permalink
Network: Rename Network to DirectedNetwork
Browse files Browse the repository at this point in the history
Since, we have started to introduce an undirected network as well, this
just makes sense. Later we can have a base class that is just called
Network.

Co-authored-by: Amrita Goswami <[email protected]>
  • Loading branch information
MSallermann and amritagos committed Mar 27, 2024
1 parent a98ef81 commit beb4712
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 135 deletions.
4 changes: 2 additions & 2 deletions include/agent_io.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "directed_network.hpp"
#include "fstream"
#include "network.hpp"
#include "util/misc.hpp"
#include <fmt/core.h>
#include <fmt/format.h>
Expand Down Expand Up @@ -36,7 +36,7 @@ template<typename AgentT>
}

template<typename AgentT>
void agents_to_file( const Network<AgentT> & network, const std::string & file_path )
void agents_to_file( const DirectedNetwork<AgentT> & network, const std::string & file_path )
{
std::fstream fs;
fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc );
Expand Down
24 changes: 12 additions & 12 deletions include/network.hpp → include/directed_network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Seldon
Note: switch is equivalent to toggle + transpose, but much cheaper!
*/
template<typename AgentType, typename WeightType = double>
class Network
class DirectedNetwork
{
public:
enum class EdgeDirection
Expand All @@ -42,23 +42,23 @@ class Network
// @TODO: Make this private later
std::vector<AgentT> agents{}; // List of agents of type AgentType

Network() = default;
DirectedNetwork() = default;

Network( size_t n_agents )
DirectedNetwork( size_t n_agents )
: agents( std::vector<AgentT>( n_agents ) ),
neighbour_list( std::vector<std::vector<size_t>>( n_agents, std::vector<size_t>{} ) ),
weight_list( std::vector<std::vector<WeightT>>( n_agents, std::vector<WeightT>{} ) )
{
}

Network( std::vector<AgentT> agents )
DirectedNetwork( std::vector<AgentT> agents )
: agents( agents ),
neighbour_list( std::vector<std::vector<size_t>>( agents.size(), std::vector<size_t>{} ) ),
weight_list( std::vector<std::vector<WeightT>>( agents.size(), std::vector<WeightT>{} ) )
{
}

Network(
DirectedNetwork(
std::vector<std::vector<size_t>> && neighbour_list, std::vector<std::vector<WeightT>> && weight_list,
EdgeDirection direction )
: agents( std::vector<AgentT>( neighbour_list.size() ) ),
Expand Down Expand Up @@ -136,18 +136,17 @@ class Network
{
if( neighbour_list[agent_idx].size() != weights.size() )
{
throw std::runtime_error( "Network::set_weights: tried to set weights of the wrong size!" );
throw std::runtime_error( "DirectedNetwork::set_weights: tried to set weights of the wrong size!" );
}
weight_list[agent_idx].assign( weights.begin(), weights.end() );
}

/*
Sets the neighbour indices
*/
void set_edge(
std::size_t agent_idx, std::size_t index_neighbour, std::size_t agent_jdx )
void set_edge( std::size_t agent_idx, std::size_t index_neighbour, std::size_t agent_jdx )
{
neighbour_list[agent_idx][index_neighbour]=agent_jdx;
neighbour_list[agent_idx][index_neighbour] = agent_jdx;
}

/*
Expand All @@ -170,7 +169,7 @@ class Network
if( buffer_neighbours.size() != buffer_weights.size() )
{
throw std::runtime_error(
"Network::set_neighbours_and_weights: both buffers need to have the same length!" );
"DirectedNetwork::set_neighbours_and_weights: both buffers need to have the same length!" );
}

neighbour_list[agent_idx].assign( buffer_neighbours.begin(), buffer_neighbours.end() );
Expand All @@ -180,14 +179,15 @@ class Network
/*
Sets the weight for agent_idx, for a neighbour index
*/
void set_edge_weight(std::size_t agent_idx, std::size_t index_neighbour, WeightT weight){
void set_edge_weight( std::size_t agent_idx, std::size_t index_neighbour, WeightT weight )
{
weight_list[agent_idx][index_neighbour] = weight;
}

/*
Gets the weight for agent_idx, for a neighbour index
*/
const WeightT get_edge_weight(std::size_t agent_idx, std::size_t index_neighbour) const
const WeightT get_edge_weight( std::size_t agent_idx, std::size_t index_neighbour ) const
{
return weight_list[agent_idx][index_neighbour];
}
Expand Down
17 changes: 9 additions & 8 deletions include/model_factory.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "config_parser.hpp"
#include "directed_network.hpp"
#include "model.hpp"
#include "models/ActivityDrivenModel.hpp"
#include "models/DeGroot.hpp"
#include "models/DeffuantModel.hpp"
#include "models/InertialModel.hpp"
#include "network.hpp"
#include <memory>
#include <random>
#include <stdexcept>
Expand All @@ -30,7 +30,7 @@ auto check_agent_type( FuncT func )
}

template<typename AgentT>
inline auto create_model_degroot( Network<AgentT> & network, const ModelVariantT & model_settings )
inline auto create_model_degroot( DirectedNetwork<AgentT> & network, const ModelVariantT & model_settings )
{
if constexpr( std::is_same_v<AgentT, DeGrootModel::AgentT> )
{
Expand All @@ -46,8 +46,8 @@ inline auto create_model_degroot( Network<AgentT> & network, const ModelVariantT
}

template<typename AgentT>
inline auto
create_model_activity_driven( Network<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
inline auto create_model_activity_driven(
DirectedNetwork<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
{
if constexpr( std::is_same_v<AgentT, ActivityDrivenModel::AgentT> )
{
Expand All @@ -64,7 +64,7 @@ create_model_activity_driven( Network<AgentT> & network, const ModelVariantT & m

template<typename AgentT>
inline auto create_model_activity_driven_inertial(
Network<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
DirectedNetwork<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
{
if constexpr( std::is_same_v<AgentT, InertialModel::AgentT> )
{
Expand All @@ -80,7 +80,8 @@ inline auto create_model_activity_driven_inertial(
}

template<typename AgentT>
inline auto create_model_deffuant( Network<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
inline auto
create_model_deffuant( DirectedNetwork<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
{
if constexpr( std::is_same_v<AgentT, DeffuantModel::AgentT> )
{
Expand All @@ -97,8 +98,8 @@ inline auto create_model_deffuant( Network<AgentT> & network, const ModelVariant
}

template<typename AgentT>
inline auto
create_model_deffuant_vector( Network<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
inline auto create_model_deffuant_vector(
DirectedNetwork<AgentT> & network, const ModelVariantT & model_settings, std::mt19937 & gen )
{
if constexpr( std::is_same_v<AgentT, DeffuantModelVector::AgentT> )
{
Expand Down
20 changes: 10 additions & 10 deletions include/models/ActivityDrivenModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "agents/activity_agent.hpp"
#include "agents/inertial_agent.hpp"
#include "config_parser.hpp"
#include "directed_network.hpp"
#include "model.hpp"
#include "network.hpp"
#include "network_generation.hpp"
#include <cstddef>
#include <random>
Expand All @@ -20,12 +20,12 @@ template<typename AgentT_>
class ActivityDrivenModelAbstract : public Model<AgentT_>
{
public:
using AgentT = AgentT_;
using NetworkT = Network<AgentT>;
using WeightT = typename NetworkT::WeightT;
using AgentT = AgentT_;
using DirectedNetworkT = DirectedNetwork<AgentT>;
using WeightT = typename DirectedNetworkT::WeightT;

ActivityDrivenModelAbstract(
const Config::ActivityDrivenSettings & settings, NetworkT & network, std::mt19937 & gen )
const Config::ActivityDrivenSettings & settings, DirectedNetworkT & network, std::mt19937 & gen )
: Model<AgentT>( settings.max_iterations ),
network( network ),
contact_prob_list( std::vector<std::vector<WeightT>>( network.n_agents() ) ),
Expand Down Expand Up @@ -56,15 +56,15 @@ class ActivityDrivenModelAbstract : public Model<AgentT_>
if( mean_weights )
{
auto agents_copy = network.agents;
network = NetworkGeneration::generate_fully_connected<AgentT>( network.n_agents() );
network = DirectedNetworkGeneration::generate_fully_connected<AgentT>( network.n_agents() );
network.agents = agents_copy;
}
}

void iteration() override {};

protected:
NetworkT & network;
DirectedNetworkT & network;

private:
std::vector<std::vector<WeightT>> contact_prob_list; // Probability of choosing i in 1 to m rounds
Expand Down Expand Up @@ -291,17 +291,17 @@ class ActivityDrivenModelAbstract : public Model<AgentT_>
double prob_contact_ji = contact_prob_list[j][idx_agent];

// Set the incoming agent weight, j-i in weight list
double win_ji = network.get_edge_weight( j ,idx_agent);
double win_ji = network.get_edge_weight( j, idx_agent );
win_ji += prob_contact_ij;
network.set_edge_weight(j, idx_agent, win_ji);
network.set_edge_weight( j, idx_agent, win_ji );

// Handle the reciprocity for j->i
// Update incoming weight i-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;
network.set_edge_weight(idx_agent, j, win_ij);
network.set_edge_weight( idx_agent, j, win_ij );
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions include/models/DeGroot.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include "agents/simple_agent.hpp"
#include "config_parser.hpp"
#include "directed_network.hpp"
#include "model.hpp"
#include "network.hpp"
#include <optional>
#include <vector>

Expand All @@ -12,18 +12,18 @@ namespace Seldon
class DeGrootModel : public Model<SimpleAgent>
{
public:
using AgentT = SimpleAgent;
using NetworkT = Network<AgentT>;
using AgentT = SimpleAgent;
using DirectedNetworkT = DirectedNetwork<AgentT>;

DeGrootModel( Config::DeGrootSettings settings, NetworkT & network );
DeGrootModel( Config::DeGrootSettings settings, DirectedNetworkT & network );

void iteration() override;
bool finished() override;

private:
double convergence_tol{};
std::optional<double> max_opinion_diff = std::nullopt;
NetworkT & network;
DirectedNetworkT & network;
std::vector<AgentT> agents_current_copy;
};

Expand Down
12 changes: 6 additions & 6 deletions include/models/DeffuantModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "agents/discrete_vector_agent.hpp"
#include "agents/simple_agent.hpp"
#include "config_parser.hpp"
#include "directed_network.hpp"
#include "model.hpp"
#include "network.hpp"
#include "util/math.hpp"
#include <cstddef>
#include <random>
Expand All @@ -20,10 +20,10 @@ template<typename AgentT_>
class DeffuantModelAbstract : public Model<AgentT_>
{
public:
using AgentT = AgentT_;
using NetworkT = Network<AgentT>;
using AgentT = AgentT_;
using DirectedNetworkT = DirectedNetwork<AgentT>;

DeffuantModelAbstract( const Config::DeffuantSettings & settings, NetworkT & network, std::mt19937 & gen )
DeffuantModelAbstract( const Config::DeffuantSettings & settings, DirectedNetworkT & network, std::mt19937 & gen )
: Model<AgentT>( settings.max_iterations ),
homophily_threshold( settings.homophily_threshold ),
mu( settings.mu ),
Expand All @@ -39,7 +39,7 @@ class DeffuantModelAbstract : public Model<AgentT_>
{
throw std::runtime_error( "Number of agents is not a square number." );
}
network = NetworkGeneration::generate_square_lattice<AgentT>( n_edge );
network = DirectedNetworkGeneration::generate_square_lattice<AgentT>( n_edge );
}
}

Expand Down Expand Up @@ -104,7 +104,7 @@ class DeffuantModelAbstract : public Model<AgentT_>
double homophily_threshold{}; // d in paper
double mu{}; // convergence parameter
bool use_network{}; // for the basic Deffuant model
NetworkT & network;
DirectedNetworkT & network;
std::mt19937 & gen; // reference to simulation Mersenne-Twister engine
};

Expand Down
11 changes: 6 additions & 5 deletions include/models/InertialModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "agents/activity_agent.hpp"
#include "agents/inertial_agent.hpp"
#include "config_parser.hpp"
#include "directed_network.hpp"
#include "model.hpp"
#include "models/ActivityDrivenModel.hpp"
#include "network.hpp"
#include "network_generation.hpp"
#include <cstddef>
#include <random>
Expand All @@ -20,11 +20,12 @@ namespace Seldon
class InertialModel : public ActivityDrivenModelAbstract<InertialAgent>
{
public:
using AgentT = InertialAgent;
using NetworkT = Network<AgentT>;
using WeightT = typename NetworkT::WeightT;
using AgentT = InertialAgent;
using DirectedNetworkT = DirectedNetwork<AgentT>;
using WeightT = typename DirectedNetworkT::WeightT;

InertialModel( const Config::ActivityDrivenInertialSettings & settings, NetworkT & network, std::mt19937 & gen );
InertialModel(
const Config::ActivityDrivenInertialSettings & settings, DirectedNetworkT & network, std::mt19937 & gen );

void iteration() override;

Expand Down
Loading

0 comments on commit beb4712

Please sign in to comment.