CYCLUS
resource_exchange.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_RESOURCE_EXCHANGE_H_
2 #define CYCLUS_SRC_RESOURCE_EXCHANGE_H_
3 
4 #include <algorithm>
5 #include <functional>
6 #include <set>
7 
8 #include "bid_portfolio.h"
9 #include "context.h"
10 #include "exchange_context.h"
11 #include "product.h"
12 #include "material.h"
13 #include "request_portfolio.h"
14 #include "trader.h"
15 #include "trader_management.h"
16 
17 namespace cyclus {
18 
19 /// @brief Preference adjustment method helpers to convert from templates to the
20 /// Agent inheritance hierarchy
21 template<class T>
22 inline static void AdjustPrefs(Agent* m, typename PrefMap<T>::type& prefs) {}
23 inline static void AdjustPrefs(Agent* m, PrefMap<Material>::type& prefs) {
24  m->AdjustMatlPrefs(prefs);
25 }
26 inline static void AdjustPrefs(Agent* m, PrefMap<Product>::type& prefs) {
27  m->AdjustProductPrefs(prefs);
28 }
29 inline static void AdjustPrefs(Trader* t, PrefMap<Material>::type& prefs) {
30  t->AdjustMatlPrefs(prefs);
31 }
32 inline static void AdjustPrefs(Trader* t, PrefMap<Product>::type& prefs) {
33  t->AdjustProductPrefs(prefs);
34 }
35 
36 /// @class ResourceExchange
37 ///
38 /// The ResourceExchange class manages the communication for the supply and
39 /// demand of resources in a simulation. At any given timestep, there are three
40 /// main phases involved:
41 /// -# Request for Bids
42 /// Agents that demand resources of a given type post their\n
43 /// demands to the exchange
44 /// -# Response to Request for Bids
45 /// Agents that supply resources of a given type respond to\n
46 /// those requests
47 /// -# Preference Adjustment
48 /// Preferences for each request-bid pair are set, informing\n
49 /// the evenutal soluation mechanism
50 ///
51 /// For example, assuming a simulation Context, ctx, and resource type,
52 /// ResourceType:
53 ///
54 /// @code
55 /// ResourceExchange<ResourceType> exchng(ctx);
56 /// exchng.AddAllRequests();
57 /// exchng.AddAllBids();
58 /// exchng.AdjustAll();
59 /// @endcode
60 template <class T>
62  public:
63  /// @brief default constructor
64  ///
65  /// @param ctx the simulation context
67  sim_ctx_ = ctx;
68  }
69 
71  return ex_ctx_;
72  }
73 
74  /// @brief queries traders and collects all requests for bids
75  void AddAllRequests() {
76  InitTraders();
77  std::for_each(
78  traders_.begin(),
79  traders_.end(),
80  std::bind1st(std::mem_fun(&cyclus::ResourceExchange<T>::AddRequests_),
81  this));
82  }
83 
84  /// @brief queries traders and collects all responses to requests for bids
85  void AddAllBids() {
86  InitTraders();
87  std::for_each(
88  traders_.begin(),
89  traders_.end(),
90  std::bind1st(std::mem_fun(&cyclus::ResourceExchange<T>::AddBids_),
91  this));
92  }
93 
94  /// @brief adjust preferences for requests given bid responses
95  void AdjustAll() {
96  InitTraders();
97  std::set<Trader*> traders = ex_ctx_.requesters;
98  std::for_each(
99  traders.begin(),
100  traders.end(),
101  std::bind1st(
103  this));
104  }
105 
106  /// return true if this is an empty exchange (i.e., no requests exist,
107  /// therefore no bids)
108  inline bool Empty() { return ex_ctx_.bids_by_request.empty(); }
109 
110  private:
111  void InitTraders() {
112  if (traders_.size() == 0) {
113  std::set<Trader*> orig = sim_ctx_->traders();
114  std::set<Trader*>::iterator it;
115  for (it = orig.begin(); it != orig.end(); ++it) {
116  traders_.insert(*it);
117  }
118  }
119  }
120 
121  /// @brief queries a given facility agent for
122  void AddRequests_(Trader* t) {
123  std::set<typename RequestPortfolio<T>::Ptr> rp = QueryRequests<T>(t);
124  typename std::set<typename RequestPortfolio<T>::Ptr>::iterator it;
125  for (it = rp.begin(); it != rp.end(); ++it) {
126  ex_ctx_.AddRequestPortfolio(*it);
127  }
128  }
129 
130  /// @brief queries a given facility agent for
131  void AddBids_(Trader* t) {
132  std::set<typename BidPortfolio<T>::Ptr> bp =
133  QueryBids<T>(t, ex_ctx_.commod_requests);
134  typename std::set<typename BidPortfolio<T>::Ptr>::iterator it;
135  for (it = bp.begin(); it != bp.end(); ++it) {
136  ex_ctx_.AddBidPortfolio(*it);
137  }
138  }
139 
140  /// @brief allows a trader and its parents to adjust any preferences in the
141  /// system
142  void AdjustPrefs_(Trader* t) {
143  typename PrefMap<T>::type& prefs = ex_ctx_.trader_prefs[t];
144  AdjustPrefs(t, prefs);
145  Agent* m = t->manager()->parent();
146  while (m != NULL) {
147  AdjustPrefs(m, prefs);
148  m = m->parent();
149  }
150  }
151 
152  struct trader_compare {
153  bool operator()(Trader* lhs, Trader* rhs) const {
154  int left = lhs->manager()->id();
155  int right = rhs->manager()->id();
156  if (left != right) {
157  return left < right;
158  } else {
159  return lhs < rhs;
160  }
161  }
162  };
163 
164  // this sorts traders (and results in iteration...) based on traders'
165  // manager id. Iterating over traders in this order helps increase the
166  // determinism of Cyclus overall. This allows all traders' resource
167  // exchange functions are called in a much closer to deterministic order.
168  std::set<Trader*, trader_compare> traders_;
169 
170  Context* sim_ctx_;
171  ExchangeContext<T> ex_ctx_;
172 };
173 
174 } // namespace cyclus
175 
176 #endif // CYCLUS_SRC_RESOURCE_EXCHANGE_H_
const std::set< Trader * > & traders() const
Definition: context.h:171
The ResourceExchange class manages the communication for the supply and demand of resources in a simu...
ExchangeContext< T > & ex_ctx()
static void AdjustPrefs(Agent *m, typename PrefMap< T >::type &prefs)
Preference adjustment method helpers to convert from templates to the Agent inheritance hierarchy...
bool Empty()
return true if this is an empty exchange (i.e., no requests exist, therefore no bids) ...
virtual const int id() const
The agent instance&#39;s unique ID within a simulation.
Definition: agent.h:354
ResourceExchange(Context *ctx)
default constructor
A simple API for agents that wish to exchange resources in the simulation.
Definition: trader.h:24
The ExchangeContext is designed to provide an ease-of-use interface for querying and reaggregating in...
void AddAllRequests()
queries traders and collects all requests for bids
std::map< Request< T > *, std::map< Bid< T > *, double > > type
virtual void AdjustMatlPrefs(PrefMap< Material >::type &prefs)
default implementation for material preferences.
Definition: trader.h:57
virtual Agent * manager()
Definition: trader.h:28
void AddAllBids()
queries traders and collects all responses to requests for bids
void AdjustAll()
adjust preferences for requests given bid responses
virtual void AdjustProductPrefs(PrefMap< Product >::type &prefs)
default implementation for material preferences.
Definition: agent.h:330
virtual void AdjustProductPrefs(PrefMap< Product >::type &prefs)
default implementation for material preferences.
Definition: trader.h:60
Agent * parent() const
Returns parent of this agent. Returns NULL if the agent has no parent.
Definition: agent.h:375
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:130
The abstract base class used by all types of agents that live and interact in a simulation.
Definition: agent.h:51
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
virtual void AdjustMatlPrefs(PrefMap< Material >::type &prefs)
default implementation for material preferences.
Definition: agent.h:327