Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Utopia::Models::Opinionet::Opinionet< NWType > Class Template Reference

The Opinionet model class. More...

#include <Opinionet.hh>

Inheritance diagram for Utopia::Models::Opinionet::Opinionet< NWType >:
Inheritance graph
[legend]
Collaboration diagram for Utopia::Models::Opinionet::Opinionet< NWType >:
Collaboration graph
[legend]

Public Types

using Base = Model< Opinionet, OpinionetTypes >
 The base model type.
 
using Config = typename Base::Config
 Data type that holds the configuration.
 
using DataGroup = typename Base::DataGroup
 Data type of the group to write model data to, holding datasets.
 
using DataSet = typename Base::DataSet
 Data type for a dataset.
 
using RNG = typename Base::RNG
 Data type of the shared RNG.
 
- Public Types inherited from Utopia::Model< Derived, ModelTypes >
using Config = typename ModelTypes::Config
 Data type that holds the configuration.
 
using DataManager = DataIO::Default::DefaultDataManager< Derived >
 The data manager to use, specialized with the derived model.
 
using DataGroup = typename ModelTypes::DataGroup
 Data type that is used for storing datasets.
 
using DataSet = typename ModelTypes::DataSet
 Data type that is used for storing data.
 
using RNG = typename ModelTypes::RNG
 Data type of the shared RNG.
 
using Space = typename ModelTypes::Space
 Data type of the space this model resides in.
 
using Time = typename ModelTypes::Time
 Data type for the model time.
 
using Monitor = typename ModelTypes::Monitor
 Data type for the monitor.
 
using MonitorManager = typename ModelTypes::MonitorManager
 Data type for the monitor manager.
 
using Level = typename ModelTypes::Level
 Data type for the hierarchical level.
 

Public Member Functions

template<class ParentModel >
 Opinionet (const std::string name, ParentModel &parent)
 Construct the Opinionet model.
 
void perform_step ()
 Iterate a single step Each step consists of an opinion update and edge rewiring.
 
void monitor ()
 Monitor model information.
 
void write_data ()
 Write data.
 
- Public Member Functions inherited from Utopia::Model< Derived, ModelTypes >
template<class ParentModel , class... WriterArgs>
 Model (const std::string &name, const ParentModel &parent_model, const Config &custom_cfg={}, std::tuple< WriterArgs... > w_args={}, const DataIO::Default::DefaultDecidermap< Derived > &w_deciders=DataIO::Default::default_deciders< Derived >, const DataIO::Default::DefaultTriggermap< Derived > &w_triggers=DataIO::Default::default_triggers< Derived >)
 Constructs a Model instance.
 
const std::shared_ptr< Space > & get_space () const
 Return the space this model resides in.
 
Time get_time () const
 Return the current time of this model.
 
Time get_time_max () const
 Return the maximum time possible for this model.
 
Config get_cfg () const
 Return the config node of this model.
 
std::string get_name () const
 Return the name of this model instance.
 
std::string get_full_name () const
 Return the full name of this model within the model hierarchy.
 
std::shared_ptr< DataGroupget_hdfgrp () const
 Return a pointer to the HDF group this model stores data in.
 
Time get_write_start () const
 Return the parameter that controls when write_data is called first.
 
Time get_write_every () const
 Return the parameter that controls how often write_data is called.
 
DataManager get_datamanager () const
 return the datamanager
 
hsize_t get_remaining_num_writes () const
 Return the number of remaining write_data calls this model will make.
 
std::shared_ptr< RNGget_rng () const
 Return a pointer to the shared RNG.
 
std::shared_ptr< spdlog::logger > get_logger () const
 Return a pointer to the logger of this model.
 
Monitor get_monitor () const
 Return the monitor of this model.
 
std::shared_ptr< MonitorManagerget_monitor_manager () const
 Get the monitor manager of the root model.
 
Level get_level () const
 Return the hierarchical level within the model hierarchy.
 
