-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from seldon-code/develop
Develop
- Loading branch information
Showing
28 changed files
with
967 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
#include "util/misc.hpp" | ||
#include <cstddef> | ||
#include <vector> | ||
|
||
namespace Seldon::AgentGeneration | ||
{ | ||
|
||
template<typename AgentT> | ||
std::vector<AgentT> generate_from_file( const std::string & file ) | ||
{ | ||
std::vector<AgentT> agents{}; | ||
|
||
std::string file_contents = get_file_contents( file ); | ||
bool finished = false; | ||
size_t start_of_line = 0; | ||
|
||
while( !finished ) | ||
{ | ||
// Find the end of the current line | ||
auto end_of_line = file_contents.find( '\n', start_of_line ); | ||
if( end_of_line == std::string::npos ) | ||
{ | ||
finished = true; | ||
} | ||
// 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; | ||
} | ||
if( line[0] == '#' ) | ||
{ | ||
continue; | ||
} | ||
|
||
agents.push_back( AgentT() ); | ||
|
||
// First column is the index of the agent | ||
auto end_of_first_column = line.find( ',', 0 ); | ||
auto opinion_substring = line.substr( end_of_first_column + 1, end_of_line ); | ||
agents.back().from_string( opinion_substring ); | ||
} | ||
|
||
return agents; | ||
} | ||
|
||
} // namespace Seldon::AgentGeneration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#pragma once | ||
#include <fmt/format.h> | ||
#include <fmt/ostream.h> | ||
#include <fmt/ranges.h> | ||
#include <optional> | ||
#include <random> | ||
#include <string_view> | ||
#include <variant> | ||
#include <vector> | ||
|
||
namespace Seldon::Config | ||
{ | ||
|
||
/* | ||
As a convention: When a setting has type std::optional, | ||
nullopt signifies to not perform the operation associated | ||
with that setting. Also, the default value of any optional should | ||
be nullopt. | ||
If you feel tempted to make an optional setting with a different default, | ||
just make the setting not optional. | ||
*/ | ||
|
||
enum class Model | ||
{ | ||
DeGroot, | ||
ActivityDrivenModel | ||
}; | ||
|
||
struct OutputSettings | ||
{ | ||
// Write out the agents/network every n iterations, nullopt means never | ||
std::optional<size_t> n_output_agents = std::nullopt; | ||
std::optional<size_t> n_output_network = std::nullopt; | ||
bool print_progress = true; // Print the iteration time, by default always prints | ||
}; | ||
|
||
struct DeGrootSettings | ||
{ | ||
std::optional<int> max_iterations = std::nullopt; | ||
double convergence_tol; | ||
}; | ||
|
||
struct ActivityDrivenSettings | ||
{ | ||
std::optional<int> max_iterations = std::nullopt; | ||
double dt = 0.01; | ||
int m = 10; | ||
double eps = 0.01; | ||
double gamma = 2.1; | ||
double alpha = 3.0; | ||
double homophily = 0.5; | ||
double reciprocity = 0.5; | ||
double K = 3.0; | ||
bool mean_activities = false; | ||
bool mean_weights = false; | ||
size_t n_bots = 0; //@TODO why is this here? | ||
std::vector<int> bot_m = std::vector<int>( 0 ); | ||
std::vector<double> bot_activity = std::vector<double>( 0 ); | ||
std::vector<double> bot_opinion = std::vector<double>( 0 ); | ||
std::vector<double> bot_homophily = std::vector<double>( 0 ); | ||
}; | ||
|
||
struct InitialNetworkSettings | ||
{ | ||
std::optional<std::string> file; | ||
size_t n_agents = 200; | ||
size_t n_connections = 10; | ||
}; | ||
|
||
struct SimulationOptions | ||
{ | ||
Model model; | ||
std::string model_string; | ||
int rng_seed = std::random_device()(); | ||
OutputSettings output_settings; | ||
std::variant<DeGrootSettings, ActivityDrivenSettings> model_settings; | ||
InitialNetworkSettings network_settings; | ||
}; | ||
|
||
SimulationOptions parse_config_file( std::string_view config_file_path ); | ||
void validate_settings( const SimulationOptions & options ); | ||
void print_settings( const SimulationOptions & options ); | ||
|
||
} // namespace Seldon::Config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,31 @@ | ||
#pragma once | ||
|
||
#include "config_parser.hpp" | ||
#include "model_base.hpp" | ||
#include "network.hpp" | ||
#include <filesystem> | ||
#include <memory> | ||
#include <optional> | ||
#include <random> | ||
#include <string> | ||
namespace fs = std::filesystem; | ||
|
||
namespace Seldon | ||
{ | ||
|
||
class Simulation | ||
{ | ||
struct OutputSettings | ||
{ | ||
// Write out the agents/network every n iterations, nullopt means never | ||
std::optional<size_t> n_output_agents = 1; | ||
std::optional<size_t> n_output_network = std::nullopt; | ||
bool print_progress = true; // Print the iteration time, by default always prints | ||
}; | ||
|
||
private: | ||
std::mt19937 gen; | ||
|
||
public: | ||
int n_agents; | ||
std::unique_ptr<ModelBase> model; | ||
std::unique_ptr<Network> network; | ||
OutputSettings output_settings; | ||
Config::OutputSettings output_settings; | ||
Simulation( | ||
const std::string & config_file, const std::optional<std::string> & cli_network_file, | ||
const Config::SimulationOptions & options, const std::optional<std::string> & cli_network_file, | ||
const std::optional<std::string> & cli_agent_file ); | ||
void run( fs::path output_dir_path ); | ||
}; | ||
|
||
} // namespace Seldon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.