Skip to content

Commit

Permalink
Title: Clean up code after linting
Browse files Browse the repository at this point in the history
- cleaned up the code after running clang-format.
- using NetworkT instead of using Network, to not shadow the Network
class
- also put the `typename` in `using typename NetworkT::WeightT` etc.
- renamed unittests so that they dont have any whitepsace, i.empty
"Test_Activity Driven" -> "Test_Activity_Driven", because such paths are
nicer to work with
  • Loading branch information
MSallermann committed Mar 15, 2024
1 parent 1aa976e commit 3f134a4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 39 deletions.
10 changes: 5 additions & 5 deletions include/models/ActivityDrivenModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ inline void Agent<ActivityAgentData>::from_string( const std::string & str )
class ActivityAgentModel : public Model<Agent<ActivityAgentData>>
{
public:
using AgentT = Agent<ActivityAgentData>;
using Network = Network<AgentT>;
using AgentT = Agent<ActivityAgentData>;
using NetworkT = Network<AgentT>;

private:
Network & network;
std::vector<std::vector<Network::WeightT>> contact_prob_list; // Probability of choosing i in 1 to m rounds
NetworkT & network;
std::vector<std::vector<NetworkT::WeightT>> contact_prob_list; // Probability of choosing i in 1 to m rounds
// Random number generation
std::mt19937 & gen; // reference to simulation Mersenne-Twister engine
std::set<std::pair<size_t, size_t>> reciprocal_edge_buffer{};
Expand Down Expand Up @@ -114,7 +114,7 @@ class ActivityAgentModel : public Model<Agent<ActivityAgentData>>
return n_bots > 0;
}

ActivityAgentModel( Network & network, std::mt19937 & gen );
ActivityAgentModel( NetworkT & network, std::mt19937 & gen );

void get_agents_from_power_law(); // This needs to be called after eps and gamma have been set

Expand Down
6 changes: 3 additions & 3 deletions include/models/DeGroot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class DeGrootModel : public Model<Agent<double>>
{
public:
using AgentT = Agent<double>;
using Network = Network<AgentT>;
using NetworkT = Network<AgentT>;
double convergence_tol = 1e-12;

DeGrootModel( Network & network );
DeGrootModel( NetworkT & network );

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

private:
double max_opinion_diff = 0;
Network & network;
NetworkT & network;
std::vector<AgentT> agents_current_copy;
};

Expand Down
8 changes: 4 additions & 4 deletions include/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class Network
Network(
std::vector<std::vector<size_t>> && neighbour_list, std::vector<std::vector<WeightT>> && weight_list,
EdgeDirection direction )
: neighbour_list( neighbour_list ),
: agents( std::vector<AgentT>( neighbour_list.size() ) ),
neighbour_list( neighbour_list ),
weight_list( weight_list ),
_direction( direction ),
agents( std::vector<AgentT>( neighbour_list.size() ) )
_direction( direction )
{
}