virtual void prolog ()
 A function that is called before starting model iteration.
 
virtual void epilog ()
 A function that is called after the last iteration of a model.
 
void iterate ()
 Iterate one (time) step of this model.
 
void run ()
 Run the model from the current time to the maximum time.
 
std::shared_ptr< DataSetcreate_dset (const std::string name, const std::shared_ptr< DataGroup > &hdfgrp, std::vector< hsize_t > add_write_shape, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a new dataset within the given group.
 
std::shared_ptr< DataSetcreate_dset (const std::string name, const std::vector< hsize_t > add_write_shape, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a new dataset within the model's base data group.
 
template<class CellManager >
std::shared_ptr< DataSetcreate_cm_dset (const std::string name, const CellManager &cm, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a dataset storing data from a CellManager.
 
template<class AgentManager >
std::shared_ptr< DataSetcreate_am_dset (const std::string name, const AgentManager &am, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a dataset storing data from a AgentManager.
 

Private Member Functions

Interaction_type initialize_interaction ()
 
Opinion_space_type initialize_opinion_space ()
 
Rewiring initialize_rewiring ()
 
void initialize_properties ()
 
NWType initialize_nw () const
 
std::shared_ptr< DataSetcreate_edge_weight_dset ()
 

Private Attributes

const Interaction_type _interaction
 Modes.
 
const Opinion_space_type _opinion_space
 
const Rewiring _rewire
 
NWType _nw
 Network and model dynamics parameters.
 
const double _tolerance
 
const double _susceptibility
 
const double _weighting
 
std::uniform_real_distribution< double_uniform_prob_distr
 A uniform probability distribution.
 
const std::shared_ptr< DataGroup_dgrp_nw
 
const std::shared_ptr< DataSet_dset_opinion
 
const std::shared_ptr< DataSet_dset_edge_weights
 

Additional Inherited Members

- Protected Member Functions inherited from Utopia::Model< Derived, ModelTypes >
void __perform_step ()
 Perform the computation of a step.
 
void __monitor ()
 Monitor information in the terminal.
 
void __write_data ()
 Write data; calls the implementation's write_data method.
 
void __write_initial_state ()
 Write the initial state.
 
void increment_time (const Time dt=1)
 Increment time.
 
void __prolog ()
 The default prolog of a model.
 
void __epilog ()
 The default epilog of a model.
 
Derivedimpl ()
 cast to the derived class
 
const Derivedimpl () const
 const cast to the derived interface
 
- Protected Attributes inherited from Utopia::Model< Derived, ModelTypes >
const std::string _name
 Name of the model instance.
 
const std::string _full_name
 The full name within the model hierarchy.
 
const Level _level
 The level within the model hierarchy.
 
const Config _cfg
 Config node belonging to this model instance.
 
const std::shared_ptr< RNG_rng
 The RNG shared between models.
 
const std::shared_ptr< spdlog::logger > _log
 The (model) logger.
 
std::shared_ptr< Space_space
 The space this model resides in.
 
Time _time
 Model-internal current time stamp.
 
const Time _time_max
 Model-internal maximum time stamp.
 
const std::shared_ptr< DataGroup_hdfgrp
 The HDF group this model instance should write its data to.
 
const Time _write_start
 First time at which write_data is called.
 
const Time _write_every
 How often to call write_data from iterate.
 
Monitor _monitor
 The monitor.
 
DataManager _datamanager
 Manager object for handling data output; see DataManager.
 
- Static Protected Attributes inherited from Utopia::Model< Derived, ModelTypes >
static constexpr WriteMode _write_mode = ModelTypes::write_mode
 Which data-writing mode the base model should use.
 

Detailed Description

template<typename NWType = NetworkUndirected>
class Utopia::Models::Opinionet::Opinionet< NWType >

The Opinionet model class.

This is a 1d opinion dynamics model with interactions based on bounded confidence.

Member Typedef Documentation

◆ Base

The base model type.

◆ Config

Data type that holds the configuration.

◆ DataGroup

Data type of the group to write model data to, holding datasets.

◆ DataSet

Data type for a dataset.

◆ RNG

Data type of the shared RNG.

Constructor & Destructor Documentation

◆ Opinionet()

template<typename NWType = NetworkUndirected>
template<class ParentModel >
Utopia::Models::Opinionet::Opinionet< NWType >::Opinionet ( const std::string  name,
ParentModel parent 
)
inline

Construct the Opinionet model.

Parameters
nameName of this model instance
parentThe parent model this model instance resides in
129 :
130 // Initialize first via base model
131 Base(name, parent),
132 // Initialize modes
136 // Initialize the network
137 _nw(this->initialize_nw()),
138 // Initialize the model parameters
139 _tolerance(get_as<double>("tolerance", this->_cfg)),
140 _susceptibility(get_as<double>("susceptibility", this->_cfg)),
142 "weighting", this->_cfg["network"]["edges"])),
143 _uniform_prob_distr(0., 1.),
144 // Create datagroups and datasets
145 _dgrp_nw(create_graph_group(_nw, this->_hdfgrp, "nw")),
146 _dset_opinion(this->create_dset("opinion", _dgrp_nw,
147 {boost::num_vertices(_nw)})),
149 {
150 this->_log->debug("Constructing the Opinionet Model ...");
151
152 // Initialize the network properties
153 this->initialize_properties();
154
155 this->_log->info(
156 "Initialized network with {} vertices and {} edges. Directed: {}",
157 boost::num_vertices(_nw),
158 boost::num_edges(_nw),
159 boost::is_directed(_nw)
160 );
161
162 // Mark the datasets as vertex properties and add dimensions and
163 // coordinates.
164 _dset_opinion->add_attribute("is_vertex_property", true);
165 _dset_opinion->add_attribute("dim_name__1", "vertex_idx");
166 _dset_opinion->add_attribute("coords_mode__vertex_idx", "trivial");
167
168 if (_rewire == Rewiring::RewiringOff) {
170 this->_log->debug("Network saved.");
171 }
172
173 else {
174 // Write the vertex data once, as it does not change with time
175 auto _dset_vertices = _dgrp_nw->open_dataset(
176 "_vertices", {boost::num_vertices(_nw)}
177 );
178 auto [v, v_end] = boost::vertices(_nw);
179 _dset_vertices->write(
180 v, v_end,
181 [&](auto vd){
182 return boost::get(boost::vertex_index_t(), _nw, vd);
183 }
184 );
185 _dset_vertices->add_attribute("dim_name__0", "vertex_idx");
186 _dset_vertices->add_attribute(
187 "coords_mode__vertex_idx", "trivial"
188 );
189 }
190
191 }
const std::shared_ptr< DataGroup > _hdfgrp
The HDF group this model instance should write its data to.
Definition model.hh:176
const Config _cfg
Config node belonging to this model instance.
Definition model.hh:158
const std::shared_ptr< spdlog::logger > _log
The (model) logger.
Definition model.hh:164
std::shared_ptr< DataSet > create_dset(const std::string name, const std::shared_ptr< DataGroup > &hdfgrp, std::vector< hsize_t > add_write_shape, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
Create a new dataset within the given group.
Definition model.hh:752
Rewiring initialize_rewiring()
Definition Opinionet.hh:219
const double _tolerance
Definition Opinionet.hh:109
const double _susceptibility
Definition Opinionet.hh:110
const std::shared_ptr< DataSet > _dset_edge_weights
Definition Opinionet.hh:119
NWType _nw
Network and model dynamics parameters.
Definition Opinionet.hh:108
const Opinion_space_type _opinion_space
Definition Opinionet.hh:104
std::shared_ptr< DataSet > create_edge_weight_dset()
Definition Opinionet.hh:285
Opinion_space_type initialize_opinion_space()
Definition Opinionet.hh:208
const std::shared_ptr< DataSet > _dset_opinion
Definition Opinionet.hh:118
const double _weighting
Definition Opinionet.hh:111
void initialize_properties()
Definition Opinionet.hh:228
const std::shared_ptr< DataGroup > _dgrp_nw
Definition Opinionet.hh:117
NWType initialize_nw() const
Definition Opinionet.hh:274
Model< Opinionet, OpinionetTypes > Base
The base model type.
Definition Opinionet.hh:85
Interaction_type initialize_interaction()
Definition Opinionet.hh:197
const Interaction_type _interaction
Modes.
Definition Opinionet.hh:103
const Rewiring _rewire
Definition Opinionet.hh:105
std::uniform_real_distribution< double > _uniform_prob_distr
A uniform probability distribution.
Definition Opinionet.hh:114
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
std::shared_ptr< HDFGroup > create_graph_group(const Graph &g, const std::shared_ptr< HDFGroup > &parent_grp, const std::string &name)
Definition graph_utils.hh:291
void save_graph(const Graph &g, const std::shared_ptr< HDFGroup > &grp)
Write function for a boost::Graph.
Definition graph_utils.hh:332

Member Function Documentation

◆ create_edge_weight_dset()

template<typename NWType = NetworkUndirected>
std::shared_ptr< DataSet > Utopia::Models::Opinionet::Opinionet< NWType >::create_edge_weight_dset ( )
inlineprivate
285 {
286 if constexpr (Utils::is_directed<NWType>()) {
287 return this->create_dset(
288 "edge_weights", _dgrp_nw, {boost::num_edges(_nw)}
289 );
290 }
291 else {
292 return 0;
293 }
294 }

◆ initialize_interaction()

template<typename NWType = NetworkUndirected>
Interaction_type Utopia::Models::Opinionet::Opinionet< NWType >::initialize_interaction ( )
inlineprivate
197 {
198 if (get_as<std::string>("interaction_function", this->_cfg)
199 == "Deffuant")
200 {
201 return Interaction_type::Deffuant;
202 }
203 else {
204 return Interaction_type::HegselmannKrause;
205 }
206 }

◆ initialize_nw()

template<typename NWType = NetworkUndirected>
NWType Utopia::Models::Opinionet::Opinionet< NWType >::initialize_nw ( ) const
inlineprivate
274 {
275 this->_log->debug("Creating the network ...");
276
277 auto g = Graph::create_graph<NWType>(this->_cfg["network"], *this->_rng);
278
279 this->_log->debug("Network created.");
280
281 return g;
282 }
const std::shared_ptr< RNG > _rng
The RNG shared between models.
Definition model.hh:161

◆ initialize_opinion_space()

template<typename NWType = NetworkUndirected>
Opinion_space_type Utopia::Models::Opinionet::Opinionet< NWType >::initialize_opinion_space ( )
inlineprivate
208 {
209 if (get_as<std::string>("type", this->_cfg["opinion_space"])
210 == "discrete")
211 {
212 return Opinion_space_type::discrete;
213 }
214 else {
215 return Opinion_space_type::continuous;
216 }
217 }

◆ initialize_properties()

template<typename NWType = NetworkUndirected>
void Utopia::Models::Opinionet::Opinionet< NWType >::initialize_properties ( )
inlineprivate
228 {
229 this->_log->debug("Initializing the properties ...");
230
231 // Continuous opinion space: draw opinions from a continuous interval
232 if (_opinion_space == Opinion_space_type::continuous) {
233 const std::pair<double, double> opinion_interval =
235 "interval", this->_cfg["opinion_space"]
236 );
237
238 if (opinion_interval.first >= opinion_interval.second) {
239 throw std::invalid_argument("Error: The given opinion interval"
240 " is invalid! Specify an interval of the kind [a, b], "
241 "with a<b!");
242 }
243
244 for (const auto v : range<IterateOver::vertices>(_nw)) {
245 _nw[v].opinion = Utils::get_rand<double>(
246 opinion_interval, *this->_rng);
247 }
248 }
249 // Discrete opinion space: draw opinions from a discrete set
250 else {
251 const int opinion_values =
252 get_as<int>("num_opinions", this->_cfg["opinion_space"]);
253
254 for (const auto v : range<IterateOver::vertices>(_nw)) {
255 _nw[v].opinion =
256 Utils::get_rand<int>(
257 std::make_pair<int, int>(0, opinion_values-1),
258 *this->_rng
259 );
260 }
261 }
262
263 // For directed network: Initialize weights depending on the opinion
264 // distances. In the undirected case, weights are not required.
265 if constexpr (Utils::is_directed<NWType>()) {
266 for (const auto v : range<IterateOver::vertices>(_nw)) {
267 if (boost::out_degree(v, _nw) != 0) {
269 }
270 }
271 }
272 }
IterateOver
Over which graph entity to iterate.
Definition iterator.hh:19
decltype(auto) range(const Graph &g)
Get the iterator range over selected graph entities.
Definition iterator.hh:149
@ vertices
Iterate over vertices.
void set_and_normalize_weights(const VertexDescType v, NWType &nw, const double weighting)
Set and normalize weights according to opinion difference.
Definition utils.hh:102

◆ initialize_rewiring()

template<typename NWType = NetworkUndirected>
Rewiring Utopia::Models::Opinionet::Opinionet< NWType >::initialize_rewiring ( )
inlineprivate
219 {
220 if (get_as<bool>("rewiring", this->_cfg["network"]["edges"])) {
221 return Rewiring::RewiringOn;
222 }
223 else {
224 return Rewiring::RewiringOff;
225 }
226 }

◆ monitor()

template<typename NWType = NetworkUndirected>
void Utopia::Models::Opinionet::Opinionet< NWType >::monitor ( )
inline

Monitor model information.

Here, functions and values can be supplied to the monitor that are then available to the frontend. The monitor() function is only called if a certain emit interval has passed; thus, the performance hit is small.

330 {
331 double mean_opinion = 0.;
332 double opinion_std = 0.;
333 double min_opinion = std::numeric_limits<double>::infinity();
334 double max_opinion = -std::numeric_limits<double>::infinity();
335
336 double temp_op;
337 for (const auto v : range<IterateOver::vertices>(_nw)) {
338 temp_op = _nw[v].opinion;
340
341 if (temp_op < min_opinion) {
343 }
344
345 if (temp_op > max_opinion) {
347 }
348 }
349
350 mean_opinion /= boost::num_vertices(_nw);
351
352 for (const auto v : range<IterateOver::vertices>(_nw)) {
353 opinion_std += std::pow(_nw[v].opinion - mean_opinion, 2.);
354 }
355
356 opinion_std = std::sqrt(opinion_std / (boost::num_vertices(_nw) - 1.));
357
358 this->_monitor.set_entry("mean_opinion", mean_opinion);
359 this->_monitor.set_entry("opinion_std", opinion_std);
360 this->_monitor.set_entry("min_opinion", min_opinion);
361 this->_monitor.set_entry("max_opinion", max_opinion);
362 }
Monitor _monitor
The monitor.
Definition model.hh:188

◆ perform_step()

template<typename NWType = NetworkUndirected>
void Utopia::Models::Opinionet::Opinionet< NWType >::perform_step ( )
inline

Iterate a single step Each step consists of an opinion update and edge rewiring.

Opinion update: Apply the interaction function to a randomly chosen vertex.

Rewiring, if enabled: Rewire a random edge based on selective exposure.

310 {
312 _nw,
318 _rewire,
320 *this->_rng
321 );
322 }
void revision(NWType &nw, const double susceptibility, const double tolerance, const double weighting, const Interaction_type interaction, const Opinion_space_type opinion_space, const Rewiring rewire, std::uniform_real_distribution< double > &prob_distr, RNGType &rng)
Performs an opinion update and edge rewiring (if enabled).
Definition revision.hh:149

◆ write_data()

template<typename NWType = NetworkUndirected>
void Utopia::Models::Opinionet::Opinionet< NWType >::write_data ( )
inline

Write data.

367 {
368 // Get the vertex iterators
369 auto [v, v_end] = boost::vertices(_nw);
370
371 // Write opinions
372 _dset_opinion->write(
373 v, v_end, [this](auto vd) { return _nw[vd].opinion; }
374 );
375
376 // Write edges
377 if (_rewire == Rewiring::RewiringOn){
378 // Adaptor tuple that allows to save the edge data
379 const auto get_edge_data = std::make_tuple(
380 std::make_tuple("_edges", "type",
381 std::make_tuple("source",
382 [](auto& ed, auto& _nw) {
383 return boost::get(
384 boost::vertex_index_t(), _nw,
385 boost::source(ed, _nw));
386 }
387 ),
388 std::make_tuple("target",
389 [](auto& ed, auto& _nw) {
390 return boost::get(
391 boost::vertex_index_t(), _nw,
392 boost::target(ed, _nw));
393 }
394 )
395 )
396 );
397 // Save the edge data using the current time as label.
399 _nw, _dgrp_nw, std::to_string(this->get_time()), get_edge_data
400 );
401 }
402
403 // Write edge weights
404 if constexpr (Utils::is_directed<NWType>()) {
405
406 auto [e, e_end] = boost::edges(_nw);
407
408 _dset_edge_weights->write(
409 e, e_end, [this](auto ed) { return _nw[ed].weight; }
410 );
411 }
412 }
Time get_time() const
Return the current time of this model.
Definition model.hh:393
void save_edge_properties(Graph &&g, const std::shared_ptr< HDFGroup > &nw_grp, const std::string &label, const std::tuple< Adaptors... > &adaptor_tuple)
Definition graph_utils.hh:573

