Skip to content

Commit

Permalink
Merge pull request #25 from seldon-code/network_refactor
Browse files Browse the repository at this point in the history
Template Network on the agent type
  • Loading branch information
MSallermann authored Mar 15, 2024
2 parents 7f77d24 + 2b91f28 commit 0b91ba6
Show file tree
Hide file tree
Showing 23 changed files with 726 additions and 790 deletions.
6 changes: 3 additions & 3 deletions include/agent.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "agent_base.hpp"
#include <fmt/format.h>
namespace Seldon
{
Expand All @@ -8,17 +7,18 @@ namespace Seldon
(which contains an opinion and perhaps some other things),
it needs to implement to_string and from_string*/
template<typename T>
class Agent : public AgentBase
class Agent
{
public:
using data_t = T;
data_t data;
Agent() = default;
Agent( data_t data ) : data( data ) {}
virtual ~Agent() = default;

void from_string( const std::string & str );

std::string to_string() const override
virtual std::string to_string() const
{
return fmt::format( "{:.16f}", data );
}
Expand Down
17 changes: 0 additions & 17 deletions include/agent_base.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion include/agent_generation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::vector<AgentT> generate_from_file( const std::string & file )
// Get the current line as a substring
auto line = file_contents.substr( start_of_line, end_of_line - start_of_line );
start_of_line = end_of_line + 1;
// TODO: check if empty or comment

if( line.empty() )
{
break;
Expand Down
35 changes: 21 additions & 14 deletions include/model.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "model_base.hpp"
#include <cstddef>
#include <optional>

namespace Seldon
Expand All @@ -8,37 +8,44 @@ namespace Seldon
/* Model<T> is a base class from which the acutal models would derive. They have efficient access to a vector of AgentT,
* without any pointer indirections */
template<typename AgentT_>
class Model : public ModelBase
class Model
{
public:
using AgentT = AgentT_;
std::vector<AgentT> agents;

std::optional<int> max_iterations = std::nullopt;
Model( size_t n_agents ) : agents( std::vector<AgentT>( int( n_agents ), AgentT() ) ) {}
Model( std::vector<AgentT> && agents ) : agents( agents ) {}
std::optional<size_t> max_iterations = std::nullopt;

void iteration() override
virtual void initialize_iterations()
{
n_iterations++;
_n_iterations = 0;
}

virtual void iteration()
{
_n_iterations++;
};

bool finished() override
size_t n_iterations()
{
return _n_iterations;
}

virtual bool finished()
{
if( max_iterations.has_value() )
{
return max_iterations.value() <= n_iterations;
return max_iterations.value() <= n_iterations();
}
else
{
return false;
}
};

AgentBase * get_agent( int idx ) override // For this to work AgentT needs to be a subclass of AgentBase
{
return &agents[idx];
}
virtual ~Model() = default;

private:
size_t _n_iterations{};
};

} // namespace Seldon
29 changes: 0 additions & 29 deletions include/model_base.hpp

This file was deleted.

9 changes: 5 additions & 4 deletions include/models/ActivityDrivenModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ inline void Agent<ActivityAgentData>::from_string( const std::string & str )
class ActivityAgentModel : public Model<Agent<ActivityAgentData>>
{
public:
using AgentT = Agent<ActivityAgentData>;
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 @@ -113,7 +114,7 @@ class ActivityAgentModel : public Model<Agent<ActivityAgentData>>
return n_bots > 0;
}

ActivityAgentModel( int n_agents, 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
16 changes: 9 additions & 7 deletions include/models/DeGroot.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "agent.hpp"
#include "model.hpp"
#include "network.hpp"
#include <vector>
Expand All @@ -8,19 +9,20 @@ namespace Seldon

class DeGrootModel : public Model<Agent<double>>
{

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

public:
using AgentT = Agent<double>;
using NetworkT = Network<AgentT>;
double convergence_tol = 1e-12;

DeGrootModel( int n_agents, Network & network );
DeGrootModel( NetworkT & network );

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

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

} // namespace Seldon
Loading

0 comments on commit 0b91ba6

Please sign in to comment.