Expand Down Expand Up @@ -124,7 +124,7 @@ class Network
/*
Gives a view into the edge weights going out/coming in at agent_idx
*/
void set_weights( std::size_t agent_idx, std::span<const WeightT> weights )
void set_weights( std::size_t agent_idx, const std::span<const WeightT> weights )
{
if( neighbour_list[agent_idx].size() != weights.size() )
{
Expand Down
32 changes: 16 additions & 16 deletions include/network_generation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ template<typename AgentType>
std::unique_ptr<Network<AgentType>>
generate_n_connections( size_t n_agents, size_t n_connections, bool self_interaction, std::mt19937 & gen )
{
using Network = Network<AgentType>;
using WeightT = Network::WeightT;
using NetworkT = Network<AgentType>;
using WeightT = typename NetworkT::WeightT;

std::vector<std::vector<size_t>> neighbour_list; // Neighbour list for the connections
std::vector<std::vector<WeightT>> weight_list; // List for the interaction weights of each connection
Expand Down Expand Up @@ -70,16 +70,16 @@ generate_n_connections( size_t n_agents, size_t n_connections, bool self_interac

} // end of loop through n_agents

return std::make_unique<Network>(
std::move( neighbour_list ), std::move( weight_list ), Network::EdgeDirection::Incoming );
return std::make_unique<NetworkT>(
std::move( neighbour_list ), std::move( weight_list ), NetworkT::EdgeDirection::Incoming );
}

template<typename AgentType>
std::unique_ptr<Network<AgentType>>
generate_fully_connected( size_t n_agents, typename Network<AgentType>::WeightT weight = 0.0 )
{
using Network = Network<AgentType>;
using WeightT = Network::WeightT;
using NetworkT = Network<AgentType>;
using WeightT = typename NetworkT::WeightT;

std::vector<std::vector<size_t>> neighbour_list; // Neighbour list for the connections
std::vector<std::vector<WeightT>> weight_list; // List for the interaction weights of each connection
Expand All @@ -104,15 +104,15 @@ generate_fully_connected( size_t n_agents, typename Network<AgentType>::WeightT

} // end of loop through n_agents

return std::make_unique<Network>(
std::move( neighbour_list ), std::move( weight_list ), Network::EdgeDirection::Incoming );
return std::make_unique<NetworkT>(
std::move( neighbour_list ), std::move( weight_list ), NetworkT::EdgeDirection::Incoming );
}

template<typename AgentType>
std::unique_ptr<Network<AgentType>> generate_fully_connected( size_t n_agents, std::mt19937 & gen )
{
using Network = Network<AgentType>;
using WeightT = Network::WeightT;
using NetworkT = Network<AgentType>;
using WeightT = typename NetworkT::WeightT;

std::vector<std::vector<size_t>> neighbour_list; // Neighbour list for the connections
std::vector<std::vector<WeightT>> weight_list; // List for the interaction weights of each connection
Expand Down Expand Up @@ -156,15 +156,15 @@ std::unique_ptr<Network<AgentType>> generate_fully_connected( size_t n_agents, s

} // end of loop through n_agents

return std::make_unique<Network>(
std::move( neighbour_list ), std::move( weight_list ), Network::EdgeDirection::Incoming );
return std::make_unique<NetworkT>(
std::move( neighbour_list ), std::move( weight_list ), NetworkT::EdgeDirection::Incoming );
}

template<typename AgentType>
std::unique_ptr<Network<AgentType>> generate_from_file( const std::string & file )
{
using Network = Network<AgentType>;
using WeightT = Network::WeightT;
using NetworkT = Network<AgentType>;
using WeightT = typename NetworkT::WeightT;
std::vector<std::vector<size_t>> neighbour_list; // Neighbour list for the connections
std::vector<std::vector<WeightT>> weight_list; // List for the interaction weights of each connection

Expand Down Expand Up @@ -238,7 +238,7 @@ std::unique_ptr<Network<AgentType>> generate_from_file( const std::string & file
}
}

return std::make_unique<Network>(
std::move( neighbour_list ), std::move( weight_list ), Network::EdgeDirection::Incoming );
return std::make_unique<NetworkT>(
std::move( neighbour_list ), std::move( weight_list ), NetworkT::EdgeDirection::Incoming );
}
} // namespace Seldon::NetworkGeneration
2 changes: 1 addition & 1 deletion include/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Simulation
model = std::move( model_activityDriven );
}
}
void run( fs::path output_dir_path )
void run( const fs::path & output_dir_path )
{
auto n_output_agents = this->output_settings.n_output_agents;
auto n_output_network = this->output_settings.n_output_network;
Expand Down
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ exe = executable('seldon', sources_seldon + 'src/main.cpp',
tests = [
['Test_Tarjan', 'test/test_tarjan.cpp'],
['Test_DeGroot', 'test/test_deGroot.cpp'],
['Test_Activity Driven', 'test/test_activity.cpp'],
['Test_Activity_Driven', 'test/test_activity.cpp'],
['Test_Network', 'test/test_network.cpp'],
['Test_Network Generation', 'test/test_network_generation.cpp'],
['Test_Network_Generation', 'test/test_network_generation.cpp'],
['Test_Sampling', 'test/test_sampling.cpp'],
['Test_IO', 'test/test_io.cpp'],
]
Expand Down
6 changes: 3 additions & 3 deletions src/models/ActivityDrivenModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace Seldon
{

ActivityAgentModel::ActivityAgentModel( Network & network, std::mt19937 & gen )
ActivityAgentModel::ActivityAgentModel( NetworkT & network, std::mt19937 & gen )
: Model<ActivityAgentModel::AgentT>(),
network( network ),
contact_prob_list( std::vector<std::vector<Network::WeightT>>( network.n_agents() ) ),
contact_prob_list( std::vector<std::vector<NetworkT::WeightT>>( network.n_agents() ) ),
gen( gen )
{
}
Expand Down Expand Up @@ -135,7 +135,7 @@ void ActivityAgentModel::update_network_probabilistic()

void ActivityAgentModel::update_network_mean()
{
using WeightT = Network::WeightT;
using WeightT = NetworkT::WeightT;
std::vector<WeightT> weights( network.n_agents(), 0.0 );

// Set all weights to zero in the beginning
Expand Down
13 changes: 9 additions & 4 deletions src/models/DeGroot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#include <cmath>
#include <iterator>

Seldon::DeGrootModel::DeGrootModel( Network & network )
namespace Seldon
{

DeGrootModel::DeGrootModel( NetworkT & network )
: Model<AgentT>(), network( network ), agents_current_copy( network.agents )
{
// For a strongly connected network, the number of SCCs should be 1
Expand All @@ -19,7 +22,7 @@ Seldon::DeGrootModel::DeGrootModel( Network & network )
}
}

void Seldon::DeGrootModel::iteration()
void DeGrootModel::iteration()
{
Model<AgentT>::iteration();

Expand Down Expand Up @@ -49,8 +52,10 @@ void Seldon::DeGrootModel::iteration()
}
}

bool Seldon::DeGrootModel::finished()
bool DeGrootModel::finished()
{
bool converged = max_opinion_diff < convergence_tol;
return Model<AgentT>::finished() || converged;
}
}

} // namespace Seldon
3 changes: 2 additions & 1 deletion test/test_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ TEST_CASE( "Test the meanfield activity driven model with 10 agents", "[activity
auto mean_activity = dist.mean();

// Calculate the critical controversialness
auto set_opinions_and_run = [&]( bool above_critical_controversialness ) {
auto set_opinions_and_run = [&]( bool above_critical_controversialness )
{
auto simulation = Simulation<AgentT>( options, std::nullopt, std::nullopt );
auto initial_opinion_delta = 0.1; // Set the initial opinion in the interval [-delta, delta]

Expand Down

0 comments on commit 3f134a4

Please sign in to comment.