creativity  v1.3.0
Agent-based model of creativity and piracy
Reader.hpp
1 #pragma once
2 #include <eris/Optimize.hpp>
3 #include <eris/WrappedPositional.hpp>
4 #include <eris/Agent.hpp>
5 #include <eris/Market.hpp>
6 #include "creativity/Book.hpp"
7 #include "creativity/BookCopy.hpp" // IWYU pragma: keep
8 #include "creativity/belief/ProfitStream.hpp"
9 #include "creativity/belief/Demand.hpp"
10 #include <vector>
11 #include <unordered_set>
12 #include <forward_list>
13 #include <functional>
14 #include <map>
15 #include <unordered_map>
16 #include <set>
17 #include <type_traits>
18 
19 namespace creativity {
20 
21 class Creativity;
22 
24 namespace belief { class Profit; }
25 
141 class Reader : public eris::WrappedPositional<eris::Agent>,
142  public virtual eris::interopt::Begin,
143  public virtual eris::interopt::OptApply,
144  public virtual eris::intraopt::Initialize,
145  public virtual eris::intraopt::OptApplyReset
146 {
147  public:
148  Reader() = delete;
149 
157  Reader(
159  const eris::Position &pos
160  );
161 
170  template <typename Container>
171  typename std::enable_if<std::is_base_of<Book, typename Container::value_type::member_type>::value, double>::type
172  u(double money, const Container &books) const {
173  double u = money - numBooksPenalty(books.size());
174  for (auto &book : books) {
175  u += uBook(book);
176  }
177  return u;
178  }
179 
183  template <typename Container>
184  typename std::enable_if<std::is_same<eris::eris_id_t, typename Container::value_type>::value, double>::type
185  u(double money, const Container &books) const {
186  double u = money - numBooksPenalty(books.size());
187  for (auto &book_id : books) {
188  u += uBook(simGood<Book>(book_id));
189  }
190  return u;
191  }
192 
196  double u(double money, const std::unordered_map<eris::SharedMember<Book>, std::reference_wrapper<BookCopy>> &books) const;
197 
201  const double& u() const;
202 
206  const double& uLifetime() const;
207 
214  const std::unordered_map<eris::SharedMember<Book>, BookCopy>& library() const;
215 
223  const std::unordered_map<eris::SharedMember<Book>, std::reference_wrapper<BookCopy>>& newBooks() const;
224 
229  const std::set<eris::SharedMember<Book>>& wrote() const;
230 
249  virtual double uBook(const eris::SharedMember<Book> &b) const;
250 
252  virtual double uBook(const eris::SharedMember<Book> &b, double quality) const;
253 
258  virtual double quality(const eris::SharedMember<Book> &b) const;
259 
287  void distancePenaltyPolynomial(std::vector<double> coef);
288 
293  const std::vector<double>& distancePenaltyPolynomial() const;
294 
297  static constexpr std::initializer_list<double> default_distance_penalty_polynomial{0, 0, 3};
298 
302  double distancePenalty(double distance) const;
303 
322  void numBooksPenaltyPolynomial(std::vector<double> coef);
323 
328  const std::vector<double>& numBooksPenaltyPolynomial() const;
329 
333  static constexpr std::initializer_list<double> default_num_books_penalty_polynomial{0, 0, 1};
334 
343  virtual double numBooksPenalty(unsigned long books) const;
344 
364  double creation_shape = 0.0;
365 
379  double creation_scale = 10.0;
380 
388  double creationQuality(double effort) const;
389 
398  double creationEffort(double quality) const;
399 
409  static double creationQuality(double shape, double scale, double effort);
410 
420  static double creationEffort(double shape, double scale, double effort);
421 
423  const belief::Profit& profitBelief() const;
425  const belief::Profit& profitExtrapBelief() const;
429  bool profitExtrapBeliefDiffers() const;
446  const belief::ProfitStream& profitStreamBelief(unsigned int age, bool usable = false) const;
447 
454  bool usableBelief(const eris::learning::BayesianLinear &model) const;
455 
462  const std::map<unsigned int, belief::ProfitStream>& profitStreamBeliefs() const;
463 
468  static const std::vector<unsigned int> profit_stream_ages;
469 
471  const belief::Demand& demandBelief() const;
472 
478  double piracyCost() const;
479 
481  const std::unordered_set<eris::SharedMember<Reader>>& friends() const;
482 
494  bool addFriend(eris::SharedMember<Reader> new_pal, bool recurse = true);
495 
505  bool removeFriend(const eris::SharedMember<Reader> &old_pal, bool recurse = true);
506 
515  void alterUtility(double amount);
516 
518  void interBegin() override;
519 
526  void interOptimize() override;
527 
535  void interApply() override;
536 
538  void intraInitialize() override;
539 
559  void intraOptimize() override;
561  void intraApply() override;
563  void intraReset() override;
564 
565  protected:
568 
569  std::shared_ptr<belief::Profit> profit_belief_,
572 
576  std::map<unsigned int, belief::ProfitStream> profit_stream_beliefs_;
577 
579  std::unordered_set<eris::SharedMember<Reader>> friends_;
580 
599  void updateBeliefs();
600 
612  void updateDemandBelief();
613 
625  void updateProfitBelief();
626 
636  void updateProfitStreamBelief();
637 
643  void registerAuthoredBook(eris::SharedMember<Book> book);
647  void registerMarketUpdate(eris::SharedMember<Book> book);
648 #ifndef DOXYGEN_SHOULD_SEE_THIS
649  // The protected register* methods are called from Book:
650  friend void Book::added();
651  friend void Book::setMarket(eris::SharedMember<BookMarket>);
652  friend void Book::weakDepRemoved(eris::SharedMember<eris::Member>, eris::eris_id_t);
653 #endif
654 
655  private:
656  std::vector<double> dist_penalty_poly_;
657  std::vector<double> nbooks_penalty_poly_;
659  std::unordered_map<eris::SharedMember<Book>, BookCopy> library_;
661  std::unordered_map<eris::SharedMember<Book>, std::reference_wrapper<BookCopy>> library_new_;
662 
663  std::unordered_set<eris::SharedMember<Book>>
665  wrote_market_,
667  book_cache_,
672  book_cache_market_;
675  std::unordered_map<eris::SharedMember<Book>, BookCopy::Status> reserved_books_;
676 
680  std::set<eris::SharedMember<Book>> wrote_;
681 
683  std::forward_list<eris::Market::Reservation> reservations_;
685  double reserved_piracy_cost_ = 0.0;
686 
687  // Track current and cumulative utility:
688  double u_curr_ = 0, u_lifetime_ = 0;
689 
690  // Book prices for the upcoming period. If a book currently on the market isn't in here,
691  // or has a negative price, it'll be removed from the market
692  std::unordered_map<eris::SharedMember<Book>, double> new_prices_;
693 
694  // True in the period in which creation started, false otherwise.
695  bool create_starting_ = false;
696  int create_countdown_ = -1; // the number of periods remaining until the book is finished; new books can only be initiated when this is -1.
697  double create_effort_ = 0, create_quality_ = 0, create_price_ = 0;
698  // The author's position at the time of creation; the final position will be this plus noise
699  eris::Position create_position_;
700 
701 };
702 
703 }
Primary namespace for all Creativity library code.
Definition: config.hpp:4
This class represents an author&#39;s belief about the per-period demand for books.
Definition: Demand.hpp:36
std::map< unsigned int, belief::ProfitStream > profit_stream_beliefs_
Profit stream beliefs for books on market for various lengths of time.
Definition: Reader.hpp:576
std::shared_ptr< belief::Profit > profit_belief_extrap_
Beliefs about lifetime book profits using profit stream expectations.
Definition: Reader.hpp:569
Creativity & creativity_
The Creativity object that owns the simulation this reader belongs to.
Definition: Reader.hpp:567
A Reader is an agent that both consumes previously unread books and potentially writes new books and ...
Definition: Reader.hpp:141
static const std::vector< unsigned int > profit_stream_ages
The different ages that will be considered for profit_stream_beliefs.
Definition: Reader.hpp:468
void added() override
When the Book is added to the simulation it also creates an associated BookMarket object that handles...
void setMarket(eris::SharedMember< BookMarket > market)
Sets a new market for this book.
Status
The status a book copy can have.
Definition: BookCopy.hpp:10
belief::Demand demand_belief_
Belief about per-period demand.
Definition: Reader.hpp:571
Container class storing the data associated with a reader&#39;s copy of a book.
Definition: BookCopy.hpp:7
This class represents an author&#39;s belief about the lifetime profitability of a work.
Definition: Profit.hpp:33
void weakDepRemoved(eris::SharedMember< eris::Member >, eris::eris_id_t old) override
When the market is removed from the simulation, record it by clearing the stored market.
Central class for a creativity simulation; this class handles setting up the simulation according to ...
Definition: Creativity.hpp:22
std::enable_if< std::is_same< eris::eris_id_t, typename Container::value_type >::value, double >::type u(double money, const Container &books) const
Same as the above, but takes a container of eris_id_t values of books rather than SharedMember<Book> ...
Definition: Reader.hpp:185
std::enable_if< std::is_base_of< Book, typename Container::value_type::member_type >::value, double >::type u(double money, const Container &books) const
Takes a money value and a container of SharedMember<Book> objects and returns the utility for that se...
Definition: Reader.hpp:172
This class represents an author&#39;s belief about the lifetime profits of a book based on partial lifeti...
Definition: ProfitStream.hpp:31
std::unordered_set< eris::SharedMember< Reader > > friends_
The set of friends of this reader.
Definition: Reader.hpp:579