Member Data Documentation

◆ _dgrp_nw

template<typename NWType = NetworkUndirected>
const std::shared_ptr<DataGroup> Utopia::Models::Opinionet::Opinionet< NWType >::_dgrp_nw
private

◆ _dset_edge_weights

template<typename NWType = NetworkUndirected>
const std::shared_ptr<DataSet> Utopia::Models::Opinionet::Opinionet< NWType >::_dset_edge_weights
private

◆ _dset_opinion

template<typename NWType = NetworkUndirected>
const std::shared_ptr<DataSet> Utopia::Models::Opinionet::Opinionet< NWType >::_dset_opinion
private

◆ _interaction

template<typename NWType = NetworkUndirected>
const Interaction_type Utopia::Models::Opinionet::Opinionet< NWType >::_interaction
private

◆ _nw

template<typename NWType = NetworkUndirected>
NWType Utopia::Models::Opinionet::Opinionet< NWType >::_nw
private

Network and model dynamics parameters.

◆ _opinion_space

template<typename NWType = NetworkUndirected>
const Opinion_space_type Utopia::Models::Opinionet::Opinionet< NWType >::_opinion_space
private

◆ _rewire

template<typename NWType = NetworkUndirected>
const Rewiring Utopia::Models::Opinionet::Opinionet< NWType >::_rewire
private

◆ _susceptibility

template<typename NWType = NetworkUndirected>
const double Utopia::Models::Opinionet::Opinionet< NWType >::_susceptibility
private

◆ _tolerance

template<typename NWType = NetworkUndirected>
const double Utopia::Models::Opinionet::Opinionet< NWType >::_tolerance
private

◆ _uniform_prob_distr

template<typename NWType = NetworkUndirected>
std::uniform_real_distribution<double> Utopia::Models::Opinionet::Opinionet< NWType >::_uniform_prob_distr
private

A uniform probability distribution.

◆ _weighting

template<typename NWType = NetworkUndirected>
const double Utopia::Models::Opinionet::Opinionet< NWType >::_weighting
private

The documentation for this class was generated from the following file: