creativity  v1.3.0
Agent-based model of creativity and piracy
Creativity.hpp
1 #pragma once
2 #include <eris/Good.hpp>
3 #include <eris/noncopyable.hpp>
4 #include "creativity/CreativitySettings.hpp"
5 #include "creativity/state/Storage.hpp"
6 #include <cstdint>
7 #include <mutex>
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 namespace creativity { class Book; }
13 
15 namespace creativity {
16 
22 class Creativity : private eris::noncopyable {
23  public:
25  Creativity() = default;
26 
28  std::shared_ptr<eris::Simulation> sim;
29 
31  eris::SharedMember<eris::Good> money;
32 
37 
42  CreativitySettings& set();
43 
52  template <class T, typename... Args>
53  void write(Args&&... args) {
54  auto new_storage = state::Storage::create<T>(set_, std::forward<Args>(args)...);
55  auto spair = storage();
56  auto &old_storage = spair.first;
57  if (old_storage) {
58  for (const auto &state : *old_storage) {
59  new_storage->push_back(state);
60  // Don't let the old storage get ahead of the new storage (we don't want to
61  // pointlessly slam a bunch of things into memory).
62  new_storage->flush(false);
63  }
64  }
65 
66  spair.first = new_storage;
67  }
68 
76  template <class T, typename... Args>
77  void read(Args&&... args) {
78  if (setup_sim_) throw std::logic_error("Creativity: attempt to read() after calling setup()");
79  storage().first = state::Storage::create<T>(set_, std::forward<Args>(args)...);
80  setup_read_ = true;
81  }
82 
94  void checkParameters();
95 
102  void setup();
103 
108  void run();
109 
115  static double boundaryFromDensity(uint32_t readers, uint32_t dimensions, double density);
116 
122  static double densityFromBoundary(uint32_t readers, uint32_t dimensions, double boundary);
123 
127  double densityFromBoundary() const;
128 
133  bool piracy() const;
134 
141  bool policyActive() const;
142 
148  bool publicSharingActive() const;
149 
155  bool publicVotingActive() const;
156 
162  bool catchPiratesActive() const;
163 
170  double policyTaxes() const;
171 
175  static double policyTaxes(const CreativitySettings &s);
176 
181  double disposableIncome() const;
182 
188  double priorWeight() const;
189 
214  double meanInitialQuality() const;
215 
223  std::pair<std::vector<eris::SharedMember<Book>>&, std::unique_lock<std::mutex>> newBooks();
224 
232  std::pair<std::shared_ptr<state::Storage>&, std::unique_lock<std::mutex>> storage();
233 
244  template <typename Iter, typename = typename std::enable_if<std::is_arithmetic<
245  typename std::iterator_traits<Iter>::value_type>::value>::type>
246  static double evalPolynomial(double x, Iter it, Iter end) {
247  double v = 0;
248  double xi = 1;
249  for (; it != end; ++it) {
250  v += *it * xi;
251  xi *= x;
252  if (xi == 0) break;
253  }
254  return v;
255  }
256 
261  template <typename Container, typename = typename std::enable_if<std::is_arithmetic<
262  typename Container::value_type>::value>::type>
263  static double evalPolynomial(double x, const Container &cont) {
264  return evalPolynomial(x, cont.begin(), cont.end());
265  }
266 
270  unsigned long market_books{0};
271 
275  double market_books_avg{0.0};
276 
277  protected:
281  void createPiracyNetwork();
282 
283  private:
284  // True if this is a live simulation. Exclusive of setup_read_.
285  bool setup_sim_{false};
286 
287  // True if this has loaded a previously simulation data file. Exclusive of setup_sim_.
288  bool setup_read_{false};
289 
290  // The actual, non-const settings (the public `.parameters` is a const reference to this)
291  CreativitySettings set_;
292 
298  std::vector<eris::SharedMember<Book>> new_books_;
299 
303  std::list<unsigned int> mkt_books_;
304 
308  std::shared_ptr<state::Storage> storage_;
309 
310  // Mutex controlling .storage_ and new_books_ access
311  std::mutex storage_mutex_, new_books_mutex_;
312 };
313 
314 }
double meanInitialQuality() const
Returns the mean quality of books created with initial parameter distributions.
bool publicSharingActive() const
Returns true if public sharing is an enabled policy response and it is active in the simulation (i...
Primary namespace for all Creativity library code.
Definition: config.hpp:4
void write(Args &&... args)
Creates a new Storage object with any type of StorageBackend class.
Definition: Creativity.hpp:53
bool policyActive() const
Returns true if a policy response exists yet in the simulation.
static double boundaryFromDensity(uint32_t readers, uint32_t dimensions, double density)
Static method that calculates a boundary given a number of readers, dimensions, and a desired density...
static double evalPolynomial(double x, Iter it, Iter end)
Evaluates a polynomial at the value x.
Definition: Creativity.hpp:246
const CreativitySettings & parameters
Simulation parameters that are used to configure the simulation when calling setup().
Definition: Creativity.hpp:36
eris::SharedMember< eris::Good > money
The money good. Will be empty until setup() is called.
Definition: Creativity.hpp:31
bool piracy() const
Returns true if piracy exists yet in the simulation.
void checkParameters()
Checks .parameters to make sure that all configured values are valid, throwing an exception if any in...
double policyTaxes() const
Returns the per-user policies taxes currently in effect.
bool publicVotingActive() const
Returns true if public sharing with voting is an enabled policy response and it is active in the simu...
double densityFromBoundary() const
Returns the value of densityFromBoundary called with the current readers, dimensions, and boundary settings.
Creativity()=default
Default constructor.
unsigned long market_books
Stores the number of on-market books.
Definition: Creativity.hpp:270
double market_books_avg
Stores the average number of on-market books over the past creation_time + 1 periods.
Definition: Creativity.hpp:275
bool catchPiratesActive() const
Returns true if getting caught for piracy is a configured policy response and it is active in the sim...
Central class for a creativity simulation; this class handles setting up the simulation according to ...
Definition: Creativity.hpp:22
double priorWeight() const
Returns the prior multiplier currently in effect.
std::shared_ptr< eris::Simulation > sim
The simulation object.
Definition: Creativity.hpp:28
std::pair< std::vector< eris::SharedMember< Book > > &, std::unique_lock< std::mutex > > newBooks()
Establishes a lock on the new books storage and returns a pair consisting of the new books reference ...
void run()
Runs one iteration of the simulation.
static double evalPolynomial(double x, const Container &cont)
Evaluates a polynomial at the value x.
Definition: Creativity.hpp:263
Simulation parameters that are used to configure the simulation when calling setup().
Definition: CreativitySettings.hpp:10
double disposableIncome() const
Returns the level of exogenous, disposable income, i.e.
void createPiracyNetwork()
Sets up the piracy network.
void read(Args &&... args)
Opens a creativity simulation storage source for reading via storage().
Definition: Creativity.hpp:77
void setup()
Creates the Simulation object and adds the configured number of readers plus some base members (such ...
std::pair< std::shared_ptr< state::Storage > &, std::unique_lock< std::mutex > > storage()
Establishes a lock on the storage object and returns a pair consisting of the storage object and the ...